author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Mon, 19 Apr 2010 10:15:19 +0300 | |
branch | RCL_3 |
changeset 9 | b5b118452c7d |
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 |
||
43 |
#include "interfaces.h" |
|
44 |
#include "mainwindow.h" |
|
45 |
#include "paintarea.h" |
|
46 |
#include "plugindialog.h" |
|
47 |
||
48 |
#include <QPluginLoader> |
|
49 |
#include <QTimer> |
|
50 |
||
51 |
#include <QScrollArea> |
|
52 |
#include <QMessageBox> |
|
53 |
#include <QActionGroup> |
|
54 |
#include <QAction> |
|
55 |
#include <QMenu> |
|
56 |
#include <QMenuBar> |
|
57 |
#include <QFileDialog> |
|
58 |
#include <QColorDialog> |
|
59 |
#include <QInputDialog> |
|
60 |
#include <QApplication> |
|
61 |
||
62 |
MainWindow::MainWindow() : |
|
63 |
paintArea(new PaintArea), |
|
64 |
scrollArea(new QScrollArea) |
|
65 |
{ |
|
66 |
scrollArea->setBackgroundRole(QPalette::Dark); |
|
67 |
scrollArea->setWidget(paintArea); |
|
68 |
setCentralWidget(scrollArea); |
|
69 |
||
70 |
createActions(); |
|
71 |
createMenus(); |
|
72 |
loadPlugins(); |
|
73 |
||
74 |
setWindowTitle(tr("Plug & Paint")); |
|
75 |
||
76 |
if (!brushActionGroup->actions().isEmpty()) |
|
77 |
brushActionGroup->actions().first()->trigger(); |
|
78 |
||
79 |
QTimer::singleShot(500, this, SLOT(aboutPlugins())); |
|
80 |
} |
|
81 |
||
82 |
void MainWindow::open() |
|
83 |
{ |
|
84 |
const QString fileName = QFileDialog::getOpenFileName(this, |
|
85 |
tr("Open File"), |
|
86 |
QDir::currentPath()); |
|
87 |
if (!fileName.isEmpty()) { |
|
88 |
if (!paintArea->openImage(fileName)) { |
|
89 |
QMessageBox::information(this, tr("Plug & Paint"), |
|
90 |
tr("Cannot load %1.").arg(fileName)); |
|
91 |
return; |
|
92 |
} |
|
93 |
paintArea->adjustSize(); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
bool MainWindow::saveAs() |
|
98 |
{ |
|
99 |
const QString initialPath = QDir::currentPath() + "/untitled.png"; |
|
100 |
||
101 |
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), |
|
102 |
initialPath); |
|
103 |
if (fileName.isEmpty()) { |
|
104 |
return false; |
|
105 |
} else { |
|
106 |
return paintArea->saveImage(fileName, "png"); |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
void MainWindow::brushColor() |
|
111 |
{ |
|
112 |
const QColor newColor = QColorDialog::getColor(paintArea->brushColor()); |
|
113 |
if (newColor.isValid()) |
|
114 |
paintArea->setBrushColor(newColor); |
|
115 |
} |
|
116 |
||
117 |
void MainWindow::brushWidth() |
|
118 |
{ |
|
119 |
bool ok; |
|
120 |
const int newWidth = QInputDialog::getInteger(this, tr("Plug & Paint"), |
|
121 |
tr("Select brush width:"), |
|
122 |
paintArea->brushWidth(), |
|
123 |
1, 50, 1, &ok); |
|
124 |
if (ok) |
|
125 |
paintArea->setBrushWidth(newWidth); |
|
126 |
} |
|
127 |
||
128 |
//! [0] |
|
129 |
void MainWindow::changeBrush() |
|
130 |
{ |
|
131 |
QAction *action = qobject_cast<QAction *>(sender()); |
|
132 |
BrushInterface *iBrush = qobject_cast<BrushInterface *>(action->parent()); |
|
133 |
const QString brush = action->text(); |
|
134 |
||
135 |
paintArea->setBrush(iBrush, brush); |
|
136 |
} |
|
137 |
//! [0] |
|
138 |
||
139 |
//! [1] |
|
140 |
void MainWindow::insertShape() |
|
141 |
{ |
|
142 |
QAction *action = qobject_cast<QAction *>(sender()); |
|
143 |
ShapeInterface *iShape = qobject_cast<ShapeInterface *>(action->parent()); |
|
144 |
||
145 |
const QPainterPath path = iShape->generateShape(action->text(), this); |
|
146 |
if (!path.isEmpty()) |
|
147 |
paintArea->insertShape(path); |
|
148 |
} |
|
149 |
//! [1] |
|
150 |
||
151 |
//! [2] |
|
152 |
void MainWindow::applyFilter() |
|
153 |
{ |
|
154 |
QAction *action = qobject_cast<QAction *>(sender()); |
|
155 |
FilterInterface *iFilter = |
|
156 |
qobject_cast<FilterInterface *>(action->parent()); |
|
157 |
||
158 |
const QImage image = iFilter->filterImage(action->text(), paintArea->image(), |
|
159 |
this); |
|
160 |
paintArea->setImage(image); |
|
161 |
} |
|
162 |
//! [2] |
|
163 |
||
164 |
void MainWindow::about() |
|
165 |
{ |
|
166 |
QMessageBox::about(this, tr("About Plug & Paint"), |
|
167 |
tr("The <b>Plug & Paint</b> example demonstrates how to write Qt " |
|
168 |
"applications that can be extended through plugins.")); |
|
169 |
} |
|
170 |
||
171 |
//! [3] |
|
172 |
void MainWindow::aboutPlugins() |
|
173 |
{ |
|
174 |
PluginDialog dialog(pluginsDir.path(), pluginFileNames, this); |
|
175 |
dialog.exec(); |
|
176 |
} |
|
177 |
//! [3] |
|
178 |
||
179 |
void MainWindow::createActions() |
|
180 |
{ |
|
181 |
openAct = new QAction(tr("&Open..."), this); |
|
182 |
openAct->setShortcuts(QKeySequence::Open); |
|
183 |
connect(openAct, SIGNAL(triggered()), this, SLOT(open())); |
|
184 |
||
185 |
saveAsAct = new QAction(tr("&Save As..."), this); |
|
186 |
saveAsAct->setShortcuts(QKeySequence::SaveAs); |
|
187 |
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); |
|
188 |
||
189 |
exitAct = new QAction(tr("E&xit"), this); |
|
190 |
exitAct->setShortcuts(QKeySequence::Quit); |
|
191 |
connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); |
|
192 |
||
193 |
brushColorAct = new QAction(tr("&Brush Color..."), this); |
|
194 |
connect(brushColorAct, SIGNAL(triggered()), this, SLOT(brushColor())); |
|
195 |
||
196 |
brushWidthAct = new QAction(tr("&Brush Width..."), this); |
|
197 |
connect(brushWidthAct, SIGNAL(triggered()), this, SLOT(brushWidth())); |
|
198 |
||
199 |
brushActionGroup = new QActionGroup(this); |
|
200 |
||
201 |
aboutAct = new QAction(tr("&About"), this); |
|
202 |
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); |
|
203 |
||
204 |
aboutQtAct = new QAction(tr("About &Qt"), this); |
|
205 |
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
|
206 |
||
207 |
aboutPluginsAct = new QAction(tr("About &Plugins"), this); |
|
208 |
connect(aboutPluginsAct, SIGNAL(triggered()), this, SLOT(aboutPlugins())); |
|
209 |
} |
|
210 |
||
211 |
void MainWindow::createMenus() |
|
212 |
{ |
|
213 |
fileMenu = menuBar()->addMenu(tr("&File")); |
|
214 |
fileMenu->addAction(openAct); |
|
215 |
fileMenu->addAction(saveAsAct); |
|
216 |
fileMenu->addSeparator(); |
|
217 |
fileMenu->addAction(exitAct); |
|
218 |
||
219 |
brushMenu = menuBar()->addMenu(tr("&Brush")); |
|
220 |
brushMenu->addAction(brushColorAct); |
|
221 |
brushMenu->addAction(brushWidthAct); |
|
222 |
brushMenu->addSeparator(); |
|
223 |
||
224 |
shapesMenu = menuBar()->addMenu(tr("&Shapes")); |
|
225 |
||
226 |
filterMenu = menuBar()->addMenu(tr("&Filter")); |
|
227 |
||
228 |
menuBar()->addSeparator(); |
|
229 |
||
230 |
helpMenu = menuBar()->addMenu(tr("&Help")); |
|
231 |
helpMenu->addAction(aboutAct); |
|
232 |
helpMenu->addAction(aboutQtAct); |
|
233 |
helpMenu->addAction(aboutPluginsAct); |
|
234 |
} |
|
235 |
||
236 |
//! [4] |
|
237 |
void MainWindow::loadPlugins() |
|
238 |
{ |
|
239 |
foreach (QObject *plugin, QPluginLoader::staticInstances()) |
|
240 |
populateMenus(plugin); |
|
241 |
//! [4] //! [5] |
|
242 |
||
243 |
pluginsDir = QDir(qApp->applicationDirPath()); |
|
244 |
||
245 |
#if defined(Q_OS_WIN) |
|
246 |
if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release") |
|
247 |
pluginsDir.cdUp(); |
|
248 |
#elif defined(Q_OS_MAC) |
|
249 |
if (pluginsDir.dirName() == "MacOS") { |
|
250 |
pluginsDir.cdUp(); |
|
251 |
pluginsDir.cdUp(); |
|
252 |
pluginsDir.cdUp(); |
|
253 |
} |
|
254 |
#endif |
|
255 |
pluginsDir.cd("plugins"); |
|
256 |
//! [5] |
|
257 |
||
258 |
//! [6] |
|
259 |
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { |
|
260 |
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName)); |
|
261 |
QObject *plugin = loader.instance(); |
|
262 |
if (plugin) { |
|
263 |
populateMenus(plugin); |
|
264 |
pluginFileNames += fileName; |
|
265 |
//! [6] //! [7] |
|
266 |
} |
|
267 |
//! [7] //! [8] |
|
268 |
} |
|
269 |
//! [8] |
|
270 |
||
271 |
//! [9] |
|
272 |
brushMenu->setEnabled(!brushActionGroup->actions().isEmpty()); |
|
273 |
shapesMenu->setEnabled(!shapesMenu->actions().isEmpty()); |
|
274 |
filterMenu->setEnabled(!filterMenu->actions().isEmpty()); |
|
275 |
} |
|
276 |
//! [9] |
|
277 |
||
278 |
//! [10] |
|
279 |
void MainWindow::populateMenus(QObject *plugin) |
|
280 |
{ |
|
281 |
BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin); |
|
282 |
if (iBrush) |
|
283 |
addToMenu(plugin, iBrush->brushes(), brushMenu, SLOT(changeBrush()), |
|
284 |
brushActionGroup); |
|
285 |
||
286 |
ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin); |
|
287 |
if (iShape) |
|
288 |
addToMenu(plugin, iShape->shapes(), shapesMenu, SLOT(insertShape())); |
|
289 |
||
290 |
FilterInterface *iFilter = qobject_cast<FilterInterface *>(plugin); |
|
291 |
if (iFilter) |
|
292 |
addToMenu(plugin, iFilter->filters(), filterMenu, SLOT(applyFilter())); |
|
293 |
} |
|
294 |
//! [10] |
|
295 |
||
296 |
void MainWindow::addToMenu(QObject *plugin, const QStringList &texts, |
|
297 |
QMenu *menu, const char *member, |
|
298 |
QActionGroup *actionGroup) |
|
299 |
{ |
|
300 |
foreach (QString text, texts) { |
|
301 |
QAction *action = new QAction(text, plugin); |
|
302 |
connect(action, SIGNAL(triggered()), this, member); |
|
303 |
menu->addAction(action); |
|
304 |
||
305 |
if (actionGroup) { |
|
306 |
action->setCheckable(true); |
|
307 |
actionGroup->addAction(action); |
|
308 |
} |
|
309 |
} |
|
310 |
} |