20#include <opencalcstyleexport.h>
24#include <kspread_cell.h>
25#include <kspread_doc.h>
26#include <kspread_format.h>
27#include <kspread_sheet.h>
28#include <kspread_style.h>
29#include <kspread_style_manager.h>
33using namespace KSpread;
35OpenCalcStyles::OpenCalcStyles()
37 m_cellStyles.setAutoDelete(
true );
38 m_columnStyles.setAutoDelete(
true );
39 m_numberStyles.setAutoDelete(
true );
40 m_rowStyles.setAutoDelete(
true );
41 m_sheetStyles.setAutoDelete(
true );
43 m_fontList.setAutoDelete(
true );
46OpenCalcStyles::~OpenCalcStyles()
50void OpenCalcStyles::writeStyles( TQDomDocument & doc, TQDomElement & autoStyles )
52 addColumnStyles( doc, autoStyles );
53 addRowStyles( doc, autoStyles );
54 addSheetStyles( doc, autoStyles );
55 addNumberStyles( doc, autoStyles );
56 addCellStyles( doc, autoStyles );
59void OpenCalcStyles::writeFontDecl( TQDomDocument & doc, TQDomElement & fontDecls )
61 TQFont * f = m_fontList.first();
64 TQDomElement fontDecl = doc.createElement(
"style:font-decl" );
66 fontDecl.setAttribute(
"style:name", f->family() );
67 fontDecl.setAttribute(
"fo:font-family", f->family() );
68 fontDecl.setAttribute(
"style:font-pitch", ( f->fixedPitch() ?
"fixed" :
"variable" ) );
74 fontDecls.appendChild( fontDecl );
76 f = m_fontList.next();
80void OpenCalcStyles::addFont( TQFont
const & font,
bool def )
85 TQFont * f = m_fontList.first();
88 if ( f->family() == font.family() )
91 f = m_fontList.next();
94 f =
new TQFont( font );
95 m_fontList.append( f );
98TQString OpenCalcStyles::cellStyle( CellStyle
const & cs )
100 CellStyle * t = m_cellStyles.first();
103 if ( CellStyle::isEqual( t, cs ) )
106 t = m_cellStyles.next();
112 m_cellStyles.append( t );
114 t->name = TQString(
"ce%1" ).arg( m_cellStyles.count() );
119TQString OpenCalcStyles::columnStyle( ColumnStyle
const & cs )
121 ColumnStyle * t = m_columnStyles.first();
124 if ( ColumnStyle::isEqual( t, cs ) )
127 t = m_columnStyles.next();
130 t =
new ColumnStyle();
133 m_columnStyles.append( t );
135 t->name = TQString(
"co%1" ).arg( m_columnStyles.count() );
140TQString OpenCalcStyles::numberStyle( NumberStyle
const & )
145TQString OpenCalcStyles::rowStyle( RowStyle
const & rs )
147 RowStyle * t = m_rowStyles.first();
150 if ( RowStyle::isEqual( t, rs ) )
153 t = m_rowStyles.next();
159 m_rowStyles.append( t );
161 t->name = TQString(
"ro%1" ).arg( m_rowStyles.count() );
166TQString OpenCalcStyles::sheetStyle( SheetStyle
const & ts )
168 SheetStyle * t = m_sheetStyles.first();
171 if ( SheetStyle::isEqual( t, ts ) )
174 t = m_sheetStyles.next();
177 t =
new SheetStyle();
180 m_sheetStyles.append( t );
182 t->name = TQString(
"ta%1" ).arg( m_sheetStyles.count() );
187TQString convertPenToString( TQPen
const & pen )
189 TQString s( TQString(
"%1cm solid " ).arg( pen.width() * 0.035 ) );
190 s += pen.color().name();
195void OpenCalcStyles::addCellStyles( TQDomDocument & doc, TQDomElement & autoStyles )
197 CellStyle * t = m_cellStyles.first();
200 TQDomElement ts = doc.createElement(
"style:style" );
201 ts.setAttribute(
"style:name", t->name );
202 ts.setAttribute(
"style:family",
"table-cell" );
203 ts.setAttribute(
"style:parent-style-name",
"Default" );
204 if ( t->numberStyle.length() > 0 )
205 ts.setAttribute(
"style:data-style-name", t->numberStyle );
207 TQDomElement prop = doc.createElement(
"style:properties" );
209 if ( t->font.family() != m_defaultFont.family() )
210 prop.setAttribute(
"style:font-name", t->font.family() );
212 if ( t->font.bold() != m_defaultFont.bold() )
213 prop.setAttribute(
"fo:font-weight", ( t->font.bold() ?
"bold" :
"light" ) );
215 prop.setAttribute(
"fo:font-size", TQString(
"%1pt" ).arg( t->font.pointSize() ) );
217 if ( t->font.underline() != m_defaultFont.underline() )
219 prop.setAttribute(
"style:text-underline", ( t->font.underline() ?
"single" :
"none" ) );
220 if ( t->font.underline() )
221 prop.setAttribute(
"style:text-underline-color",
"font-color" );
224 if ( t->font.italic() != m_defaultFont.italic() )
225 prop.setAttribute(
"fo:font-style", ( t->font.italic() ?
"italic" :
"none" ) );
227 if ( t->font.strikeOut() != m_defaultFont.strikeOut() )
228 prop.setAttribute(
"style:text-crossing-out", ( t->font.strikeOut() ?
"single-line" :
"none" ) );
230 if ( t->color.name() !=
"#000000" )
231 prop.setAttribute(
"fo:color", t->color.name() );
233 if ( t->bgColor.name() !=
"#ffffff" )
234 prop.setAttribute(
"fo:background-color", t->bgColor.name() );
236 if ( t->alignX != Format::Undefined )
239 if ( t->alignX == Format::Center )
241 else if ( t->alignX == Format::Right )
243 else if ( t->alignX == Format::Left )
245 prop.setAttribute(
"fo:text-align", value );
248 if ( t->alignY != Format::Bottom )
249 prop.setAttribute(
"fo:vertical-align", ( t->alignY == Format::Middle ?
"middle" :
"top" ) );
251 if ( t->indent > 0.0 )
253 prop.setAttribute(
"fo:margin-left", TQString(
"%1pt" ).arg( t->indent ) );
254 if ( t->alignX == Format::Undefined )
255 prop.setAttribute(
"fo:text-align",
"start" );
259 prop.setAttribute(
"fo:wrap-option",
"wrap" );
263 prop.setAttribute(
"fo:direction",
"ttb" );
264 prop.setAttribute(
"style:rotation-angle",
"0" );
268 prop.setAttribute(
"style:rotation-angle", TQString::number( t->angle ) );
271 prop.setAttribute(
"style:print-content",
"false" );
274 prop.setAttribute(
"style:cell-protect",
"hidden-and-protected" );
276 if ( t->notProtected && !t->hideFormula )
277 prop.setAttribute(
"style:cell-protect",
"none" );
279 if ( t->notProtected && t->hideFormula )
280 prop.setAttribute(
"style:cell-protect",
"formula-hidden" );
281 else if ( t->hideFormula )
282 prop.setAttribute(
"style:cell-protect",
"protected formula-hidden" );
283 else if ( !t->notProtected )
284 prop.setAttribute(
"style:cell-protect",
"protected" );
287 if ( ( t->left == t->right ) && ( t->left == t->top ) && ( t->left == t->bottom ) )
289 if ( ( t->left.width() != 0 ) && ( t->left.style() != TQt::NoPen ) )
290 prop.setAttribute(
"fo:border", convertPenToString( t->left ) );
294 if ( ( t->left.width() != 0 ) && ( t->left.style() != TQt::NoPen ) )
295 prop.setAttribute(
"fo:border-left", convertPenToString( t->left ) );
297 if ( ( t->right.width() != 0 ) && ( t->right.style() != TQt::NoPen ) )
298 prop.setAttribute(
"fo:border-right", convertPenToString( t->right ) );
300 if ( ( t->top.width() != 0 ) && ( t->top.style() != TQt::NoPen ) )
301 prop.setAttribute(
"fo:border-top", convertPenToString( t->top ) );
303 if ( ( t->bottom.width() != 0 ) && ( t->bottom.style() != TQt::NoPen ) )
304 prop.setAttribute(
"fo:border-bottom", convertPenToString( t->bottom ) );
307 ts.appendChild( prop );
308 autoStyles.appendChild( ts );
310 t = m_cellStyles.next();
314void OpenCalcStyles::addColumnStyles( TQDomDocument & doc, TQDomElement & autoStyles )
316 ColumnStyle * t = m_columnStyles.first();
319 TQDomElement ts = doc.createElement(
"style:style" );
320 ts.setAttribute(
"style:name", t->name );
321 ts.setAttribute(
"style:family",
"table-column" );
323 TQDomElement prop = doc.createElement(
"style:properties" );
324 if ( t->breakB != ::Style::none )
325 prop.setAttribute(
"fo:break-before", ( t->breakB == ::Style::automatic ?
"auto" :
"page" ) );
326 prop.setAttribute(
"style:column-width", TQString(
"%1cm" ).arg( t->size ) );
328 ts.appendChild( prop );
329 autoStyles.appendChild( ts );
331 t = m_columnStyles.next();
335void OpenCalcStyles::addNumberStyles( TQDomDocument & , TQDomElement & )
339void OpenCalcStyles::addRowStyles( TQDomDocument & doc, TQDomElement & autoStyles )
341 RowStyle * t = m_rowStyles.first();
344 TQDomElement ts = doc.createElement(
"style:style" );
345 ts.setAttribute(
"style:name", t->name );
346 ts.setAttribute(
"style:family",
"table-row" );
348 TQDomElement prop = doc.createElement(
"style:properties" );
349 prop.setAttribute(
"style:row-height", TQString(
"%1cm" ).arg( t->size ) );
350 if ( t->breakB != ::Style::none )
351 prop.setAttribute(
"fo:break-before", ( t->breakB == ::Style::automatic ?
"auto" :
"page" ) );
353 ts.appendChild( prop );
354 autoStyles.appendChild( ts );
356 t = m_rowStyles.next();
360void OpenCalcStyles::addSheetStyles( TQDomDocument & doc, TQDomElement & autoStyles )
362 SheetStyle * t = m_sheetStyles.first();
365 TQDomElement ts = doc.createElement(
"style:style" );
366 ts.setAttribute(
"style:name", t->name );
367 ts.setAttribute(
"style:family",
"table" );
368 ts.setAttribute(
"style:master-page-name",
"Default" );
370 TQDomElement prop = doc.createElement(
"style:properties" );
371 prop.setAttribute(
"table:display", ( t->visible ?
"true" :
"false" ) );
373 ts.appendChild( prop );
374 autoStyles.appendChild( ts );
376 t = m_sheetStyles.next();
380bool SheetStyle::isEqual( SheetStyle
const *
const t1, SheetStyle
const & t2 )
382 if ( t1->visible == t2.visible )
388CellStyle::CellStyle()
389 : color( TQt::black ),
390 bgColor( TQt::white ),
396 left ( TQt::black, 0, TQt::NoPen ),
397 right( TQt::black, 0, TQt::NoPen ),
398 top ( TQt::black, 0, TQt::NoPen ),
399 bottom( TQt::black, 0, TQt::NoPen ),
401 hideFormula( false ),
402 notProtected ( false ),
403 alignX(
Format::Undefined ),
408void CellStyle::copyData( CellStyle
const & ts )
411 numberStyle = ts.numberStyle;
413 bgColor = ts.bgColor;
416 vertical = ts.vertical;
423 hideAll = ts.hideAll;
424 hideFormula = ts.hideFormula;
425 notProtected = ts.notProtected;
430bool CellStyle::isEqual( CellStyle
const *
const t1, CellStyle
const & t2 )
432 if ( ( t1->font == t2.font ) && ( t1->numberStyle == t2.numberStyle )
433 && ( t1->color == t2.color ) && ( t1->bgColor == t2.bgColor )
434 && ( t1->alignX == t2.alignX ) && ( t1->alignY == t2.alignY )
435 && ( t1->indent == t2.indent ) && ( t1->wrap == t2.wrap )
436 && ( t1->vertical == t2.vertical ) && ( t1->angle == t2.angle )
437 && ( t1->print == t2.print ) && ( t1->left == t2.left )
438 && ( t1->right == t2.right ) && ( t1->top == t2.top )
439 && ( t1->bottom == t2.bottom ) && ( t1->hideAll == t2.hideAll )
440 && ( t1->hideFormula == t2.hideFormula ) && ( t1->notProtected == t2.notProtected )
448void CellStyle::loadData( CellStyle & cs, Cell
const *
const cell )
450 int col = cell->column();
451 int row = cell->row();
453 Format * f =
new Format( 0, cell->sheet()->doc()->styleManager()->defaultStyle() );
455 TQFont font = cell->format()->textFont( col, row );
456 if ( font != f->font() )
459 TQColor color = cell->format()->textColor( col, row );
460 if ( color != f->textColor( col, row ) )
463 TQColor bgColor = cell->bgColor( col, row );
464 if ( bgColor != f->bgColor( col, row ) )
465 cs.bgColor = bgColor;
467 if ( cell->format()->hasProperty( Format::PAlign ) || !cell->format()->hasNoFallBackProperties( Format::PAlign ) )
468 cs.alignX = cell->format()->align( col, row );
470 if ( cell->format()->hasProperty( Format::PAlignY ) || !cell->format()->hasNoFallBackProperties( Format::PAlignY ) )
471 cs.alignY = cell->format()->alignY( col, row );
473 if ( cell->format()->hasProperty( Format::PIndent ) || !cell->format()->hasNoFallBackProperties( Format::PIndent ) )
474 cs.indent = cell->format()->getIndent( col, row );
476 if ( cell->format()->hasProperty( Format::PAngle ) || !cell->format()->hasNoFallBackProperties( Format::PAngle ) )
477 cs.angle = -cell->format()->getAngle( col, row );
479 if ( cell->format()->hasProperty( Format::PMultiRow ) || !cell->format()->hasNoFallBackProperties( Format::PMultiRow ) )
480 cs.wrap = cell->format()->multiRow( col, row );
482 if ( cell->format()->hasProperty( Format::PVerticalText )
483 || !cell->format()->hasNoFallBackProperties( Format::PVerticalText ) )
484 cs.vertical = cell->format()->verticalText( col, row );
486 if ( cell->format()->hasProperty( Format::PDontPrintText )
487 || !cell->format()->hasNoFallBackProperties( Format::PDontPrintText ) )
488 cs.print = !cell->format()->getDontprintText( col, row );
490 if ( cell->format()->hasProperty( Format::PLeftBorder ) || !cell->format()->hasNoFallBackProperties( Format::PLeftBorder ) )
491 cs.left = cell->leftBorderPen( col, row );
493 if ( cell->format()->hasProperty( Format::PRightBorder ) || !cell->format()->hasNoFallBackProperties( Format::PRightBorder ) )
494 cs.right = cell->rightBorderPen( col, row );
496 if ( cell->format()->hasProperty( Format::PTopBorder ) || !cell->format()->hasNoFallBackProperties( Format::PTopBorder ) )
497 cs.top = cell->topBorderPen( col, row );
499 if ( cell->format()->hasProperty( Format::PBottomBorder ) || !cell->format()->hasNoFallBackProperties( Format::PBottomBorder ) )
500 cs.bottom = cell->bottomBorderPen( col, row );
502 if ( cell->format()->hasProperty( Format::PNotProtected ) || !cell->format()->hasNoFallBackProperties( Format::PNotProtected ) )
503 cs.notProtected = cell->format()->notProtected( col, row );
505 if ( cell->format()->hasProperty( Format::PHideAll ) || !cell->format()->hasNoFallBackProperties( Format::PHideAll ) )
506 cs.hideAll = cell->format()->isHideAll( col, row );
508 if ( cell->format()->hasProperty( Format::PHideFormula ) || !cell->format()->hasNoFallBackProperties( Format::PHideFormula ) )
509 cs.hideFormula = cell->format()->isHideFormula( col, row );
512bool NumberStyle::isEqual( NumberStyle
const *
const t1, NumberStyle
const & t2 )
514 if ( ( t1->type == t2.type ) && ( t1->pattern == t2.pattern ) )
520void ColumnStyle::copyData( ColumnStyle
const & cs )
526bool ColumnStyle::isEqual( ColumnStyle
const *
const c1, ColumnStyle
const & c2 )
528 if ( ( c1->breakB == c2.breakB ) && ( c1->size == c2.size ) )
534void RowStyle::copyData( RowStyle
const & cs )
540bool RowStyle::isEqual( RowStyle
const *
const c1, RowStyle
const & c2 )
542 if ( ( c1->breakB == c2.breakB ) && ( c1->size == c2.size ) )