messagingapp/msgui/unifiedviewer/src/univiewerbodywidget.cpp
changeset 23 238255e8b033
child 25 84d9eb65b26f
equal deleted inserted replaced
5:4697dfb2d7ad 23:238255e8b033
       
     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 body of the viewer
       
    15  *
       
    16  */
       
    17 
       
    18 #include "univiewerbodywidget.h"
       
    19 
       
    20 #include <QFile>
       
    21 #include <QFileInfo>
       
    22 #include <QPixmap>
       
    23 #include <QGraphicsLayout>
       
    24 
       
    25 #include <HbTextItem>
       
    26 #include <HbIconItem>
       
    27 #include <HbPushButton>
       
    28 #include <HbAction>
       
    29 #include <HbMenu>
       
    30 #include <HbMainWindow>
       
    31 #include <HbInstance>
       
    32 #include <HbGesture>
       
    33 #include <HbGestureSceneFilter>
       
    34 
       
    35 #include "debugtraces.h"
       
    36 #include "univiewertextitem.h"
       
    37 
       
    38 // LOCAL CONSTANTS
       
    39 const QString AUDIO_ICON("qtg_mono_audio.svg");
       
    40 
       
    41 //---------------------------------------------------------------
       
    42 //UniViewerBodyWidget::UniViewerBodyWidget
       
    43 // @see header file
       
    44 //---------------------------------------------------------------
       
    45 UniViewerBodyWidget::UniViewerBodyWidget(QGraphicsItem *parent) :
       
    46     HbWidget(parent), mHasText(false), mTextItem(0), mIconItem(0), mAudioItem(0)
       
    47 {
       
    48     //This is permanent item in the widget
       
    49     mTextItem = new UniViewerTextItem(this);
       
    50     HbStyle::setItemName(mTextItem, "textItem");
       
    51 
       
    52     //Gesture filter for the image
       
    53     gestureFilter = new HbGestureSceneFilter(Qt::LeftButton, this);
       
    54     gestureFilter->setLongpressAnimation(true);
       
    55     HbGesture *gesture = new HbGesture(HbGesture::longpress, 20);
       
    56     gestureFilter->addGesture(gesture);
       
    57     connect(gesture, SIGNAL(longPress(QPointF)), this, SLOT(longPressed(QPointF)));
       
    58     
       
    59     connect(mTextItem,SIGNAL(sendMessage(const QString&)),
       
    60             this, SIGNAL(sendMessage(const QString&)));
       
    61 }
       
    62 
       
    63 //---------------------------------------------------------------
       
    64 //UniViewerBodyWidget::~UniViewerBodyWidget
       
    65 // @see header file
       
    66 //---------------------------------------------------------------
       
    67 UniViewerBodyWidget::~UniViewerBodyWidget()
       
    68 {
       
    69 }
       
    70 
       
    71 //---------------------------------------------------------------
       
    72 //UniViewerBodyWidget::setImage
       
    73 // @see header file
       
    74 //---------------------------------------------------------------
       
    75 void UniViewerBodyWidget::setImage(QString imagefile)
       
    76 {
       
    77     //create image item instance
       
    78     if (!mIconItem) {
       
    79         mIconItem = new HbIconItem(this);
       
    80         HbStyle::setItemName(mIconItem, "pixmap");
       
    81     }
       
    82 
       
    83     QPixmap pixmap(imagefile);
       
    84     mIconItem->setIcon(HbIcon(pixmap));
       
    85     this->repolish();
       
    86 }
       
    87 
       
    88 //---------------------------------------------------------------
       
    89 //UniViewerBodyWidget::setAudio
       
    90 // @see header file
       
    91 //---------------------------------------------------------------
       
    92 void UniViewerBodyWidget::setAudio(QString audiofile)
       
    93 {
       
    94     if (!mAudioItem) {
       
    95         mAudioItem = new HbPushButton(this);
       
    96         HbStyle::setItemName(mAudioItem, "audioItem");
       
    97     }
       
    98     QFileInfo fileInfo(audiofile);
       
    99     mAudioItem->setIcon(HbIcon(AUDIO_ICON));
       
   100     mAudioItem->setText(fileInfo.baseName());
       
   101     mAudioItem->setAdditionalText("00:00");
       
   102     mAudioItem->setTextAlignment(Qt::AlignLeft);
       
   103     this->repolish();
       
   104 }
       
   105 
       
   106 //---------------------------------------------------------------
       
   107 //UniViewerBodyWidget::setVideo
       
   108 // @see header file
       
   109 //---------------------------------------------------------------
       
   110 void UniViewerBodyWidget::setVideo(QString videofile)
       
   111 {
       
   112     Q_UNUSED(videofile)
       
   113 }
       
   114 
       
   115 //---------------------------------------------------------------
       
   116 //UniViewerBodyWidget::resizeEvent
       
   117 // @see header file
       
   118 //---------------------------------------------------------------
       
   119 void UniViewerBodyWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
       
   120 {
       
   121     Q_UNUSED(event)
       
   122 
       
   123     HbMainWindow *mainWindow = this->mainWindow();
       
   124     if (mainWindow) {
       
   125         qreal screenWidth = 0.0;
       
   126         qreal screenHeight = 0.0;
       
   127         qreal leftMargin = 0.0;
       
   128         qreal rightMargin = 0.0;
       
   129         qreal chromeHeight = 0.0;
       
   130         qreal toolbarHeight = 0.0;
       
   131         qreal iconSize = 0.0;
       
   132         style()->parameter("hb-param-screen-width", screenWidth);
       
   133         style()->parameter("hb-param-screen-height", screenHeight);
       
   134         style()->parameter("hb-param-margin-gene-left", leftMargin);
       
   135         style()->parameter("hb-param-margin-gene-right", rightMargin);
       
   136         style()->parameter("hb-param-widget-chrome-height", chromeHeight);
       
   137         style()->parameter("hb-param-widget-toolbar-height", toolbarHeight);
       
   138         style()->parameter("hb-param-graphic-size-primary-large", iconSize);
       
   139 
       
   140         qreal maxWidth = 0.0;
       
   141         qreal maxHeight = 0.0;
       
   142 
       
   143         if (mainWindow->orientation() == Qt::Horizontal) {
       
   144             if (mIconItem) {
       
   145                 if (mHasText) {
       
   146                     maxWidth = (screenWidth / 2) - leftMargin
       
   147                         - HbDeviceProfile::profile(mIconItem).unitValue();
       
   148                 }
       
   149                 else {
       
   150                     maxWidth = screenWidth - leftMargin - rightMargin;
       
   151                 }
       
   152                 maxHeight = screenHeight - chromeHeight - toolbarHeight;
       
   153             }
       
   154             if (mAudioItem) {
       
   155                 mAudioItem->setStretched(true);
       
   156             }
       
   157         }
       
   158         else if (mainWindow->orientation() == Qt::Vertical) {
       
   159             if (mIconItem) {
       
   160                 maxWidth = screenWidth - leftMargin - rightMargin;
       
   161                 maxHeight = screenHeight - chromeHeight - toolbarHeight;
       
   162             }
       
   163             if (mAudioItem) {
       
   164                 mAudioItem->setStretched(false);
       
   165             }
       
   166         }
       
   167 
       
   168         if (mIconItem) {
       
   169             qreal imageWidth = mIconItem->icon().defaultSize().width();
       
   170             qreal imageHeight = mIconItem->icon().defaultSize().height();
       
   171 
       
   172             qreal widthToSet = 0.0;
       
   173             qreal heightToSet = 0.0;
       
   174 
       
   175             if (imageWidth < iconSize) {
       
   176                 widthToSet = iconSize;
       
   177                 heightToSet = iconSize;
       
   178             }
       
   179             else if (imageWidth <= maxWidth) {
       
   180                 // resize not needed
       
   181                 widthToSet = imageWidth;
       
   182                 heightToSet = qMin(imageHeight, maxHeight);
       
   183             }
       
   184             else {
       
   185                 // resize needed, keep aspect-ratio and resize
       
   186                 widthToSet = maxWidth;
       
   187                 heightToSet = maxWidth * (imageHeight / imageWidth);
       
   188                 heightToSet = qMin(heightToSet, maxHeight);
       
   189 
       
   190             }
       
   191             if (heightToSet == maxHeight) {
       
   192                 widthToSet = heightToSet * (imageWidth / imageHeight);
       
   193             }
       
   194 
       
   195             mIconItem->setMinimumWidth(widthToSet);
       
   196             mIconItem->setMinimumHeight(heightToSet);
       
   197             mIconItem->setMaximumWidth(widthToSet);
       
   198             mIconItem->setMaximumHeight(heightToSet);
       
   199         }
       
   200     }
       
   201 }
       
   202 
       
   203 //---------------------------------------------------------------
       
   204 //UniViewerBodyWidget::longPressed
       
   205 // @see header file
       
   206 //---------------------------------------------------------------
       
   207 void UniViewerBodyWidget::longPressed(QPointF position)
       
   208 {
       
   209     HbMenu* menu = new HbMenu;
       
   210     menu->addAction(tr("Open"), this, SLOT(openMedia()));
       
   211     menu->addAction(tr("View details"), this, SLOT(viewDetails()));
       
   212     menu->exec(position);
       
   213     delete menu;
       
   214 }
       
   215 
       
   216 //---------------------------------------------------------------
       
   217 //UniViewerBodyWidget::openMedia
       
   218 // @see header file
       
   219 //---------------------------------------------------------------
       
   220 void UniViewerBodyWidget::openMedia()
       
   221 {
       
   222     //open corresponding viewer app.
       
   223 }
       
   224 
       
   225 //---------------------------------------------------------------
       
   226 //UniViewerBodyWidget::viewDetails
       
   227 // @see header file
       
   228 //---------------------------------------------------------------
       
   229 void UniViewerBodyWidget::viewDetails()
       
   230 {
       
   231     //open details view.
       
   232 }
       
   233 
       
   234 //---------------------------------------------------------------
       
   235 //UniViewerBodyWidget::setTextContent
       
   236 // @see header file
       
   237 //---------------------------------------------------------------
       
   238 void UniViewerBodyWidget::setTextContent(QString text)
       
   239 {
       
   240     text.replace('\n', QChar::LineSeparator);
       
   241     mTextItem->setText(text);
       
   242     setHasText(true);
       
   243 }
       
   244 
       
   245 //---------------------------------------------------------------
       
   246 // UniViewerBodyWidget::hasText
       
   247 // @see header file
       
   248 //---------------------------------------------------------------
       
   249 bool UniViewerBodyWidget::hasText()
       
   250 {
       
   251     return mHasText;
       
   252 }
       
   253 
       
   254 //---------------------------------------------------------------
       
   255 // UniViewerBodyWidget::setHasText
       
   256 // @see header file
       
   257 //---------------------------------------------------------------
       
   258 void UniViewerBodyWidget::setHasText(bool text)
       
   259 {
       
   260     mHasText = text;
       
   261 }
       
   262 
       
   263 //---------------------------------------------------------------
       
   264 // UniViewerBodyWidget::setSlideContents
       
   265 // @see header file
       
   266 //---------------------------------------------------------------
       
   267 void UniViewerBodyWidget::setSlideContents(UniMessageInfoList objList, QString slideString)
       
   268 {
       
   269     Q_UNUSED(slideString)
       
   270 
       
   271     int count = objList.count();
       
   272 
       
   273     for (int a = 0; a < count; ++a) {
       
   274         UniMessageInfo* info = objList.at(a);
       
   275         QString type = info->mimetype();
       
   276 
       
   277         if (type.contains("video")) {
       
   278             setVideo(info->path());
       
   279         }
       
   280         else if (type.contains("audio")) {
       
   281             setAudio(info->path());
       
   282         }
       
   283         if (type.contains("image")) {
       
   284             setImage(info->path());
       
   285         }
       
   286         else if (type.contains("text")) {
       
   287             QFile file(info->path());
       
   288             file.open(QIODevice::ReadOnly);
       
   289             QString textContent(file.readAll());
       
   290             setTextContent(textContent);
       
   291         }
       
   292         delete info;
       
   293     }
       
   294 }
       
   295 
       
   296 //---------------------------------------------------------------
       
   297 //UniViewerBodyWidget :: clearContent
       
   298 // @see header file
       
   299 //---------------------------------------------------------------
       
   300 void UniViewerBodyWidget::clearContent()
       
   301 {
       
   302     // delete the temp items(pixmap) & clear permanent items(text)
       
   303     if (mIconItem) {
       
   304         mIconItem->setParent(NULL);
       
   305         delete mIconItem;
       
   306         mIconItem = NULL;
       
   307     }
       
   308 
       
   309     if (mAudioItem) {
       
   310         mAudioItem->setParent(NULL);
       
   311         delete mAudioItem;
       
   312         mAudioItem = NULL;
       
   313     }
       
   314 
       
   315     mTextItem->setText(QString());
       
   316     setHasText(false);
       
   317 }
       
   318 
       
   319 // EOF