1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
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 "tabletcanvas.h"
|
---|
45 |
|
---|
46 | //! [0]
|
---|
47 | MainWindow::MainWindow(TabletCanvas *canvas)
|
---|
48 | {
|
---|
49 | myCanvas = canvas;
|
---|
50 | createActions();
|
---|
51 | createMenus();
|
---|
52 |
|
---|
53 | myCanvas->setColor(Qt::red);
|
---|
54 | myCanvas->setLineWidthType(TabletCanvas::LineWidthPressure);
|
---|
55 | myCanvas->setAlphaChannelType(TabletCanvas::NoAlpha);
|
---|
56 | myCanvas->setColorSaturationType(TabletCanvas::NoSaturation);
|
---|
57 |
|
---|
58 | setWindowTitle(tr("Tablet Example"));
|
---|
59 | setCentralWidget(myCanvas);
|
---|
60 | }
|
---|
61 | //! [0]
|
---|
62 |
|
---|
63 | //! [1]
|
---|
64 | void MainWindow::brushColorAct()
|
---|
65 | {
|
---|
66 | QColor color = QColorDialog::getColor(myCanvas->color());
|
---|
67 |
|
---|
68 | if (color.isValid())
|
---|
69 | myCanvas->setColor(color);
|
---|
70 | }
|
---|
71 | //! [1]
|
---|
72 |
|
---|
73 | //! [2]
|
---|
74 | void MainWindow::alphaActionTriggered(QAction *action)
|
---|
75 | {
|
---|
76 | if (action == alphaChannelPressureAction) {
|
---|
77 | myCanvas->setAlphaChannelType(TabletCanvas::AlphaPressure);
|
---|
78 | } else if (action == alphaChannelTiltAction) {
|
---|
79 | myCanvas->setAlphaChannelType(TabletCanvas::AlphaTilt);
|
---|
80 | } else {
|
---|
81 | myCanvas->setAlphaChannelType(TabletCanvas::NoAlpha);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | //! [2]
|
---|
85 |
|
---|
86 | //! [3]
|
---|
87 | void MainWindow::lineWidthActionTriggered(QAction *action)
|
---|
88 | {
|
---|
89 | if (action == lineWidthPressureAction) {
|
---|
90 | myCanvas->setLineWidthType(TabletCanvas::LineWidthPressure);
|
---|
91 | } else if (action == lineWidthTiltAction) {
|
---|
92 | myCanvas->setLineWidthType(TabletCanvas::LineWidthTilt);
|
---|
93 | } else {
|
---|
94 | myCanvas->setLineWidthType(TabletCanvas::NoLineWidth);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | //! [3]
|
---|
98 |
|
---|
99 | //! [4]
|
---|
100 | void MainWindow::saturationActionTriggered(QAction *action)
|
---|
101 | {
|
---|
102 | if (action == colorSaturationVTiltAction) {
|
---|
103 | myCanvas->setColorSaturationType(TabletCanvas::SaturationVTilt);
|
---|
104 | } else if (action == colorSaturationHTiltAction) {
|
---|
105 | myCanvas->setColorSaturationType(TabletCanvas::SaturationHTilt);
|
---|
106 | } else if (action == colorSaturationPressureAction) {
|
---|
107 | myCanvas->setColorSaturationType(TabletCanvas::SaturationPressure);
|
---|
108 | } else {
|
---|
109 | myCanvas->setColorSaturationType(TabletCanvas::NoSaturation);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | //! [4]
|
---|
113 |
|
---|
114 | //! [5]
|
---|
115 | void MainWindow::saveAct()
|
---|
116 | {
|
---|
117 | QString path = QDir::currentPath() + "/untitled.png";
|
---|
118 | QString fileName = QFileDialog::getSaveFileName(this, tr("Save Picture"),
|
---|
119 | path);
|
---|
120 |
|
---|
121 | if (!myCanvas->saveImage(fileName))
|
---|
122 | QMessageBox::information(this, "Error Saving Picture",
|
---|
123 | "Could not save the image");
|
---|
124 | }
|
---|
125 | //! [5]
|
---|
126 |
|
---|
127 | //! [6]
|
---|
128 | void MainWindow::loadAct()
|
---|
129 | {
|
---|
130 | QString fileName = QFileDialog::getOpenFileName(this, tr("Open Picture"),
|
---|
131 | QDir::currentPath());
|
---|
132 |
|
---|
133 | if (!myCanvas->loadImage(fileName))
|
---|
134 | QMessageBox::information(this, "Error Opening Picture",
|
---|
135 | "Could not open picture");
|
---|
136 | }
|
---|
137 | //! [6]
|
---|
138 |
|
---|
139 | //! [7]
|
---|
140 | void MainWindow::aboutAct()
|
---|
141 | {
|
---|
142 | QMessageBox::about(this, tr("About Tablet Example"),
|
---|
143 | tr("This example shows use of a Wacom tablet in Qt"));
|
---|
144 | }
|
---|
145 | //! [7]
|
---|
146 |
|
---|
147 | //! [8]
|
---|
148 | void MainWindow::createActions()
|
---|
149 | {
|
---|
150 | //! [8]
|
---|
151 | brushColorAction = new QAction(tr("&Brush Color..."), this);
|
---|
152 | brushColorAction->setShortcut(tr("Ctrl+C"));
|
---|
153 | connect(brushColorAction, SIGNAL(triggered()),
|
---|
154 | this, SLOT(brushColorAct()));
|
---|
155 |
|
---|
156 | //! [9]
|
---|
157 | alphaChannelPressureAction = new QAction(tr("&Pressure"), this);
|
---|
158 | alphaChannelPressureAction->setCheckable(true);
|
---|
159 |
|
---|
160 | alphaChannelTiltAction = new QAction(tr("&Tilt"), this);
|
---|
161 | alphaChannelTiltAction->setCheckable(true);
|
---|
162 |
|
---|
163 | noAlphaChannelAction = new QAction(tr("No Alpha Channel"), this);
|
---|
164 | noAlphaChannelAction->setCheckable(true);
|
---|
165 | noAlphaChannelAction->setChecked(true);
|
---|
166 |
|
---|
167 | alphaChannelGroup = new QActionGroup(this);
|
---|
168 | alphaChannelGroup->addAction(alphaChannelPressureAction);
|
---|
169 | alphaChannelGroup->addAction(alphaChannelTiltAction);
|
---|
170 | alphaChannelGroup->addAction(noAlphaChannelAction);
|
---|
171 | connect(alphaChannelGroup, SIGNAL(triggered(QAction*)),
|
---|
172 | this, SLOT(alphaActionTriggered(QAction*)));
|
---|
173 |
|
---|
174 | //! [9]
|
---|
175 | colorSaturationVTiltAction = new QAction(tr("&Vertical Tilt"), this);
|
---|
176 | colorSaturationVTiltAction->setCheckable(true);
|
---|
177 |
|
---|
178 | colorSaturationHTiltAction = new QAction(tr("&Horizontal Tilt"), this);
|
---|
179 | colorSaturationHTiltAction->setCheckable(true);
|
---|
180 |
|
---|
181 | colorSaturationPressureAction = new QAction(tr("&Pressure"), this);
|
---|
182 | colorSaturationPressureAction->setCheckable(true);
|
---|
183 |
|
---|
184 | noColorSaturationAction = new QAction(tr("&No Color Saturation"), this);
|
---|
185 | noColorSaturationAction->setCheckable(true);
|
---|
186 | noColorSaturationAction->setChecked(true);
|
---|
187 |
|
---|
188 | colorSaturationGroup = new QActionGroup(this);
|
---|
189 | colorSaturationGroup->addAction(colorSaturationVTiltAction);
|
---|
190 | colorSaturationGroup->addAction(colorSaturationHTiltAction);
|
---|
191 | colorSaturationGroup->addAction(colorSaturationPressureAction);
|
---|
192 | colorSaturationGroup->addAction(noColorSaturationAction);
|
---|
193 | connect(colorSaturationGroup, SIGNAL(triggered(QAction*)),
|
---|
194 | this, SLOT(saturationActionTriggered(QAction*)));
|
---|
195 |
|
---|
196 | lineWidthPressureAction = new QAction(tr("&Pressure"), this);
|
---|
197 | lineWidthPressureAction->setCheckable(true);
|
---|
198 | lineWidthPressureAction->setChecked(true);
|
---|
199 |
|
---|
200 | lineWidthTiltAction = new QAction(tr("&Tilt"), this);
|
---|
201 | lineWidthTiltAction->setCheckable(true);
|
---|
202 |
|
---|
203 | lineWidthFixedAction = new QAction(tr("&Fixed"), this);
|
---|
204 | lineWidthFixedAction->setCheckable(true);
|
---|
205 |
|
---|
206 | lineWidthGroup = new QActionGroup(this);
|
---|
207 | lineWidthGroup->addAction(lineWidthPressureAction);
|
---|
208 | lineWidthGroup->addAction(lineWidthTiltAction);
|
---|
209 | lineWidthGroup->addAction(lineWidthFixedAction);
|
---|
210 | connect(lineWidthGroup, SIGNAL(triggered(QAction*)),
|
---|
211 | this, SLOT(lineWidthActionTriggered(QAction*)));
|
---|
212 |
|
---|
213 | exitAction = new QAction(tr("E&xit"), this);
|
---|
214 | exitAction->setShortcuts(QKeySequence::Quit);
|
---|
215 | connect(exitAction, SIGNAL(triggered()),
|
---|
216 | this, SLOT(close()));
|
---|
217 |
|
---|
218 | loadAction = new QAction(tr("&Open..."), this);
|
---|
219 | loadAction->setShortcuts(QKeySequence::Open);
|
---|
220 | connect(loadAction, SIGNAL(triggered()),
|
---|
221 | this, SLOT(loadAct()));
|
---|
222 |
|
---|
223 | saveAction = new QAction(tr("&Save As..."), this);
|
---|
224 | saveAction->setShortcuts(QKeySequence::SaveAs);
|
---|
225 | connect(saveAction, SIGNAL(triggered()),
|
---|
226 | this, SLOT(saveAct()));
|
---|
227 |
|
---|
228 | aboutAction = new QAction(tr("A&bout"), this);
|
---|
229 | aboutAction->setShortcut(tr("Ctrl+B"));
|
---|
230 | connect(aboutAction, SIGNAL(triggered()),
|
---|
231 | this, SLOT(aboutAct()));
|
---|
232 |
|
---|
233 | aboutQtAction = new QAction(tr("About &Qt"), this);
|
---|
234 | aboutQtAction->setShortcut(tr("Ctrl+Q"));
|
---|
235 | connect(aboutQtAction, SIGNAL(triggered()),
|
---|
236 | qApp, SLOT(aboutQt()));
|
---|
237 | //! [10]
|
---|
238 | }
|
---|
239 | //! [10]
|
---|
240 |
|
---|
241 | //! [11]
|
---|
242 | void MainWindow::createMenus()
|
---|
243 | {
|
---|
244 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
245 | fileMenu->addAction(loadAction);
|
---|
246 | fileMenu->addAction(saveAction);
|
---|
247 | fileMenu->addSeparator();
|
---|
248 | fileMenu->addAction(exitAction);
|
---|
249 |
|
---|
250 | brushMenu = menuBar()->addMenu(tr("&Brush"));
|
---|
251 | brushMenu->addAction(brushColorAction);
|
---|
252 |
|
---|
253 | tabletMenu = menuBar()->addMenu(tr("&Tablet"));
|
---|
254 |
|
---|
255 | lineWidthMenu = tabletMenu->addMenu(tr("&Line Width"));
|
---|
256 | lineWidthMenu->addAction(lineWidthPressureAction);
|
---|
257 | lineWidthMenu->addAction(lineWidthTiltAction);
|
---|
258 | lineWidthMenu->addAction(lineWidthFixedAction);
|
---|
259 |
|
---|
260 | alphaChannelMenu = tabletMenu->addMenu(tr("&Alpha Channel"));
|
---|
261 | alphaChannelMenu->addAction(alphaChannelPressureAction);
|
---|
262 | alphaChannelMenu->addAction(alphaChannelTiltAction);
|
---|
263 | alphaChannelMenu->addAction(noAlphaChannelAction);
|
---|
264 |
|
---|
265 | colorSaturationMenu = tabletMenu->addMenu(tr("&Color Saturation"));
|
---|
266 | colorSaturationMenu->addAction(colorSaturationVTiltAction);
|
---|
267 | colorSaturationMenu->addAction(colorSaturationHTiltAction);
|
---|
268 | colorSaturationMenu->addAction(noColorSaturationAction);
|
---|
269 |
|
---|
270 | helpMenu = menuBar()->addMenu("&Help");
|
---|
271 | helpMenu->addAction(aboutAction);
|
---|
272 | helpMenu->addAction(aboutQtAction);
|
---|
273 | }
|
---|
274 | //! [11]
|
---|