73 //! [event handler] |
74 //! [event handler] |
74 |
75 |
75 void ImageWidget::paintEvent(QPaintEvent*) |
76 void ImageWidget::paintEvent(QPaintEvent*) |
76 { |
77 { |
77 QPainter p(this); |
78 QPainter p(this); |
78 p.fillRect(rect(), Qt::white); |
|
79 |
79 |
80 float iw = currentImage.width(); |
80 float iw = currentImage.width(); |
81 float ih = currentImage.height(); |
81 float ih = currentImage.height(); |
82 float wh = height(); |
82 float wh = height(); |
83 float ww = width(); |
83 float ww = width(); |
84 |
84 |
85 p.translate(ww/2, wh/2); |
85 p.translate(ww/2, wh/2); |
86 p.translate(horizontalOffset, verticalOffset); |
86 p.translate(horizontalOffset, verticalOffset); |
87 p.rotate(rotationAngle); |
87 p.rotate(rotationAngle); |
88 p.scale(scaleFactor, scaleFactor); |
88 p.scale(currentStepScaleFactor * scaleFactor, currentStepScaleFactor * scaleFactor); |
89 p.translate(-iw/2, -ih/2); |
89 p.translate(-iw/2, -ih/2); |
90 p.drawImage(0, 0, currentImage); |
90 p.drawImage(0, 0, currentImage); |
91 } |
91 } |
92 |
92 |
93 void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) |
93 void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) |
94 { |
94 { |
95 rotationAngle = 0; |
95 rotationAngle = 0; |
96 scaleFactor = 1; |
96 scaleFactor = 1; |
|
97 currentStepScaleFactor = 1; |
97 verticalOffset = 0; |
98 verticalOffset = 0; |
98 horizontalOffset = 0; |
99 horizontalOffset = 0; |
99 update(); |
100 update(); |
100 } |
101 } |
101 |
102 |
102 //! [gesture event handler] |
103 //! [gesture event handler] |
103 bool ImageWidget::gestureEvent(QGestureEvent *event) |
104 bool ImageWidget::gestureEvent(QGestureEvent *event) |
104 { |
105 { |
105 if (QGesture *pan = event->gesture(Qt::PanGesture)) { |
106 if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) |
106 panTriggered(static_cast<QPanGesture*>(pan)); |
107 swipeTriggered(static_cast<QSwipeGesture *>(swipe)); |
107 return true; |
108 else if (QGesture *pan = event->gesture(Qt::PanGesture)) |
108 } else if (QGesture *pinch = event->gesture(Qt::PinchGesture)) { |
109 panTriggered(static_cast<QPanGesture *>(pan)); |
109 pinchTriggered(static_cast<QPinchGesture*>(pinch)); |
110 if (QGesture *pinch = event->gesture(Qt::PinchGesture)) |
110 return true; |
111 pinchTriggered(static_cast<QPinchGesture *>(pinch)); |
111 } else if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) { |
112 return true; |
112 swipeTriggered(static_cast<QSwipeGesture*>(swipe)); |
|
113 return true; |
|
114 } |
|
115 return false; |
|
116 } |
113 } |
117 //! [gesture event handler] |
114 //! [gesture event handler] |
118 |
115 |
119 void ImageWidget::panTriggered(QPanGesture *gesture) |
116 void ImageWidget::panTriggered(QPanGesture *gesture) |
120 { |
117 { |
126 break; |
123 break; |
127 default: |
124 default: |
128 setCursor(Qt::ArrowCursor); |
125 setCursor(Qt::ArrowCursor); |
129 } |
126 } |
130 #endif |
127 #endif |
131 QPointF lastOffset = gesture->offset(); |
128 QPointF delta = gesture->delta(); |
132 horizontalOffset += lastOffset.x(); |
129 horizontalOffset += delta.x(); |
133 verticalOffset += lastOffset.y(); |
130 verticalOffset += delta.y(); |
134 update(); |
131 update(); |
135 } |
132 } |
136 |
133 |
137 void ImageWidget::pinchTriggered(QPinchGesture *gesture) |
134 void ImageWidget::pinchTriggered(QPinchGesture *gesture) |
138 { |
135 { |
139 QPinchGesture::WhatChanged whatChanged = gesture->whatChanged(); |
136 QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); |
140 if (whatChanged & QPinchGesture::RotationAngleChanged) { |
137 if (changeFlags & QPinchGesture::RotationAngleChanged) { |
141 qreal value = gesture->property("rotationAngle").toReal(); |
138 qreal value = gesture->property("rotationAngle").toReal(); |
142 qreal lastValue = gesture->property("lastRotationAngle").toReal(); |
139 qreal lastValue = gesture->property("lastRotationAngle").toReal(); |
143 rotationAngle += value - lastValue; |
140 rotationAngle += value - lastValue; |
144 } |
141 } |
145 if (whatChanged & QPinchGesture::ScaleFactorChanged) { |
142 if (changeFlags & QPinchGesture::ScaleFactorChanged) { |
146 qreal value = gesture->property("scaleFactor").toReal(); |
143 qreal value = gesture->property("scaleFactor").toReal(); |
147 qreal lastValue = gesture->property("lastScaleFactor").toReal(); |
144 currentStepScaleFactor = value; |
148 scaleFactor += value - lastValue; |
145 } |
|
146 if (gesture->state() == Qt::GestureFinished) { |
|
147 scaleFactor *= currentStepScaleFactor; |
|
148 currentStepScaleFactor = 1; |
149 } |
149 } |
150 update(); |
150 update(); |
151 } |
151 } |
152 |
152 |
153 //! [swipe function] |
153 //! [swipe function] |
154 void ImageWidget::swipeTriggered(QSwipeGesture *gesture) |
154 void ImageWidget::swipeTriggered(QSwipeGesture *gesture) |
155 { |
155 { |
156 if (gesture->horizontalDirection() == QSwipeGesture::Left |
156 if (gesture->state() == Qt::GestureFinished) { |
|
157 if (gesture->horizontalDirection() == QSwipeGesture::Left |
157 || gesture->verticalDirection() == QSwipeGesture::Up) |
158 || gesture->verticalDirection() == QSwipeGesture::Up) |
158 goPrevImage(); |
159 goPrevImage(); |
159 else |
160 else |
160 goNextImage(); |
161 goNextImage(); |
161 update(); |
162 update(); |
|
163 } |
162 } |
164 } |
163 //! [swipe function] |
165 //! [swipe function] |
164 |
166 |
165 void ImageWidget::resizeEvent(QResizeEvent*) |
167 void ImageWidget::resizeEvent(QResizeEvent*) |
166 { |
168 { |