~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/touch/fingerpaint/mainwindow.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
14
**   * Redistributions of source code must retain the above copyright
 
15
**     notice, this list of conditions and the following disclaimer.
 
16
**   * Redistributions in binary form must reproduce the above copyright
 
17
**     notice, this list of conditions and the following disclaimer in
 
18
**     the documentation and/or other materials provided with the
 
19
**     distribution.
 
20
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its contributors may be used to endorse or promote products derived
 
22
**     from this software without specific prior written permission.
 
23
**
 
24
**
 
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include <QtWidgets>
 
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::fromLatin1(fileFormat.toUpper()))
 
210
                               .arg(QString::fromLatin1(fileFormat)));
 
211
    if (fileName.isEmpty()) {
 
212
        return false;
 
213
    } else {
 
214
        return scribbleArea->saveImage(fileName, fileFormat.constData());
 
215
    }
 
216
}
 
217
//! [20]