1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "interfaces.h"
32
#include "mainwindow.h"
33
#include "paintarea.h"
34
#include "plugindialog.h"
36
MainWindow::MainWindow()
38
paintArea = new PaintArea;
40
scrollArea = new QScrollArea;
41
scrollArea->setBackgroundRole(QPalette::Dark);
42
scrollArea->setWidget(paintArea);
43
setCentralWidget(scrollArea);
49
setWindowTitle(tr("Plug & Paint"));
51
if (!brushActionGroup->actions().isEmpty())
52
brushActionGroup->actions().first()->trigger();
54
QTimer::singleShot(500, this, SLOT(aboutPlugins()));
57
void MainWindow::open()
59
QString fileName = QFileDialog::getOpenFileName(this,
60
tr("Open File"), QDir::currentPath());
61
if (!fileName.isEmpty()) {
62
if (!paintArea->openImage(fileName)) {
63
QMessageBox::information(this, tr("Plug & Paint"),
64
tr("Cannot load %1.").arg(fileName));
67
paintArea->adjustSize();
71
bool MainWindow::saveAs()
73
QString initialPath = QDir::currentPath() + "/untitled.png";
75
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
77
if (fileName.isEmpty()) {
80
return paintArea->saveImage(fileName, "png");
84
void MainWindow::brushColor()
86
QColor newColor = QColorDialog::getColor(paintArea->brushColor());
87
if (newColor.isValid())
88
paintArea->setBrushColor(newColor);
91
void MainWindow::brushWidth()
94
int newWidth = QInputDialog::getInteger(this, tr("Plug & Paint"),
95
tr("Select brush width:"),
96
paintArea->brushWidth(),
99
paintArea->setBrushWidth(newWidth);
102
void MainWindow::changeBrush()
104
QAction *action = qobject_cast<QAction *>(sender());
105
BrushInterface *iBrush = qobject_cast<BrushInterface *>(action->parent());
106
QString brush = action->text();
108
paintArea->setBrush(iBrush, brush);
111
void MainWindow::insertShape()
113
QAction *action = qobject_cast<QAction *>(sender());
114
ShapeInterface *iShape = qobject_cast<ShapeInterface *>(action->parent());
116
QPainterPath path = iShape->generateShape(action->text(), this);
118
paintArea->insertShape(path);
121
void MainWindow::applyFilter()
123
QAction *action = qobject_cast<QAction *>(sender());
124
FilterInterface *iFilter =
125
qobject_cast<FilterInterface *>(action->parent());
127
QImage image = iFilter->filterImage(action->text(), paintArea->image(),
129
paintArea->setImage(image);
132
void MainWindow::about()
134
QMessageBox::about(this, tr("About Plug & Paint"),
135
tr("The <b>Plug & Paint</b> example demonstrates how to write Qt "
136
"applications that can be extended through plugins."));
139
void MainWindow::aboutPlugins()
141
PluginDialog dialog(pluginsDir.path(), pluginFileNames, this);
145
void MainWindow::createActions()
147
openAct = new QAction(tr("&Open..."), this);
148
openAct->setShortcut(tr("Ctrl+O"));
149
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
151
saveAsAct = new QAction(tr("&Save As..."), this);
152
saveAsAct->setShortcut(tr("Ctrl+S"));
153
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
155
exitAct = new QAction(tr("E&xit"), this);
156
exitAct->setShortcut(tr("Ctrl+Q"));
157
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
159
brushColorAct = new QAction(tr("&Brush Color..."), this);
160
connect(brushColorAct, SIGNAL(triggered()), this, SLOT(brushColor()));
162
brushWidthAct = new QAction(tr("&Brush Width..."), this);
163
connect(brushWidthAct, SIGNAL(triggered()), this, SLOT(brushWidth()));
165
brushActionGroup = new QActionGroup(this);
167
aboutAct = new QAction(tr("&About"), this);
168
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
170
aboutQtAct = new QAction(tr("About &Qt"), this);
171
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
173
aboutPluginsAct = new QAction(tr("About &Plugins"), this);
174
connect(aboutPluginsAct, SIGNAL(triggered()), this, SLOT(aboutPlugins()));
177
void MainWindow::createMenus()
179
fileMenu = menuBar()->addMenu(tr("&File"));
180
fileMenu->addAction(openAct);
181
fileMenu->addAction(saveAsAct);
182
fileMenu->addSeparator();
183
fileMenu->addAction(exitAct);
185
brushMenu = menuBar()->addMenu(tr("&Brush"));
186
brushMenu->addAction(brushColorAct);
187
brushMenu->addAction(brushWidthAct);
188
brushMenu->addSeparator();
190
shapesMenu = menuBar()->addMenu(tr("&Shapes"));
192
filterMenu = menuBar()->addMenu(tr("&Filter"));
194
menuBar()->addSeparator();
196
helpMenu = menuBar()->addMenu(tr("&Help"));
197
helpMenu->addAction(aboutAct);
198
helpMenu->addAction(aboutQtAct);
199
helpMenu->addAction(aboutPluginsAct);
202
void MainWindow::loadPlugins()
204
pluginsDir = QDir(qApp->applicationDirPath());
205
#if defined(Q_OS_WIN)
206
if (pluginsDir.dirName() == "debug" || pluginsDir.dirName() == "release")
208
#elif defined(Q_OS_MAC)
209
if (pluginsDir.dirName() == "MacOS") {
215
pluginsDir.cd("plugins");
217
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
218
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
219
QObject *plugin = loader.instance();
221
BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin);
223
addToMenu(plugin, iBrush->brushes(), brushMenu,
224
SLOT(changeBrush()), brushActionGroup);
226
ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin);
228
addToMenu(plugin, iShape->shapes(), shapesMenu,
229
SLOT(insertShape()));
231
FilterInterface *iFilter = qobject_cast<FilterInterface *>(plugin);
233
addToMenu(plugin, iFilter->filters(), filterMenu,
234
SLOT(applyFilter()));
236
pluginFileNames += fileName;
240
brushMenu->setEnabled(!brushActionGroup->actions().isEmpty());
241
shapesMenu->setEnabled(!shapesMenu->actions().isEmpty());
242
filterMenu->setEnabled(!filterMenu->actions().isEmpty());
245
void MainWindow::addToMenu(QObject *plugin, const QStringList &texts,
246
QMenu *menu, const char *member,
247
QActionGroup *actionGroup)
249
foreach (QString text, texts) {
250
QAction *action = new QAction(text, plugin);
251
connect(action, SIGNAL(triggered()), this, member);
252
menu->addAction(action);
255
action->setCheckable(true);
256
actionGroup->addAction(action);