examples/widgets/scribble/mainwindow.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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->setShortcuts(QKeySequence::Open);
       
   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->setShortcuts(QKeySequence::Quit);
       
   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]