| 37 |      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 | const QString PIXMAP_ICON("qtg_small_image");
 | 
|  |     38 | const QString CORRUPTED_PIXMAP_ICON("qtg_large_corrupted");
 | 
|  |     39 | const QString VIDEO_MIMETYPE("video");
 | 
|  |     40 | const QString MSG_VIDEO_ICON("qtg_small_video");
 | 
| 41 |     41 | const QString VIDEO_OVERLAY_ICON("qtg_large_video_player");
 | 
|  |     42 | 
 | 
|  |     43 | const int WIDTH_RATIO = 4;
 | 
|  |     44 | const int HEIGHT_RATIO = 3;
 | 
| 37 |     45 | 
 | 
|  |     46 | //---------------------------------------------------------------
 | 
|  |     47 | // UniViewerPixmapWidget::UniViewerPixmapWidget
 | 
|  |     48 | // @see header file
 | 
|  |     49 | //---------------------------------------------------------------
 | 
|  |     50 | UniViewerPixmapWidget::UniViewerPixmapWidget(QGraphicsItem *parent) :
 | 
|  |     51 |     HbIconItem(parent), mViewerUtils(0), mThumbnailManager(0)
 | 
|  |     52 | {
 | 
|  |     53 |     this->grabGesture(Qt::TapGesture);
 | 
|  |     54 |     init();
 | 
|  |     55 | }
 | 
|  |     56 | 
 | 
|  |     57 | //---------------------------------------------------------------
 | 
|  |     58 | // UniViewerPixmapWidget::~UniViewerPixmapWidget
 | 
|  |     59 | // @see header file
 | 
|  |     60 | //---------------------------------------------------------------
 | 
|  |     61 | UniViewerPixmapWidget::~UniViewerPixmapWidget()
 | 
|  |     62 | {
 | 
|  |     63 | }
 | 
|  |     64 | 
 | 
|  |     65 | //---------------------------------------------------------------
 | 
|  |     66 | // UniViewerPixmapWidget::setPixmap
 | 
|  |     67 | // @see header file
 | 
|  |     68 | //---------------------------------------------------------------
 | 
|  |     69 | void UniViewerPixmapWidget::populate(UniMessageInfo *info)
 | 
|  |     70 | {
 | 
|  |     71 |     mMimeType = info->mimetype();
 | 
|  |     72 |     mPixmapPath = info->path();
 | 
|  |     73 |     if (mMimeType.contains(VIDEO_MIMETYPE)) {
 | 
|  |     74 |         this->setIcon(MSG_VIDEO_ICON);
 | 
|  |     75 |         mThumbnailManager->getThumbnail(mPixmapPath);
 | 
|  |     76 |         this->ungrabGesture(Qt::TapGesture);
 | 
|  |     77 |     }
 | 
|  |     78 |     else if (info->isProtected()) {
 | 
|  |     79 |         this->setIconName(PIXMAP_ICON);
 | 
|  |     80 |     }
 | 
|  |     81 |     else if (info->isCorrupted()) {
 | 
|  |     82 |         this->setIconName(CORRUPTED_PIXMAP_ICON);
 | 
|  |     83 |     }
 | 
|  |     84 |     else {
 | 
|  |     85 |         QPixmap pixmap(mPixmapPath);
 | 
|  |     86 |         this->setIcon(HbIcon(pixmap));
 | 
|  |     87 |     }
 | 
|  |     88 | }
 | 
|  |     89 | 
 | 
|  |     90 | //---------------------------------------------------------------
 | 
|  |     91 | // UniViewerPixmapWidget::gestureEvent
 | 
|  |     92 | // @see header file
 | 
|  |     93 | //---------------------------------------------------------------
 | 
|  |     94 | void UniViewerPixmapWidget::gestureEvent(QGestureEvent *event)
 | 
