emailuis/nmailuiwidgets/src/nmattachmentlistwidget.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 widget
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmailuiwidgetsheaders.h"
       
    19 
       
    20 static const QString FILE_PATH_DOCML = ":nmattachmentlistwidget.docml";
       
    21 static const QString ATTACHMENT_WIDGET = "nmattachmentlistwidget";
       
    22 static const QString NmAttachmentTextColor = "qtc_default_main_pane_normal";
       
    23 
       
    24 static const qreal NmItemLineOpacity = 0.4;
       
    25 
       
    26 /*!
       
    27  @nmailuiwidgets
       
    28  \class NmAttachmentListWidget
       
    29 
       
    30  \brief The NmAttachmentListWidget widget provides a common look and feel for attachment lists.
       
    31 
       
    32 Basic usage of the widget:
       
    33 \code
       
    34 NmAttachmentListWidget* attachmentWidget = new NmAttachmentListWidget(this);
       
    35 connect(attachmentWidget,SIGNAL(itemActivated(int)),this,SLOT(attachmentActivated(int)));
       
    36 connect(attachmentWidget,SIGNAL(longPressed(int,QPointF)),this,SLOT(attachmentLongPressed(int,QPointF)));
       
    37 ... (loop)
       
    38 attachmentWidget->insertAttachment(i,list[i].attachmentNameString,attachmentSizeString);
       
    39 ... (loop)
       
    40 layout()->addItem(attachmentWidget);
       
    41 
       
    42 attachmentWidget->setProgressValue(index, value);
       
    43 \endcode
       
    44 
       
    45  The NmAttachmentListWidget contains the following UI graphics items by default:
       
    46  \li file name (HbTextItem)
       
    47  \li file size (HbTextItem)
       
    48 
       
    49  The NmAttachmentListWidget may contain the following optional UI graphics items:
       
    50  \li progressbar (HbProgressBar)
       
    51 
       
    52  The progressbar will exists when setProgressValue function is called with value over 0.
       
    53  */
       
    54 
       
    55 /*!
       
    56     \fn void NmAttachmentListWidget::insertAttachment(int index, const QString &fileName, const QString &fileSize)
       
    57     Inserts attachment to given index with name and size.
       
    58     \a index Index where attachment will be added in QList
       
    59     \a fileName Full name of attachment
       
    60     \a fileSize Full size of attachment in MB with accuracy one decimal
       
    61 */
       
    62 
       
    63 /*!
       
    64     \fn int NmAttachmentListWidget::removeAttachment(int index)
       
    65     Removes attachment from given index.
       
    66     \a index Attachment index in QList
       
    67 */
       
    68 
       
    69 /*!
       
    70     \fn int NmAttachmentListWidget::count()
       
    71     Returns attachment(s) count.
       
    72 */
       
    73 
       
    74 /*!
       
    75     \fn int NmAttachmentListWidget::progressValue(int index)
       
    76     Returns progress bar value (0-100) of given attachment.
       
    77     \a index Attachment index in QList
       
    78 */
       
    79 
       
    80 /*!
       
    81     \fn void NmAttachmentListWidget::setProgressValue(int index, int value)
       
    82     Set progress bar value (0-100) for given attachment.
       
    83     \a index Attachment index in QList
       
    84     \a value Value for progress bar
       
    85 */
       
    86 
       
    87 /*!
       
    88     \fn void NmAttachmentListWidget::itemActivated(int index)
       
    89     Emits signal when attachment item is clicked. Item is identified by parameter.
       
    90     \a index Attachment index in QList
       
    91 */
       
    92 
       
    93 /*!
       
    94     \fn void NmAttachmentListWidget::longPressed(int index, QPointF point);
       
    95     Emits signal when attachment item is long pressed.
       
    96     Item identification and the scene position of click are given by parameters.
       
    97     \a index Attachment index in QList
       
    98     \a point Point of location
       
    99 */
       
   100 
       
   101 
       
   102 /*!
       
   103     Constructs a new NmAttachmentListWidget with \a parent.
       
   104  */
       
   105 NmAttachmentListWidget::NmAttachmentListWidget(QGraphicsItem *parent)
       
   106     : HbWidget( parent ),
       
   107     mLayout(NULL),
       
   108     mOrientation(Qt::Vertical)
       
   109 {
       
   110     NM_FUNCTION;
       
   111     
       
   112     init( );
       
   113 }
       
   114 
       
   115 
       
   116 /*!
       
   117     Destructor.
       
   118  */
       
   119 NmAttachmentListWidget::~NmAttachmentListWidget( )
       
   120 {
       
   121     NM_FUNCTION;
       
   122     
       
   123     qDeleteAll(mItemList);
       
   124 
       
   125     mItemList.clear();
       
   126 	
       
   127 }
       
   128 
       
   129 /*!
       
   130     Setter for items text color override. This fucntion can be used
       
   131     if theme background is not used and text needs to be shown in diferent color.
       
   132  */
       
   133 void NmAttachmentListWidget::setTextColor(const QColor color)
       
   134 {
       
   135     NM_FUNCTION;
       
   136     
       
   137     mTextColor=color;
       
   138 }
       
   139 
       
   140 
       
   141 /*!
       
   142     Inserts attachment to given index. If index already contains data,
       
   143     old one will be moved to next.
       
   144  */
       
   145 void NmAttachmentListWidget::insertAttachment(
       
   146         int index, 
       
   147         const QString &fileName, 
       
   148         const QString &fileSize)
       
   149 {   
       
   150     NM_FUNCTION;
       
   151     
       
   152     NmAttachmentListItem *item = new NmAttachmentListItem(this);
       
   153     item->setObjectName(QString("nmattachmentlistitem_%1").arg(index));
       
   154 
       
   155     //connect to signals
       
   156     connect(item, SIGNAL(itemActivated()), this, SLOT(handleItemActivated()));
       
   157     connect(item, SIGNAL(itemLongPressed(QPointF)), this, SLOT(handleLongPressed(QPointF)));
       
   158 
       
   159     //set texts
       
   160     item->setTextColor(checkColor());      
       
   161     item->setFileNameText(fileName);
       
   162     item->setFileSizeText(fileSize);
       
   163 
       
   164     //finally add item to item's list
       
   165     mItemList.insert(index,item);
       
   166 
       
   167     //do layout again
       
   168     rearrangeLayout();
       
   169 }
       
   170 
       
   171 /*!
       
   172     Removes attachment from given index.
       
   173  */
       
   174 void NmAttachmentListWidget::removeAttachment(int index)
       
   175 {
       
   176     NM_FUNCTION;
       
   177     
       
   178     if(!mLayout) {
       
   179         NM_ERROR(1,"NmAttachmentListWidget::removeAttachment(): layout loading failed");
       
   180         return;
       
   181     }
       
   182 
       
   183     if(index >= 0 && index < mItemList.count())
       
   184     {
       
   185         //remove from layout
       
   186         mLayout->removeAt(index);
       
   187         //remove from the array
       
   188         NmAttachmentListItem *item = mItemList.takeAt(index);
       
   189         delete item;
       
   190         item = NULL;
       
   191 
       
   192         //do layout again
       
   193         rearrangeLayout();
       
   194     }
       
   195 }
       
   196 
       
   197 /*!
       
   198     Set attachment file size.
       
   199  */
       
   200 void NmAttachmentListWidget::setAttachmentSize(
       
   201         int index, 
       
   202         const QString &fileSize)
       
   203 {
       
   204     NM_FUNCTION;
       
   205     
       
   206 	if (index>=0 && index<mItemList.count()) {
       
   207 	    mItemList.at(index)->setFileSizeText(fileSize);
       
   208 	}
       
   209 }
       
   210 
       
   211 /*!
       
   212     Returns attachment(s) count. 
       
   213  */
       
   214 int NmAttachmentListWidget::count() const
       
   215 {
       
   216     NM_FUNCTION;
       
   217     
       
   218     return mItemList.count();
       
   219 }
       
   220 
       
   221 /*!
       
   222     Returns value of item, otherwise -1 if index not found from list.
       
   223  */
       
   224 int NmAttachmentListWidget::progressValue(int index) const
       
   225 {
       
   226     NM_FUNCTION;
       
   227     
       
   228     int ret(NmNotFoundError);
       
   229     if(index >= 0 && index < mItemList.count()){
       
   230         ret = mItemList.at(index)->progressBarValue();
       
   231     }
       
   232     return ret;
       
   233 }
       
   234 
       
   235 /*!
       
   236     Hides progress of item, if index is negative or creater 
       
   237     than last index function does nothing
       
   238  */
       
   239 void NmAttachmentListWidget::hideProgressBar(int index)
       
   240 {
       
   241     NM_FUNCTION;
       
   242     
       
   243     if(index >= 0 && index < mItemList.count()){
       
   244         mItemList.at(index)->hideProgressBar();
       
   245     }
       
   246 }
       
   247 
       
   248 
       
   249 /*!
       
   250     paint. Paint function for line painting.
       
   251 */
       
   252 void NmAttachmentListWidget::paint(
       
   253     QPainter *painter,
       
   254     const QStyleOptionGraphicsItem *option,
       
   255     QWidget *widget)
       
   256 {
       
   257     NM_FUNCTION;
       
   258     
       
   259     Q_UNUSED(option);
       
   260     Q_UNUSED(widget);
       
   261     if (painter&&mLayout){
       
   262         painter->save();
       
   263         
       
   264         // Use text color as a line color if set, otherwise use theme
       
   265         // normal list content color.
       
   266         painter->setPen(checkColor());
       
   267         painter->setOpacity(NmItemLineOpacity);
       
   268         // Draw line after each item
       
   269         int rowCount = mLayout->rowCount();
       
   270         QRectF layoutRect = mLayout->geometry ();
       
   271         for (int i=0;i<rowCount;i++){
       
   272             QGraphicsLayoutItem *item = mLayout->itemAt(i,0);
       
   273             if (item){
       
   274                 QRectF itemRect = item->geometry();      
       
   275                 QLineF line1( itemRect.topLeft().x(), itemRect.bottomRight().y(),
       
   276                               layoutRect.bottomRight().x(), itemRect.bottomRight().y());
       
   277                 painter->drawLine(line1);                     
       
   278             }     
       
   279         }
       
   280         painter->restore();
       
   281     }
       
   282 }
       
   283 
       
   284 /*!
       
   285     Public slot connected to set items value. Shows progress bar when called
       
   286     with match index and positive value (1-100). Zero value hides progress bar.
       
   287  */
       
   288 void NmAttachmentListWidget::setProgressBarValue(int index, int value)
       
   289 {
       
   290     NM_FUNCTION;
       
   291     
       
   292     if(index >= 0 && index < mItemList.count()){
       
   293         mItemList[index]->setProgressBarValue(value);
       
   294     }
       
   295 }
       
   296 
       
   297 /*!
       
   298     Helper function for initialize object.
       
   299  */
       
   300 void NmAttachmentListWidget::init( )
       
   301 {
       
   302     NM_FUNCTION;
       
   303     
       
   304     //Get mainwindow for orientation changes
       
   305     HbMainWindow *mw = hbInstance->allMainWindows().at(0);
       
   306 
       
   307     //connect to mainwindow signal
       
   308     if(mw){
       
   309         connect(mw, SIGNAL(orientationChanged(Qt::Orientation)),this, SLOT(orientationChanged(Qt::Orientation)));
       
   310         mOrientation = mw->orientation();
       
   311     } else {
       
   312         NM_ERROR(1,"NmAttachmentListWidget::init: mainWindow missing!");
       
   313     }
       
   314 
       
   315     //construct UI after orientation has been figured out
       
   316     constructUi();
       
   317 
       
   318     //set flags
       
   319     setFlag(QGraphicsItem::ItemIsFocusable);  
       
   320     setFlag(QGraphicsItem::ItemHasNoContents,false);
       
   321 }
       
   322 
       
   323 /*!
       
   324     Helper function for constructing UI components.
       
   325  */
       
   326 void NmAttachmentListWidget::constructUi()
       
   327 {
       
   328     NM_FUNCTION;
       
   329     
       
   330     setObjectName(QString(ATTACHMENT_WIDGET));
       
   331     HbDocumentLoader loader;
       
   332     bool loadingOk = false;
       
   333 
       
   334     //Pass this widget to documentloader
       
   335     QObjectList objectList;
       
   336     objectList.append(this);
       
   337     loader.setObjectTree(objectList);
       
   338     QObjectList widgetlist = loader.load(FILE_PATH_DOCML, &loadingOk);
       
   339 
       
   340     if(loadingOk){
       
   341         if(layout()){
       
   342             mLayout = dynamic_cast<QGraphicsGridLayout*>(layout());
       
   343             mLayout->setContentsMargins(0,0,0,0);
       
   344         } else {
       
   345             NM_ERROR(1,"NmAttachmentListWidget::constructUi: Widget doesn't have layout!");
       
   346         }
       
   347     } else {
       
   348         NM_ERROR(1,"NmAttachmentListWidget::constructUi: DocML loading failed.");
       
   349     }
       
   350 }
       
   351 
       
   352 /*!
       
   353     Helper slot for handling longpressed signals.
       
   354 */
       
   355 void NmAttachmentListWidget::handleLongPressed(QPointF point)
       
   356 {
       
   357     NM_FUNCTION;
       
   358     
       
   359     QObject *sender = QObject::sender();
       
   360     int index = findItem(sender);
       
   361     if(NmNotFoundError != index){
       
   362         emit longPressed(index, point);
       
   363     }
       
   364     else {
       
   365         NM_ERROR(1,"NmAttachmentListWidget::handleLongPressed: item cannot found!");
       
   366     }
       
   367 
       
   368 }
       
   369 
       
   370 /*!
       
   371     Helper slot for handling activated signals.
       
   372 */
       
   373 void NmAttachmentListWidget::handleItemActivated()
       
   374 {
       
   375     NM_FUNCTION;
       
   376     
       
   377     QObject *sender = QObject::sender();
       
   378     int index = findItem(sender);
       
   379     if(NmNotFoundError != index){
       
   380         emit itemActivated(index);
       
   381     }
       
   382     else {
       
   383         NM_ERROR(1,"NmAttachmentListWidget::handleItemActivated: item cannot found!");
       
   384     }
       
   385 }
       
   386 
       
   387 
       
   388 /*!
       
   389     Helper slot for handling orientation change.
       
   390 */
       
   391 void NmAttachmentListWidget::orientationChanged(Qt::Orientation orientation)
       
   392 {
       
   393     NM_FUNCTION;
       
   394     
       
   395     //be sure that orientation has been changed
       
   396     if(mOrientation != orientation){
       
   397         mOrientation = orientation;
       
   398         rearrangeLayout();
       
   399     }
       
   400 }
       
   401 
       
   402 
       
   403 /*!
       
   404     Helper function for finding attachment list items from the array.
       
   405     \param QObject searched item.
       
   406     \return int as index
       
   407 */
       
   408 int NmAttachmentListWidget::findItem(const QObject *obj)
       
   409 {
       
   410     NM_FUNCTION;
       
   411     
       
   412     int found(NmNotFoundError);
       
   413     int index(0);
       
   414 
       
   415     QListIterator<NmAttachmentListItem*> iter(mItemList);
       
   416     while(iter.hasNext() && found == NmNotFoundError){
       
   417         if(iter.next() == obj){
       
   418             found = index;
       
   419         }
       
   420         index++;
       
   421     }
       
   422 
       
   423     return found;
       
   424 }
       
   425 
       
   426 /*!
       
   427     Helper function for calculating items correct place on layout
       
   428     \param NmAttachmentListItem item to add to the layout
       
   429 */
       
   430 void NmAttachmentListWidget::insertItemToLayout(NmAttachmentListItem* item)
       
   431 {
       
   432     NM_FUNCTION;
       
   433     
       
   434     if(!mLayout) {
       
   435         NM_ERROR(1,"NmAttachmentListWidget::insertItemToLayout: Layout loading failed!");
       
   436         return;
       
   437     }
       
   438     int layout_count = mLayout->count();
       
   439 
       
   440     //check which orientation in use
       
   441     //if layout is horizontal, items need to be placed in 2 columns.
       
   442     if(Qt::Vertical == mOrientation){
       
   443         mLayout->addItem(item,layout_count,0);
       
   444     } else {
       
   445         // Qt FW sets the correct width automatically based on the width of the child widgets
       
   446         mLayout->addItem(item,layout_count / 2, layout_count % 2);
       
   447     }
       
   448 }
       
   449 
       
   450 /*!
       
   451     Helper function for rearrange layout after removing or layout change
       
   452 */
       
   453 void NmAttachmentListWidget::rearrangeLayout()
       
   454 {
       
   455     NM_FUNCTION;
       
   456     
       
   457     if(!mLayout) {
       
   458         NM_ERROR(1,"NmAttachmentListWidget::rearrangeLayout: Layout loading failed!");
       
   459         return;
       
   460     }
       
   461     
       
   462     //remove all items from the layout
       
   463     int count(mLayout->count());
       
   464     for(int i = count - 1; i >= 0; --i){
       
   465         mLayout->removeAt(i);
       
   466     }
       
   467 
       
   468     //then add them back
       
   469     QColor textColor = checkColor();    
       
   470     foreach(NmAttachmentListItem *item, mItemList){
       
   471         item->setTextColor(textColor);
       
   472         insertItemToLayout(item);
       
   473     }
       
   474 }
       
   475 
       
   476 /*!
       
   477     Helper function to set text color
       
   478 */
       
   479 QColor NmAttachmentListWidget::checkColor()
       
   480 {
       
   481     NM_FUNCTION;
       
   482     
       
   483     QColor retColor;
       
   484 
       
   485     if (mTextColor.isValid()){
       
   486         retColor = mTextColor;
       
   487     }
       
   488     else {    
       
   489         retColor =  HbColorScheme::color(NmAttachmentTextColor);
       
   490     }
       
   491     return retColor;
       
   492 }
       
   493 
       
   494 /*!
       
   495     \reimp
       
   496 */
       
   497 
       
   498 void NmAttachmentListWidget::changeEvent(QEvent *event)
       
   499 {
       
   500     NM_FUNCTION;
       
   501 
       
   502     if (event->type() == HbEvent::ThemeChanged) {
       
   503         rearrangeLayout();    
       
   504     }
       
   505     return HbWidgetBase::changeEvent(event);
       
   506 }