| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 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->setShortcut(tr("Ctrl+O")); | 
|---|
| 183 | connect(openAct, SIGNAL(triggered()), this, SLOT(open())); | 
|---|
| 184 |  | 
|---|
| 185 | saveAsAct = new QAction(tr("&Save As..."), this); | 
|---|
| 186 | saveAsAct->setShortcut(tr("Ctrl+S")); | 
|---|
| 187 | connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); | 
|---|
| 188 |  | 
|---|
| 189 | exitAct = new QAction(tr("E&xit"), this); | 
|---|
| 190 | exitAct->setShortcut(tr("Ctrl+Q")); | 
|---|
| 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 | } | 
|---|