src/hbwidgets/itemviews/hblistviewitem.cpp
changeset 34 ed14f46c0e55
parent 6 c3690ec91ef8
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    31 #include <hbicon.h>
    31 #include <hbicon.h>
    32 #include <hbiconitem.h>
    32 #include <hbiconitem.h>
    33 #include <hbtextitem.h>
    33 #include <hbtextitem.h>
    34 #include <hbrichtextitem.h>
    34 #include <hbrichtextitem.h>
    35 #include <hbstyle.h>
    35 #include <hbstyle.h>
    36 #include <hbstyleoptionlistviewitem_p.h>
    36 #include <hbstyleprimitivedata.h>
       
    37 #include <hbstyletextprimitivedata.h>
       
    38 #include <hbstylerichtextprimitivedata.h>
       
    39 #include <hbstyleiconprimitivedata.h>
    37 
    40 
    38 #include <QPersistentModelIndex>
    41 #include <QPersistentModelIndex>
    39 #include <QVariant>
    42 #include <QVariant>
    40 #include <QItemSelectionModel>
    43 #include <QItemSelectionModel>
    41 #include <QStyleOption>
       
    42 #include <QSizePolicy>
    44 #include <QSizePolicy>
    43 
    45 
    44 /*!
    46 /*!
    45     @beta
    47     @beta
    46     @hbwidgets
    48     @hbwidgets
    47     \class HbListViewItem
    49     \class HbListViewItem
    48     \brief The HbListViewItem class represents a single row in a list.
    50     \brief The HbListViewItem class represents a single row in a list.
    49 
    51 
    50     The HbListViewItem class provides an item that is used by the HbListView class to
    52     The HbListViewItem class provides an item that is used by the HbListView class to
    51     visualize content within single model index. By default HbListViewItem supports 
    53     visualize content within single model index. 
    52     QStringList that is stored into Qt::DisplayRole role and a list of QIcons or 
    54     
    53     HbIcons that is stored into Qt::DecoratorRole role within the index. 
    55     HbListViewItem supports following model item types in Hb::ItemTypeRole role of data model
       
    56     \li Hb::StandardItem. 
       
    57     \li Hb::SeparatorItem. 
       
    58     HbAbstractViewItem documentation provides details how these item types distinguish from each other. 
       
    59 
       
    60     Following item data roles are supported by HbListViewItem
       
    61     \li QString and QStringList stored in Qt::DisplayRole role of the data model
       
    62     \li QIcon, HbIcon or list of them in QVariantList stored in Qt::DecoratorRole role. 
       
    63     QIcon is supported only for compatibility reasons. If QIcon is used the limitations 
       
    64     described in the HbIcon::HbIcon(const QIcon &icon) apply.
       
    65     
       
    66     Handling Qt::BackgroundRole item data role takes place in base class HbAbstractViewItem.
    54 
    67 
    55     This class is provided mainly for customization purposes but it also acts as a default
    68     This class is provided mainly for customization purposes but it also acts as a default
    56     item prototype inside HbListView. See HbListView how to set customized class as a item prototype.
    69     item prototype inside HbListView. See HbListView how to set customized class as a item prototype.
    57 
    70 
    58     \b Subclassing
    71     \b Subclassing
   100     Below is an example how the layout properties can be configured.
   113     Below is an example how the layout properties can be configured.
   101 
   114 
   102     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,45}
   115     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,45}
   103 
   116 
   104     \primitives
   117     \primitives
   105     \primitive{icon-1} HbIconItem representing the icon-1 as described in the table above. 
   118     \primitive{icon-1} HbIconItem with item name "icon-1" representing icon-1 as described in the table above. 
   106     \primitive{icon-2} HbIconItem representing the icon-2 as described in the table above. 
   119     \primitive{icon-2} HbIconItem with item name "icon-2" representing the icon-2 as described in the table above. 
   107     \primitive{text-1} HbTextItem or HbRichTextItem representing the text-1 as described in the table above. The type of the return value depends on the textFormat() of the item.
   120     \primitive{text-1} HbTextItem or HbRichTextItem with item name "text-1" representing the text-1 as described in the table above. The type of the return value depends on the textFormat() of the item.
   108     \primitive{text-2} HbTextItem or HbRichTextItem representing the text-2 as described in the table above.
   121     \primitive{text-2} HbTextItem or HbRichTextItem with item name "text-2" representing the text-2 as described in the table above.
   109     \primitive{text-3} HbTextItem or HbRichTextItem representing the text-3 as described in the table above.
   122     \primitive{text-3} HbTextItem or HbRichTextItem with item name "text-3" representing the text-3 as described in the table above.
   110 
   123 
       
   124     \sa HbAbstractViewItem
   111 */
   125 */
   112 
   126 
   113 /*!
   127 /*!
   114     \enum HbListViewItem::StretchingStyle
   128     \enum HbListViewItem::StretchingStyle
   115 
   129 
   217                                        const int index)
   231                                        const int index)
   218 {
   232 {
   219     Q_Q( HbListViewItem );
   233     Q_Q( HbListViewItem );
   220 
   234 
   221     // create icon item and set it to layout
   235     // create icon item and set it to layout
   222     HbStyle::Primitive primitive = decorationPrimitive(value);
   236      HbStyle::PrimitiveType primitive = decorationPrimitive(value);
   223 
   237 
   224     if (primitive != HbStyle::P_None) {
   238      if (primitive != HbStyle::PT_None) {
   225         QGraphicsItem *item = mDecorationRoleItems.value(index);
   239         QGraphicsObject *item = mDecorationRoleItems.value(index);
   226         if (!item) {
   240         if (!item) {
   227             mItemsChanged = true;
   241             mItemsChanged = true;
   228             themingPending = true;
   242             themingPending = true;
   229             item = q->style()->createPrimitive(primitive, q);
   243 
       
   244             QString name;
       
   245             if (index == 0) {
       
   246                 name = QLatin1String("icon-1");
       
   247             } else if (index == 1) {
       
   248                 name = QLatin1String("icon-2");
       
   249             } else {
       
   250                 name = QLatin1String("icon-") + QString::number(index + 1);
       
   251             }
       
   252 
       
   253             item = q->style()->createPrimitive(primitive, name, 0);
       
   254             item->setParentItem(q); // To enable asynchronous icon loading.
       
   255             item->setZValue(index + 1);
   230 
   256 
   231             if (index < mDecorationRoleItems.count()) {
   257             if (index < mDecorationRoleItems.count()) {
   232                 mDecorationRoleItems.replace(index, item);
   258                 mDecorationRoleItems.replace(index, item);
   233             } else {
   259             } else {
   234                 mDecorationRoleItems.append(item);
   260                 mDecorationRoleItems.append(item);
   250 {
   276 {
   251     Q_Q( HbListViewItem );
   277     Q_Q( HbListViewItem );
   252 
   278 
   253     // create text item  and set it to layout
   279     // create text item  and set it to layout
   254     if (!value.isNull()) {
   280     if (!value.isNull()) {
   255         QGraphicsItem *textItem = mDisplayRoleTextItems.value(index);
   281         QGraphicsObject *textItem = mDisplayRoleTextItems.value(index);
   256 
   282 
   257         HbStyle::Primitive primitive = displayPrimitive();
   283         HbStyle::PrimitiveType primitive = displayPrimitive();
   258         if (!textItem) {
   284         if (!textItem) {
   259             mItemsChanged = true;
   285             mItemsChanged = true;
   260             themingPending = true;
   286             themingPending = true;
   261             textItem = q->style()->createPrimitive(primitive, q);
   287 
       
   288             QString name;
       
   289             if (index == 0) {
       
   290                 name = QLatin1String("text-1");
       
   291             } else if (index == 1) {
       
   292                 name = QLatin1String("text-2");
       
   293             } else if (index == 2) {
       
   294                 name = QLatin1String("text-3");
       
   295             } else {
       
   296                 name = QLatin1String("text-") + QString::number(index + 1);
       
   297             }
       
   298 
       
   299             textItem = q->style()->createPrimitive(primitive, name, q);
       
   300             HbTextItem *realTextItem = qobject_cast<HbTextItem*>(textItem);
       
   301             if (realTextItem) {
       
   302                 realTextItem->setTextWrapping(Hb::TextNoWrap);
       
   303             }
   262             if (index < mDisplayRoleTextItems.count()) {
   304             if (index < mDisplayRoleTextItems.count()) {
   263                 mDisplayRoleTextItems.replace(index, textItem);
   305                 mDisplayRoleTextItems.replace(index, textItem);
   264             } else {
   306             } else {
   265                 mDisplayRoleTextItems.append(textItem);
   307                 mDisplayRoleTextItems.append(textItem);
   266             }
   308             }
   358 
   400 
   359     // DisplayRoles
   401     // DisplayRoles
   360     QVariant displayRole = d->mIndex.data(Qt::DisplayRole);
   402     QVariant displayRole = d->mIndex.data(Qt::DisplayRole);
   361     QStringList stringList;
   403     QStringList stringList;
   362     if (displayRole.isValid()) {
   404     if (displayRole.isValid()) {
   363         if (displayRole.canConvert<QString>()) {
   405         if (displayRole.type() == QVariant::String) {
       
   406             stringList.append(displayRole.toString());
       
   407         } else if (displayRole.type() == QVariant::StringList) {
       
   408             stringList = displayRole.toStringList();
       
   409         } else if (displayRole.canConvert<QString>()) {
   364             stringList.append(displayRole.toString());
   410             stringList.append(displayRole.toString());
   365         } else if (displayRole.canConvert<QStringList>()) {
   411         } else if (displayRole.canConvert<QStringList>()) {
   366             stringList = displayRole.toStringList();
   412             stringList = displayRole.toStringList();
   367         }
   413         }
   368     }
   414     }
   383 
   429 
   384     // decorationroles
   430     // decorationroles
   385     QVariant decorationRole = d->mIndex.data(Qt::DecorationRole);
   431     QVariant decorationRole = d->mIndex.data(Qt::DecorationRole);
   386     QVariantList variantList;
   432     QVariantList variantList;
   387     if (decorationRole.isValid()) {
   433     if (decorationRole.isValid()) {
   388         if (decorationRole.canConvert<QIcon>()
   434         if (decorationRole.type() == QVariant::Icon 
       
   435             || decorationRole.userType() == qMetaTypeId<HbIcon>()) {
       
   436             variantList.append(decorationRole);
       
   437         } else if (decorationRole.type() == QVariant::List) { 
       
   438             variantList = decorationRole.toList();
       
   439         } else if (decorationRole.canConvert<QIcon>()
   389              || decorationRole.canConvert<HbIcon>()) {
   440              || decorationRole.canConvert<HbIcon>()) {
   390             variantList.append(decorationRole);
   441             variantList.append(decorationRole);
   391         } else if (decorationRole.canConvert< QList<QVariant> >()) {
   442         } else if (decorationRole.canConvert< QList<QVariant> >()) {
   392             variantList = decorationRole.toList();
   443             variantList = decorationRole.toList();
   393         }
   444         }
   408 
   459 
   409     HbAbstractViewItem::updateChildItems();
   460     HbAbstractViewItem::updateChildItems();
   410 }
   461 }
   411 
   462 
   412 /*!
   463 /*!
       
   464   Initializes the HbListViewItem primitive data. 
       
   465   
       
   466   This function calls HbWidgetBase::initPrimitiveData().
       
   467   \a primitiveData is data object, which is populated with data. \a primitive is the primitive.
       
   468   \a index is used to index data item in the internal store.
       
   469 */
       
   470 void HbListViewItem::initPrimitiveData(HbStylePrimitiveData     *primitiveData, 
       
   471                                        const QGraphicsObject    *primitive,
       
   472                                        int                      index)
       
   473 {
       
   474     Q_ASSERT_X(primitive && primitiveData, "HbListViewItem::initPrimitiveData" , "NULL data not permitted");
       
   475     HB_SDD(HbListViewItem);
       
   476 
       
   477     HbWidgetBase::initPrimitiveData(primitiveData, primitive);
       
   478     if (primitiveData->type == HbStylePrimitiveData::SPD_Text) {
       
   479         HbStyleTextPrimitiveData *textPrimitiveData = hbstyleprimitivedata_cast<HbStyleTextPrimitiveData*>(primitiveData);
       
   480 
       
   481          textPrimitiveData->text = d->mStringList.at(index);
       
   482 
       
   483         if (index == 1) {
       
   484             textPrimitiveData->textWrapping = Hb::TextNoWrap;
       
   485             textPrimitiveData->elideMode = Qt::ElideNone;
       
   486             if (d->isMultilineSupported()) {
       
   487                 if (sd->mMinimumSecondaryTextRowCount != -1) {
       
   488                     // min & max secondary text row counts set by app
       
   489                     if (sd->mMaximumSecondaryTextRowCount != 1) {
       
   490                         textPrimitiveData->textWrapping = Hb::TextWordWrap;
       
   491                         textPrimitiveData->elideMode = Qt::ElideRight;
       
   492                     }
       
   493                     textPrimitiveData->minimumLines = sd->mMinimumSecondaryTextRowCount;
       
   494                     textPrimitiveData->maximumLines = sd->mMaximumSecondaryTextRowCount;
       
   495                 }
       
   496                 else {
       
   497                     // min & max secondary text row counts not set by app. Allow setting those from .css
       
   498                     // Needed when multilineSecondaryTextSupported changed from FALSE to TRUE and
       
   499                     // min & max secondary text row counts has not been set by app
       
   500                     HbWidgetBasePrivate *widgetBaseP = HbStylePrivate::widgetBasePrivate(
       
   501                         qobject_cast<HbWidgetBase*>(const_cast<QGraphicsObject*>(primitive)));
       
   502                     if (widgetBaseP) {
       
   503                         widgetBaseP->setApiProtectionFlag(HbWidgetBasePrivate::AC_TextWrapMode, false);
       
   504                         widgetBaseP->setApiProtectionFlag(HbWidgetBasePrivate::AC_TextLinesMin, false);
       
   505                         widgetBaseP->setApiProtectionFlag(HbWidgetBasePrivate::AC_TextLinesMax, false);
       
   506                     }
       
   507                 }
       
   508             } else {
       
   509                 // min & max secondary text row counts must always be 1. They cannot be overridden by .css
       
   510                 textPrimitiveData->minimumLines = 1;
       
   511                 textPrimitiveData->maximumLines = 1;
       
   512             }
       
   513         } 
       
   514     } else if (primitiveData->type == HbStylePrimitiveData::SPD_RichText) {
       
   515         hbstyleprimitivedata_cast<HbStyleRichTextPrimitiveData*>(primitiveData)->text = d->mStringList.at(index);
       
   516     } else if (primitiveData->type == HbStylePrimitiveData::SPD_Icon) {
       
   517         if (d->mDecorationList.at(index).canConvert<HbIcon>()){
       
   518             hbstyleprimitivedata_cast<HbStyleIconPrimitiveData*>(primitiveData)->icon = d->mDecorationList.at(index).value<HbIcon>();
       
   519         } else if (d->mDecorationList.at(index).canConvert<QIcon>()){
       
   520             hbstyleprimitivedata_cast<HbStyleIconPrimitiveData*>(primitiveData)->icon = HbIcon(d->mDecorationList.at(index).value<QIcon>());
       
   521         }
       
   522     }
       
   523 }
       
   524 
       
   525 
       
   526 /*!
   413     \reimp
   527     \reimp
   414 */
   528 */
   415 void HbListViewItem::updatePrimitives()
   529 void HbListViewItem::updatePrimitives()
   416 {
   530 {
   417     HB_SDD(HbListViewItem);
   531     Q_D( HbListViewItem );
   418     HbStyleOptionListViewItem styleOption;
       
   419     initStyleOption(&styleOption);
       
   420 
   532 
   421     int count = d->mStringList.count();
   533     int count = d->mStringList.count();
   422     for (int i = 0; i < count; ++i) {
   534     for (int i = 0; i < count; ++i) {
   423         QGraphicsItem *item = d->mDisplayRoleTextItems.value(i);
   535         QGraphicsObject *item = d->mDisplayRoleTextItems.value(i);
   424         if (item) {
   536         if (item) {
   425             styleOption.index = i;
   537             if (d->displayPrimitive() == HbStyle::PT_TextItem) {
   426             styleOption.content = d->mStringList.at(i);
   538                 HbStyleTextPrimitiveData textPrimitiveData;
   427             styleOption.multilineSecondaryTextSupported = d->isMultilineSupported();
   539                 initPrimitiveData(&textPrimitiveData, item, i);
   428 
   540                 style()->updatePrimitive(item,&textPrimitiveData,this);
   429             if (i == 1) {
   541             } else {
   430                 // criteria of secondary text in middle column is fulfilled
   542                 HbStyleRichTextPrimitiveData richTextPrimitiveData;
   431                 styleOption.minimumLines = sd->mMinimumSecondaryTextRowCount;
   543                 initPrimitiveData(&richTextPrimitiveData, item, i);
   432                 styleOption.maximumLines = sd->mMaximumSecondaryTextRowCount;
   544                 style()->updatePrimitive(item,&richTextPrimitiveData,this);
   433             } 
   545             }
   434             style()->updatePrimitive(item, d->displayPrimitive(), &styleOption);
   546         }
   435         }
   547     }
   436     }
   548 
   437 
       
   438     styleOption.role = Qt::DecorationRole;
       
   439     count = d->mDecorationList.count();
   549     count = d->mDecorationList.count();
   440     for (int i = 0; i < count; ++i) {
   550     for (int i = 0; i < count; ++i) {
   441         QGraphicsItem *item = d->mDecorationRoleItems.value(i);
   551         QGraphicsObject *item = d->mDecorationRoleItems.value(i);
   442         if (item) {
   552         if (item) {
   443             styleOption.index = i;
   553             HbStyleIconPrimitiveData iconPrimitiveData;
   444             styleOption.content = d->mDecorationList.at(i);
   554             initPrimitiveData(&iconPrimitiveData, item, i);
   445             style()->updatePrimitive(   
   555             style()->updatePrimitive(item,&iconPrimitiveData,this);
   446                         item, 
       
   447                         d->decorationPrimitive(d->mDecorationList.at(i)), 
       
   448                         &styleOption);
       
   449         }
   556         }
   450     }
   557     }
   451     HbAbstractViewItem::updatePrimitives();
   558     HbAbstractViewItem::updatePrimitives();
   452 }
   559 }
   453 
   560 
   566     }
   673     }
   567 }
   674 }
   568 
   675 
   569 /*!
   676 /*!
   570     Returns secondary text item maximum and minimum row counts. 
   677     Returns secondary text item maximum and minimum row counts. 
   571 	The default maximum row count is 1 and minimum row count is 1.
   678     The default maximum row count is 1 and minimum row count is 1.
   572 
   679 
   573     \note This function does not return values from .css although they were effective. 
   680     \note This function does not return values from .css although they were effective. 
   574 
   681 
   575     \sa setSecondaryTextRowCount
   682     \sa setSecondaryTextRowCount
   576 */
   683 */