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

« back to all changes in this revision

Viewing changes to examples/tools/plugandpaint/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 "interfaces.h"
 
32
#include "mainwindow.h"
 
33
#include "paintarea.h"
 
34
#include "plugindialog.h"
 
35
 
 
36
MainWindow::MainWindow()
 
37
{
 
38
    paintArea = new PaintArea;
 
39
 
 
40
    scrollArea = new QScrollArea;
 
41
    scrollArea->setBackgroundRole(QPalette::Dark);
 
42
    scrollArea->setWidget(paintArea);
 
43
    setCentralWidget(scrollArea);
 
44
 
 
45
    createActions();
 
46
    createMenus();
 
47
    loadPlugins();
 
48
 
 
49
    setWindowTitle(tr("Plug & Paint"));
 
50
 
 
51
    if (!brushActionGroup->actions().isEmpty())
 
52
        brushActionGroup->actions().first()->trigger();
 
53
 
 
54
    QTimer::singleShot(500, this, SLOT(aboutPlugins()));
 
55
}
 
56
 
 
57
void MainWindow::open()
 
58
{
 
59
    QString fileName = QFileDialog::getOpenFileName(this,
 
60
                                    tr("Open File"), QDir::currentPath());
 
61
    if (!fileName.isEmpty()) {
 
62
        if (!paintArea->openImage(fileName)) {
 
63
            QMessageBox::information(this, tr("Plug & Paint"),
 
64
                                     tr("Cannot load %1.").arg(fileName));
 
65
            return;
 
66
        }
 
67
        paintArea->adjustSize();
 
68
    }
 
69
}
 
70
 
 
71
bool MainWindow::saveAs()
 
72
{
 
73
    QString initialPath = QDir::currentPath() + "/untitled.png";
 
74
 
 
75
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
 
76
                                                    initialPath);
 
77
    if (fileName.isEmpty()) {
 
78
        return false;
 
79
    } else {
 
80
        return paintArea->saveImage(fileName, "png");
 
81
    }
 
82
}
 
83
 
 
84
void MainWindow::brushColor()
 
85
{
 
86
    QColor newColor = QColorDialog::getColor(paintArea->brushColor());
 
87
    if (newColor.isValid())
 
88
        paintArea->setBrushColor(newColor);
 
89
}
 
90
 
 
91
void MainWindow::brushWidth()
 
92
{
 
93
    bool ok;
 
94
    int newWidth = QInputDialog::getInteger(this, tr("Plug & Paint"),
 
95
                                            tr("Select brush width:"),
 
96
                                            paintArea->brushWidth(),
 
97
                                            1, 50, 1, &ok);
 
98
    if (ok)
 
99
        paintArea->setBrushWidth(newWidth);
 
100
}
 
101
 
 
102
void MainWindow::changeBrush()
 
103
{
 
104
    QAction *action = qobject_cast<QAction *>(sender());
 
105
    BrushInterface *iBrush = qobject_cast<BrushInterface *>(action->parent());
 
106
    QString brush = action->text();
 
107
 
 
108
    paintArea->setBrush(iBrush, brush);
 
109
}
 
110
 
 
111
void MainWindow::insertShape()
 
112
{
 
113
    QAction *action = qobject_cast<QAction *>(sender());
 
114
    ShapeInterface *iShape = qobject_cast<ShapeInterface *>(action->parent());
 
115
 
 
116
    QPainterPath path = iShape->generateShape(action->text(), this);
 
117
    if (!path.isEmpty())
 
118
        paintArea->insertShape(path);
 
119
}
 
120
 
 
121
void MainWindow::applyFilter()
 
122
{
 
123
    QAction *action = qobject_cast<QAction *>(sender());
 
124
    FilterInterface *iFilter =
 
125
            qobject_cast<FilterInterface *>(action->parent());
 
126
 
 
127
    QImage image = iFilter->filterImage(action->text(), paintArea->image(),
 
128
                                        this);
 
129
    paintArea->setImage(image);
 
130
}
 
131
 
 
132
void MainWindow::about()
 
133
{
 
134
   QMessageBox::about(this, tr("About Plug & Paint"),
 
135
            tr("The <b>Plug & Paint</b> example demonstrates how to write Qt "
 
136
               "applications that can be extended through plugins."));
 
137
}
 
138
 
 
139
void MainWindow::aboutPlugins()
 
140
{
 
141
    PluginDialog dialog(pluginsDir.path(), pluginFileNames, this);
 
142
    dialog.exec();
 
143
}
 
144
 
 
145
void MainWindow::createActions()
 
