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(s: "Plug & Paint" )); |
82 | |
83 | if (!brushActionGroup->actions().isEmpty()) |
84 | brushActionGroup->actions().first()->trigger(); |
85 | |
86 | QTimer::singleShot(interval: 500, receiver: this, slot: &MainWindow::aboutPlugins); |
87 | } |
88 | |
89 | void MainWindow::open() |
90 | { |
91 | const QString fileName = QFileDialog::getOpenFileName(parent: this, |
92 | caption: tr(s: "Open File" ), |
93 | dir: QDir::currentPath()); |
94 | if (!fileName.isEmpty()) { |
95 | if (!paintArea->openImage(fileName)) { |
96 | QMessageBox::information(parent: this, title: tr(s: "Plug & Paint" ), |
97 | text: tr(s: "Cannot load %1." ).arg(a: 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(parent: this, caption: tr(s: "Save As" ), |
109 | dir: initialPath); |
110 | if (fileName.isEmpty()) |
111 | return false; |
112 | |
113 | return paintArea->saveImage(fileName, fileFormat: "png" ); |
114 | } |
115 | |
116 | void MainWindow::brushColor() |
117 | { |
118 | const QColor newColor = QColorDialog::getColor(initial: 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(parent: this, title: tr(s: "Plug & Paint" ), |
127 | label: tr(s: "Select brush width:" ), |
128 | value: paintArea->brushWidth(), |
129 | minValue: 1, maxValue: 50, step: 1, ok: &ok); |
130 | if (ok) |
131 | paintArea->setBrushWidth(newWidth); |
132 | } |
133 | |
134 | //! [0] |
135 | void MainWindow::changeBrush() |
136 | { |
137 | auto action = qobject_cast<QAction *>(object: sender()); |
138 | if (!action) |
139 | return; |
140 | auto iBrush = qobject_cast<BrushInterface *>(object: action->parent()); |
141 | if (!iBrush) |
142 | return; |
143 | const QString brush = action->text(); |
144 | |
145 | paintArea->setBrush(brushInterface: iBrush, brush); |
146 | } |
147 | //! [0] |
148 | |
149 | //! [1] |
150 | void MainWindow::insertShape() |
151 | { |
152 | auto action = qobject_cast<QAction *>(object: sender()); |
153 | if (!action) |
154 | return; |
155 | auto iShape = qobject_cast<ShapeInterface *>(object: action->parent()); |
156 | if (!iShape) |
157 | return; |
158 | |
159 | const QPainterPath path = iShape->generateShape(shape: action->text(), parent: 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 *>(object: sender()); |
169 | if (!action) |
170 | return; |
171 | auto iFilter = qobject_cast<FilterInterface *>(object: action->parent()); |
172 | if (!iFilter) |
173 | return; |
174 | |
175 | const QImage image = iFilter->filterImage(filter: action->text(), image: paintArea->image(), |
176 | parent: this); |
177 | paintArea->setImage(image); |
178 | } |
179 | //! [2] |
180 | |
181 | void MainWindow::about() |
182 | { |
183 | QMessageBox::about(parent: this, title: tr(s: "About Plug & Paint" ), |
184 | text: tr(s: "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(s: "&Open..." ), this); |
199 | openAct->setShortcuts(QKeySequence::Open); |
200 | connect(sender: openAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::open); |
201 | |
202 | saveAsAct = new QAction(tr(s: "&Save As..." ), this); |
203 | saveAsAct->setShortcuts(QKeySequence::SaveAs); |
204 | connect(sender: saveAsAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::saveAs); |
205 | |
206 | exitAct = new QAction(tr(s: "E&xit" ), this); |
207 | exitAct->setShortcuts(QKeySequence::Quit); |
208 | connect(sender: exitAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::close); |
209 | |
210 | brushColorAct = new QAction(tr(s: "&Brush Color..." ), this); |
211 | connect(sender: brushColorAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::brushColor); |
212 | |
213 | brushWidthAct = new QAction(tr(s: "&Brush Width..." ), this); |
214 | connect(sender: brushWidthAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::brushWidth); |
215 | |
216 | brushActionGroup = new QActionGroup(this); |
217 | |
218 | aboutAct = new QAction(tr(s: "&About" ), this); |
219 | connect(sender: aboutAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::about); |
220 | |
221 | aboutQtAct = new QAction(tr(s: "About &Qt" ), this); |
222 | connect(sender: aboutQtAct, signal: &QAction::triggered, qApp, slot: &QApplication::aboutQt); |
223 | |
224 | aboutPluginsAct = new QAction(tr(s: "About &Plugins" ), this); |
225 | connect(sender: aboutPluginsAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::aboutPlugins); |
226 | } |
227 | |
228 | void MainWindow::createMenus() |
229 | { |
230 | fileMenu = menuBar()->addMenu(title: tr(s: "&File" )); |
231 | fileMenu->addAction(action: openAct); |
232 | fileMenu->addAction(action: saveAsAct); |
233 | fileMenu->addSeparator(); |
234 | fileMenu->addAction(action: exitAct); |
235 | |
236 | brushMenu = menuBar()->addMenu(title: tr(s: "&Brush" )); |
237 | brushMenu->addAction(action: brushColorAct); |
238 | brushMenu->addAction(action: brushWidthAct); |
239 | brushMenu->addSeparator(); |
240 | |
241 | shapesMenu = menuBar()->addMenu(title: tr(s: "&Shapes" )); |
242 | |
243 | filterMenu = menuBar()->addMenu(title: tr(s: "&Filter" )); |
244 | |
245 | menuBar()->addSeparator(); |
246 | |
247 | helpMenu = menuBar()->addMenu(title: tr(s: "&Help" )); |
248 | helpMenu->addAction(action: aboutAct); |
249 | helpMenu->addAction(action: aboutQtAct); |
250 | helpMenu->addAction(action: 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(dirName: "plugins" ); |
274 | //! [5] |
275 | |
276 | //! [6] |
277 | const auto entryList = pluginsDir.entryList(filters: 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 *>(object: plugin); |
301 | if (iBrush) |
302 | addToMenu(plugin, texts: iBrush->brushes(), menu: brushMenu, member: &MainWindow::changeBrush, |
303 | actionGroup: brushActionGroup); |
304 | |
305 | auto iShape = qobject_cast<ShapeInterface *>(object: plugin); |
306 | if (iShape) |
307 | addToMenu(plugin, texts: iShape->shapes(), menu: shapesMenu, member: &MainWindow::insertShape); |
308 | |
309 | auto iFilter = qobject_cast<FilterInterface *>(object: plugin); |
310 | if (iFilter) |
311 | addToMenu(plugin, texts: iFilter->filters(), menu: filterMenu, member: &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(sender: action, signal: &QAction::triggered, receiver: this, slot: member); |
322 | menu->addAction(action); |
323 | |
324 | if (actionGroup) { |
325 | action->setCheckable(true); |
326 | actionGroup->addAction(a: action); |
327 | } |
328 | } |
329 | } |
330 | |