20#include <tqstringlist.h>
24#include "kspread_cell.h"
25#include "kspread_doc.h"
26#include "kspread_global.h"
27#include "kspread_map.h"
28#include "kspread_sheet.h"
29#include "kspread_util.h"
30#include "kspread_view.h"
46 TQValueList<Element*> cells;
68 TQStringList substrings = TQStringList::split( ';', string);
69 TQStringList::ConstIterator end = substrings.constEnd();
70 for (TQStringList::ConstIterator it = substrings.constBegin(); it != end; ++it)
72 TQString sRegion = *it;
78 int delimiterPos = sRegion.find( ':');
79 if (delimiterPos > -1)
82 Point ul(sRegion.left(delimiterPos));
83 Point lr(sRegion.mid(delimiterPos + 1));
85 if (ul.isValid() && lr.isValid())
87 Range* range = createRange(sRegion);
88 range->setSheet(sheet);
89 d->cells.append(range);
91 else if (ul.isValid())
93 Point* point = createPoint(sRegion.left(delimiterPos));
94 point->setSheet(sheet);
95 d->cells.append(point);
99 Point* point = createPoint(sRegion.right(delimiterPos + 1));
100 point->setSheet(sheet);
101 d->cells.append(point);
107 Point* point = createPoint(sRegion);
108 point->setSheet(sheet);
109 d->cells.append(point);
120 kdError(36001) << "Region::Region(const TQRect&): TQRect is empty!" << endl;
132 kdError(36001) << "Region::Region(const TQPoint&): TQPoint is empty!" << endl;
141 d->view = list.d->view;
143 ConstIterator end(list.d->cells.constEnd());
144 for (ConstIterator it = list.d->cells.constBegin(); it != end; ++it)
147 if (element->type() == Element::Point)
150 d->cells.append(createPoint(*point));
155 d->cells.append(createRange(*range));
166 kdError(36001) << "Region::Region(int x, int y): Coordinates are invalid!" << endl;
169 add(TQPoint(x,y), sheet);
176 if (x<1 || y<1 || width<1 || height<1)
178 kdError(36001) << "Region::Region(int x, int y, int width, int height): Dimensions are invalid!" << endl;
181 add(TQRect(x,y,width,height), sheet);
204 ConstIterator end = d->cells.constEnd();
205 for (ConstIterator it = d->cells.constBegin(); it != end; ++it)
207 if (!(*it)->isValid())
217 if (d->cells.isEmpty() || d->cells.count() > 1 || (*d->cells.constBegin())->type() != Element::Point)
226 if (d->cells.count() != 1 || ! isValid())
236 ConstIterator endOfList(d->cells.constEnd());
237 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
240 names += element->name(originSheet);
242 return names.isEmpty() ? "" : names.join( ";");
248 if (point.x() < 1 || point.y() < 1)
252 Iterator it = insert(d->cells.end(), point, sheet, false);
253 return (it == d->cells.end()) ? 0 : *it;
258 if (range.normalize().width() == 0 || range.normalize().height() == 0)
262 if (range.size() == TQSize(1,1))
264 return add(range.topLeft(), sheet);
266 Iterator it = insert(d->cells.end(), range, sheet, false);
267 return (it == d->cells.end()) ? 0 : *it;
272 ConstIterator endOfList(region.d->cells.constEnd());
273 for (ConstIterator it = region.d->cells.constBegin(); it != endOfList; ++it)
275 add((*it)->rect(), (*it)->sheet());
277 return d->cells.isEmpty() ? 0 : d->cells.last();
283 Iterator endOfList(d->cells.end());
284 for (Iterator it = d->cells.begin(); it != endOfList; ++it)
287 if (element->rect() == TQRect(point,point))
290 d->cells.remove(element);
299 Iterator endOfList(d->cells.end());
300 for (Iterator it = d->cells.begin(); it != endOfList; ++it)
303 if (element->rect().normalize() == range.normalize())
306 d->cells.remove(element);
314 ConstIterator endOfList(region.constEnd());
315 for (ConstIterator it = region.constBegin(); it != endOfList; ++it)
318 if (element->type() == Element::Point)
325 sub(element->rect());
332 bool containsPoint = false;
334 Iterator it = cells().begin();
335 Iterator endOfList = cells().end();
336 while (it != endOfList)
338 if (!(*it)->contains(point))
343 containsPoint = true;
346 TQRect fullRange = (*it)->rect().normalize();
348 it = cells().remove(it);
351 int left = fullRange.left();
352 int top = fullRange.top();
353 int width = fullRange.width();
354 int height = y - top;
357 insert(it, TQRect(left, top, width, height), sheet);
360 left = fullRange.left();
362 width = TQMAX(0, x - left);
366 insert(it, TQRect(left, top, width, height), sheet);
369 left = TQMIN(x+1, fullRange.right());
371 width = TQMAX(0, fullRange.right() - x);
375 insert(it, TQRect(left, top, width, height), sheet);
378 left = fullRange.left();
380 width = fullRange.width();
381 height = TQMAX(0, fullRange.bottom() - y);
384 insert(it, TQRect(left, top, width, height), sheet);
391 return add(point, sheet);
396Region::Iterator Region::insert(Region::Iterator pos, const TQPoint& point, Sheet* sheet, bool multi)
398 if (point.x() < 1 || point.y() < 1)
403 bool containsPoint = false;
410 Point* rpoint = createPoint(point);
411 rpoint->setSheet(sheet);
412 return d->cells.insert(pos, rpoint);
415 ConstIterator endOfList(d->cells.constEnd());
416 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
419 if (sheet && sheet != element->sheet())
423 if (element->contains(point))
425 containsPoint = true;
440 if ( !containsPoint )
442 Point* rpoint = createPoint(point);
443 rpoint->setSheet(sheet);
444 return d->cells.insert(pos, rpoint);
449Region::Iterator Region::insert(Region::Iterator pos, const TQRect& range, Sheet* sheet, bool multi)
451 if (range.size() == TQSize(1,1))
453 return insert(pos, range.topLeft(), sheet);
458 Range* rrange = createRange(range);
459 rrange->setSheet(sheet);
460 return d->cells.insert(pos, rrange);
463 bool containsRange = false;
465 Iterator it( d->cells.begin() );
466 Iterator endOfList( d->cells.end() );
467 while ( it != endOfList )
469 if (sheet && sheet != (*it)->sheet())
474 if ((*it)->contains(range))
476 containsRange = true;
478 else if (range.contains((*it)->rect()))
481 it = d->cells.remove(it);
486 if ( !containsRange )
488 Range* rrange = createRange(range);
489 rrange->setSheet(sheet);
490 return d->cells.insert(pos, rrange);
497 ConstIterator endOfList(d->cells.constEnd());
498 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
501 TQRect normalizedRegion = element->rect().normalize();
502 if (( int)col >= normalizedRegion.left() && ( int)col <= normalizedRegion.right())
512 ConstIterator endOfList(d->cells.constEnd());
513 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
516 TQRect normalizedRegion = element->rect().normalize();
517 if (( int)row >= normalizedRegion.top() && ( int)row <= normalizedRegion.bottom())
527 ConstIterator endOfList(d->cells.constEnd());
528 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
531 TQRect region = element->rect().normalize();
532 if ((col == 0 || (( int)col >= region.left() && ( int)col <= region.right())) &&
533 region.top() == 1 && region.bottom() == KS_rowMax)
543 ConstIterator endOfList(d->cells.constEnd());
544 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
547 TQRect region = element->rect().normalize();
548 if ((row == 0 || (( int)row >= region.top() && ( int)row <= region.bottom())) &&
549 region.left() == 1 && region.right() == KS_colMax)
559 ConstIterator endOfList(d->cells.constEnd());
560 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
563 TQRect region = element->rect().normalize();
564 if ((region.top() == 1 && region.bottom() == KS_rowMax) ||
565 (region.left() == 1 && region.right() == KS_colMax))
575 if (d->cells.isEmpty())
579 ConstIterator endOfList(d->cells.constEnd());
580 for (ConstIterator it = d->cells.constBegin(); it != endOfList; ++it)
583 if (element->contains(point))
585 if (sheet && element->sheet() != sheet)
597 return d->cells.isEmpty();
602 Iterator end(d->cells.end());
603 for (Iterator it = d->cells.begin(); it != end; it = d->cells.remove(it))
609TQRect Region::boundingRect() const
611 int left = KS_colMax;
615 Region::ConstIterator endOfList = cells().constEnd();
616 for (Region::ConstIterator it = cells().constBegin(); it != endOfList; ++it)
618 TQRect range = (*it)->rect().normalize();
619 if (range.left() < left)
623 if (range.right() > right)
625 right = range.right();
627 if (range.top() < top)
631 if (range.bottom() > bottom)
633 bottom = range.bottom();
636 return TQRect(left, top, right-left+1, bottom-top+1);
639Region::ConstIterator Region::constBegin() const
641 return d->cells.constBegin();
644Region::ConstIterator Region::constEnd() const
646 return d->cells.constEnd();
656 ConstIterator endOfList(d->cells.constEnd());
657 ConstIterator endOfOtherList(other.d->cells.constEnd());
658 ConstIterator it = d->cells.constBegin();
659 ConstIterator it2 = other.d->cells.constBegin();
660 while (it != endOfList && it2 != endOfOtherList)
662 if ((*it++)->rect() != (*it2++)->rect())
672 d->view = other.d->view;
674 ConstIterator end(other.d->cells.constEnd());
675 for (ConstIterator it = other.d->cells.constBegin(); it != end; ++it)
678 if (element->type() == Element::Point)
681 d->cells.append(createPoint(*point));
686 d->cells.append(createRange(*range));
694 int delimiterPos = sRegion.find( '!' );
695 if (delimiterPos > -1)
697 TQString sheetName = sRegion.left(delimiterPos);
699 sRegion = sRegion.right(sRegion.length() - delimiterPos - 1);
700 sheet = d->view->doc()->map()->findSheet(sheetName);
703 kdDebug() << "Sheet " << sheetName << " not found. Using active sheet!" << endl;
704 sheet = d->view->activeSheet();
710Region::Point* Region::createPoint( const TQPoint& point) const
712 return new Point(point);
715Region::Point* Region::createPoint( const TQString& string) const
717 return new Point( string);
722 return new Point(point);
727 return new Range(rect);
730Region::Range* Region::createRange( const TQString& string) const
732 return new Range( string);
737 return new Range(range);
744Region::Element::Element()
749Region::Element::~Element()
758Region::Point::Point( const TQPoint& point)
764Region::Point::Point( const TQString& sCell)
768 uint length = sCell.length();
772 kdDebug(36001) << "Region::Point::init: length = 0" << endl;
776 TQString string = sCell;
781 if ( string[0] == '$')
789 kdDebug(36001) << "Region::Point::init: no point after '$' (string: '" << string.mid(p) << "'" << endl;
793 if ( string[p] < 'A' || string[p] > 'Z')
795 if ( string[p] < 'a' || string[p] > 'z')
797 kdDebug(36001) << "Region::Point::init: wrong first character in point (string: '" << string.mid(p) << "'" << endl;
804 int result = string.find( TQRegExp( "[^A-Za-z]+"), p );
813 kdDebug(36001) << "Region::Point::init: no number in string (string: '" << string.mid( p, result ) << "'" << endl;
821 kdDebug(36001) << "Region::Point::init: column value too high (col: " << x << ")" << endl;
828 kdDebug(36001) << "Region::Point::init: p==length after cols" << endl;
832 if ( string[p] == '$')
838 kdDebug(36001) << "Region::Point::init: p==length after $ of row" << endl;
846 if (!TQChar( string[p++]).isDigit())
848 kdDebug(36001) << "Region::Point::init: no number" << endl;
854 int y = string.mid( p2, p-p2 ).toInt( &ok );
857 kdDebug(36001) << "Region::Point::init: Invalid number (string: '" << string.mid( p2, p-p2 ) << "'" << endl;
862 kdDebug(36001) << "Region::Point::init: row value too high (row: " << y << ")" << endl;
867 kdDebug(36001) << "Region::Point::init: y <= 0" << endl;
871 m_point = TQPoint(x, y);
874Region::Point::~Point()
878TQString Region::Point::name(Sheet* originSheet) const
881 if (m_sheet && m_sheet != originSheet)
883 name = m_sheet->sheetName() + "!";
888bool Region::Point::contains( const TQPoint& point) const
890 return (m_point == point);
893bool Region::Point::contains( const TQRect& range) const
895 return (range.width() == 1) && (range.height() == 1) && (range.topLeft() == m_point);
903Region::Range::Range( const TQRect& rect)
909Region::Range::Range( const TQString& sRange)
913 int delimiterPos = sRange.find( ':');
914 if (delimiterPos == -1)
921 Region::Point ul(sRange.left(delimiterPos));
922 Region::Point lr(sRange.mid(delimiterPos + 1));
924 if (!ul.isValid() || !lr.isValid())
928 m_range = TQRect(ul.pos(), lr.pos());
931Region::Range::~Range()
935TQString Region::Range::name(Sheet* originSheet) const
938 if (m_sheet && m_sheet != originSheet)
940 name = m_sheet->sheetName() + "!";
943 Cell::name(m_range.right(), m_range.bottom() );
946bool Region::Range::contains( const TQPoint& point) const
948 return m_range.normalize().contains(point);
951bool Region::Range::contains( const TQRect& range) const
953 return m_range.normalize().contains(range.normalize());
TQString name() const Returns the name of the cell.
Represents the position of a single cell in a workbook.
Represents a region within a workbook.
Base class for region elements, which can be points or ranges.
The one for all class for points and ranges.
void setView(View *) Sets the view to which this region belongs.
bool isColumnAffected(uint col) const
void operator=(const Region ®ion)
Element * add(const TQPoint &point, Sheet *sheet=0) Adds the point point to this region.
virtual void clear() Deletes all elements of the region.
bool isRowAffected(uint row) const
bool isRowSelected(uint row=0) const
virtual ~Region() Destructor.
bool isContiguous() const
TQString name(Sheet *originSheet=0) const
bool contains(const TQPoint &point, Sheet *sheet=0) const
TQValueList< Element * > & cells() const
bool isColumnSelected(uint col=0) const
bool operator==(const Region ®ion) const
Sheet * filterSheetName(TQString &sRegion)
virtual Element * eor(const TQPoint &point, Sheet *sheet=0)
Iterator insert(Iterator iterator, const TQPoint &point, Sheet *, bool multi=true)
bool isColumnOrRowSelected() const
void sub(const TQPoint &point) Substracts the point point from this region.
The View class displays a KSpread document.
KSPREAD_EXPORT int util_decodeColumnLabelText(const TQString &_col) Call this function to decode the text of a column label to an integer i.e.
|