146
{
 
147
    openAct = new QAction(tr("&Open..."), this);
 
148
    openAct->setShortcut(tr("Ctrl+O"));
 
149
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
150
 
 
151
    saveAsAct = new QAction(tr("&Save As..."), this);
 
152
    saveAsAct->setShortcut(tr("Ctrl+S"));
 
153
    connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
 
154
 
 
155
    exitAct = new QAction(tr("E&xit"), this);
 
156
    exitAct->setShortcut(tr("Ctrl+Q"));
 
157
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
158
 
 
159
    brushColorAct = new QAction(tr("&Brush Color..."), this);
 
160
    connect(brushColorAct, SIGNAL(triggered()), this, SLOT(brushColor()));
 
161
 
 
162
    brushWidthAct = new QAction(tr("&Brush Width..."), this);
 
163
    connect(brushWidthAct, SIGNAL(triggered()), this, SLOT(brushWidth()));
 
164
 
 
165
    brushActionGroup = new QActionGroup(this);
 
166
 
 
167
    aboutAct = new QAction(tr("&About"), this);
 
168
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
169
 
 
170
    aboutQtAct = new QAction(tr("About &Qt"), this);
 
171
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
172
 
 
173
    aboutPluginsAct = new QAction(tr("About &Plugins"), this);
 
174
    connect(aboutPluginsAct, SIGNAL(triggered()), this, SLOT(aboutPlugins()));
 
175
}
 
176
 
 
177
void MainWindow::createMenus()
 
178
{
 
179
    fileMenu = menuBar()->addMenu(tr("&File"));
 
180
    fileMenu->addAction(openAct);
 
181
    fileMenu->addAction(saveAsAct);
 
182
    fileMenu->addSeparator();
 
183
    fileMenu->addAction(exitAct);
 
184
 
 
185
    brushMenu = menuBar()->addMenu(tr("&Brush"));
 
186
    brushMenu->addAction(brushColorAct);
 
187
    brushMenu->addAction(brushWidthAct);
 
188
    brushMenu->addSeparator();
 
189
 
 
190
    shapesMenu = menuBar()->addMenu(tr("&Shapes"));
 
191
 
 
192
    filterMenu = menuBar()->addMenu(tr("&Filter"));
 
193
 
 
194
    menuBar()->addSeparator();
 
195
 
 
196
    helpMenu = menuBar()->addMenu(tr("&Help"));
 
197
    helpMenu->addAction(aboutAct);
 
198
    helpMenu->addAction(aboutQtAct);
 
199
    helpMenu->addAction(aboutPluginsAct);
 
200
}
 
201
 
 
202
void MainWindow::loadPlugins()
 
203
{
 
204
    pluginsDir = QDir(qApp->applicationDirPath());
 
205
#if defined(Q_OS_WIN)
 
206
    if (pluginsDir.dirName() == "debug" || pluginsDir.dirName() == "release")
 
207
        pluginsDir.cdUp();
 
208
#elif defined(Q_OS_MAC)
 
209
    if (pluginsDir.dirName() == "MacOS") {
 
210
        pluginsDir.cdUp();
 
211
        pluginsDir.cdUp();
 
212
        pluginsDir.cdUp();
 
213
    }
 
214
#endif
 
215
    pluginsDir.cd("plugins");
 
216
 
 
217
    foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
 
218
        QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
 
219
        QObject *plugin = loader.instance();
 
220
        if (plugin) {
 
221
            BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin);
 
222
            if (iBrush)
 
223
                addToMenu(plugin, iBrush->brushes(), brushMenu,
 
224
                          SLOT(changeBrush()), brushActionGroup);
 
225
 
 
226
            ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin);
 
227
            if (iShape)
 
228
                addToMenu(plugin, iShape->shapes(), shapesMenu,
 
229
                          SLOT(insertShape()));
 
230
 
 
231
            FilterInterface *iFilter = qobject_cast<FilterInterface *>(plugin);
 
232
            if (iFilter)
 
233
                addToMenu(plugin, iFilter->filters(), filterMenu,
 
234
                          SLOT(applyFilter()));
 
235
 
 
236
            pluginFileNames += fileName;
 
237
        }
 
238
    }
 
239
 
 
240
    brushMenu->setEnabled(!brushActionGroup->actions().isEmpty());
 
241
    shapesMenu->setEnabled(!shapesMenu->actions().isEmpty());
 
242
    filterMenu->setEnabled(!filterMenu->actions().isEmpty());
 
243
}
 
244
 
 
245
void MainWindow::addToMenu(QObject *plugin, const QStringList &texts,
 
246
                           QMenu *menu, const char *member,
 
247
                           QActionGroup *actionGroup)
 
248
{
 
249
    foreach (QString text, texts) {
 
250
        QAction *action = new QAction(text, plugin);
 
251
        connect(action, SIGNAL(triggered()), this, member);
 
252
        menu->addAction(action);
 
253
 
 
254
        if (actionGroup) {
 
255
            action->setCheckable(true);
 
256
            actionGroup->addAction(action);
 
257
        }
 
258
    }
 
259
}