~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/widgets/scribble/mainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "mainwindow.h"
 
32
#include "scribblearea.h"
 
33
 
 
34
MainWindow::MainWindow()
 
35
{
 
36
    scribbleArea = new ScribbleArea;
 
37
    setCentralWidget(scribbleArea);
 
38
 
 
39
    createActions();
 
40
    createMenus();
 
41
 
 
42
    setWindowTitle(tr("Scribble"));
 
43
    resize(500, 500);
 
44
}
 
45
 
 
46
void MainWindow::closeEvent(QCloseEvent *event)
 
47
{
 
48
    if (maybeSave()) {
 
49
        event->accept();
 
50
    } else {
 
51
        event->ignore();
 
52
    }
 
53
}
 
54
 
 
55
void MainWindow::open()
 
56
{
 
57
    if (maybeSave()) {
 
58
        QString fileName = QFileDialog::getOpenFileName(this,
 
59
                                   tr("Open File"), QDir::currentPath());
 
60
        if (!fileName.isEmpty())
 
61
            scribbleArea->openImage(fileName);
 
62
    }
 
63
}
 
64
 
 
65
void MainWindow::save()
 
66
{
 
67
    QAction *action = qobject_cast<QAction *>(sender());
 
68
    QByteArray fileFormat = action->data().toByteArray();
 
69
    saveFile(fileFormat);
 
70
}
 
71
 
 
72
void MainWindow::penColor()
 
73
{
 
74
    QColor newColor = QColorDialog::getColor(scribbleArea->penColor());
 
75
    if (newColor.isValid())
 
76
        scribbleArea->setPenColor(newColor);
 
77
}
 
78
 
 
79
void MainWindow::penWidth()
 
80
{
 
81
    bool ok;
 
82
    int newWidth = QInputDialog::getInteger(this, tr("Scribble"),
 
83
                                            tr("Select pen width:"),
 
84
                                            scribbleArea->penWidth(),
 
85
                                            1, 50, 1, &ok);
 
86
    if (ok)
 
87
        scribbleArea->setPenWidth(newWidth);
 
88
}
 
89
 
 
90
void MainWindow::about()
 
91
{
 
92
    QMessageBox::about(this, tr("About Scribble"),
 
93
            tr("<p>The <b>Scribble</b> example shows how to use QMainWindow as the "
 
94
               "base widget for an application, and how to reimplement some of "
 
95
               "QWidget's event handlers to receive the events generated for "
 
96
               "the application's widgets:</p><p> We reimplement the mouse event "
 
97
               "handlers to facilitate drawing, the paint event handler to "
 
98
               "update the application and the resize event handler to optimize "
 
99
               "the application's appearance. In addition we reimplement the "
 
100
               "close event handler to intercept the close events before "
 
101
               "terminating the application.</p><p> The example also demonstrates "
 
102
               "how to use QPainter to draw an image in real time, as well as "
 
103
               "to repaint widgets.</p>"));
 
104
}
 
105
 
 
106
void MainWindow::createActions()
 
107
{
 
108
    openAct = new QAction(tr("&Open..."), this);
 
109
    openAct->setShortcut(tr("Ctrl+O"));
 
110
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
111
 
 
112
    foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
 
113
        QString text = tr("%1...").arg(QString(format).toUpper());
 
114
 
 
115
        QAction *action = new QAction(text, this);
 
116
        action->setData(format);
 
117
        connect(action, SIGNAL(triggered()), this, SLOT(save()));
 
118
        saveAsActs.append(action);
 
119
    }
 
120
 
 
121
    exitAct = new QAction(tr("E&xit"), this);
 
122
    exitAct->setShortcut(tr("Ctrl+Q"));
 
123
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
124
 
 
125
    penColorAct = new QAction(tr("&Pen Color..."), this);
 
126
    connect(penColorAct, SIGNAL(triggered()), this, SLOT(penColor()));
 
127
 
 
128
    penWidthAct = new QAction(tr("Pen &Width..."), this);
 
129
    connect(penWidthAct, SIGNAL(triggered()), this, SLOT(penWidth()));
 
130
 
 
131
    clearScreenAct = new QAction(tr("&Clear Screen"), this);
 
132
    clearScreenAct->setShortcut(tr("Ctrl+L"));
 
133
    connect(clearScreenAct, SIGNAL(triggered()),
 
134
            scribbleArea, SLOT(clearImage()));
 
135
 
 
136
    aboutAct = new QAction(tr("&About"), this);
 
137
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
138
 
 
139
    aboutQtAct = new QAction(tr("About &Qt"), this);
 
140
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
141
}
 
142
 
 
143
void MainWindow::createMenus()
 
144
{
 
145
    saveAsMenu = new QMenu(tr("&Save As"), this);
 
146
    foreach (QAction *action, saveAsActs)
 
147
        saveAsMenu->addAction(action);
 
148
 
 
149
    fileMenu = new QMenu(tr("&File"), this);
 
150
    fileMenu->addAction(openAct);
 
151
    fileMenu->addMenu(saveAsMenu);
 
152
    fileMenu->addSeparator();
 
153
    fileMenu->addAction(exitAct);
 
154
 
 
155
    optionMenu = new QMenu(tr("&Options"), this);
 
156
    optionMenu->addAction(penColorAct);
 
157
    optionMenu->addAction(penWidthAct);
 
158
    optionMenu->addSeparator();
 
159
    optionMenu->addAction(clearScreenAct);
 
160
 
 
161
    helpMenu = new QMenu(tr("&Help"), this);
 
162
    helpMenu->addAction(aboutAct);
 
163
    helpMenu->addAction(aboutQtAct);
 
164
 
 
165
    menuBar()->addMenu(fileMenu);
 
166
    menuBar()->addMenu(optionMenu);
 
167
    menuBar()->addMenu(helpMenu);
 
168
}
 
169
 
 
170
bool MainWindow::maybeSave()
 
171
{
 
172
    if (scribbleArea->isModified()) {
 
173
        int ret = QMessageBox::warning(this, tr("Scribble"),
 
174
                          tr("The image has been modified.\n"
 
175
                             "Do you want to save your changes?"),
 
176
                          QMessageBox::Yes | QMessageBox::Default,
 
177
                          QMessageBox::No,
 
178
                          QMessageBox::Cancel | QMessageBox::Escape);
 
179
        if (ret == QMessageBox::Yes) {
 
180
            return saveFile("png");
 
181
        } else if (ret == QMessageBox::Cancel) {
 
182
            return false;
 
183
        }
 
184
    }
 
185
    return true;
 
186
}
 
187
 
 
188
bool MainWindow::saveFile(const QByteArray &fileFormat)
 
189
{
 
190
    QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
 
191
 
 
192
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
 
193
                               initialPath,
 
194
                               tr("%1 Files (*.%2);;All Files (*)")
 
195
                               .arg(QString(fileFormat.toUpper()))
 
196
                               .arg(QString(fileFormat)));
 
197
    if (fileName.isEmpty()) {
 
198
        return false;
 
199
    } else {
 
200
        return scribbleArea->saveImage(fileName, fileFormat);
 
201
    }
 
202
}