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.

450 lines
12KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2005-2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #ifndef KSPREAD_REGION
  16. #define KSPREAD_REGION
  17. #include <tqrect.h>
  18. #include <tqstring.h>
  19. #include <tqvaluelist.h>
  20. #include <kdebug.h>
  21. #include <koffice_export.h>
  22. #include "kspread_global.h"
  23. #include "kspread_util.h"
  24. namespace KSpread
  25. {
  26. class Manipulator;
  27. class Sheet;
  28. class View;
  29. /**
  30. * The one for all class for points and ranges.
  31. */
  32. class KSPREAD_EXPORT Region
  33. {
  34. protected:
  35. class Element;
  36. class Point;
  37. class Range;
  38. public:
  39. /**
  40. * Constructor.
  41. * Creates an empty region.
  42. */
  43. Region();
  44. /**
  45. * Constructor.
  46. * Creates a region consisting of a point.
  47. * @param point the point's location
  48. * @param sheet the sheet the point belongs to
  49. */
  50. Region(const TQPoint& point, Sheet* sheet = 0);
  51. /**
  52. * Constructor.
  53. * Creates a region consisting of a range.
  54. * @param range the range's location
  55. * @param sheet the sheet the range belongs to
  56. */
  57. Region(const TQRect& range, Sheet* sheet = 0);
  58. /**
  59. * Constructor.
  60. * Creates a region consisting of the region defined in @p strRegion .
  61. * @param view used to determine the sheet, if it's named in the string
  62. * @param strRegion a string representing the region (e.g. "A1:B3")
  63. * @param sheet the sheet the region belongs to
  64. */
  65. Region(View* view, const TQString& strRegion, Sheet* sheet = 0);
  66. /**
  67. * Copy Constructor.
  68. * Creates a copy of the region.
  69. * @param region the region to copy
  70. */
  71. Region(const Region& region);
  72. /**
  73. * Constructor.
  74. * Creates a region consisting of a point.
  75. * @param col the column of the point
  76. * @param row the row of the point
  77. * @param sheet the sheet the point belongs to
  78. */
  79. Region(int col, int row, Sheet* sheet = 0);
  80. /**
  81. * Constructor.
  82. * Creates a region consisting of a range at the location
  83. * @param col the column of the range' starting point
  84. * @param row the row of the range' starting point
  85. * @param width the width of the range
  86. * @param height the height of the range
  87. * @param sheet the sheet the range belongs to
  88. */
  89. Region(int col, int row, int width, int height, Sheet* sheet = 0);
  90. /**
  91. * Destructor.
  92. */
  93. virtual ~Region();
  94. /**
  95. * @param originSheet The name is created relative to this sheet.
  96. * @return the name of the region (e.g. "A1:A2")
  97. */
  98. TQString name(Sheet* originSheet = 0) const;
  99. /**
  100. * @param sRegion will be modified, if a valid sheet was found. The sheetname
  101. * will be removed
  102. * @return sheet named in the @p sRegion or the active sheet of the view
  103. */
  104. Sheet* filterSheetName(TQString& sRegion);
  105. /**
  106. * @return @c true, if this region contains no elements
  107. */
  108. bool isEmpty() const;
  109. /**
  110. * @return @c true, if this region contains only a single point
  111. */
  112. bool isSingular() const;
  113. /**
  114. * @return @c true, if this region is contiguous
  115. */
  116. bool isContiguous() const;
  117. /**
  118. * @return @c true, if this region contains at least one valid point or one valid range
  119. */
  120. bool isValid() const;
  121. /**
  122. * @param col the col to check
  123. * @return @c true, if the colum @p col is selected. If column @p col
  124. * is not given, it returns true, if at least one column is selected
  125. */
  126. bool isColumnSelected(uint col = 0) const;
  127. /**
  128. * @param row the row to check
  129. * @return @c true, if the row @p row is selected. If row @p row
  130. * is not given, it returns true, if at least one row is selected
  131. */
  132. bool isRowSelected(uint row = 0) const;
  133. /**
  134. * @return @c true,if at least one column or one row is selected
  135. */
  136. bool isColumnOrRowSelected() const;
  137. /**
  138. * @param col the col to check
  139. * @return @c true, if the at least one cell in column @p col is selected
  140. */
  141. bool isColumnAffected(uint col) const;
  142. /**
  143. * @param row the row to check
  144. * @return @c true, if the at least one cell in row @p row is selected
  145. */
  146. bool isRowAffected(uint row) const;
  147. /**
  148. * @param point the point's location
  149. * @param sheet the sheet the point belongs to
  150. * @return @c true, if the region contains the point @p point
  151. */
  152. bool contains(const TQPoint& point, Sheet* sheet = 0) const;
  153. /* TODO Stefan #2: Optimize! Adjacent Points/Ranges */
  154. /**
  155. * Adds the point @p point to this region.
  156. * @param point the point's location
  157. * @param sheet the sheet the point belongs to
  158. */
  159. Element* add(const TQPoint& point, Sheet* sheet = 0);
  160. /**
  161. * Adds the range @p range to this region.
  162. * @param range the range's location
  163. * @param sheet the sheet the range belongs to
  164. */
  165. Element* add(const TQRect& range, Sheet* sheet = 0);
  166. /**
  167. * Adds the region @p region to this region.
  168. * @param region the range's location
  169. */
  170. Element* add(const Region& region);
  171. /* TODO Stefan #3: Improve! */
  172. /**
  173. * Substracts the point @p point from this region.
  174. * @param point the point's location
  175. */
  176. void sub(const TQPoint& point);
  177. /**
  178. * Substracts the range @p range from this region.
  179. * @param range the range's location
  180. */
  181. void sub(const TQRect& range);
  182. /**
  183. * Substracts the region @p region from this region.
  184. * @param region the region to substract
  185. */
  186. void sub(const Region& region);
  187. /**
  188. * @param point the point's location
  189. * @param sheet the sheet the point belongs to
  190. */
  191. virtual Element* eor(const TQPoint& point, Sheet* sheet = 0);
  192. /**
  193. * Deletes all elements of the region. The result is an empty region.
  194. */
  195. virtual void clear();
  196. TQRect boundingRect() const;
  197. /**
  198. * @param region the region to compare
  199. * @return @c true, if this region equals region @p region
  200. */
  201. bool operator==(const Region& region) const;
  202. /**
  203. * @param region the region to copy
  204. */
  205. void operator=(const Region& region);
  206. /**
  207. * @return the view to which this region belongs.
  208. */
  209. View* view() const;
  210. /**
  211. * Sets the view to which this region belongs.
  212. */
  213. void setView(View*);
  214. typedef TQValueList<Element*>::Iterator Iterator;
  215. typedef TQValueList<Element*>::ConstIterator ConstIterator;
  216. ConstIterator constBegin() const;
  217. ConstIterator constEnd() const;
  218. protected:
  219. /**
  220. * @return the list of elements
  221. */
  222. TQValueList<Element*>& cells() const;
  223. /**
  224. * @param iterator the iterator to the element in whose front the new point
  225. * is inserted
  226. * @param point the location of the point to be inserted
  227. * @param multi @c true to allow multiple occurences of a point
  228. * @return an iterator to the added point or @p iterator, if @p point is not
  229. * valid or the point is already in the list
  230. */
  231. Iterator insert(Iterator iterator, const TQPoint& point, Sheet*, bool multi = true);
  232. /**
  233. * @param iterator the iterator to the element in whose front the new range
  234. * is inserted
  235. * @param range the location of the range to be inserted
  236. * @param multi @c true to allow multiple occurences of a range
  237. * @return an iterator to the added range or @p iterator, if @p range is not
  238. * valid or the range is already in the list
  239. */
  240. Iterator insert(Iterator iterator, const TQRect& range, Sheet*, bool multi = true);
  241. virtual Point* createPoint(const TQPoint&) const;
  242. virtual Point* createPoint(const TQString&) const;
  243. virtual Point* createPoint(const Point&) const;
  244. virtual Range* createRange(const TQRect&) const;
  245. virtual Range* createRange(const TQString&) const;
  246. virtual Range* createRange(const Range&) const;
  247. private:
  248. class Private;
  249. Private *d;
  250. };
  251. /***************************************************************************
  252. class Region::Element
  253. ****************************************************************************/
  254. /**
  255. * Base class for region elements, which can be points or ranges.
  256. * This class is used by KSpread::Region and could not be used outside of it.
  257. *
  258. * Size:
  259. * m_sheet: 4 bytes
  260. * vtable: 4 bytes
  261. * sum: 8 bytes
  262. */
  263. class Region::Element
  264. {
  265. public:
  266. enum Type { Undefined, Point, Range };
  267. Element();
  268. virtual ~Element();
  269. virtual Type type() const { return Undefined; }
  270. virtual bool isValid() const { return false; }
  271. virtual bool isColumn() const { return false; }
  272. virtual bool isRow() const { return false; }
  273. virtual bool contains(const TQPoint&) const { return false; }
  274. virtual bool contains(const TQRect&) const { return false; }
  275. virtual TQString name(Sheet* = 0) const { return TQString(""); }
  276. virtual TQRect rect() const { return TQRect(); }
  277. virtual const TQColor& color() const { return TQt::black; }
  278. Sheet* sheet() const { return m_sheet; }
  279. void setSheet(Sheet* sheet) { m_sheet = sheet; }
  280. protected:
  281. /* TODO Stefan #6:
  282. Elaborate, if this pointer could be avoided by TQDict or whatever in
  283. Region.
  284. */
  285. Sheet* m_sheet;
  286. };
  287. /***************************************************************************
  288. class Region::Point
  289. ****************************************************************************/
  290. /**
  291. * A point in a region.
  292. * This class is used by KSpread::Region and could not be used outside of it.
  293. *
  294. * Size:
  295. * m_sheet: 4 bytes
  296. * vtable: 4 bytes
  297. * m_point: 8 bytes
  298. * sum: 16 bytes
  299. */
  300. class Region::Point : public Region::Element
  301. {
  302. public:
  303. Point(const TQPoint&);
  304. Point(const TQString&);
  305. virtual ~Point();
  306. virtual Type type() const { return Element::Point; }
  307. virtual bool isValid() const { return (!m_point.isNull() && util_isPointValid(pos())); }
  308. virtual bool isColumn() const { return false; }
  309. virtual bool isRow() const { return false; }
  310. virtual bool contains(const TQPoint&) const;
  311. virtual bool contains(const TQRect&) const;
  312. virtual TQString name(Sheet* originSheet = 0) const;
  313. virtual TQRect rect() const { return TQRect(m_point,m_point); }
  314. TQPoint pos() const { return m_point; }
  315. private:
  316. TQPoint m_point;
  317. };
  318. /***************************************************************************
  319. class Region:.Range
  320. ****************************************************************************/
  321. /**
  322. * A range in a region.
  323. * This class is used by KSpread::Region and could not be used outside of it.
  324. *
  325. * Size:
  326. * m_sheet: 4 bytes
  327. * vtable: 4 bytes
  328. * m_range: 16 bytes
  329. * sum: 24 bytes
  330. */
  331. class Region::Range : public Region::Element
  332. {
  333. public:
  334. Range(const TQRect&);
  335. Range(const TQString&);
  336. virtual ~Range();
  337. virtual Type type() const { return Element::Range; }
  338. virtual bool isValid() const { return !m_range.isNull() && util_isRectValid(rect()); }
  339. virtual bool isColumn() const { return (m_range.normalize().top() == 1 && m_range.normalize().bottom() == KS_rowMax); }
  340. virtual bool isRow() const { return (m_range.normalize().left() == 1 && m_range.normalize().right() == KS_colMax); }
  341. virtual bool contains(const TQPoint&) const;
  342. virtual bool contains(const TQRect&) const;
  343. virtual TQString name(Sheet* originSheet = 0) const;
  344. virtual TQRect rect() const { return m_range; }
  345. int width() const;
  346. int height() const;
  347. private:
  348. TQRect m_range;
  349. };
  350. } // namespace KSpread
  351. /***************************************************************************
  352. kdDebug support
  353. ****************************************************************************/
  354. inline kdbgstream operator<<( kdbgstream str, const KSpread::Region& r )
  355. {
  356. str << "Region = " << r.name();
  357. return str;
  358. }
  359. #endif // KSPREAD_REGION