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