emailuis/nmailuiwidgets/src/nmattachmentlistwidget.cpp
changeset 43 99bcbff212ad
parent 23 2dc6caa42ec3
child 44 c2d07d913565
equal deleted inserted replaced
42:139d4b7b2938 43:99bcbff212ad
   104 NmAttachmentListWidget::NmAttachmentListWidget(QGraphicsItem *parent)
   104 NmAttachmentListWidget::NmAttachmentListWidget(QGraphicsItem *parent)
   105     : HbWidget( parent ),
   105     : HbWidget( parent ),
   106     mLayout(NULL),
   106     mLayout(NULL),
   107     mOrientation(Qt::Vertical)
   107     mOrientation(Qt::Vertical)
   108 {
   108 {
       
   109     NM_FUNCTION;
       
   110     
   109     init( );
   111     init( );
   110 }
   112 }
   111 
   113 
   112 
   114 
   113 /*!
   115 /*!
   114     Destructor.
   116     Destructor.
   115  */
   117  */
   116 NmAttachmentListWidget::~NmAttachmentListWidget( )
   118 NmAttachmentListWidget::~NmAttachmentListWidget( )
   117 {
   119 {
       
   120     NM_FUNCTION;
       
   121     
   118     mItemList.clear();
   122     mItemList.clear();
   119 }
   123 }
   120 
   124 
   121 /*!
   125 /*!
   122     Setter for items text color override. This fucntion can be used
   126     Setter for items text color override. This fucntion can be used
   123     if theme background is not used and text needs to be shown in diferent color.
   127     if theme background is not used and text needs to be shown in diferent color.
   124  */
   128  */
   125 void NmAttachmentListWidget::setTextColor(const QColor color)
   129 void NmAttachmentListWidget::setTextColor(const QColor color)
   126 {
   130 {
       
   131     NM_FUNCTION;
       
   132     
   127     mTextColor=color;
   133     mTextColor=color;
   128 }
   134 }
   129 
   135 
   130 
   136 
   131 /*!
   137 /*!
   135 void NmAttachmentListWidget::insertAttachment(
   141 void NmAttachmentListWidget::insertAttachment(
   136         int index, 
   142         int index, 
   137         const QString &fileName, 
   143         const QString &fileName, 
   138         const QString &fileSize)
   144         const QString &fileSize)
   139 {   
   145 {   
       
   146     NM_FUNCTION;
       
   147     
   140     NmAttachmentListItem *item = new NmAttachmentListItem(this);
   148     NmAttachmentListItem *item = new NmAttachmentListItem(this);
   141     item->setObjectName(QString("nmattachmentlistitem_%1").arg(index));
   149     item->setObjectName(QString("nmattachmentlistitem_%1").arg(index));
   142 
   150 
   143     //connect to signals
   151     //connect to signals
   144     connect(item, SIGNAL(itemActivated()), this, SLOT(handleItemActivated()));
   152     connect(item, SIGNAL(itemActivated()), this, SLOT(handleItemActivated()));
   161 /*!
   169 /*!
   162     Removes attachment from given index.
   170     Removes attachment from given index.
   163  */
   171  */
   164 void NmAttachmentListWidget::removeAttachment(int index)
   172 void NmAttachmentListWidget::removeAttachment(int index)
   165 {
   173 {
       
   174     NM_FUNCTION;
       
   175     
   166     if(!mLayout) {
   176     if(!mLayout) {
   167         NMLOG("NmAttachmentListWidget::removeAttachment: Layout loading failed!");
   177         NM_ERROR(1,"NmAttachmentListWidget::removeAttachment(): layout loading failed");
   168         return;
   178         return;
   169     }
   179     }
   170 
   180 
   171     if(index >= 0 && index < mItemList.count())
   181     if(index >= 0 && index < mItemList.count())
   172     {
   182     {
   187  */
   197  */
   188 void NmAttachmentListWidget::setAttachmentSize(
   198 void NmAttachmentListWidget::setAttachmentSize(
   189         int index, 
   199         int index, 
   190         const QString &fileSize)
   200         const QString &fileSize)
   191 {
   201 {
       
   202     NM_FUNCTION;
       
   203     
   192 	if (index>=0 && index<mItemList.count()) {
   204 	if (index>=0 && index<mItemList.count()) {
   193 	    mItemList.at(index)->setFileSizeText(fileSize);
   205 	    mItemList.at(index)->setFileSizeText(fileSize);
   194 	}
   206 	}
   195 }
   207 }
   196 
   208 
   197 /*!
   209 /*!
   198     Returns attachment(s) count. 
   210     Returns attachment(s) count. 
   199  */
   211  */
   200 int NmAttachmentListWidget::count() const
   212 int NmAttachmentListWidget::count() const
   201 {
   213 {
       
   214     NM_FUNCTION;
       
   215     
   202     return mItemList.count();
   216     return mItemList.count();
   203 }
   217 }
   204 
   218 
   205 /*!
   219 /*!
   206     Returns value of item, otherwise -1 if index not found from list.
   220     Returns value of item, otherwise -1 if index not found from list.
   207  */
   221  */
   208 int NmAttachmentListWidget::progressValue(int index) const
   222 int NmAttachmentListWidget::progressValue(int index) const
   209 {
   223 {
       
   224     NM_FUNCTION;
       
   225     
   210     int ret(NmNotFoundError);
   226     int ret(NmNotFoundError);
   211     if(index >= 0 && index < mItemList.count()){
   227     if(index >= 0 && index < mItemList.count()){
   212         ret = mItemList.at(index)->progressBarValue();
   228         ret = mItemList.at(index)->progressBarValue();
   213     }
   229     }
   214     return ret;
   230     return ret;
   218     Hides progress of item, if index is negative or creater 
   234     Hides progress of item, if index is negative or creater 
   219     than last index function does nothing
   235     than last index function does nothing
   220  */
   236  */
   221 void NmAttachmentListWidget::hideProgressBar(int index)
   237 void NmAttachmentListWidget::hideProgressBar(int index)
   222 {
   238 {
       
   239     NM_FUNCTION;
       
   240     
   223     if(index >= 0 && index < mItemList.count()){
   241     if(index >= 0 && index < mItemList.count()){
   224         mItemList.at(index)->hideProgressBar();
   242         mItemList.at(index)->hideProgressBar();
   225     }
   243     }
   226 }
   244 }
   227 
   245 
   232 void NmAttachmentListWidget::paint(
   250 void NmAttachmentListWidget::paint(
   233     QPainter *painter,
   251     QPainter *painter,
   234     const QStyleOptionGraphicsItem *option,
   252     const QStyleOptionGraphicsItem *option,
   235     QWidget *widget)
   253     QWidget *widget)
   236 {
   254 {
       
   255     NM_FUNCTION;
       
   256     
   237     Q_UNUSED(option);
   257     Q_UNUSED(option);
   238     Q_UNUSED(widget);
   258     Q_UNUSED(widget);
   239     if (painter&&mLayout){
   259     if (painter&&mLayout){
   240         // Use text color as a line color if set, otherwise use theme
   260         // Use text color as a line color if set, otherwise use theme
   241         // normal list content color.
   261         // normal list content color.
   268     Public slot connected to set items value. Shows progress bar when called
   288     Public slot connected to set items value. Shows progress bar when called
   269     with match index and positive value (1-100). Zero value hides progress bar.
   289     with match index and positive value (1-100). Zero value hides progress bar.
   270  */
   290  */
   271 void NmAttachmentListWidget::setProgressBarValue(int index, int value)
   291 void NmAttachmentListWidget::setProgressBarValue(int index, int value)
   272 {
   292 {
       
   293     NM_FUNCTION;
       
   294     
   273     if(index >= 0 && index < mItemList.count()){
   295     if(index >= 0 && index < mItemList.count()){
   274         mItemList[index]->setProgressBarValue(value);
   296         mItemList[index]->setProgressBarValue(value);
   275     }
   297     }
   276 }
   298 }
   277 
   299 
   278 /*!
   300 /*!
   279     Helper function for initialize object.
   301     Helper function for initialize object.
   280  */
   302  */
   281 void NmAttachmentListWidget::init( )
   303 void NmAttachmentListWidget::init( )
   282 {
   304 {
       
   305     NM_FUNCTION;
       
   306     
   283     //Get mainwindow for orientation changes
   307     //Get mainwindow for orientation changes
   284     HbMainWindow *mw = hbInstance->allMainWindows().at(0);
   308     HbMainWindow *mw = hbInstance->allMainWindows().at(0);
   285 
   309 
   286     //connect to mainwindow signal
   310     //connect to mainwindow signal
   287     if(mw){
   311     if(mw){
   288         connect(mw, SIGNAL(orientationChanged(Qt::Orientation)),this, SLOT(orientationChanged(Qt::Orientation)));
   312         connect(mw, SIGNAL(orientationChanged(Qt::Orientation)),this, SLOT(orientationChanged(Qt::Orientation)));
   289         mOrientation = mw->orientation();
   313         mOrientation = mw->orientation();
   290     } else {
   314     } else {
   291         NMLOG("NmAttachmentListWidget::init: mainWindow missing!");
   315         NM_ERROR(1,"NmAttachmentListWidget::init: mainWindow missing!");
   292     }
   316     }
   293 
   317 
   294     //construct UI after orientation has been figured out
   318     //construct UI after orientation has been figured out
   295     constructUi();
   319     constructUi();
   296 
   320 
   301 /*!
   325 /*!
   302     Helper function for constructing UI components.
   326     Helper function for constructing UI components.
   303  */
   327  */
   304 void NmAttachmentListWidget::constructUi()
   328 void NmAttachmentListWidget::constructUi()
   305 {
   329 {
       
   330     NM_FUNCTION;
       
   331     
   306     setObjectName(QString(ATTACHMENT_WIDGET));
   332     setObjectName(QString(ATTACHMENT_WIDGET));
   307     HbDocumentLoader loader;
   333     HbDocumentLoader loader;
   308     bool loadingOk = false;
   334     bool loadingOk = false;
   309 
   335 
   310     //Pass this widget to documentloader
   336     //Pass this widget to documentloader
   317     if(loadingOk && widgetCount){
   343     if(loadingOk && widgetCount){
   318         if(layout()){
   344         if(layout()){
   319             mLayout = dynamic_cast<QGraphicsGridLayout*>(layout());
   345             mLayout = dynamic_cast<QGraphicsGridLayout*>(layout());
   320             mLayout->setContentsMargins(0,0,0,0);
   346             mLayout->setContentsMargins(0,0,0,0);
   321         } else {
   347         } else {
   322             NMLOG("NmAttachmentListWidget::constructUi: Widget doesn't have layout!");
   348             NM_ERROR(1,"NmAttachmentListWidget::constructUi: Widget doesn't have layout!");
   323         }
   349         }
   324     } else {
   350     } else {
   325         NMLOG("NmAttachmentListWidget::constructUi: DocML loading failed.");
   351         NM_ERROR(1,"NmAttachmentListWidget::constructUi: DocML loading failed.");
   326     }
   352     }
   327 
   353 
   328 }
   354 }
   329 
   355 
   330 /*!
   356 /*!
   331     Helper slot for handling longpressed signals.
   357     Helper slot for handling longpressed signals.
   332 */
   358 */
   333 void NmAttachmentListWidget::handleLongPressed(QPointF point)
   359 void NmAttachmentListWidget::handleLongPressed(QPointF point)
   334 {
   360 {
       
   361     NM_FUNCTION;
       
   362     
   335     QObject *sender = QObject::sender();
   363     QObject *sender = QObject::sender();
   336     int index = findItem(sender);
   364     int index = findItem(sender);
   337     if(NmNotFoundError != index){
   365     if(NmNotFoundError != index){
   338         emit longPressed(index, point);
   366         emit longPressed(index, point);
   339     }
   367     }
   340     else {
   368     else {
   341         NMLOG("NmAttachmentListWidget::handleLongPressed: item cannot found!");
   369         NM_ERROR(1,"NmAttachmentListWidget::handleLongPressed: item cannot found!");
   342     }
   370     }
   343 
   371 
   344 }
   372 }
   345 
   373 
   346 /*!
   374 /*!
   347     Helper slot for handling activated signals.
   375     Helper slot for handling activated signals.
   348 */
   376 */
   349 void NmAttachmentListWidget::handleItemActivated()
   377 void NmAttachmentListWidget::handleItemActivated()
   350 {
   378 {
       
   379     NM_FUNCTION;
       
   380     
   351     QObject *sender = QObject::sender();
   381     QObject *sender = QObject::sender();
   352     int index = findItem(sender);
   382     int index = findItem(sender);
   353     if(NmNotFoundError != index){
   383     if(NmNotFoundError != index){
   354         emit itemActivated(index);
   384         emit itemActivated(index);
   355     }
   385     }
   356     else {
   386     else {
   357         NMLOG("NmAttachmentListWidget::handleItemActivated: item cannot found!");
   387         NM_ERROR(1,"NmAttachmentListWidget::handleItemActivated: item cannot found!");
   358     }
   388     }
   359 }
   389 }
   360 
   390 
   361 
   391 
   362 /*!
   392 /*!
   363     Helper slot for handling orientation change.
   393     Helper slot for handling orientation change.
   364 */
   394 */
   365 void NmAttachmentListWidget::orientationChanged(Qt::Orientation orientation)
   395 void NmAttachmentListWidget::orientationChanged(Qt::Orientation orientation)
   366 {
   396 {
   367     NMLOG("NmAttachmentListWidget::orientationChanged");
   397     NM_FUNCTION;
   368 
   398     
   369     //be sure that orientation has been changed
   399     //be sure that orientation has been changed
   370     if(mOrientation != orientation){
   400     if(mOrientation != orientation){
   371         mOrientation = orientation;
   401         mOrientation = orientation;
   372         rearrangeLayout();
   402         rearrangeLayout();
   373     }
   403     }
   379     \param QObject searched item.
   409     \param QObject searched item.
   380     \return int as index
   410     \return int as index
   381 */
   411 */
   382 int NmAttachmentListWidget::findItem(const QObject *obj)
   412 int NmAttachmentListWidget::findItem(const QObject *obj)
   383 {
   413 {
       
   414     NM_FUNCTION;
       
   415     
   384     int found(NmNotFoundError);
   416     int found(NmNotFoundError);
   385     int index(0);
   417     int index(0);
   386 
   418 
   387     QListIterator<NmAttachmentListItem*> iter(mItemList);
   419     QListIterator<NmAttachmentListItem*> iter(mItemList);
   388     while(iter.hasNext() && found == NmNotFoundError){
   420     while(iter.hasNext() && found == NmNotFoundError){
   399     Helper function for calculating items correct place on layout
   431     Helper function for calculating items correct place on layout
   400     \param NmAttachmentListItem item to add to the layout
   432     \param NmAttachmentListItem item to add to the layout
   401 */
   433 */
   402 void NmAttachmentListWidget::insertItemToLayout(NmAttachmentListItem* item)
   434 void NmAttachmentListWidget::insertItemToLayout(NmAttachmentListItem* item)
   403 {
   435 {
       
   436     NM_FUNCTION;
       
   437     
   404     if(!mLayout) {
   438     if(!mLayout) {
   405         NMLOG("NmAttachmentListWidget::insertItemToLayout: Layout loading failed!");
   439         NM_ERROR(1,"NmAttachmentListWidget::insertItemToLayout: Layout loading failed!");
   406         return;
   440         return;
   407     }
   441     }
   408     int layout_count = mLayout->count();
   442     int layout_count = mLayout->count();
   409 
   443 
   410     //check which orientation in use
   444     //check which orientation in use
   422 /*!
   456 /*!
   423     Helper function for rearrange layout after removing or layout change
   457     Helper function for rearrange layout after removing or layout change
   424 */
   458 */
   425 void NmAttachmentListWidget::rearrangeLayout()
   459 void NmAttachmentListWidget::rearrangeLayout()
   426 {
   460 {
       
   461     NM_FUNCTION;
       
   462     
   427     if(!mLayout) {
   463     if(!mLayout) {
   428         NMLOG("NmAttachmentListWidget::rearrangeLayout: Layout loading failed!");
   464         NM_ERROR(1,"NmAttachmentListWidget::rearrangeLayout: Layout loading failed!");
   429         return;
   465         return;
   430     }
   466     }
   431 
   467 
   432     //remove all items from the layout
   468     //remove all items from the layout
   433     int count(mLayout->count());
   469     int count(mLayout->count());