|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: glxzoomwidget.cpp |
|
15 * description of the class GlxGlxZoomWidget which controls the Zoom behavior of coverflow. |
|
16 * |
|
17 */ |
|
18 #include <QPinchGesture> |
|
19 #include <hbiconitem.h> |
|
20 #include <QTimeLine> |
|
21 #include <QGesture> |
|
22 #include "glximagedecoderwrapper.h" |
|
23 #include "glxmodelparm.h" |
|
24 #include "glxzoomwidget.h" |
|
25 |
|
26 GlxZoomWidget::GlxZoomWidget(QGraphicsItem *parent):HbScrollArea(parent), mModel(NULL), mMinZValue(MINZVALUE), mMaxZValue(MAXZVALUE), mImageDecodeRequestSend(false), mPinchGestureOngoing(false), mDecodedImageAvailable(false) |
|
27 { |
|
28 grabGesture(Qt::PinchGesture); |
|
29 setAcceptTouchEvents(true) ; |
|
30 setFrictionEnabled(false); |
|
31 setZValue(mMinZValue); |
|
32 //create the child items and background |
|
33 mZoomWidget = new QGraphicsWidget(this); |
|
34 mZoomItem = new QGraphicsPixmapItem(mZoomWidget); |
|
35 mZoomItem->setTransformationMode(Qt::SmoothTransformation); |
|
36 //the black background |
|
37 //replace when a proper substitute for setting backgrounds is known |
|
38 mBlackBackgroundItem = new HbIconItem(this); |
|
39 mBlackBackgroundItem->setBrush(QBrush(Qt::black)); |
|
40 mBlackBackgroundItem->hide(); |
|
41 //does not work so is commented |
|
42 //setBackgroundItem(mBlackBackgroundItem); |
|
43 |
|
44 //initializing the image decoder |
|
45 mImageDecoder = new GlxImageDecoderWrapper; |
|
46 |
|
47 //inititalizing the timer for animation |
|
48 m_AnimTimeLine = new QTimeLine(1000, this); |
|
49 m_AnimTimeLine->setFrameRange(0, 100); |
|
50 connect(m_AnimTimeLine, SIGNAL(frameChanged(int)), this, SLOT(animationFrameChanged(int))); |
|
51 connect(m_AnimTimeLine, SIGNAL(finished()), this, SLOT(animationTimeLineFinished())); |
|
52 } |
|
53 |
|
54 GlxZoomWidget::~GlxZoomWidget() |
|
55 { |
|
56 //disconnect all existing signals |
|
57 disconnect(this,SIGNAL( pinchGestureReceived(int) ), this, SLOT( sendDecodeRequest(int) ) ); |
|
58 //no Null checks required |
|
59 delete mZoomItem; |
|
60 // delete mZoomWidget; //as this is a content widegt it will automatically be deleted |
|
61 delete mBlackBackgroundItem; |
|
62 //reset the decoder to cancel pending tasks |
|
63 if(mImageDecoder) { |
|
64 mImageDecoder->resetDecoder(); |
|
65 delete mImageDecoder; |
|
66 } |
|
67 } |
|
68 |
|
69 void GlxZoomWidget::setModel (QAbstractItemModel *model) |
|
70 { |
|
71 if(model) |
|
72 { |
|
73 mModel = model; |
|
74 retreiveFocusedImage(); //Update mZoomItem with focused Image |
|
75 connect( mModel, SIGNAL( dataChanged(QModelIndex,QModelIndex) ), this, SLOT( dataChanged(QModelIndex,QModelIndex) ) ); |
|
76 } |
|
77 } |
|
78 |
|
79 void GlxZoomWidget::setWindowSize(QSize windowSize) |
|
80 { |
|
81 mWindowSize = windowSize; |
|
82 mBlackBackgroundItem->setGeometry(QRectF(QPointF(0,0), mWindowSize)); |
|
83 //try to reset the max and min zoomed size here |
|
84 } |
|
85 |
|
86 void GlxZoomWidget::indexChanged(int index) |
|
87 { |
|
88 Q_UNUSED(index); |
|
89 if(mFocusIndex != index) { |
|
90 mImageDecoder->resetDecoder();//reset the decoder first to cancel pending tasks |
|
91 mImageDecodeRequestSend = false; |
|
92 mDecodedImageAvailable = false; |
|
93 retreiveFocusedImage(); //Update mZoomItem with focused Image |
|
94 } |
|
95 } |
|
96 |
|
97 void GlxZoomWidget::cleanUp() |
|
98 { |
|
99 // disconnect( mModel, SIGNAL( dataChanged(QModelIndex,QModelIndex) ), this, SLOT( dataChanged(QModelIndex,QModelIndex) ) ); |
|
100 if(mImageDecoder) { |
|
101 mImageDecoder->resetDecoder(); |
|
102 } |
|
103 mZoomItem->setPixmap(QPixmap()); |
|
104 } |
|
105 |
|
106 void GlxZoomWidget::activate() |
|
107 { |
|
108 } |
|
109 |
|
110 void GlxZoomWidget::setMinMaxZValue(int minZvalue, int maxZvalue) |
|
111 { |
|
112 mMinZValue = minZvalue; |
|
113 mMaxZValue = maxZvalue; |
|
114 } |
|
115 |
|
116 void GlxZoomWidget::connectDecodeRequestToPinchEvent() |
|
117 { |
|
118 connect(this,SIGNAL( pinchGestureReceived(int) ), this, SLOT( sendDecodeRequest(int) ), Qt::QueuedConnection ); |
|
119 } |
|
120 |
|
121 bool GlxZoomWidget::sceneEvent(QEvent *event) |
|
122 { |
|
123 bool consume(false); |
|
124 if (event->type() == QEvent::Gesture) { |
|
125 consume = executeGestureEvent(this, static_cast<QGestureEvent*>(event)); |
|
126 } |
|
127 if(!consume) |
|
128 { |
|
129 consume = HbScrollArea::sceneEvent(event); |
|
130 } |
|
131 return consume; |
|
132 } |
|
133 |
|
134 bool GlxZoomWidget::sceneEventFilter(QGraphicsItem *watched,QEvent *event) |
|
135 { |
|
136 qDebug("GlxCoverFlow::eventFilter " ); |
|
137 bool consume = false; |
|
138 if (event->type() == QEvent::Gesture) { |
|
139 consume = executeGestureEvent(watched, static_cast<QGestureEvent*>(event)); |
|
140 } |
|
141 |
|
142 if(!consume) { |
|
143 consume = HbScrollArea::sceneEventFilter(watched,event); |
|
144 } |
|
145 return consume; |
|
146 |
|
147 } |
|
148 |
|
149 bool GlxZoomWidget::executeGestureEvent(QGraphicsItem *source,QGestureEvent *event) |
|
150 { |
|
151 if (QGesture *pinch = event->gesture(Qt::PinchGesture)) { |
|
152 QPinchGesture* pinchG = static_cast<QPinchGesture *>(pinch); |
|
153 QPinchGesture::ChangeFlags changeFlags = pinchG->changeFlags(); |
|
154 if (changeFlags & QPinchGesture::ScaleFactorChanged) { |
|
155 mPinchGestureOngoing = true; |
|
156 //bring the zoom widget to foreground |
|
157 setZValue(mMaxZValue); |
|
158 //show the black background |
|
159 mBlackBackgroundItem->setParentItem(parentItem()); |
|
160 mBlackBackgroundItem->setZValue(mMaxZValue - 1); |
|
161 mBlackBackgroundItem->show(); |
|
162 |
|
163 //retreive the gesture values |
|
164 qreal value = pinchG->scaleFactor() / pinchG->lastScaleFactor(); |
|
165 QPointF center = pinchG->property("centerPoint").toPointF(); |
|
166 //set the gesture center to the scene coordinates |
|
167 QPointF sceneGestureCenter = source->sceneTransform().map(center); |
|
168 zoomImage(value, sceneGestureCenter); |
|
169 |
|
170 } |
|
171 if (pinchG->state() == Qt::GestureStarted) { |
|
172 emit pinchGestureReceived(mFocusIndex); |
|
173 } |
|
174 |
|
175 if (pinchG->state() == Qt::GestureFinished) { |
|
176 if(mStepCurrentSize != mCurrentSize) { |
|
177 //For giving a spring effect when user has zoomed more than normal. |
|
178 if(mStepCurrentSize.width() > mMaxScaleDecSize.width()) { |
|
179 //scale the image to limited size |
|
180 qreal value = mMaxScaleDecSize.width()/mCurrentSize.width(); |
|
181 QPointF center(mWindowSize.width()/2, mWindowSize.height()/2); |
|
182 QPointF sceneGestureCenter = source->sceneTransform().map(center); |
|
183 zoomImage(value, sceneGestureCenter); |
|
184 } |
|
185 mPinchGestureOngoing = false; |
|
186 //finalize the transforms to the geometry else panning will not work |
|
187 finalizeWidgetTransform(); |
|
188 } |
|
189 //push the Zoom widget to background when zoomed image size nears FS image |
|
190 if(mStepCurrentSize.width() <= mMinDecScaleSize.width()*1.3) { |
|
191 mBlackBackgroundItem->hide(); |
|
192 //push the widget back to background |
|
193 setZValue(mMinZValue); |
|
194 emit zoomWidgetMovedBackground(mFocusIndex); |
|
195 //do not reset the transform here as it will then zoom-in the widget to decoded image size |
|
196 } |
|
197 } |
|
198 //gesture accepted |
|
199 return true; |
|
200 } |
|
201 //gesture rejected |
|
202 if(!mPinchGestureOngoing) { |
|
203 return false; |
|
204 } |
|
205 return true; |
|
206 |
|
207 } |
|
208 |
|
209 void GlxZoomWidget::zoomImage(qreal zoomFactor, QPointF center) |
|
210 { |
|
211 adjustGestureCenter(center, zoomFactor); |
|
212 QSizeF requiredSize(mCurrentSize.width()*zoomFactor, mCurrentSize.height()*zoomFactor); |
|
213 limitRequiredSize(requiredSize); |
|
214 if(requiredSize != mCurrentSize) { |
|
215 QTransform zoomTransform = mZoomWidget->transform(); |
|
216 QPointF transformedCenter = mZoomWidget->sceneTransform().inverted().map(center); |
|
217 zoomTransform.translate(transformedCenter.x(),transformedCenter.y()); |
|
218 zoomTransform.scale(requiredSize.width()/mCurrentSize.width(), requiredSize.height()/mCurrentSize.height()); |
|
219 zoomTransform.translate(-transformedCenter.x(),-transformedCenter.y()); |
|
220 mZoomWidget->setTransform(zoomTransform); |
|
221 mCurrentSize = requiredSize; |
|
222 } |
|
223 |
|
224 } |
|
225 |
|
226 |
|
227 void GlxZoomWidget::limitRequiredSize(QSizeF &requiredSize) |
|
228 { |
|
229 if(requiredSize.width() > mMaxScaleSize.width() ) { |
|
230 requiredSize = mMaxScaleSize ; |
|
231 } |
|
232 else if(requiredSize.width() < mMinDecScaleSize.width() ) { |
|
233 requiredSize = mMinDecScaleSize ; |
|
234 } |
|
235 |
|
236 |
|
237 } |
|
238 |
|
239 //makes sure that the gesture is on the screen center if the image is smaller than the screen |
|
240 void GlxZoomWidget::adjustGestureCenter(QPointF & gestureCenter, qreal& zoomFactor) |
|
241 { |
|
242 if(zoomFactor > 1 &&zoomFactor > 1.2 ) { |
|
243 zoomFactor = 1.2; |
|
244 } |
|
245 |
|
246 if(zoomFactor < 1 &&zoomFactor < 0.8 ) { |
|
247 zoomFactor = 0.8; |
|
248 } |
|
249 QSizeF requiredSize(mCurrentSize.width()*zoomFactor, mCurrentSize.height()*zoomFactor); |
|
250 //keep smaller image centered |
|
251 if(mCurrentSize.width() <= mWindowSize.width() ) |
|
252 { |
|
253 gestureCenter.setX(mWindowSize.width()/2); |
|
254 |
|
255 } |
|
256 if(mCurrentSize.height() <= mWindowSize.height()) |
|
257 { |
|
258 gestureCenter.setY(mWindowSize.height()/2); |
|
259 |
|
260 } |
|
261 //maintains the boundary of the edges for zoom out conditions |
|
262 if(zoomFactor < 1) |
|
263 { |
|
264 QPointF itemOriginPos = mZoomWidget->sceneTransform().map(QPointF(0,0)); |
|
265 bool hasWidthExceededWindow = mCurrentSize.width() > mWindowSize.width(); |
|
266 bool hasHeightExceededWindow = mCurrentSize.height() > mWindowSize.height(); |
|
267 if(itemOriginPos.x() >= 0) { |
|
268 //image has crossed left boundry leaving blank space |
|
269 if(hasWidthExceededWindow) { |
|
270 //stick the gesture to the left corner |
|
271 gestureCenter.setX(itemOriginPos.x()); |
|
272 } |
|
273 } |
|
274 //Check if the right boundry can be adjusted |
|
275 if(itemOriginPos.x()+ mCurrentSize.width() <= mWindowSize.width()) { |
|
276 //Image is before the right boundry leaving blank space |
|
277 if(hasWidthExceededWindow) { |
|
278 //stick the gesture to the right corner |
|
279 gestureCenter.setX(itemOriginPos.x()+ mCurrentSize.width()); |
|
280 } |
|
281 } |
|
282 //check if the upper boundry could be adjusted |
|
283 if(itemOriginPos.y() >= 0) { |
|
284 //image has crossed the upper boundry leaving blank space |
|
285 if(hasHeightExceededWindow) { |
|
286 //stick the image to the upper boundry |
|
287 gestureCenter.setY(itemOriginPos.y()); |
|
288 } |
|
289 } |
|
290 //check if the lower boundry could be adjusted |
|
291 if(itemOriginPos.y()+ mCurrentSize.height() <= mWindowSize.height()) { |
|
292 //Image is before the right boundry leaving blank space |
|
293 if(hasHeightExceededWindow) { |
|
294 //stick the image to the right corner |
|
295 gestureCenter.setY(itemOriginPos.y()+ mCurrentSize.height()); |
|
296 } |
|
297 |
|
298 } |
|
299 } |
|
300 //control the zoom Factor to boundaries |
|
301 if(mCurrentSize.width() > mWindowSize.width() && requiredSize.width() <= mWindowSize.width()) |
|
302 { |
|
303 zoomFactor = mWindowSize.width()/mCurrentSize.width(); |
|
304 |
|
305 } |
|
306 else if(mCurrentSize.height() > mWindowSize.height() && requiredSize.height() <= mWindowSize.height()) |
|
307 { |
|
308 zoomFactor = mWindowSize.height()/mCurrentSize.height(); |
|
309 |
|
310 } |
|
311 |
|
312 //reduce the ZF so as to show a decelerated effect at max/min levels |
|
313 |
|
314 if(mCurrentSize.width() > mMaxScaleDecSize.width() && zoomFactor > 1 ) { |
|
315 zoomFactor = 1.0 + ((zoomFactor-1.0)/6) ; |
|
316 } |
|
317 if(mCurrentSize.width() < mMinDecScaleSize.width() && zoomFactor < 1 ) { |
|
318 zoomFactor = 1.0 - ((1.0-zoomFactor)/6) ; |
|
319 } |
|
320 |
|
321 |
|
322 } |
|
323 |
|
324 //get the latest focused image and set it to mZoomItem |
|
325 void GlxZoomWidget::retreiveFocusedImage() |
|
326 { |
|
327 |
|
328 QPixmap targetPixmap(getFocusedImage()); |
|
329 //initialize all the variables wrt the focussed pixmap |
|
330 mZoomWidget->resetTransform(); |
|
331 mItemSize = targetPixmap.size(); |
|
332 mMaxScaleSize = mItemSize; |
|
333 mMaxScaleSize.scale(mWindowSize*13, Qt::KeepAspectRatio); |
|
334 mMaxScaleDecSize = mItemSize; |
|
335 mMaxScaleDecSize.scale(mWindowSize*7, Qt::KeepAspectRatio); |
|
336 mMinScaleSize = mItemSize* 0.7; |
|
337 mMinDecScaleSize = mItemSize; |
|
338 QPointF originPos = sceneTransform().map(QPointF(0,0)); |
|
339 mZoomWidget->setGeometry(QRectF(QPointF(mWindowSize.width()/2 - mItemSize.width()/2,mWindowSize.height()/2 - mItemSize.height()/2),mItemSize )); //chk this |
|
340 mZoomWidget->setPreferredSize(mItemSize); |
|
341 mZoomItem->setPixmap(targetPixmap); |
|
342 mCurrentSize = mItemSize; |
|
343 mStepCurrentSize = mItemSize; |
|
344 setContentWidget(mZoomWidget); |
|
345 show(); |
|
346 } |
|
347 |
|
348 |
|
349 void GlxZoomWidget::dataChanged(QModelIndex startIndex, QModelIndex endIndex) |
|
350 { |
|
351 if(mFocusIndex >= startIndex.row() && mFocusIndex <= endIndex.row()) { |
|
352 //get the latest image from the model |
|
353 //will replace a decoded image if callback is received after decoded image is received so a fix is required |
|
354 //retreiveFocusedImage(); |
|
355 if(!mDecodedImageAvailable) { |
|
356 QPixmap targetPixmap(getFocusedImage()); |
|
357 mZoomItem->setPixmap(targetPixmap); |
|
358 finalizeWidgetTransform(); |
|
359 } |
|
360 } |
|
361 } |
|
362 |
|
363 void GlxZoomWidget::decodedImageAvailable() |
|
364 { |
|
365 //new bitmap with better resolution is available |
|
366 //so set it to the item |
|
367 QPixmap decodedPixmap = mImageDecoder->getPixmap(); |
|
368 disconnect(mImageDecoder, SIGNAL(pixmapDecoded()), this, SLOT(decodedImageAvailable())); |
|
369 if(decodedPixmap.isNull()){ |
|
370 return; |
|
371 } |
|
372 mDecodedImageAvailable = true; |
|
373 mZoomItem->setPixmap(decodedPixmap); |
|
374 mItemSize = decodedPixmap.size(); |
|
375 //this is important if not done then old transforms will be applied on the new image |
|
376 finalizeWidgetTransform(); |
|
377 } |
|
378 |
|
379 void GlxZoomWidget::sendDecodeRequest(int index) |
|
380 { |
|
381 if(!mImageDecodeRequestSend) { |
|
382 QString imagePath = (mModel->data(mModel->index(index,0),GlxUriRole)).value<QString>(); |
|
383 mImageDecoder->decodeImage(imagePath); |
|
384 connect(mImageDecoder, SIGNAL(pixmapDecoded()), this, SLOT(decodedImageAvailable())); |
|
385 mImageDecodeRequestSend = true; |
|
386 } |
|
387 } |
|
388 |
|
389 |
|
390 void GlxZoomWidget::finalizeWidgetTransform() |
|
391 { |
|
392 QPointF widgetPos = mZoomWidget->sceneTransform().map(QPointF(0,0)); //Map the origin wrt scene |
|
393 mZoomWidget->resetTransform(); |
|
394 mZoomWidget->scale(mCurrentSize.width()/mItemSize.width(), mCurrentSize.height()/mItemSize.height()); |
|
395 mZoomWidget->setGeometry(QRectF(widgetPos , mCurrentSize)); |
|
396 // this updates HbScrollArea on the sizeHint of ZoomWidget |
|
397 mZoomWidget->setPreferredSize(mCurrentSize); |
|
398 mStepCurrentSize = mCurrentSize; |
|
399 } |
|
400 |
|
401 QPixmap GlxZoomWidget::getFocusedImage() |
|
402 { |
|
403 mFocusIndex = mModel->data(mModel->index(0,0),GlxFocusIndexRole).value<int>(); |
|
404 QVariant iconVariant = mModel->data(mModel->index(mFocusIndex,0),GlxFsImageRole); |
|
405 QVariant sizeVariant = mModel->data(mModel->index(mFocusIndex,0),GlxDimensionsRole); |
|
406 QPixmap targetPixmap; |
|
407 //retreive pixmap from the HbIcon received from model |
|
408 //should change the model to return and save pixmaps and convert to HbIcons Instead |
|
409 if ( iconVariant.isValid() && iconVariant.canConvert<HbIcon> () ) { |
|
410 QIcon itemIcon = iconVariant.value<HbIcon>().qicon(); |
|
411 QSize itemSize = itemIcon.actualSize(mWindowSize); |
|
412 QSize scaleSize; |
|
413 if(sizeVariant.isValid() && sizeVariant.canConvert<QSize> ()) { |
|
414 scaleSize = sizeVariant.toSize(); |
|
415 if(!(scaleSize.width() < mWindowSize.width() && scaleSize.height() < mWindowSize.height())) { |
|
416 scaleSize = mWindowSize; |
|
417 } |
|
418 } |
|
419 targetPixmap = itemIcon.pixmap(itemSize).scaled(scaleSize, Qt::KeepAspectRatio); |
|
420 mItemSize = targetPixmap.size(); |
|
421 } |
|
422 return targetPixmap; |
|
423 |
|
424 } |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 void GlxZoomWidget::animateZoomIn(QPointF animRefPoint) |
|
431 { |
|
432 emit pinchGestureReceived(mFocusIndex); |
|
433 //bring the zoom widget to foreground |
|
434 setZValue(mMaxZValue); |
|
435 //show the black background |
|
436 mBlackBackgroundItem->setParentItem(parentItem()); |
|
437 mBlackBackgroundItem->setZValue(mMaxZValue - 1); |
|
438 mBlackBackgroundItem->show(); |
|
439 m_AnimRefPoint = animRefPoint; |
|
440 QSizeF requiredSize = mItemSize; |
|
441 requiredSize.scale(mWindowSize*3.5, Qt::KeepAspectRatio); |
|
442 m_FinalAnimatedScaleFactor = requiredSize.width()*3.5/mMinScaleSize.width(); |
|
443 m_AnimTimeLine->setDirection(QTimeLine::Forward); |
|
444 m_AnimTimeLine->start(); |
|
445 // zoomImage(5, m_AnimRefPoint); |
|
446 |
|
447 } |
|
448 void GlxZoomWidget::animateZoomOut(QPointF animRefPoint) |
|
449 { |
|
450 m_AnimRefPoint = animRefPoint; |
|
451 m_FinalAnimatedScaleFactor = mMinScaleSize.width()/mCurrentSize.width(); |
|
452 //m_AnimTimeLine->setDirection(QTimeLine::Backward); |
|
453 m_AnimTimeLine->start(); |
|
454 } |
|
455 void GlxZoomWidget::animationFrameChanged(int frameNumber) |
|
456 { |
|
457 qreal scaleFactor = 1; |
|
458 if(m_FinalAnimatedScaleFactor > 1) { |
|
459 // qreal scaleFactor = (100+ ((m_FinalAnimatedScaleFactor*100 - 100)/100)*frameNumber)/100; |
|
460 scaleFactor = 1.0 + (((m_FinalAnimatedScaleFactor - 1)/100)*frameNumber); |
|
461 } |
|
462 if(m_FinalAnimatedScaleFactor < 1) { |
|
463 scaleFactor = (m_FinalAnimatedScaleFactor*100+ ((100 - m_FinalAnimatedScaleFactor*100 )/100)*frameNumber)/100; |
|
464 // qreal scaleFactor = 1.0 + (((m_FinalAnimatedScaleFactor - 1)/100)*frameNumber) |
|
465 } |
|
466 |
|
467 zoomImage(scaleFactor, m_AnimRefPoint); |
|
468 |
|
469 } |
|
470 void GlxZoomWidget::animationTimeLineFinished() |
|
471 { |
|
472 finalizeWidgetTransform(); |
|
473 //push the Zoom widget to background when zoomed image size nears FS image |
|
474 if(mStepCurrentSize.width() <= mMinDecScaleSize.width()*1.3) { |
|
475 mBlackBackgroundItem->hide(); |
|
476 //push the widget back to background |
|
477 setZValue(mMinZValue); |
|
478 emit zoomWidgetMovedBackground(mFocusIndex); |
|
479 //do not reset the transform here as it will then zoom-in the widget to decoded image size |
|
480 } |
|
481 } |
|
482 |