src/hbwidgets/itemviews/hblistwidget.cpp
changeset 6 c3690ec91ef8
parent 3 11d3954df52a
equal deleted inserted replaced
5:627c4a0fd0e7 6:c3690ec91ef8
    41 
    41 
    42 /*!
    42 /*!
    43     @beta
    43     @beta
    44     @hbwidgets
    44     @hbwidgets
    45     \class HbListWidget
    45     \class HbListWidget
    46     \brief HbListWidget represents a list
    46     \brief The HbListWidget class is for simple lists consisting of single row items of an icon and some text, and together with HbListWidgetItem consisting of icon and at most two-row text items.
    47 
    47 
    48     This class has been provided with the class HbListWidgetItem as a convenience
    48     
    49     API. It provides an item-based interface for adding and removing items. 
    49     HbListWidget provides a convenience API for adding, modifying, and removing single row items in simple lists. Single row items consist of some text and an icon in one line. If you want list items with two texts or two icons, that is, with multirow items, then you can create HbListWidgetItem objects and add them to an HbListWidget. See HbListWidgetItem for the detailed description of the multirow item. 
    50 
    50 
    51     SetModel() function of the base class HbListView cannot be used. If the user wants to start 
    51      HbListWidget model supports arranging items with drag and drop.
    52     using a list view with e.g. a directory model (QDirModel) this convenience API cannot be used.
    52      
    53     
    53      \link HbAbstractItemView::setModel(QAbstractItemModel *model, HbAbstractViewItem *prototype=0) SetModel()\endlink method of the base class HbAbstractItemView cannot be used. HbListWidget cannot be used for list views with a directory model (QDirModel), for example.
    54     HbListWidget can be used without HbListWidgetItem to populate 
    54     
    55     simple list with single row items consisting of a text and an icon. 
    55     \section _usecases_hblistwidget Using the HbListWidget class
    56 
    56     
    57     If multirow items are needed the HbListWidgetItem must be used.
    57     \subsection _uc_hblistwidget_001 Creating a list and adding one single row item into it.
    58 
    58     
       
    59     The following code snippet first creates a list and then adds one item which contains some text and an icon to the list.
       
    60    
    59     \snippet{unittest_hblistwidget.cpp,1}
    61     \snippet{unittest_hblistwidget.cpp,1}
    60 
    62 
    61     See HbListWidgetItem for more usage examples.
    63     \sa HbListWidgetItem, HbListView for more use cases.
    62 
       
    63     \sa HbListWidgetItem, HbListView 
       
    64 */
    64 */
    65 
    65 
    66 /*!
    66 /*!
    67     \fn void HbListWidget::pressed(HbListWidgetItem *item)
    67     \fn void HbListWidget::pressed(HbListWidgetItem *item)
    68 
    68 
    69     This signal is emitted when a list item is pressed.
    69     This signal is emitted when the user presses an item in the list. \a item indicates the pressed item.
    70     The pressed item is specified by \a item.
    70 
    71 
    71     \sa released(), longPressed(), activated()
    72     See also released(), longPressed() and activated().
       
    73 */
    72 */
    74 
    73 
    75 /*!
    74 /*!
    76   \fn void HbListWidget::released(HbListWidgetItem *item)
    75   \fn void HbListWidget::released(HbListWidgetItem *item)
    77 
    76 
    78     This signal is emitted when a list item is no more pressed.
    77     This signal is emitted when when release event occurs for \a item.
    79     The released item is specified by \a item.
    78 
    80 
    79     \sa pressed(), longPressed(), activated()
    81     See also pressed(), longPressed() and activated().
    80     \sa HbAbstractViewItem::released(const QPointF &position)
    82 */
    81 */
    83 
    82 
    84 /*!
    83 /*!
    85     \fn void HbListWidget::activated(HbListWidgetItem *item)
    84     \fn void HbListWidget::activated(HbListWidgetItem *item)
    86 
    85     
    87     This signal is emitted when the item specified by \a item is activated by the user.
    86     This signal is emitted when the user activates an item in the list. \a item indicates the activated item. The activation of the item depends on the input method. An item can be activated in the following ways:
    88     How to activate items depends on the input method; e.g., with mouse by clicking the item,
    87     - by clicking the item with a mouse
    89     or with touch input by tapping the item, or with hardware keys by pressing the Return
    88     - by tapping the item on a touch screen
    90     or Enter key when the item is current, or with soft keys by pressing choosing "Select"
    89     - by pressing Return or Enter hardware keys when the item has focus
    91     when the item is current.
    90     - by pressing Select soft key when the item has focus
    92 
    91     
    93     See also pressed(), released() and longPressed().
    92     \sa pressed(), released(), longPressed()
    94 */
    93 */
    95 
    94 
    96 /*!
    95 /*!
    97     \fn void HbListWidget::longPressed(HbListWidgetItem *item, const QPointF &coords)
    96     \fn void HbListWidget::longPressed(HbListWidgetItem *item, const QPointF &coords)
    98 
    97 
    99     This signal is emitted when a list item is long pressed.
    98     This signal is emitted when the user presses an item in the list for a long time. \a item indicates the activated item and \a coords the screen position where the long press happened.
   100     The pressed item is specified by \a item and \a coords.
    99 
   101     See also pressed(), released() and activated().
   100     \sa pressed(), released(), activated()
   102 */
   101 */
   103 
   102 
   104 /*!
   103 /*!
   105     Constructs a list widget with \a parent.
   104     Constructs a list widget with \a parent.
   106  */
   105 */
   107 HbListWidget::HbListWidget(QGraphicsItem *parent)
   106 HbListWidget::HbListWidget(QGraphicsItem *parent)
   108     : HbListView( *new HbListWidgetPrivate, new HbListItemContainer, parent )
   107     : HbListView( *new HbListWidgetPrivate, new HbListItemContainer, parent )
   109 {
   108 {
   110     Q_D( HbListWidget );
   109     Q_D( HbListWidget );
   111     d->q_ptr = this;
   110     d->q_ptr = this;
   117     connect(this, SIGNAL(released(QModelIndex)), this, SLOT(_q_itemReleased(QModelIndex)));
   116     connect(this, SIGNAL(released(QModelIndex)), this, SLOT(_q_itemReleased(QModelIndex)));
   118     connect(this, SIGNAL(longPressed(HbAbstractViewItem*, QPointF)), this, SLOT(_q_itemLongPress(HbAbstractViewItem*, QPointF)));
   117     connect(this, SIGNAL(longPressed(HbAbstractViewItem*, QPointF)), this, SLOT(_q_itemLongPress(HbAbstractViewItem*, QPointF)));
   119 }
   118 }
   120 
   119 
   121 /*!
   120 /*!
   122     Destructs the list view.
   121     Destructor.
   123  */ 
   122 */ 
   124 HbListWidget::~HbListWidget()
   123 HbListWidget::~HbListWidget()
   125 {
   124 {
   126 }
   125 }
   127 
   126 
   128 /*!
   127 /*!
   129     Returns the total number of rows.
   128     Returns the total number of rows in the list.
   130 */
   129 */
   131 int HbListWidget::count() const
   130 int HbListWidget::count() const
   132 {
   131 {
   133     return modelIterator()->indexCount();
   132     return modelIterator()->indexCount();
   134 }
   133 }
   135 
   134 
   136 /*!
   135 /*!
   137     Returns the current row number
   136     Returns the current row number in the list. A current row is a row having focus.
   138  */
   137 */
   139 int HbListWidget::currentRow() const
   138 int HbListWidget::currentRow() const
   140 {
   139 {
   141     return modelIterator()->indexPosition(currentIndex());
   140     return modelIterator()->indexPosition(currentIndex());
   142 }
   141 }
   143 
   142 
   144 /*!
   143 /*!
   145    Sets current \a row. This function sets current index into selection model. 
   144     Sets the current row to \a row. A current row is a row having focus.
   146    Selection is never changed.
   145     
   147  */
   146     \sa currentRow()
       
   147 */
   148 void HbListWidget::setCurrentRow(int row)
   148 void HbListWidget::setCurrentRow(int row)
   149 {
   149 {
   150     Q_D(HbListWidget);
   150     Q_D(HbListWidget);
   151     setCurrentIndex(d->mModelIterator->index(row));
   151     setCurrentIndex(d->mModelIterator->index(row));
   152 }
   152 }
   153 
   153 
   154 /*!
   154 /*!
   155     Returns a pointer to the current HbListWidgetItem. 
   155     Returns a pointer to the current list item.  A current list item is a list item having focus.
   156     Ownership not transferred to the caller.
   156     Ownership is not transferred so the item should not be deleted by the caller.
   157     Should not be deleted by the caller.
   157     
   158  */
   158     \sa currentRow()
       
   159 */
   159 HbListWidgetItem *HbListWidget::currentItem() const 
   160 HbListWidgetItem *HbListWidget::currentItem() const 
   160 {
   161 {
   161     Q_D( const HbListWidget );
   162     Q_D( const HbListWidget );
   162     return d->mListModel->item(modelIterator()->indexPosition(currentIndex()));
   163     return d->mListModel->item(modelIterator()->indexPosition(currentIndex()));
   163 }
   164 }
   164 
   165 
   165 /*!
   166 /*!
   166     Sets the current item to item.
   167     Sets the current item to \a item. A current item is an item having focus.
   167  */
   168 */
   168 void HbListWidget::setCurrentItem (HbListWidgetItem *item)
   169 void HbListWidget::setCurrentItem (HbListWidgetItem *item)
   169 {
   170 {
   170     setCurrentRow(row(item));
   171     setCurrentRow(row(item));
   171 }
   172 }
   172 
   173 
   173 /*!
   174 /*!
   174     Returns a pointer to an item specified by \a row.
   175     Returns a pointer to the list item specified by \a row.
   175     Ownership not transferred to the caller.
   176     Ownership is not transferred to the caller so the item should not be deleted by the caller.
   176     Should not be deleted by the caller.
   177 */
   177  */
       
   178 HbListWidgetItem *HbListWidget::item (int row) const
   178 HbListWidgetItem *HbListWidget::item (int row) const
   179 {
   179 {
   180     Q_D( const HbListWidget );
   180     Q_D( const HbListWidget );
   181     return d->mListModel->item(row);
   181     return d->mListModel->item(row);
   182 }
   182 }
   183 
   183 
   184 /*!
   184 /*!
   185     Creates new one row item based on a string and an icon and append it to the end of the list.
   185     Creates a single row item specified by \a icon and \a text and appends it to the end of the list.
   186  */
   186 */
   187 void HbListWidget::addItem (const HbIcon &icon, const QString &text)
   187 void HbListWidget::addItem (const HbIcon &icon, const QString &text)
   188 {
   188 {
   189     Q_D( HbListWidget );
   189     Q_D( HbListWidget );
   190 
   190 
   191     HbListWidgetItem *item = new HbListWidgetItem();
   191     HbListWidgetItem *item = new HbListWidgetItem();
   193     item->setIcon(icon);
   193     item->setIcon(icon);
   194     d->mListModel->appendRow(item);
   194     d->mListModel->appendRow(item);
   195 }
   195 }
   196 
   196 
   197 /*!
   197 /*!
   198     Creates new one row item based on a string and append it to the end of the list.
   198     Creates a new single row item spcified by \a text and appends it to the end of the list.
   199  */ 
   199 */ 
   200 void HbListWidget::addItem (const QString &text)
   200 void HbListWidget::addItem (const QString &text)
   201 {
   201 {
   202     Q_D( HbListWidget );
   202     Q_D( HbListWidget );
   203 
   203 
   204     HbListWidgetItem *item = new HbListWidgetItem();
   204     HbListWidgetItem *item = new HbListWidgetItem();
   205     item->setText(text);
   205     item->setText(text);
   206     d->mListModel->appendRow(item);
   206     d->mListModel->appendRow(item);
   207 }
   207 }
   208 
   208 
   209 /*!
   209 /*!
   210    Appends an item created by the user to the end of the list.
   210    Appends \a item to the end of the list.
   211  */
   211 */
   212 void HbListWidget::addItem (HbListWidgetItem *item)
   212 void HbListWidget::addItem (HbListWidgetItem *item)
   213 {
   213 {
   214     Q_D( HbListWidget );
   214     Q_D( HbListWidget );
   215 
   215 
   216     if (item) {
   216     if (item) {
   217         d->mListModel->appendRow(item);
   217         d->mListModel->appendRow(item);
   218     }
   218     }
   219 }
   219 }
   220 
   220 
   221 /*!
   221 /*!
   222     Creates a new item based on the string and an icon into a specified location in the list            
   222     Creates a list widget item with \a icon and \a text and inserts it into the list at the \a row location. Regarding \a row the following rules apply:
   223  */
   223     - if \a row is negative the item is inserted into the first row
       
   224     - if \a row is greater than the number of list's items the item is added at the last item
       
   225 */
   224 void HbListWidget::insertItem (int row, const HbIcon &icon, const QString &text)
   226 void HbListWidget::insertItem (int row, const HbIcon &icon, const QString &text)
   225 {
   227 {
   226     Q_D( HbListWidget );
   228     Q_D( HbListWidget );
   227 
   229 
   228     //Do not enter parent in constr!   
   230     //Do not enter parent in constr!   
   231     item->setIcon(icon);
   233     item->setIcon(icon);
   232     d->mListModel->insert(row, item);
   234     d->mListModel->insert(row, item);
   233 }
   235 }
   234 
   236 
   235 /*!
   237 /*!
   236     Creates a new item based on the string into a specified location in the list.
   238     Creates a list widget item with \a text and inserts it into the list at the \a row location. Regarding \a row the following rules apply:
   237  */
   239     - if \a row is negative the item is inserted into the first row
       
   240     - if \a row is greater than the number of list's items the item is added at the last item
       
   241 */
   238 void HbListWidget::insertItem (int row, const QString &text)
   242 void HbListWidget::insertItem (int row, const QString &text)
   239 {
   243 {
   240     Q_D( HbListWidget );
   244     Q_D( HbListWidget );
   241 
   245 
   242     //Do not enter parent in constr! 
   246     //Do not enter parent in constr! 
   244     item->setText(text);
   248     item->setText(text);
   245     d->mListModel->insert(row, item);
   249     d->mListModel->insert(row, item);
   246 }
   250 }
   247 
   251 
   248 /*!
   252 /*!
   249    Inserts an item created by the user to the specified place on the list.
   253     Creates a list widget item with \a item and inserts it into the list at the \a row location. Regarding \a row the following rules apply:
   250  */
   254     - if \a row is negative the item is inserted into the first row
       
   255     - if \a row is greater than the number of list's items the item is added at the last item
       
   256 */
   251 void HbListWidget::insertItem (int row, HbListWidgetItem *item)
   257 void HbListWidget::insertItem (int row, HbListWidgetItem *item)
   252 {
   258 {
   253     Q_D( HbListWidget );
   259     Q_D( HbListWidget );
   254 
   260 
   255     if (item) {
   261     if (item) {
   256         d->mListModel->insert(row, item);
   262         d->mListModel->insert(row, item);
   257     }
   263     }
   258 }
   264 }
   259 
   265 
   260 /*!
   266 /*!
   261     Deletes every item in the list     
   267     Deletes all the items in the list.     
   262  */
   268 */
   263 void HbListWidget::clear() 
   269 void HbListWidget::clear() 
   264 {
   270 {
   265     Q_D( HbListWidget );
   271     Q_D( HbListWidget );
   266 
   272 
   267     if (d->mListModel) {
   273     if (d->mListModel) {
   268         d->mListModel->clear();
   274         d->mListModel->clear();
   269     }
   275     }
   270 }
   276 }
   271 
   277 
   272 /*!
   278 /*!
   273     Returns the row containing the given item.
   279     Returns the row number of given \a item.
   274  */
   280 */
   275 int HbListWidget::row(const HbListWidgetItem *item) const
   281 int HbListWidget::row(const HbListWidgetItem *item) const
   276 {
   282 {
   277     Q_D( const HbListWidget );
   283     Q_D( const HbListWidget );
   278 
   284 
   279     return d->mListModel->index(const_cast<HbListWidgetItem *>(item)).row();
   285     return d->mListModel->index(const_cast<HbListWidgetItem *>(item)).row();
   280 }
   286 }
   281 
   287 
   282 /*!
   288 /*!
   283     \reimp
   289     This method cannot be used. If you call it in debug builds you will get an ASSERT failure i.e. Q_ASSERT_X(). In release builds nothing will happen.
   284 
       
   285     Setting own model to HbListWidget is not allowed.
       
   286 */
   290 */
   287 void HbListWidget::setModel(QAbstractItemModel *model, HbAbstractViewItem *prototype)
   291 void HbListWidget::setModel(QAbstractItemModel *model, HbAbstractViewItem *prototype)
   288 {
   292 {
   289     Q_UNUSED(model);
   293     Q_UNUSED(model);
   290     Q_UNUSED(prototype);
   294     Q_UNUSED(prototype);
   291     Q_ASSERT_X(false, "HbListWidget::setModel()", "Model cannot be set by user if convenience API is used");
   295     Q_ASSERT_X(false, "HbListWidget::setModel()", "Model cannot be set by user if convenience API is used");
   292 }
   296 }
   293 
   297 
   294 
   298 
   295 /*!
   299 /*!
   296     Set the text for a simple one row item or the first text row for a 
   300     Replaces the text on the given \a row with the given \a text. Only the text is replaced, the icon stays unchanged. In case of a multi-row only the first text row is replaced. Use HbListWidgetItem to modify multirow items.
   297     multi row item. Only the text is replaced, no icon removed. 
   301     
   298     Use HbListWidgetItem to modify multirow items.
   302     \param text - Text to be shown in the list widget item.
       
   303     \param row - Row of the list widget item.
       
   304     
   299 */
   305 */
   300 void HbListWidget::setText(int row, const QString &text)
   306 void HbListWidget::setText(int row, const QString &text)
   301 {
   307 {
   302     Q_D( const HbListWidget );
   308     Q_D( const HbListWidget );
   303 
   309 
   304     HbListWidgetItem *item = d->mListModel->item(row);
   310     HbListWidgetItem *item = d->mListModel->item(row);
   305     item->setText(text);
   311     item->setText(text);
   306 }
   312 }
   307 
   313 
   308 /*!
   314 /*!
   309     Set the icon for a simple one row item or the first icon for a 
   315     Replaces the icon on the given \a row with the given \a icon. Only the icon is replaced, the text stays unchanged. The item can be a single row or a multirow item. Use HbListWidgetItem to modify multirow items.
   310     multi row item. Only the icon is replaced, no text removed.
   316     
   311     Use HbListWidgetItem to modify multirow items.
   317     \param icon - Icon to be shown in the list widget item.
       
   318     \param row - Row of the list widget item.
   312 */
   319 */
   313 void HbListWidget::setIcon(int row, const HbIcon& icon)
   320 void HbListWidget::setIcon(int row, const HbIcon& icon)
   314 {
   321 {
   315     Q_D( const HbListWidget );
   322     Q_D( const HbListWidget );
   316 
   323 
   317     HbListWidgetItem *item = d->mListModel->item(row);
   324     HbListWidgetItem *item = d->mListModel->item(row);
   318     item->setIcon(icon);
   325     item->setIcon(icon);
   319 }
   326 }
   320 
   327 
   321 /*!
   328 /*!
   322     Removes and returns the item from the given row in the list widget; otherwise returns 0.
   329     Returns the list widget item of given row and then removes the item from the list. Items removed from a list must be deleted manually.
   323     Items removed from a list widget will not be managed, and will need to be deleted manually.   
   330     
   324     Use this function to remove items form the list.    
   331     \param row - Row to be removed. 
       
   332 
       
   333     \return
       
   334     - The list widget item of \a row if it exists in the list.
       
   335     - 0, if \a row doesn't exist in the list.
   325 */
   336 */
   326 HbListWidgetItem *HbListWidget::takeItem(int row)
   337 HbListWidgetItem *HbListWidget::takeItem(int row)
   327 {
   338 {
   328     Q_D( const HbListWidget );
   339     Q_D( const HbListWidget );
   329 
   340 
   331         return 0;
   342         return 0;
   332     return d->mListModel->take(row);
   343     return d->mListModel->take(row);
   333 }
   344 }
   334 
   345 
   335 /*!
   346 /*!
   336     HbListWidget model supports arranging items with drag and drop, so 
   347     HbListWidget supports arranging items with drag and drop. If \a arrangeMode is \c true and the list is not in the selection mode, the list is put into the arrange list mode which allows the user to rearrange the list by dragging and dropping items. If \a arrangeMode is \c false, then the arrange list mode is cancelled. 
   337     this method returns true unless the widget is in some selection mode. 
   348     
       
   349     The list can be in either arrange or selection mode but not in both modes at any given time.
       
   350     
       
   351     \return
       
   352     - \c True if the arrange list mode is successfully set according to \a arrangeMode. This includes the case where the requested \a arrangeMode is the same as what was already set for the list.
       
   353     - \c False if the requested \a arrangeMode cannot be set.   
   338 */
   354 */
   339 bool HbListWidget::setArrangeMode(const bool arrangeMode)
   355 bool HbListWidget::setArrangeMode(const bool arrangeMode)
   340 {
   356 {
   341     Q_D( HbListWidget );
   357     Q_D( HbListWidget );
   342 
   358 
   343     if (arrangeMode != d->mArrangeMode) {
   359     if (arrangeMode != d->mArrangeMode) {
   344         if (arrangeMode == true) {
   360         if (d->mSelectionMode != HbAbstractItemView::NoSelection) {
   345             if (d->mSelectionMode != HbAbstractItemView::NoSelection) {
   361             return false;
   346                 return false;
       
   347             }
       
   348             verticalScrollBar()->setInteractive(true);
       
   349         } else {
       
   350             verticalScrollBar()->setInteractive(false);
       
   351         }
   362         }
   352 
   363         d->arrangeModeSetup(arrangeMode);
   353         d->mArrangeMode = arrangeMode;
       
   354 
       
   355         if (d->mArrangeMode == true) {
       
   356             d->mOriginalFriction = d->mFrictionEnabled;
       
   357             setFrictionEnabled(false);
       
   358         } else {
       
   359             setFrictionEnabled(d->mOriginalFriction);
       
   360         }
       
   361     }
   364     }
   362     return true;
   365     return true;
   363 }
   366 }
   364 
   367 
   365 /*!
   368 /*!
       
   369     Moves an item from given source row to given target row. The operation is cancelled in the following cases:
       
   370     - Source and target rows are the same.
       
   371     - Either or both given rows do not exist in the list.
       
   372     
       
   373     \param from - Source row that is the row from where the item is moved.
       
   374     \param to - Target row that is the row to where the item is moved.
   366 */
   375 */
   367  void HbListWidget::move(const QModelIndex &from, const QModelIndex &to)
   376  void HbListWidget::move(const QModelIndex &from, const QModelIndex &to)
   368 {
   377 {
   369     int fromRow = from.row();
   378     int fromRow = from.row();
   370     int toRow = to.row();
   379     int toRow = to.row();
   381     insertItem(toRow, item);
   390     insertItem(toRow, item);
   382 }
   391 }
   383 
   392 
   384 /*!
   393 /*!
   385     Constructs a list widget with private view widget \a dd and \a parent.
   394     Constructs a list widget with private view widget \a dd and \a parent.
   386  */
   395 */
   387 HbListWidget::HbListWidget( HbListWidgetPrivate& dd, HbAbstractItemContainer *container, QGraphicsItem* parent ):
   396 HbListWidget::HbListWidget( HbListWidgetPrivate& dd, HbAbstractItemContainer *container, QGraphicsItem* parent ):
   388         HbListView( dd, container, parent )
   397         HbListView( dd, container, parent )
   389 {
   398 {
   390     Q_D( HbListWidget );
   399     Q_D( HbListWidget );
   391     d->q_ptr = this;
   400     d->q_ptr = this;