|  |     95 | {
 | 
|  |     96 |     HbTapGesture *tapGesture = qobject_cast<HbTapGesture*> (event->gesture(Qt::TapGesture));
 | 
|  |     97 |     if (tapGesture) {
 | 
|  |     98 |         switch (tapGesture->state()) {
 | 
|  |     99 |         case Qt::GestureStarted:
 | 
|  |    100 |         {
 | 
|  |    101 |             // Trigger haptic feedback.
 | 
|  |    102 |             HbInstantFeedback::play(HbFeedback::Basic);
 | 
|  |    103 |             break;
 | 
|  |    104 |         }
 | 
|  |    105 |         case Qt::GestureUpdated:
 | 
|  |    106 |         {
 | 
|  |    107 |             if (HbTapGesture::TapAndHold == tapGesture->tapStyleHint()) {
 | 
|  |    108 |                 // Handle longtap.
 | 
|  |    109 |                 handleLongTap(tapGesture->scenePosition());
 | 
|  |    110 |             }
 | 
|  |    111 |             break;
 | 
|  |    112 |         }
 | 
|  |    113 |         case Qt::GestureFinished:
 | 
|  |    114 |         {
 | 
|  |    115 |             HbInstantFeedback::play(HbFeedback::Basic);
 | 
|  |    116 |             if (HbTapGesture::Tap == tapGesture->tapStyleHint()) {
 | 
|  |    117 |                 // Handle short tap
 | 
|  |    118 |                 handleShortTap();
 | 
|  |    119 |             }
 | 
|  |    120 |             break;
 | 
|  |    121 |         }
 | 
|  |    122 |         case Qt::GestureCanceled:
 | 
|  |    123 |         {
 | 
|  |    124 |             HbInstantFeedback::play(HbFeedback::Basic);
 | 
|  |    125 |             break;
 | 
|  |    126 |         }
 | 
|  |    127 |         }
 | 
|  |    128 |     }
 | 
|  |    129 |     else {
 | 
|  |    130 |         HbIconItem::gestureEvent(event);
 | 
|  |    131 |     }
 | 
|  |    132 | }
 | 
|  |    133 | 
 | 
|  |    134 | //---------------------------------------------------------------
 | 
|  |    135 | // UniViewerPixmapWidget::handleOpen
 | 
|  |    136 | // @see header file
 | 
|  |    137 | //---------------------------------------------------------------
 | 
|  |    138 | void UniViewerPixmapWidget::handleOpen()
 | 
|  |    139 | {
 | 
|  |    140 |     this->ungrabGesture(Qt::TapGesture);
 | 
|  |    141 | 
 | 
|  |    142 |     if (!mViewerUtils) {
 | 
|  |    143 |         mViewerUtils = new UniViewerUtils(this);
 | 
|  |    144 |     }
 | 
|  |    145 |     mViewerUtils->launchContentViewer(mMimeType, mPixmapPath);
 | 
|  |    146 | 
 | 
|  |    147 |     //fire timer to regrab gesture after some delay.
 | 
|  |    148 |     QTimer::singleShot(300,this,SLOT(regrabGesture()));
 | 
|  |    149 | }
 | 
|  |    150 | 
 | 
|  |    151 | //---------------------------------------------------------------
 | 
|  |    152 | // UniViewerPixmapWidget::handleSave
 | 
|  |    153 | // @see header file
 | 
|  |    154 | //---------------------------------------------------------------
 | 
|  |    155 | void UniViewerPixmapWidget::handleSave()
 | 
|  |    156 | {
 | 
|  |    157 | }
 | 
|  |    158 | 
 | 
| 41 |    159 | //---------------------------------------------------------------
 | 
|  |    160 | // UniViewerPixmapWidget::regrabGesture
 | 
|  |    161 | // @see header file
 | 
|  |    162 | //---------------------------------------------------------------
 | 
|  |    163 | void UniViewerPixmapWidget::regrabGesture()
 | 
|  |    164 | {
 | 
|  |    165 |     this->grabGesture(Qt::TapGesture);
 | 
|  |    166 | }
 | 
|  |    167 | 
 | 
|  |    168 | //---------------------------------------------------------------
 | 
|  |    169 | // UniViewerPixmapWidget::thumbnailReady
 | 
|  |    170 | // @see header
 | 
|  |    171 | //---------------------------------------------------------------
 | 
|  |    172 | void UniViewerPixmapWidget::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
 | 
|  |    173 | {
 | 
|  |    174 |     Q_UNUSED(data)
 | 
|  |    175 |     Q_UNUSED(id)
 | 
|  |    176 |     this->grabGesture(Qt::TapGesture);
 | 
|  |    177 |     if (!error) {
 | 
|  |    178 |         this->setIcon(HbIcon(pixmap));
 | 
|  |    179 |         this->hide();
 | 
|  |    180 |         emit setOverlayIcon(VIDEO_OVERLAY_ICON);
 | 
|  |    181 |     }
 | 
|  |    182 | }
 | 
