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.
1010 lines
25 KiB
1010 lines
25 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
|
||
|
Copyright (C) 2005-2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library 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
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include "kspread_canvas.h"
|
||
|
#include "kspread_cell.h"
|
||
|
#include "kspread_doc.h"
|
||
|
#include "kspread_editors.h"
|
||
|
#include "kspread_sheet.h"
|
||
|
#include "kspread_view.h"
|
||
|
#include "kspread_util.h"
|
||
|
|
||
|
#include "selection.h"
|
||
|
|
||
|
// TODO Stefan: Substract points in selections
|
||
|
// TODO Stefan: KPart signal (kspread_events.h)
|
||
|
|
||
|
using namespace KSpread;
|
||
|
|
||
|
/***************************************************************************
|
||
|
class Selection::Private
|
||
|
****************************************************************************/
|
||
|
|
||
|
class Selection::Private
|
||
|
{
|
||
|
public:
|
||
|
Private(View *v)
|
||
|
{
|
||
|
view = v;
|
||
|
sheet = 0;
|
||
14 years ago
|
anchor = TQPoint(1,1);
|
||
|
cursor = TQPoint(1,1);
|
||
|
marker = TQPoint(1,1);
|
||
|
|
||
|
colors.push_back(TQt::red);
|
||
|
colors.push_back(TQt::blue);
|
||
|
colors.push_back(TQt::magenta);
|
||
|
colors.push_back(TQt::darkRed);
|
||
|
colors.push_back(TQt::darkGreen);
|
||
|
colors.push_back(TQt::darkMagenta);
|
||
|
colors.push_back(TQt::darkCyan);
|
||
|
colors.push_back(TQt::darkYellow);
|
||
15 years ago
|
|
||
|
multipleSelection = false;
|
||
|
|
||
|
activeElement = Iterator();
|
||
|
activeSubRegionStart = 0;
|
||
|
activeSubRegionLength = 0;
|
||
|
}
|
||
|
|
||
|
View* view;
|
||
|
Sheet* sheet;
|
||
14 years ago
|
TQPoint anchor;
|
||
|
TQPoint cursor;
|
||
|
TQPoint marker;
|
||
|
TQValueList<TQColor> colors;
|
||
15 years ago
|
|
||
|
bool multipleSelection : 1;
|
||
|
|
||
|
Selection::Iterator activeElement;
|
||
|
uint activeSubRegionStart;
|
||
|
uint activeSubRegionLength;
|
||
|
};
|
||
|
|
||
|
/***************************************************************************
|
||
|
class Selection
|
||
|
****************************************************************************/
|
||
|
namespace KSpread {
|
||
|
|
||
|
Selection::Selection(View *view)
|
||
14 years ago
|
: TQObject(view), Region(1,1)
|
||
15 years ago
|
{
|
||
|
d = new Private(view);
|
||
|
d->activeSubRegionStart = 0;
|
||
|
d->activeSubRegionLength = 1;
|
||
|
}
|
||
|
|
||
|
Selection::Selection(const Selection& selection)
|
||
14 years ago
|
: TQObject(selection.d->view), Region()
|
||
15 years ago
|
{
|
||
|
/* kdDebug() << k_funcinfo << endl;*/
|
||
|
d = new Private(selection.d->view);
|
||
|
d->sheet = selection.d->sheet;
|
||
|
d->activeSubRegionStart = 0;
|
||
|
d->activeSubRegionLength = cells().count();
|
||
|
}
|
||
|
|
||
|
Selection::~Selection()
|
||
|
{
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
14 years ago
|
void Selection::initialize(const TQPoint& point, Sheet* sheet)
|
||
15 years ago
|
{
|
||
|
if (!util_isPointValid(point))
|
||
|
return;
|
||
|
|
||
|
if (!d->view->activeSheet())
|
||
|
return;
|
||
|
|
||
|
if (!sheet)
|
||
|
{
|
||
|
if (d->sheet)
|
||
|
{
|
||
|
sheet = d->sheet;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
sheet = d->view->activeSheet();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Region changedRegion(*this);
|
||
14 years ago
|
changedRegion.add(extendToMergedAreas(TQRect(d->anchor,d->marker)));
|
||
15 years ago
|
|
||
14 years ago
|
TQPoint topLeft(point);
|
||
15 years ago
|
Cell* cell = d->view->activeSheet()->cellAt(point);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
topLeft = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
|
d->anchor = topLeft;
|
||
|
d->cursor = point;
|
||
|
d->marker = topLeft;
|
||
|
|
||
|
fixSubRegionDimension(); // TODO remove this sanity check
|
||
|
Iterator it = cells().begin() += d->activeSubRegionStart + d->activeSubRegionLength;
|
||
|
if (it != insert(it, topLeft, sheet/*, true*/))
|
||
|
{
|
||
|
// if the point was inserted
|
||
|
clearSubRegion();
|
||
|
}
|
||
|
Element* element = *(cells().begin() += d->activeSubRegionStart);
|
||
|
// we end up with one element in the subregion
|
||
|
d->activeSubRegionLength = 1;
|
||
|
if (element && element->type() == Element::Point)
|
||
|
{
|
||
|
Point* point = static_cast<Point*>(element);
|
||
|
point->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
else if (element && element->type() == Element::Range)
|
||
|
{
|
||
|
Range* range = static_cast<Range*>(element);
|
||
|
range->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
|
||
|
d->activeElement = cells().begin();
|
||
|
|
||
|
if (changedRegion == *this)
|
||
|
{
|
||
|
emit changed(Region(topLeft, sheet));
|
||
|
return;
|
||
|
}
|
||
|
changedRegion.add(topLeft, sheet);
|
||
|
|
||
|
emit changed(changedRegion);
|
||
|
}
|
||
|
|
||
14 years ago
|
void Selection::initialize(const TQRect& range, Sheet* sheet)
|
||
15 years ago
|
{
|
||
14 years ago
|
if (!util_isRectValid(range) || ( range == TQRect(0,0,1,1) ))
|
||
15 years ago
|
return;
|
||
|
|
||
|
if (!sheet)
|
||
|
{
|
||
|
if (d->sheet)
|
||
|
{
|
||
|
sheet = d->sheet;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
sheet = d->view->activeSheet();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Region changedRegion(*this);
|
||
14 years ago
|
changedRegion.add(extendToMergedAreas(TQRect(d->anchor,d->marker)));
|
||
15 years ago
|
|
||
14 years ago
|
TQPoint topLeft(range.topLeft());
|
||
15 years ago
|
Cell* cell = d->view->activeSheet()->cellAt(topLeft);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
topLeft = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQPoint bottomRight(range.bottomRight());
|
||
15 years ago
|
cell = d->view->activeSheet()->cellAt(bottomRight);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
bottomRight = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
|
d->anchor = topLeft;
|
||
|
d->cursor = bottomRight;
|
||
|
d->marker = bottomRight;
|
||
|
|
||
|
fixSubRegionDimension(); // TODO remove this sanity check
|
||
|
Iterator it = cells().begin() += d->activeSubRegionStart + d->activeSubRegionLength;
|
||
14 years ago
|
if (it != insert(it, TQRect(topLeft, bottomRight), sheet/*, true*/))
|
||
15 years ago
|
{
|
||
|
// if the range was inserted
|
||
|
clearSubRegion();
|
||
|
}
|
||
|
|
||
|
Element* element = *(cells().begin() += d->activeSubRegionStart);
|
||
|
// we end up with one element in the subregion
|
||
|
d->activeSubRegionLength = 1;
|
||
|
if (element && element->type() == Element::Point)
|
||
|
{
|
||
|
Point* point = static_cast<Point*>(element);
|
||
|
point->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
else if (element && element->type() == Element::Range)
|
||
|
{
|
||
|
Range* range = static_cast<Range*>(element);
|
||
|
range->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
|
||
|
d->activeElement = cells().begin();
|
||
|
|
||
|
if (changedRegion == *this)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
14 years ago
|
changedRegion.add(TQRect(topLeft, bottomRight), sheet);
|
||
15 years ago
|
|
||
|
emit changed(changedRegion);
|
||
|
}
|
||
|
|
||
|
void Selection::initialize(const Region& region, Sheet* sheet)
|
||
|
{
|
||
|
if (!region.isValid())
|
||
|
return;
|
||
|
|
||
|
if (!sheet)
|
||
|
{
|
||
|
if (d->sheet)
|
||
|
{
|
||
|
sheet = d->sheet;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
sheet = d->view->activeSheet();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Region changedRegion(*this);
|
||
14 years ago
|
changedRegion.add(extendToMergedAreas(TQRect(d->anchor,d->marker)));
|
||
15 years ago
|
|
||
|
// TODO Stefan: handle subregion insertion
|
||
|
// TODO Stefan: handle obscured cells correctly
|
||
|
clear();
|
||
|
Element* element = add(region);
|
||
|
if (element && element->type() == Element::Point)
|
||
|
{
|
||
|
Point* point = static_cast<Point*>(element);
|
||
|
point->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
else if (element && element->type() == Element::Range)
|
||
|
{
|
||
|
Range* range = static_cast<Range*>(element);
|
||
|
range->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
|
||
14 years ago
|
TQPoint topLeft(cells().last()->rect().normalize().topLeft());
|
||
15 years ago
|
Cell* cell = d->view->activeSheet()->cellAt(topLeft);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
topLeft = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQPoint bottomRight(cells().last()->rect().normalize().bottomRight());
|
||
15 years ago
|
cell = d->view->activeSheet()->cellAt(bottomRight);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
bottomRight = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
|
d->anchor = topLeft;
|
||
|
d->cursor = topLeft;
|
||
|
d->marker = bottomRight;
|
||
|
|
||
|
d->activeElement = --cells().end();
|
||
|
d->activeSubRegionStart = 0;
|
||
|
d->activeSubRegionLength = cells().count();
|
||
|
|
||
|
if (changedRegion == *this)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
changedRegion.add( region );
|
||
|
|
||
|
emit changed(changedRegion);
|
||
|
}
|
||
|
|
||
|
void Selection::update()
|
||
|
{
|
||
|
emit changed(*this);
|
||
|
}
|
||
|
|
||
14 years ago
|
void Selection::update(const TQPoint& point)
|
||
15 years ago
|
{
|
||
|
uint count = cells().count();
|
||
|
|
||
|
if (cells().isEmpty())
|
||
|
{
|
||
|
add(point);
|
||
|
d->activeSubRegionLength += cells().count() - count;
|
||
|
return;
|
||
|
}
|
||
|
if (d->activeElement == cells().end())
|
||
|
{
|
||
|
// we're not empty, so this will not become again end()
|
||
|
d->activeElement--;
|
||
|
}
|
||
|
|
||
|
Sheet* sheet = (*d->activeElement)->sheet();
|
||
|
if (sheet != d->view->activeSheet())
|
||
|
{
|
||
|
extend(point);
|
||
|
d->activeSubRegionLength += cells().count() - count;
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQPoint topLeft(point);
|
||
15 years ago
|
Cell* cell = d->view->activeSheet()->cellAt(point);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
topLeft = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
|
if (topLeft == d->marker)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQRect area1 = (*d->activeElement)->rect().normalize();
|
||
|
TQRect newRange = extendToMergedAreas(TQRect(d->anchor, topLeft));
|
||
15 years ago
|
|
||
|
Element* oldElement = *d->activeElement;
|
||
|
// returns iterator to the next element or end
|
||
|
Iterator it = cells().remove(d->activeElement);
|
||
|
delete oldElement;
|
||
|
// returns iterator to the new element (before 'it') or 'it'
|
||
|
d->activeElement = insert(it, newRange, sheet, d->multipleSelection);
|
||
|
d->activeSubRegionLength += cells().count() - count;
|
||
|
|
||
|
// The call to insert() above can just return the iterator which has been
|
||
|
// passed in. This may be cells.end(), if the old active element was the
|
||
|
// iterator to the list's end (!= last element). So attempts to dereference
|
||
|
// it will fail.
|
||
|
if (d->activeElement == cells().end())
|
||
|
{
|
||
|
d->activeElement--;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQRect area2 = (*d->activeElement)->rect().normalize();
|
||
15 years ago
|
Region changedRegion;
|
||
|
|
||
|
bool newLeft = area1.left() != area2.left();
|
||
|
bool newTop = area1.top() != area2.top();
|
||
|
bool newRight = area1.right() != area2.right();
|
||
|
bool newBottom = area1.bottom() != area2.bottom();
|
||
|
|
||
|
/* first, calculate some numbers that we'll use a few times */
|
||
14 years ago
|
int farLeft = TQMIN(area1.left(), area2.left());
|
||
|
int innerLeft = TQMAX(area1.left(), area2.left());
|
||
15 years ago
|
|
||
14 years ago
|
int farTop = TQMIN(area1.top(), area2.top());
|
||
|
int innerTop = TQMAX(area1.top(), area2.top());
|
||
15 years ago
|
|
||
14 years ago
|
int farRight = TQMAX(area1.right(), area2.right());
|
||
|
int innerRight = TQMIN(area1.right(), area2.right());
|
||
15 years ago
|
|
||
14 years ago
|
int farBottom = TQMAX(area1.bottom(), area2.bottom());
|
||
|
int innerBottom = TQMIN(area1.bottom(), area2.bottom());
|
||
15 years ago
|
|
||
|
if (newLeft)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(farLeft, innerTop),
|
||
|
TQPoint(innerLeft-1, innerBottom)));
|
||
15 years ago
|
if (newTop)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(farLeft, farTop),
|
||
|
TQPoint(innerLeft-1, innerTop-1)));
|
||
15 years ago
|
}
|
||
|
if (newBottom)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(farLeft, innerBottom+1),
|
||
|
TQPoint(innerLeft-1, farBottom)));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (newTop)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(innerLeft, farTop),
|
||
|
TQPoint(innerRight, innerTop-1)));
|
||
15 years ago
|
}
|
||
|
|
||
|
if (newRight)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(innerRight+1, innerTop),
|
||
|
TQPoint(farRight, innerBottom)));
|
||
15 years ago
|
if (newTop)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(innerRight+1, farTop),
|
||
|
TQPoint(farRight, innerTop-1)));
|
||
15 years ago
|
}
|
||
|
if (newBottom)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(innerRight+1, innerBottom+1),
|
||
|
TQPoint(farRight, farBottom)));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (newBottom)
|
||
|
{
|
||
14 years ago
|
changedRegion.add(TQRect(TQPoint(innerLeft, innerBottom+1),
|
||
|
TQPoint(innerRight, farBottom)));
|
||
15 years ago
|
}
|
||
|
|
||
|
d->marker = topLeft;
|
||
|
d->cursor = point;
|
||
|
|
||
|
emit changed(changedRegion);
|
||
|
}
|
||
|
|
||
14 years ago
|
void Selection::extend(const TQPoint& point, Sheet* sheet)
|
||
15 years ago
|
{
|
||
|
if (!util_isPointValid(point))
|
||
|
return;
|
||
|
|
||
|
if (isEmpty())
|
||
|
{
|
||
|
initialize(point, sheet);
|
||
|
return;
|
||
|
}
|
||
|
if (d->activeElement == cells().end())
|
||
|
{
|
||
|
// we're not empty, so this will not become again end()
|
||
|
d->activeElement--;
|
||
|
}
|
||
|
|
||
|
if (!sheet)
|
||
|
{
|
||
|
if (d->sheet)
|
||
|
{
|
||
|
sheet = d->sheet;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
sheet = d->view->activeSheet();
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
Region changedRegion = Region(extendToMergedAreas(TQRect(d->marker,d->marker)));
|
||
15 years ago
|
|
||
14 years ago
|
TQPoint topLeft(point);
|
||
15 years ago
|
Cell* cell = d->view->activeSheet()->cellAt(point);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
topLeft = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
|
uint count = cells().count();
|
||
|
if (d->multipleSelection)
|
||
|
{
|
||
|
d->activeElement = insert(++d->activeElement, point, sheet, false);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
eor(topLeft, sheet);
|
||
|
d->activeElement = --cells().end();
|
||
|
}
|
||
|
d->anchor = (*d->activeElement)->rect().topLeft();
|
||
|
d->cursor = (*d->activeElement)->rect().bottomRight();
|
||
|
d->marker = d->cursor;
|
||
|
|
||
|
d->activeSubRegionLength += cells().count() - count;
|
||
|
|
||
|
changedRegion.add(topLeft, sheet);
|
||
|
changedRegion.add(*this);
|
||
|
|
||
|
emit changed(changedRegion);
|
||
|
}
|
||
|
|
||
14 years ago
|
void Selection::extend(const TQRect& range, Sheet* sheet)
|
||
15 years ago
|
{
|
||
14 years ago
|
//See comment in Selection::initialize(const TQRect& range, Sheet* sheet)
|
||
|
if (!util_isRectValid(range) || (range == TQRect(0,0,1,1)))
|
||
15 years ago
|
return;
|
||
|
|
||
|
if (isEmpty())
|
||
|
{
|
||
|
initialize(range, sheet);
|
||
|
return;
|
||
|
}
|
||
|
if (d->activeElement == cells().end())
|
||
|
{
|
||
|
// we're not empty, so this will not become again end()
|
||
|
d->activeElement--;
|
||
|
}
|
||
|
|
||
|
if (!sheet)
|
||
|
{
|
||
|
if (d->sheet)
|
||
|
{
|
||
|
sheet = d->sheet;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
sheet = d->view->activeSheet();
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
TQPoint topLeft(range.topLeft());
|
||
15 years ago
|
Cell* cell = d->view->activeSheet()->cellAt(topLeft);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
topLeft = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQPoint bottomRight(range.bottomRight());
|
||
15 years ago
|
cell = d->view->activeSheet()->cellAt(bottomRight);
|
||
|
if (cell->isObscured() && cell->isPartOfMerged())
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
bottomRight = TQPoint(cell->column(), cell->row());
|
||
15 years ago
|
}
|
||
|
|
||
|
d->anchor = topLeft;
|
||
|
d->cursor = topLeft;
|
||
|
d->marker = bottomRight;
|
||
|
|
||
|
uint count = cells().count();
|
||
|
Element* element;
|
||
|
if (d->multipleSelection)
|
||
|
{
|
||
14 years ago
|
d->activeElement = insert(++d->activeElement, extendToMergedAreas(TQRect(topLeft, bottomRight)).normalize(), sheet, false);
|
||
15 years ago
|
element = (d->activeElement == cells().end()) ? 0 : *d->activeElement;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
element = add(extendToMergedAreas(TQRect(topLeft, bottomRight)).normalize(), sheet);
|
||
15 years ago
|
d->activeElement = --cells().end();
|
||
|
}
|
||
|
if (element && element->type() == Element::Point)
|
||
|
{
|
||
|
Point* point = static_cast<Point*>(element);
|
||
|
point->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
else if (element && element->type() == Element::Range)
|
||
|
{
|
||
|
Range* range = static_cast<Range*>(element);
|
||
|
range->setColor(d->colors[cells().size() % d->colors.size()]);
|
||
|
}
|
||
|
|
||
|
d->activeSubRegionLength += cells().count() - count;
|
||
|
|
||
|
emit changed(*this);
|
||
|
}
|
||
|
|
||
|
void Selection::extend(const Region& region)
|
||
|
{
|
||
|
if (!region.isValid())
|
||
|
return;
|
||
|
|
||
|
uint count = cells().count();
|
||
|
ConstIterator end(region.constEnd());
|
||
|
for (ConstIterator it = region.constBegin(); it != end; ++it)
|
||
|
{
|
||
|
Element *element = *it;
|
||
|
if (!element) continue;
|
||
|
if (element->type() == Element::Point)
|
||
|
{
|
||
|
Point* point = static_cast<Point*>(element);
|
||
|
extend(point->pos(), element->sheet());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
extend(element->rect(), element->sheet());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
d->activeSubRegionLength += cells().count() - count;
|
||
|
|
||
|
emit changed(*this);
|
||
|
}
|
||
|
|
||
14 years ago
|
Selection::Element* Selection::eor(const TQPoint& point, Sheet* sheet)
|
||
15 years ago
|
{
|
||
|
if (isSingular())
|
||
|
{
|
||
|
return Region::add(point, sheet);
|
||
|
}
|
||
|
return Region::eor(point, sheet);
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQPoint& Selection::anchor() const
|
||
15 years ago
|
{
|
||
|
return d->anchor;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQPoint& Selection::cursor() const
|
||
15 years ago
|
{
|
||
|
return d->cursor;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQPoint& Selection::marker() const
|
||
15 years ago
|
{
|
||
|
return d->marker;
|
||
|
}
|
||
|
|
||
|
bool Selection::isSingular() const
|
||
|
{
|
||
|
return Region::isSingular();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQRect Selection::selectionHandleArea() const
|
||
15 years ago
|
{
|
||
|
int column, row;
|
||
|
|
||
|
// complete rows/columns are selected, use the marker.
|
||
|
if (isColumnOrRowSelected())
|
||
|
{
|
||
|
column = d->marker.x();
|
||
|
row = d->marker.y();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
column = lastRange().right();
|
||
|
row = lastRange().bottom();
|
||
|
}
|
||
|
const Cell* cell = d->view->activeSheet()->cellAt(column, row);
|
||
|
|
||
|
double xpos = d->view->activeSheet()->dblColumnPos( column );
|
||
|
double ypos = d->view->activeSheet()->dblRowPos( row );
|
||
|
double width = cell->dblWidth( column );
|
||
|
double height = cell->dblHeight( row );
|
||
|
|
||
14 years ago
|
TQPoint rightBottom( d->view->doc()->zoomItX( xpos + width ),
|
||
15 years ago
|
d->view->doc()->zoomItY( ypos + height ) );
|
||
|
|
||
14 years ago
|
TQRect handle( ( rightBottom.x() - 2 ),
|
||
15 years ago
|
( rightBottom.y() - 2 ),
|
||
|
( 5 ),
|
||
|
( 5 ) );
|
||
|
return handle;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Selection::name(Sheet* sheet) const
|
||
15 years ago
|
{
|
||
|
return Region::name(sheet ? sheet : d->sheet);
|
||
|
}
|
||
|
|
||
|
void Selection::setSheet(Sheet* sheet)
|
||
|
{
|
||
|
d->sheet = sheet;
|
||
|
}
|
||
|
|
||
|
Sheet* Selection::sheet() const
|
||
|
{
|
||
|
return d->sheet;
|
||
|
}
|
||
|
|
||
14 years ago
|
void Selection::setActiveElement(const TQPoint& point)
|
||
15 years ago
|
{
|
||
|
uint counter = 0;
|
||
|
Iterator end = cells().end();
|
||
|
for (Iterator it = cells().begin(); it != end; ++it)
|
||
|
{
|
||
14 years ago
|
TQRect range = (*it)->rect();
|
||
15 years ago
|
if (range.topLeft() == point || range.bottomRight() == point)
|
||
|
{
|
||
|
d->anchor = range.topLeft();
|
||
|
d->cursor = range.bottomRight();
|
||
|
d->marker = range.bottomRight();
|
||
|
d->activeElement = it;
|
||
|
d->activeSubRegionStart = counter;
|
||
|
d->activeSubRegionLength = 1;
|
||
|
if (d->view->canvasWidget()->editor())
|
||
|
{
|
||
|
d->view->canvasWidget()->editor()->setCursorToRange(counter);
|
||
|
}
|
||
|
}
|
||
|
counter++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Selection::setActiveElement(uint pos)
|
||
|
{
|
||
|
if (pos >= cells().count())
|
||
|
{
|
||
|
kdDebug() << "Selection::setActiveElement: position exceeds list" << endl;
|
||
|
d->activeElement = cells().begin();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Iterator it = cells().begin() += pos;
|
||
14 years ago
|
TQRect range = (*it)->rect();
|
||
15 years ago
|
d->anchor = range.topLeft();
|
||
|
d->cursor = range.bottomRight();
|
||
|
d->marker = range.bottomRight();
|
||
|
d->activeElement = it;
|
||
|
}
|
||
|
|
||
|
Region::Element* Selection::activeElement() const
|
||
|
{
|
||
|
return (d->activeElement == cells().end()) ? 0 : *d->activeElement;
|
||
|
}
|
||
|
|
||
|
void Selection::clear()
|
||
|
{
|
||
|
d->activeSubRegionStart = 0;
|
||
|
d->activeSubRegionLength = 0;
|
||
|
Region::clear();
|
||
|
d->activeElement = cells().begin();
|
||
|
}
|
||
|
|
||
|
void Selection::clearSubRegion()
|
||
|
{
|
||
|
if (isEmpty())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
// kdDebug() << *this << endl;
|
||
|
// kdDebug() << d->activeSubRegionStart << endl;
|
||
|
// kdDebug() << d->activeSubRegionLength << endl;
|
||
|
|
||
|
Iterator it = cells().begin();
|
||
|
Iterator end = it += d->activeSubRegionStart;
|
||
|
end += d->activeSubRegionLength;
|
||
|
while (it != end)
|
||
|
{
|
||
|
/* kdDebug() << (*it)->name() << endl;*/
|
||
|
delete *it;
|
||
|
it = cells().remove(it);
|
||
|
}
|
||
|
d->activeSubRegionLength = 0;
|
||
|
d->activeElement = it;
|
||
|
/* kdDebug() << "ENDE" << endl;*/
|
||
|
}
|
||
|
|
||
|
void Selection::fixSubRegionDimension()
|
||
|
{
|
||
|
if (d->activeSubRegionStart > cells().count())
|
||
|
{
|
||
|
kdDebug() << "Selection::fixSubRegionDimension: start position exceeds list" << endl;
|
||
|
d->activeSubRegionStart = 0;
|
||
|
d->activeSubRegionLength = cells().count();
|
||
|
return;
|
||
|
}
|
||
|
if (d->activeSubRegionStart + d->activeSubRegionLength > cells().count())
|
||
|
{
|
||
|
kdDebug() << "Selection::fixSubRegionDimension: length exceeds list" << endl;
|
||
|
d->activeSubRegionLength = cells().count() - d->activeSubRegionStart;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Selection::setActiveSubRegion(uint start, uint length)
|
||
|
{
|
||
|
// kdDebug() << k_funcinfo << endl;
|
||
|
d->activeSubRegionStart = start;
|
||
|
d->activeSubRegionLength = length;
|
||
|
fixSubRegionDimension();
|
||
|
d->activeElement = cells().begin() += d->activeSubRegionStart;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Selection::activeSubRegionName() const
|
||
15 years ago
|
{
|
||
|
// kdDebug() << k_funcinfo << endl;
|
||
|
// kdDebug() << *this << endl;
|
||
|
// kdDebug() << "start = " << d->activeSubRegionStart << ", len = " << d->activeSubRegionLength << endl;
|
||
|
|
||
14 years ago
|
TQStringList names;
|
||
15 years ago
|
Iterator it = cells().begin();
|
||
|
it += d->activeSubRegionStart;
|
||
|
Iterator end = it;
|
||
|
end += d->activeSubRegionLength;
|
||
|
while (it != end)
|
||
|
{
|
||
|
names += (*it++)->name(d->sheet);
|
||
|
}
|
||
|
/* kdDebug() << "ENDE" << endl;*/
|
||
|
return names.isEmpty() ? "" : names.join(";");
|
||
|
}
|
||
|
|
||
|
void Selection::setMultipleSelection(bool state)
|
||
|
{
|
||
|
d->multipleSelection = state;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQValueList<TQColor>& Selection::colors() const
|
||
15 years ago
|
{
|
||
|
return d->colors;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQRect Selection::lastRange(bool extend) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQRect selection = TQRect(d->anchor, d->marker).normalize();
|
||
15 years ago
|
return extend ? extendToMergedAreas(selection) : selection;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQRect Selection::selection(bool extend) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQRect selection = TQRect(d->anchor, d->marker).normalize();
|
||
15 years ago
|
return extend ? extendToMergedAreas(selection) : selection;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQRect Selection::extendToMergedAreas(TQRect area) const
|
||
15 years ago
|
{
|
||
|
if (!d->view->activeSheet())
|
||
|
return area;
|
||
|
|
||
|
area = area.normalize(); // TODO Stefan: avoid this
|
||
|
const Cell *cell = d->view->activeSheet()->cellAt(area.left(), area.top());
|
||
|
|
||
|
if( Region::Range(area).isColumn() || Region::Range(area).isRow() )
|
||
|
{
|
||
|
return area;
|
||
|
}
|
||
|
else if ( !(cell->isObscured() && cell->isPartOfMerged()) &&
|
||
|
(cell->mergedXCells() + 1) >= area.width() &&
|
||
|
(cell->mergedYCells() + 1) >= area.height())
|
||
|
{
|
||
|
/* if just a single cell is selected, we need to merge even when
|
||
|
the obscuring isn't forced. But only if this is the cell that
|
||
|
is doing the obscuring -- we still want to be able to click on a cell
|
||
|
that is being obscured.
|
||
|
*/
|
||
|
area.setWidth(cell->mergedXCells() + 1);
|
||
|
area.setHeight(cell->mergedYCells() + 1);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int top=area.top();
|
||
|
int left=area.left();
|
||
|
int bottom=area.bottom();
|
||
|
int right=area.right();
|
||
|
for ( int x = area.left(); x <= area.right(); x++ )
|
||
|
for ( int y = area.top(); y <= area.bottom(); y++ )
|
||
|
{
|
||
|
cell = d->view->activeSheet()->cellAt( x, y );
|
||
|
if( cell->doesMergeCells())
|
||
|
{
|
||
14 years ago
|
right=TQMAX(right,cell->mergedXCells()+x);
|
||
|
bottom=TQMAX(bottom,cell->mergedYCells()+y);
|
||
15 years ago
|
}
|
||
|
else if ( cell->isObscured() && cell->isPartOfMerged() )
|
||
|
{
|
||
|
cell = cell->obscuringCells().first();
|
||
14 years ago
|
left=TQMIN(left,cell->column());
|
||
|
top=TQMIN(top,cell->row());
|
||
|
bottom=TQMAX(bottom,cell->row() + cell->mergedYCells());
|
||
|
right=TQMAX(right,cell->column() + cell->mergedXCells());
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
area.setCoords(left,top,right,bottom);
|
||
|
}
|
||
|
return area;
|
||
|
}
|
||
|
|
||
14 years ago
|
Selection::Region::Point* Selection::createPoint(const TQPoint& point) const
|
||
15 years ago
|
{
|
||
|
return new Point(point);
|
||
|
}
|
||
|
|
||
14 years ago
|
Selection::Region::Point* Selection::createPoint(const TQString& string) const
|
||
15 years ago
|
{
|
||
|
return new Point(string);
|
||
|
}
|
||
|
|
||
|
Selection::Region::Point* Selection::createPoint(const Point& point) const
|
||
|
{
|
||
|
return new Point(point);
|
||
|
}
|
||
|
|
||
14 years ago
|
Selection::Region::Range* Selection::createRange(const TQRect& rect) const
|
||
15 years ago
|
{
|
||
|
return new Range(rect);
|
||
|
}
|
||
|
|
||
14 years ago
|
Selection::Region::Range* Selection::createRange(const TQString& string) const
|
||
15 years ago
|
{
|
||
|
return new Range(string);
|
||
|
}
|
||
|
|
||
|
Selection::Region::Range* Selection::createRange(const Range& range) const
|
||
|
{
|
||
|
return new Range(range);
|
||
|
}
|
||
|
|
||
|
/***************************************************************************
|
||
|
class Point
|
||
|
****************************************************************************/
|
||
|
|
||
14 years ago
|
Selection::Point::Point(const TQPoint& point)
|
||
15 years ago
|
: Region::Point(point),
|
||
14 years ago
|
m_color(TQt::black),
|
||
15 years ago
|
m_columnFixed(false),
|
||
|
m_rowFixed(false)
|
||
|
{
|
||
|
}
|
||
|
|
||
14 years ago
|
Selection::Point::Point(const TQString& string)
|
||
15 years ago
|
: Region::Point(string),
|
||
14 years ago
|
m_color(TQt::black),
|
||
15 years ago
|
m_columnFixed(false),
|
||
|
m_rowFixed(false)
|
||
|
{
|
||
|
if (!isValid())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
uint p = 0;
|
||
|
// Fixed?
|
||
|
if (string[p++] == '$')
|
||
|
{
|
||
|
m_columnFixed = true;
|
||
|
}
|
||
|
|
||
|
//search for the first character != text
|
||
14 years ago
|
int result = string.find( TQRegExp("[^A-Za-z]+"), p );
|
||
15 years ago
|
if (string[result] == '$')
|
||
|
{
|
||
|
m_rowFixed = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/***************************************************************************
|
||
|
class Range
|
||
|
****************************************************************************/
|
||
|
|
||
14 years ago
|
Selection::Range::Range(const TQRect& range)
|
||
15 years ago
|
: Region::Range(range),
|
||
14 years ago
|
m_color(TQt::black),
|
||
15 years ago
|
m_leftFixed(false),
|
||
|
m_rightFixed(false),
|
||
|
m_topFixed(false),
|
||
|
m_bottomFixed(false)
|
||
|
{
|
||
|
}
|
||
|
|
||
14 years ago
|
Selection::Range::Range(const TQString& string)
|
||
15 years ago
|
: Region::Range(string),
|
||
14 years ago
|
m_color(TQt::black),
|
||
15 years ago
|
m_leftFixed(false),
|
||
|
m_rightFixed(false),
|
||
|
m_topFixed(false),
|
||
|
m_bottomFixed(false)
|
||
|
{
|
||
|
if (!isValid())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
int delimiterPos = string.find(':');
|
||
15 years ago
|
if (delimiterPos == -1)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Selection::Point ul(string.left(delimiterPos));
|
||
|
Selection::Point lr(string.mid(delimiterPos + 1));
|
||
|
|
||
|
if (!ul.isValid() || !lr.isValid())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
m_leftFixed = ul.columnFixed();
|
||
|
m_rightFixed = lr.columnFixed();
|
||
|
m_topFixed = ul.rowFixed();
|
||
|
m_bottomFixed = lr.rowFixed();
|
||
|
}
|
||
|
|
||
|
} // namespace KSpread
|
||
|
#include "selection.moc"
|