| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | |
| 52 | #include "mainwindow.h" |
| 53 | #include "interfaces.h" |
| 54 | #include "paintarea.h" |
| 55 | #include "plugindialog.h" |
| 56 | |
| 57 | #include <QAction> |
| 58 | #include <QActionGroup> |
| 59 | #include <QApplication> |
| 60 | #include <QColorDialog> |
| 61 | #include <QFileDialog> |
| 62 | #include <QInputDialog> |
| 63 | #include <QMenu> |
| 64 | #include <QMenuBar> |
| 65 | #include <QMessageBox> |
| 66 | #include <QPluginLoader> |
| 67 | #include <QScrollArea> |
| 68 | #include <QTimer> |
| 69 | |
| 70 | MainWindow::MainWindow() : paintArea(new PaintArea) |
| 71 | , scrollArea(new QScrollArea) |
| 72 | { |
| 73 | scrollArea->setBackgroundRole(QPalette::Dark); |
| 74 | scrollArea->setWidget(paintArea); |
| 75 | setCentralWidget(scrollArea); |
| 76 | |
| 77 | createActions(); |
| 78 | createMenus(); |
| 79 | loadPlugins(); |
| 80 | |
| 81 | setWindowTitle(tr("Plug & Paint" )); |
| 82 | |
| 83 | if (!brushActionGroup->actions().isEmpty()) |
| 84 | brushActionGroup->actions().first()->trigger(); |
| 85 | |
| 86 | QTimer::singleShot(500, this, &MainWindow::aboutPlugins); |
| 87 | } |
| 88 | |
| 89 | void MainWindow::open() |
| 90 | { |
| 91 | const QString fileName = QFileDialog::getOpenFileName(this, |
| 92 | tr("Open File" ), |
| 93 | QDir::currentPath()); |
| 94 | if (!fileName.isEmpty()) { |
| 95 | if (!paintArea->openImage(fileName)) { |
| 96 | QMessageBox::information(this, tr("Plug & Paint" ), |
| 97 | tr("Cannot load %1." ).arg(fileName)); |
| 98 | return; |
| 99 | } |
| 100 | paintArea->adjustSize(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | bool MainWindow::saveAs() |
| 105 | { |
| 106 | const QString initialPath = QDir::currentPath() + "/untitled.png" ; |
| 107 | |
| 108 | const QString fileName = QFileDialog::getSaveFileName(this, tr("Save As" ), |
| 109 | initialPath); |
| 110 | if (fileName.isEmpty()) |
| 111 | return false; |
| 112 | |
| 113 | return paintArea->saveImage(fileName, "png" ); |
| 114 | } |
| 115 | |
| 116 | void MainWindow::brushColor() |
| 117 | { |
| 118 | const QColor newColor = QColorDialog::getColor(paintArea->brushColor()); |
| 119 | if (newColor.isValid()) |
| 120 | paintArea->setBrushColor(newColor); |
| 121 | } |
| 122 | |
| 123 | void MainWindow::brushWidth() |
| 124 | { |
| 125 | bool ok; |
| 126 | const int newWidth = QInputDialog::getInt(this, tr("Plug & Paint" ), |
| 127 | tr("Select brush width:" ), |
| 128 | paintArea->brushWidth(), |
| 129 | 1, 50, 1, &ok); |
| 130 | if (ok) |
| 131 | paintArea->setBrushWidth(newWidth); |
| 132 | } |
| 133 | |
| 134 | //! [0] |
| 135 | void MainWindow::changeBrush() |
| 136 | { |
| 137 | auto action = qobject_cast<QAction *>(sender()); |
| 138 | if (!action) |
| 139 | return; |
| 140 | auto iBrush = qobject_cast<BrushInterface *>(action->parent()); |
| 141 | if (!iBrush) |
| 142 | return; |
| 143 | const QString brush = action->text(); |
| 144 | |
| 145 | paintArea->setBrush(iBrush, brush); |
| 146 | } |
| 147 | //! [0] |
| 148 | |
| 149 | //! [1] |
| 150 | void MainWindow::insertShape() |
| 151 | { |
| 152 | auto action = qobject_cast<QAction *>(sender()); |
| 153 | if (!action) |
| 154 | return; |
| 155 | auto iShape = qobject_cast<ShapeInterface *>(action->parent()); |
| 156 | if (!iShape) |
| 157 | return; |
| 158 | |
| 159 | const QPainterPath path = iShape->generateShape(action->text(), this); |
| 160 | if (!path.isEmpty()) |
| 161 | paintArea->insertShape(path); |
| 162 | } |
| 163 | //! [1] |
| 164 | |
| 165 | //! [2] |
| 166 | void MainWindow::applyFilter() |
| 167 | { |
| 168 | auto action = qobject_cast<QAction *>(sender()); |
| 169 | if (!action) |
| 170 | return; |
| 171 | auto iFilter = qobject_cast<FilterInterface *>(action->parent()); |
| 172 | if (!iFilter) |
| 173 | return; |
| 174 | |
| 175 | const QImage image = iFilter->filterImage(action->text(), paintArea->image(), |
| 176 | this); |
| 177 | paintArea->setImage(image); |
| 178 | } |
| 179 | //! [2] |
| 180 | |
| 181 | void MainWindow::about() |
| 182 | { |
| 183 | QMessageBox::about(this, tr("About Plug & Paint" ), |
| 184 | tr("The <b>Plug & Paint</b> example demonstrates how to write Qt " |
| 185 | "applications that can be extended through plugins." )); |
| 186 | } |
| 187 | |
| 188 | //! [3] |
| 189 | void MainWindow::aboutPlugins() |
| 190 | { |
| 191 | PluginDialog dialog(pluginsDir.path(), pluginFileNames, this); |
| 192 | dialog.exec(); |
| 193 | } |
| 194 | //! [3] |
| 195 | |
| 196 | void MainWindow::createActions() |
| 197 | { |
| 198 | openAct = new QAction(tr("&Open..." ), this); |
| 199 | openAct->setShortcuts(QKeySequence::Open); |
| 200 | connect(openAct, &QAction::triggered, this, &MainWindow::open); |
| 201 | |
| 202 | saveAsAct = new QAction(tr("&Save As..." ), this); |
| 203 | saveAsAct->setShortcuts(QKeySequence::SaveAs); |
| 204 | connect(saveAsAct, &QAction::triggered, this, &MainWindow::saveAs); |
| 205 | |
| 206 | exitAct = new QAction(tr("E&xit" ), this); |
| 207 | exitAct->setShortcuts(QKeySequence::Quit); |
| 208 | connect(exitAct, &QAction::triggered, this, &MainWindow::close); |
| 209 | |
| 210 | brushColorAct = new QAction(tr("&Brush Color..." ), this); |
| 211 | connect(brushColorAct, &QAction::triggered, this, &MainWindow::brushColor); |
| 212 | |
| 213 | brushWidthAct = new QAction(tr("&Brush Width..." ), this); |
| 214 | connect(brushWidthAct, &QAction::triggered, this, &MainWindow::brushWidth); |
| 215 | |
| 216 | brushActionGroup = new QActionGroup(this); |
| 217 | |
| 218 | aboutAct = new QAction(tr("&About" ), this); |
| 219 | connect(aboutAct, &QAction::triggered, this, &MainWindow::about); |
| 220 | |
| 221 | aboutQtAct = new QAction(tr("About &Qt" ), this); |
| 222 | connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); |
| 223 | |
| 224 | aboutPluginsAct = new QAction(tr("About &Plugins" ), this); |
| 225 | connect(aboutPluginsAct, &QAction::triggered, this, &MainWindow::aboutPlugins); |
| 226 | } |
| 227 | |
| 228 | void MainWindow::createMenus() |
| 229 | { |
| 230 | fileMenu = menuBar()->addMenu(tr("&File" )); |
| 231 | fileMenu->addAction(openAct); |
| 232 | fileMenu->addAction(saveAsAct); |
| 233 | fileMenu->addSeparator(); |
| 234 | fileMenu->addAction(exitAct); |
| 235 | |
| 236 | brushMenu = menuBar()->addMenu(tr("&Brush" )); |
| 237 | brushMenu->addAction(brushColorAct); |
| 238 | brushMenu->addAction(brushWidthAct); |
| 239 | brushMenu->addSeparator(); |
| 240 | |
| 241 | shapesMenu = menuBar()->addMenu(tr("&Shapes" )); |
| 242 | |
| 243 | filterMenu = menuBar()->addMenu(tr("&Filter" )); |
| 244 | |
| 245 | menuBar()->addSeparator(); |
| 246 | |
| 247 | helpMenu = menuBar()->addMenu(tr("&Help" )); |
| 248 | helpMenu->addAction(aboutAct); |
| 249 | helpMenu->addAction(aboutQtAct); |
| 250 | helpMenu->addAction(aboutPluginsAct); |
| 251 | } |
| 252 | |
| 253 | //! [4] |
| 254 | void MainWindow::loadPlugins() |
| 255 | { |
| 256 | const auto staticInstances = QPluginLoader::staticInstances(); |
| 257 | for (QObject *plugin : staticInstances) |
| 258 | populateMenus(plugin); |
| 259 | //! [4] //! [5] |
| 260 | |
| 261 | pluginsDir = QDir(QCoreApplication::applicationDirPath()); |
| 262 | |
| 263 | #if defined(Q_OS_WIN) |
| 264 | if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release" ) |
| 265 | pluginsDir.cdUp(); |
| 266 | #elif defined(Q_OS_MAC) |
| 267 | if (pluginsDir.dirName() == "MacOS" ) { |
| 268 | pluginsDir.cdUp(); |
| 269 | pluginsDir.cdUp(); |
| 270 | pluginsDir.cdUp(); |
| 271 | } |
| 272 | #endif |
| 273 | pluginsDir.cd("plugins" ); |
| 274 | //! [5] |
| 275 | |
| 276 | //! [6] |
| 277 | const auto entryList = pluginsDir.entryList(QDir::Files); |
| 278 | for (const QString &fileName : entryList) { |
| 279 | QPluginLoader loader(pluginsDir.absoluteFilePath(fileName)); |
| 280 | QObject *plugin = loader.instance(); |
| 281 | if (plugin) { |
| 282 | populateMenus(plugin); |
| 283 | pluginFileNames += fileName; |
| 284 | //! [6] //! [7] |
| 285 | } |
| 286 | //! [7] //! [8] |
| 287 | } |
| 288 | //! [8] |
| 289 | |
| 290 | //! [9] |
| 291 | brushMenu->setEnabled(!brushActionGroup->actions().isEmpty()); |
| 292 | shapesMenu->setEnabled(!shapesMenu->actions().isEmpty()); |
| 293 | filterMenu->setEnabled(!filterMenu->actions().isEmpty()); |
| 294 | } |
| 295 | //! [9] |
| 296 | |
| 297 | //! [10] |
| 298 | void MainWindow::populateMenus(QObject *plugin) |
| 299 | { |
| 300 | auto iBrush = qobject_cast<BrushInterface *>(plugin); |
| 301 | if (iBrush) |
| 302 | addToMenu(plugin, iBrush->brushes(), brushMenu, &MainWindow::changeBrush, |
| 303 | brushActionGroup); |
| 304 | |
| 305 | auto iShape = qobject_cast<ShapeInterface *>(plugin); |
| 306 | if (iShape) |
| 307 | addToMenu(plugin, iShape->shapes(), shapesMenu, &MainWindow::insertShape); |
| 308 | |
| 309 | auto iFilter = qobject_cast<FilterInterface *>(plugin); |
| 310 | if (iFilter) |
| 311 | addToMenu(plugin, iFilter->filters(), filterMenu, &MainWindow::applyFilter); |
| 312 | } |
| 313 | //! [10] |
| 314 | |
| 315 | void MainWindow::addToMenu(QObject *plugin, const QStringList &texts, |
| 316 | QMenu *, Member member, |
| 317 | QActionGroup *actionGroup) |
| 318 | { |
| 319 | for (const QString &text : texts) { |
| 320 | auto action = new QAction(text, plugin); |
| 321 | connect(action, &QAction::triggered, this, member); |
| 322 | menu->addAction(action); |
| 323 | |
| 324 | if (actionGroup) { |
| 325 | action->setCheckable(true); |
| 326 | actionGroup->addAction(action); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |