emailuis/nmailuiwidgets/src/nmattachmentlistitem.cpp
changeset 72 64e38f08e49c
parent 65 478bc57ad291
equal deleted inserted replaced
65:478bc57ad291 72:64e38f08e49c
    22 static const QString LIST_ITEM_BG_FRAME_NORMAL ("qtg_fr_list_normal");
    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");
    23 static const QString LIST_ITEM_BG_FRAME_PRESSED("qtg_fr_list_pressed");
    24 
    24 
    25 static const int PROGRESSBAR_MIN = 0; 
    25 static const int PROGRESSBAR_MIN = 0; 
    26 static const int PROGRESSBAR_MAX = 100;
    26 static const int PROGRESSBAR_MAX = 100;
    27 static const int PROGRESSBAR_HIDE_COUNTDOWN = 500;
    27 static const int PROGRESSBAR_HIDE_COUNTDOWN = 500; // Milliseconds
    28 
    28 
    29 /*!
    29 
    30  @nmailuiwidgets
    30 /*!
    31  \class NmAttachmentListItem
    31     @nmailuiwidgets
    32 
    32 
    33  \brief The NmAttachmentListItem widget provides for showing a single attachment with file size and
    33     \class NmAttachmentListItem
    34         download progress in the attachment list.
    34 
    35 
    35     \brief  NmAttachmentListItem is a widget used for displaying a single
    36 
    36             attachment with the attachment file size and download progress in
    37 
    37             the attachment list.
    38  */
    38 */
    39 
    39 
    40 
    40 
    41 /*!
    41 /*!
    42     Constructs a new NmAttachmentListItem with \a parent.
    42     Constructs a new NmAttachmentListItem with \a parent.
    43  */
    43 */
    44 NmAttachmentListItem::NmAttachmentListItem(QGraphicsItem *parent)
    44 NmAttachmentListItem::NmAttachmentListItem(QGraphicsItem *parent)
    45     : HbWidget( parent ),
    45 : HbWidget( parent ),
    46       mFileNameText(NULL),
    46   mFileNameText(NULL),
    47       mFileSizeText(NULL),
    47   mFileSizeText(NULL),
    48       mProgressBar(NULL),
    48   mProgressBar(NULL),
    49       mBackGround(NULL)
    49   mBackground(NULL)
    50 {
    50 {
    51     NM_FUNCTION;
    51     NM_FUNCTION;
    52     
    52 
    53     init( );
    53     init();
    54 	
    54 	
    55     // Informs GestureFramework that NmAttachmentListItem widget is interested 
    55     // Informs GestureFramework that NmAttachmentListItem widget is interested 
    56     // Tap gesture and TapAndHold gesture.
    56     // in Tap and TapAndHold gestures.
    57     grabGesture(Qt::TapGesture);
    57     grabGesture(Qt::TapGesture);
    58     HbEffect::add("mailAttachmentWidget", "listviewitem_press", "pressed");
    58     HbEffect::add("mailAttachmentWidget", "listviewitem_press", "pressed");
    59     HbEffect::add("mailAttachmentWidget", "listviewitem_release", "released");
    59     HbEffect::add("mailAttachmentWidget", "listviewitem_release", "released");
    60 }
    60 }
    61 
    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 
    62 
    76 /*!
    63 /*!
    77     Destructor.
    64     Destructor.
    78  */
    65 */
    79 NmAttachmentListItem::~NmAttachmentListItem( )
    66 NmAttachmentListItem::~NmAttachmentListItem()
    80 {
    67 {
    81     NM_FUNCTION;
    68     NM_FUNCTION;
    82     
    69     
    83     HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML);
    70     HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML);
    84     HbStyleLoader::unregisterFilePath(FILE_PATH_CSS);
    71     HbStyleLoader::unregisterFilePath(FILE_PATH_CSS);
    85 }
    72 }
    86 
    73 
    87 /*!
    74 
    88     Set the text to be displayed in the file name item.
    75 /*!
    89  */
    76     Setter for items text color override. This method can be used if the theme
       
    77     background is not used and text needs to be shown with a different color.
       
    78 
       
    79     \param color The new test color.
       
    80 */
       
    81 void NmAttachmentListItem::setTextColor(const QColor color)
       
    82 {
       
    83     NM_FUNCTION;
       
    84     
       
    85     mTextColor = color;
       
    86 
       
    87     if (mTextColor.isValid() && mFileNameText) {
       
    88         mFileNameText->setTextColor(mTextColor);
       
    89     }
       
    90 }
       
    91 
       
    92 
       
    93 /*!
       
    94     Sets the background color. This method can be used to override the
       
    95     default background color set by the current theme. Because of the effect,
       
    96     the implementation "smoke and mirrors" but it works.
       
    97 
       
    98     \param color The new background color.
       
    99 */
       
   100 void NmAttachmentListItem::setBackgroundColor(const QColor color)
       
   101 {
       
   102     NM_FUNCTION;
       
   103 
       
   104     if (!color.isValid()) {
       
   105         // Invalid color!
       
   106         return;
       
   107     }
       
   108 
       
   109     mBackgroundColor = color;
       
   110 
       
   111     // The effect color is the opposite color of the background color.
       
   112     QColor effectColor(qAbs(color.red() - 255),
       
   113                        qAbs(color.green() - 255),
       
   114                        qAbs(color.blue() - 255));
       
   115     
       
   116     QPalette palette = QApplication::palette();
       
   117     QBrush brush(effectColor);
       
   118     palette.setBrush(QPalette::Window, effectColor);
       
   119     setPalette(palette);
       
   120     setAutoFillBackground(true);
       
   121 
       
   122     if (mBackground) {
       
   123         // Set the background color for the background item.
       
   124         brush.setColor(mBackgroundColor);
       
   125         palette.setBrush(QPalette::Window, brush);
       
   126         mBackground->setPalette(palette);
       
   127         mBackground->setAutoFillBackground(true);
       
   128         mBackground->frameDrawer().setFrameType(HbFrameDrawer::OnePiece);
       
   129     }
       
   130 
       
   131     repolish();
       
   132 }
       
   133 
       
   134 
       
   135 /*!
       
   136     Sets the text to be displayed in the file name item.
       
   137 
       
   138     \param filename The filename.
       
   139 */
    90 void NmAttachmentListItem::setFileNameText(const QString &fileName)
   140 void NmAttachmentListItem::setFileNameText(const QString &fileName)
    91 {
   141 {
    92     NM_FUNCTION;
   142     NM_FUNCTION;
    93     
   143     
    94     if (mFileNameText){
   144     if (mFileNameText) {
    95         if (mTextColor.isValid()){
   145         if (mTextColor.isValid()) {
    96             mFileNameText->setTextColor(mTextColor);
   146             mFileNameText->setTextColor(mTextColor);
    97         }
   147         }
       
   148 
    98         mFileNameText->setTextWrapping(Hb::TextNoWrap);
   149         mFileNameText->setTextWrapping(Hb::TextNoWrap);
    99         mFileNameText->setText(fileName);    
   150         mFileNameText->setText(fileName);    
   100     }
   151     }
   101 }
   152 }
   102 
   153 
   103 /*!
   154 
   104     Set the text to be displayed in the file size item
   155 /*!
   105  */
   156     Sets the value of the text for displaying the attachment file size.
       
   157     
       
   158     \param fileSize The file size as a string.
       
   159 */
   106 void NmAttachmentListItem::setFileSizeText(const QString &fileSize)
   160 void NmAttachmentListItem::setFileSizeText(const QString &fileSize)
   107 {
   161 {
   108     NM_FUNCTION;
   162     NM_FUNCTION;
   109     
   163     
   110     if (mFileSizeText){
   164     if (mFileSizeText) {
   111         if (mTextColor.isValid()){
   165         if (mTextColor.isValid()) {
   112             mFileSizeText->setTextColor(mTextColor);
   166             mFileSizeText->setTextColor(mTextColor);
   113         }
   167         }
       
   168 
   114         mFileSizeText->setTextWrapping(Hb::TextNoWrap);
   169         mFileSizeText->setTextWrapping(Hb::TextNoWrap);
   115         mFileSizeText->setText(fileSize);
   170         mFileSizeText->setText(fileSize);
   116     }
   171     }
   117 }
   172 }
   118 
   173 
   119 /*!
   174 
   120     Set the download progress bar value (0-100)%, if value is 0 progress bar is hidden
   175 /*!
   121  */
   176     Set the download progress bar value (0 - 100 %). If the given value is 0,
       
   177     the progress bar is hidden.
       
   178 
       
   179     \param value The progress bar value.
       
   180 */
   122 void NmAttachmentListItem::setProgressBarValue(const int value)
   181 void NmAttachmentListItem::setProgressBarValue(const int value)
   123 {
   182 {
   124     NM_FUNCTION;
   183     NM_FUNCTION;
   125     
   184     
   126     //first check if value is 0 or below -> hide progressbar
   185     // First check if the value is 0 or below -> hide progressbar.
   127     if ( 0 >= value ){
   186     if (0 >= value) {
   128         removeProgressBar();
   187         removeProgressBar();
   129         return;
   188         return;
   130     }
   189     }
   131 
   190 
   132     if ( !mProgressBar ){
   191     if (!mProgressBar) {
   133         mProgressBar = new HbProgressBar(this); 
   192         mProgressBar = new HbProgressBar(this); 
   134         mProgressBar->setObjectName("attachmentlistitem_progress");
   193         mProgressBar->setObjectName("attachmentlistitem_progress");
   135         mProgressBar->setRange(PROGRESSBAR_MIN,PROGRESSBAR_MAX);
   194         mProgressBar->setRange(PROGRESSBAR_MIN,PROGRESSBAR_MAX);
   136         HbStyle::setItemName( mProgressBar, "progressbar" );
   195         HbStyle::setItemName(mProgressBar, "progressbar");
   137         repolish();
   196         repolish();
   138     }
   197     }
       
   198 
   139     mProgressBar->setProgressValue(value);
   199     mProgressBar->setProgressValue(value);
   140     
   200     
   141     //start hiding count down
   201     // Start hiding the count down.
   142     if(PROGRESSBAR_MAX <= value){
   202     if (PROGRESSBAR_MAX <= value){
   143         hideProgressBar();
   203         hideProgressBar();
   144     }
   204     }
   145 }
   205 }
   146 
   206 
   147 /*!
   207 
   148     Get the download progress bar value
   208 /*!
       
   209     \return The value of the download progress bar.
   149 */
   210 */
   150 int NmAttachmentListItem::progressBarValue() const
   211 int NmAttachmentListItem::progressBarValue() const
   151 {
   212 {
   152     NM_FUNCTION;
   213     NM_FUNCTION;
   153     
   214     
   154     int ret = 0;
   215     int ret = 0;
   155     if ( mProgressBar ){
   216 
       
   217     if (mProgressBar) {
   156         ret = mProgressBar->progressValue();
   218         ret = mProgressBar->progressValue();
   157     }
   219     }
   158 
   220 
   159     return ret;
   221     return ret;
   160 }
   222 }
   161 
   223 
   162 /*!
   224 
   163     Hides progress bar, used if download is cancelled before 100 precent is reached
   225 /*!
       
   226     Hides the progress bar. Used if the download is cancelled before 100
       
   227     percent is reached.
   164 */
   228 */
   165 void NmAttachmentListItem::hideProgressBar()
   229 void NmAttachmentListItem::hideProgressBar()
   166 {
   230 {
   167     NM_FUNCTION;
   231     NM_FUNCTION;
   168     
   232     
   169     QTimer::singleShot(PROGRESSBAR_HIDE_COUNTDOWN,this, SLOT(removeProgressBar()));
   233     QTimer::singleShot(PROGRESSBAR_HIDE_COUNTDOWN, this, SLOT(removeProgressBar()));
   170 }
   234 }
   171 
   235 
   172 /*!
   236 
   173     Initialize
   237 /*!
   174 */
   238     Displays a visual effect according to the current pressed state.
   175 void NmAttachmentListItem::init( )
   239 
   176 {
   240     \param pressed True, if the item was pressed.
   177     NM_FUNCTION;
   241 */
   178     
   242 void NmAttachmentListItem::setPressed(bool pressed)
   179     constructUi();
   243 {
   180 
   244     if (pressed) {
   181     //set default values
   245         setProperty("state", "pressed");
   182     setFlag(QGraphicsItem::ItemIsFocusable);
   246         mBackground->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_PRESSED);
   183     setFlag(QGraphicsItem::ItemIsSelectable);
   247         HbEffect::cancel(mBackground, "released");
   184 }
   248         HbEffect::start(mBackground, "mailAttachmentWidget", "pressed");
   185 
   249     }
   186 /*!
   250     else {
   187     Constructs the UI, sets style itemnames etc.
   251         setProperty("state", "normal");
   188 */
   252         mBackground->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
   189 void NmAttachmentListItem::constructUi()
   253         HbEffect::cancel(mBackground, "pressed");
   190 {
   254         HbEffect::start(mBackground, "mailAttachmentWidget", "released");
   191     NM_FUNCTION;
   255     }    
   192     
   256 }
   193     //background
   257 
   194     QScopedPointer<HbFrameItem> backGround(new HbFrameItem(this));
   258 
   195     backGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
   259 /*!
   196     backGround->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
   260     Handles the given gesture event.
   197     setBackgroundItem(backGround.data());
   261 
   198     // ownership was transferred to base class
   262     \param event The event to handle.
   199     mBackGround = backGround.take();
   263 */
   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)
   264 void NmAttachmentListItem::gestureEvent(QGestureEvent *event)
   245 {
   265 {
   246     NM_FUNCTION;
   266     NM_FUNCTION;
   247     
   267     
   248     if (HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
   268     if (HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
   249         switch(tap->tapStyleHint()) {
   269         switch (tap->tapStyleHint()) {
   250         case HbTapGesture::Tap: 
   270             case HbTapGesture::Tap: {
   251             {
       
   252                 Qt::GestureState state = tap->state();
   271                 Qt::GestureState state = tap->state();
   253                 HbInstantFeedback::play(HbFeedback::Basic);
   272                 HbInstantFeedback::play(HbFeedback::Basic);
   254                 setPressed(true);
   273                 setPressed(true);
       
   274 
   255                 if (state == Qt::GestureFinished) {
   275                 if (state == Qt::GestureFinished) {
   256                     emit itemActivated();
   276                     emit itemActivated();
   257                     setPressed(false);
   277                     setPressed(false);
   258                 }
   278                 }
   259                 else if (state == Qt::GestureCanceled) {
   279                 else if (state == Qt::GestureCanceled) {
   260                     setPressed(false);
   280                     setPressed(false);
   261                 }
   281                 }
   262              }
   282 
   263              break;
   283                 break;
   264             
   284             }
   265          case HbTapGesture::TapAndHold:
   285             case HbTapGesture::TapAndHold: {
   266              {
   286                 emit itemLongPressed(event->mapToGraphicsScene(tap->position()));
   267                  emit itemLongPressed(event->mapToGraphicsScene(tap->position()));
   287                 setPressed(false);    
   268                  setPressed(false);
   288                 break;
   269              }    
   289             }
   270              break;
   290         } // switch
   271         }
       
   272     }
   291     }
   273     else {
   292     else {
   274            HbWidget::gestureEvent(event);
   293         // Let the HbWidget handle the event.
   275     }
   294         HbWidget::gestureEvent(event);
   276 }
   295     }
   277 
   296 }
   278 /*!
   297 
   279     Sets the effect of the item when tapping it
   298 
   280 */
   299 /*!
   281 void NmAttachmentListItem::setPressed(bool pressed)
   300     Hides and deletes the download progress bar.
   282 {
   301 */
   283     if (pressed) {
   302 void NmAttachmentListItem::removeProgressBar()
   284         setProperty("state", "pressed");
   303 {
   285         mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_PRESSED);
   304     NM_FUNCTION;
   286         HbEffect::cancel(mBackGround, "released");
   305     
   287         HbEffect::start(mBackGround, "mailAttachmentWidget", "pressed");
   306     if (mProgressBar) {
   288 
   307         HbStyle::setItemName(mProgressBar, "");
   289     }
   308         mProgressBar->deleteLater();
   290     else {
   309         mProgressBar = 0;
   291         setProperty("state", "normal");
   310         repolish();
   292         mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
   311     }
   293         HbEffect::cancel(mBackGround, "pressed");
   312 }
   294         HbEffect::start(mBackGround, "mailAttachmentWidget", "released");
   313 
   295     }    
   314 
   296 }
   315 /*!
       
   316     Initializes the widget.
       
   317 */
       
   318 void NmAttachmentListItem::init( )
       
   319 {
       
   320     NM_FUNCTION;
       
   321     
       
   322     constructUi();
       
   323 
       
   324     // Set the default flag values.
       
   325     setFlag(QGraphicsItem::ItemIsFocusable);
       
   326     setFlag(QGraphicsItem::ItemIsSelectable);
       
   327 }
       
   328 
       
   329 
       
   330 /*!
       
   331     Constructs the UI, sets style item names etc.
       
   332 */
       
   333 void NmAttachmentListItem::constructUi()
       
   334 {
       
   335     NM_FUNCTION;
       
   336     
       
   337     // Background
       
   338     QScopedPointer<HbFrameItem> background(new HbFrameItem(this));
       
   339     background->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
       
   340     background->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
       
   341     setBackgroundItem(background.data());
       
   342 
       
   343     // The ownership was transferred to the base class.
       
   344     mBackground = background.take();
       
   345     
       
   346     // Construct the default UI.
       
   347     HbStyleLoader::registerFilePath(FILE_PATH_WIDGETML);
       
   348     HbStyleLoader::registerFilePath(FILE_PATH_CSS);
       
   349     
       
   350     QScopedPointer<HbTextItem> fileNameText(new HbTextItem(this));
       
   351     fileNameText->setObjectName("nmattachmentlistitem_filenametext");
       
   352     HbStyle::setItemName(fileNameText.data(), "filename");
       
   353     fileNameText->setElideMode(Qt::ElideRight);
       
   354     
       
   355     QScopedPointer<HbTextItem> fileSizeText(new HbTextItem(this));
       
   356     fileSizeText->setObjectName("nmattachmentlistitem_filenamesize");
       
   357     HbStyle::setItemName(fileSizeText.data(), "filesize");
       
   358     fileSizeText->setElideMode(Qt::ElideNone);
       
   359     
       
   360     // The ownership was transferred to this instance.
       
   361     mFileSizeText = fileSizeText.take(); 
       
   362     mFileNameText = fileNameText.take();
       
   363 }
       
   364 
       
   365 
       
   366 
       
   367 // End of file.