summaryrefslogtreecommitdiffstats
path: root/kspread/region.h
blob: 359726837451857855bfd5dab64705562e2df5e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* This file is part of the KDE project
   Copyright (C) 2005-2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#ifndef KSPREAD_REGION
#define KSPREAD_REGION

#include <tqrect.h>
#include <tqstring.h>
#include <tqvaluelist.h>

#include <kdebug.h>

#include <koffice_export.h>

#include "kspread_global.h"
#include "kspread_util.h"

namespace KSpread
{
class Manipulator;
class Sheet;
class View;

/**
 * The one for all class for points and ranges.
 */
class KSPREAD_EXPORT Region
{
protected:
  class Element;
  class Point;
  class Range;

public:
  /**
   * Constructor.
   * Creates an empty region.
   */
  Region();

  /**
   * Constructor.
   * Creates a region consisting of a point.
   * @param point the point's location
   * @param sheet the sheet the point belongs to
   */
  Region(const TQPoint& point, Sheet* sheet = 0);

  /**
   * Constructor.
   * Creates a region consisting of a range.
   * @param range the range's location
   * @param sheet the sheet the range belongs to
   */
  Region(const TQRect& range, Sheet* sheet = 0);

  /**
   * Constructor.
   * Creates a region consisting of the region defined in @p strRegion .
   * @param view used to determine the sheet, if it's named in the string
   * @param strRegion a string representing the region (e.g. "A1:B3")
   * @param sheet the sheet the region belongs to
   */
  Region(View* view, const TQString& strRegion, Sheet* sheet = 0);

  /**
   * Copy Constructor.
   * Creates a copy of the region.
   * @param region the region to copy
   */
  Region(const Region& region);

  /**
   * Constructor.
   * Creates a region consisting of a point.
   * @param col the column of the point
   * @param row the row of the point
   * @param sheet the sheet the point belongs to
   */
  Region(int col, int row, Sheet* sheet = 0);

  /**
   * Constructor.
   * Creates a region consisting of a range at the location
   * @param col the column of the range' starting point
   * @param row the row of the range' starting point
   * @param width the width of the range
   * @param height the height of the range
   * @param sheet the sheet the range belongs to
   */
  Region(int col, int row, int width, int height, Sheet* sheet = 0);

  /**
   * Destructor.
   */
  virtual ~Region();



  /**
   * @param originSheet The name is created relative to this sheet.
   * @return the name of the region (e.g. "A1:A2")
   */
  TQString name(Sheet* originSheet = 0) const;

  /**
   * @param sRegion will be modified, if a valid sheet was found. The sheetname
   * will be removed
   * @return sheet named in the @p sRegion or the active sheet of the view
   */
  Sheet* filterSheetName(TQString& sRegion);



  /**
   * @return @c true, if this region contains no elements
   */
  bool isEmpty() const;

  /**
   * @return @c true, if this region contains only a single point
   */
  bool isSingular() const;

  /**
   * @return @c true, if this region is contiguous
   */
  bool isContiguous() const;

  /**
   * @return @c true, if this region contains at least one valid point or one valid range
   */
  bool isValid() const;

  /**
   * @param col the col to check
   * @return @c true, if the colum @p col is selected. If column @p col
   * is not given, it returns true, if at least one column is selected
   */
  bool isColumnSelected(uint col = 0) const;

  /**
   * @param row the row to check
   * @return @c true, if the row @p row is selected. If row @p row
   * is not given, it returns true, if at least one row is selected
   */
  bool isRowSelected(uint row = 0) const;

  /**
   * @return @c true,if at least one column or one row is selected
   */
  bool isColumnOrRowSelected() const;

  /**
   * @param col the col to check
   * @return @c true, if the at least one cell in column @p col is selected
   */
  bool isColumnAffected(uint col) const;

  /**
   * @param row the row to check
   * @return @c true, if the at least one cell in row @p row is selected
   */
  bool isRowAffected(uint row) const;

  /**
   * @param point the point's location
   * @param sheet the sheet the point belongs to
   * @return @c true, if the region contains the point @p point
   */
  bool contains(const TQPoint& point, Sheet* sheet = 0) const;



  /* TODO Stefan #2: Optimize! Adjacent Points/Ranges */
  /**
   * Adds the point @p point to this region.
   * @param point the point's location
   * @param sheet the sheet the point belongs to
   */
  Element* add(const TQPoint& point, Sheet* sheet = 0);
  /**
   * Adds the range @p range to this region.
   * @param range the range's location
   * @param sheet the sheet the range belongs to
   */
  Element* add(const TQRect& range, Sheet* sheet = 0);
  /**
   * Adds the region @p region to this region.
   * @param region the range's location
   */
  Element* add(const Region& region);

  /* TODO Stefan #3: Improve! */
  /**
   * Substracts the point @p point from this region.
   * @param point the point's location
   */
  void sub(const TQPoint& point);
  /**
   * Substracts the range @p range from this region.
   * @param range the range's location
   */
  void sub(const TQRect& range);
  /**
   * Substracts the region @p region from this region.
   * @param region the region to substract
   */
  void sub(const Region& region);

  /**
   * @param point the point's location
   * @param sheet the sheet the point belongs to
   */
  virtual Element* eor(const TQPoint& point, Sheet* sheet = 0);

  /**
   * Deletes all elements of the region. The result is an empty region.
   */
  virtual void clear();



  TQRect boundingRect() const;



