emailuis/nmailuiwidgets/src/nmattachmentlistwidget.cpp
changeset 72 64e38f08e49c
parent 59 16ed8d08d0b1
child 75 47d84de1c893
equal deleted inserted replaced
65:478bc57ad291 72:64e38f08e49c
    21 static const QString ATTACHMENT_WIDGET = "nmattachmentlistwidget";
    21 static const QString ATTACHMENT_WIDGET = "nmattachmentlistwidget";
    22 static const QString NmAttachmentTextColor = "qtc_default_main_pane_normal";
    22 static const QString NmAttachmentTextColor = "qtc_default_main_pane_normal";
    23 
    23 
    24 static const qreal NmItemLineOpacity = 0.4;
    24 static const qreal NmItemLineOpacity = 0.4;
    25 
    25 
       
    26 
    26 /*!
    27 /*!
    27  @nmailuiwidgets
    28  @nmailuiwidgets
    28  \class NmAttachmentListWidget
    29  \class NmAttachmentListWidget
    29 
    30 
    30  \brief The NmAttachmentListWidget widget provides a common look and feel for attachment lists.
    31  \brief The NmAttachmentListWidget widget provides a common look and feel for attachment lists.
   101 
   102 
   102 /*!
   103 /*!
   103     Constructs a new NmAttachmentListWidget with \a parent.
   104     Constructs a new NmAttachmentListWidget with \a parent.
   104  */
   105  */
   105 NmAttachmentListWidget::NmAttachmentListWidget(QGraphicsItem *parent)
   106 NmAttachmentListWidget::NmAttachmentListWidget(QGraphicsItem *parent)
   106     : HbWidget( parent ),
   107 : HbWidget( parent ),
   107     mLayout(NULL),
   108   mLayout(NULL),
   108     mOrientation(Qt::Vertical)
   109   mOrientation(Qt::Vertical)
   109 {
   110 {
   110     NM_FUNCTION;
   111     NM_FUNCTION;
   111     
   112 
   112     init( );
   113     init( );
   113 }
   114 }
   114 
   115 
   115 
   116 
   116 /*!
   117 /*!
   117     Destructor.
   118     Destructor.
   118  */
   119  */
   119 NmAttachmentListWidget::~NmAttachmentListWidget( )
   120 NmAttachmentListWidget::~NmAttachmentListWidget( )
   120 {
   121 {
   121     NM_FUNCTION;
   122     NM_FUNCTION;
   122     
   123 
   123     qDeleteAll(mItemList);
   124     qDeleteAll(mItemList);
   124 
   125 
   125     mItemList.clear();
   126     mItemList.clear();
   126 	
   127 
   127 }
   128 }
   128 
   129 
   129 /*!
   130 /*!
   130     Setter for items text color override. This fucntion can be used
   131     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     if theme background is not used and text needs to be shown in diferent color.
   132  */
   133  */
   133 void NmAttachmentListWidget::setTextColor(const QColor color)
   134 void NmAttachmentListWidget::setTextColor(const QColor color)
   134 {
   135 {
   135     NM_FUNCTION;
   136     NM_FUNCTION;
   136     
   137 
   137     mTextColor=color;
   138     mTextColor=color;
       
   139 }
       
   140 
       
   141 
       
   142 /*!
       
   143     Sets the background color. This method can be used to override the
       
   144     default background color set by the current theme.
       
   145 
       
   146     \param color The new background color.
       
   147 */
       
   148 void NmAttachmentListWidget::setBackgroundColor(const QColor color)
       
   149 {
       
   150     NM_FUNCTION;
       
   151     mBackgroundColor = color;
   138 }
   152 }
   139 
   153 
   140 
   154 
   141 /*!
   155 /*!
   142     Inserts attachment to given index. If index already contains data,
   156     Inserts attachment to given index. If index already contains data,
   143     old one will be moved to next.
   157     old one will be moved to next.
   144  */
   158  */
   145 void NmAttachmentListWidget::insertAttachment(
   159 void NmAttachmentListWidget::insertAttachment(
   146         int index, 
   160         int index,
   147         const QString &fileName, 
   161         const QString &fileName,
   148         const QString &fileSize)
   162         const QString &fileSize)
   149 {   
   163 {
   150     NM_FUNCTION;
   164     NM_FUNCTION;
   151     
   165 
   152     NmAttachmentListItem *item = new NmAttachmentListItem(this);
   166     NmAttachmentListItem *item = new NmAttachmentListItem(this);
       
   167 
       
   168     if (mBackgroundColor.isValid()) {
       
   169         item->setBackgroundColor(mBackgroundColor);
       
   170     }
       
   171 
   153     item->setObjectName(QString("nmattachmentlistitem_%1").arg(index));
   172     item->setObjectName(QString("nmattachmentlistitem_%1").arg(index));
   154 
   173 
   155     //connect to signals
   174     //connect to signals
   156     connect(item, SIGNAL(itemActivated()), this, SLOT(handleItemActivated()));
   175     connect(item, SIGNAL(itemActivated()), this, SLOT(handleItemActivated()));
   157     connect(item, SIGNAL(itemLongPressed(QPointF)), this, SLOT(handleLongPressed(QPointF)));
   176     connect(item, SIGNAL(itemLongPressed(QPointF)), this, SLOT(handleLongPressed(QPointF)));
   158 
   177 
   159     //set texts
   178     //set texts
   160     item->setTextColor(checkColor());      
   179     item->setTextColor(checkColor());
   161     item->setFileNameText(fileName);
   180     item->setFileNameText(fileName);
   162     item->setFileSizeText(fileSize);
   181     item->setFileSizeText(fileSize);
   163 
   182 
   164     //finally add item to item's list
   183     //finally add item to item's list
   165     mItemList.insert(index,item);
   184     mItemList.insert(index,item);
   172     Removes attachment from given index.
   191     Removes attachment from given index.
   173  */
   192  */
   174 void NmAttachmentListWidget::removeAttachment(int index)
   193 void NmAttachmentListWidget::removeAttachment(int index)
   175 {
   194 {
   176     NM_FUNCTION;
   195     NM_FUNCTION;
   177     
   196 
   178     if(!mLayout) {
   197     if(!mLayout) {
   179         NM_ERROR(1,"NmAttachmentListWidget::removeAttachment(): layout loading failed");
   198         NM_ERROR(1,"NmAttachmentListWidget::removeAttachment(): layout loading failed");
   180         return;
   199         return;
   181     }
   200     }
   182 
   201 
   196 
   215 
   197 /*!
   216 /*!
   198     Set attachment file size.
   217     Set attachment file size.
   199  */
   218  */
   200 void NmAttachmentListWidget::setAttachmentSize(
   219 void NmAttachmentListWidget::setAttachmentSize(
   201         int index, 
   220         int index,
   202         const QString &fileSize)
   221         const QString &fileSize)
   203 {
   222 {
   204     NM_FUNCTION;
   223     NM_FUNCTION;
   205     
   224 
   206 	if (index>=0 && index<mItemList.count()) {
   225 	if (index>=0 && index<mItemList.count()) {
   207 	    mItemList.at(index)->setFileSizeText(fileSize);
   226 	    mItemList.at(index)->setFileSizeText(fileSize);
   208 	}
   227 	}
   209 }
   228 }
   210 
   229 
   211 /*!
   230 /*!
   212     Returns attachment(s) count. 
   231     Returns attachment(s) count.
   213  */
   232  */
   214 int NmAttachmentListWidget::count() const
   233 int NmAttachmentListWidget::count() const
   215 {
   234 {
   216     NM_FUNCTION;
   235     NM_FUNCTION;
   217     
   236 
   218     return mItemList.count();
   237     return mItemList.count();
   219 }
   238 }
   220 
   239 
   221 /*!
   240 /*!
   222     Returns value of item, otherwise -1 if index not found from list.
   241     Returns value of item, otherwise -1 if index not found from list.
   223  */
   242  */
   224 int NmAttachmentListWidget::progressValue(int index) const
   243 int NmAttachmentListWidget::progressValue(int index) const
   225 {
   244 {
   226     NM_FUNCTION;
   245     NM_FUNCTION;
   227     
   246 
   228     int ret(NmNotFoundError);
   247     int ret(NmNotFoundError);
   229     if(index >= 0 && index < mItemList.count()){
   248     if(index >= 0 && index < mItemList.count()){
   230         ret = mItemList.at(index)->progressBarValue();
   249         ret = mItemList.at(index)->progressBarValue();
   231     }
   250     }
   232     return ret;
   251     return ret;
   233 }
   252 }
   234 
   253 
   235 /*!
   254 /*!
   236     Hides progress of item, if index is negative or creater 
   255     Hides progress of item, if index is negative or creater
   237     than last index function does nothing
   256     than last index function does nothing
   238  */
   257  */
   239 void NmAttachmentListWidget::hideProgressBar(int index)
   258 void NmAttachmentListWidget::hideProgressBar(int index)
   240 {
   259 {
   241     NM_FUNCTION;
   260     NM_FUNCTION;
   242     
   261 
   243     if(index >= 0 && index < mItemList.count()){
   262     if(index >= 0 && index < mItemList.count()){
   244         mItemList.at(index)->hideProgressBar();
   263         mItemList.at(index)->hideProgressBar();
   245     }
   264     }
   246 }
   265 }
   247 
   266 
   253     QPainter *painter,
   272     QPainter *painter,
   254     const QStyleOptionGraphicsItem *option,
   273     const QStyleOptionGraphicsItem *option,
   255     QWidget *widget)
   274     QWidget *widget)
   256 {
   275 {
   257     NM_FUNCTION;
   276     NM_FUNCTION;
   258     
   277 
   259     Q_UNUSED(option);
   278     Q_UNUSED(option);
   260     Q_UNUSED(widget);
   279     Q_UNUSED(widget);
   261     if (painter&&mLayout){
   280     if (painter&&mLayout){
   262         painter->save();
   281         painter->save();
   263         
   282 
   264         // Use text color as a line color if set, otherwise use theme
   283         // Use text color as a line color if set, otherwise use theme
   265         // normal list content color.
   284         // normal list content color.
   266         painter->setPen(checkColor());
   285         painter->setPen(checkColor());
   267         painter->setOpacity(NmItemLineOpacity);
   286         painter->setOpacity(NmItemLineOpacity);
   268         // Draw line after each item
   287         // Draw line after each item
   269         int rowCount = mLayout->rowCount();
   288         int rowCount = mLayout->rowCount();
   270         QRectF layoutRect = mLayout->geometry ();
   289         QRectF layoutRect = mLayout->geometry ();
   271         for (int i=0;i<rowCount;i++){
   290         for (int i=0;i<rowCount;i++){
   272             QGraphicsLayoutItem *item = mLayout->itemAt(i,0);
   291             QGraphicsLayoutItem *item = mLayout->itemAt(i,0);
   273             if (item){
   292             if (item){
   274                 QRectF itemRect = item->geometry();      
   293 			    qreal offsetY = 0.5;
   275                 QLineF line1( itemRect.topLeft().x(), itemRect.bottomRight().y(),
   294                 QRectF itemRect = item->geometry();
   276                               layoutRect.bottomRight().x(), itemRect.bottomRight().y());
   295                 QLineF line1( itemRect.topLeft().x(), itemRect.bottomRight().y()+offsetY,
   277                 painter->drawLine(line1);                     
   296                               layoutRect.bottomRight().x(), itemRect.bottomRight().y()+offsetY);
   278             }     
   297                 painter->drawLine(line1);
       
   298             }
   279         }
   299         }
   280         painter->restore();
   300         painter->restore();
   281     }
   301     }
   282 }
   302 }
   283 
   303 
   286     with match index and positive value (1-100). Zero value hides progress bar.
   306     with match index and positive value (1-100). Zero value hides progress bar.
   287  */
   307  */
   288 void NmAttachmentListWidget::setProgressBarValue(int index, int value)
   308 void NmAttachmentListWidget::setProgressBarValue(int index, int value)
   289 {
   309 {
   290     NM_FUNCTION;
   310     NM_FUNCTION;
   291     
   311 
   292     if(index >= 0 && index < mItemList.count()){
   312     if(index >= 0 && index < mItemList.count()){
   293         mItemList[index]->setProgressBarValue(value);
   313         mItemList[index]->setProgressBarValue(value);
   294     }
   314     }
   295 }
   315 }
   296 
   316 
   298     Helper function for initialize object.
   318     Helper function for initialize object.
   299  */
   319  */
   300 void NmAttachmentListWidget::init( )
   320 void NmAttachmentListWidget::init( )
   301 {
   321 {
   302     NM_FUNCTION;
   322     NM_FUNCTION;
   303     
   323 
   304     //Get mainwindow for orientation changes
   324     //Get mainwindow for orientation changes
   305     HbMainWindow *mw = hbInstance->allMainWindows().at(0);
   325     HbMainWindow *mw = hbInstance->allMainWindows().at(0);
   306 
   326 
   307     //connect to mainwindow signal
   327     //connect to mainwindow signal
   308     if(mw){
   328     if(mw){
   314 
   334 
   315     //construct UI after orientation has been figured out
   335     //construct UI after orientation has been figured out
   316     constructUi();
   336     constructUi();
   317 
   337 
   318     //set flags
   338     //set flags
   319     setFlag(QGraphicsItem::ItemIsFocusable);  
   339     setFlag(QGraphicsItem::ItemIsFocusable);
   320     setFlag(QGraphicsItem::ItemHasNoContents,false);
   340     setFlag(QGraphicsItem::ItemHasNoContents,false);
   321 }
   341 }
   322 
   342 
   323 /*!
   343 /*!
   324     Helper function for constructing UI components.
   344     Helper function for constructing UI components.
   325  */
   345  */
   326 void NmAttachmentListWidget::constructUi()
   346 void NmAttachmentListWidget::constructUi()
   327 {
   347 {
   328     NM_FUNCTION;
   348     NM_FUNCTION;
   329     
   349 
   330     setObjectName(QString(ATTACHMENT_WIDGET));
   350     setObjectName(QString(ATTACHMENT_WIDGET));
   331     HbDocumentLoader loader;
   351     HbDocumentLoader loader;
   332     bool loadingOk = false;
   352     bool loadingOk = false;
   333 
   353 
   334     //Pass this widget to documentloader
   354     //Pass this widget to documentloader
   353     Helper slot for handling longpressed signals.
   373     Helper slot for handling longpressed signals.
   354 */
   374 */
   355 void NmAttachmentListWidget::handleLongPressed(QPointF point)
   375 void NmAttachmentListWidget::handleLongPressed(QPointF point)
   356 {
   376 {
   357     NM_FUNCTION;
   377     NM_FUNCTION;
   358     
   378 
   359     QObject *sender = QObject::sender();
   379     QObject *sender = QObject::sender();
   360     int index = findItem(sender);
   380     int index = findItem(sender);
   361     if(NmNotFoundError != index){
   381     if(NmNotFoundError != index){
   362         emit longPressed(index, point);
   382         emit longPressed(index, point);
   363     }
   383     }
   371     Helper slot for handling activated signals.
   391     Helper slot for handling activated signals.
   372 */
   392 */
   373 void NmAttachmentListWidget::handleItemActivated()
   393 void NmAttachmentListWidget::handleItemActivated()
   374 {
   394 {
   375     NM_FUNCTION;
   395     NM_FUNCTION;
   376     
   396 
   377     QObject *sender = QObject::sender();
   397     QObject *sender = QObject::sender();
   378     int index = findItem(sender);
   398     int index = findItem(sender);
   379     if(NmNotFoundError != index){
   399     if(NmNotFoundError != index){
   380         emit itemActivated(index);
   400         emit itemActivated(index);
   381     }
   401     }
   389     Helper slot for handling orientation change.
   409     Helper slot for handling orientation change.
   390 */
   410 */
   391 void NmAttachmentListWidget::orientationChanged(Qt::Orientation orientation)
   411 void NmAttachmentListWidget::orientationChanged(Qt::Orientation orientation)
   392 {
   412 {
   393     NM_FUNCTION;
   413     NM_FUNCTION;
   394     
   414 
   395     //be sure that orientation has been changed
   415     //be sure that orientation has been changed
   396     if(mOrientation != orientation){
   416     if(mOrientation != orientation){
   397         mOrientation = orientation;
   417         mOrientation = orientation;
   398         rearrangeLayout();
   418         rearrangeLayout();
   399     }
   419     }
   406     \return int as index
   426     \return int as index
   407 */
   427 */
   408 int NmAttachmentListWidget::findItem(const QObject *obj)
   428 int NmAttachmentListWidget::findItem(const QObject *obj)
   409 {
   429 {
   410     NM_FUNCTION;
   430     NM_FUNCTION;
   411     
   431 
   412     int found(NmNotFoundError);
   432     int found(NmNotFoundError);
   413     int index(0);
   433     int index(0);
   414 
   434 
   415     QListIterator<NmAttachmentListItem*> iter(mItemList);
   435     QListIterator<NmAttachmentListItem*> iter(mItemList);
   416     while(iter.hasNext() && found == NmNotFoundError){
   436     while(iter.hasNext() && found == NmNotFoundError){
   428     \param NmAttachmentListItem item to add to the layout
   448     \param NmAttachmentListItem item to add to the layout
   429 */
   449 */
   430 void NmAttachmentListWidget::insertItemToLayout(NmAttachmentListItem* item)
   450 void NmAttachmentListWidget::insertItemToLayout(NmAttachmentListItem* item)
   431 {
   451 {
   432     NM_FUNCTION;
   452     NM_FUNCTION;
   433     
   453 
   434     if(!mLayout) {
   454     if(!mLayout) {
   435         NM_ERROR(1,"NmAttachmentListWidget::insertItemToLayout: Layout loading failed!");
   455         NM_ERROR(1,"NmAttachmentListWidget::insertItemToLayout: Layout loading failed!");
   436         return;
   456         return;
   437     }
   457     }
   438     int layout_count = mLayout->count();
   458     int layout_count = mLayout->count();
   451     Helper function for rearrange layout after removing or layout change
   471     Helper function for rearrange layout after removing or layout change
   452 */
   472 */
   453 void NmAttachmentListWidget::rearrangeLayout()
   473 void NmAttachmentListWidget::rearrangeLayout()
   454 {
   474 {
   455     NM_FUNCTION;
   475     NM_FUNCTION;
   456     
   476 
   457     if(!mLayout) {
   477     if(!mLayout) {
   458         NM_ERROR(1,"NmAttachmentListWidget::rearrangeLayout: Layout loading failed!");
   478         NM_ERROR(1,"NmAttachmentListWidget::rearrangeLayout: Layout loading failed!");
   459         return;
   479         return;
   460     }
   480     }
   461     
   481 
   462     //remove all items from the layout
   482     //remove all items from the layout
   463     int count(mLayout->count());
   483     int count(mLayout->count());
   464     for(int i = count - 1; i >= 0; --i){
   484     for(int i = count - 1; i >= 0; --i){
   465         mLayout->removeAt(i);
   485         mLayout->removeAt(i);
   466     }
   486     }
   467 
   487 
   468     //then add them back
   488     //then add them back
   469     QColor textColor = checkColor();    
   489     QColor textColor = checkColor();
   470     foreach(NmAttachmentListItem *item, mItemList){
   490     foreach(NmAttachmentListItem *item, mItemList){
   471         item->setTextColor(textColor);
   491         item->setTextColor(textColor);
   472         insertItemToLayout(item);
   492         insertItemToLayout(item);
   473     }
   493     }
   474 }
   494 }
   477     Helper function to set text color
   497     Helper function to set text color
   478 */
   498 */
   479 QColor NmAttachmentListWidget::checkColor()
   499 QColor NmAttachmentListWidget::checkColor()
   480 {
   500 {
   481     NM_FUNCTION;
   501     NM_FUNCTION;
   482     
   502 
   483     QColor retColor;
   503     QColor retColor;
   484 
   504 
   485     if (mTextColor.isValid()){
   505     if (mTextColor.isValid()){
   486         retColor = mTextColor;
   506         retColor = mTextColor;
   487     }
   507     }
   488     else {    
   508     else {
   489         retColor =  HbColorScheme::color(NmAttachmentTextColor);
   509         retColor =  HbColorScheme::color(NmAttachmentTextColor);
   490     }
   510     }
   491     return retColor;
   511     return retColor;
   492 }
   512 }
   493 
   513 
   498 void NmAttachmentListWidget::changeEvent(QEvent *event)
   518 void NmAttachmentListWidget::changeEvent(QEvent *event)
   499 {
   519 {
   500     NM_FUNCTION;
   520     NM_FUNCTION;
   501 
   521 
   502     if (event->type() == HbEvent::ThemeChanged) {
   522     if (event->type() == HbEvent::ThemeChanged) {
   503         rearrangeLayout();    
   523         rearrangeLayout();
   504     }
   524     }
   505     return HbWidgetBase::changeEvent(event);
   525     return HbWidgetBase::changeEvent(event);
   506 }
   526 }