Spreadsheet▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include <QtWidgets>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog)
#include <QPrinter>
#include <QPrintDialog>
#endif
#if QT_CONFIG(printpreviewdialog)
#include <QPrintPreviewDialog>
#endif
#endif
#include
"spreadsheet.h"
#include
"spreadsheetdelegate.h"
#include
"spreadsheetitem.h"
#include
"printview.h"
SpreadSheet::
SpreadSheet(int
rows, int
cols, QWidget *
parent)
:
QMainWindow(parent)
{
addToolBar(toolBar =
new
QToolBar());
formulaInput =
new
QLineEdit();
cellLabel =
new
QLabel(toolBar);
cellLabel-&
gt;setMinimumSize(80
, 0
);
toolBar-&
gt;addWidget(cellLabel);
toolBar-&
gt;addWidget(formulaInput);
table =
new
QTableWidget(rows, cols, this
);
table-&
gt;setSizeAdjustPolicy(QTableWidget::
AdjustToContents);
for
(int
c =
0
; c &
lt; cols; ++
c) {
QString character(QChar('A'
+
c));
table-&
gt;setHorizontalHeaderItem(c, new
QTableWidgetItem(character));
}
table-&
gt;setItemPrototype(table-&
gt;item(rows -
1
, cols -
1
));
table-&
gt;setItemDelegate(new
SpreadSheetDelegate());
createActions();
updateColor(0
);
setupMenuBar();
setupContents();
setupContextMenu();
setCentralWidget(table);
statusBar();
connect(table, &
amp;QTableWidget::
currentItemChanged,
this
, &
amp;SpreadSheet::
updateStatus);
connect(table, &
amp;QTableWidget::
currentItemChanged,
this
, &
amp;SpreadSheet::
updateColor);
connect(table, &
amp;QTableWidget::
currentItemChanged,
this
, &
amp;SpreadSheet::
updateLineEdit);
connect(table, &
amp;QTableWidget::
itemChanged,
this
, &
amp;SpreadSheet::
updateStatus);
connect(formulaInput, &
amp;QLineEdit::
returnPressed, this
, &
amp;SpreadSheet::
returnPressed);
connect(table, &
amp;QTableWidget::
itemChanged,
this
, &
amp;SpreadSheet::
updateLineEdit);
setWindowTitle(tr("Spreadsheet"
));
}
void
SpreadSheet::
createActions()
{
cell_sumAction =
new
QAction(tr("Sum"
), this
);
connect(cell_sumAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
actionSum);
cell_addAction =
new
QAction(tr("&Add"
), this
);
cell_addAction-&
gt;setShortcut(Qt::
CTRL |
Qt::
Key_Plus);
connect(cell_addAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
actionAdd);
cell_subAction =
new
QAction(tr("&Subtract"
), this
);
cell_subAction-&
gt;setShortcut(Qt::
CTRL |
Qt::
Key_Minus);
connect(cell_subAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
actionSubtract);
cell_mulAction =
new
QAction(tr("&Multiply"
), this
);
cell_mulAction-&
gt;setShortcut(Qt::
CTRL |
Qt::
Key_multiply);
connect(cell_mulAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
actionMultiply);
cell_divAction =
new
QAction(tr("&Divide"
), this
);
cell_divAction-&
gt;setShortcut(Qt::
CTRL |
Qt::
Key_division);
connect(cell_divAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
actionDivide);
fontAction =
new
QAction(tr("Font..."
), this
);
fontAction-&
gt;setShortcut(Qt::
CTRL |
Qt::
Key_F);
connect(fontAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
selectFont);
colorAction =
new
QAction(QPixmap(16
, 16
), tr("Background &Color..."
), this
);
connect(colorAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
selectColor);
clearAction =
new
QAction(tr("Clear"
), this
);
clearAction-&
gt;setShortcut(Qt::
Key_Delete);
connect(clearAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
clear);
aboutSpreadSheet =
new
QAction(tr("About Spreadsheet"
), this
);
connect(aboutSpreadSheet, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
showAbout);
exitAction =
new
QAction(tr("E&xit"
), this
);
connect(exitAction, &
amp;QAction::
triggered, qApp, &
amp;QCoreApplication::
quit);
printAction =
new
QAction(tr("&Print"
), this
);
connect(printAction, &
amp;QAction::
triggered, this
, &
amp;SpreadSheet::
print);
firstSeparator =
new
QAction(this
);
firstSeparator-&
gt;setSeparator(true
);
secondSeparator =
new
QAction(this
);
secondSeparator-&
gt;setSeparator(true
);
}
void
SpreadSheet::
setupMenuBar()
{
QMenu *
fileMenu =
menuBar()-&
gt;addMenu(tr("&File"
));
fileMenu-&
gt;addAction(printAction);
fileMenu-&
gt;addAction(exitAction);
QMenu *
cellMenu =
menuBar()-&
gt;addMenu(tr("&Cell"
));
cellMenu-&
gt;addAction(cell_addAction);
cellMenu-&
gt;addAction(cell_subAction);
cellMenu-&
gt;addAction(cell_mulAction);
cellMenu-&
gt;addAction(cell_divAction);
cellMenu-&
gt;addAction(cell_sumAction);
cellMenu-&
gt;addSeparator();
cellMenu-&
gt;addAction(colorAction);
cellMenu-&
gt;addAction(fontAction);
menuBar()-&
gt;addSeparator();
QMenu *
aboutMenu =
menuBar()-&
gt;addMenu(tr("&Help"
));
aboutMenu-&
gt;addAction(aboutSpreadSheet);
}
void
SpreadSheet::
updateStatus(QTableWidgetItem *
item)
{
if
(item &
amp;&
amp; item ==
table-&
gt;currentItem()) {
statusBar()-&
gt;showMessage(item-&
gt;data(Qt::
StatusTipRole).toString(), 1000
);
cellLabel-&
gt;setText(tr("Cell: (%1)"
).arg(encode_pos(table-&
gt;row(item), table-&
gt;column(item))));
}
}
void
SpreadSheet::
updateColor(QTableWidgetItem *
item)
{
QPixmap pix(16
, 16
);
QColor col;
if
(item)
col =
item-&
gt;backgroundColor();
if
(!
col.isValid())
col =
palette().base().color();
QPainter pt(&
amp;pix);
pt.fillRect(0
, 0
, 16
, 16
, col);
QColor lighter =
col.light();
pt.setPen(lighter);
QPoint lightFrame[] =
{
QPoint(0
, 15
), QPoint(0
, 0
), QPoint(15
, 0
) }
;
pt.drawPolyline(lightFrame, 3
);
pt.setPen(col.dark());
QPoint darkFrame[] =
{
QPoint(1
, 15
), QPoint(15
, 15
), QPoint(15
, 1
) }
;
pt.drawPolyline(darkFrame, 3
);
pt.end();
colorAction-&
gt;setIcon(pix);
}
void
SpreadSheet::
updateLineEdit(QTableWidgetItem *
item)
{
if
(item !=
table-&
gt;currentItem())
return
;
if
(item)
formulaInput-&
gt;setText(item-&
gt;data(Qt::
EditRole).toString());
else
formulaInput-&
gt;clear();
}
void
SpreadSheet::
returnPressed()
{
QString text =
formulaInput-&
gt;text();
int
row =
table-&
gt;currentRow();
int
col =
table-&
gt;currentColumn();
QTableWidgetItem *
item =
table-&
gt;item(row, col);
if
(!
item)
table-&
gt;setItem(row, col, new
SpreadSheetItem(text));
else
item-&
gt;setData(Qt::
EditRole, text);
table-&
gt;viewport()-&
gt;update();
}
void
SpreadSheet::
selectColor()
{
QTableWidgetItem *
item =
table-&
gt;currentItem();
QColor col =
item ? item-&
gt;backgroundColor() : table-&
gt;palette().base().color();
col =
QColorDialog::
getColor(col, this
);
if
(!
col.isValid())
return
;
QList&
lt;QTableWidgetItem*&
gt; selected =
table-&
gt;selectedItems();
if
(selected.count() ==
0
)
return
;
foreach (QTableWidgetItem *
i, selected) {
if
(i)
i-&
gt;setBackgroundColor(col);
}
updateColor(table-&
gt;currentItem());
}
void
SpreadSheet::
selectFont()
{
QList&
lt;QTableWidgetItem*&
gt; selected =
table-&
gt;selectedItems();
if
(selected.count() ==
0
)
return
;
bool
ok =
false
;
QFont fnt =
QFontDialog::
getFont(&
amp;ok, font(), this
);
if
(!
ok)
return
;
foreach (QTableWidgetItem *
i, selected) {
if
(i)
i-&
gt;setFont(fnt);
}
}
bool
SpreadSheet::
runInputDialog(const
QString &
amp;title,
const
QString &
amp;c1Text,
const
QString &
amp;c2Text,
const
QString &
amp;opText,
const
QString &
amp;outText,
QString *
cell1, QString *
cell2, QString *
outCell)
{
QStringList rows, cols;
for
(int
c =
0
; c &
lt; table-&
gt;columnCount(); ++
c)
cols &
lt;&
lt; QChar('A'
+
c);
for
(int
r =
0
; r &
lt; table-&
gt;rowCount(); ++
r)
rows &
lt;&
lt; QString::
number(1
+
r);
QDialog addDialog(this
);
addDialog.setWindowTitle(title);
QGroupBox group(title, &
amp;addDialog);
group.setMinimumSize(250
, 100
);
QLabel cell1Label(c1Text, &
amp;group);
QComboBox cell1RowInput(&
amp;group);
int
c1Row, c1Col;
decode_pos(*
cell1, &
amp;c1Row, &
amp;c1Col);
cell1RowInput.addItems(rows);
cell1RowInput.setCurrentIndex(c1Row);
QComboBox cell1ColInput(&
amp;group);
cell1ColInput.addItems(cols);
cell1ColInput.setCurrentIndex(c1Col);
QLabel operatorLabel(opText, &
amp;group);
operatorLabel.setAlignment(Qt::
AlignHCenter);
QLabel cell2Label(c2Text, &
amp;group);
QComboBox cell2RowInput(&
amp;group);
int
c2Row, c2Col;
decode_pos(*
cell2, &
amp;c2Row, &
amp;c2Col);
cell2RowInput.addItems(rows);
cell2RowInput.setCurrentIndex(c2Row);
QComboBox cell2ColInput(&
amp;group);
cell2ColInput.addItems(cols);
cell2ColInput.setCurrentIndex(c2Col);
QLabel equalsLabel("="
, &
amp;group);
equalsLabel.setAlignment(Qt::
AlignHCenter);
QLabel outLabel(outText, &
amp;group);
QComboBox outRowInput(&
amp;group);
int
outRow, outCol;
decode_pos(*
outCell, &
amp;outRow, &
amp;outCol);
outRowInput.addItems(rows);
outRowInput.setCurrentIndex(outRow);
QComboBox outColInput(&
amp;group);
outColInput.addItems(cols);
outColInput.setCurrentIndex(outCol);
QPushButton cancelButton(tr("Cancel"
), &
amp;addDialog);
connect(&
amp;cancelButton, &
amp;QAbstractButton::
clicked, &
amp;addDialog, &
amp;QDialog::
reject);
QPushButton okButton(tr("OK"
), &
amp;addDialog);
okButton.setDefault(true
);
connect(&
amp;okButton, &
amp;QAbstractButton::
clicked, &
amp;addDialog, &
amp;QDialog::
accept);
QHBoxLayout *
buttonsLayout =
new
QHBoxLayout;
buttonsLayout-&
gt;addStretch(1
);
buttonsLayout-&
gt;addWidget(&
amp;okButton);
buttonsLayout-&
gt;addSpacing(10
);
buttonsLayout-&
gt;addWidget(&
amp;cancelButton);
QVBoxLayout *
dialogLayout =
new
QVBoxLayout(&
amp;addDialog);
dialogLayout-&
gt;addWidget(&
amp;group);
dialogLayout-&
gt;addStretch(1
);
dialogLayout-&
gt;addItem(buttonsLayout);
QHBoxLayout *
cell1Layout =
new
QHBoxLayout;
cell1Layout-&
gt;addWidget(&
amp;cell1Label);
cell1Layout-&
gt;addSpacing(10
);
cell1Layout-&
gt;addWidget(&
amp;cell1ColInput);
cell1Layout-&
gt;addSpacing(10
);
cell1Layout-&
gt;addWidget(&
amp;cell1RowInput);
QHBoxLayout *
cell2Layout =
new
QHBoxLayout;
cell2Layout-&
gt;addWidget(&
amp;cell2Label);
cell2Layout-&
gt;addSpacing(10
);
cell2Layout-&
gt;addWidget(&
amp;cell2ColInput);
cell2Layout-&
gt;addSpacing(10
);
cell2Layout-&
gt;addWidget(&
amp;cell2RowInput);
QHBoxLayout *
outLayout =
new
QHBoxLayout;
outLayout-&
gt;addWidget(&
amp;outLabel);
outLayout-&
gt;addSpacing(10
);
outLayout-&
gt;addWidget(&
amp;outColInput);
outLayout-&
gt;addSpacing(10
);
outLayout-&
gt;addWidget(&
amp;outRowInput);
QVBoxLayout *
vLayout =
new
QVBoxLayout(&
amp;group);
vLayout-&
gt;addItem(cell1Layout);
vLayout-&
gt;addWidget(&
amp;operatorLabel);
vLayout-&
gt;addItem(cell2Layout);
vLayout-&
gt;addWidget(&
amp;equalsLabel);
vLayout-&
gt;addStretch(1
);
vLayout-&
gt;addItem(outLayout);
if
(addDialog.exec()) {
*
cell1 =
cell1ColInput.currentText() +
cell1RowInput.currentText();
*
cell2 =
cell2ColInput.currentText() +
cell2RowInput.currentText();
*
outCell =
outColInput.currentText() +
outRowInput.currentText();
return
true
;
}
return
false
;
}
void
SpreadSheet::
actionSum()
{
int
row_first =
0
;
int
row_last =
0
;
int
row_cur =
0
;
int
col_first =
0
;
int
col_last =
0
;
int
col_cur =
0
;
QList&
lt;QTableWidgetItem*&
gt; selected =
table-&
gt;selectedItems();
if
(!
selected.isEmpty()) {
QTableWidgetItem *
first =
selected.first();
QTableWidgetItem *
last =
selected.last();
row_first =
table-&
gt;row(first);
row_last =
table-&
gt;row(last);
col_first =
table-&
gt;column(first);
col_last =
table-&
gt;column(last);
}
QTableWidgetItem *
current =
table-&
gt;currentItem();
if
(current) {
row_cur =
table-&
gt;row(current);
col_cur =
table-&
gt;column(current);
}
QString cell1 =
encode_pos(row_first, col_first);
QString cell2 =
encode_pos(row_last, col_last);
QString out =
encode_pos(row_cur, col_cur);
if
(runInputDialog(tr("Sum cells"
), tr("First cell:"
), tr("Last cell:"
),
QString("%1"
).arg(QChar(0x03a3
)), tr("Output to:"
),
&
amp;cell1, &
amp;cell2, &
amp;out)) {
int
row;
int
col;
decode_pos(out, &
amp;row, &
amp;col);
table-&
gt;item(row, col)-&
gt;setText(tr("sum %1 %2"
).arg(cell1, cell2));
}
}
void
SpreadSheet::
actionMath_helper(const
QString &
amp;title, const
QString &
amp;op)
{
QString cell1 =
"C1"
;
QString cell2 =
"C2"
;
QString out =
"C3"
;
QTableWidgetItem *
current =
table-&
gt;currentItem();
if
(current)
out =
encode_pos(table-&
gt;currentRow(), table-&
gt;currentColumn());
if
(runInputDialog(title, tr("Cell 1"
), tr("Cell 2"
), op, tr("Output to:"
),
&
amp;cell1, &
amp;cell2, &
amp;out)) {
int
row, col;
decode_pos(out, &
amp;row, &
amp;col);
table-&
gt;item(row, col)-&
gt;setText(tr("%1 %2 %3"
).arg(op, cell1, cell2));
}
}
void
SpreadSheet::
actionAdd()
{
actionMath_helper(tr("Addition"
), "+"
);
}
void
SpreadSheet::
actionSubtract()
{
actionMath_helper(tr("Subtraction"
), "-"
);
}
void
SpreadSheet::
actionMultiply()
{
actionMath_helper(tr("Multiplication"
), "*"
);
}
void
SpreadSheet::
actionDivide()
{
actionMath_helper(tr("Division"
), "/"
);
}
void
SpreadSheet::
clear()
{
foreach (QTableWidgetItem *
i, table-&
gt;selectedItems())
i-&
gt;setText(""
);
}
void
SpreadSheet::
setupContextMenu()
{
addAction(cell_addAction);
addAction(cell_subAction);
addAction(cell_mulAction);
addAction(cell_divAction);
addAction(cell_sumAction);
addAction(firstSeparator);
addAction(colorAction);
addAction(fontAction);
addAction(secondSeparator);
addAction(clearAction);
setContextMenuPolicy(Qt::
ActionsContextMenu);
}
void
SpreadSheet::
setupContents()
{
QColor titleBackground(Qt::
lightGray);
QFont titleFont =
table-&
gt;font();
titleFont.setBold(true
);
// column 0
table-&
gt;setItem(0
, 0
, new
SpreadSheetItem("Item"
));
table-&
gt;item(0
, 0
)-&
gt;setBackgroundColor(titleBackground);
table-&
gt;item(0
, 0
)-&
gt;setToolTip("This column shows the purchased item/service"
);
table-&
gt;item(0
, 0
)-&
gt;setFont(titleFont);
table-&
gt;setItem(1
, 0
, new
SpreadSheetItem("AirportBus"
));
table-&
gt;setItem(2
, 0
, new
SpreadSheetItem("Flight (Munich)"
));
table-&
gt;setItem(3
, 0
, new
SpreadSheetItem("Lunch"
));
table-&
gt;setItem(4
, 0
, new
SpreadSheetItem("Flight (LA)"
));
table-&
gt;setItem(5
, 0
, new
SpreadSheetItem("Taxi"
));
table-&
gt;setItem(6
, 0
, new
SpreadSheetItem("Dinner"
));
table-&
gt;setItem(7
, 0
, new
SpreadSheetItem("Hotel"
));
table-&
gt;setItem(8
, 0
, new
SpreadSheetItem("Flight (Oslo)"
));
table-&
gt;setItem(9
, 0
, new
SpreadSheetItem("Total:"
));
table-&
gt;item(9
, 0
)-&
gt;setFont(titleFont);
table-&
gt;item(9
, 0
)-&
gt;setBackgroundColor(Qt::
lightGray);
// column 1
table-&
gt;setItem(0
, 1
, new
SpreadSheetItem("Date"
));
table-&
gt;item(0
, 1
)-&
gt;setBackgroundColor(titleBackground);
table-&
gt;item(0
, 1
)-&
gt;setToolTip("This column shows the purchase date, double click to change"
);
table-&
gt;item(0
, 1
)-&
gt;setFont(titleFont);
table-&
gt;setItem(1
, 1
, new
SpreadSheetItem("15/6/2006"
));
table-&
gt;setItem(2
, 1
, new
SpreadSheetItem("15/6/2006"
));
table-&
gt;setItem(3
, 1
, new
SpreadSheetItem("15/6/2006"
));
table-&
gt;setItem(4
, 1
, new
SpreadSheetItem("21/5/2006"
));
table-&
gt;setItem(5
, 1
, new
SpreadSheetItem("16/6/2006"
));
table-&
gt;setItem(6
, 1
, new
SpreadSheetItem("16/6/2006"
));
table-&
gt;setItem(7
, 1
, new
SpreadSheetItem("16/6/2006"
));
table-&
gt;setItem(8
, 1
, new
SpreadSheetItem("18/6/2006"
));
table-&
gt;setItem(9
, 1
, new
SpreadSheetItem());
table-&
gt;item(9
, 1
)-&
gt;setBackgroundColor(Qt::
lightGray);
// column 2
table-&
gt;setItem(0
, 2
, new
SpreadSheetItem("Price"
));
table-&
gt;item(0
, 2
)-&
gt;setBackgroundColor(titleBackground);
table-&
gt;item(0
, 2
)-&
gt;setToolTip("This column shows the price of the purchase"
);
table-&
gt;item(0
, 2
)-&
gt;setFont(titleFont);
table-&
gt;setItem(1
, 2
, new
SpreadSheetItem("150"
));
table-&
gt;setItem(2
, 2
, new
SpreadSheetItem("2350"
));
table-&
gt;setItem(3
, 2
, new
SpreadSheetItem("-14"
));
table-&
gt;setItem(4
, 2
, new
SpreadSheetItem("980"
));
table-&
gt;setItem(5
, 2
, new
SpreadSheetItem("5"
));
table-&
gt;setItem(6
, 2
, new
SpreadSheetItem("120"
));
table-&
gt;setItem(7
, 2
, new
SpreadSheetItem("300"
));
table-&
gt;setItem(8
, 2
, new
SpreadSheetItem("1240"
));
table-&
gt;setItem(9
, 2
, new
SpreadSheetItem());
table-&
gt;item(9
, 2
)-&
gt;setBackgroundColor(Qt::
lightGray);
// column 3
table-&
gt;setItem(0
, 3
, new
SpreadSheetItem("Currency"
));
table-&
gt;item(0
, 3
)-&
gt;setBackgroundColor(titleBackground);
table-&
gt;item(0
, 3
)-&
gt;setToolTip("This column shows the currency"
);
table-&
gt;item(0
, 3
)-&
gt;setFont(titleFont);
table-&
gt;setItem(1
, 3
, new
SpreadSheetItem("NOK"
));
table-&
gt;setItem(2
, 3
, new
SpreadSheetItem("NOK"
));
table-&
gt;setItem(3
, 3
, new
SpreadSheetItem("EUR"
));
table-&
gt;setItem(4
, 3
, new
SpreadSheetItem("EUR"
));
table-&
gt;setItem(5
, 3
, new
SpreadSheetItem("USD"
));
table-&
gt;setItem(6
, 3
, new
SpreadSheetItem("USD"
));
table-&
gt;setItem(7
, 3
, new
SpreadSheetItem("USD"
));
table-&
gt;setItem(8
, 3
, new
SpreadSheetItem("USD"
));
table-&
gt;setItem(9
, 3
, new
SpreadSheetItem());
table-&
gt;item(9
,3
)-&
gt;setBackgroundColor(Qt::
lightGray);
// column 4
table-&
gt;setItem(0
, 4
, new
SpreadSheetItem("Ex. Rate"
));
table-&
gt;item(0
, 4
)-&
gt;setBackgroundColor(titleBackground);
table-&
gt;item(0
, 4
)-&
gt;setToolTip("This column shows the exchange rate to NOK"
);
table-&
gt;item(0
, 4
)-&
gt;setFont(titleFont);
table-&
gt;setItem(1
, 4
, new
SpreadSheetItem("1"
));
table-&
gt;setItem(2
, 4
, new
SpreadSheetItem("1"
));
table-&
gt;setItem(3
, 4
, new
SpreadSheetItem("8"
));
table-&
gt;setItem(4
, 4
, new
SpreadSheetItem("8"
));
table-&
gt;setItem(5
, 4
, new
SpreadSheetItem("7"
));
table-&
gt;setItem(6
, 4
, new
SpreadSheetItem("7"
));
table-&
gt;setItem(7
, 4
, new
SpreadSheetItem("7"
));
table-&
gt;setItem(8
, 4
, new
SpreadSheetItem("7"
));
table-&
gt;setItem(9
, 4
, new
SpreadSheetItem());
table-&
gt;item(9
,4
)-&
gt;setBackgroundColor(Qt::
lightGray);
// column 5
table-&
gt;setItem(0
, 5
, new
SpreadSheetItem("NOK"
));
table-&
gt;item(0
, 5
)-&
gt;setBackgroundColor(titleBackground);
table-&
gt;item(0
, 5
)-&
gt;setToolTip("This column shows the expenses in NOK"
);
table-&
gt;item(0
, 5
)-&
gt;setFont(titleFont);
table-&
gt;setItem(1
, 5
, new
SpreadSheetItem("* C2 E2"
));
table-&
gt;setItem(2
, 5
, new
SpreadSheetItem("* C3 E3"
));
table-&
gt;setItem(3
, 5
, new
SpreadSheetItem("* C4 E4"
));
table-&
gt;setItem(4
, 5
, new
SpreadSheetItem("* C5 E5"
));
table-&
gt;setItem(5
, 5
, new
SpreadSheetItem("* C6 E6"
));
table-&
gt;setItem(6
, 5
, new
SpreadSheetItem("* C7 E7"
));
table-&
gt;setItem(7
, 5
, new
SpreadSheetItem("* C8 E8"
));
table-&
gt;setItem(8
, 5
, new
SpreadSheetItem("* C9 E9"
));
table-&
gt;setItem(9
, 5
, new
SpreadSheetItem("sum F2 F9"
));
table-&
gt;item(9
,5
)-&
gt;setBackgroundColor(Qt::
lightGray);
}
const
char
*
htmlText =
"<HTML>"
"<p><b>This demo shows use of <c>QTableWidget</c> with custom handling for"
" individual cells.</b></p>"
"<p>Using a customized table item we make it possible to have dynamic"
" output in different cells. The content that is implemented for this"
" particular demo is:"
"<ul>"
"<li>Adding two cells.</li>"
"<li>Subtracting one cell from another.</li>"
"<li>Multiplying two cells.</li>"
"<li>Dividing one cell with another.</li>"
"<li>Summing the contents of an arbitrary number of cells.</li>"
"</HTML>"
;
void
SpreadSheet::
showAbout()
{
QMessageBox::
about(this
, "About Spreadsheet"
, htmlText);
}
void
decode_pos(const
QString &
amp;pos, int
*
row, int
*
col)
{
if
(pos.isEmpty()) {
*
col =
-
1
;
*
row =
-
1
;
}
else
{
*
col =
pos.at(0
).toLatin1() -
'A'
;
*
row =
pos.right(pos.size() -
1
).toInt() -
1
;
}
}
QString encode_pos(int
row, int
col)
{
return
QString(col +
'A'
) +
QString::
number(row +
1
);
}
void
SpreadSheet::
print()
{
#if QT_CONFIG(printpreviewdialog)
QPrinter printer(QPrinter::
ScreenResolution);
QPrintPreviewDialog dlg(&
amp;printer);
PrintView view;
view.setModel(table-&
gt;model());
connect(&
amp;dlg, &
amp;QPrintPreviewDialog::
paintRequested, &
amp;view, &
amp;PrintView::
print);
dlg.exec();
#endif
}