1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: mainwindow.cpp Example File (tools/plugandpaint/mainwindow.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">mainwindow.cpp Example File<br /><small><small>tools/plugandpaint/mainwindow.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
50
#include "interfaces.h"
51
#include "mainwindow.h"
52
#include "paintarea.h"
53
#include "plugindialog.h"
55
MainWindow::MainWindow()
57
paintArea = new PaintArea;
59
scrollArea = new QScrollArea;
60
scrollArea->setBackgroundRole(QPalette::Dark);
61
scrollArea->setWidget(paintArea);
62
setCentralWidget(scrollArea);
68
setWindowTitle(tr("Plug & Paint"));
70
if (!brushActionGroup->actions().isEmpty())
71
brushActionGroup->actions().first()->trigger();
73
QTimer::singleShot(500, this, SLOT(aboutPlugins()));
76
void MainWindow::open()
78
QString fileName = QFileDialog::getOpenFileName(this,
79
tr("Open File"), QDir::currentPath());
80
if (!fileName.isEmpty()) {
81
if (!paintArea->openImage(fileName)) {
82
QMessageBox::information(this, tr("Plug & Paint"),
83
tr("Cannot load %1.").arg(fileName));
86
paintArea->adjustSize();
90
bool MainWindow::saveAs()
92
QString initialPath = QDir::currentPath() + "/untitled.png";
94
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
96
if (fileName.isEmpty()) {
99
return paintArea->saveImage(fileName, "png");
103
void MainWindow::brushColor()
105
QColor newColor = QColorDialog::getColor(paintArea->brushColor());
106
if (newColor.isValid())
107
paintArea->setBrushColor(newColor);
110
void MainWindow::brushWidth()
113
int newWidth = QInputDialog::getInteger(this, tr("Plug & Paint"),
114
tr("Select brush width:"),
115
paintArea->brushWidth(),
118
paintArea->setBrushWidth(newWidth);
121
void MainWindow::changeBrush()
123
QAction *action = qobject_cast<QAction *>(sender());
124
BrushInterface *iBrush = qobject_cast<BrushInterface *>(action->parent());
125
QString brush = action->text();
127
paintArea->setBrush(iBrush, brush);
130
void MainWindow::insertShape()
132
QAction *action = qobject_cast<QAction *>(sender());
133
ShapeInterface *iShape = qobject_cast<ShapeInterface *>(action->parent());
135
QPainterPath path = iShape->generateShape(action->text(), this);
137
paintArea->insertShape(path);
140
void MainWindow::applyFilter()
142
QAction *action = qobject_cast<QAction *>(sender());
143
FilterInterface *iFilter =
144
qobject_cast<FilterInterface *>(action->parent());
146
QImage image = iFilter->filterImage(action->text(), paintArea->image(),
148
paintArea->setImage(image);
151
void MainWindow::about()
153
QMessageBox::about(this, tr("About Plug & Paint"),
154
tr("The <b>Plug & Paint</b> example demonstrates how to write Qt "
155
"applications that can be extended through plugins."));
158
void MainWindow::aboutPlugins()
160
PluginDialog dialog(pluginsDir.path(), pluginFileNames, this);
164
void MainWindow::createActions()
166
openAct = new QAction(tr("&Open..."), this);
167
openAct->setShortcut(tr("Ctrl+O"));
168
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
170
saveAsAct = new QAction(tr("&Save As..."), this);
171
saveAsAct->setShortcut(tr("Ctrl+S"));
172
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
174
exitAct = new QAction(tr("E&xit"), this);
175
exitAct->setShortcut(tr("Ctrl+Q"));
176
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
178
brushColorAct = new QAction(tr("&Brush Color..."), this);
179
connect(brushColorAct, SIGNAL(triggered()), this, SLOT(brushColor()));
181
brushWidthAct = new QAction(tr("&Brush Width..."), this);
182
connect(brushWidthAct, SIGNAL(triggered()), this, SLOT(brushWidth()));
184
brushActionGroup = new QActionGroup(this);
186
aboutAct = new QAction(tr("&About"), this);
187
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
189
aboutQtAct = new QAction(tr("About &Qt"), this);
190
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
192
aboutPluginsAct = new QAction(tr("About &Plugins"), this);
193
connect(aboutPluginsAct, SIGNAL(triggered()), this, SLOT(aboutPlugins()));
196
void MainWindow::createMenus()
198
fileMenu = menuBar()->addMenu(tr("&File"));
199
fileMenu->addAction(openAct);
200
fileMenu->addAction(saveAsAct);
201
fileMenu->addSeparator();
202
fileMenu->addAction(exitAct);
204
brushMenu = menuBar()->addMenu(tr("&Brush"));
205
brushMenu->addAction(brushColorAct);
206
brushMenu->addAction(brushWidthAct);
207
brushMenu->addSeparator();
209
shapesMenu = menuBar()->addMenu(tr("&Shapes"));
211
filterMenu = menuBar()->addMenu(tr("&Filter"));
213
menuBar()->addSeparator();
215
helpMenu = menuBar()->addMenu(tr("&Help"));
216
helpMenu->addAction(aboutAct);
217
helpMenu->addAction(aboutQtAct);
218
helpMenu->addAction(aboutPluginsAct);
221
void MainWindow::loadPlugins()
223
pluginsDir = QDir(qApp->applicationDirPath());
224
#if defined(Q_OS_WIN)
225
if (pluginsDir.dirName() == "debug" || pluginsDir.dirName() == "release")
227
#elif defined(Q_OS_MAC)
228
if (pluginsDir.dirName() == "MacOS") {
234
pluginsDir.cd("plugins");
236
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
237
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
238
QObject *plugin = loader.instance();
240
BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin);
242
addToMenu(plugin, iBrush->brushes(), brushMenu,
243
SLOT(changeBrush()), brushActionGroup);
245
ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin);
247
addToMenu(plugin, iShape->shapes(), shapesMenu,
248
SLOT(insertShape()));
250
FilterInterface *iFilter = qobject_cast<FilterInterface *>(plugin);
252
addToMenu(plugin, iFilter->filters(), filterMenu,
253
SLOT(applyFilter()));
255
pluginFileNames += fileName;
259
brushMenu->setEnabled(!brushActionGroup->actions().isEmpty());
260
shapesMenu->setEnabled(!shapesMenu->actions().isEmpty());
261
filterMenu->setEnabled(!filterMenu->actions().isEmpty());
264
void MainWindow::addToMenu(QObject *plugin, const QStringList &texts,
265
QMenu *menu, const char *member,
266
QActionGroup *actionGroup)
268
foreach (QString text, texts) {
269
QAction *action = new QAction(text, plugin);
270
connect(action, SIGNAL(triggered()), this, member);
271
menu->addAction(action);
274
action->setCheckable(true);
275
actionGroup->addAction(action);
279
<p /><address><hr /><div align="center">
280
<table width="100%" cellspacing="0" border="0"><tr class="address">
281
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
282
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
283
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
284
</tr></table></div></address></body>