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