[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include <QtGui>
|
---|
| 43 | #include "spreadsheet.h"
|
---|
| 44 | #include "spreadsheetdelegate.h"
|
---|
| 45 | #include "spreadsheetitem.h"
|
---|
| 46 | #include "printview.h"
|
---|
| 47 |
|
---|
| 48 | SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
|
---|
| 49 | : QMainWindow(parent)
|
---|
| 50 | {
|
---|
| 51 | addToolBar(toolBar = new QToolBar());
|
---|
| 52 | formulaInput = new QLineEdit();
|
---|
| 53 |
|
---|
| 54 | cellLabel = new QLabel(toolBar);
|
---|
| 55 | cellLabel->setMinimumSize(80, 0);
|
---|
| 56 |
|
---|
| 57 | toolBar->addWidget(cellLabel);
|
---|
| 58 | toolBar->addWidget(formulaInput);
|
---|
| 59 |
|
---|
| 60 | table = new QTableWidget(rows, cols, this);
|
---|
| 61 | for (int c = 0; c < cols; ++c) {
|
---|
| 62 | QString character(QChar('A' + c));
|
---|
| 63 | table->setHorizontalHeaderItem(c, new QTableWidgetItem(character));
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | table->setItemPrototype(table->item(rows -1, cols - 1));
|
---|
| 67 | table->setItemDelegate(new SpreadSheetDelegate());
|
---|
| 68 |
|
---|
| 69 | createActions();
|
---|
| 70 | updateColor(0);
|
---|
| 71 | setupMenuBar();
|
---|
| 72 | setupContents();
|
---|
[846] | 73 | setupContextMenu();
|
---|
[2] | 74 | setCentralWidget(table);
|
---|
| 75 |
|
---|
| 76 | statusBar();
|
---|
[561] | 77 | connect(table, SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)),
|
---|
[2] | 78 | this, SLOT(updateStatus(QTableWidgetItem*)));
|
---|
[561] | 79 | connect(table, SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)),
|
---|
[2] | 80 | this, SLOT(updateColor(QTableWidgetItem*)));
|
---|
| 81 | connect(table, SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)),
|
---|
| 82 | this, SLOT(updateLineEdit(QTableWidgetItem*)));
|
---|
| 83 | connect(table, SIGNAL(itemChanged(QTableWidgetItem*)),
|
---|
| 84 | this, SLOT(updateStatus(QTableWidgetItem*)));
|
---|
| 85 | connect(formulaInput, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
|
---|
| 86 | connect(table, SIGNAL(itemChanged(QTableWidgetItem*)),
|
---|
| 87 | this, SLOT(updateLineEdit(QTableWidgetItem*)));
|
---|
| 88 |
|
---|
| 89 | setWindowTitle(tr("Spreadsheet"));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void SpreadSheet::createActions()
|
---|
| 93 | {
|
---|
| 94 | cell_sumAction = new QAction(tr("Sum"), this);
|
---|
| 95 | connect(cell_sumAction, SIGNAL(triggered()), this, SLOT(actionSum()));
|
---|
| 96 |
|
---|
| 97 | cell_addAction = new QAction(tr("&Add"), this);
|
---|
| 98 | cell_addAction->setShortcut(Qt::CTRL | Qt::Key_Plus);
|
---|
| 99 | connect(cell_addAction, SIGNAL(triggered()), this, SLOT(actionAdd()));
|
---|
| 100 |
|
---|
| 101 | cell_subAction = new QAction(tr("&Subtract"), this);
|
---|
| 102 | cell_subAction->setShortcut(Qt::CTRL | Qt::Key_Minus);
|
---|
| 103 | connect(cell_subAction, SIGNAL(triggered()), this, SLOT(actionSubtract()));
|
---|
| 104 |
|
---|
| 105 | cell_mulAction = new QAction(tr("&Multiply"), this);
|
---|
| 106 | cell_mulAction->setShortcut(Qt::CTRL | Qt::Key_multiply);
|
---|
| 107 | connect(cell_mulAction, SIGNAL(triggered()), this, SLOT(actionMultiply()));
|
---|
| 108 |
|
---|
| 109 | cell_divAction = new QAction(tr("&Divide"), this);
|
---|
| 110 | cell_divAction->setShortcut(Qt::CTRL | Qt::Key_division);
|
---|
| 111 | connect(cell_divAction, SIGNAL(triggered()), this, SLOT(actionDivide()));
|
---|
| 112 |
|
---|
| 113 | fontAction = new QAction(tr("Font..."), this);
|
---|
| 114 | fontAction->setShortcut(Qt::CTRL | Qt::Key_F);
|
---|
| 115 | connect(fontAction, SIGNAL(triggered()), this, SLOT(selectFont()));
|
---|
| 116 |
|
---|
| 117 | colorAction = new QAction(QPixmap(16, 16), tr("Background &Color..."), this);
|
---|
| 118 | connect(colorAction, SIGNAL(triggered()), this, SLOT(selectColor()));
|
---|
| 119 |
|
---|
| 120 | clearAction = new QAction(tr("Clear"), this);
|
---|
| 121 | clearAction->setShortcut(Qt::Key_Delete);
|
---|
| 122 | connect(clearAction, SIGNAL(triggered()), this, SLOT(clear()));
|
---|
| 123 |
|
---|
| 124 | aboutSpreadSheet = new QAction(tr("About Spreadsheet"), this);
|
---|
| 125 | connect(aboutSpreadSheet, SIGNAL(triggered()), this, SLOT(showAbout()));
|
---|
| 126 |
|
---|
| 127 | exitAction = new QAction(tr("E&xit"), this);
|
---|
| 128 | connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
---|
| 129 |
|
---|
| 130 | printAction = new QAction(tr("&Print"), this);
|
---|
| 131 | connect(printAction, SIGNAL(triggered()), this, SLOT(print()));
|
---|
| 132 |
|
---|
| 133 | firstSeparator = new QAction(this);
|
---|
| 134 | firstSeparator->setSeparator(true);
|
---|
| 135 |
|
---|
| 136 | secondSeparator = new QAction(this);
|
---|
| 137 | secondSeparator->setSeparator(true);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void SpreadSheet::setupMenuBar()
|
---|
| 141 | {
|
---|
| 142 | QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
| 143 | fileMenu->addAction(printAction);
|
---|
| 144 | fileMenu->addAction(exitAction);
|
---|
| 145 |
|
---|
| 146 | QMenu *cellMenu = menuBar()->addMenu(tr("&Cell"));
|
---|
| 147 | cellMenu->addAction(cell_addAction);
|
---|
| 148 | cellMenu->addAction(cell_subAction);
|
---|
| 149 | cellMenu->addAction(cell_mulAction);
|
---|
| 150 | cellMenu->addAction(cell_divAction);
|
---|
| 151 | cellMenu->addAction(cell_sumAction);
|
---|
| 152 | cellMenu->addSeparator();
|
---|
| 153 | cellMenu->addAction(colorAction);
|
---|
| 154 | cellMenu->addAction(fontAction);
|
---|
| 155 |
|
---|
| 156 | menuBar()->addSeparator();
|
---|
| 157 |
|
---|
| 158 | QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
|
---|
| 159 | aboutMenu->addAction(aboutSpreadSheet);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | void SpreadSheet::updateStatus(QTableWidgetItem *item)
|
---|
| 163 | {
|
---|
| 164 | if (item && item == table->currentItem()) {
|
---|
| 165 | statusBar()->showMessage(item->data(Qt::StatusTipRole).toString(),
|
---|
| 166 | 1000);
|
---|
| 167 | cellLabel->setText(tr("Cell: (%1)").arg(encode_pos(table->row(item),
|
---|
| 168 | table->column(item))));
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void SpreadSheet::updateColor(QTableWidgetItem *item)
|
---|
| 173 | {
|
---|
| 174 | QPixmap pix(16, 16);
|
---|
| 175 | QColor col;
|
---|
| 176 | if (item)
|
---|
| 177 | col = item->backgroundColor();
|
---|
| 178 | if (!col.isValid())
|
---|
| 179 | col = palette().base().color();
|
---|
| 180 |
|
---|
| 181 | QPainter pt(&pix);
|
---|
| 182 | pt.fillRect(0, 0, 16, 16, col);
|
---|
| 183 |
|
---|
| 184 | QColor lighter = col.light();
|
---|
| 185 | pt.setPen(lighter);
|
---|
| 186 | QPoint lightFrame[] = { QPoint(0, 15), QPoint(0, 0), QPoint(15, 0) };
|
---|
| 187 | pt.drawPolyline(lightFrame, 3);
|
---|
| 188 |
|
---|
| 189 | pt.setPen(col.dark());
|
---|
| 190 | QPoint darkFrame[] = { QPoint(1, 15), QPoint(15, 15), QPoint(15, 1) };
|
---|
| 191 | pt.drawPolyline(darkFrame, 3);
|
---|
| 192 |
|
---|
| 193 | pt.end();
|
---|
| 194 |
|
---|
| 195 | colorAction->setIcon(pix);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | void SpreadSheet::updateLineEdit(QTableWidgetItem *item)
|
---|
| 199 | {
|
---|
| 200 | if (item != table->currentItem())
|
---|
| 201 | return;
|
---|
| 202 | if (item)
|
---|
| 203 | formulaInput->setText(item->data(Qt::EditRole).toString());
|
---|
| 204 | else
|
---|
| 205 | formulaInput->clear();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | void SpreadSheet::returnPressed()
|
---|
| 209 | {
|
---|
| 210 | QString text = formulaInput->text();
|
---|
| 211 | int row = table->currentRow();
|
---|
| 212 | int col = table->currentColumn();
|
---|
| 213 | QTableWidgetItem *item = table->item(row, col);
|
---|
| 214 | if (!item)
|
---|
| 215 | table->setItem(row, col, new SpreadSheetItem(text));
|
---|
| 216 | else
|
---|
| 217 | item->setData(Qt::EditRole, text);
|
---|
| 218 | table->viewport()->update();
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | void SpreadSheet::selectColor()
|
---|
| 222 | {
|
---|
| 223 | QTableWidgetItem *item = table->currentItem();
|
---|
| 224 | QColor col = item ? item->backgroundColor() : table->palette().base().color();
|
---|
| 225 | col = QColorDialog::getColor(col, this);
|
---|
| 226 | if (!col.isValid())
|
---|
| 227 | return;
|
---|
| 228 |
|
---|
| 229 | QList<QTableWidgetItem*> selected = table->selectedItems();
|
---|
| 230 | if (selected.count() == 0)
|
---|
| 231 | return;
|
---|
| 232 |
|
---|
| 233 | foreach(QTableWidgetItem *i, selected)
|
---|
| 234 | if (i)
|
---|
| 235 | i->setBackgroundColor(col);
|
---|
| 236 |
|
---|
| 237 | updateColor(table->currentItem());
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | void SpreadSheet::selectFont()
|
---|
| 241 | {
|
---|
| 242 | QList<QTableWidgetItem*> selected = table->selectedItems();
|
---|
| 243 | if (selected.count() == 0)
|
---|
| 244 | return;
|
---|
| 245 |
|
---|
| 246 | bool ok = false;
|
---|
| 247 | QFont fnt = QFontDialog::getFont(&ok, font(), this);
|
---|
| 248 |
|
---|
| 249 | if (!ok)
|
---|
| 250 | return;
|
---|
| 251 | foreach(QTableWidgetItem *i, selected)
|
---|
| 252 | if (i)
|
---|
| 253 | i->setFont(fnt);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | bool SpreadSheet::runInputDialog(const QString &title,
|
---|
| 257 | const QString &c1Text,
|
---|
| 258 | const QString &c2Text,
|
---|
| 259 | const QString &opText,
|
---|
| 260 | const QString &outText,
|
---|
| 261 | QString *cell1, QString *cell2, QString *outCell)
|
---|
| 262 | {
|
---|
| 263 | QStringList rows, cols;
|
---|
| 264 | for (int c = 0; c < table->columnCount(); ++c)
|
---|
| 265 | cols << QChar('A' + c);
|
---|
| 266 | for (int r = 0; r < table->rowCount(); ++r)
|
---|
| 267 | rows << QString::number(1 + r);
|
---|
| 268 |
|
---|
| 269 | QDialog addDialog(this);
|
---|
| 270 | addDialog.setWindowTitle(title);
|
---|
| 271 |
|
---|
| 272 | QGroupBox group(title, &addDialog);
|
---|
| 273 | group.setMinimumSize(250, 100);
|
---|
| 274 |
|
---|
| 275 | QLabel cell1Label(c1Text, &group);
|
---|
| 276 | QComboBox cell1RowInput(&group);
|
---|
| 277 | int c1Row, c1Col;
|
---|
| 278 | decode_pos(*cell1, &c1Row, &c1Col);
|
---|
| 279 | cell1RowInput.addItems(rows);
|
---|
| 280 | cell1RowInput.setCurrentIndex(c1Row);
|
---|
| 281 |
|
---|
| 282 | QComboBox cell1ColInput(&group);
|
---|
| 283 | cell1ColInput.addItems(cols);
|
---|
| 284 | cell1ColInput.setCurrentIndex(c1Col);
|
---|
| 285 |
|
---|
| 286 | QLabel operatorLabel(opText, &group);
|
---|
| 287 | operatorLabel.setAlignment(Qt::AlignHCenter);
|
---|
| 288 |
|
---|
| 289 | QLabel cell2Label(c2Text, &group);
|
---|
| 290 | QComboBox cell2RowInput(&group);
|
---|
| 291 | int c2Row, c2Col;
|
---|
| 292 | decode_pos(*cell2, &c2Row, &c2Col);
|
---|
| 293 | cell2RowInput.addItems(rows);
|
---|
| 294 | cell2RowInput.setCurrentIndex(c2Row);
|
---|
| 295 | QComboBox cell2ColInput(&group);
|
---|
| 296 | cell2ColInput.addItems(cols);
|
---|
| 297 | cell2ColInput.setCurrentIndex(c2Col);
|
---|
| 298 |
|
---|
| 299 | QLabel equalsLabel("=", &group);
|
---|
| 300 | equalsLabel.setAlignment(Qt::AlignHCenter);
|
---|
| 301 |
|
---|
| 302 | QLabel outLabel(outText, &group);
|
---|
| 303 | QComboBox outRowInput(&group);
|
---|
| 304 | int outRow, outCol;
|
---|
| 305 | decode_pos(*outCell, &outRow, &outCol);
|
---|
| 306 | outRowInput.addItems(rows);
|
---|
| 307 | outRowInput.setCurrentIndex(outRow);
|
---|
| 308 | QComboBox outColInput(&group);
|
---|
| 309 | outColInput.addItems(cols);
|
---|
| 310 | outColInput.setCurrentIndex(outCol);
|
---|
| 311 |
|
---|
| 312 | QPushButton cancelButton(tr("Cancel"), &addDialog);
|
---|
| 313 | connect(&cancelButton, SIGNAL(clicked()), &addDialog, SLOT(reject()));
|
---|
| 314 |
|
---|
| 315 | QPushButton okButton(tr("OK"), &addDialog);
|
---|
| 316 | okButton.setDefault(true);
|
---|
| 317 | connect(&okButton, SIGNAL(clicked()), &addDialog, SLOT(accept()));
|
---|
| 318 |
|
---|
| 319 | QHBoxLayout *buttonsLayout = new QHBoxLayout;
|
---|
| 320 | buttonsLayout->addStretch(1);
|
---|
| 321 | buttonsLayout->addWidget(&okButton);
|
---|
| 322 | buttonsLayout->addSpacing(10);
|
---|
| 323 | buttonsLayout->addWidget(&cancelButton);
|
---|
| 324 |
|
---|
| 325 | QVBoxLayout *dialogLayout = new QVBoxLayout(&addDialog);
|
---|
| 326 | dialogLayout->addWidget(&group);
|
---|
| 327 | dialogLayout->addStretch(1);
|
---|
| 328 | dialogLayout->addItem(buttonsLayout);
|
---|
| 329 |
|
---|
| 330 | QHBoxLayout *cell1Layout = new QHBoxLayout;
|
---|
| 331 | cell1Layout->addWidget(&cell1Label);
|
---|
| 332 | cell1Layout->addSpacing(10);
|
---|
| 333 | cell1Layout->addWidget(&cell1ColInput);
|
---|
| 334 | cell1Layout->addSpacing(10);
|
---|
| 335 | cell1Layout->addWidget(&cell1RowInput);
|
---|
| 336 |
|
---|
| 337 | QHBoxLayout *cell2Layout = new QHBoxLayout;
|
---|
| 338 | cell2Layout->addWidget(&cell2Label);
|
---|
| 339 | cell2Layout->addSpacing(10);
|
---|
| 340 | cell2Layout->addWidget(&cell2ColInput);
|
---|
| 341 | cell2Layout->addSpacing(10);
|
---|
| 342 | cell2Layout->addWidget(&cell2RowInput);
|
---|
| 343 |
|
---|
| 344 | QHBoxLayout *outLayout = new QHBoxLayout;
|
---|
| 345 | outLayout->addWidget(&outLabel);
|
---|
| 346 | outLayout->addSpacing(10);
|
---|
| 347 | outLayout->addWidget(&outColInput);
|
---|
| 348 | outLayout->addSpacing(10);
|
---|
| 349 | outLayout->addWidget(&outRowInput);
|
---|
| 350 |
|
---|
| 351 | QVBoxLayout *vLayout = new QVBoxLayout(&group);
|
---|
| 352 | vLayout->addItem(cell1Layout);
|
---|
| 353 | vLayout->addWidget(&operatorLabel);
|
---|
| 354 | vLayout->addItem(cell2Layout);
|
---|
| 355 | vLayout->addWidget(&equalsLabel);
|
---|
| 356 | vLayout->addStretch(1);
|
---|
| 357 | vLayout->addItem(outLayout);
|
---|
| 358 |
|
---|
| 359 | if (addDialog.exec()) {
|
---|
| 360 | *cell1 = cell1ColInput.currentText() + cell1RowInput.currentText();
|
---|
| 361 | *cell2 = cell2ColInput.currentText() + cell2RowInput.currentText();
|
---|
| 362 | *outCell = outColInput.currentText() + outRowInput.currentText();
|
---|
| 363 | return true;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | return false;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | void SpreadSheet::actionSum()
|
---|
| 370 | {
|
---|
| 371 | int row_first = 0;
|
---|
| 372 | int row_last = 0;
|
---|
| 373 | int row_cur = 0;
|
---|
| 374 |
|
---|
| 375 | int col_first = 0;
|
---|
| 376 | int col_last = 0;
|
---|
| 377 | int col_cur = 0;
|
---|
| 378 |
|
---|
| 379 | QList<QTableWidgetItem*> selected = table->selectedItems();
|
---|
| 380 |
|
---|
| 381 | if (!selected.isEmpty()) {
|
---|
| 382 | QTableWidgetItem *first = selected.first();
|
---|
| 383 | QTableWidgetItem *last = selected.last();
|
---|
| 384 | row_first = table->row(first);
|
---|
| 385 | row_last = table->row(last);
|
---|
| 386 | col_first = table->column(first);
|
---|
| 387 | col_last = table->column(last);
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | QTableWidgetItem *current = table->currentItem();
|
---|
| 391 |
|
---|
| 392 | if (current) {
|
---|
| 393 | row_cur = table->row(current);
|
---|
| 394 | col_cur = table->column(current);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | QString cell1 = encode_pos(row_first, col_first);
|
---|
| 398 | QString cell2 = encode_pos(row_last, col_last);
|
---|
| 399 | QString out = encode_pos(row_cur, col_cur);
|
---|
| 400 |
|
---|
| 401 | if (runInputDialog(tr("Sum cells"), tr("First cell:"), tr("Last cell:"),
|
---|
| 402 | QString("%1").arg(QChar(0x03a3)), tr("Output to:"),
|
---|
| 403 | &cell1, &cell2, &out)) {
|
---|
| 404 | int row, col;
|
---|
| 405 | decode_pos(out, &row, &col);
|
---|
| 406 | table->item(row, col)->setText(tr("sum %1 %2").arg(cell1, cell2));
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | void SpreadSheet::actionMath_helper(const QString &title, const QString &op)
|
---|
| 411 | {
|
---|
| 412 | QString cell1 = "C1";
|
---|
| 413 | QString cell2 = "C2";
|
---|
| 414 | QString out = "C3";
|
---|
| 415 |
|
---|
| 416 | QTableWidgetItem *current = table->currentItem();
|
---|
| 417 | if (current)
|
---|
| 418 | out = encode_pos(table->currentRow(), table->currentColumn());
|
---|
| 419 |
|
---|
| 420 | if (runInputDialog(title, tr("Cell 1"), tr("Cell 2"), op, tr("Output to:"),
|
---|
| 421 | &cell1, &cell2, &out)) {
|
---|
| 422 | int row, col;
|
---|
| 423 | decode_pos(out, &row, &col);
|
---|
[561] | 424 | table->item(row, col)->setText(tr("%1 %2 %3").arg(op, cell1, cell2));
|
---|
[2] | 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | void SpreadSheet::actionAdd()
|
---|
| 429 | {
|
---|
| 430 | actionMath_helper(tr("Addition"), "+");
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | void SpreadSheet::actionSubtract()
|
---|
| 434 | {
|
---|
| 435 | actionMath_helper(tr("Subtraction"), "-");
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | void SpreadSheet::actionMultiply()
|
---|
| 439 | {
|
---|
| 440 | actionMath_helper(tr("Multiplication"), "*");
|
---|
| 441 | }
|
---|
| 442 | void SpreadSheet::actionDivide()
|
---|
| 443 | {
|
---|
| 444 | actionMath_helper(tr("Division"), "/");
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | void SpreadSheet::clear()
|
---|
| 448 | {
|
---|
| 449 | foreach (QTableWidgetItem *i, table->selectedItems())
|
---|
| 450 | i->setText("");
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | void SpreadSheet::setupContextMenu()
|
---|
| 454 | {
|
---|
| 455 | addAction(cell_addAction);
|
---|
| 456 | addAction(cell_subAction);
|
---|
| 457 | addAction(cell_mulAction);
|
---|
| 458 | addAction(cell_divAction);
|
---|
| 459 | addAction(cell_sumAction);
|
---|
| 460 | addAction(firstSeparator);
|
---|
| 461 | addAction(colorAction);
|
---|
| 462 | addAction(fontAction);
|
---|
| 463 | addAction(secondSeparator);
|
---|
| 464 | addAction(clearAction);
|
---|
| 465 | setContextMenuPolicy(Qt::ActionsContextMenu);
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | void SpreadSheet::setupContents()
|
---|
| 469 | {
|
---|
| 470 | QColor titleBackground(Qt::lightGray);
|
---|
| 471 | QFont titleFont = table->font();
|
---|
| 472 | titleFont.setBold(true);
|
---|
| 473 |
|
---|
| 474 | // column 0
|
---|
| 475 | table->setItem(0, 0, new SpreadSheetItem("Item"));
|
---|
| 476 | table->item(0, 0)->setBackgroundColor(titleBackground);
|
---|
| 477 | table->item(0, 0)->setToolTip("This column shows the purchased item/service");
|
---|
| 478 | table->item(0, 0)->setFont(titleFont);
|
---|
| 479 |
|
---|
| 480 | table->setItem(1, 0, new SpreadSheetItem("AirportBus"));
|
---|
| 481 | table->setItem(2, 0, new SpreadSheetItem("Flight (Munich)"));
|
---|
| 482 | table->setItem(3, 0, new SpreadSheetItem("Lunch"));
|
---|
| 483 | table->setItem(4, 0, new SpreadSheetItem("Flight (LA)"));
|
---|
| 484 | table->setItem(5, 0, new SpreadSheetItem("Taxi"));
|
---|
[561] | 485 | table->setItem(6, 0, new SpreadSheetItem("Dinner"));
|
---|
[2] | 486 | table->setItem(7, 0, new SpreadSheetItem("Hotel"));
|
---|
| 487 | table->setItem(8, 0, new SpreadSheetItem("Flight (Oslo)"));
|
---|
| 488 | table->setItem(9, 0, new SpreadSheetItem("Total:"));
|
---|
| 489 |
|
---|
| 490 | table->item(9, 0)->setFont(titleFont);
|
---|
| 491 | table->item(9, 0)->setBackgroundColor(Qt::lightGray);
|
---|
| 492 |
|
---|
| 493 | // column 1
|
---|
| 494 | table->setItem(0, 1, new SpreadSheetItem("Date"));
|
---|
| 495 | table->item(0, 1)->setBackgroundColor(titleBackground);
|
---|
| 496 | table->item(0, 1)->setToolTip("This column shows the purchase date, double click to change");
|
---|
| 497 | table->item(0, 1)->setFont(titleFont);
|
---|
| 498 |
|
---|
| 499 | table->setItem(1, 1, new SpreadSheetItem("15/6/2006"));
|
---|
| 500 | table->setItem(2, 1, new SpreadSheetItem("15/6/2006"));
|
---|
| 501 | table->setItem(3, 1, new SpreadSheetItem("15/6/2006"));
|
---|
| 502 | table->setItem(4, 1, new SpreadSheetItem("21/5/2006"));
|
---|
| 503 | table->setItem(5, 1, new SpreadSheetItem("16/6/2006"));
|
---|
| 504 | table->setItem(6, 1, new SpreadSheetItem("16/6/2006"));
|
---|
| 505 | table->setItem(7, 1, new SpreadSheetItem("16/6/2006"));
|
---|
| 506 | table->setItem(8, 1, new SpreadSheetItem("18/6/2006"));
|
---|
| 507 |
|
---|
| 508 | table->setItem(9, 1, new SpreadSheetItem());
|
---|
| 509 | table->item(9, 1)->setBackgroundColor(Qt::lightGray);
|
---|
| 510 |
|
---|
| 511 | // column 2
|
---|
| 512 | table->setItem(0, 2, new SpreadSheetItem("Price"));
|
---|
| 513 | table->item(0, 2)->setBackgroundColor(titleBackground);
|
---|
[846] | 514 | table->item(0, 2)->setToolTip("This column shows the price of the purchase");
|
---|
[2] | 515 | table->item(0, 2)->setFont(titleFont);
|
---|
| 516 |
|
---|
| 517 | table->setItem(1, 2, new SpreadSheetItem("150"));
|
---|
| 518 | table->setItem(2, 2, new SpreadSheetItem("2350"));
|
---|
| 519 | table->setItem(3, 2, new SpreadSheetItem("-14"));
|
---|
| 520 | table->setItem(4, 2, new SpreadSheetItem("980"));
|
---|
| 521 | table->setItem(5, 2, new SpreadSheetItem("5"));
|
---|
| 522 | table->setItem(6, 2, new SpreadSheetItem("120"));
|
---|
| 523 | table->setItem(7, 2, new SpreadSheetItem("300"));
|
---|
| 524 | table->setItem(8, 2, new SpreadSheetItem("1240"));
|
---|
| 525 |
|
---|
| 526 | table->setItem(9, 2, new SpreadSheetItem());
|
---|
[561] | 527 | table->item(9, 2)->setBackgroundColor(Qt::lightGray);
|
---|
[2] | 528 |
|
---|
| 529 | // column 3
|
---|
| 530 | table->setItem(0, 3, new SpreadSheetItem("Currency"));
|
---|
| 531 | table->item(0, 3)->setBackgroundColor(titleBackground);
|
---|
| 532 | table->item(0, 3)->setToolTip("This column shows the currency");
|
---|
| 533 | table->item(0, 3)->setFont(titleFont);
|
---|
| 534 |
|
---|
| 535 | table->setItem(1, 3, new SpreadSheetItem("NOK"));
|
---|
| 536 | table->setItem(2, 3, new SpreadSheetItem("NOK"));
|
---|
| 537 | table->setItem(3, 3, new SpreadSheetItem("EUR"));
|
---|
| 538 | table->setItem(4, 3, new SpreadSheetItem("EUR"));
|
---|
| 539 | table->setItem(5, 3, new SpreadSheetItem("USD"));
|
---|
| 540 | table->setItem(6, 3, new SpreadSheetItem("USD"));
|
---|
| 541 | table->setItem(7, 3, new SpreadSheetItem("USD"));
|
---|
| 542 | table->setItem(8, 3, new SpreadSheetItem("USD"));
|
---|
| 543 |
|
---|
| 544 | table->setItem(9, 3, new SpreadSheetItem());
|
---|
| 545 | table->item(9,3)->setBackgroundColor(Qt::lightGray);
|
---|
| 546 |
|
---|
| 547 | // column 4
|
---|
| 548 | table->setItem(0, 4, new SpreadSheetItem("Ex. Rate"));
|
---|
| 549 | table->item(0, 4)->setBackgroundColor(titleBackground);
|
---|
| 550 | table->item(0, 4)->setToolTip("This column shows the exchange rate to NOK");
|
---|
| 551 | table->item(0, 4)->setFont(titleFont);
|
---|
| 552 |
|
---|
| 553 | table->setItem(1, 4, new SpreadSheetItem("1"));
|
---|
| 554 | table->setItem(2, 4, new SpreadSheetItem("1"));
|
---|
| 555 | table->setItem(3, 4, new SpreadSheetItem("8"));
|
---|
| 556 | table->setItem(4, 4, new SpreadSheetItem("8"));
|
---|
| 557 | table->setItem(5, 4, new SpreadSheetItem("7"));
|
---|
| 558 | table->setItem(6, 4, new SpreadSheetItem("7"));
|
---|
| 559 | table->setItem(7, 4, new SpreadSheetItem("7"));
|
---|
| 560 | table->setItem(8, 4, new SpreadSheetItem("7"));
|
---|
| 561 |
|
---|
| 562 | table->setItem(9, 4, new SpreadSheetItem());
|
---|
| 563 | table->item(9,4)->setBackgroundColor(Qt::lightGray);
|
---|
| 564 |
|
---|
| 565 | // column 5
|
---|
| 566 | table->setItem(0, 5, new SpreadSheetItem("NOK"));
|
---|
| 567 | table->item(0, 5)->setBackgroundColor(titleBackground);
|
---|
| 568 | table->item(0, 5)->setToolTip("This column shows the expenses in NOK");
|
---|
| 569 | table->item(0, 5)->setFont(titleFont);
|
---|
| 570 |
|
---|
| 571 | table->setItem(1, 5, new SpreadSheetItem("* C2 E2"));
|
---|
| 572 | table->setItem(2, 5, new SpreadSheetItem("* C3 E3"));
|
---|
| 573 | table->setItem(3, 5, new SpreadSheetItem("* C4 E4"));
|
---|
| 574 | table->setItem(4, 5, new SpreadSheetItem("* C5 E5"));
|
---|
| 575 | table->setItem(5, 5, new SpreadSheetItem("* C6 E6"));
|
---|
| 576 | table->setItem(6, 5, new SpreadSheetItem("* C7 E7"));
|
---|
| 577 | table->setItem(7, 5, new SpreadSheetItem("* C8 E8"));
|
---|
| 578 | table->setItem(8, 5, new SpreadSheetItem("* C9 E9"));
|
---|
| 579 |
|
---|
| 580 | table->setItem(9, 5, new SpreadSheetItem("sum F2 F9"));
|
---|
| 581 | table->item(9,5)->setBackgroundColor(Qt::lightGray);
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | const char *htmlText =
|
---|
| 585 | "<HTML>"
|
---|
| 586 | "<p><b>This demo shows use of <c>QTableWidget</c> with custom handling for"
|
---|
| 587 | " individual cells.</b></p>"
|
---|
| 588 | "<p>Using a customized table item we make it possible to have dynamic"
|
---|
| 589 | " output in different cells. The content that is implemented for this"
|
---|
| 590 | " particular demo is:"
|
---|
| 591 | "<ul>"
|
---|
| 592 | "<li>Adding two cells.</li>"
|
---|
| 593 | "<li>Subtracting one cell from another.</li>"
|
---|
| 594 | "<li>Multiplying two cells.</li>"
|
---|
| 595 | "<li>Dividing one cell with another.</li>"
|
---|
| 596 | "<li>Summing the contents of an arbitrary number of cells.</li>"
|
---|
| 597 | "</HTML>";
|
---|
| 598 |
|
---|
| 599 | void SpreadSheet::showAbout()
|
---|
| 600 | {
|
---|
| 601 | QMessageBox::about(this, "About Spreadsheet", htmlText);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | void decode_pos(const QString &pos, int *row, int *col)
|
---|
| 605 | {
|
---|
| 606 | if (pos.isEmpty()) {
|
---|
| 607 | *col = -1;
|
---|
| 608 | *row = -1;
|
---|
| 609 | } else {
|
---|
| 610 | *col = pos.at(0).toLatin1() - 'A';
|
---|
| 611 | *row = pos.right(pos.size() - 1).toInt() - 1;
|
---|
| 612 | }
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | QString encode_pos(int row, int col)
|
---|
| 616 | {
|
---|
| 617 | return QString(col + 'A') + QString::number(row + 1);
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 |
|
---|
| 621 | void SpreadSheet::print()
|
---|
| 622 | {
|
---|
| 623 | #ifndef QT_NO_PRINTER
|
---|
| 624 | QPrinter printer(QPrinter::ScreenResolution);
|
---|
| 625 | QPrintPreviewDialog dlg(&printer);
|
---|
| 626 | PrintView view;
|
---|
| 627 | view.setModel(table->model());
|
---|
[561] | 628 | connect(&dlg, SIGNAL(paintRequested(QPrinter*)),
|
---|
| 629 | &view, SLOT(print(QPrinter*)));
|
---|
[2] | 630 | dlg.exec();
|
---|
| 631 | #endif
|
---|
| 632 | }
|
---|
| 633 |
|
---|