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
61SpreadSheet::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);
69
70 cellLabel->setMinimumSize(80, 0);
71
72 toolBar->addWidget(cellLabel);
73 toolBar->addWidget(formulaInput);
74
75 table->setSizeAdjustPolicy(QTableWidget::AdjustToContents);
76 for (int c = 0; c < cols; ++c) {
77 QString character(QChar('A' + c));
78 table->setHorizontalHeaderItem(c, new QTableWidgetItem(character));
79 }
80
81 table->setItemPrototype(table->item(rows - 1, cols - 1));
82 table->setItemDelegate(new SpreadSheetDelegate());
83
84 createActions();
85 updateColor(nullptr);
86 setupMenuBar();
87 setupContents();
88 setupContextMenu();
89 setCentralWidget(table);
90
91 statusBar();
92 connect(table, &QTableWidget::currentItemChanged,
93 this, &SpreadSheet::updateStatus);
94 connect(table, &QTableWidget::currentItemChanged,
95 this, &SpreadSheet::updateColor);
96 connect(table, &QTableWidget::currentItemChanged,
97 this, &SpreadSheet::updateLineEdit);
98 connect(table, &QTableWidget::itemChanged,
99 this, &SpreadSheet::updateStatus);
100 connect(formulaInput, &QLineEdit::returnPressed,
101 this, &SpreadSheet::returnPressed);
102 connect(table, &QTableWidget::itemChanged,
103 this, &SpreadSheet::updateLineEdit);
104
105 setWindowTitle(tr("Spreadsheet"));
106}
107
108void SpreadSheet::createActions()
109{
110 cell_sumAction = new QAction(tr("Sum"), this);
111 connect(cell_sumAction, &QAction::triggered, this, &SpreadSheet::actionSum);
112
113 cell_addAction = new QAction(tr("&Add"), this);
114 cell_addAction->setShortcut(Qt::CTRL | Qt::Key_Plus);
115 connect(cell_addAction, &QAction::triggered, this, &SpreadSheet::actionAdd);
116
117 cell_subAction = new QAction(tr("&Subtract"), this);
118 cell_subAction->setShortcut(Qt::CTRL | Qt::Key_Minus);
119 connect(cell_subAction, &QAction::triggered, this, &SpreadSheet::actionSubtract);
120
121 cell_mulAction = new QAction(tr("&Multiply"), this);
122 cell_mulAction->setShortcut(Qt::CTRL | Qt::Key_multiply);
123 connect(cell_mulAction, &QAction::triggered, this, &SpreadSheet::actionMultiply);
124
125 cell_divAction = new QAction(tr("&Divide"), this);
126 cell_divAction->setShortcut(Qt::CTRL | Qt::Key_division);
127 connect(cell_divAction, &QAction::triggered, this, &SpreadSheet::actionDivide);
128
129 fontAction = new QAction(tr("Font..."), this);
130 fontAction->setShortcut(Qt::CTRL | Qt::Key_F);
131 connect(fontAction, &QAction::triggered, this, &SpreadSheet::selectFont);
132
133 colorAction = new QAction(QPixmap(16, 16), tr("Background &Color..."), this);
134 connect(colorAction, &QAction::triggered, this, &SpreadSheet::selectColor);
135
136 clearAction = new QAction(tr("Clear"), this);
137 clearAction->setShortcut(Qt::Key_Delete);
138 connect(clearAction, &QAction::triggered, this, &SpreadSheet::clear);
139
140 aboutSpreadSheet = new QAction(tr("About Spreadsheet"), this);
141 connect(aboutSpreadSheet, &QAction::triggered, this, &SpreadSheet::showAbout);
142
143 exitAction = new QAction(tr("E&xit"), this);
144 connect(exitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
145
146 printAction = new QAction(tr("&Print"), this);
147 connect(printAction, &QAction::triggered, this, &SpreadSheet::print);
148
149 firstSeparator = new QAction(this);
150 firstSeparator->setSeparator(true);
151
152 secondSeparator = new QAction(this);
153 secondSeparator->setSeparator(true);
154}
155
156void SpreadSheet::setupMenuBar()
157{
158 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
159 fileMenu->addAction(printAction);
160 fileMenu->addAction(exitAction);
161
162 QMenu *cellMenu = menuBar()->addMenu(tr("&Cell"));
163 cellMenu->addAction(cell_addAction);
164 cellMenu->addAction(cell_subAction);
165 cellMenu->addAction(cell_mulAction);
166 cellMenu->addAction(cell_divAction);
167 cellMenu->addAction(cell_sumAction);
168 cellMenu->addSeparator();
169 cellMenu->addAction(colorAction);
170 cellMenu->addAction(fontAction);
171
172 menuBar()->addSeparator();
173
174 QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
175 aboutMenu->addAction(aboutSpreadSheet);
176}
177
178void SpreadSheet::updateStatus(QTableWidgetItem *item)
179{
180 if (item && item == table->currentItem()) {
181 statusBar()->showMessage(item->data(Qt::StatusTipRole).toString(), 1000);
182 cellLabel->setText(tr("Cell: (%1)").arg(encode_pos(table->row(item), table->column(item))));
183 }
184}
185
186void 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(0, 0, 16, 16, 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(lightFrame, 3);
202
203 pt.setPen(col.darker());
204 QPoint darkFrame[] = { QPoint(1, 15), QPoint(15, 15), QPoint(15, 1) };
205 pt.drawPolyline(darkFrame, 3);
206
207 pt.end();
208
209 colorAction->setIcon(pix);
210}
211
212void SpreadSheet::updateLineEdit(QTableWidgetItem *item)
213{
214 if (item != table->currentItem())
215 return;
216 if (item)
217 formulaInput->setText(item->data(Qt::EditRole).toString());
218 else
219 formulaInput->clear();
220}
221
222void SpreadSheet::returnPressed()
223{
224 QString text = formulaInput->text();
225 int row = table->currentRow();
226 int col = table->currentColumn();
227 QTableWidgetItem *item = table->item(row, col);
228 if (!item)
229 table->setItem(row, col, new SpreadSheetItem(text));
230 else
231 item->setData(Qt::EditRole, text);
232 table->viewport()->update();
233}
234
235void SpreadSheet::selectColor()
236{
237 QTableWidgetItem *item = table->currentItem();
238 QColor col = item ? item->background().color() : table->palette().base().color();
239 col = QColorDialog::getColor(col, 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(table->currentItem());
253}
254
255void 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, font(), this);
263
264 if (!ok)
265 return;
266 for (QTableWidgetItem *i : selected) {
267 if (i)
268 i->setFont(fnt);
269 }
270}
271
272bool 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(250, 100);
290
291 QLabel cell1Label(c1Text, &group);
292 QComboBox cell1RowInput(&group);
293 int c1Row, c1Col;
294 decode_pos(*cell1, &c1Row, &c1Col);
295 cell1RowInput.addItems(rows);
296 cell1RowInput.setCurrentIndex(c1Row);
297
298 QComboBox cell1ColInput(&group);
299 cell1ColInput.addItems(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(*cell2, &c2Row, &c2Col);
309 cell2RowInput.addItems(rows);
310 cell2RowInput.setCurrentIndex(c2Row);
311 QComboBox cell2ColInput(&group);
312 cell2ColInput.addItems(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(*outCell, &outRow, &outCol);
322 outRowInput.addItems(rows);
323 outRowInput.setCurrentIndex(outRow);
324 QComboBox outColInput(&group);
325 outColInput.addItems(cols);
326 outColInput.setCurrentIndex(outCol);
327
328 QPushButton cancelButton(tr("Cancel"), &addDialog);
329 connect(&cancelButton, &QAbstractButton::clicked, &addDialog, &QDialog::reject);
330
331 QPushButton okButton(tr("OK"), &addDialog);
332 okButton.setDefault(true);
333 connect(&okButton, &QAbstractButton::clicked, &addDialog, &QDialog::accept);
334
335 QHBoxLayout *buttonsLayout = new QHBoxLayout;
336 buttonsLayout->addStretch(1);
337 buttonsLayout->addWidget(&okButton);
338 buttonsLayout->addSpacing(10);
339 buttonsLayout->addWidget(&cancelButton);
340
341 QVBoxLayout *dialogLayout = new QVBoxLayout(&addDialog);
342 dialogLayout->addWidget(&group);
343 dialogLayout->addStretch(1);
344 dialogLayout->addItem(buttonsLayout);
345
346 QHBoxLayout *cell1Layout = new QHBoxLayout;
347 cell1Layout->addWidget(&cell1Label);
348 cell1Layout->addSpacing(10);
349 cell1Layout->addWidget(&cell1ColInput);
350 cell1Layout->addSpacing(10);
351 cell1Layout->addWidget(&cell1RowInput);
352
353 QHBoxLayout *cell2Layout = new QHBoxLayout;
354 cell2Layout->addWidget(&cell2Label);
355 cell2Layout->addSpacing(10);
356 cell2Layout->addWidget(&cell2ColInput);
357 cell2Layout->addSpacing(10);
358 cell2Layout->addWidget(&cell2RowInput);
359
360 QHBoxLayout *outLayout = new QHBoxLayout;
361 outLayout->addWidget(&outLabel);
362 outLayout->addSpacing(10);
363 outLayout->addWidget(&outColInput);
364 outLayout->addSpacing(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(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
385void 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(first);
401 row_last = table->row(last);
402 col_first = table->column(first);
403 col_last = table->column(last);
404 }
405
406 const QTableWidgetItem *current = table->currentItem();
407
408 if (current) {
409 row_cur = table->row(current);
410 col_cur = table->column(current);
411 }
412
413 QString cell1 = encode_pos(row_first, col_first);
414 QString cell2 = encode_pos(row_last, col_last);
415 QString out = encode_pos(row_cur, col_cur);
416
417 if (runInputDialog(tr("Sum cells"), tr("First cell:"), tr("Last cell:"),
418 QString("%1").arg(QChar(0x03a3)), tr("Output to:"),
419 &cell1, &cell2, &out)) {
420 int row;
421 int col;
422 decode_pos(out, &row, &col);
423 table->item(row, col)->setText(tr("sum %1 %2").arg(cell1, cell2));
424 }
425}
426
427void 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(table->currentRow(), table->currentColumn());
436
437 if (runInputDialog(title, tr("Cell 1"), tr("Cell 2"), op, tr("Output to:"),
438 &cell1, &cell2, &out)) {
439 int row, col;
440 decode_pos(out, &row, &col);
441 table->item(row, col)->setText(tr("%1 %2 %3").arg(op, cell1, cell2));
442 }
443}
444
445void SpreadSheet::actionAdd()
446{
447 actionMath_helper(tr("Addition"), "+");
448}
449
450void SpreadSheet::actionSubtract()
451{
452 actionMath_helper(tr("Subtraction"), "-");
453}
454
455void SpreadSheet::actionMultiply()
456{
457 actionMath_helper(tr("Multiplication"), "*");
458}
459void SpreadSheet::actionDivide()
460{
461 actionMath_helper(tr("Division"), "/");
462}
463
464void SpreadSheet::clear()
465{
466 const QList<QTableWidgetItem *> selectedItems = table->selectedItems();
467 for (QTableWidgetItem *i : selectedItems)
468 i->setText(QString());
469}
470
471void SpreadSheet::setupContextMenu()
472{
473 addAction(cell_addAction);
474 addAction(cell_subAction);
475 addAction(cell_mulAction);
476 addAction(cell_divAction);
477 addAction(cell_sumAction);
478 addAction(firstSeparator);
479 addAction(colorAction);
480 addAction(fontAction);
481 addAction(secondSeparator);
482 addAction(clearAction);
483 setContextMenuPolicy(Qt::ActionsContextMenu);
484}
485
486void SpreadSheet::setupContents()
487{
488 QBrush titleBackground(Qt::lightGray);
489 QFont titleFont = table->font();
490 titleFont.setBold(true);
491
492 // column 0
493 table->setItem(0, 0, new SpreadSheetItem("Item"));
494 table->item(0, 0)->setBackground(titleBackground);
495 table->item(0, 0)->setToolTip("This column shows the purchased item/service");
496 table->item(0, 0)->setFont(titleFont);
497
498 table->setItem(1, 0, new SpreadSheetItem("AirportBus"));
499 table->setItem(2, 0, new SpreadSheetItem("Flight (Munich)"));
500 table->setItem(3, 0, new SpreadSheetItem("Lunch"));
501 table->setItem(4, 0, new SpreadSheetItem("Flight (LA)"));
502 table->setItem(5, 0, new SpreadSheetItem("Taxi"));
503 table->setItem(6, 0, new SpreadSheetItem("Dinner"));
504 table->setItem(7, 0, new SpreadSheetItem("Hotel"));
505 table->setItem(8, 0, new SpreadSheetItem("Flight (Oslo)"));
506 table->setItem(9, 0, new SpreadSheetItem("Total:"));
507
508 table->item(9, 0)->setFont(titleFont);
509 table->item(9, 0)->setBackground(titleBackground);
510
511 // column 1
512 table->setItem(0, 1, new SpreadSheetItem("Date"));
513 table->item(0, 1)->setBackground(titleBackground);
514 table->item(0, 1)->setToolTip("This column shows the purchase date, double click to change");
515 table->item(0, 1)->setFont(titleFont);
516
517 table->setItem(1, 1, new SpreadSheetItem("15/6/2006"));
518 table->setItem(2, 1, new SpreadSheetItem("15/6/2006"));
519 table->setItem(3, 1, new SpreadSheetItem("15/6/2006"));
520 table->setItem(4, 1, new SpreadSheetItem("21/5/2006"));
521 table->setItem(5, 1, new SpreadSheetItem("16/6/2006"));
522 table->setItem(6, 1, new SpreadSheetItem("16/6/2006"));
523 table->setItem(7, 1, new SpreadSheetItem("16/6/2006"));
524 table->setItem(8, 1, new SpreadSheetItem("18/6/2006"));
525
526 table->setItem(9, 1, new SpreadSheetItem());
527 table->item(9, 1)->setBackground(titleBackground);
528
529 // column 2
530 table->setItem(0, 2, new SpreadSheetItem("Price"));
531 table->item(0, 2)->setBackground(titleBackground);
532 table->item(0, 2)->setToolTip("This column shows the price of the purchase");
533 table->item(0, 2)->setFont(titleFont);
534
535 table->setItem(1, 2, new SpreadSheetItem("150"));
536 table->setItem(2, 2, new SpreadSheetItem("2350"));
537 table->setItem(3, 2, new SpreadSheetItem("-14"));
538 table->setItem(4, 2, new SpreadSheetItem("980"));
539 table->setItem(5, 2, new SpreadSheetItem("5"));
540 table->setItem(6, 2, new SpreadSheetItem("120"));
541 table->setItem(7, 2, new SpreadSheetItem("300"));
542 table->setItem(8, 2, new SpreadSheetItem("1240"));
543
544 table->setItem(9, 2, new SpreadSheetItem());
545 table->item(9, 2)->setBackground(Qt::lightGray);
546
547 // column 3
548 table->setItem(0, 3, new SpreadSheetItem("Currency"));
549 table->item(0, 3)->setBackground(titleBackground);
550 table->item(0, 3)->setToolTip("This column shows the currency");
551 table->item(0, 3)->setFont(titleFont);
552
553 table->setItem(1, 3, new SpreadSheetItem("NOK"));
554 table->setItem(2, 3, new SpreadSheetItem("NOK"));
555 table->setItem(3, 3, new SpreadSheetItem("EUR"));
556 table->setItem(4, 3, new SpreadSheetItem("EUR"));
557 table->setItem(5, 3, new SpreadSheetItem("USD"));
558 table->setItem(6, 3, new SpreadSheetItem("USD"));
559 table->setItem(7, 3, new SpreadSheetItem("USD"));
560 table->setItem(8, 3, new SpreadSheetItem("USD"));
561
562 table->setItem(9, 3, new SpreadSheetItem());
563 table->item(9, 3)->setBackground(Qt::lightGray);
564
565 // column 4
566 table->setItem(0, 4, new SpreadSheetItem("Ex. Rate"));
567 table->item(0, 4)->setBackground(titleBackground);
568 table->item(0, 4)->setToolTip("This column shows the exchange rate to NOK");
569 table->item(0, 4)->setFont(titleFont);
570
571 table->setItem(1, 4, new SpreadSheetItem("1"));
572 table->setItem(2, 4, new SpreadSheetItem("1"));
573 table->setItem(3, 4, new SpreadSheetItem("8"));
574 table->setItem(4, 4, new SpreadSheetItem("8"));
575 table->setItem(5, 4, new SpreadSheetItem("7"));
576 table->setItem(6, 4, new SpreadSheetItem("7"));
577 table->setItem(7, 4, new SpreadSheetItem("7"));
578 table->setItem(8, 4, new SpreadSheetItem("7"));
579
580 table->setItem(9, 4, new SpreadSheetItem());
581 table->item(9, 4)->setBackground(titleBackground);
582
583 // column 5
584 table->setItem(0, 5, new SpreadSheetItem("NOK"));
585 table->item(0, 5)->setBackground(titleBackground);
586 table->item(0, 5)->setToolTip("This column shows the expenses in NOK");
587 table->item(0, 5)->setFont(titleFont);
588
589 table->setItem(1, 5, new SpreadSheetItem("* C2 E2"));
590 table->setItem(2, 5, new SpreadSheetItem("* C3 E3"));
591 table->setItem(3, 5, new SpreadSheetItem("* C4 E4"));
592 table->setItem(4, 5, new SpreadSheetItem("* C5 E5"));
593 table->setItem(5, 5, new SpreadSheetItem("* C6 E6"));
594 table->setItem(6, 5, new SpreadSheetItem("* C7 E7"));
595 table->setItem(7, 5, new SpreadSheetItem("* C8 E8"));
596 table->setItem(8, 5, new SpreadSheetItem("* C9 E9"));
597
598 table->setItem(9, 5, new SpreadSheetItem("sum F2 F9"));
599 table->item(9, 5)->setBackground(titleBackground);
600}
601
602const 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
617void SpreadSheet::showAbout()
618{
619 QMessageBox::about(this, "About Spreadsheet", htmlText);
620}
621
622void 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(0).toLatin1() - 'A';
629 *row = pos.right(pos.size() - 1).toInt() - 1;
630 }
631}
632
633QString encode_pos(int row, int col)
634{
635 return QString(char16_t(col + 'A')) + QString::number(row + 1);
636}
637
638
639void 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(&dlg, &QPrintPreviewDialog::paintRequested, &view, &PrintView::print);
647 dlg.exec();
648#endif
649}
650
651