source: trunk/demos/spreadsheet/spreadsheet.cpp@ 569

Last change on this file since 569 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 21.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the demonstration applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia 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
48SpreadSheet::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
91void 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
139void 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
161void 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
171void 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
197void 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
207void 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
220void 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
239void 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)