author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 20:15:53 +0300 | |
branch | RCL_3 |
changeset 13 | c0432d11811c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "mainwindow.h" |
|
45 |
#include "scribblearea.h" |
|
46 |
||
47 |
//! [0] |
|
48 |
MainWindow::MainWindow() |
|
49 |
{ |
|
50 |
scribbleArea = new ScribbleArea; |
|
51 |
setCentralWidget(scribbleArea); |
|
52 |
||
53 |
createActions(); |
|
54 |
createMenus(); |
|
55 |
||
56 |
setWindowTitle(tr("Finger Paint")); |
|
57 |
resize(500, 500); |
|
58 |
} |
|
59 |
//! [0] |
|
60 |
||
61 |
//! [1] |
|
62 |
void MainWindow::closeEvent(QCloseEvent *event) |
|
63 |
//! [1] //! [2] |
|
64 |
{ |
|
65 |
if (maybeSave()) { |
|
66 |
event->accept(); |
|
67 |
} else { |
|
68 |
event->ignore(); |
|
69 |
} |
|
70 |
} |
|
71 |
//! [2] |
|
72 |
||
73 |
//! [3] |
|
74 |
void MainWindow::open() |
|
75 |
//! [3] //! [4] |
|
76 |
{ |
|
77 |
if (maybeSave()) { |
|
78 |
QString fileName = QFileDialog::getOpenFileName(this, |
|
79 |
tr("Open File"), QDir::currentPath()); |
|
80 |
if (!fileName.isEmpty()) |
|
81 |
scribbleArea->openImage(fileName); |
|
82 |
} |
|
83 |
} |
|
84 |
//! [4] |
|
85 |
||
86 |
//! [5] |
|
87 |
void MainWindow::save() |
|
88 |
//! [5] //! [6] |
|
89 |
{ |
|
90 |
QAction *action = qobject_cast<QAction *>(sender()); |
|
91 |
QByteArray fileFormat = action->data().toByteArray(); |
|
92 |
saveFile(fileFormat); |
|
93 |
} |
|
94 |
//! [6] |
|
95 |
||
96 |
//! [11] |
|
97 |
void MainWindow::about() |
|
98 |
//! [11] //! [12] |
|
99 |
{ |
|
100 |
QMessageBox::about(this, tr("About Scribble"), |
|
101 |
tr("<p>The <b>Scribble</b> example shows how to use QMainWindow as the " |
|
102 |
"base widget for an application, and how to reimplement some of " |
|
103 |
"QWidget's event handlers to receive the events generated for " |
|
104 |
"the application's widgets:</p><p> We reimplement the mouse event " |
|
105 |
"handlers to facilitate drawing, the paint event handler to " |
|
106 |
"update the application and the resize event handler to optimize " |
|
107 |
"the application's appearance. In addition we reimplement the " |
|
108 |
"close event handler to intercept the close events before " |
|
109 |
"terminating the application.</p><p> The example also demonstrates " |
|
110 |
"how to use QPainter to draw an image in real time, as well as " |
|
111 |
"to repaint widgets.</p>")); |
|
112 |
} |
|
113 |
//! [12] |
|
114 |
||
115 |
//! [13] |
|
116 |
void MainWindow::createActions() |
|
117 |
//! [13] //! [14] |
|
118 |
{ |
|
119 |
openAct = new QAction(tr("&Open..."), this); |
|
120 |
openAct->setShortcut(tr("Ctrl+O")); |
|
121 |
connect(openAct, SIGNAL(triggered()), this, SLOT(open())); |
|
122 |
||
123 |
foreach (QByteArray format, QImageWriter::supportedImageFormats()) { |
|
124 |
QString text = tr("%1...").arg(QString(format).toUpper()); |
|
125 |
||
126 |
QAction *action = new QAction(text, this); |
|
127 |
action->setData(format); |
|
128 |
connect(action, SIGNAL(triggered()), this, SLOT(save())); |
|
129 |
saveAsActs.append(action); |
|
130 |
} |
|
131 |
||
132 |
printAct = new QAction(tr("&Print..."), this); |
|
133 |
connect(printAct, SIGNAL(triggered()), scribbleArea, SLOT(print())); |
|
134 |
||
135 |
exitAct = new QAction(tr("E&xit"), this); |
|
136 |
exitAct->setShortcut(tr("Ctrl+Q")); |
|
137 |
connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); |
|
138 |
||
139 |
clearScreenAct = new QAction(tr("&Clear Screen"), this); |
|
140 |
clearScreenAct->setShortcut(tr("Ctrl+L")); |
|
141 |
connect(clearScreenAct, SIGNAL(triggered()), |
|
142 |
scribbleArea, SLOT(clearImage())); |
|
143 |
||
144 |
aboutAct = new QAction(tr("&About"), this); |
|
145 |
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); |
|
146 |
||
147 |
aboutQtAct = new QAction(tr("About &Qt"), this); |
|
148 |
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
|
149 |
} |
|
150 |
//! [14] |
|
151 |
||
152 |
//! [15] |
|
153 |
void MainWindow::createMenus() |
|
154 |
//! [15] //! [16] |
|
155 |
{ |
|
156 |
saveAsMenu = new QMenu(tr("&Save As"), this); |
|
157 |
foreach (QAction *action, saveAsActs) |
|
158 |
saveAsMenu->addAction(action); |
|
159 |
||
160 |
fileMenu = new QMenu(tr("&File"), this); |
|
161 |
fileMenu->addAction(openAct); |
|
162 |
fileMenu->addMenu(saveAsMenu); |
|
163 |
fileMenu->addAction(printAct); |
|
164 |
fileMenu->addSeparator(); |
|
165 |
fileMenu->addAction(exitAct); |
|
166 |
||
167 |
optionMenu = new QMenu(tr("&Options"), this); |
|
168 |
optionMenu->addAction(clearScreenAct); |
|
169 |
||
170 |
helpMenu = new QMenu(tr("&Help"), this); |
|
171 |
helpMenu->addAction(aboutAct); |
|
172 |
helpMenu->addAction(aboutQtAct); |
|
173 |
||
174 |
menuBar()->addMenu(fileMenu); |
|
175 |
menuBar()->addMenu(optionMenu); |
|
176 |
menuBar()->addMenu(helpMenu); |
|
177 |
} |
|
178 |
//! [16] |
|
179 |
||
180 |
//! [17] |
|
181 |
bool MainWindow::maybeSave() |
|
182 |
//! [17] //! [18] |
|
183 |
{ |
|
184 |
if (scribbleArea->isModified()) { |
|
185 |
QMessageBox::StandardButton ret; |
|
186 |
ret = QMessageBox::warning(this, tr("Scribble"), |
|
187 |
tr("The image has been modified.\n" |
|
188 |
"Do you want to save your changes?"), |
|
189 |
QMessageBox::Save | QMessageBox::Discard |
|
190 |
| QMessageBox::Cancel); |
|
191 |
if (ret == QMessageBox::Save) { |
|
192 |
return saveFile("png"); |
|
193 |
} else if (ret == QMessageBox::Cancel) { |
|
194 |
return false; |
|
195 |
} |
|
196 |
} |
|
197 |
return true; |
|
198 |
} |
|
199 |
//! [18] |
|
200 |
||
201 |
//! [19] |
|
202 |
bool MainWindow::saveFile(const QByteArray &fileFormat) |
|
203 |
//! [19] //! [20] |
|
204 |
{ |
|
205 |
QString initialPath = QDir::currentPath() + "/untitled." + fileFormat; |
|
206 |
||
207 |
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), |
|
208 |
initialPath, |
|
209 |
tr("%1 Files (*.%2);;All Files (*)") |
|
210 |
.arg(QString(fileFormat.toUpper())) |
|
211 |
.arg(QString(fileFormat))); |
|
212 |
if (fileName.isEmpty()) { |
|
213 |
return false; |
|
214 |
} else { |
|
215 |
return scribbleArea->saveImage(fileName, fileFormat); |
|
216 |
} |
|
217 |
} |
|
218 |
//! [20] |