emailuis/nmailuiwidgets/src/nmattachmentlistitem.cpp
changeset 72 64e38f08e49c
parent 65 478bc57ad291
--- a/emailuis/nmailuiwidgets/src/nmattachmentlistitem.cpp	Thu Sep 02 20:15:00 2010 +0300
+++ b/emailuis/nmailuiwidgets/src/nmattachmentlistitem.cpp	Fri Sep 17 08:27:21 2010 +0300
@@ -24,59 +24,46 @@
 
 static const int PROGRESSBAR_MIN = 0; 
 static const int PROGRESSBAR_MAX = 100;
-static const int PROGRESSBAR_HIDE_COUNTDOWN = 500;
+static const int PROGRESSBAR_HIDE_COUNTDOWN = 500; // Milliseconds
+
 
 /*!
- @nmailuiwidgets
- \class NmAttachmentListItem
+    @nmailuiwidgets
+
+    \class NmAttachmentListItem
 
- \brief The NmAttachmentListItem widget provides for showing a single attachment with file size and
-        download progress in the attachment list.
-
-
-
- */
+    \brief  NmAttachmentListItem is a widget used for displaying a single
+            attachment with the attachment file size and download progress in
+            the attachment list.
+*/
 
 
 /*!
     Constructs a new NmAttachmentListItem with \a parent.
- */
+*/
 NmAttachmentListItem::NmAttachmentListItem(QGraphicsItem *parent)
-    : HbWidget( parent ),
-      mFileNameText(NULL),
-      mFileSizeText(NULL),
-      mProgressBar(NULL),
-      mBackGround(NULL)
+: HbWidget( parent ),
+  mFileNameText(NULL),
+  mFileSizeText(NULL),
+  mProgressBar(NULL),
+  mBackground(NULL)
 {
     NM_FUNCTION;
-    
-    init( );
+
+    init();
 	
     // Informs GestureFramework that NmAttachmentListItem widget is interested 
-    // Tap gesture and TapAndHold gesture.
+    // in Tap and TapAndHold gestures.
     grabGesture(Qt::TapGesture);
     HbEffect::add("mailAttachmentWidget", "listviewitem_press", "pressed");
     HbEffect::add("mailAttachmentWidget", "listviewitem_release", "released");
 }
 
-/*!
-    Setter for items text color override. This fucntion can be used
-    if theme background is not used and text needs to be shown in diferent color.
- */
-void NmAttachmentListItem::setTextColor(const QColor color)
-{
-    NM_FUNCTION;
-    
-    mTextColor=color;
-    if (mTextColor.isValid() && mFileNameText){
-        mFileNameText->setTextColor(mTextColor);
-    }    
-}
 
 /*!
     Destructor.
- */
-NmAttachmentListItem::~NmAttachmentListItem( )
+*/
+NmAttachmentListItem::~NmAttachmentListItem()
 {
     NM_FUNCTION;
     
@@ -84,93 +71,249 @@
     HbStyleLoader::unregisterFilePath(FILE_PATH_CSS);
 }
 
