1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "mainwindow.h"
32
#include "scribblearea.h"
34
MainWindow::MainWindow()
36
scribbleArea = new ScribbleArea;
37
setCentralWidget(scribbleArea);
42
setWindowTitle(tr("Scribble"));
46
void MainWindow::closeEvent(QCloseEvent *event)
55
void MainWindow::open()
58
QString fileName = QFileDialog::getOpenFileName(this,
59
tr("Open File"), QDir::currentPath());
60
if (!fileName.isEmpty())
61
scribbleArea->openImage(fileName);
65
void MainWindow::save()
67
QAction *action = qobject_cast<QAction *>(sender());
68
QByteArray fileFormat = action->data().toByteArray();
72
void MainWindow::penColor()
74
QColor newColor = QColorDialog::getColor(scribbleArea->penColor());
75
if (newColor.isValid())
76
scribbleArea->setPenColor(newColor);
79
void MainWindow::penWidth()
82
int newWidth = QInputDialog::getInteger(this, tr("Scribble"),
83
tr("Select pen width:"),
84
scribbleArea->penWidth(),
87
scribbleArea->setPenWidth(newWidth);
90
void MainWindow::about()
92
QMessageBox::about(this, tr("About Scribble"),
93
tr("<p>The <b>Scribble</b> example shows how to use QMainWindow as the "
94
"base widget for an application, and how to reimplement some of "
95
"QWidget's event handlers to receive the events generated for "
96
"the application's widgets:</p><p> We reimplement the mouse event "
97
"handlers to facilitate drawing, the paint event handler to "
98
"update the application and the resize event handler to optimize "
99
"the application's appearance. In addition we reimplement the "
100
"close event handler to intercept the close events before "
101
"terminating the application.</p><p> The example also demonstrates "
102
"how to use QPainter to draw an image in real time, as well as "
103
"to repaint widgets.</p>"));
106
void MainWindow::createActions()
108
openAct = new QAction(tr("&Open..."), this);
109
openAct->setShortcut(tr("Ctrl+O"));
110
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
112
foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
113
QString text = tr("%1...").arg(QString(format).toUpper());
115
QAction *action = new QAction(text, this);
116
action->setData(format);
117
connect(action, SIGNAL(triggered()), this, SLOT(save()));
118
saveAsActs.append(action);
121
exitAct = new QAction(tr("E&xit"), this);
122
exitAct->setShortcut(tr("Ctrl+Q"));
123
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
125
penColorAct = new QAction(tr("&Pen Color..."), this);
126
connect(penColorAct, SIGNAL(triggered()), this, SLOT(penColor()));
128
penWidthAct = new QAction(tr("Pen &Width..."), this);
129
connect(penWidthAct, SIGNAL(triggered()), this, SLOT(penWidth()));
131
clearScreenAct = new QAction(tr("&Clear Screen"), this);
132
clearScreenAct->setShortcut(tr("Ctrl+L"));
133
connect(clearScreenAct, SIGNAL(triggered()),
134
scribbleArea, SLOT(clearImage()));
136
aboutAct = new QAction(tr("&About"), this);
137
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
139
aboutQtAct = new QAction(tr("About &Qt"), this);
140
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
143
void MainWindow::createMenus()
145
saveAsMenu = new QMenu(tr("&Save As"), this);
146
foreach (QAction *action, saveAsActs)
147
saveAsMenu->addAction(action);
149
fileMenu = new QMenu(tr("&File"), this);
150
fileMenu->addAction(openAct);
151
fileMenu->addMenu(saveAsMenu);
152
fileMenu->addSeparator();
153
fileMenu->addAction(exitAct);
155
optionMenu = new QMenu(tr("&Options"), this);
156
optionMenu->addAction(penColorAct);
157
optionMenu->addAction(penWidthAct);
158
optionMenu->addSeparator();
159
optionMenu->addAction(clearScreenAct);
161
helpMenu = new QMenu(tr("&Help"), this);
162
helpMenu->addAction(aboutAct);
163
helpMenu->addAction(aboutQtAct);
165
menuBar()->addMenu(fileMenu);
166
menuBar()->addMenu(optionMenu);
167
menuBar()->addMenu(helpMenu);
170
bool MainWindow::maybeSave()
172
if (scribbleArea->isModified()) {
173
int ret = QMessageBox::warning(this, tr("Scribble"),
174
tr("The image has been modified.\n"
175
"Do you want to save your changes?"),
176
QMessageBox::Yes | QMessageBox::Default,
178
QMessageBox::Cancel | QMessageBox::Escape);
179
if (ret == QMessageBox::Yes) {
180
return saveFile("png");
181
} else if (ret == QMessageBox::Cancel) {
188
bool MainWindow::saveFile(const QByteArray &fileFormat)
190
QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
192
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
194
tr("%1 Files (*.%2);;All Files (*)")
195
.arg(QString(fileFormat.toUpper()))
196
.arg(QString(fileFormat)));
197
if (fileName.isEmpty()) {
200
return scribbleArea->saveImage(fileName, fileFormat);