  /**
   * @param region the region to compare
   * @return @c true, if this region equals region @p region
   */
  bool operator==(const Region& region) const;

  /**
   * @param region the region to copy
   */
  void operator=(const Region& region);



  /**
   * @return the view to which this region belongs.
   */
  View* view() const;

  /**
   * Sets the view to which this region belongs.
   */
  void setView(View*);


  typedef TQValueList<Element*>::Iterator      Iterator;
  typedef TQValueList<Element*>::ConstIterator ConstIterator;

  ConstIterator constBegin() const;
  ConstIterator constEnd() const;

protected:
  /**
   * @return the list of elements
   */
  TQValueList<Element*>& cells() const;

  /**
   * @param iterator the iterator to the element in whose front the new point
   * is inserted
   * @param point the location of the point to be inserted
   * @param multi @c true to allow multiple occurences of a point
   * @return an iterator to the added point or @p iterator, if @p point is not
   * valid or the point is already in the list
   */
  Iterator insert(Iterator iterator, const TQPoint& point, Sheet*, bool multi = true);
  /**
   * @param iterator the iterator to the element in whose front the new range
   * is inserted
   * @param range the location of the range to be inserted
   * @param multi @c true to allow multiple occurences of a range
   * @return an iterator to the added range or @p iterator, if @p range is not
   * valid or the range is already in the list
   */
  Iterator insert(Iterator iterator, const TQRect& range, Sheet*, bool multi = true);

  virtual Point* createPoint(const TQPoint&) const;
  virtual Point* createPoint(const TQString&) const;
  virtual Point* createPoint(const Point&) const;
  virtual Range* createRange(const TQRect&) const;
  virtual Range* createRange(const TQString&) const;
  virtual Range* createRange(const Range&) const;

private:
  class Private;
  Private *d;
};


/***************************************************************************
  class Region::Element
****************************************************************************/
/**
 * Base class for region elements, which can be points or ranges.
 * This class is used by KSpread::Region and could not be used outside of it.
 *
 * Size:
 * m_sheet: 4 bytes
 * vtable: 4 bytes
 * sum: 8 bytes
 */
class Region::Element
{
public:
  enum Type { Undefined, Point, Range };

  Element();
  virtual ~Element();

  virtual Type type() const { return Undefined; }
  virtual bool isValid() const { return false; }
  virtual bool isColumn() const { return false; }
  virtual bool isRow() const { return false; }

  virtual bool contains(const TQPoint&) const { return false; }
  virtual bool contains(const TQRect&) const { return false; }

  virtual TQString name(Sheet* = 0) const { return TQString(""); }
  virtual TQRect rect() const { return TQRect(); }
  virtual const TQColor& color() const { return TQt::black; }

  Sheet* sheet() const { return m_sheet; }
  void setSheet(Sheet* sheet) { m_sheet = sheet; }

protected:
  /* TODO Stefan #6:
      Elaborate, if this pointer could be avoided by TQDict or whatever in
      Region.
  */
  Sheet* m_sheet;
};


/***************************************************************************
  class Region::Point
****************************************************************************/

/**
 * A point in a region.
 * This class is used by KSpread::Region and could not be used outside of it.
 *
 * Size:
 * m_sheet: 4 bytes
 * vtable: 4 bytes
 * m_point: 8 bytes
 * sum: 16 bytes
 */
class Region::Point : public Region::Element
{
public:
  Point(const TQPoint&);
  Point(const TQString&);
  virtual ~Point();

  virtual Type type() const { return Element::Point; }
  virtual bool isValid() const { return (!m_point.isNull() && util_isPointValid(pos())); }
  virtual bool isColumn() const { return false; }
  virtual bool isRow() const { return false; }

  virtual bool contains(const TQPoint&) const;
  virtual bool contains(const TQRect&) const;

  virtual TQString name(Sheet* originSheet = 0) const;

  virtual TQRect rect() const { return TQRect(m_point,m_point); }

  TQPoint pos() const { return m_point; }

private:
  TQPoint m_point;
};


/***************************************************************************
  class Region:.Range
****************************************************************************/

/**
 * A range in a region.
 * This class is used by KSpread::Region and could not be used outside of it.
 * 
 * Size:
 * m_sheet: 4 bytes
 * vtable: 4 bytes
 * m_range: 16 bytes
 * sum: 24 bytes
 */
class Region::Range : public Region::Element
{
public:
  Range(const TQRect&);
  Range(const TQString&);
  virtual ~Range();

  virtual Type type() const { return Element::Range; }
  virtual bool isValid() const { return !m_range.isNull() && util_isRectValid(rect()); }
  virtual bool isColumn() const { return (m_range.normalize().top() == 1 && m_range.normalize().bottom() == KS_rowMax); }
  virtual bool isRow() const { return (m_range.normalize().left() == 1 && m_range.normalize().right() == KS_colMax); }

  virtual bool contains(const TQPoint&) const;
  virtual bool contains(const TQRect&) const;

  virtual TQString name(Sheet* originSheet = 0) const;

  virtual TQRect rect() const { return m_range; }

  int width() const;
  int height() const;

private:
  TQRect m_range;
};

} // namespace KSpread


/***************************************************************************
  kdDebug support
****************************************************************************/

inline kdbgstream operator<<( kdbgstream str, const KSpread::Region& r )
{
  str << "Region = " << r.name();
  return str;
}

#endif // KSPREAD_REGION