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