examples/touch/fingerpaint/mainwindow.cpp
changeset 37 758a864f9613
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    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.
       
    25 **
       
    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."
       
    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("Finger Paint"));
       
    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 //! [11]
       
    96 void MainWindow::about()
       
    97 //! [11] //! [12]
       
    98 {
       
    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>"));
       
   111 }
       
   112 //! [12]
       
   113 
       
   114 //! [13]
       
   115 void MainWindow::createActions()
       
   116 //! [13] //! [14]
       
   117 {
       
   118     openAct = new QAction(tr("&Open..."), this);
       
   119     openAct->setShortcut(tr("Ctrl+O"));
       
   120     connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
       
   121 
       
   122     foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
       
   123         QString text = tr("%1...").arg(QString(format).toUpper());
       
   124 
       
   125         QAction *action = new QAction(text, this);
       
   126         action->setData(format);
       
   127         connect(action, SIGNAL(triggered()), this, SLOT(save()));
       
   128         saveAsActs.append(action);
       
   129     }
       
   130 
       
   131     printAct = new QAction(tr("&Print..."), this);
       
   132     connect(printAct, SIGNAL(triggered()), scribbleArea, SLOT(print()));
       
   133 
       
   134     exitAct = new QAction(tr("E&xit"), this);
       
   135     exitAct->setShortcut(tr("Ctrl+Q"));
       
   136     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
       
   137 
       
   138     clearScreenAct = new QAction(tr("&Clear Screen"), this);
       
   139     clearScreenAct->setShortcut(tr("Ctrl+L"));
       
   140     connect(clearScreenAct, SIGNAL(triggered()),
       
   141             scribbleArea, SLOT(clearImage()));
       
   142 
       
   143     aboutAct = new QAction(tr("&About"), this);
       
   144     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
       
   145 
       
   146     aboutQtAct = new QAction(tr("About &Qt"), this);
       
   147     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
       
   148 }
       
   149 //! [14]
       
   150 
       
   151 //! [15]
       
   152 void MainWindow::createMenus()
       
   153 //! [15] //! [16]
       
   154 {
       
   155     saveAsMenu = new QMenu(tr("&Save As"), this);
       
   156     foreach (QAction *action, saveAsActs)
       
   157         saveAsMenu->addAction(action);
       
   158 
       
   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);
       
   165 
       
   166     optionMenu = new QMenu(tr("&Options"), this);
       
   167     optionMenu->addAction(clearScreenAct);
       
   168 
       
   169     helpMenu = new QMenu(tr("&Help"), this);
       
   170     helpMenu->addAction(aboutAct);
       
   171     helpMenu->addAction(aboutQtAct);
       
   172 
       
   173     menuBar()->addMenu(fileMenu);
       
   174     menuBar()->addMenu(optionMenu);
       
   175     menuBar()->addMenu(helpMenu);
       
   176 }
       
   177 //! [16]
       
   178 
       
   179 //! [17]
       
   180 bool MainWindow::maybeSave()
       
   181 //! [17] //! [18]
       
   182 {
       
   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) {
       
   193             return false;
       
   194         }
       
   195     }
       
   196     return true;
       
   197 }
       
   198 //! [18]
       
   199 
       
   200 //! [19]
       
   201 bool MainWindow::saveFile(const QByteArray &fileFormat)
       
   202 //! [19] //! [20]
       
   203 {
       
   204     QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
       
   205 
       
   206     QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
       
   207                                initialPath,
       
   208                                tr("%1 Files (*.%2);;All Files (*)")
       
   209                                .arg(QString(fileFormat.toUpper()))
       
   210                                .arg(QString(fileFormat)));
       
   211     if (fileName.isEmpty()) {
       
   212         return false;
       
   213     } else {
       
   214         return scribbleArea->saveImage(fileName, fileFormat);
       
   215     }
       
   216 }
       
   217 //! [20]