filters

format.cpp

00001 /* Swinder - Portable library for spreadsheet
00002    Copyright (C) 2003-2006 Ariya Hidayat <ariya@kde.org>
00003    Copyright (C) 2006 Marijn Kruisselbrink <m.kruisselbrink@student.tue.nl>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA
00019 */
00020 
00021 #include "format.h"
00022 
00023 #include "ustring.h"
00024 
00025 
00026 using namespace Swinder;
00027 
00028 class FormatFont::Private
00029 {
00030 public:
00031   bool null : 1 ;
00032   bool bold  : 1;
00033   bool italic : 1;
00034   bool underline : 1;
00035   bool strikeout : 1;
00036   bool subscript : 1;
00037   bool superscript : 1;
00038   UString fontFamily;
00039   double fontSize;
00040   Color color;
00041 
00042   static UString defaultFont;
00043 };
00044 
00045 UString FormatFont::Private::defaultFont("Arial");
00046 
00047 FormatFont::FormatFont()
00048 {
00049   d = new FormatFont::Private();
00050   d->null        = true;
00051   d->fontFamily  = Private::defaultFont;
00052   d->fontSize    = 11;
00053   d->bold        = false;
00054   d->italic      = false;
00055   d->underline   = false;
00056   d->strikeout   = false;
00057   d->subscript   = false;
00058   d->superscript = false;
00059 }
00060 
00061 FormatFont::~FormatFont()
00062 {
00063   delete d;
00064 }
00065 
00066 FormatFont::FormatFont( const FormatFont& f )
00067 {
00068   d = new FormatFont::Private();
00069   assign( f );
00070 }
00071 
00072 FormatFont& FormatFont::operator=( const FormatFont& f )
00073 {
00074   return assign( f );
00075 }
00076 
00077 FormatFont& FormatFont::assign( const FormatFont& f )
00078 {
00079   d->null        = f.isNull();
00080   d->fontFamily  = f.fontFamily();
00081   d->fontSize    = f.fontSize();
00082   d->color       = f.color();
00083   d->bold        = f.bold();
00084   d->italic      = f.italic();
00085   d->underline   = f.underline();
00086   d->strikeout   = f.strikeout();
00087   d->subscript   = f.subscript();
00088   d->superscript = f.superscript();
00089   
00090   return *this;
00091 }
00092 
00093 bool FormatFont::isNull() const
00094 {
00095   return d->null;
00096 }
00097 
00098 const UString& FormatFont::fontFamily() const
00099 {
00100   return d->fontFamily;
00101 }
00102 
00103 void FormatFont::setFontFamily( const UString& fontFamily )
00104 {
00105   d->fontFamily = fontFamily;
00106   d->null = false;
00107 }
00108 
00109 double FormatFont::fontSize() const
00110 {
00111   return d->fontSize;
00112 }
00113   
00114 void FormatFont::setFontSize( double fs )
00115 {
00116   d->fontSize = fs;
00117   d->null = false;
00118 }
00119 
00120 Color FormatFont::color() const
00121 {
00122   return d->color;
00123 }
00124 
00125 void FormatFont::setColor( const Color& c )
00126 {
00127   d->color = c;
00128   d->null = false;
00129 }
00130 
00131 bool FormatFont::bold() const
00132 {
00133   return d->bold;
00134 }
00135 
00136 void FormatFont::setBold( bool b )
00137 {
00138   d->bold = b;
00139   d->null = false;
00140 }
00141 
00142 bool FormatFont::italic() const
00143 {
00144   return d->italic;
00145 }
00146 
00147 void FormatFont::setItalic( bool b )
00148 {
00149   d->italic = b;
00150   d->null = false;
00151 }
00152 
00153 bool FormatFont::underline() const
00154 {
00155   return d->underline;
00156 }
00157 
00158 void FormatFont::setUnderline( bool b )
00159 {
00160   d->underline = b;
00161   d->null = false;
00162 }
00163 
00164 bool FormatFont::strikeout() const
00165 {
00166   return d->strikeout;
00167 }
00168 
00169 void FormatFont::setStrikeout( bool s )
00170 {
00171   d->strikeout = s;
00172   d->null = false;
00173 }
00174 
00175 bool FormatFont::subscript() const
00176 {
00177   return d->subscript;
00178 }
00179 
00180 void FormatFont::setSubscript( bool s )
00181 {
00182   d->subscript = s;
00183   d->null = false;
00184 
00185   // mutually exclusive
00186   if( d->subscript && d->superscript )
00187     d->superscript = false;
00188 }
00189 
00190 bool FormatFont::superscript() const
00191 {
00192   return d->superscript;
00193 }
00194 
00195 void FormatFont::setSuperscript( bool s )
00196 {
00197   d->superscript = s;
00198   d->null = false;
00199 
00200   // mutually exclusive
00201   if( d->superscript && d->subscript )
00202     d->subscript = false;
00203 }
00204 
00205 bool FormatFont::operator==(const FormatFont& font) const
00206 {
00207     return 
00208     d->bold == font.d->bold &&
00209     d->italic == font.d->italic &&
00210     d->underline == font.d->underline &&
00211     d->strikeout == font.d->strikeout &&
00212     d->subscript == font.d->subscript &&
00213     d->superscript == font.d->superscript &&
00214     d->fontFamily == font.d->fontFamily &&
00215     d->fontSize == font.d->fontSize &&
00216     d->color == font.d->color;
00217 }
00218 
00219 bool FormatFont::operator!=(const FormatFont& font) const
00220 {
00221     return 
00222     d->bold != font.d->bold ||
00223     d->italic != font.d->italic ||
00224     d->underline != font.d->underline ||
00225     d->strikeout != font.d->strikeout ||
00226     d->subscript != font.d->subscript ||
00227     d->superscript != font.d->superscript ||
00228     d->fontFamily != font.d->fontFamily ||
00229     d->fontSize != font.d->fontSize ||
00230     d->color != font.d->color;
00231 }
00232 
00233 class FormatAlignment::Private
00234 {
00235 public:
00236   bool null;
00237   unsigned alignX;
00238   unsigned alignY;
00239   bool wrap;
00240   unsigned indentLevel;
00241   unsigned rotationAngle;
00242 };
00243 
00244 FormatAlignment::FormatAlignment()
00245 {
00246   d = new FormatAlignment::Private;
00247   d->null          = true;
00248   d->alignX        = Format::Left;
00249   d->alignY        = Format::Middle;
00250   d->wrap          = false;
00251   d->indentLevel   = 0;
00252   d->rotationAngle = 0;
00253 }
00254 
00255 // destructor
00256 FormatAlignment::~FormatAlignment()
00257 {
00258   delete d;
00259 }
00260 
00261 // copy constructor
00262 FormatAlignment::FormatAlignment( const FormatAlignment& align )
00263 {
00264   d = new FormatAlignment::Private;
00265   assign( align );
00266 }
00267 
00268 // assignment operator
00269 FormatAlignment& FormatAlignment::operator=( const FormatAlignment& align )
00270 {
00271   return assign( align );
00272 }
00273 
00274 // assign from another alignment
00275 FormatAlignment& FormatAlignment::assign( const FormatAlignment& align )
00276 {
00277   d->null          = align.isNull();
00278   d->alignX        = align.alignX();
00279   d->alignY        = align.alignY();
00280   d->wrap          = align.wrap();
00281   d->indentLevel   = align.indentLevel();
00282   d->rotationAngle = align.rotationAngle();
00283   return *this;
00284 }
00285 
00286 bool FormatAlignment::isNull() const
00287 {
00288   return d->null;
00289 }
00290 
00291 unsigned FormatAlignment::alignX() const
00292 {
00293   return d->alignX;
00294 }
00295 
00296 void FormatAlignment::setAlignX( unsigned xa )
00297 {
00298   d->alignX = xa;
00299   d->null = false;
00300 }
00301 
00302 unsigned FormatAlignment::alignY() const
00303 {
00304   return d->alignY;
00305 }
00306 
00307 void FormatAlignment::setAlignY( unsigned ya )
00308 {
00309   d->alignY = ya;
00310   d->null = false;
00311 }
00312 
00313 bool FormatAlignment::wrap() const
00314 {
00315   return d->wrap;
00316 }
00317 
00318 void FormatAlignment::setWrap( bool w )
00319 {
00320   d->wrap = w;
00321   d->null = false;
00322 }
00323 
00324 unsigned FormatAlignment::indentLevel() const
00325 {
00326   return d->indentLevel;
00327 }
00328 
00329 void FormatAlignment::setIndentLevel( unsigned i )
00330 {
00331   d->indentLevel = i;
00332   d->null = false;
00333 }
00334 
00335 unsigned FormatAlignment::rotationAngle() const
00336 {
00337   return d->rotationAngle;
00338 }
00339 
00340 void FormatAlignment::setRotationAngle( unsigned r )
00341 {
00342   d->rotationAngle = r;
00343   d->null = false;
00344 }
00345 
00346 bool FormatAlignment::operator==(const FormatAlignment& font) const
00347 {
00348     return 
00349     d->alignX == font.d->alignX &&
00350     d->alignY == font.d->alignY &&
00351     d->wrap == font.d->wrap &&
00352     d->indentLevel == font.d->indentLevel &&
00353     d->rotationAngle == font.d->rotationAngle;
00354 }
00355 
00356 bool FormatAlignment::operator!=(const FormatAlignment& font) const
00357 {
00358     return 
00359     d->alignX != font.d->alignX ||
00360     d->alignY != font.d->alignY ||
00361     d->wrap != font.d->wrap ||
00362     d->indentLevel != font.d->indentLevel ||
00363     d->rotationAngle != font.d->rotationAngle;
00364 }
00365 
00366 class FormatBackground::Private
00367 {
00368 public:
00369   bool null;
00370   unsigned pattern;
00371   Color background;
00372   Color foreground;
00373 };
00374 
00375 // constructor
00376 FormatBackground::FormatBackground()
00377 {
00378   d = new FormatBackground::Private();
00379   d->null = true;
00380 }
00381 
00382 // destructor
00383 FormatBackground::~FormatBackground()
00384 {
00385   delete d;
00386 }
00387 
00388 // copy constructor
00389 FormatBackground::FormatBackground( const FormatBackground& background )
00390 {
00391   d = new FormatBackground::Private;
00392   assign( background );
00393 }
00394 
00395 // assignment operator
00396 FormatBackground& FormatBackground::operator=( const FormatBackground& background )
00397 {
00398   return assign( background );
00399 }
00400 
00401 // assign from another alignment
00402 FormatBackground& FormatBackground::assign( const FormatBackground& background )
00403 {
00404   d->null         = background.isNull();
00405   d->pattern      = background.pattern();
00406   d->background   = background.backgroundColor();
00407   d->foreground   = background.foregroundColor();
00408   return *this;
00409 }
00410 
00411 bool FormatBackground::isNull() const
00412 {
00413   return d->null;
00414 }
00415 
00416 unsigned FormatBackground::pattern() const
00417 {
00418   return d->pattern;
00419 }
00420 
00421 void FormatBackground::setPattern( unsigned pattern )
00422 {
00423   d->pattern = pattern;
00424   d->null = false;
00425 }
00426 
00427 Color FormatBackground::backgroundColor() const
00428 {
00429   return d->background;
00430 }
00431 
00432 void FormatBackground::setBackgroundColor( const Color& color )
00433 {
00434   d->background = color;
00435   d->null = false;
00436 }
00437 
00438 Color FormatBackground::foregroundColor() const
00439 {
00440   return d->foreground;
00441 }
00442 
00443 void FormatBackground::setForegroundColor( const Color& color )
00444 {
00445   d->foreground = color;
00446   d->null = false;
00447 }
00448 
00449 bool FormatBackground::operator==(const FormatBackground& font) const
00450 {
00451     return 
00452     d->pattern == font.d->pattern &&
00453     d->background == font.d->background &&
00454     d->foreground == font.d->foreground;
00455 }
00456 
00457 bool FormatBackground::operator!=(const FormatBackground& font) const
00458 {
00459     return 
00460     d->pattern != font.d->pattern ||
00461     d->background != font.d->background ||
00462     d->foreground != font.d->foreground;
00463 }
00464 
00465 class FormatBorders::Private
00466 {
00467 public:
00468   bool null;
00469   Pen leftBorder;
00470   Pen rightBorder;
00471   Pen topBorder;
00472   Pen bottomBorder;
00473 };
00474 
00475 // constructor
00476 FormatBorders::FormatBorders()
00477 {
00478   d = new FormatBorders::Private;
00479   d->null = true;
00480 }
00481 
00482 // destructor
00483 FormatBorders::~FormatBorders()
00484 {
00485   delete d;
00486 }
00487 
00488 // copy constructor
00489 FormatBorders::FormatBorders( const FormatBorders& border )
00490 {
00491   d = new FormatBorders::Private;
00492   assign( border );
00493 }
00494 
00495 // assignment operator
00496 FormatBorders& FormatBorders::operator=( const FormatBorders& border )
00497 {
00498   return assign( border );
00499 }
00500 
00501 // assign from another alignment
00502 FormatBorders& FormatBorders::assign( const FormatBorders& border )
00503 {
00504   d->null         = border.isNull();
00505   d->leftBorder   = border.leftBorder();
00506   d->rightBorder  = border.rightBorder();
00507   d->topBorder    = border.topBorder();
00508   d->bottomBorder = border.bottomBorder();
00509   return *this;
00510 }
00511 
00512 bool FormatBorders::isNull() const
00513 {
00514   return d->null;
00515 }
00516 
00517 const Pen& FormatBorders::leftBorder() const
00518 {
00519   return d->leftBorder;
00520 }
00521 
00522 void FormatBorders::setLeftBorder( const Pen& pen )
00523 {
00524   d->leftBorder = pen;
00525   d->null = false;
00526 }
00527 
00528 const Pen& FormatBorders::rightBorder() const
00529 {
00530   return d->rightBorder;
00531 }
00532 
00533 void FormatBorders::setRightBorder( const Pen& pen )
00534 {
00535   d->rightBorder = pen;
00536   d->null = false;
00537 }
00538 
00539 const Pen& FormatBorders::topBorder() const
00540 {
00541   return d->topBorder;
00542 }
00543 
00544 void FormatBorders::setTopBorder( const Pen& pen )
00545 {
00546   d->topBorder = pen;
00547   d->null = false;
00548 }
00549 
00550 const Pen& FormatBorders::bottomBorder() const
00551 {
00552   return d->bottomBorder;
00553 }
00554 
00555 void FormatBorders::setBottomBorder( const Pen& pen )
00556 {
00557   d->bottomBorder = pen;
00558   d->null = false;
00559 }
00560 
00561 bool FormatBorders::operator==(const FormatBorders& font) const
00562 {
00563     return 
00564     d->leftBorder == font.d->leftBorder &&
00565     d->rightBorder == font.d->rightBorder &&
00566     d->topBorder == font.d->topBorder &&
00567     d->bottomBorder == font.d->bottomBorder;
00568 }
00569 
00570 bool FormatBorders::operator!=(const FormatBorders& font) const
00571 {
00572     return 
00573     d->leftBorder != font.d->leftBorder ||
00574     d->rightBorder != font.d->rightBorder ||
00575     d->topBorder != font.d->topBorder ||
00576     d->bottomBorder != font.d->bottomBorder;
00577 }
00578 
00579 // helper class for Format class
00580 class Format::Private
00581 {
00582 public:
00583   FormatFont font;
00584   FormatAlignment alignment;
00585   FormatBorders borders;
00586   FormatBackground background;
00587   UString valueFormat;
00588 };
00589 
00590 // create an empty format
00591 Format::Format()
00592 {
00593   d = new Format::Private;
00594   d->valueFormat = "General";
00595 }
00596 
00597 // destructor
00598 Format::~Format()
00599 {
00600   delete d;
00601 }
00602 
00603 // copy constructor
00604 Format::Format( const Format& f )
00605 {
00606   d = new Format::Private;
00607   assign( f );
00608 }
00609 
00610 // assignment operator
00611 Format& Format::operator=( const Format& f )
00612 {
00613   return assign( f );
00614 }
00615 
00616 // assign from another format
00617 Format& Format::assign( const Format& f )
00618 {
00619   d->font = f.font();
00620   d->alignment = f.alignment();
00621   d->borders = f.borders();
00622   d->valueFormat = f.valueFormat();
00623   d->background = f.background();
00624   return *this;
00625 }
00626 
00627 bool Format::isNull() const
00628 {
00629   return d->font.isNull() && d->alignment.isNull() && d->borders.isNull();
00630 }
00631 
00632 FormatFont& Format::font() const
00633 {
00634   return d->font;
00635 }
00636 
00637 void Format::setFont( const FormatFont& font )
00638 {
00639   d->font = font;
00640 }
00641 
00642 FormatAlignment& Format::alignment() const
00643 {
00644   return d->alignment;
00645 }
00646 
00647 void Format::setAlignment( const FormatAlignment& alignment )
00648 {
00649   d->alignment = alignment;
00650 }
00651 
00652 FormatBorders& Format::borders() const
00653 {
00654   return d->borders;
00655 }
00656 
00657 void Format::setBorders( const FormatBorders& borders )
00658 {
00659   d->borders = borders;
00660 }
00661 
00662 FormatBackground& Format::background() const
00663 {
00664   return d->background;
00665 }
00666 
00667 void Format::setBackground( const FormatBackground& background )
00668 {
00669   d->background = background;
00670 }
00671 
00672 const UString& Format::valueFormat() const
00673 {
00674   return d->valueFormat;
00675 }
00676 
00677 void Format::setValueFormat( const UString& valueFormat )
00678 {
00679   d->valueFormat = valueFormat;
00680 }
00681 
00682 // merge f into current format
00683 Format& Format::apply( const Format& f )
00684 {
00685   if( !f.alignment().isNull() )
00686     alignment() = f.alignment();
00687   if( !f.font().isNull() )
00688     font() = f.font();
00689   if( !f.borders().isNull() )
00690     borders() = f.borders();
00691   if( f.valueFormat().isEmpty() || f.valueFormat() == "General" )
00692     setValueFormat( f.valueFormat() );
00693   if (!f.background().isNull() )
00694     background() = f.background();
00695   
00696   return *this;
00697 }
00698 
00699 bool Format::operator==(const Format& format) const
00700 {
00701     return 
00702     d->font == format.d->font &&
00703     d->alignment == format.d->alignment &&
00704     d->borders == format.d->borders &&
00705     d->background == format.d->background &&
00706     d->valueFormat == format.d->valueFormat;
00707 }
00708 
00709 bool Format::operator!=(const Format& format) const
00710 {
00711     return 
00712     d->font != format.d->font ||
00713     d->alignment != format.d->alignment ||
00714     d->borders != format.d->borders ||
00715     d->background != format.d->background ||
00716     d->valueFormat != format.d->valueFormat;
00717 }
KDE Home | KDE Accessibility Home | Description of Access Keys