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