KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

492 lines
16KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2003 Norbert Andres, nandres@web.de
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #ifndef __kspread_style__
  17. #define __kspread_style__
  18. #include <tqbrush.h>
  19. #include <tqcolor.h>
  20. #include <tqfont.h>
  21. #include <tqpen.h>
  22. #include "kspread_format.h"
  23. class TQDomDocument;
  24. class TQDomElement;
  25. class KoGenStyles;
  26. class KoOasisStyles;
  27. namespace KSpread
  28. {
  29. class CustomStyle;
  30. /**
  31. * @brief The Style class represents a cell style.
  32. * Built-in and custom styles, the ones shown in the StyleManager dialog,
  33. * are CustomStyles. Pure Styles have no name, for instance, and are only
  34. * used as AUTO styles.
  35. */
  36. class Style
  37. {
  38. public:
  39. /// The style type
  40. enum StyleType
  41. {
  42. BUILTIN, ///< built-in style (the default style)
  43. CUSTOM, ///< custom style (defined in the StyleManager dialog)
  44. AUTO, ///< automatically generated on cell format changes
  45. TENTATIVE ///< @internal temporary state
  46. };
  47. enum FontFlags
  48. {
  49. FBold = 0x01,
  50. FUnderline = 0x02,
  51. FItalic = 0x04,
  52. FStrike = 0x08
  53. };
  54. /// @see Format::FormatFlags
  55. enum Properties
  56. {
  57. PDontPrintText = 0x01,
  58. PCustomFormat = 0x02,
  59. PNotProtected = 0x04,
  60. PHideAll = 0x08,
  61. PHideFormula = 0x10,
  62. PMultiRow = 0x20,
  63. PVerticalText = 0x40
  64. };
  65. // TODO Stefan: merge with Format::Properties
  66. /// @see Format::Properties
  67. enum FlagsSet
  68. {
  69. SAlignX = 0x01,
  70. SAlignY = 0x02,
  71. //SFactor was here
  72. SPrefix = 0x08,
  73. SPostfix = 0x10,
  74. SLeftBorder = 0x20,
  75. SRightBorder = 0x40,
  76. STopBorder = 0x80,
  77. SBottomBorder = 0x100,
  78. SFallDiagonal = 0x200,
  79. SGoUpDiagonal = 0x400,
  80. SBackgroundBrush = 0x800,
  81. SFont = 0x1000,
  82. STextPen = 0x2000,
  83. SBackgroundColor = 0x4000,
  84. SFloatFormat = 0x8000,
  85. SFloatColor = 0x10000,
  86. SMultiRow = 0x20000,
  87. SVerticalText = 0x40000,
  88. SPrecision = 0x80000,
  89. SFormatType = 0x100000,
  90. SAngle = 0x200000,
  91. //SComment = 0x400000,
  92. SIndent = 0x800000,
  93. SDontPrintText = 0x1000000,
  94. SCustomFormat = 0x2000000,
  95. SNotProtected = 0x4000000,
  96. SHideAll = 0x8000000,
  97. SHideFormula = 0x10000000,
  98. SFontSize = 0x20000000,
  99. SFontFlag = 0x40000000,
  100. SFontFamily = 0x80000000
  101. };
  102. /**
  103. * Constructor.
  104. * Creates an empty automatic style.
  105. */
  106. Style();
  107. /**
  108. * Constructor.
  109. * Creates an automatic style.
  110. * If @p style is a custom or built-in style (e.g. the default style),
  111. * @p style becomes the parent style. In this case, features are NOT SET.
  112. * @param style The style which features are copied.
  113. */
  114. Style( Style* style );
  115. /**
  116. * Destructor.
  117. */
  118. virtual ~Style();
  119. /** Returns true if both styles have the same properties */
  120. bool operator == (const Style& style) const;
  121. inline bool operator!=( const Style& other ) const { return !operator==( other ); }
  122. static FormatType dateType( const TQString &_format );
  123. static FormatType timeType( const TQString &_format );
  124. static FormatType fractionType( const TQString &_format );
  125. /**
  126. * @return the name of the data style (number, currency, percentage, date,
  127. * boolean, text)
  128. */
  129. static TQString saveOasisStyleNumeric( KoGenStyle &style, KoGenStyles &mainStyles, FormatType _style,
  130. const TQString &_prefix, const TQString &_postfix, int _precision, const TQString& symbol );
  131. static TQString saveOasisStyleNumericDate( KoGenStyles &mainStyles, FormatType _style,
  132. const TQString &_prefix, const TQString &_suffix );
  133. static TQString saveOasisStyleNumericFraction( KoGenStyles &mainStyles, FormatType _style,
  134. const TQString &_prefix, const TQString _suffix );
  135. static TQString saveOasisStyleNumericTime( KoGenStyles& mainStyles, FormatType _style,
  136. const TQString &_prefix, const TQString &_suffix );
  137. static TQString saveOasisStyleNumericCustom( KoGenStyles&mainStyles, FormatType _style,
  138. const TQString &_prefix, const TQString &_suffix );
  139. static TQString saveOasisStyleNumericScientific( KoGenStyles&mainStyles, FormatType _style,
  140. const TQString &_prefix, const TQString _suffix, int _precision );
  141. static TQString saveOasisStyleNumericPercentage( KoGenStyles&mainStyles, FormatType _style, int _precision,
  142. const TQString &_prefix, const TQString &_suffix );
  143. static TQString saveOasisStyleNumericMoney( KoGenStyles&mainStyles, FormatType _style,
  144. const TQString& symbol, int _precision,
  145. const TQString &_prefix, const TQString &_suffix );
  146. static TQString saveOasisStyleNumericText( KoGenStyles&mainStyles, FormatType _style, int _precision,
  147. const TQString &_prefix, const TQString &_suffix );
  148. static TQString saveOasisStyleNumericNumber( KoGenStyles&mainStyles, FormatType _style, int _precision,
  149. const TQString &_prefix, const TQString &_suffix );
  150. StyleType type() const { return m_type; }
  151. void saveXML( TQDomDocument & doc, TQDomElement & format ) const;
  152. bool loadXML( TQDomElement & format );
  153. /**
  154. * Saves an OASIS automatic style.
  155. * Reimplemented by CustomStyle for OASIS user styles.
  156. * @return always TQString()
  157. */
  158. virtual TQString saveOasis( KoGenStyle& style, KoGenStyles& mainStyles);
  159. void loadOasisStyle( KoOasisStyles& oasisStyles, const TQDomElement & element );
  160. static TQString saveOasisBackgroundStyle( KoGenStyles &mainStyles, const TQBrush &brush );
  161. /**
  162. * Releases this style. The internal reference counter is decremented.
  163. * @return true, if this style is not used anymore and should be deleted.
  164. */
  165. bool release();
  166. /**
  167. * Marks this style as used. The internal reference counter is incremented.
  168. */
  169. void addRef();
  170. /**
  171. * @return the number of references to this style.
  172. */
  173. int usage() const { return m_usageCount; }
  174. bool hasProperty( Properties p ) const;
  175. bool hasFeature( FlagsSet f, bool withoutParent ) const;
  176. void clearFeature( FlagsSet f );
  177. uint features() const { return m_featuresSet; }
  178. uint bottomPenValue() const { return m_bottomPenValue; }
  179. uint rightPenValue() const { return m_rightPenValue; }
  180. uint leftPenValue() const { return m_leftPenValue; }
  181. uint topPenValue() const { return m_topPenValue; }
  182. TQPen const & pen() const;
  183. TQColor const & bgColor() const;
  184. TQPen const & rightBorderPen() const;
  185. TQPen const & bottomBorderPen() const;
  186. TQPen const & leftBorderPen() const;
  187. TQPen const & topBorderPen() const;
  188. TQPen const & fallDiagonalPen() const;
  189. TQPen const & goUpDiagonalPen() const;
  190. TQBrush const & backGroundBrush() const;
  191. TQString const & strFormat() const;
  192. TQString const & prefix() const;
  193. TQString const & postfix() const;
  194. TQString const & fontFamily() const;
  195. Format::Align alignX() const;
  196. Format::AlignY alignY() const;
  197. Format::FloatFormat floatFormat() const;
  198. Format::FloatColor floatColor() const;
  199. FormatType formatType() const;
  200. Format::Currency const & currency() const;
  201. TQFont font() const;
  202. uint fontFlags() const;
  203. int fontSize() const;
  204. int precision() const;
  205. int rotateAngle() const;
  206. double indent() const;
  207. Style * setAlignX( Format::Align alignX );
  208. Style * setAlignY( Format::AlignY alignY );
  209. Style * setFont( TQFont const & f );
  210. Style * setFontFamily( TQString const & fam );
  211. Style * setFontFlags( uint flags );
  212. Style * setFontSize( int size );
  213. Style * setPen( TQPen const & pen );
  214. Style * setBgColor( TQColor const & color );
  215. Style * setRightBorderPen( TQPen const & pen );
  216. Style * setBottomBorderPen( TQPen const & pen );
  217. Style * setLeftBorderPen( TQPen const & pen );
  218. Style * setTopBorderPen( TQPen const & pen );
  219. Style * setFallDiagonalPen( TQPen const & pen );
  220. Style * setGoUpDiagonalPen( TQPen const & pen );
  221. Style * setRotateAngle( int angle );
  222. Style * setIndent( double indent );
  223. Style * setBackGroundBrush( TQBrush const & brush );
  224. Style * setFloatFormat( Format::FloatFormat format );
  225. Style * setFloatColor( Format::FloatColor color );
  226. Style * setFormatType( FormatType format );
  227. Style * setStrFormat( TQString const & strFormat );
  228. Style * setPrecision( int precision );
  229. Style * setPrefix( TQString const & prefix );
  230. Style * setPostfix( TQString const & postfix );
  231. Style * setCurrency( Format::Currency const & currency );
  232. Style * setProperty( Properties p );
  233. Style * clearProperty( Properties p );
  234. CustomStyle * parent() const;
  235. TQString const & parentName() const { return m_parentName; }
  236. void setParent( CustomStyle * parent );
  237. /**
  238. * Returns the name of a colour. This is the same as returned by TQColor::name, but an internal cache
  239. * is used to reduce the overhead when asking for the name of the same colour.
  240. */
  241. static TQString colorName( const TQColor& color );
  242. protected:
  243. /**
  244. * Helper function for saveOasis
  245. * Does the real work by determining the used attributes.
  246. */
  247. void saveOasisStyle( KoGenStyle &style, KoGenStyles &mainStyles );
  248. void loadOasisDataStyle( KoOasisStyles& oasisStyles, const TQDomElement& styleStack );
  249. void loadOasisParagraphProperties( KoOasisStyles& oasisStyles, const KoStyleStack& styleStack );
  250. void loadOasisTableCellProperties( KoOasisStyles& oasisStyles, const KoStyleStack& styleStack );
  251. void loadOasisTextProperties( KoOasisStyles& oasisStyles, const KoStyleStack& styleStack );
  252. CustomStyle * m_parent;
  253. TQString m_parentName;
  254. StyleType m_type;
  255. uint m_usageCount;
  256. uint m_featuresSet;
  257. /**
  258. * Alignment of the text
  259. */
  260. Format::Align m_alignX;
  261. /**
  262. * Aligment of the text at top middle or bottom
  263. */
  264. Format::AlignY m_alignY;
  265. Format::FloatFormat m_floatFormat;
  266. /**
  267. * The color format of a floating point value
  268. */
  269. Format::FloatColor m_floatColor;
  270. FormatType m_formatType;
  271. /**
  272. * The font used to draw the text
  273. */
  274. TQString m_fontFamily;
  275. uint m_fontFlags;
  276. int m_fontSize;
  277. /**
  278. * The pen used to draw the text
  279. */
  280. TQPen m_textPen;
  281. /**
  282. * The background color
  283. */
  284. TQColor m_bgColor;
  285. /**
  286. * The pen used to draw the right border
  287. */
  288. TQPen m_rightBorderPen;
  289. /**
  290. * The pen used to draw the bottom border
  291. */
  292. TQPen m_bottomBorderPen;
  293. /**
  294. * The pen used to draw the left border
  295. */
  296. TQPen m_leftBorderPen;
  297. /**
  298. * The pen used to draw the top border
  299. */
  300. TQPen m_topBorderPen;
  301. /**
  302. * The pen used to draw the diagonal
  303. */
  304. TQPen m_fallDiagonalPen;
  305. /**
  306. * The pen used to draw the the diagonal which go up
  307. */
  308. TQPen m_goUpDiagonalPen;
  309. /**
  310. * The brush used to draw the background.
  311. */
  312. TQBrush m_backGroundBrush;
  313. int m_rotateAngle;
  314. /**
  315. * Give indent
  316. */
  317. double m_indent;
  318. /**
  319. * Format of the content, e.g. #.##0.00, dd/mmm/yyyy,...
  320. */
  321. TQString m_strFormat;
  322. /**
  323. * The precision of the floating point representation
  324. * If precision is -1, this means that no precision is specified.
  325. */
  326. int m_precision;
  327. /**
  328. * The prefix of a numeric value ( for example "$" )
  329. * May be empty.
  330. */
  331. TQString m_prefix;
  332. /**
  333. * The postfix of a numeric value ( for example "DM" )
  334. * May be empty.
  335. */
  336. TQString m_postfix;
  337. /**
  338. * Currency information:
  339. * about which currency from which country
  340. */
  341. Format::Currency m_currency;
  342. /**
  343. * Stores information like: DonPrint, DontShowFormula, Protected...
  344. */
  345. uint m_properties;
  346. uint m_bottomPenValue;
  347. uint m_rightPenValue;
  348. uint m_leftPenValue;
  349. uint m_topPenValue;
  350. bool featureSet( FlagsSet f ) const { return ( !m_parent || ( m_featuresSet & (uint) f ) ); }
  351. };
  352. /**
  353. * @brief Built-in or custom style defined in StyleManager dialog.
  354. */
  355. class CustomStyle : public Style
  356. {
  357. public:
  358. /**
  359. * Constructor.
  360. * Creates a custom style.
  361. * @param style The style which's features are copied.
  362. * @param name The name of this style.
  363. */
  364. CustomStyle( Style * style, TQString const & name );
  365. CustomStyle( TQString const & name, CustomStyle * parent );
  366. ~CustomStyle();
  367. TQString const & name() const { return m_name; }
  368. void save( TQDomDocument & doc, TQDomElement & styles );
  369. /**
  370. * @reimp
  371. * Stores an OASIS user style.
  372. * @return the OASIS style's name
  373. */
  374. virtual TQString saveOasis( KoGenStyle& style, KoGenStyles &mainStyles );
  375. /**
  376. * Loads the style properties from @p style .
  377. * Determines also the parent's name.
  378. * @param oasisStyles map of all styles
  379. * @param style the DOM element defining the style
  380. * @param name the style's new name
  381. */
  382. void loadOasis( KoOasisStyles& oasisStyles, const TQDomElement & style, const TQString & name );
  383. bool loadXML( TQDomElement const & style, TQString const & name );
  384. void setType( StyleType type ) { m_type = type; }
  385. void setName( TQString const & name );
  386. void refreshParentName();
  387. bool definesAll() const;
  388. void changeAlignX( Format::Align alignX );
  389. void changeAlignY( Format::AlignY alignY );
  390. void changeFont( TQFont const & f );
  391. void changeFontFamily( TQString const & fam );
  392. void changeFontSize( int size );
  393. void changeFontFlags( uint flags );
  394. void changePen( TQPen const & pen );
  395. void changeTextColor( TQColor const & color );
  396. void changeBgColor( TQColor const & color );
  397. void changeRightBorderPen( TQPen const & pen );
  398. void changeBottomBorderPen( TQPen const & pen );
  399. void changeLeftBorderPen( TQPen const & pen );
  400. void changeTopBorderPen( TQPen const & pen );
  401. void changeFallBorderPen( TQPen const & pen );
  402. void changeGoUpBorderPen( TQPen const & pen );
  403. void changeRotateAngle( int angle );
  404. void changeIndent( double indent );
  405. void changeBackGroundBrush( TQBrush const & brush );
  406. void changeFloatFormat( Format::FloatFormat format );
  407. void changeFloatColor( Format::FloatColor color );
  408. void changeFormatType( FormatType format );
  409. void changeStrFormat( TQString const & strFormat );
  410. void changePrecision( int precision );
  411. void changePrefix( TQString const & prefix );
  412. void changePostfix( TQString const & postfix );
  413. void changeCurrency( Format::Currency const & currency );
  414. void addProperty( Properties p );
  415. void removeProperty( Properties p );
  416. bool operator==( const CustomStyle& other ) const;
  417. inline bool operator!=( const CustomStyle& other ) const { return !operator==( other ); }
  418. private:
  419. friend class StyleManager;
  420. TQString m_name;
  421. /**
  422. * Constructor.
  423. * Constructs the default cell style.
  424. */
  425. CustomStyle();
  426. };
  427. } // namespace KSpread
  428. #endif