messagingapp/msgui/unifiedviewer/src/univiewerbodywidget.cpp
branchRCL_3
changeset 26 ebe688cedc25
equal deleted inserted replaced
25:fa1df4b99609 26: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 body of the viewer
       
    15  *
       
    16  */
       
    17 
       
    18 #include "univiewerbodywidget.h"
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <QFile>
       
    22 #include <HbTextItem>
       
    23 #include <HbMainWindow>
       
    24 #include <HbIconItem>
       
    25 
       
    26 // USER INCLUDES
       
    27 #include "univiewertextitem.h"
       
    28 #include "univiewerpixmapwidget.h"
       
    29 #include "univieweraudiowidget.h"
       
    30 
       
    31 // LOCAL CONSTANTS
       
    32 const QString IMAGE_MIMETYPE("image");
       
    33 const QString AUDIO_MIMETYPE("audio");
       
    34 const QString VIDEO_MIMETYPE("video");
       
    35 const QString TEXT_MIMETYPE("text");
       
    36 
       
    37 static const char VIDEO_ICON[] = "qtg_large_video_player";
       
    38 
       
    39 //---------------------------------------------------------------
       
    40 //UniViewerBodyWidget::UniViewerBodyWidget
       
    41 // @see header file
       
    42 //---------------------------------------------------------------
       
    43 UniViewerBodyWidget::UniViewerBodyWidget(QGraphicsItem *parent) :
       
    44     HbWidget(parent), mHasText(false), mHasPixmap(false), mTextItem(0), mSlideCounter(0),
       
    45         mPixmapItem(0), mAudioItem(0), mOverlayItem(0)
       
    46 {
       
    47     this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
       
    48 }
       
    49 
       
    50 //---------------------------------------------------------------
       
    51 //UniViewerBodyWidget::~UniViewerBodyWidget
       
    52 // @see header file
       
    53 //---------------------------------------------------------------
       
    54 UniViewerBodyWidget::~UniViewerBodyWidget()
       
    55 {
       
    56 }
       
    57 
       
    58 //---------------------------------------------------------------
       
    59 // UniViewerBodyWidget::setHasText
       
    60 // @see header file
       
    61 //---------------------------------------------------------------
       
    62 void UniViewerBodyWidget::setHasText(bool text)
       
    63 {
       
    64     mHasText = text;
       
    65 }
       
    66 
       
    67 //---------------------------------------------------------------
       
    68 // UniViewerBodyWidget::hasText
       
    69 // @see header file
       
    70 //---------------------------------------------------------------
       
    71 bool UniViewerBodyWidget::hasText()
       
    72 {
       
    73     return mHasText;
       
    74 }
       
    75 
       
    76 //---------------------------------------------------------------
       
    77 // UniViewerBodyWidget::setHasPixmap
       
    78 // @see header file
       
    79 //---------------------------------------------------------------
       
    80 void UniViewerBodyWidget::setHasPixmap(bool pixmap)
       
    81 {
       
    82     mHasPixmap = pixmap;
       
    83 }
       
    84 
       
    85 //---------------------------------------------------------------
       
    86 // UniViewerBodyWidget::hasPixmap
       
    87 // @see header file
       
    88 //---------------------------------------------------------------
       
    89 bool UniViewerBodyWidget::hasPixmap()
       
    90 {
       
    91     return mHasPixmap;
       
    92 }
       
    93 
       
    94 //---------------------------------------------------------------
       
    95 // UniViewerBodyWidget::setSlideContents
       
    96 // @see header file
       
    97 //---------------------------------------------------------------
       
    98 void UniViewerBodyWidget::setSlideContents(UniMessageInfoList objList, QString slideString)
       
    99 {
       
   100     if (!slideString.isEmpty()) {
       
   101         setSlideCounter(slideString);
       
   102     }
       
   103 
       
   104     int count = objList.count();
       
   105     for (int a = 0; a < count; ++a) {
       
   106         UniMessageInfo* info = objList.at(a);
       
   107         QString mimeType = info->mimetype();
       
   108 
       
   109         if (mimeType.contains(TEXT_MIMETYPE)) {
       
   110             QFile file(info->path());
       
   111             if (file.open(QIODevice::ReadOnly)) {
       
   112                 QString textContent;
       
   113                 QByteArray textArray;
       
   114                 textArray = file.readAll();
       
   115                 char *data = new char[textArray.size()+1];
       
   116                 strcpy(data,textArray.data());
       
   117                 //This is needed since MMS text content 
       
   118                 //is stored in UTF8 format
       
   119                 textContent = textContent.fromUtf8(data,strlen(data));
       
   120                 file.close();
       
   121                 delete []data;
       
   122                 setText(textContent);
       
   123             }
       
   124         }
       
   125         else if (mimeType.contains(AUDIO_MIMETYPE)) {
       
   126             setAudio(info);
       
   127         }
       
   128         else if (mimeType.contains(VIDEO_MIMETYPE)) {
       
   129             setVideo(info);
       
   130         }
       
   131         else if (mimeType.contains(IMAGE_MIMETYPE)) {
       
   132             setPixmap(info);
       
   133         }
       
   134 
       
   135         delete info;
       
   136     }
       
   137 }
       
   138 
       
   139 //---------------------------------------------------------------
       
   140 // UniViewerBodyWidget :: clearContent
       
   141 // @see header file
       
   142 //---------------------------------------------------------------
       
   143 void UniViewerBodyWidget::clearContent()
       
   144 {
       
   145     // delete the temp items(pixmap) & clear permanent items(text)
       
   146     if (mPixmapItem) {
       
   147         mPixmapItem->setParent(NULL);
       
   148         delete mPixmapItem;
       
   149         mPixmapItem = NULL;
       
   150     }
       
   151 
       
   152     if (mAudioItem) {
       
   153         mAudioItem->setParent(NULL);
       
   154         delete mAudioItem;
       
   155         mAudioItem = NULL;
       
   156     }
       
   157 
       
   158     if (mTextItem) {
       
   159         mTextItem->setParent(NULL);
       
   160         delete mTextItem;
       
   161         mTextItem = NULL;
       
   162     }
       
   163 
       
   164     if (mOverlayItem) {
       
   165         mOverlayItem->setParent(NULL);
       
   166         delete mOverlayItem;
       
   167         mOverlayItem = NULL;
       
   168     }
       
   169 
       
   170     setHasText(false);
       
   171     setHasPixmap(false);
       
   172     repolish();
       
   173 }
       
   174 
       
   175 //---------------------------------------------------------------
       
   176 // UniViewerBodyWidget::setText
       
   177 // @see header file
       
   178 //---------------------------------------------------------------
       
   179 void UniViewerBodyWidget::setText(QString text)
       
   180 {
       
   181     setHasText(true);
       
   182 
       
   183     if (!mTextItem) {
       
   184         mTextItem = new UniViewerTextItem(this);
       
   185         mTextItem->setObjectName("textItem");
       
   186         HbStyle::setItemName(mTextItem, "textItem");
       
   187         connect(mTextItem, SIGNAL(sendMessage(const QString&)), this,
       
   188             SIGNAL(sendMessage(const QString&)));
       
   189     }
       
   190     mTextItem->hide();
       
   191     text.replace(QChar::ParagraphSeparator, QChar::LineSeparator);
       
   192     text.replace('\r', QChar::LineSeparator);
       
   193     mTextItem->setText(text);
       
   194 
       
   195     this->repolish();
       
   196 }
       
   197 
       
   198 //---------------------------------------------------------------
       
   199 //UniViewerBodyWidget::sizeHint
       
   200 // @see header file
       
   201 //---------------------------------------------------------------
       
   202 QSizeF UniViewerBodyWidget::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
       
   203 {
       
   204     QSizeF szHint = HbWidget::sizeHint(which, constraint);
       
   205 
       
   206     HbMainWindow *mainWindow = this->mainWindow();
       
   207     if (!mainWindow) {
       
   208         return szHint;
       
   209     }
       
   210 
       
   211     qreal screenWidth = 0.0;
       
   212     qreal screenHeight = 0.0;
       
   213     qreal leftMargin = 0.0;
       
   214     qreal rightMargin = 0.0;
       
   215     qreal chromeHeight = 0.0;
       
   216     qreal toolbarHeight = 0.0;
       
   217     qreal iconSize = 0.0;
       
   218     qreal spacing = 0.0;
       
   219     qreal unitSize = HbDeviceProfile::profile(mPixmapItem).unitValue();
       
   220     style()->parameter("hb-param-screen-width", screenWidth);
       
   221     style()->parameter("hb-param-screen-height", screenHeight);
       
   222     style()->parameter("hb-param-margin-gene-left", leftMargin);
       
   223     style()->parameter("hb-param-margin-gene-right", rightMargin);
       
   224     style()->parameter("hb-param-widget-chrome-height", chromeHeight);
       
   225     style()->parameter("hb-param-widget-toolbar-height", toolbarHeight);
       
   226     style()->parameter("hb-param-graphic-size-primary-large", iconSize);
       
   227     style()->parameter("hb-param-margin-gene-middle-vertical", spacing);
       
   228 
       
   229     qreal maxWidth = 0.0;
       
   230     qreal maxHeight = 0.0;
       
   231 
       
   232     // Calculate max height & max width.
       
   233     if (mainWindow->orientation() == Qt::Horizontal) {
       
   234         qreal temp;
       
   235         temp = screenWidth;
       
   236         screenWidth = screenHeight;
       
   237         screenHeight = temp;
       
   238 
       
   239         if (mPixmapItem && mHasText) {
       
   240             maxWidth = (screenWidth / 2) - leftMargin - unitSize;
       
   241         }
       
   242         else {
       
   243             maxWidth = screenWidth - leftMargin - rightMargin;
       
   244         }
       
   245         maxHeight = screenHeight - chromeHeight - toolbarHeight;
       
   246 
       
   247         if (mAudioItem) {
       
   248             mAudioItem->setStretched(true);
       
   249         }
       
   250     }
       
   251     else if (mainWindow->orientation() == Qt::Vertical) {
       
   252         maxWidth = screenWidth - leftMargin - rightMargin;
       
   253         maxHeight = screenHeight - chromeHeight - toolbarHeight;
       
   254 
       
   255         if (mAudioItem) {
       
   256             mAudioItem->setStretched(false);
       
   257         }
       
   258     }
       
   259 
       
   260     // Slide Counter
       
   261     QSizeF slideCounterSize(0, 0);
       
   262     if (mSlideCounter) {
       
   263         slideCounterSize = mSlideCounter->effectiveSizeHint(which, constraint);
       
   264         mSlideCounter->show();
       
   265     }
       
   266     // Audio Item
       
   267     QSizeF audioSize(0, 0);
       
   268     if (mAudioItem) {
       
   269         audioSize = mAudioItem->effectiveSizeHint(which, constraint);
       
   270         mAudioItem->show();
       
   271     }
       
   272 
       
   273     // Text Item
       
   274     QSizeF textSize(0, 0);
       
   275     if (mTextItem) {
       
   276         textSize = mTextItem->effectiveSizeHint(which, constraint);
       
   277         mTextItem->show();
       
   278     }
       
   279 
       
   280     // Pixmap Item
       
   281     QSizeF pixmapSize(0, 0);
       
   282     if (mPixmapItem) {
       
   283         qreal imageWidth = mPixmapItem->icon().defaultSize().width();
       
   284         qreal imageHeight = mPixmapItem->icon().defaultSize().height();
       
   285 
       
   286         qreal widthToSet = 0.0;
       
   287         qreal heightToSet = 0.0;
       
   288 
       
   289         if (imageWidth < iconSize) {
       
   290             widthToSet = iconSize;
       
   291             heightToSet = iconSize;
       
   292         }
       
   293         else if (imageWidth <= maxWidth) {
       
   294             // resize not needed
       
   295             widthToSet = imageWidth;
       
   296             heightToSet = qMin(imageHeight, maxHeight);
       
   297         }
       
   298         else {
       
   299             // resize needed, keep aspect-ratio and resize
       
   300             widthToSet = maxWidth;
       
   301             heightToSet = maxWidth * (imageHeight / imageWidth);
       
   302             heightToSet = qMin(heightToSet, maxHeight);
       
   303 
       
   304         }
       
   305         if (heightToSet == maxHeight) {
       
   306             widthToSet = heightToSet * (imageWidth / imageHeight);
       
   307         }
       
   308 
       
   309         pixmapSize.setHeight(heightToSet);
       
   310         pixmapSize.setWidth(widthToSet);
       
   311         mPixmapItem->setPreferredSize(pixmapSize);
       
   312         mPixmapItem->show();
       
   313     }
       
   314 
       
   315     if (mOverlayItem) {
       
   316         mOverlayItem->show();
       
   317     }
       
   318 
       
   319     // Calculate the size hint to be returned.
       
   320     szHint.setHeight(0);
       
   321 
       
   322     if (!slideCounterSize.isNull()) {
       
   323         szHint.rheight() += (slideCounterSize.height() + spacing);
       
   324     }
       
   325     if (!audioSize.isNull()) {
       
   326         szHint.rheight() += (audioSize.height() + spacing);
       
   327     }
       
   328 
       
   329     if (mainWindow->orientation() == Qt::Horizontal) {
       
   330         qreal remainingHeight = qMax(pixmapSize.height(), textSize.height());
       
   331         if (remainingHeight > 0.0) {
       
   332             szHint.rheight() += (remainingHeight + spacing);
       
   333         }
       
   334     }
       
   335     else if (mainWindow->orientation() == Qt::Vertical) {
       
   336         if (!pixmapSize.isNull()) {
       
   337             szHint.rheight() += (pixmapSize.height() + spacing);
       
   338         }
       
   339         if (!textSize.isNull()) {
       
   340             szHint.rheight() += (textSize.height() + spacing);
       
   341         }
       
   342     }
       
   343     szHint.rheight() = qMax(maxHeight, szHint.height());
       
   344 
       
   345     return szHint;
       
   346 }
       
   347 
       
   348 //---------------------------------------------------------------
       
   349 //UniViewerBodyWidget::setPixmap
       
   350 // @see header file
       
   351 //---------------------------------------------------------------
       
   352 void UniViewerBodyWidget::setPixmap(UniMessageInfo *info)
       
   353 {
       
   354     setHasPixmap(true);
       
   355     //create image item instance
       
   356     if (!mPixmapItem) {
       
   357         mPixmapItem = new UniViewerPixmapWidget(this);
       
   358         HbStyle::setItemName(mPixmapItem, "pixmap");
       
   359     }
       
   360     mPixmapItem->hide();
       
   361     mPixmapItem->populate(info);
       
   362 
       
   363     this->repolish();
       
   364 }
       
   365 
       
   366 //---------------------------------------------------------------
       
   367 //UniViewerBodyWidget::setAudio
       
   368 // @see header file
       
   369 //---------------------------------------------------------------
       
   370 void UniViewerBodyWidget::setAudio(UniMessageInfo *info)
       
   371 {
       
   372     if (!mAudioItem) {
       
   373         mAudioItem = new UniViewerAudioWidget(this);
       
   374         HbStyle::setItemName(mAudioItem, "audioItem");
       
   375     }
       
   376     mAudioItem->hide();
       
   377     mAudioItem->populate(info);
       
   378 
       
   379     this->repolish();
       
   380 }
       
   381 
       
   382 //---------------------------------------------------------------
       
   383 //UniViewerBodyWidget::setVideo
       
   384 // @see header file
       
   385 //---------------------------------------------------------------
       
   386 void UniViewerBodyWidget::setVideo(UniMessageInfo *info)
       
   387 {
       
   388     setHasPixmap(true);
       
   389     //create image item instance
       
   390     if (!mPixmapItem) {
       
   391         mPixmapItem = new UniViewerPixmapWidget(this);
       
   392         HbStyle::setItemName(mPixmapItem, "pixmap");
       
   393         connect(mPixmapItem, SIGNAL(thumbnailFound(bool, UniMessageInfo*)), this,
       
   394             SLOT(onThumbnailFound(bool, UniMessageInfo*)));
       
   395     }
       
   396     mPixmapItem->hide();
       
   397     mPixmapItem->populate(info);
       
   398 
       
   399     this->repolish();
       
   400 }
       
   401 
       
   402 //---------------------------------------------------------------
       
   403 // UniViewerBodyWidget::setSlideCounter
       
   404 // @see header file
       
   405 //---------------------------------------------------------------
       
   406 void UniViewerBodyWidget::setSlideCounter(QString &slideCounter)
       
   407 {
       
   408     if (!mSlideCounter) {
       
   409         mSlideCounter = new HbTextItem(this);
       
   410         HbStyle::setItemName(mSlideCounter, "slideCounter");
       
   411     }
       
   412 
       
   413     mSlideCounter->hide();
       
   414     mSlideCounter->setText(slideCounter);
       
   415 
       
   416     this->repolish();
       
   417 }
       
   418 
       
   419 //---------------------------------------------------------------
       
   420 // UniViewerBodyWidget::setOverlayIcon
       
   421 // @see header file
       
   422 //---------------------------------------------------------------
       
   423 void UniViewerBodyWidget::setOverlayIcon(const QString &iconName)
       
   424 {
       
   425     if (!mOverlayItem) {
       
   426         mOverlayItem = new HbIconItem(this);
       
   427         HbStyle::setItemName(mOverlayItem, "overlayItem");
       
   428     }
       
   429 
       
   430     mOverlayItem->hide();
       
   431     mOverlayItem->setIconName(iconName);
       
   432 
       
   433     this->repolish();
       
   434 }
       
   435 
       
   436 //---------------------------------------------------------------
       
   437 // UniViewerBodyWidget::onThumbnailFound
       
   438 // @see header file
       
   439 //---------------------------------------------------------------
       
   440 void UniViewerBodyWidget::onThumbnailFound(bool result, UniMessageInfo *info)
       
   441 {
       
   442     if (result) {
       
   443         // Thumbnail generation success
       
   444         setOverlayIcon(VIDEO_ICON);
       
   445     }
       
   446     else {
       
   447         // Show video content in audio widget.
       
   448         setAudio(info);
       
   449 
       
   450         // Remove the pixmap widget.
       
   451         setHasPixmap(false);
       
   452         if (mPixmapItem) {
       
   453             HbStyle::setItemName(mPixmapItem, "");
       
   454             mPixmapItem->hide();
       
   455         }
       
   456     }
       
   457 }
       
   458 
       
   459 // EOF