1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
43
#include "mainwindow.h"
44
#include "scribblearea.h"
47
MainWindow::MainWindow()
49
scribbleArea = new ScribbleArea;
50
setCentralWidget(scribbleArea);
55
setWindowTitle(tr("Finger Paint"));
61
void MainWindow::closeEvent(QCloseEvent *event)
73
void MainWindow::open()
77
QString fileName = QFileDialog::getOpenFileName(this,
78
tr("Open File"), QDir::currentPath());
79
if (!fileName.isEmpty())
80
scribbleArea->openImage(fileName);
86
void MainWindow::save()
89
QAction *action = qobject_cast<QAction *>(sender());
90
QByteArray fileFormat = action->data().toByteArray();
96
void MainWindow::about()
99
QMessageBox::about(this, tr("About Scribble"),
100
tr("<p>The <b>Scribble</b> example shows how to use QMainWindow as the "
101
"base widget for an application, and how to reimplement some of "
102
"QWidget's event handlers to receive the events generated for "
103
"the application's widgets:</p><p> We reimplement the mouse event "
104
"handlers to facilitate drawing, the paint event handler to "
105
"update the application and the resize event handler to optimize "
106
"the application's appearance. In addition we reimplement the "
107
"close event handler to intercept the close events before "
108
"terminating the application.</p><p> The example also demonstrates "
109
"how to use QPainter to draw an image in real time, as well as "
110
"to repaint widgets.</p>"));
115
void MainWindow::createActions()
118
openAct = new QAction(tr("&Open..."), this);
119
openAct->setShortcut(tr("Ctrl+O"));
120
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
122
foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
123
QString text = tr("%1...").arg(QString(format).toUpper());
125
QAction *action = new QAction(text, this);
126
action->setData(format);
127
connect(action, SIGNAL(triggered()), this, SLOT(save()));
128
saveAsActs.append(action);
131
printAct = new QAction(tr("&Print..."), this);
132
connect(printAct, SIGNAL(triggered()), scribbleArea, SLOT(print()));
134
exitAct = new QAction(tr("E&xit"), this);
135
exitAct->setShortcut(tr("Ctrl+Q"));
136
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
138
clearScreenAct = new QAction(tr("&Clear Screen"), this);
139
clearScreenAct->setShortcut(tr("Ctrl+L"));
140
connect(clearScreenAct, SIGNAL(triggered()),
141
scribbleArea, SLOT(clearImage()));
143
aboutAct = new QAction(tr("&About"), this);
144
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
146
aboutQtAct = new QAction(tr("About &Qt"), this);
147
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
152
void MainWindow::createMenus()
155
saveAsMenu = new QMenu(tr("&Save As"), this);
156
foreach (QAction *action, saveAsActs)
157
saveAsMenu->addAction(action);
159
fileMenu = new QMenu(tr("&File"), this);
160
fileMenu->addAction(openAct);
161
fileMenu->addMenu(saveAsMenu);
162
fileMenu->addAction(printAct);
163
fileMenu->addSeparator();
164
fileMenu->addAction(exitAct);
166
optionMenu = new QMenu(tr("&Options"), this);
167
optionMenu->addAction(clearScreenAct);
169
helpMenu = new QMenu(tr("&Help"), this);
170
helpMenu->addAction(aboutAct);
171
helpMenu->addAction(aboutQtAct);
173
menuBar()->addMenu(fileMenu);
174
menuBar()->addMenu(optionMenu);
175
menuBar()->addMenu(helpMenu);
180
bool MainWindow::maybeSave()
183
if (scribbleArea->isModified()) {
184
QMessageBox::StandardButton ret;
185
ret = QMessageBox::warning(this, tr("Scribble"),
186
tr("The image has been modified.\n"
187
"Do you want to save your changes?"),
188
QMessageBox::Save | QMessageBox::Discard
189
| QMessageBox::Cancel);
190
if (ret == QMessageBox::Save) {
191
return saveFile("png");
192
} else if (ret == QMessageBox::Cancel) {
201
bool MainWindow::saveFile(const QByteArray &fileFormat)
204
QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
206
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
208
tr("%1 Files (*.%2);;All Files (*)")
209
.arg(QString::fromLatin1(fileFormat.toUpper()))
210
.arg(QString::fromLatin1(fileFormat)));
211
if (fileName.isEmpty()) {
214
return scribbleArea->saveImage(fileName, fileFormat.constData());