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 tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include <QtWidgets>
30
31 #include "mainwindow.h"
32 #include "scribblearea.h"
33
34 //! [0]
MainWindow()35 MainWindow::MainWindow()
36 {
37 scribbleArea = new ScribbleArea;
38 setCentralWidget(scribbleArea);
39
40 createActions();
41 createMenus();
42
43 setWindowTitle(tr("Scribble"));
44 resize(500, 500);
45 }
46 //! [0]
47
48 //! [1]
closeEvent(QCloseEvent * event)49 void MainWindow::closeEvent(QCloseEvent *event)
50 //! [1] //! [2]
51 {
52 if (maybeSave()) {
53 event->accept();
54 } else {
55 event->ignore();
56 }
57 }
58 //! [2]
59
60 //! [3]
open()61 void MainWindow::open()
62 //! [3] //! [4]
63 {
64 if (maybeSave()) {
65 QString fileName = QFileDialog::getOpenFileName(this,
66 tr("Open File"), QDir::currentPath());
67 if (!fileName.isEmpty())
68 scribbleArea->openImage(fileName);
69 }
70 }
71 //! [4]
72
73 //! [5]
save()74 void MainWindow::save()
75 //! [5] //! [6]
76 {
77 QAction *action = qobject_cast<QAction *>(sender());
78 QByteArray fileFormat = action->data().toByteArray();
79 saveFile(fileFormat);
80 }
81 //! [6]
82
83 //! [7]
penColor()84 void MainWindow::penColor()
85 //! [7] //! [8]
86 {
87 QColor newColor = QColorDialog::getColor(scribbleArea->penColor());
88 if (newColor.isValid())
89 scribbleArea->setPenColor(newColor);
90 }
91 //! [8]
92
93 //! [9]
penWidth()94 void MainWindow::penWidth()
95 //! [9] //! [10]
96 {
97 bool ok;
98 int newWidth = QInputDialog::getInteger(this, tr("Scribble"),
99 tr("Select pen width:"),
100 scribbleArea->penWidth(),
101 1, 50, 1, &ok);
102 if (ok)
103 scribbleArea->setPenWidth(newWidth);
104 }
105 //! [10]
106
107 //! [11]
about()108 void MainWindow::about()
109 //! [11] //! [12]
110 {
111 QMessageBox::about(this, tr("About Scribble"),
112 tr("<p>The <b>Scribble</b> example shows how to use QMainWindow as the "
113 "base widget for an application, and how to reimplement some of "
114 "QWidget's event handlers to receive the events generated for "
115 "the application's widgets:</p><p> We reimplement the mouse event "
116 "handlers to facilitate drawing, the paint event handler to "
117 "update the application and the resize event handler to optimize "
118 "the application's appearance. In addition we reimplement the "
119 "close event handler to intercept the close events before "
120 "terminating the application.</p><p> The example also demonstrates "
121 "how to use QPainter to draw an image in real time, as well as "
122 "to repaint widgets.</p>"));
123 }
124 //! [12]
125
126 //! [13]
createActions()127 void MainWindow::createActions()
128 //! [13] //! [14]
129 {
130 openAct = new QAction(tr("&Open..."), this);
131 openAct->setShortcuts(QKeySequence::Open);
132 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
133
134 foreach (const QByteArray &format, QImageWriter::supportedImageFormats()) {
135 QString text = tr("%1...").arg(QString(format).toUpper());
136
137 QAction *action = new QAction(text, this);
138 action->setData(format);
139 connect(action, SIGNAL(triggered()), this, SLOT(save()));
140 saveAsActs.append(action);
141 }
142
143 printAct = new QAction(tr("&Print..."), this);
144 connect(printAct, SIGNAL(triggered()), scribbleArea, SLOT(print()));
145
146 exitAct = new QAction(tr("E&xit"), this);
147 exitAct->setShortcuts(QKeySequence::Quit);
148 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
149
150 penColorAct = new QAction(tr("&Pen Color..."), this);
151 connect(penColorAct, SIGNAL(triggered()), this, SLOT(penColor()));
152
153 penWidthAct = new QAction(tr("Pen &Width..."), this);
154 connect(penWidthAct, SIGNAL(triggered()), this, SLOT(penWidth()));
155
156 clearScreenAct = new QAction(tr("&Clear Screen"), this);
157 clearScreenAct->setShortcut(tr("Ctrl+L"));
158 connect(clearScreenAct, SIGNAL(triggered()),
159 scribbleArea, SLOT(clearImage()));
160
161 aboutAct = new QAction(tr("&About"), this);
162 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
163
164 aboutQtAct = new QAction(tr("About &Qt"), this);
165 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
166 }
167 //! [14]
168
169 //! [15]
createMenus()170 void MainWindow::createMenus()
171 //! [15] //! [16]
172 {
173 saveAsMenu = new QMenu(tr("&Save As"), this);
174 foreach (QAction *action, saveAsActs)
175 saveAsMenu->addAction(action);
176
177 fileMenu = new QMenu(tr("&File"), this);
178 fileMenu->addAction(openAct);
179 fileMenu->addMenu(saveAsMenu);
180 fileMenu->addAction(printAct);
181 fileMenu->addSeparator();
182 fileMenu->addAction(exitAct);
183
184 optionMenu = new QMenu(tr("&Options"), this);
185 optionMenu->addAction(penColorAct);
186 optionMenu->addAction(penWidthAct);
187 optionMenu->addSeparator();
188 optionMenu->addAction(clearScreenAct);
189
190 helpMenu = new QMenu(tr("&Help"), this);
191 helpMenu->addAction(aboutAct);
192 helpMenu->addAction(aboutQtAct);
193
194 menuBar()->addMenu(fileMenu);
195 menuBar()->addMenu(optionMenu);
196 menuBar()->addMenu(helpMenu);
197 }
198 //! [16]
199
200 //! [17]
maybeSave()201 bool MainWindow::maybeSave()
202 //! [17] //! [18]
203 {
204 if (scribbleArea->isModified()) {
205 QMessageBox::StandardButton ret;
206 ret = QMessageBox::warning(this, tr("Scribble"),
207 tr("The image has been modified.\n"
208 "Do you want to save your changes?"),
209 QMessageBox::Save | QMessageBox::Discard
210 | QMessageBox::Cancel);
211 if (ret == QMessageBox::Save) {
212 return saveFile("png");
213 } else if (ret == QMessageBox::Cancel) {
214 return false;
215 }
216 }
217 return true;
218 }
219 //! [18]
220
221 //! [19]
saveFile(const QByteArray & fileFormat)222 bool MainWindow::saveFile(const QByteArray &fileFormat)
223 //! [19] //! [20]
224 {
225 QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
226
227 QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
228 initialPath,
229 tr("%1 Files (*.%2);;All Files (*)")
230 .arg(QString(fileFormat.toUpper()))
231 .arg(QString(fileFormat)));
232 if (fileName.isEmpty()) {
233 return false;
234 } else {
235 return scribbleArea->saveImage(fileName, fileFormat);
236 }
237 }
238 //! [20]
239