emailuis/nmailuiwidgets/src/nmattachmentlistwidget.cpp
changeset 18 578830873419
child 20 ecc8def7944a
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     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 
       
    23 /*!
       
    24  @nmailuiwidgets
       
    25  \class NmAttachmentListWidget
       
    26 
       
    27  \brief The NmAttachmentListWidget widget provides a common look and feel for attachment lists.
       
    28 
       
    29 Basic usage of the widget:
       
    30 \code
       
    31 NmAttachmentListWidget* attachmentWidget = new NmAttachmentListWidget(this);
       
    32 connect(attachmentWidget,SIGNAL(itemActivated(int)),this,SLOT(attachmentActivated(int)));
       
    33 connect(attachmentWidget,SIGNAL(longPressed(int,QPointF)),this,SLOT(attachmentLongPressed(int,QPointF)));
       
    34 ... (loop)
       
    35 attachmentWidget->insertAttachment(i,list[i].attachmentNameString,attachmentSizeString);
       
    36 ... (loop)
       
    37 layout()->addItem(attachmentWidget);
       
    38 
       
    39 attachmentWidget->setProgressValue(index, value);
       
    40 \endcode
       
    41 
       
    42  The NmAttachmentListWidget contains the following UI graphics items by default:
       
    43  \li file name (HbTextItem)
       
    44  \li file size (HbTextItem)
       
    45 
       
    46  The NmAttachmentListWidget may contain the following optional UI graphics items:
       
    47  \li progressbar (HbProgressBar)
       
    48 
       
    49  The progressbar will exists when setProgressValue function is called with value over 0.
       
    50  */
       
    51 
       
    52 /*!
       
    53     \fn void NmAttachmentListWidget::insertAttachment(int index, const QString &fileName, const QString &fileSize)
       
    54     Inserts attachment to given index with name and size.
       
    55     \a index Index where attachment will be added in QList
       
    56     \a fileName Full name of attachment
       
    57     \a fileSize Full size of attachment in MB with accuracy one decimal
       
    58 */
       
    59 
       
    60 /*!
       
    61     \fn int NmAttachmentListWidget::removeAttachment(int index)
       
    62     Removes attachment from given index.
       
    63     \a index Attachment index in QList
       
    64 */
       
    65 
       
    66 /*!
       
    67     \fn int NmAttachmentListWidget::count()
       
    68     Returns attachment(s) count.
       
    69 */
       
    70 
       
    71 /*!
       
    72     \fn int NmAttachmentListWidget::progressValue(int index)
       
    73     Returns progress bar value (0-100) of given attachment.
       
    74     \a index Attachment index in QList
       
    75 */
       
    76 
       
    77 /*!
       
    78     \fn void NmAttachmentListWidget::setProgressValue(int index, int value)
       
    79     Set progress bar value (0-100) for given attachment.
       
    80     \a index Attachment index in QList
       
    81     \a value Value for progress bar
       
    82 */
       
    83 
       
    84 /*!
       
    85     \fn void NmAttachmentListWidget::itemActivated(int index)
       
    86     Emits signal when attachment item is clicked. Item is identified by parameter.
       
    87     \a index Attachment index in QList
       
    88 */
       
    89 
       
    90 /*!
       
    91     \fn void NmAttachmentListWidget::longPressed(int index, QPointF point);
       
    92     Emits signal when attachment item is long pressed.
       
    93     Item identification and the scene position of click are given by parameters.
       
    94     \a index Attachment index in QList
       
    95     \a point Point of location
       
    96 */
       
    97 
       
    98 
       
    99 /*!
       
   100     Constructs a new NmAttachmentListWidget with \a parent.
       
   101  */
       
   102 NmAttachmentListWidget::NmAttachmentListWidget(QGraphicsItem *parent)
       
   103     : HbWidget( parent ),
       
   104     mLayout(NULL),
       
   105     mOrientation(Qt::Vertical)
       
   106 {
       
   107     init( );
       
   108 }
       
   109 
       
   110 
       
   111 /*!
       
   112     Destructor.
       
   113  */
       
   114 NmAttachmentListWidget::~NmAttachmentListWidget( )
       
   115 {
       
   116     mItemList.clear();
       
   117 }
       
   118 
       
   119 /*!
       
   120     Inserts attachment to given index. If index already contains data,
       
   121     old one will be moved to next.
       
   122  */
       
   123 void NmAttachmentListWidget::insertAttachment(
       
   124         int index, 
       
   125         const QString &fileName, 
       
   126         const QString &fileSize)
       
   127 {   
       
   128     NmAttachmentListItem *item = new NmAttachmentListItem(this);
       
   129     item->setObjectName(QString("nmattachmentlistitem_%1").arg(index));
       
   130 
       
   131     //connect to signals
       
   132     connect(item, SIGNAL(itemActivated()), this, SLOT(handleItemActivated()));
       
   133     connect(item, SIGNAL(itemLongPressed(QPointF)), this, SLOT(handleLongPressed(QPointF)));
       
   134 
       
   135     //set texts
       
   136     item->setTextItems(fileName,fileSize);
       
   137 
       
   138     //finally add item to item's list
       
   139     mItemList.insert(index,item);
       
   140 
       
   141     //do layout again
       
   142     rearrangeLayout();
       
   143 }
       
   144 
       
   145 /*!
       
   146     Removes attachment from given index.
       
   147  */
       
   148 void NmAttachmentListWidget::removeAttachment(int index)
       
   149 {
       
   150     if(!mLayout) {
       
   151         NMLOG("NmAttachmentListWidget::removeAttachment: Layout loading failed!");
       
   152         return;
       
   153     }
       
   154 
       
   155     if(index >= 0 && index < mItemList.count())
       
   156     {
       
   157         //remove from layout
       
   158         mLayout->removeAt(index);
       
   159         //remove from the array
       
   160         NmAttachmentListItem *item = mItemList.takeAt(index);
       
   161         delete item;
       
   162         item = NULL;
       
   163 
       
   164         //do layout again
       
   165         rearrangeLayout();
       
   166     }
       
   167 }
       
   168 
       
   169 /*!
       
   170     Returns attachment(s) count. 
       
   171  */
       
   172 int NmAttachmentListWidget::count() const
       
   173 {
       
   174     return mItemList.count();
       
   175 }
       
   176 
       
   177 /*!
       
   178     Returns value of item, otherwise -1 if index not found from list.
       
   179  */
       
   180 int NmAttachmentListWidget::progressValue(int index) const
       
   181 {
       
   182     int ret(NmNotFoundError);
       
   183     if(index >= 0 && index < mItemList.count()){
       
   184         ret = mItemList.at(index)->progressBarValue();
       
   185     }
       
   186     return ret;
       
   187 }
       
   188 
       
   189 /*!
       
   190     Public slot connected to set items value. Shows progress bar when called
       
   191     with match index and positive value (1-100). Zero value hides progress bar.
       
   192  */
       
   193 void NmAttachmentListWidget::setProgressBarValue(int index, int value)
       
   194 {
       
   195     if(index >= 0 && index < mItemList.count()){
       
   196         mItemList[index]->setProgressBarValue(value);
       
   197     }
       
   198 }
       
   199 
       
   200 /*!
       
   201     Helper function for initialize object.
       
   202  */
       
   203 void NmAttachmentListWidget::init( )
       
   204 {
       
   205     //Get mainwindow for orientation changes
       
   206     HbMainWindow *mw = hbInstance->allMainWindows().at(0);
       
   207 
       
   208     //connect to mainwindow signal
       
   209     if(mw){
       
   210         connect(mw, SIGNAL(orientationChanged(Qt::Orientation)),this, SLOT(orientationChanged(Qt::Orientation)));
       
   211         mOrientation = mw->orientation();
       
   212     } else {
       
   213         NMLOG("NmAttachmentListWidget::init: mainWindow missing!");
       
   214     }
       
   215 
       
   216     //construct UI after orientation has been figured out
       
   217     constructUi();
       
   218 
       
   219     //set default values, needed?
       
   220     setFlag(QGraphicsItem::ItemIsFocusable);      
       
   221 }
       
   222 
       
   223 /*!
       
   224     Helper function for constructing UI components.
       
   225  */
       
   226 void NmAttachmentListWidget::constructUi()
       
   227 {
       
   228     setObjectName(QString(ATTACHMENT_WIDGET));
       
   229     HbDocumentLoader loader;
       
   230     bool loadingOk = false;
       
   231 
       
   232     //Pass this widget to documentloader
       
   233     QObjectList objectList;
       
   234     objectList.append(this);
       
   235     loader.setObjectTree(objectList);
       
   236     QObjectList widgetlist = loader.load(FILE_PATH_DOCML, &loadingOk);
       
   237 
       
   238     int widgetCount = widgetlist.count();
       
   239     if(loadingOk && widgetCount){
       
   240         if(layout()){
       
   241             mLayout = dynamic_cast<QGraphicsGridLayout*>(layout());
       
   242         } else {
       
   243             NMLOG("NmAttachmentListWidget::constructUi: Widget doesn't have layout!");
       
   244         }
       
   245     } else {
       
   246         NMLOG("NmAttachmentListWidget::constructUi: DocML loading failed.");
       
   247     }
       
   248 
       
   249 }
       
   250 
       
   251 /*!
       
   252     Helper slot for handling longpressed signals.
       
   253 */
       
   254 void NmAttachmentListWidget::handleLongPressed(QPointF point)
       
   255 {
       
   256     QObject *sender = QObject::sender();
       
   257     int index = findItem(sender);
       
   258     if(NmNotFoundError != index){
       
   259         emit longPressed(index, point);
       
   260     }
       
   261     else {
       
   262         NMLOG("NmAttachmentListWidget::handleLongPressed: item cannot found!");
       
   263     }
       
   264 
       
   265 }
       
   266 
       
   267 /*!
       
   268     Helper slot for handling activated signals.
       
   269 */
       
   270 void NmAttachmentListWidget::handleItemActivated()
       
   271 {
       
   272     QObject *sender = QObject::sender();
       
   273     int index = findItem(sender);
       
   274     if(NmNotFoundError != index){
       
   275         emit itemActivated(index);
       
   276     }
       
   277     else {
       
   278         NMLOG("NmAttachmentListWidget::handleItemActivated: item cannot found!");
       
   279     }
       
   280 }
       
   281 
       
   282 
       
   283 /*!
       
   284     Helper slot for handling orientation change.
       
   285 */
       
   286 void NmAttachmentListWidget::orientationChanged(Qt::Orientation orientation)
       
   287 {
       
   288     NMLOG("NmAttachmentListWidget::orientationChanged");
       
   289 
       
   290     //be sure that orientation has been changed
       
   291     if(mOrientation != orientation){
       
   292         mOrientation = orientation;
       
   293         rearrangeLayout();
       
   294     }
       
   295 }
       
   296 
       
   297 
       
   298 /*!
       
   299     Helper function for finding attachment list items from the array.
       
   300     \param QObject searched item.
       
   301     \return int as index
       
   302 */
       
   303 int NmAttachmentListWidget::findItem(const QObject *obj)
       
   304 {
       
   305     int found(NmNotFoundError);
       
   306     int index(0);
       
   307 
       
   308     QListIterator<NmAttachmentListItem*> iter(mItemList);
       
   309     while(iter.hasNext() && found == NmNotFoundError){
       
   310         if(iter.next() == obj){
       
   311             found = index;
       
   312         }
       
   313         index++;
       
   314     }
       
   315 
       
   316     return found;
       
   317 }
       
   318 
       
   319 /*!
       
   320     Helper function for calculating items correct place on layout
       
   321     \param NmAttachmentListItem item to add to the layout
       
   322 */
       
   323 void NmAttachmentListWidget::insertItemToLayout(NmAttachmentListItem* item)
       
   324 {
       
   325     if(!mLayout) {
       
   326         NMLOG("NmAttachmentListWidget::insertItemToLayout: Layout loading failed!");
       
   327         return;
       
   328     }
       
   329     int layout_count = mLayout->count();
       
   330 
       
   331     //check which orientation in use
       
   332     //if layout is horizontal, items need to be placed in 2 columns.
       
   333     if(Qt::Vertical == mOrientation){
       
   334         mLayout->addItem(item,layout_count,0);
       
   335     } else {
       
   336         mLayout->addItem(item,layout_count / 2, layout_count % 2);
       
   337     }
       
   338 
       
   339 }
       
   340 
       
   341 /*!
       
   342     Helper function for rearrange layout after removing or layout change
       
   343 */
       
   344 void NmAttachmentListWidget::rearrangeLayout()
       
   345 {
       
   346     if(!mLayout) {
       
   347         NMLOG("NmAttachmentListWidget::rearrangeLayout: Layout loading failed!");
       
   348         return;
       
   349     }
       
   350 
       
   351     //remove all items from the layout
       
   352     int count(mLayout->count());
       
   353     for(int i = count - 1; i >= 0; --i){
       
   354         mLayout->removeAt(i);
       
   355     }
       
   356 
       
   357     //then add them back
       
   358     foreach(NmAttachmentListItem *item, mItemList){
       
   359         insertItemToLayout(item);
       
   360     }
       
   361 }