source: trunk/examples/gestures/imagegestures/imagewidget.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "imagewidget.h"
43
44#include <QtGui>
45
46//! [constructor]
47ImageWidget::ImageWidget(QWidget *parent)
48 : QWidget(parent),
49 position(0),
50 horizontalOffset(0),
51 verticalOffset(0),
52 rotationAngle(0),
53 scaleFactor(1),
54 currentStepScaleFactor(1)
55
56{
57 setMinimumSize(QSize(100,100));
58
59//! [enable gestures]
60 grabGesture(Qt::PanGesture);
61 grabGesture(Qt::PinchGesture);
62 grabGesture(Qt::SwipeGesture);
63//! [enable gestures]
64}
65//! [constructor]
66
67//! [event handler]
68bool ImageWidget::event(QEvent *event)
69{
70 if (event->type() == QEvent::Gesture)
71 return gestureEvent(static_cast<QGestureEvent*>(event));
72 return QWidget::event(event);
73}
74//! [event handler]
75
76void ImageWidget::paintEvent(QPaintEvent*)
77{
78 QPainter p(this);
79
80 float iw = currentImage.width();
81 float ih = currentImage.height();
82 float wh = height();
83 float ww = width();
84
85 p.translate(ww/2, wh/2);
86 p.translate(horizontalOffset, verticalOffset);
87 p.rotate(rotationAngle);
88 p.scale(currentStepScaleFactor * scaleFactor, currentStepScaleFactor * scaleFactor);
89 p.translate(-iw/2, -ih/2);
90 p.drawImage(0, 0, currentImage);
91}
92
93void ImageWidget::mouseDoubleClickEvent(QMouseEvent *)
94{
95 rotationAngle = 0;
96 scaleFactor = 1;
97 currentStepScaleFactor = 1;
98 verticalOffset = 0;
99 horizontalOffset = 0;
100 update();
101}
102
103//! [gesture event handler]
104bool ImageWidget::gestureEvent(QGestureEvent *event)
105{
106 if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
107 swipeTriggered(static_cast<QSwipeGesture *>(swipe));
108 else if (QGesture *pan = event->gesture(Qt::PanGesture))
109 panTriggered(static_cast<QPanGesture *>(pan));
110 if (QGesture *pinch = event->gesture(Qt::PinchGesture))
111 pinchTriggered(static_cast<QPinchGesture *>(pinch));
112 return true;
113}
114//! [gesture event handler]
115
116void ImageWidget::panTriggered(QPanGesture *gesture)
117{
118#ifndef QT_NO_CURSOR
119 switch (gesture->state()) {
120 case Qt::GestureStarted:
121 case Qt::GestureUpdated:
122 setCursor(Qt::SizeAllCursor);
123 break;
124 default:
125 setCursor(Qt::ArrowCursor);
126 }
127#endif
128 QPointF delta = gesture->delta();
129 horizontalOffset += delta.x();
130 verticalOffset += delta.y();
131 update();
132}
133
134void ImageWidget::pinchTriggered(QPinchGesture *gesture)
135{
136 QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
137 if (changeFlags & QPinchGesture::RotationAngleChanged) {
138 qreal value = gesture->property("rotationAngle").toReal();
139 qreal lastValue = gesture->property("lastRotationAngle").toReal();
140 rotationAngle += value - lastValue;
141 }
142 if (changeFlags & QPinchGesture::ScaleFactorChanged) {
143 qreal value = gesture->property("scaleFactor").toReal();
144 currentStepScaleFactor = value;
145 }
146 if (gesture->state() == Qt::GestureFinished) {
147 scaleFactor *= currentStepScaleFactor;
148 currentStepScaleFactor = 1;
149 }
150 update();
151}
152
153//! [swipe function]
154void ImageWidget::swipeTriggered(QSwipeGesture *gesture)
155{
156 if (gesture->state() == Qt::GestureFinished) {
157 if (gesture->horizontalDirection() == QSwipeGesture::Left
158 || gesture->verticalDirection() == QSwipeGesture::Up)
159 goPrevImage();
160 else
161 goNextImage();
162 update();
163 }
164}
165//! [swipe function]
166
167void ImageWidget::resizeEvent(QResizeEvent*)
168{
169 update();
170}
171
172void ImageWidget::openDirectory(const QString &path)
173{
174 this->path = path;
175 QDir dir(path);
176 QStringList nameFilters;
177 nameFilters << "*.jpg" << "*.png";
178 files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
179
180 position = 0;
181 goToImage(0);
182 update();
183}
184
185QImage ImageWidget::loadImage(const QString &fileName)
186{
187 QImageReader reader(fileName);
188 if (!reader.canRead()) {
189 qDebug() << fileName << ": can't load image";
190 return QImage();
191 }
192
193 QImage image;
194 if (!reader.read(&image)) {
195 qDebug() << fileName << ": corrupted image";
196 return QImage();
197 }
198 return image;
199}
200
201void ImageWidget::goNextImage()
202{
203 if (files.isEmpty())
204 return;
205
206 if (position < files.size()-1) {
207 ++position;
208 prevImage = currentImage;
209 currentImage = nextImage;
210 if (position+1 < files.size())
211 nextImage = loadImage(path+QLatin1String("/")+files.at(position+1));
212 else
213 nextImage = QImage();
214 }
215 update();
216}
217
218void ImageWidget::goPrevImage()
219{
220 if (files.isEmpty())
221 return;
222
223 if (position > 0) {
224 --position;
225 nextImage = currentImage;
226 currentImage = prevImage;
227 if (position > 0)
228 prevImage = loadImage(path+QLatin1String("/")+files.at(position-1));
229 else
230 prevImage = QImage();
231 }
232 update();
233}
234
235void ImageWidget::goToImage(int index)
236{
237 if (files.isEmpty())
238 return;
239
240 if (index < 0 || index >= files.size()) {
241 qDebug() << "goToImage: invalid index: " << index;
242 return;
243 }
244
245 if (index == position+1) {
246 goNextImage();
247 return;
248 }
249
250 if (position > 0 && index == position-1) {
251 goPrevImage();
252 return;
253 }
254
255 position = index;
256
257 if (index > 0)
258 prevImage = loadImage(path+QLatin1String("/")+files.at(position-1));
259 else
260 prevImage = QImage();
261 currentImage = loadImage(path+QLatin1String("/")+files.at(position));
262 if (position+1 < files.size())
263 nextImage = loadImage(path+QLatin1String("/")+files.at(position+1));
264 else
265 nextImage = QImage();
266 update();
267}
Note: See TracBrowser for help on using the repository browser.