|  |    183 | 
 | 
|  |    184 | //---------------------------------------------------------------
 | 
|  |    185 | // UniViewerPixmapWidget::init
 | 
|  |    186 | // @see header file
 | 
|  |    187 | //---------------------------------------------------------------
 | 
|  |    188 | void UniViewerPixmapWidget::init()
 | 
|  |    189 | {
 | 
|  |    190 |     mThumbnailManager = new ThumbnailManager(this);
 | 
|  |    191 |     mThumbnailManager->setMode(ThumbnailManager::CropToAspectRatio);
 | 
|  |    192 |     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality);
 | 
|  |    193 |     mThumbnailManager->setThumbnailSize(getThumbnailSize());
 | 
|  |    194 | 
 | 
|  |    195 |     connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void*, int, int)), this,
 | 
|  |    196 |         SLOT(thumbnailReady(QPixmap, void*, int, int)));
 | 
|  |    197 | }
 | 
|  |    198 | 
 | 
| 37 |    199 | //----------------------------------------------------------------------------
 | 
|  |    200 | // UniViewerPixmapWidget::handleShortTap
 | 
|  |    201 | // @see header file
 | 
|  |    202 | //----------------------------------------------------------------------------
 | 
|  |    203 | void UniViewerPixmapWidget::handleShortTap()
 | 
|  |    204 | {
 | 
|  |    205 |     emit shortTap(mPixmapPath);
 | 
|  |    206 | 
 | 
|  |    207 |     // Open the media.
 | 
|  |    208 |     handleOpen();
 | 
|  |    209 | }
 | 
|  |    210 | 
 | 
|  |    211 | //---------------------------------------------------------------
 | 
|  |    212 | // UniViewerPixmapWidget::handleLongTap
 | 
|  |    213 | // @see header file
 | 
|  |    214 | //---------------------------------------------------------------
 | 
|  |    215 | void UniViewerPixmapWidget::handleLongTap(const QPointF &position)
 | 
|  |    216 | {
 | 
|  |    217 |     emit longTap(position);
 | 
|  |    218 | 
 | 
|  |    219 |     HbMenu* menu = new HbMenu;
 | 
|  |    220 |     menu->setAttribute(Qt::WA_DeleteOnClose);
 | 
|  |    221 |     menu->addAction(LOC_OPEN, this, SLOT(handleOpen()));
 | 
|  |    222 |     menu->addAction(LOC_SAVE, this, SLOT(handleSave()));
 | 
|  |    223 |     menu->setPreferredPos(position);
 | 
|  |    224 |     menu->show();
 | 
|  |    225 | }
 | 
|  |    226 | 
 | 
|  |    227 | //---------------------------------------------------------------
 | 
| 41 |    228 | // UniViewerPixmapWidget::getThumbnailSize
 | 
| 37 |    229 | // @see header file
 | 
|  |    230 | //---------------------------------------------------------------
 | 
| 41 |    231 | QSize UniViewerPixmapWidget::getThumbnailSize()
 | 
| 37 |    232 | {
 | 
| 41 |    233 |     QSize thumbnailSize(1, 1);
 | 
|  |    234 |     HbWidget *parent = qobject_cast<HbWidget *>(this->parentWidget());
 | 
|  |    235 | 
 | 
|  |    236 |     if (parent) {
 | 
|  |    237 |         qreal thumbnailWidth = 0.0;
 | 
|  |    238 |         qreal thumbnailHeight = 0.0;
 | 
|  |    239 |         parent->style()->parameter("hb-param-screen-short-edge", thumbnailWidth);
 | 
|  |    240 |         thumbnailHeight = (thumbnailWidth * HEIGHT_RATIO) / WIDTH_RATIO;
 | 
|  |    241 |         thumbnailSize.setHeight(qRound(thumbnailHeight));
 | 
|  |    242 |         thumbnailSize.setWidth(qRound(thumbnailWidth));
 | 
|  |    243 |     }
 | 
|  |    244 |     return thumbnailSize;
 | 
| 37 |    245 | }
 | 
|  |    246 | 
 | 
|  |    247 | // EOF
 |