[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "mainwindow.h"
|
---|
| 44 | #include "scribblearea.h"
|
---|
| 45 |
|
---|
| 46 | //! [0]
|
---|
| 47 | MainWindow::MainWindow()
|
---|
| 48 | {
|
---|
| 49 | scribbleArea = new ScribbleArea;
|
---|
| 50 | setCentralWidget(scribbleArea);
|
---|
| 51 |
|
---|
| 52 | createActions();
|
---|
| 53 | createMenus();
|
---|
| 54 |
|
---|
| 55 | setWindowTitle(tr("Scribble"));
|
---|
| 56 | resize(500, 500);
|
---|
| 57 | }
|
---|
| 58 | //! [0]
|
---|
| 59 |
|
---|
| 60 | //! [1]
|
---|
| 61 | void MainWindow::closeEvent(QCloseEvent *event)
|
---|
| 62 | //! [1] //! [2]
|
---|
| 63 | {
|
---|
| 64 | if (maybeSave()) {
|
---|
| 65 | event->accept();
|
---|
| 66 | } else {
|
---|
| 67 | event->ignore();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | //! [2]
|
---|
| 71 |
|
---|
| 72 | //! [3]
|
---|
| 73 | void MainWindow::open()
|
---|
| 74 | //! [3] //! [4]
|
---|
| 75 | {
|
---|
| 76 | if (maybeSave()) {
|
---|
| 77 | QString fileName = QFileDialog::getOpenFileName(this,
|
---|
| 78 | tr("Open File"), QDir::currentPath());
|
---|
| 79 | if (!fileName.isEmpty())
|
---|
| 80 | scribbleArea->openImage(fileName);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | //! [4]
|
---|
| 84 |
|
---|
| 85 | //! [5]
|
---|
| 86 | void MainWindow::save()
|
---|
| 87 | //! [5] //! [6]
|
---|
| 88 | {
|
---|
| 89 | QAction *action = qobject_cast<QAction *>(sender());
|
---|
| 90 | QByteArray fileFormat = action->data().toByteArray();
|
---|
| 91 | saveFile(fileFormat);
|
---|
| 92 | }
|
---|
| 93 | //! [6]
|
---|
| 94 |
|
---|
| 95 | //! [7]
|
---|
| 96 | void MainWindow::penColor()
|
---|
| 97 | //! [7] //! [8]
|
---|
| 98 | {
|
---|
| 99 | QColor newColor = QColorDialog::getColor(scribbleArea->penColor());
|
---|
| 100 | if (newColor.isValid())
|
---|
| 101 | scribbleArea->setPenColor(newColor);
|
---|
| 102 | }
|
---|
| 103 | //! [8]
|
---|
| 104 |
|
---|
| 105 | //! [9]
|
---|
| 106 | void MainWindow::penWidth()
|
---|
| 107 | //! [9] //! [10]
|
---|
| 108 | {
|
---|
| 109 | bool ok;
|
---|
| 110 | int newWidth = QInputDialog::getInteger(this, tr("Scribble"),
|
---|
| 111 | tr("Select pen width:"),
|
---|
| 112 | scribbleArea->penWidth(),
|
---|
| 113 | 1, 50, 1, &ok);
|
---|
| 114 | if (ok)
|
---|
| 115 | scribbleArea->setPenWidth(newWidth);
|
---|
| 116 | }
|
---|
| 117 | //! [10]
|
---|
| 118 |
|
---|
| 119 | //! [11]
|
---|
| 120 | void MainWindow::about()
|
---|
| 121 | //! [11] //! [12]
|
---|
| 122 | {
|
---|
| 123 | QMessageBox::about(this, tr("About Scribble"),
|
---|
| 124 | tr("<p>The <b>Scribble</b> example shows how to use QMainWindow as the "
|
---|
| 125 | "base widget for an application, and how to reimplement some of "
|
---|
| 126 | "QWidget's event handlers to receive the events generated for "
|
---|
| 127 | "the application's widgets:</p><p> We reimplement the mouse event "
|
---|
| 128 | "handlers to facilitate drawing, the paint event handler to "
|
---|
| 129 | "update the application and the resize event handler to optimize "
|
---|
| 130 | "the application's appearance. In addition we reimplement the "
|
---|
| 131 | "close event handler to intercept the close events before "
|
---|
| 132 | "terminating the application.</p><p> The example also demonstrates "
|
---|
| 133 | "how to use QPainter to draw an image in real time, as well as "
|
---|
| 134 | "to repaint widgets.</p>"));
|
---|
| 135 | }
|
---|
| 136 | //! [12]
|
---|
| 137 |
|
---|
| 138 | //! [13]
|
---|
| 139 | void MainWindow::createActions()
|
---|
| 140 | //! [13] //! [14]
|
---|
| 141 | {
|
---|
| 142 | openAct = new QAction(tr("&Open..."), this);
|
---|
[561] | 143 | openAct->setShortcuts(QKeySequence::Open);
|
---|
[2] | 144 | connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
---|
| 145 |
|
---|
| 146 | foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
|
---|
| 147 | QString text = tr("%1...").arg(QString(format).toUpper());
|
---|
| 148 |
|
---|
| 149 | QAction *action = new QAction(text, this);
|
---|
| 150 | action->setData(format);
|
---|
| 151 | connect(action, SIGNAL(triggered()), this, SLOT(save()));
|
---|
| 152 | saveAsActs.append(action);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | printAct = new QAction(tr("&Print..."), this);
|
---|
| 156 | connect(printAct, SIGNAL(triggered()), scribbleArea, SLOT(print()));
|
---|
| 157 |
|
---|
| 158 | exitAct = new QAction(tr("E&xit"), this);
|
---|
[561] | 159 | exitAct->setShortcuts(QKeySequence::Quit);
|
---|
[2] | 160 | connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
|
---|
| 161 |
|
---|
| 162 | penColorAct = new QAction(tr("&Pen Color..."), this);
|
---|
| 163 | connect(penColorAct, SIGNAL(triggered()), this, SLOT(penColor()));
|
---|
| 164 |
|
---|
| 165 | penWidthAct = new QAction(tr("Pen &Width..."), this);
|
---|
| 166 | connect(penWidthAct, SIGNAL(triggered()), this, SLOT(penWidth()));
|
---|
| 167 |
|
---|
| 168 | clearScreenAct = new QAction(tr("&Clear Screen"), this);
|
---|
| 169 | clearScreenAct->setShortcut(tr("Ctrl+L"));
|
---|
| 170 | connect(clearScreenAct, SIGNAL(triggered()),
|
---|
| 171 | scribbleArea, SLOT(clearImage()));
|
---|
| 172 |
|
---|
| 173 | aboutAct = new QAction(tr("&About"), this);
|
---|
| 174 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
---|
| 175 |
|
---|
| 176 | aboutQtAct = new QAction(tr("About &Qt"), this);
|
---|
| 177 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
---|
| 178 | }
|
---|
| 179 | //! [14]
|
---|
| 180 |
|
---|
| 181 | //! [15]
|
---|
| 182 | void MainWindow::createMenus()
|
---|
| 183 | //! [15] //! [16]
|
---|
| 184 | {
|
---|
| 185 | saveAsMenu = new QMenu(tr("&Save As"), this);
|
---|
| 186 | foreach (QAction *action, saveAsActs)
|
---|
| 187 | saveAsMenu->addAction(action);
|
---|
| 188 |
|
---|
| 189 | fileMenu = new QMenu(tr("&File"), this);
|
---|
| 190 | fileMenu->addAction(openAct);
|
---|
| 191 | fileMenu->addMenu(saveAsMenu);
|
---|
| 192 | fileMenu->addAction(printAct);
|
---|
| 193 | fileMenu->addSeparator();
|
---|
| 194 | fileMenu->addAction(exitAct);
|
---|
| 195 |
|
---|
| 196 | optionMenu = new QMenu(tr("&Options"), this);
|
---|
| 197 | optionMenu->addAction(penColorAct);
|
---|
| 198 | optionMenu->addAction(penWidthAct);
|
---|
| 199 | optionMenu->addSeparator();
|
---|
| 200 | optionMenu->addAction(clearScreenAct);
|
---|
| 201 |
|
---|
| 202 | helpMenu = new QMenu(tr("&Help"), this);
|
---|
| 203 | helpMenu->addAction(aboutAct);
|
---|
| 204 | helpMenu->addAction(aboutQtAct);
|
---|
| 205 |
|
---|
| 206 | menuBar()->addMenu(fileMenu);
|
---|
| 207 | menuBar()->addMenu(optionMenu);
|
---|
| 208 | menuBar()->addMenu(helpMenu);
|
---|
| 209 | }
|
---|
| 210 | //! [16]
|
---|
| 211 |
|
---|
| 212 | //! [17]
|
---|
| 213 | bool MainWindow::maybeSave()
|
---|
| 214 | //! [17] //! [18]
|
---|
| 215 | {
|
---|
| 216 | if (scribbleArea->isModified()) {
|
---|
| 217 | QMessageBox::StandardButton ret;
|
---|
| 218 | ret = QMessageBox::warning(this, tr("Scribble"),
|
---|
| 219 | tr("The image has been modified.\n"
|
---|
| 220 | "Do you want to save your changes?"),
|
---|
| 221 | QMessageBox::Save | QMessageBox::Discard
|
---|
| 222 | | QMessageBox::Cancel);
|
---|
| 223 | if (ret == QMessageBox::Save) {
|
---|
| 224 | return saveFile("png");
|
---|
| 225 | } else if (ret == QMessageBox::Cancel) {
|
---|
| 226 | return false;
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | return true;
|
---|
| 230 | }
|
---|
| 231 | //! [18]
|
---|
| 232 |
|
---|
| 233 | //! [19]
|
---|
| 234 | bool MainWindow::saveFile(const QByteArray &fileFormat)
|
---|
| 235 | //! [19] //! [20]
|
---|
| 236 | {
|
---|
| 237 | QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
|
---|
| 238 |
|
---|
| 239 | QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
|
---|
| 240 | initialPath,
|
---|
| 241 | tr("%1 Files (*.%2);;All Files (*)")
|
---|
| 242 | .arg(QString(fileFormat.toUpper()))
|
---|
| 243 | .arg(QString(fileFormat)));
|
---|
| 244 | if (fileName.isEmpty()) {
|
---|
| 245 | return false;
|
---|
| 246 | } else {
|
---|
| 247 | return scribbleArea->saveImage(fileName, fileFormat);
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | //! [20]
|
---|