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
|
/* This file is part of the KDE project
Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
(C) 2002 Philipp Mueller <philipp.mueller@gmx.de>
(C) 2002 Laurent Montel <montel@kde.org>
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.
*/
// Local
#include "SubtotalDialog.h"
#include "ui_SubtotalWidget.h"
#include "ui_SubtotalsDetailsWidget.h"
// Qt
#include <QCheckBox>
#include <QListWidget>
#include <QVector>
// KDE
#include <kcombobox.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
// KSpread
#include "ui/Selection.h"
#include "Sheet.h"
// commands
#include "commands/DataManipulators.h"
using namespace Calligra::Sheets;
class SubtotalDialog::Private
{
public:
Selection *selection;
Ui::SubtotalsWidget mainWidget;
Ui::SubtotalsDetailsWidget detailsWidget;
};
SubtotalDialog::SubtotalDialog(QWidget* parent, Selection* selection)
: KDialog(parent)
, d(new Private)
{
d->selection = selection;
setCaption(i18n("Subtotals"));
setButtons(Ok | Cancel | Details | User1);
setButtonGuiItem(User1, KGuiItem(i18n("Remove All")));
QWidget* widget = new QWidget(this);
d->mainWidget.setupUi(widget);
setMainWidget(widget);
widget = new QWidget(this);
d->detailsWidget.setupUi(widget);
setDetailsWidget(widget);
fillColumnBoxes();
fillFunctionBox();
connect(this, SIGNAL(user1Clicked()), this, SLOT(slotUser1()));
}
SubtotalDialog::~SubtotalDialog()
{
delete d;
}
void SubtotalDialog::accept()
{
Sheet *const sheet = d->selection->lastSheet();
const QRect range = d->selection->lastRange();
int numOfCols = range.width();
QVector<int> columns(numOfCols);
bool empty = true;
int left = range.left();
for (int i = 0; i < d->mainWidget.m_columnList->count(); ++i) {
QListWidgetItem* item = d->mainWidget.m_columnList->item(i);
if (item->checkState() == Qt::Checked) {
columns[i] = left + i;
empty = false;
} else
columns[i] = -1;
}
if (empty) {
KMessageBox::sorry(this, i18n("You need to select at least one column for adding subtotals."));
return;
}
if (d->detailsWidget.m_replaceSubtotals->isChecked())
removeSubtotalLines();
int mainCol = left + d->mainWidget.m_columnBox->currentIndex();
int bottom = range.bottom();
int top = range.top();
left = range.left();
QString oldText = Cell(sheet, mainCol, top).displayText();
QString newText;
QString result(' ' + i18n("Result"));
int lastChangedRow = top;
bool ignoreEmptyCells = d->detailsWidget.m_IgnoreBox->isChecked();
bool addRow;
if (!d->detailsWidget.m_summaryOnly->isChecked()) {
int y = top + 1;
kDebug() << "Starting in row" << y;
while (y <= bottom) {
addRow = true;
newText = Cell(sheet, mainCol, y).displayText();
if (ignoreEmptyCells && (newText.length() == 0)) {
++y;
kDebug() << "Still the same ->" << y;
continue;
}
if (newText != oldText) {
int saveY = y;
for (int x = 0; x < numOfCols; ++x) {
kDebug() << "Column:" << x << "," << columns[x];
if (columns[x] != -1) {
if (!addSubtotal(mainCol, columns[x], y - 1, lastChangedRow, addRow, oldText + result))
reject();
if (addRow) {
++saveY;
++bottom;
}
addRow = false;
}
}
y = saveY;
lastChangedRow = y;
}
oldText = newText;
++y;
}
addRow = true;
for (int x = 0; x < numOfCols; ++x) {
if (columns[x] != -1) {
if (!addSubtotal(mainCol, columns[x], y - 1, lastChangedRow, addRow, oldText + result))
reject();
addRow = false;
}
}
++y;
}
if (d->detailsWidget.m_summaryBelow->isChecked()) {
addRow = true;
int bottom = range.bottom();
for (int x = 0; x < numOfCols; ++x) {
if (columns[x] != -1) {
addSubtotal(mainCol, columns[x], bottom, top, addRow, i18n("Grand Total"));
addRow = false;
}
}
}
KDialog::accept();
}
void SubtotalDialog::reject()
{
KDialog::reject();
}
void SubtotalDialog::slotUser1()
{
removeSubtotalLines();
KDialog::accept();
}
void SubtotalDialog::removeSubtotalLines()
{
kDebug() << "Removing subtotal lines";
Sheet *const sheet = d->selection->lastSheet();
QRect range = d->selection->lastRange();
int r = range.right();
int l = range.left();
int t = range.top();
Cell cell;
QString text;
for (int y = range.bottom(); y >= t; --y) {
kDebug() << "Checking row:" << y;
bool containsSubtotal = false;
for (int x = l; x <= r; ++x) {
cell = Cell(sheet, x, y);
if (!cell.isFormula())
continue;
text = cell.userInput();
if (text.indexOf("SUBTOTAL") != -1) {
containsSubtotal = true;
break;
}
}
if (containsSubtotal) {
kDebug() << "Line" << y << " contains a subtotal";
QRect rect(l, y, range.width(), 1);
ShiftManipulator* manipulator = new ShiftManipulator();
manipulator->setSheet(sheet);
manipulator->setDirection(ShiftManipulator::ShiftBottom);
manipulator->setReverse(true);
manipulator->add(Region(rect));
manipulator->execute(d->selection->canvas());
range.setHeight(range.height() - 1);
}
}
d->selection->initialize(range, sheet);
kDebug() << "Done removing subtotals";
}
void SubtotalDialog::fillColumnBoxes()
{
Sheet *const sheet = d->selection->lastSheet();
const QRect range = d->selection->lastRange();
int r = range.right();
int row = range.top();
Cell cell;
QListWidgetItem * item;
QString text;
int index = 0;
for (int i = range.left(); i <= r; ++i) {
cell = Cell(sheet, i, row);
text = cell.displayText();
//if ( text.length() > 0 )
{
text = i18n("Column '%1' ", Cell::columnName(i));
}
d->mainWidget.m_columnBox->insertItem(index++, text);
item = new QListWidgetItem(text);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(Qt::Unchecked);
d->mainWidget.m_columnList->addItem(item);
}
}
void SubtotalDialog::fillFunctionBox()
{
QStringList lst;
lst << i18n("Average");
lst << i18n("Count");
lst << i18n("CountA");
lst << i18n("Max");
lst << i18n("Min");
lst << i18n("Product");
lst << i18n("StDev");
lst << i18n("StDevP");
lst << i18n("Sum");
lst << i18n("Var");
lst << i18n("VarP");
d->mainWidget.m_functionBox->insertItems(0, lst);
}
bool SubtotalDialog::addSubtotal(int mainCol, int column, int row, int topRow,
bool addRow, QString const & text)
{
Sheet *const sheet = d->selection->lastSheet();
QRect range = d->selection->lastRange();
kDebug() << "Adding subtotal:" << mainCol << "," << column << ", Rows:" << row << "," << topRow
<< ": addRow: " << addRow << ", Text: " << text << endl;
if (addRow) {
QRect rect(range.left(), row + 1, range.width(), 1);
ShiftManipulator* manipulator = new ShiftManipulator();
manipulator->setSheet(sheet);
manipulator->setDirection(ShiftManipulator::ShiftBottom);
manipulator->add(Region(rect));
manipulator->execute(d->selection->canvas());
range.setHeight(range.height() + 1);
Cell cell = Cell(sheet, mainCol, row + 1);
cell.parseUserInput(text);
Style style;
style.setFontBold(true);
style.setFontItalic(true);
style.setFontUnderline(true);
cell.setStyle(style);
}
QString colName = Cell::columnName(column);
QString formula("=SUBTOTAL(");
formula += QString::number(d->mainWidget.m_functionBox->currentIndex() + 1);
formula += "; ";
formula += colName;
formula += QString::number(topRow);
// if ( topRow != row )
{
formula += ':';
formula += colName;
formula += QString::number(row);
}
formula += ')';
Cell cell = Cell(sheet, column, row + 1);
cell.parseUserInput(formula);
Style style;
style.setFontBold(true);
style.setFontItalic(true);
style.setFontUnderline(true);
cell.setStyle(style);
d->selection->initialize(range, sheet);
return true;
}
#include "SubtotalDialog.moc"
|