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.

356 lines
11KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
  3. Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. * Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef __kspread_util_h__
  18. #define __kspread_util_h__
  19. #include <tqstring.h>
  20. #include <tqrect.h>
  21. #include <tqdatetime.h>
  22. #include <koffice_export.h>
  23. #include "kspread_global.h"
  24. #include "kspread_value.h"
  25. class TQFont;
  26. class TQPen;
  27. class TQDomElement;
  28. class TQDomDocument;
  29. class TDELocale;
  30. bool util_isPointValid(TQPoint point);
  31. bool util_isRectValid(TQRect rect);
  32. namespace KSpread
  33. {
  34. class Cell;
  35. class Map;
  36. class Sheet;
  37. /**
  38. * Represents the position of a single cell in a workbook. Each position has a row, column and optionally an associated Sheet.
  39. * Columns and rows can be marked as fixed. This is for handling absolute coordinates in formulae (eg. in the
  40. * formula "=$C$1" both the column (C) and row (1) are fixed.
  41. */
  42. class KSPREAD_EXPORT Point
  43. {
  44. public:
  45. Point() { _pos.setX( -1 ); _sheet = 0; _columnFixed = false; _rowFixed = false; }
  46. Point( const TQString& );
  47. Point( const TQString&, Map*, Sheet* default_sheet = 0 );
  48. Point( const Point& c ) {
  49. _pos = c._pos;
  50. _sheet = c._sheet;
  51. _sheetName = c._sheetName;
  52. _columnFixed = c._columnFixed;
  53. _rowFixed = c._rowFixed;
  54. }
  55. bool isValid() const { return ( util_isPointValid(pos()) && ( _sheet != 0 || _sheetName.isEmpty() ) ); }
  56. bool isSheetKnown() const { return ( ! _sheetName.isEmpty() && _sheet != 0 ); }
  57. Cell* cell() const;
  58. bool operator== (const Point &cell) const;
  59. bool operator< (const Point &cell) const;
  60. int row () const { return _pos.y(); };
  61. int column () const { return _pos.x(); };
  62. void setRow (int r) { _pos.setY (r); };
  63. void setColumn (int c) { _pos.setX (c); };
  64. /**
  65. * Sets the sheet which this point lies on.
  66. */
  67. void setSheet(Sheet* sheet);
  68. Sheet* sheet() const;
  69. /**
  70. * Sets the name of the sheet which this point lies on.
  71. */
  72. void setSheetName(TQString name);
  73. TQString sheetName() const;
  74. /**
  75. * Sets the position of this point (in rows and columns)
  76. */
  77. void setPos(TQPoint pos);
  78. TQPoint pos() const;
  79. /**
  80. * Sets whether or not the column (x coordinate) of this point is fixed (ie. it represents an absolute
  81. * coordinate - eg. the column letter B in the formula "=$B30" is fixed)
  82. */
  83. void setColumnFixed(bool colFixed);
  84. bool columnFixed() const;
  85. /**
  86. * Sets whether or not the row (y coordinate) of this point is fixed (ie. it represents an absolute coordinate - eg. the row number 30 in the formula "=A$30" is fixed)
  87. */
  88. void setRowFixed(bool rowFixed);
  89. bool rowFixed() const;
  90. private:
  91. Sheet* _sheet;
  92. TQString _sheetName;
  93. TQPoint _pos;
  94. bool _columnFixed;
  95. bool _rowFixed;
  96. private:
  97. void init( const TQString& );
  98. };
  99. /**
  100. * Represents a region within a workbook. The region has an area (the columns and rows that it includes) and
  101. * optionally an associated @ref KSpread::Sheet
  102. * A range is defined by four coordinates: Its left column, top row, right column and bottom row. Each
  103. * of these coordinates may be set as fixed, to represent absolute coordinates in formulae.
  104. */
  105. class KSPREAD_EXPORT Range
  106. {
  107. public:
  108. Range();
  109. Range( const TQString& );
  110. Range( const TQString&, Map*, Sheet* default_sheet = 0 );
  111. Range( const Range& r );
  112. Range( const Point& ul, const Point& lr );
  113. /**
  114. * Returns true if this Range represents a valid region of a spreadsheet.
  115. * A range is valid if:
  116. * - It has an associated Sheet
  117. * - The area is non-negative (ie.
  118. * - The left-most column is non-negative
  119. * - The top-most row is non-negative
  120. */
  121. bool isValid() const;
  122. /** Returns true if this range has an associated Sheet or false otherwise */
  123. bool isSheetKnown() const { return ( !sheetName().isEmpty() && sheet() != 0 ); }
  124. /** Fills a Point with info (row,column,sheet) about the first point in the range */
  125. void getStartPoint(Point* pt);
  126. /** Fills a Point with info (row,column,sheet) about the last point the range */
  127. void getEndPoint(Point* pt);
  128. int startRow () const { return range().top(); };
  129. int startCol () const { return range().left(); };
  130. int endRow () const { return range().bottom(); };
  131. int endCol () const { return range().right(); };
  132. /**
  133. * Changes the area on the spreadsheet represented by this range
  134. * @param newRange The new area for this range.
  135. */
  136. virtual void setRange(const TQRect& newRange) {_range=newRange;}
  137. void setRange(int newStartCol, int newStartRow, int newEndCol, int newEndRow)
  138. { _range=TQRect(newStartCol,newStartRow,newEndCol-newStartCol,newEndRow-newStartRow); }
  139. /** Returns the area on the spreadsheet occupied by this range. */
  140. TQRect range() const;
  141. /** Returns true if this range includes the specified cell */
  142. bool contains (const Point &cell) const;
  143. /**
  144. * Returns true if this range intersects Range @p r (ie. there is at least one cell
  145. * which is common to both Ranges )
  146. */
  147. bool intersects (const Range &r) const;
  148. /**
  149. * Returns a string representation of this range as it would appear in a formula.
  150. * ie. In the format " SheetName! [StartCell] : [EndCell] "
  151. * The string representation uses $ characters to mark fixed parts of the range, eg. Sheet1!$A1:$A20
  152. */
  153. TQString toString() const;
  154. /** Sets whether or not the left column is fixed . */
  155. void setLeftFixed(bool fixed);
  156. bool leftFixed() const;
  157. /** Sets whether or not the right column is fixed. */
  158. void setRightFixed(bool fixed);
  159. bool rightFixed() const;
  160. /** Sets whether or not the top row is fixed. */
  161. void setTopFixed(bool fixed);
  162. bool topFixed() const;
  163. /** Sets whether or not the bottom row is fixed. */
  164. void setBottomFixed(bool fixed);
  165. bool bottomFixed() const;
  166. /** Sets the Sheet object associated with this range. The range can only span a single sheet. */
  167. void setSheet(Sheet* sheet);
  168. Sheet* sheet() const;
  169. /** Sets the name of the sheet associated with this range. */
  170. void setSheetName(TQString sheetName);
  171. TQString sheetName() const;
  172. /**
  173. * Returns the named area represented by this range or an empty string otherwise.
  174. * This is the name of the area which was passed as a TQString to the Range constructor
  175. */
  176. TQString namedArea() const;
  177. /**
  178. * Returns true if the other range occupies the same area on the same sheet as this range.
  179. */
  180. bool operator==(const Range& range) const;
  181. private:
  182. Sheet* _sheet;
  183. TQString _sheetName;
  184. TQString _namedArea;
  185. TQRect _range;
  186. bool _leftFixed;
  187. bool _rightFixed;
  188. bool _topFixed;
  189. bool _bottomFixed;
  190. };
  191. /**
  192. range-list and cell-list
  193. TODO: move to a separate file, improve structure, add iterators and all that
  194. TODO: use this class instead of other means of range-walking all over KSpread
  195. TODO: use this as selection
  196. TODO: anything I forgot ;)
  197. */
  198. struct RangeList {
  199. TQValueList<Point> cells;
  200. TQValueList<Range> ranges;
  201. };
  202. /**
  203. * RangeIterator
  204. *
  205. * Class to simplify the process of iterating through each cell in a
  206. * range that has already been allocated
  207. */
  208. class RangeIterator
  209. {
  210. public:
  211. /**
  212. * Contstruct the iterator with the rectangular cell area and which
  213. * sheet the area is on
  214. */
  215. RangeIterator(TQRect _range, Sheet* _sheet);
  216. ~RangeIterator();
  217. /**
  218. * @return the first allocated cell in the area
  219. */
  220. Cell* first();
  221. /**
  222. * @return the next allocated cell in the area after the previous one
  223. * retrieved, or NULL if it was the last one.
  224. */
  225. Cell* next();
  226. private:
  227. TQRect range;
  228. Sheet* sheet;
  229. TQPoint current;
  230. };
  231. //helper functions for the formatting
  232. bool formatIsDate (FormatType fmt);
  233. bool formatIsTime (FormatType fmt);
  234. bool formatIsFraction (FormatType fmt);
  235. KSPREAD_EXPORT TQString util_rangeName( const TQRect &_area );
  236. KSPREAD_EXPORT TQString util_rangeName( Sheet *_sheet, const TQRect &_area );
  237. TQString util_rangeColumnName( const TQRect &_area);
  238. TQString util_rangeRowName( const TQRect &_area);
  239. /**
  240. * Call this function to decode the text of a column label to an integer
  241. * i.e. AA->27
  242. */
  243. KSPREAD_EXPORT int util_decodeColumnLabelText( const TQString &_col );
  244. /**
  245. * Call this function to encode an integer to the text of the column label
  246. * i.e. 27->AA
  247. */
  248. KSPREAD_EXPORT TQString util_encodeColumnLabelText( int column );
  249. bool util_isAllSelected(const TQRect &selection);
  250. bool util_isColumnSelected(const TQRect &selection);
  251. bool util_isRowSelected(const TQRect &selection);
  252. bool util_isRowOrColumnSelected( const TQRect &selection );
  253. bool util_validateSheetName(const TQString &name);
  254. TQDomElement util_createElement( const TQString & tagName, const TQFont & font, TQDomDocument & doc );
  255. TQDomElement util_createElement( const TQString & tagname, const TQPen & pen, TQDomDocument & doc );
  256. TQFont util_toFont( TQDomElement & element );
  257. TQPen util_toPen( TQDomElement & element );
  258. int util_penCompare( TQPen const & pen1, TQPen const & pen2 );
  259. TQString convertRefToRange( const TQString & sheet, const TQRect & rect );
  260. TQString convertRefToBase( const TQString & sheet, const TQRect & rect );
  261. TQString convertRangeToRef( const TQString & sheetName, const TQRect & _area );
  262. void insertBracket( TQString & s );
  263. TQString convertOasisPenToString( const TQPen & pen );
  264. TQPen convertOasisStringToPen( const TQString &str );
  265. //Return true when it's a reference to cell from sheet.
  266. KSPREAD_EXPORT bool localReferenceAnchor( const TQString &_ref );
  267. namespace Oasis
  268. {
  269. /**
  270. * Converts an OpenDocument representation of a formula/cell reference to a
  271. * localized formula/cell reference.
  272. * @param expr The expression to convert from OpenDocument format.
  273. * @param locale The locale to which the expression should be converted.
  274. */
  275. // TODO check visibility
  276. KSPREAD_EXPORT TQString decodeFormula(const TQString& expr, const TDELocale* locale = 0);
  277. /**
  278. * Converts a localized formula/cell reference to an OpenDocument
  279. * representation of a formula/cell reference.
  280. * @param expr The expression to convert to OpenDocument format.
  281. * @param locale The locale from which the expression should be converted.
  282. */
  283. KSPREAD_EXPORT TQString encodeFormula(const TQString& expr, const TDELocale* locale = 0);
  284. }
  285. } // namespace KSpread
  286. #endif