emailuis/nmailuiwidgets/src/nmattachmentlistitem.cpp
branchRCL_3
changeset 24 d189ee25cf9d
equal deleted inserted replaced
23:dcf0eedfc1a3 24:d189ee25cf9d
       
     1 /*
       
     2 * Copyright (c) 2010 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: Attachment list item widget
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmailuiwidgetsheaders.h"
       
    19 
       
    20 static const QString FILE_PATH_WIDGETML = ":nmattachmentlistitem.widgetml";
       
    21 static const QString FILE_PATH_CSS = ":nmattachmentlistitem.css";
       
    22 static const QString LIST_ITEM_BG_FRAME_NORMAL ("qtg_fr_list_normal");
       
    23 static const QString LIST_ITEM_BG_FRAME_PRESSED("qtg_fr_list_pressed");
       
    24 
       
    25 static const int PROGRESSBAR_MIN = 0; 
       
    26 static const int PROGRESSBAR_MAX = 100;
       
    27 static const int PROGRESSBAR_HIDE_COUNTDOWN = 500;
       
    28 
       
    29 /*!
       
    30  @nmailuiwidgets
       
    31  \class NmAttachmentListItem
       
    32 
       
    33  \brief The NmAttachmentListItem widget provides for showing a single attachment with file size and
       
    34         download progress in the attachment list.
       
    35 
       
    36 
       
    37 
       
    38  */
       
    39 
       
    40 
       
    41 /*!
       
    42     Constructs a new NmAttachmentListItem with \a parent.
       
    43  */
       
    44 NmAttachmentListItem::NmAttachmentListItem(QGraphicsItem *parent)
       
    45     : HbWidget( parent ),
       
    46       mFileNameText(NULL),
       
    47       mFileSizeText(NULL),
       
    48       mProgressBar(NULL),
       
    49       mBackGround(NULL)
       
    50 {
       
    51     NM_FUNCTION;
       
    52     
       
    53     init( );
       
    54 	
       
    55     // Informs GestureFramework that NmAttachmentListItem widget is interested 
       
    56     // Tap gesture and TapAndHold gesture.
       
    57     grabGesture(Qt::TapGesture);
       
    58     HbEffect::add("mailAttachmentWidget", "listviewitem_press", "pressed");
       
    59     HbEffect::add("mailAttachmentWidget", "listviewitem_release", "released");
       
    60 }
       
    61 
       
    62 /*!
       
    63     Setter for items text color override. This fucntion can be used
       
    64     if theme background is not used and text needs to be shown in diferent color.
       
    65  */
       
    66 void NmAttachmentListItem::setTextColor(const QColor color)
       
    67 {
       
    68     NM_FUNCTION;
       
    69     
       
    70     mTextColor=color;
       
    71     if (mTextColor.isValid() && mFileNameText){
       
    72         mFileNameText->setTextColor(mTextColor);
       
    73     }    
       
    74 }
       
    75 
       
    76 /*!
       
    77     Destructor.
       
    78  */
       
    79 NmAttachmentListItem::~NmAttachmentListItem( )
       
    80 {
       
    81     NM_FUNCTION;
       
    82     
       
    83     HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML);
       
    84     HbStyleLoader::unregisterFilePath(FILE_PATH_CSS);
       
    85 }
       
    86 
       
    87 /*!
       
    88     Set the text to be displayed in the file name item.
       
    89  */
       
    90 void NmAttachmentListItem::setFileNameText(const QString &fileName)
       
    91 {
       
    92     NM_FUNCTION;
       
    93     
       
    94     if (mFileNameText){
       
    95         if (mTextColor.isValid()){
       
    96             mFileNameText->setTextColor(mTextColor);
       
    97         }
       
    98         mFileNameText->setTextWrapping(Hb::TextNoWrap);
       
    99         mFileNameText->setText(fileName);    
       
   100     }
       
   101 }
       
   102 
       
   103 /*!
       
   104     Set the text to be displayed in the file size item
       
   105  */
       
   106 void NmAttachmentListItem::setFileSizeText(const QString &fileSize)
       
   107 {
       
   108     NM_FUNCTION;
       
   109     
       
   110     if (mFileSizeText){
       
   111         if (mTextColor.isValid()){
       
   112             mFileSizeText->setTextColor(mTextColor);
       
   113         }
       
   114         mFileSizeText->setTextWrapping(Hb::TextNoWrap);
       
   115         mFileSizeText->setText(fileSize);
       
   116     }
       
   117 }
       
   118 
       
   119 /*!
       
   120     Set the download progress bar value (0-100)%, if value is 0 progress bar is hidden
       
   121  */
       
   122 void NmAttachmentListItem::setProgressBarValue(const int value)
       
   123 {
       
   124     NM_FUNCTION;
       
   125     
       
   126     //first check if value is 0 or below -> hide progressbar
       
   127     if ( 0 >= value ){
       
   128         removeProgressBar();
       
   129         return;
       
   130     }
       
   131 
       
   132     if ( !mProgressBar ){
       
   133         mProgressBar = new HbProgressBar(this); 
       
   134         mProgressBar->setObjectName("attachmentlistitem_progress");
       
   135         mProgressBar->setRange(PROGRESSBAR_MIN,PROGRESSBAR_MAX);
       
   136         HbStyle::setItemName( mProgressBar, "progressbar" );
       
   137         repolish();
       
   138     }
       
   139     mProgressBar->setProgressValue(value);
       
   140     
       
   141     //start hiding count down
       
   142     if(PROGRESSBAR_MAX <= value){
       
   143         hideProgressBar();
       
   144     }
       
   145 }
       
   146 
       
   147 /*!
       
   148     Get the download progress bar value
       
   149 */
       
   150 int NmAttachmentListItem::progressBarValue() const
       
   151 {
       
   152     NM_FUNCTION;
       
   153     
       
   154     int ret = 0;
       
   155     if ( mProgressBar ){
       
   156         ret = mProgressBar->progressValue();
       
   157     }
       
   158 
       
   159     return ret;
       
   160 }
       
   161 
       
   162 /*!
       
   163     Hides progress bar, used if download is cancelled before 100 precent is reached
       
   164 */
       
   165 void NmAttachmentListItem::hideProgressBar()
       
   166 {
       
   167     NM_FUNCTION;
       
   168     
       
   169     QTimer::singleShot(PROGRESSBAR_HIDE_COUNTDOWN,this, SLOT(removeProgressBar()));
       
   170 }
       
   171 
       
   172 /*!
       
   173     Initialize
       
   174 */
       
   175 void NmAttachmentListItem::init( )
       
   176 {
       
   177     NM_FUNCTION;
       
   178     
       
   179     constructUi();
       
   180 
       
   181     //set default values
       
   182     setFlag(QGraphicsItem::ItemIsFocusable);
       
   183     setFlag(QGraphicsItem::ItemIsSelectable);
       
   184 }
       
   185 
       
   186 /*!
       
   187     Constructs the UI, sets style itemnames etc.
       
   188 */
       
   189 void NmAttachmentListItem::constructUi()
       
   190 {
       
   191     NM_FUNCTION;
       
   192     
       
   193     //background
       
   194     QScopedPointer<HbFrameItem> backGround(new HbFrameItem(this));
       
   195     backGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
       
   196     backGround->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
       
   197     setBackgroundItem(backGround.data());
       
   198     // ownership was transferred to base class
       
   199     mBackGround = backGround.take();
       
   200     
       
   201     
       
   202     //construct default ui.    
       
   203     HbStyleLoader::registerFilePath(FILE_PATH_WIDGETML);
       
   204     HbStyleLoader::registerFilePath(FILE_PATH_CSS);
       
   205     
       
   206     
       
   207     QScopedPointer<HbTextItem> fileNameText(new HbTextItem(this));
       
   208     fileNameText->setObjectName("nmattachmentlistitem_filenametext");
       
   209     HbStyle::setItemName( fileNameText.data(), "filename" );  
       
   210     fileNameText->setElideMode(Qt::ElideRight);
       
   211     
       
   212     
       
   213     QScopedPointer<HbTextItem> fileSizeText(new HbTextItem(this));
       
   214     fileSizeText->setObjectName("nmattachmentlistitem_filenamesize");
       
   215     HbStyle::setItemName( fileSizeText.data(), "filesize" );
       
   216     fileSizeText->setElideMode(Qt::ElideNone);
       
   217     
       
   218     // ownership transferred to this object
       
   219     mFileSizeText = fileSizeText.take(); 
       
   220     mFileNameText = fileNameText.take();
       
   221 }
       
   222 
       
   223 
       
   224 /*!
       
   225     Hides the download progress bar
       
   226  */
       
   227 void NmAttachmentListItem::removeProgressBar()
       
   228 {
       
   229     NM_FUNCTION;
       
   230     
       
   231 	if ( mProgressBar ){
       
   232 	    HbStyle::setItemName( mProgressBar, "" );
       
   233 	    mProgressBar->deleteLater();
       
   234 	    mProgressBar = 0;
       
   235 	    repolish();
       
   236 	}
       
   237 }
       
   238 
       
   239 
       
   240 /*!
       
   241     This function handles gestures
       
   242  */
       
   243 
       
   244 void NmAttachmentListItem::gestureEvent(QGestureEvent *event)
       
   245 {
       
   246     NM_FUNCTION;
       
   247     
       
   248     if (HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
       
   249         switch(tap->tapStyleHint()) {
       
   250         case HbTapGesture::Tap: 
       
   251             {
       
   252                 Qt::GestureState state = tap->state();
       
   253                 HbInstantFeedback::play(HbFeedback::Basic);
       
   254                 setPressed(true);
       
   255                 if (state == Qt::GestureFinished) {
       
   256                     emit itemActivated();
       
   257                     setPressed(false);
       
   258                 }
       
   259                 else if (state == Qt::GestureCanceled) {
       
   260                     setPressed(false);
       
   261                 }
       
   262              }
       
   263              break;
       
   264             
       
   265          case HbTapGesture::TapAndHold:
       
   266              {
       
   267                  emit itemLongPressed(event->mapToGraphicsScene(tap->position()));
       
   268                  setPressed(false);
       
   269              }    
       
   270              break;
       
   271         }
       
   272     }
       
   273     else {
       
   274            HbWidget::gestureEvent(event);
       
   275     }
       
   276 }
       
   277 
       
   278 /*!
       
   279     Sets the effect of the item when tapping it
       
   280 */
       
   281 void NmAttachmentListItem::setPressed(bool pressed)
       
   282 {
       
   283     if (pressed) {
       
   284         setProperty("state", "pressed");
       
   285         mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_PRESSED);
       
   286         HbEffect::cancel(mBackGround, "released");
       
   287         HbEffect::start(mBackGround, "mailAttachmentWidget", "pressed");
       
   288 
       
   289     }
       
   290     else {
       
   291         setProperty("state", "normal");
       
   292         mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
       
   293         HbEffect::cancel(mBackGround, "pressed");
       
   294         HbEffect::start(mBackGround, "mailAttachmentWidget", "released");
       
   295     }    
       
   296 }