+
 /*!
-    Set the text to be displayed in the file name item.
- */
+    Setter for items text color override. This method can be used if the theme
+    background is not used and text needs to be shown with a different color.
+
+    \param color The new test color.
+*/
+void NmAttachmentListItem::setTextColor(const QColor color)
+{
+    NM_FUNCTION;
+    
+    mTextColor = color;
+
+    if (mTextColor.isValid() && mFileNameText) {
+        mFileNameText->setTextColor(mTextColor);
+    }
+}
+
+
+/*!
+    Sets the background color. This method can be used to override the
+    default background color set by the current theme. Because of the effect,
+    the implementation "smoke and mirrors" but it works.
+
+    \param color The new background color.
+*/
+void NmAttachmentListItem::setBackgroundColor(const QColor color)
+{
+    NM_FUNCTION;
+
+    if (!color.isValid()) {
+        // Invalid color!
+        return;
+    }
+
+    mBackgroundColor = color;
+
+    // The effect color is the opposite color of the background color.
+    QColor effectColor(qAbs(color.red() - 255),
+                       qAbs(color.green() - 255),
+                       qAbs(color.blue() - 255));
+    
+    QPalette palette = QApplication::palette();
+    QBrush brush(effectColor);
+    palette.setBrush(QPalette::Window, effectColor);
+    setPalette(palette);
+    setAutoFillBackground(true);
+
+    if (mBackground) {
+        // Set the background color for the background item.
+        brush.setColor(mBackgroundColor);
+        palette.setBrush(QPalette::Window, brush);
+        mBackground->setPalette(palette);
+        mBackground->setAutoFillBackground(true);
+        mBackground->frameDrawer().setFrameType(HbFrameDrawer::OnePiece);
+    }
+
+    repolish();
+}
+
+
+/*!
+    Sets the text to be displayed in the file name item.
+
+    \param filename The filename.
+*/
 void NmAttachmentListItem::setFileNameText(const QString &fileName)
 {
     NM_FUNCTION;
     
-    if (mFileNameText){
-        if (mTextColor.isValid()){
+    if (mFileNameText) {
+        if (mTextColor.isValid()) {
             mFileNameText->setTextColor(mTextColor);
         }
+
         mFileNameText->setTextWrapping(Hb::TextNoWrap);
         mFileNameText->setText(fileName);    
     }
 }
 
+
 /*!
-    Set the text to be displayed in the file size item
- */
+    Sets the value of the text for displaying the attachment file size.
+    
+    \param fileSize The file size as a string.
+*/
 void NmAttachmentListItem::setFileSizeText(const QString &fileSize)
 {
     NM_FUNCTION;
     
-    if (mFileSizeText){
-        if (mTextColor.isValid()){
+    if (mFileSizeText) {
+        if (mTextColor.isValid()) {
             mFileSizeText->setTextColor(mTextColor);
         }
+
         mFileSizeText->setTextWrapping(Hb::TextNoWrap);
         mFileSizeText->setText(fileSize);
     }
 }
 
+
 /*!
-    Set the download progress bar value (0-100)%, if value is 0 progress bar is hidden
- */
+    Set the download progress bar value (0 - 100 %). If the given value is 0,
+    the progress bar is hidden.
+
+    \param value The progress bar value.
+*/
 void NmAttachmentListItem::setProgressBarValue(const int value)
 {
     NM_FUNCTION;
     
-    //first check if value is 0 or below -> hide progressbar
-    if ( 0 >= value ){
+    // First check if the value is 0 or below -> hide progressbar.
+    if (0 >= value) {
         removeProgressBar();
         return;
     }
 
-    if ( !mProgressBar ){
+    if (!mProgressBar) {
         mProgressBar = new HbProgressBar(this); 
         mProgressBar->setObjectName("attachmentlistitem_progress");
         mProgressBar->setRange(PROGRESSBAR_MIN,PROGRESSBAR_MAX);
-        HbStyle::setItemName( mProgressBar, "progressbar" );
+        HbStyle::setItemName(mProgressBar, "progressbar");
         repolish();
     }
+
     mProgressBar->setProgressValue(value);
     
-    //start hiding count down
-    if(PROGRESSBAR_MAX <= value){
+    // Start hiding the count down.
+    if (PROGRESSBAR_MAX <= value){
         hideProgressBar();
     }
 }
 
+
 /*!
-    Get the download progress bar value
+    \return The value of the download progress bar.
 */
 int NmAttachmentListItem::progressBarValue() const
 {
     NM_FUNCTION;
     
     int ret = 0;
-    if ( mProgressBar ){
+
+    if (mProgressBar) {
         ret = mProgressBar->progressValue();
     }
 
     return ret;
 }
 
+
 /*!
-    Hides progress bar, used if download is cancelled before 100 precent is reached
+    Hides the progress bar. Used if the download is cancelled before 100
+    percent is reached.
 */
 void NmAttachmentListItem::hideProgressBar()
 {
     NM_FUNCTION;
     
-    QTimer::singleShot(PROGRESSBAR_HIDE_COUNTDOWN,this, SLOT(removeProgressBar()));
+    QTimer::singleShot(PROGRESSBAR_HIDE_COUNTDOWN, this, SLOT(removeProgressBar()));
+}
+
+
+/*!
+    Displays a visual effect according to the current pressed state.
+
+    \param pressed True, if the item was pressed.
+*/
+void NmAttachmentListItem::setPressed(bool pressed)
+{
+    if (pressed) {
+        setProperty("state", "pressed");
+        mBackground->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_PRESSED);
+        HbEffect::cancel(mBackground, "released");
+        HbEffect::start(mBackground, "mailAttachmentWidget", "pressed");
+    }
+    else {
+        setProperty("state", "normal");
+        mBackground->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
+        HbEffect::cancel(mBackground, "pressed");
+        HbEffect::start(mBackground, "mailAttachmentWidget", "released");
+    }    
 }
 
+
 /*!
-    Initialize
+    Handles the given gesture event.
+
+    \param event The event to handle.
+*/
+void NmAttachmentListItem::gestureEvent(QGestureEvent *event)
+{
+    NM_FUNCTION;
+    
+    if (HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
+        switch (tap->tapStyleHint()) {
+            case HbTapGesture::Tap: {
+                Qt::GestureState state = tap->state();
+                HbInstantFeedback::play(HbFeedback::Basic);
+                setPressed(true);
+
+                if (state == Qt::GestureFinished) {
+                    emit itemActivated();
+                    setPressed(false);
+                }
+                else if (state == Qt::GestureCanceled) {
+                    setPressed(false);
+                }
+
+                break;
+            }
+            case HbTapGesture::TapAndHold: {
+                emit itemLongPressed(event->mapToGraphicsScene(tap->position()));
+                setPressed(false);    
+                break;
+            }
+        } // switch
+    }
+    else {
+        // Let the HbWidget handle the event.
+        HbWidget::gestureEvent(event);
+    }
+}
+
+
+/*!
+    Hides and deletes the download progress bar.
+*/
+void NmAttachmentListItem::removeProgressBar()
+{
+    NM_FUNCTION;
+    
+    if (mProgressBar) {
+        HbStyle::setItemName(mProgressBar, "");
+        mProgressBar->deleteLater();
+        mProgressBar = 0;
+        repolish();
+    }
+}
+
+
+/*!
+    Initializes the widget.
 */
 void NmAttachmentListItem::init( )
 {
@@ -178,119 +321,47 @@
     
     constructUi();
 
-    //set default values
+    // Set the default flag values.
     setFlag(QGraphicsItem::ItemIsFocusable);
     setFlag(QGraphicsItem::ItemIsSelectable);
 }
 
+
 /*!
-    Constructs the UI, sets style itemnames etc.
+    Constructs the UI, sets style item names etc.
 */
 void NmAttachmentListItem::constructUi()
 {
     NM_FUNCTION;
     
-    //background
-    QScopedPointer<HbFrameItem> backGround(new HbFrameItem(this));
-    backGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
-    backGround->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
-    setBackgroundItem(backGround.data());
-    // ownership was transferred to base class
-    mBackGround = backGround.take();
+    // Background
+    QScopedPointer<HbFrameItem> background(new HbFrameItem(this));
+    background->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
+    background->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
+    setBackgroundItem(background.data());
+
+    // The ownership was transferred to the base class.
+    mBackground = background.take();
     
-    
-    //construct default ui.    
+    // Construct the default UI.
     HbStyleLoader::registerFilePath(FILE_PATH_WIDGETML);
     HbStyleLoader::registerFilePath(FILE_PATH_CSS);
     
-    
     QScopedPointer<HbTextItem> fileNameText(new HbTextItem(this));
     fileNameText->setObjectName("nmattachmentlistitem_filenametext");
-    HbStyle::setItemName( fileNameText.data(), "filename" );  
+    HbStyle::setItemName(fileNameText.data(), "filename");
     fileNameText->setElideMode(Qt::ElideRight);
     
-    
     QScopedPointer<HbTextItem> fileSizeText(new HbTextItem(this));
     fileSizeText->setObjectName("nmattachmentlistitem_filenamesize");
-    HbStyle::setItemName( fileSizeText.data(), "filesize" );
+    HbStyle::setItemName(fileSizeText.data(), "filesize");
     fileSizeText->setElideMode(Qt::ElideNone);
     
-    // ownership transferred to this object
+    // The ownership was transferred to this instance.
     mFileSizeText = fileSizeText.take(); 
     mFileNameText = fileNameText.take();
 }
 
 
-/*!
-    Hides the download progress bar
- */
-void NmAttachmentListItem::removeProgressBar()
-{
-    NM_FUNCTION;
-    
-	if ( mProgressBar ){
-	    HbStyle::setItemName( mProgressBar, "" );
-	    mProgressBar->deleteLater();
-	    mProgressBar = 0;
-	    repolish();
-	}
-}
 
-
-/*!
-    This function handles gestures
- */
-
-void NmAttachmentListItem::gestureEvent(QGestureEvent *event)
-{
-    NM_FUNCTION;
-    
-    if (HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
-        switch(tap->tapStyleHint()) {
-        case HbTapGesture::Tap: 
-            {
-                Qt::GestureState state = tap->state();
-                HbInstantFeedback::play(HbFeedback::Basic);
-                setPressed(true);
-                if (state == Qt::GestureFinished) {
-                    emit itemActivated();
-                    setPressed(false);
-                }
-                else if (state == Qt::GestureCanceled) {
-                    setPressed(false);
-                }
-             }
-             break;
-            
-         case HbTapGesture::TapAndHold:
-             {
-                 emit itemLongPressed(event->mapToGraphicsScene(tap->position()));
-                 setPressed(false);
-             }    
-             break;
-        }
-    }
-    else {
-           HbWidget::gestureEvent(event);
-    }
-}
-
-/*!
-    Sets the effect of the item when tapping it
-*/
-void NmAttachmentListItem::setPressed(bool pressed)
-{
-    if (pressed) {
-        setProperty("state", "pressed");
-        mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_PRESSED);
-        HbEffect::cancel(mBackGround, "released");
-        HbEffect::start(mBackGround, "mailAttachmentWidget", "pressed");
-
-    }
-    else {
-        setProperty("state", "normal");
-        mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
-        HbEffect::cancel(mBackGround, "pressed");
-        HbEffect::start(mBackGround, "mailAttachmentWidget", "released");
-    }    
-}
+// End of file.