0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 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 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
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 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "imagewidget.h"
|
|
43 |
|
|
44 |
#include <QtGui>
|
|
45 |
|
|
46 |
//! [constructor]
|
|
47 |
ImageWidget::ImageWidget(QWidget *parent)
|
|
48 |
: QWidget(parent),
|
|
49 |
position(0),
|
|
50 |
horizontalOffset(0),
|
|
51 |
verticalOffset(0),
|
|
52 |
rotationAngle(0),
|
|
53 |
scaleFactor(1)
|
|
54 |
|
|
55 |
{
|
|
56 |
setMinimumSize(QSize(100,100));
|
|
57 |
|
|
58 |
//! [enable gestures]
|
|
59 |
grabGesture(Qt::PanGesture);
|
|
60 |
grabGesture(Qt::PinchGesture);
|
|
61 |
grabGesture(Qt::SwipeGesture);
|
|
62 |
//! [enable gestures]
|
|
63 |
}
|
|
64 |
//! [constructor]
|
|
65 |
|
|
66 |
//! [event handler]
|
|
67 |
bool ImageWidget::event(QEvent *event)
|
|
68 |
{
|
|
69 |
if (event->type() == QEvent::Gesture)
|
|
70 |
return gestureEvent(static_cast<QGestureEvent*>(event));
|
|
71 |
return QWidget::event(event);
|
|
72 |
}
|
|
73 |
//! [event handler]
|
|
74 |
|
|
75 |
void ImageWidget::paintEvent(QPaintEvent*)
|
|
76 |
{
|
|
77 |
QPainter p(this);
|
|
78 |
p.fillRect(rect(), Qt::white);
|
|
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(scaleFactor, scaleFactor);
|
|
89 |
p.translate(-iw/2, -ih/2);
|
|
90 |
p.drawImage(0, 0, currentImage);
|
|
91 |
}
|
|
92 |
|
|
93 |
void ImageWidget::mouseDoubleClickEvent(QMouseEvent *)
|
|
94 |
{
|
|
95 |
rotationAngle = 0;
|
|
96 |
scaleFactor = 1;
|
|
97 |
verticalOffset = 0;
|
|
98 |
horizontalOffset = 0;
|
|
99 |
update();
|
|
100 |
}
|
|
101 |
|
|
102 |
//! [gesture event handler]
|
|
103 |
bool ImageWidget::gestureEvent(QGestureEvent *event)
|
|
104 |
{
|
|
105 |
if (QGesture *pan = event->gesture(Qt::PanGesture)) {
|
|
106 |
panTriggered(static_cast<QPanGesture*>(pan));
|
|
107 |
return true;
|
|
108 |
} else if (QGesture *pinch = event->gesture(Qt::PinchGesture)) {
|
|
109 |
pinchTriggered(static_cast<QPinchGesture*>(pinch));
|
|
110 |
return true;
|
|
111 |
} else if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) {
|
|
112 |
swipeTriggered(static_cast<QSwipeGesture*>(swipe));
|
|
113 |
return true;
|
|
114 |
}
|
|
115 |
return false;
|
|
116 |
}
|
|
117 |
//! [gesture event handler]
|
|
118 |
|
|
119 |
void ImageWidget::panTriggered(QPanGesture *gesture)
|
|
120 |
{
|
|
121 |
#ifndef QT_NO_CURSOR
|
|
122 |
switch (gesture->state()) {
|
|
123 |
case Qt::GestureStarted:
|
|
124 |
case Qt::GestureUpdated:
|
|
125 |
setCursor(Qt::SizeAllCursor);
|
|
126 |
break;
|
|
127 |
default:
|
|
128 |
setCursor(Qt::ArrowCursor);
|
|
129 |
}
|
|
130 |
#endif
|
|
131 |
QPointF lastOffset = gesture->offset();
|
|
132 |
horizontalOffset += lastOffset.x();
|
|
133 |
verticalOffset += lastOffset.y();
|
|
134 |
update();
|
|
135 |
}
|
|
136 |
|
|
137 |
void ImageWidget::pinchTriggered(QPinchGesture *gesture)
|
|
138 |
{
|
|
139 |
QPinchGesture::WhatChanged whatChanged = gesture->whatChanged();
|
|
140 |
if (whatChanged & QPinchGesture::RotationAngleChanged) {
|
|
141 |
qreal value = gesture->property("rotationAngle").toReal();
|
|
142 |
qreal lastValue = gesture->property("lastRotationAngle").toReal();
|
|
143 |
rotationAngle += value - lastValue;
|
|
144 |
}
|
|
145 |
if (whatChanged & QPinchGesture::ScaleFactorChanged) {
|
|
146 |
qreal value = gesture->property("scaleFactor").toReal();
|
|
147 |
qreal lastValue = gesture->property("lastScaleFactor").toReal();
|
|
148 |
scaleFactor += value - lastValue;
|
|
149 |
}
|
|
150 |
update();
|
|
151 |
}
|
|
152 |
|
|
153 |
//! [swipe function]
|
|
154 |
void ImageWidget::swipeTriggered(QSwipeGesture *gesture)
|
|
155 |
{
|
|
156 |
if (gesture->horizontalDirection() == QSwipeGesture::Left
|
|
157 |
|| gesture->verticalDirection() == QSwipeGesture::Up)
|
|
158 |
goPrevImage();
|
|
159 |
else
|
|
160 |
goNextImage();
|
|
161 |
update();
|
|
162 |
}
|
|
163 |
//! [swipe function]
|
|
164 |
|
|
165 |
void ImageWidget::resizeEvent(QResizeEvent*)
|
|
166 |
{
|
|
167 |
update();
|
|
168 |
}
|
|
169 |
|
|
170 |
void ImageWidget::openDirectory(const QString &path)
|
|
171 |
{
|
|
172 |
this->path = path;
|
|
173 |
QDir dir(path);
|
|
174 |
QStringList nameFilters;
|
|
175 |
nameFilters << "*.jpg" << "*.png";
|
|
176 |
files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
|
|
177 |
|
|
178 |
position = 0;
|
|
179 |
goToImage(0);
|
|
180 |
update();
|
|
181 |
}
|
|
182 |
|
|
183 |
QImage ImageWidget::loadImage(const QString &fileName)
|
|
184 |
{
|
|
185 |
QImageReader reader(fileName);
|
|
186 |
if (!reader.canRead()) {
|
|
187 |
qDebug() << fileName << ": can't load image";
|
|
188 |
return QImage();
|
|
189 |
}
|
|
190 |
|
|
191 |
QImage image;
|
|
192 |
if (!reader.read(&image)) {
|
|
193 |
qDebug() << fileName << ": corrupted image";
|
|
194 |
return QImage();
|
|
195 |
}
|
|
196 |
return image;
|
|
197 |
}
|
|
198 |
|
|
199 |
void ImageWidget::goNextImage()
|
|
200 |
{
|
|
201 |
if (files.isEmpty())
|
|
202 |
return;
|
|
203 |
|
|
204 |
if (position < files.size()-1) {
|
|
205 |
++position;
|
|
206 |
prevImage = currentImage;
|
|
207 |
currentImage = nextImage;
|
|
208 |
if (position+1 < files.size())
|
|
209 |
nextImage = loadImage(path+QLatin1String("/")+files.at(position+1));
|
|
210 |
else
|
|
211 |
nextImage = QImage();
|
|
212 |
}
|
|
213 |
update();
|
|
214 |
}
|
|
215 |
|
|
216 |
void ImageWidget::goPrevImage()
|
|
217 |
{
|
|
218 |
if (files.isEmpty())
|
|
219 |
return;
|
|
220 |
|
|
221 |
if (position > 0) {
|
|
222 |
--position;
|
|
223 |
nextImage = currentImage;
|
|
224 |
currentImage = prevImage;
|
|
225 |
if (position > 0)
|
|
226 |
prevImage = loadImage(path+QLatin1String("/")+files.at(position-1));
|
|
227 |
else
|
|
228 |
prevImage = QImage();
|
|
229 |
}
|
|
230 |
update();
|
|
231 |
}
|
|
232 |
|
|
233 |
void ImageWidget::goToImage(int index)
|
|
234 |
{
|
|
235 |
if (files.isEmpty())
|
|
236 |
return;
|
|
237 |
|
|
238 |
if (index < 0 || index >= files.size()) {
|
|
239 |
qDebug() << "goToImage: invalid index: " << index;
|
|
240 |
return;
|
|
241 |
}
|
|
242 |
|
|
243 |
if (index == position+1) {
|
|
244 |
goNextImage();
|
|
245 |
return;
|
|
246 |
}
|
|
247 |
|
|
248 |
if (position > 0 && index == position-1) {
|
|
249 |
goPrevImage();
|
|
250 |
return;
|
|
251 |
}
|
|
252 |
|
|
253 |
position = index;
|
|
254 |
|
|
255 |
if (index > 0)
|
|
256 |
prevImage = loadImage(path+QLatin1String("/")+files.at(position-1));
|
|
257 |
else
|
|
258 |
prevImage = QImage();
|
|
259 |
currentImage = loadImage(path+QLatin1String("/")+files.at(position));
|
|
260 |
if (position+1 < files.size())
|
|
261 |
nextImage = loadImage(path+QLatin1String("/")+files.at(position+1));
|
|
262 |
else
|
|
263 |
nextImage = QImage();
|
|
264 |
update();
|
|
265 |
}
|