messagingapp/msgui/unifiedviewer/src/univiewerpixmapwidget.cpp
branchRCL_3
changeset 57 ebe688cedc25
equal deleted inserted replaced
54:fa1df4b99609 57:ebe688cedc25
       
     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: This widget displays the pixmap content in viewer.
       
    15  *
       
    16  */
       
    17 
       
    18 #include "univiewerpixmapwidget.h"
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <HbTapGesture>
       
    22 #include <HbWidget>
       
    23 #include <HbInstantFeedback>
       
    24 #include <HbMenu>
       
    25 #include <QPixmap>
       
    26 #include <QTimer>
       
    27 #include <thumbnailmanager_qt.h>
       
    28 
       
    29 // USER INCLUDES
       
    30 #include "univiewerutils.h"
       
    31 #include "unidatamodelplugininterface.h"
       
    32 
       
    33 // LOCAL CONSTANTS
       
    34 #define LOC_OPEN    hbTrId("txt_common_menu_open")
       
    35 #define LOC_SAVE    hbTrId("txt_common_menu_save")
       
    36 
       
    37 static const char PIXMAP_ICON[] = "qtg_small_image";
       
    38 static const char CORRUPTED_PIXMAP_ICON[] = "qtg_large_corrupted";
       
    39 static const char VIDEO_MIMETYPE[] = "video";
       
    40 
       
    41 static const int WIDTH_RATIO = 4;
       
    42 static const int HEIGHT_RATIO = 3;
       
    43 
       
    44 //---------------------------------------------------------------
       
    45 // UniViewerPixmapWidget::UniViewerPixmapWidget
       
    46 // @see header file
       
    47 //---------------------------------------------------------------
       
    48 UniViewerPixmapWidget::UniViewerPixmapWidget(QGraphicsItem *parent) :
       
    49     HbIconItem(parent), mInfo(0), mViewerUtils(0), mThumbnailManager(0)
       
    50 {
       
    51     this->grabGesture(Qt::TapGesture);
       
    52     init();
       
    53 }
       
    54 
       
    55 //---------------------------------------------------------------
       
    56 // UniViewerPixmapWidget::~UniViewerPixmapWidget
       
    57 // @see header file
       
    58 //---------------------------------------------------------------
       
    59 UniViewerPixmapWidget::~UniViewerPixmapWidget()
       
    60 {
       
    61     if (mInfo) {
       
    62         delete mInfo;
       
    63         mInfo = NULL;
       
    64     }
       
    65 }
       
    66 
       
    67 //---------------------------------------------------------------
       
    68 // UniViewerPixmapWidget::populate
       
    69 // @see header file
       
    70 //---------------------------------------------------------------
       
    71 void UniViewerPixmapWidget::populate(UniMessageInfo *info)
       
    72 {
       
    73     mMimeType = info->mimetype();
       
    74     mPixmapPath = info->path();
       
    75 
       
    76     /**
       
    77      * Create a copy of info for video content.
       
    78      * mInfo will be deleted in the destructor.
       
    79      */
       
    80     if (mMimeType.contains(VIDEO_MIMETYPE)) {
       
    81         mInfo = new UniMessageInfo(*info);
       
    82     }
       
    83 
       
    84     if (info->isProtected()) {
       
    85         if (mMimeType.contains(VIDEO_MIMETYPE)) {
       
    86             emit thumbnailFound(false, mInfo);
       
    87         }
       
    88         else {
       
    89             this->setIconName(PIXMAP_ICON);
       
    90         }
       
    91     }
       
    92     else if (info->isCorrupted()) {
       
    93         if (mMimeType.contains(VIDEO_MIMETYPE)) {
       
    94             emit thumbnailFound(false, mInfo);
       
    95         }
       
    96         else {
       
    97             this->setIconName(CORRUPTED_PIXMAP_ICON);
       
    98         }
       
    99     }
       
   100     else {
       
   101         if (mMimeType.contains(VIDEO_MIMETYPE)) {
       
   102             mThumbnailManager->getThumbnail(mPixmapPath);
       
   103             this->ungrabGesture(Qt::TapGesture);
       
   104         }
       
   105         else {
       
   106             QPixmap pixmap(mPixmapPath);
       
   107             this->setIcon(HbIcon(pixmap));
       
   108         }
       
   109     }
       
   110 }
       
   111 
       
   112 //---------------------------------------------------------------
       
   113 // UniViewerPixmapWidget::gestureEvent
       
   114 // @see header file
       
   115 //---------------------------------------------------------------
       
   116 void UniViewerPixmapWidget::gestureEvent(QGestureEvent *event)
       
   117 {
       
   118     HbTapGesture *tapGesture = qobject_cast<HbTapGesture*> (event->gesture(Qt::TapGesture));
       
   119     if (tapGesture) {
       
   120         switch (tapGesture->state()) {
       
   121         case Qt::GestureStarted:
       
   122         {
       
   123             // Trigger haptic feedback.
       
   124             HbInstantFeedback::play(HbFeedback::Basic);
       
   125             break;
       
   126         }
       
   127         case Qt::GestureUpdated:
       
   128         {
       
   129             if (HbTapGesture::TapAndHold == tapGesture->tapStyleHint()) {
       
   130                 // Handle longtap.
       
   131                 handleLongTap(tapGesture->scenePosition());
       
   132             }
       
   133             break;
       
   134         }
       
   135         case Qt::GestureFinished:
       
   136         {
       
   137             HbInstantFeedback::play(HbFeedback::Basic);
       
   138             if (HbTapGesture::Tap == tapGesture->tapStyleHint()) {
       
   139                 // Handle short tap
       
   140                 handleShortTap();
       
   141             }
       
   142             break;
       
   143         }
       
   144         case Qt::GestureCanceled:
       
   145         {
       
   146             HbInstantFeedback::play(HbFeedback::Basic);
       
   147             break;
       
   148         }
       
   149         }
       
   150     }
       
   151     else {
       
   152         HbIconItem::gestureEvent(event);
       
   153     }
       
   154 }
       
   155 
       
   156 //---------------------------------------------------------------
       
   157 // UniViewerPixmapWidget::handleOpen
       
   158 // @see header file
       
   159 //---------------------------------------------------------------
       
   160 void UniViewerPixmapWidget::handleOpen()
       
   161 {
       
   162     this->ungrabGesture(Qt::TapGesture);
       
   163 
       
   164     if (!mViewerUtils) {
       
   165         mViewerUtils = new UniViewerUtils(this);
       
   166     }
       
   167     mViewerUtils->launchContentViewer(mMimeType, mPixmapPath);
       
   168 
       
   169     //fire timer to regrab gesture after some delay.
       
   170     QTimer::singleShot(300,this,SLOT(regrabGesture()));
       
   171 }
       
   172 
       
   173 //---------------------------------------------------------------
       
   174 // UniViewerPixmapWidget::handleSave
       
   175 // @see header file
       
   176 //---------------------------------------------------------------
       
   177 void UniViewerPixmapWidget::handleSave()
       
   178 {
       
   179 }
       
   180 
       
   181 //---------------------------------------------------------------
       
   182 // UniViewerPixmapWidget::regrabGesture
       
   183 // @see header file
       
   184 //---------------------------------------------------------------
       
   185 void UniViewerPixmapWidget::regrabGesture()
       
   186 {
       
   187     this->grabGesture(Qt::TapGesture);
       
   188 }
       
   189 
       
   190 //---------------------------------------------------------------
       
   191 // UniViewerPixmapWidget::thumbnailReady
       
   192 // @see header
       
   193 //---------------------------------------------------------------
       
   194 void UniViewerPixmapWidget::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
       
   195 {
       
   196     Q_UNUSED(data)
       
   197     Q_UNUSED(id)
       
   198     this->grabGesture(Qt::TapGesture);
       
   199     if (error) {
       
   200         emit thumbnailFound(false, mInfo);
       
   201     }
       
   202     else {
       
   203         this->setIcon(HbIcon(pixmap));
       
   204         this->hide();
       
   205         emit thumbnailFound(true, NULL);
       
   206     }
       
   207 }
       
   208 
       
   209 //---------------------------------------------------------------
       
   210 // UniViewerPixmapWidget::init
       
   211 // @see header file
       
   212 //---------------------------------------------------------------
       
   213 void UniViewerPixmapWidget::init()
       
   214 {
       
   215     mThumbnailManager = new ThumbnailManager(this);
       
   216     mThumbnailManager->setMode(ThumbnailManager::CropToAspectRatio);
       
   217     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality);
       
   218     mThumbnailManager->setThumbnailSize(getThumbnailSize());
       
   219 
       
   220     connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void*, int, int)), this,
       
   221         SLOT(thumbnailReady(QPixmap, void*, int, int)));
       
   222 }
       
   223 
       
   224 //----------------------------------------------------------------------------
       
   225 // UniViewerPixmapWidget::handleShortTap
       
   226 // @see header file
       
   227 //----------------------------------------------------------------------------
       
   228 void UniViewerPixmapWidget::handleShortTap()
       
   229 {
       
   230     emit shortTap(mPixmapPath);
       
   231 
       
   232     // Open the media.
       
   233     handleOpen();
       
   234 }
       
   235 
       
   236 //---------------------------------------------------------------
       
   237 // UniViewerPixmapWidget::handleLongTap
       
   238 // @see header file
       
   239 //---------------------------------------------------------------
       
   240 void UniViewerPixmapWidget::handleLongTap(const QPointF &position)
       
   241 {
       
   242     emit longTap(position);
       
   243 
       
   244     HbMenu* menu = new HbMenu;
       
   245     menu->setAttribute(Qt::WA_DeleteOnClose);
       
   246     menu->addAction(LOC_OPEN, this, SLOT(handleOpen()));
       
   247     menu->addAction(LOC_SAVE, this, SLOT(handleSave()));
       
   248     menu->setPreferredPos(position);
       
   249     menu->show();
       
   250 }
       
   251 
       
   252 //---------------------------------------------------------------
       
   253 // UniViewerPixmapWidget::getThumbnailSize
       
   254 // @see header file
       
   255 //---------------------------------------------------------------
       
   256 QSize UniViewerPixmapWidget::getThumbnailSize()
       
   257 {
       
   258     QSize thumbnailSize(1, 1);
       
   259     HbWidget *parent = qobject_cast<HbWidget *>(this->parentWidget());
       
   260 
       
   261     if (parent) {
       
   262         qreal thumbnailWidth = 0.0;
       
   263         qreal thumbnailHeight = 0.0;
       
   264         parent->style()->parameter("hb-param-screen-short-edge", thumbnailWidth);
       
   265         thumbnailHeight = (thumbnailWidth * HEIGHT_RATIO) / WIDTH_RATIO;
       
   266         thumbnailSize.setHeight(qRound(thumbnailHeight));
       
   267         thumbnailSize.setWidth(qRound(thumbnailWidth));
       
   268     }
       
   269     return thumbnailSize;
       
   270 }
       
   271 
       
   272 // EOF