1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
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 [email protected].
|
---|
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 | }
|
---|
|
---|