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