Some small doc fixes, typos and removal of one incorrect paragraph
[profile/ivi/qtbase.git] / src / widgets / itemviews / qlistwidget.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qlistwidget.h"
43
44 #ifndef QT_NO_LISTWIDGET
45 #include <qitemdelegate.h>
46 #include <private/qlistview_p.h>
47 #include <private/qwidgetitemdata_p.h>
48 #include <private/qlistwidget_p.h>
49
50 QT_BEGIN_NAMESPACE
51
52 // workaround for VC++ 6.0 linker bug (?)
53 typedef bool(*LessThan)(const QPair<QListWidgetItem*,int>&,const QPair<QListWidgetItem*,int>&);
54
55 class QListWidgetMimeData : public QMimeData
56 {
57     Q_OBJECT
58 public:
59     QList<QListWidgetItem*> items;
60 };
61
62 QT_BEGIN_INCLUDE_NAMESPACE
63 #include "qlistwidget.moc"
64 QT_END_INCLUDE_NAMESPACE
65
66 QListModel::QListModel(QListWidget *parent)
67     : QAbstractListModel(parent)
68 {
69 }
70
71 QListModel::~QListModel()
72 {
73     clear();
74 }
75
76 void QListModel::clear()
77 {
78     for (int i = 0; i < items.count(); ++i) {
79         if (items.at(i)) {
80             items.at(i)->d->theid = -1;
81             items.at(i)->view = 0;
82             delete items.at(i);
83         }
84     }
85     items.clear();
86     reset();
87 }
88
89 QListWidgetItem *QListModel::at(int row) const
90 {
91     return items.value(row);
92 }
93
94 void QListModel::remove(QListWidgetItem *item)
95 {
96     if (!item)
97         return;
98     int row = items.indexOf(item); // ### use index(item) - it's faster
99     Q_ASSERT(row != -1);
100     beginRemoveRows(QModelIndex(), row, row);
101     items.at(row)->d->theid = -1;
102     items.at(row)->view = 0;
103     items.removeAt(row);
104     endRemoveRows();
105 }
106
107 void QListModel::insert(int row, QListWidgetItem *item)
108 {
109     if (!item)
110         return;
111
112     item->view = qobject_cast<QListWidget*>(QObject::parent());
113     if (item->view && item->view->isSortingEnabled()) {
114         // sorted insertion
115         QList<QListWidgetItem*>::iterator it;
116         it = sortedInsertionIterator(items.begin(), items.end(),
117                                      item->view->sortOrder(), item);
118         row = qMax(it - items.begin(), 0);
119     } else {
120         if (row < 0)
121             row = 0;
122         else if (row > items.count())
123             row = items.count();
124     }
125     beginInsertRows(QModelIndex(), row, row);
126     items.insert(row, item);
127     item->d->theid = row;
128     endInsertRows();
129 }
130
131 void QListModel::insert(int row, const QStringList &labels)
132 {
133     const int count = labels.count();
134     if (count <= 0)
135         return;
136     QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
137     if (view && view->isSortingEnabled()) {
138         // sorted insertion
139         for (int i = 0; i < count; ++i) {
140             QListWidgetItem *item = new QListWidgetItem(labels.at(i));
141             insert(row, item);
142         }
143     } else {
144         if (row < 0)
145             row = 0;
146         else if (row > items.count())
147             row = items.count();
148         beginInsertRows(QModelIndex(), row, row + count - 1);
149         for (int i = 0; i < count; ++i) {
150             QListWidgetItem *item = new QListWidgetItem(labels.at(i));
151             item->d->theid = row;
152             item->view = qobject_cast<QListWidget*>(QObject::parent());
153             items.insert(row++, item);
154         }
155         endInsertRows();
156     }
157 }
158
159 QListWidgetItem *QListModel::take(int row)
160 {
161     if (row < 0 || row >= items.count())
162         return 0;
163
164     beginRemoveRows(QModelIndex(), row, row);
165     items.at(row)->d->theid = -1;
166     items.at(row)->view = 0;
167     QListWidgetItem *item = items.takeAt(row);
168     endRemoveRows();
169     return item;
170 }
171
172 void QListModel::move(int srcRow, int dstRow)
173 {
174     if (srcRow == dstRow
175         || srcRow < 0 || srcRow >= items.count()
176         || dstRow < 0 || dstRow > items.count())
177         return;
178
179     if (!beginMoveRows(QModelIndex(), srcRow, srcRow, QModelIndex(), dstRow))
180         return;
181     if (srcRow < dstRow)
182         --dstRow;
183     items.move(srcRow, dstRow);
184     endMoveRows();
185 }
186
187 int QListModel::rowCount(const QModelIndex &parent) const
188 {
189     return parent.isValid() ? 0 : items.count();
190 }
191
192 QModelIndex QListModel::index(QListWidgetItem *item) const
193 {
194     if (!item || !item->view || static_cast<const QListModel *>(item->view->model()) != this
195         || items.isEmpty())
196         return QModelIndex();
197     int row;
198     const int theid = item->d->theid;
199     if (theid >= 0 && theid < items.count() && items.at(theid) == item) {
200         row = theid;
201     } else { // we need to search for the item
202         row = items.lastIndexOf(item);  // lastIndexOf is an optimization in favor of indexOf
203         if (row == -1) // not found
204             return QModelIndex();
205         item->d->theid = row;
206     }
207     return createIndex(row, 0, item);
208 }
209
210 QModelIndex QListModel::index(int row, int column, const QModelIndex &parent) const
211 {
212     if (hasIndex(row, column, parent))
213         return createIndex(row, column, items.at(row));
214     return QModelIndex();
215 }
216
217 QVariant QListModel::data(const QModelIndex &index, int role) const
218 {
219     if (!index.isValid() || index.row() >= items.count())
220         return QVariant();
221     return items.at(index.row())->data(role);
222 }
223
224 bool QListModel::setData(const QModelIndex &index, const QVariant &value, int role)
225 {
226     if (!index.isValid() || index.row() >= items.count())
227         return false;
228     items.at(index.row())->setData(role, value);
229     return true;
230 }
231
232 QMap<int, QVariant> QListModel::itemData(const QModelIndex &index) const
233 {
234     QMap<int, QVariant> roles;
235     if (!index.isValid() || index.row() >= items.count())
236         return roles;
237     QListWidgetItem *itm = items.at(index.row());
238     for (int i = 0; i < itm->d->values.count(); ++i) {
239         roles.insert(itm->d->values.at(i).role,
240                      itm->d->values.at(i).value);
241     }
242     return roles;
243 }
244
245 bool QListModel::insertRows(int row, int count, const QModelIndex &parent)
246 {
247     if (count < 1 || row < 0 || row > rowCount() || parent.isValid())
248         return false;
249
250     beginInsertRows(QModelIndex(), row, row + count - 1);
251     QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
252     QListWidgetItem *itm = 0;
253
254     for (int r = row; r < row + count; ++r) {
255         itm = new QListWidgetItem;
256         itm->view = view;
257         itm->d->theid = r;
258         items.insert(r, itm);
259     }
260
261     endInsertRows();
262     return true;
263 }
264
265 bool QListModel::removeRows(int row, int count, const QModelIndex &parent)
266 {
267     if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid())
268         return false;
269
270     beginRemoveRows(QModelIndex(), row, row + count - 1);
271     QListWidgetItem *itm = 0;
272     for (int r = row; r < row + count; ++r) {
273         itm = items.takeAt(row);
274         itm->view = 0;
275         itm->d->theid = -1;
276         delete itm;
277     }
278     endRemoveRows();
279     return true;
280 }
281
282 Qt::ItemFlags QListModel::flags(const QModelIndex &index) const
283 {
284     if (!index.isValid() || index.row() >= items.count() || index.model() != this)
285         return Qt::ItemIsDropEnabled; // we allow drops outside the items
286     return items.at(index.row())->flags();
287 }
288
289 void QListModel::sort(int column, Qt::SortOrder order)
290 {
291     if (column != 0)
292         return;
293
294     emit layoutAboutToBeChanged();
295
296     QVector < QPair<QListWidgetItem*,int> > sorting(items.count());
297     for (int i = 0; i < items.count(); ++i) {
298         QListWidgetItem *item = items.at(i);
299         sorting[i].first = item;
300         sorting[i].second = i;
301     }
302
303     LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
304     qSort(sorting.begin(), sorting.end(), compare);
305     QModelIndexList fromIndexes;
306     QModelIndexList toIndexes;
307     for (int r = 0; r < sorting.count(); ++r) {
308         QListWidgetItem *item = sorting.at(r).first;
309         toIndexes.append(createIndex(r, 0, item));
310         fromIndexes.append(createIndex(sorting.at(r).second, 0, sorting.at(r).first));
311         items[r] = sorting.at(r).first;
312     }
313     changePersistentIndexList(fromIndexes, toIndexes);
314
315     emit layoutChanged();
316 }
317
318 /**
319  * This function assumes that all items in the model except the items that are between
320  * (inclusive) start and end are sorted.
321  * With these assumptions, this function can ensure that the model is sorted in a
322  * much more efficient way than doing a naive 'sort everything'.
323  * (provided that the range is relatively small compared to the total number of items)
324  */
325 void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int end)
326 {
327     if (column != 0)
328         return;
329
330     int count = end - start + 1;
331     QVector < QPair<QListWidgetItem*,int> > sorting(count);
332     for (int i = 0; i < count; ++i) {
333         sorting[i].first = items.at(start + i);
334         sorting[i].second = start + i;
335     }
336
337     LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
338     qSort(sorting.begin(), sorting.end(), compare);
339
340     QModelIndexList oldPersistentIndexes = persistentIndexList();
341     QModelIndexList newPersistentIndexes = oldPersistentIndexes;
342     QList<QListWidgetItem*> tmp = items;
343     QList<QListWidgetItem*>::iterator lit = tmp.begin();
344     bool changed = false;
345     for (int i = 0; i < count; ++i) {
346         int oldRow = sorting.at(i).second;
347         QListWidgetItem *item = tmp.takeAt(oldRow);
348         lit = sortedInsertionIterator(lit, tmp.end(), order, item);
349         int newRow = qMax(lit - tmp.begin(), 0);
350         lit = tmp.insert(lit, item);
351         if (newRow != oldRow) {
352             changed = true;
353             for (int j = i + 1; j < count; ++j) {
354                 int otherRow = sorting.at(j).second;
355                 if (oldRow < otherRow && newRow >= otherRow)
356                     --sorting[j].second;
357                 else if (oldRow > otherRow && newRow <= otherRow)
358                     ++sorting[j].second;
359             }
360             for (int k = 0; k < newPersistentIndexes.count(); ++k) {
361                 QModelIndex pi = newPersistentIndexes.at(k);
362                 int oldPersistentRow = pi.row();
363                 int newPersistentRow = oldPersistentRow;
364                 if (oldPersistentRow == oldRow)
365                     newPersistentRow = newRow;
366                 else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow)
367                     newPersistentRow = oldPersistentRow - 1;
368                 else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow)
369                     newPersistentRow = oldPersistentRow + 1;
370                 if (newPersistentRow != oldPersistentRow)
371                     newPersistentIndexes[k] = createIndex(newPersistentRow,
372                                                           pi.column(), pi.internalPointer());
373             }
374         }
375     }
376
377     if (changed) {
378         emit layoutAboutToBeChanged();
379         items = tmp;
380         changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);
381         emit layoutChanged();
382     }
383 }
384
385 bool QListModel::itemLessThan(const QPair<QListWidgetItem*,int> &left,
386                               const QPair<QListWidgetItem*,int> &right)
387 {
388     return (*left.first) < (*right.first);
389 }
390
391 bool QListModel::itemGreaterThan(const QPair<QListWidgetItem*,int> &left,
392                                  const QPair<QListWidgetItem*,int> &right)
393 {
394     return (*right.first) < (*left.first);
395 }
396
397 QList<QListWidgetItem*>::iterator QListModel::sortedInsertionIterator(
398     const QList<QListWidgetItem*>::iterator &begin,
399     const QList<QListWidgetItem*>::iterator &end,
400     Qt::SortOrder order, QListWidgetItem *item)
401 {
402     if (order == Qt::AscendingOrder)
403         return qLowerBound(begin, end, item, QListModelLessThan());
404     return qLowerBound(begin, end, item, QListModelGreaterThan());
405 }
406
407 void QListModel::itemChanged(QListWidgetItem *item)
408 {
409     QModelIndex idx = index(item);
410     emit dataChanged(idx, idx);
411 }
412
413 QStringList QListModel::mimeTypes() const
414 {
415     const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
416     return view->mimeTypes();
417 }
418
419 QMimeData *QListModel::internalMimeData()  const
420 {
421     return QAbstractItemModel::mimeData(cachedIndexes);
422 }
423
424 QMimeData *QListModel::mimeData(const QModelIndexList &indexes) const
425 {
426     QList<QListWidgetItem*> itemlist;
427     for (int i = 0; i < indexes.count(); ++i)
428         itemlist << at(indexes.at(i).row());
429     const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
430
431     cachedIndexes = indexes;
432     QMimeData *mimeData = view->mimeData(itemlist);
433     cachedIndexes.clear();
434     return mimeData;
435 }
436
437 #ifndef QT_NO_DRAGANDDROP
438 bool QListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
439                               int row, int column, const QModelIndex &index)
440 {
441     Q_UNUSED(column);
442     QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
443     if (index.isValid())
444         row = index.row();
445     else if (row == -1)
446         row = items.count();
447
448     return view->dropMimeData(row, data, action);
449 }
450
451 Qt::DropActions QListModel::supportedDropActions() const
452 {
453     const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
454     return view->supportedDropActions();
455 }
456 #endif // QT_NO_DRAGANDDROP
457
458 /*!
459     \class QListWidgetItem
460     \brief The QListWidgetItem class provides an item for use with the
461     QListWidget item view class.
462
463     \ingroup model-view
464     \inmodule QtWidgets
465
466     A QListWidgetItem represents a single item in a QListWidget. Each item can
467     hold several pieces of information, and will display them appropriately.
468
469     The item view convenience classes use a classic item-based interface rather
470     than a pure model/view approach. For a more flexible list view widget,
471     consider using the QListView class with a standard model.
472
473     List items can be inserted automatically into a list, when they are
474     constructed, by specifying the list widget:
475
476     \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 2
477
478     Alternatively, list items can also be created without a parent widget, and
479     later inserted into a list using QListWidget::insertItem().
480
481     List items are typically used to display text() and an icon(). These are
482     set with the setText() and setIcon() functions. The appearance of the text
483     can be customized with setFont(), setForeground(), and setBackground().
484     Text in list items can be aligned using the setTextAlignment() function.
485     Tooltips, status tips and "What's This?" help can be added to list items
486     with setToolTip(), setStatusTip(), and setWhatsThis().
487
488     By default, items are enabled, selectable, checkable, and can be the source
489     of drag and drop operations.
490
491     Each item's flags can be changed by calling setFlags() with the appropriate
492     value (see Qt::ItemFlags). Checkable items can be checked, unchecked and
493     partially checked with the setCheckState() function. The corresponding
494     checkState() function indicates the item's current check state.
495
496     The isHidden() function can be used to determine whether the item is
497     hidden. To hide an item, use setHidden().
498
499
500     \section1 Subclassing
501
502     When subclassing QListWidgetItem to provide custom items, it is possible to
503     define new types for them enabling them to be distinguished from standard
504     items. For subclasses that require this feature, ensure that you call the
505     base class constructor with a new type value equal to or greater than
506     \l UserType, within \e your constructor.
507
508     \sa QListWidget, {Model/View Programming}, QTreeWidgetItem, QTableWidgetItem
509 */
510
511 /*!
512     \enum QListWidgetItem::ItemType
513
514     This enum describes the types that are used to describe list widget items.
515
516     \value Type     The default type for list widget items.
517     \value UserType The minimum value for custom types. Values below UserType are
518                     reserved by Qt.
519
520     You can define new user types in QListWidgetItem subclasses to ensure that
521     custom items are treated specially.
522
523     \sa type()
524 */
525
526 /*!
527     \fn int QListWidgetItem::type() const
528
529     Returns the type passed to the QListWidgetItem constructor.
530 */
531
532 /*!
533     \fn QListWidget *QListWidgetItem::listWidget() const
534
535     Returns the list widget containing the item.
536 */
537
538 /*!
539     \fn void QListWidgetItem::setSelected(bool select)
540     \since 4.2
541
542     Sets the selected state of the item to \a select.
543
544     \sa isSelected()
545 */
546
547 /*!
548     \fn bool QListWidgetItem::isSelected() const
549     \since 4.2
550
551     Returns true if the item is selected; otherwise returns false.
552
553     \sa setSelected()
554 */
555
556 /*!
557     \fn void QListWidgetItem::setHidden(bool hide)
558     \since 4.2
559
560     Hides the item if \a hide is true; otherwise shows the item.
561
562     \sa isHidden()
563 */
564
565 /*!
566     \fn bool QListWidgetItem::isHidden() const
567     \since 4.2
568
569     Returns true if the item is hidden; otherwise returns false.
570
571     \sa setHidden()
572 */
573
574 /*!
575     \fn QListWidgetItem::QListWidgetItem(QListWidget *parent, int type)
576
577     Constructs an empty list widget item of the specified \a type with the
578     given \a parent. If \a parent is not specified, the item will need to be
579     inserted into a list widget with QListWidget::insertItem().
580
581     This constructor inserts the item into the model of the parent that is
582     passed to the constructor. If the model is sorted then the behavior of the
583     insert is undetermined since the model will call the \c '<' operator method
584     on the item which, at this point, is not yet constructed. To avoid the
585     undetermined behavior, we recommend not to specify the parent and use
586     QListWidget::insertItem() instead.
587
588     \sa type()
589 */
590 QListWidgetItem::QListWidgetItem(QListWidget *view, int type)
591     : rtti(type), view(view), d(new QListWidgetItemPrivate(this)),
592       itemFlags(Qt::ItemIsSelectable
593                 |Qt::ItemIsUserCheckable
594                 |Qt::ItemIsEnabled
595                 |Qt::ItemIsDragEnabled)
596 {
597     if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
598         model->insert(model->rowCount(), this);
599 }
600
601 /*!
602     \fn QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *parent, int type)
603
604     Constructs an empty list widget item of the specified \a type with the
605     given \a text and \a parent. If the parent is not specified, the item will
606     need to be inserted into a list widget with QListWidget::insertItem().
607
608     This constructor inserts the item into the model of the parent that is
609     passed to the constructor. If the model is sorted then the behavior of the
610     insert is undetermined since the model will call the \c '<' operator method
611     on the item which, at this point, is not yet constructed. To avoid the
612     undetermined behavior, we recommend not to specify the parent and use
613     QListWidget::insertItem() instead.
614
615     \sa type()
616 */
617 QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *view, int type)
618     : rtti(type), view(0), d(new QListWidgetItemPrivate(this)),
619       itemFlags(Qt::ItemIsSelectable
620                 |Qt::ItemIsUserCheckable
621                 |Qt::ItemIsEnabled
622                 |Qt::ItemIsDragEnabled)
623 {
624     setData(Qt::DisplayRole, text);
625     this->view = view;
626     if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
627         model->insert(model->rowCount(), this);
628 }
629
630 /*!
631     \fn QListWidgetItem::QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent, int type)
632
633     Constructs an empty list widget item of the specified \a type with the
634     given \a icon, \a text and \a parent. If the parent is not specified, the
635     item will need to be inserted into a list widget with
636     QListWidget::insertItem().
637
638     This constructor inserts the item into the model of the parent that is
639     passed to the constructor. If the model is sorted then the behavior of the
640     insert is undetermined since the model will call the \c '<' operator method
641     on the item which, at this point, is not yet constructed. To avoid the
642     undetermined behavior, we recommend not to specify the parent and use
643     QListWidget::insertItem() instead.
644
645     \sa type()
646 */
647 QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text,
648                                  QListWidget *view, int type)
649     : rtti(type), view(0), d(new QListWidgetItemPrivate(this)),
650       itemFlags(Qt::ItemIsSelectable
651                 |Qt::ItemIsUserCheckable
652                 |Qt::ItemIsEnabled
653                 |Qt::ItemIsDragEnabled)
654 {
655     setData(Qt::DisplayRole, text);
656     setData(Qt::DecorationRole, icon);
657     this->view = view;
658     if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
659         model->insert(model->rowCount(), this);
660 }
661
662 /*!
663     Destroys the list item.
664 */
665 QListWidgetItem::~QListWidgetItem()
666 {
667     if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
668         model->remove(this);
669     delete d;
670 }
671
672 /*!
673     Creates an exact copy of the item.
674 */
675 QListWidgetItem *QListWidgetItem::clone() const
676 {
677     return new QListWidgetItem(*this);
678 }
679
680 /*!
681     Sets the data for a given \a role to the given \a value. Reimplement this
682     function if you need extra roles or special behavior for certain roles.
683
684     \sa Qt::ItemDataRole, data()
685 */
686 void QListWidgetItem::setData(int role, const QVariant &value)
687 {
688     bool found = false;
689     role = (role == Qt::EditRole ? Qt::DisplayRole : role);
690     for (int i = 0; i < d->values.count(); ++i) {
691         if (d->values.at(i).role == role) {
692             if (d->values.at(i).value == value)
693                 return;
694             d->values[i].value = value;
695             found = true;
696             break;
697         }
698     }
699     if (!found)
700         d->values.append(QWidgetItemData(role, value));
701     if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
702         model->itemChanged(this);
703 }
704
705 /*!
706     Returns the item's data for a given \a role. Reimplement this function if
707     you need extra roles or special behavior for certain roles.
708
709     \sa Qt::ItemDataRole, setData()
710 */
711 QVariant QListWidgetItem::data(int role) const
712 {
713     role = (role == Qt::EditRole ? Qt::DisplayRole : role);
714     for (int i = 0; i < d->values.count(); ++i)
715         if (d->values.at(i).role == role)
716             return d->values.at(i).value;
717     return QVariant();
718 }
719
720 /*!
721     Returns true if this item's text is less then \a other item's text;
722     otherwise returns false.
723 */
724 bool QListWidgetItem::operator<(const QListWidgetItem &other) const
725 {
726     const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole);
727     return QAbstractItemModelPrivate::variantLessThan(v1, v2);
728 }
729
730 #ifndef QT_NO_DATASTREAM
731
732 /*!
733     Reads the item from stream \a in.
734
735     \sa write()
736 */
737 void QListWidgetItem::read(QDataStream &in)
738 {
739     in >> d->values;
740 }
741
742 /*!
743     Writes the item to stream \a out.
744
745     \sa read()
746 */
747 void QListWidgetItem::write(QDataStream &out) const
748 {
749     out << d->values;
750 }
751 #endif // QT_NO_DATASTREAM
752
753 /*!
754     \since 4.1
755
756     Constructs a copy of \a other. Note that type() and listWidget() are not
757     copied.
758
759     This function is useful when reimplementing clone().
760
761     \sa data(), flags()
762 */
763 QListWidgetItem::QListWidgetItem(const QListWidgetItem &other)
764     : rtti(Type), view(0),
765       d(new QListWidgetItemPrivate(this)),
766       itemFlags(other.itemFlags)
767 {
768     d->values = other.d->values;
769 }
770
771 /*!
772     Assigns \a other's data and flags to this item. Note that type() and
773     listWidget() are not copied.
774
775     This function is useful when reimplementing clone().
776
777     \sa data(), flags()
778 */
779 QListWidgetItem &QListWidgetItem::operator=(const QListWidgetItem &other)
780 {
781     d->values = other.d->values;
782     itemFlags = other.itemFlags;
783     return *this;
784 }
785
786 #ifndef QT_NO_DATASTREAM
787
788 /*!
789     \relates QListWidgetItem
790
791     Writes the list widget item \a item to stream \a out.
792
793     This operator uses QListWidgetItem::write().
794
795     \sa {Serializing Qt Data Types}
796 */
797 QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item)
798 {
799     item.write(out);
800     return out;
801 }
802
803 /*!
804     \relates QListWidgetItem
805
806     Reads a list widget item from stream \a in into \a item.
807
808     This operator uses QListWidgetItem::read().
809
810     \sa {Serializing Qt Data Types}
811 */
812 QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)
813 {
814     item.read(in);
815     return in;
816 }
817
818 #endif // QT_NO_DATASTREAM
819
820 /*!
821     \fn Qt::ItemFlags QListWidgetItem::flags() const
822
823     Returns the item flags for this item (see \l{Qt::ItemFlags}).
824 */
825
826 /*!
827     \fn QString QListWidgetItem::text() const
828
829     Returns the list item's text.
830
831     \sa setText()
832 */
833
834 /*!
835     \fn QIcon QListWidgetItem::icon() const
836
837     Returns the list item's icon.
838
839     \sa setIcon(), {QAbstractItemView::iconSize}{iconSize}
840 */
841
842 /*!
843     \fn QString QListWidgetItem::statusTip() const
844
845     Returns the list item's status tip.
846
847     \sa setStatusTip()
848 */
849
850 /*!
851     \fn QString QListWidgetItem::toolTip() const
852
853     Returns the list item's tooltip.
854
855     \sa setToolTip() statusTip() whatsThis()
856 */
857
858 /*!
859     \fn QString QListWidgetItem::whatsThis() const
860
861     Returns the list item's "What's This?" help text.
862
863     \sa setWhatsThis() statusTip() toolTip()
864 */
865
866 /*!
867     \fn QFont QListWidgetItem::font() const
868
869     Returns the font used to display this list item's text.
870 */
871
872 /*!
873     \fn int QListWidgetItem::textAlignment() const
874
875     Returns the text alignment for the list item.
876
877     \sa Qt::AlignmentFlag
878 */
879
880 /*!
881     \fn QColor QListWidgetItem::backgroundColor() const
882     \obsolete
883
884     This function is deprecated. Use background() instead.
885 */
886
887 /*!
888     \fn QBrush QListWidgetItem::background() const
889     \since 4.2
890
891     Returns the brush used to display the list item's background.
892
893     \sa setBackground() foreground()
894 */
895
896 /*!
897     \fn QColor QListWidgetItem::textColor() const
898     \obsolete
899
900     Returns the color used to display the list item's text.
901
902     This function is deprecated. Use foreground() instead.
903 */
904
905 /*!
906     \fn QBrush QListWidgetItem::foreground() const
907     \since 4.2
908
909     Returns the brush used to display the list item's foreground (e.g. text).
910
911     \sa setForeground() background()
912 */
913
914 /*!
915     \fn Qt::CheckState QListWidgetItem::checkState() const
916
917     Returns the checked state of the list item (see \l{Qt::CheckState}).
918
919     \sa flags()
920 */
921
922 /*!
923     \fn QSize QListWidgetItem::sizeHint() const
924     \since 4.1
925
926     Returns the size hint set for the list item.
927 */
928
929 /*!
930     \fn void QListWidgetItem::setSizeHint(const QSize &size)
931     \since 4.1
932
933     Sets the size hint for the list item to be \a size. If no size hint is set,
934     the item delegate will compute the size hint based on the item data.
935 */
936
937 /*!
938     \fn void QListWidgetItem::setFlags(Qt::ItemFlags flags)
939
940     Sets the item flags for the list item to \a flags.
941
942     \sa Qt::ItemFlags
943 */
944 void QListWidgetItem::setFlags(Qt::ItemFlags aflags) {
945     itemFlags = aflags;
946     if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
947         model->itemChanged(this);
948 }
949
950
951 /*!
952     \fn void QListWidgetItem::setText(const QString &text)
953
954     Sets the text for the list widget item's to the given \a text.
955
956     \sa text()
957 */
958
959 /*!
960     \fn void QListWidgetItem::setIcon(const QIcon &icon)
961
962     Sets the icon for the list item to the given \a icon.
963
964     \sa icon(), text(), {QAbstractItemView::iconSize}{iconSize}
965 */
966
967 /*!
968     \fn void QListWidgetItem::setStatusTip(const QString &statusTip)
969
970     Sets the status tip for the list item to the text specified by
971     \a statusTip. QListWidget mouseTracking needs to be enabled for this
972     feature to work.
973
974     \sa statusTip(), setToolTip(), setWhatsThis(), QWidget::setMouseTracking()
975 */
976
977 /*!
978     \fn void QListWidgetItem::setToolTip(const QString &toolTip)
979
980     Sets the tooltip for the list item to the text specified by \a toolTip.
981
982     \sa toolTip(), setStatusTip(), setWhatsThis()
983 */
984
985 /*!
986     \fn void QListWidgetItem::setWhatsThis(const QString &whatsThis)
987
988     Sets the "What's This?" help for the list item to the text specified by
989     \a whatsThis.
990
991     \sa whatsThis(), setStatusTip(), setToolTip()
992 */
993
994 /*!
995     \fn void QListWidgetItem::setFont(const QFont &font)
996
997     Sets the font used when painting the item to the given \a font.
998 */
999
1000 /*!
1001     \fn void QListWidgetItem::setTextAlignment(int alignment)
1002
1003     Sets the list item's text alignment to \a alignment.
1004
1005     \sa Qt::AlignmentFlag
1006 */
1007
1008 /*!
1009     \fn void QListWidgetItem::setBackgroundColor(const QColor &color)
1010     \obsolete
1011
1012     This function is deprecated. Use setBackground() instead.
1013 */
1014
1015 /*!
1016     \fn void QListWidgetItem::setBackground(const QBrush &brush)
1017     \since 4.2
1018
1019     Sets the background brush of the list item to the given \a brush.
1020
1021     \sa background() setForeground()
1022 */
1023
1024 /*!
1025     \fn void QListWidgetItem::setTextColor(const QColor &color)
1026     \obsolete
1027
1028     This function is deprecated. Use setForeground() instead.
1029 */
1030
1031 /*!
1032     \fn void QListWidgetItem::setForeground(const QBrush &brush)
1033     \since 4.2
1034
1035     Sets the foreground brush of the list item to the given \a brush.
1036
1037     \sa foreground() setBackground()
1038 */
1039
1040 /*!
1041     \fn void QListWidgetItem::setCheckState(Qt::CheckState state)
1042
1043     Sets the check state of the list item to \a state.
1044
1045     \sa checkState()
1046 */
1047
1048 void QListWidgetPrivate::setup()
1049 {
1050     Q_Q(QListWidget);
1051     q->QListView::setModel(new QListModel(q));
1052     // view signals
1053     QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex)));
1054     QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex)));
1055     QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
1056                      q, SLOT(_q_emitItemDoubleClicked(QModelIndex)));
1057     QObject::connect(q, SIGNAL(activated(QModelIndex)),
1058                      q, SLOT(_q_emitItemActivated(QModelIndex)));
1059     QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex)));
1060     QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
1061                      q, SLOT(_q_emitItemChanged(QModelIndex)));
1062     QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
1063                      q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
1064     QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
1065                      q, SIGNAL(itemSelectionChanged()));
1066     QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
1067                      q, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
1068     QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
1069 }
1070
1071 void QListWidgetPrivate::_q_emitItemPressed(const QModelIndex &index)
1072 {
1073     Q_Q(QListWidget);
1074     emit q->itemPressed(listModel()->at(index.row()));
1075 }
1076
1077 void QListWidgetPrivate::_q_emitItemClicked(const QModelIndex &index)
1078 {
1079     Q_Q(QListWidget);
1080     emit q->itemClicked(listModel()->at(index.row()));
1081 }
1082
1083 void QListWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index)
1084 {
1085     Q_Q(QListWidget);
1086     emit q->itemDoubleClicked(listModel()->at(index.row()));
1087 }
1088
1089 void QListWidgetPrivate::_q_emitItemActivated(const QModelIndex &index)
1090 {
1091     Q_Q(QListWidget);
1092     emit q->itemActivated(listModel()->at(index.row()));
1093 }
1094
1095 void QListWidgetPrivate::_q_emitItemEntered(const QModelIndex &index)
1096 {
1097     Q_Q(QListWidget);
1098     emit q->itemEntered(listModel()->at(index.row()));
1099 }
1100
1101 void QListWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
1102 {
1103     Q_Q(QListWidget);
1104     emit q->itemChanged(listModel()->at(index.row()));
1105 }
1106
1107 void QListWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,
1108                                                 const QModelIndex &previous)
1109 {
1110     Q_Q(QListWidget);
1111     QPersistentModelIndex persistentCurrent = current;
1112     QListWidgetItem *currentItem = listModel()->at(persistentCurrent.row());
1113     emit q->currentItemChanged(currentItem, listModel()->at(previous.row()));
1114
1115     //persistentCurrent is invalid if something changed the model in response
1116     //to the currentItemChanged signal emission and the item was removed
1117     if (!persistentCurrent.isValid()) {
1118         currentItem = 0;
1119     }
1120
1121     emit q->currentTextChanged(currentItem ? currentItem->text() : QString());
1122     emit q->currentRowChanged(persistentCurrent.row());
1123 }
1124
1125 void QListWidgetPrivate::_q_sort()
1126 {
1127     if (sortingEnabled)
1128         model->sort(0, sortOrder);
1129 }
1130
1131 void QListWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
1132                                         const QModelIndex &bottomRight)
1133 {
1134     if (sortingEnabled && topLeft.isValid() && bottomRight.isValid())
1135         listModel()->ensureSorted(topLeft.column(), sortOrder,
1136                               topLeft.row(), bottomRight.row());
1137 }
1138
1139 /*!
1140     \class QListWidget
1141     \brief The QListWidget class provides an item-based list widget.
1142
1143     \ingroup model-view
1144     \inmodule QtWidgets
1145
1146     QListWidget is a convenience class that provides a list view similar to the
1147     one supplied by QListView, but with a classic item-based interface for
1148     adding and removing items. QListWidget uses an internal model to manage
1149     each QListWidgetItem in the list.
1150
1151     For a more flexible list view widget, use the QListView class with a
1152     standard model.
1153
1154     List widgets are constructed in the same way as other widgets:
1155
1156     \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 0
1157
1158     The selectionMode() of a list widget determines how many of the items in
1159     the list can be selected at the same time, and whether complex selections
1160     of items can be created. This can be set with the setSelectionMode()
1161     function.
1162
1163     There are two ways to add items to the list: they can be constructed with
1164     the list widget as their parent widget, or they can be constructed with no
1165     parent widget and added to the list later. If a list widget already exists
1166     when the items are constructed, the first method is easier to use:
1167
1168     \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 1
1169
1170     If you need to insert a new item into the list at a particular position,
1171     then it should be constructed without a parent widget. The insertItem()
1172     function should then be used to place it within the list. The list widget
1173     will take ownership of the item.
1174
1175     \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 6
1176     \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 7
1177
1178     For multiple items, insertItems() can be used instead. The number of items
1179     in the list is found with the count() function. To remove items from the
1180     list, use takeItem().
1181
1182     The current item in the list can be found with currentItem(), and changed
1183     with setCurrentItem(). The user can also change the current item by
1184     navigating with the keyboard or clicking on a different item. When the
1185     current item changes, the currentItemChanged() signal is emitted with the
1186     new current item and the item that was previously current.
1187
1188     \table 100%
1189     \row \o \inlineimage windowsxp-listview.png Screenshot of a Windows XP style list widget
1190          \o \inlineimage macintosh-listview.png Screenshot of a Macintosh style table widget
1191          \o \inlineimage plastique-listview.png Screenshot of a Plastique style table widget
1192     \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} list widget.
1193          \o A \l{Macintosh Style Widget Gallery}{Macintosh style} list widget.
1194          \o A \l{Plastique Style Widget Gallery}{Plastique style} list widget.
1195     \endtable
1196
1197     \sa QListWidgetItem, QListView, QTreeView, {Model/View Programming},
1198         {Config Dialog Example}
1199 */
1200
1201 /*!
1202     \fn void QListWidget::addItem(QListWidgetItem *item)
1203
1204     Inserts the \a item at the end of the list widget.
1205
1206     \warning A QListWidgetItem can only be added to a QListWidget once. Adding
1207     the same QListWidgetItem multiple times to a QListWidget will result in
1208     undefined behavior.
1209
1210     \sa insertItem()
1211 */
1212
1213 /*!
1214     \fn void QListWidget::addItem(const QString &label)
1215
1216     Inserts an item with the text \a label at the end of the list widget.
1217 */
1218
1219 /*!
1220     \fn void QListWidget::addItems(const QStringList &labels)
1221
1222     Inserts items with the text \a labels at the end of the list widget.
1223
1224     \sa insertItems()
1225 */
1226
1227 /*!
1228     \fn void QListWidget::itemPressed(QListWidgetItem *item)
1229
1230     This signal is emitted with the specified \a item when a mouse button is
1231     pressed on an item in the widget.
1232
1233     \sa itemClicked(), itemDoubleClicked()
1234 */
1235
1236 /*!
1237     \fn void QListWidget::itemClicked(QListWidgetItem *item)
1238
1239     This signal is emitted with the specified \a item when a mouse button is
1240     clicked on an item in the widget.
1241
1242     \sa itemPressed(), itemDoubleClicked()
1243 */
1244
1245 /*!
1246     \fn void QListWidget::itemDoubleClicked(QListWidgetItem *item)
1247
1248     This signal is emitted with the specified \a item when a mouse button is
1249     double clicked on an item in the widget.
1250
1251     \sa itemClicked(), itemPressed()
1252 */
1253
1254 /*!
1255     \fn void QListWidget::itemActivated(QListWidgetItem *item)
1256
1257     This signal is emitted when the \a item is activated. The \a item is
1258     activated when the user clicks or double clicks on it, depending on the
1259     system configuration. It is also activated when the user presses the
1260     activation key (on Windows and X11 this is the \gui Return key, on Mac OS
1261     X it is \key{Ctrl+0}).
1262 */
1263
1264 /*!
1265     \fn void QListWidget::itemEntered(QListWidgetItem *item)
1266
1267     This signal is emitted when the mouse cursor enters an item. The \a item is
1268     the item entered. This signal is only emitted when mouseTracking is turned
1269     on, or when a mouse button is pressed while moving into an item.
1270
1271     \sa QWidget::setMouseTracking()
1272 */
1273
1274 /*!
1275     \fn void QListWidget::itemChanged(QListWidgetItem *item)
1276
1277     This signal is emitted whenever the data of \a item has changed.
1278 */
1279
1280 /*!
1281     \fn void QListWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
1282
1283     This signal is emitted whenever the current item changes.
1284
1285     \a previous is the item that previously had the focus; \a current is the
1286     new current item.
1287 */
1288
1289 /*!
1290     \fn void QListWidget::currentTextChanged(const QString &currentText)
1291
1292     This signal is emitted whenever the current item changes.
1293
1294     \a currentText is the text data in the current item. If there is no current
1295     item, the \a currentText is invalid.
1296 */
1297
1298 /*!
1299     \fn void QListWidget::currentRowChanged(int currentRow)
1300
1301     This signal is emitted whenever the current item changes.
1302
1303     \a currentRow is the row of the current item. If there is no current item,
1304     the \a currentRow is -1.
1305 */
1306
1307 /*!
1308     \fn void QListWidget::itemSelectionChanged()
1309
1310     This signal is emitted whenever the selection changes.
1311
1312     \sa selectedItems(), QListWidgetItem::isSelected(), currentItemChanged()
1313 */
1314
1315 /*!
1316     \since 4.3
1317
1318     \fn void QListWidget::removeItemWidget(QListWidgetItem *item)
1319
1320     Removes the widget set on the given \a item.
1321 */
1322
1323 /*!
1324     Constructs an empty QListWidget with the given \a parent.
1325 */
1326
1327 QListWidget::QListWidget(QWidget *parent)
1328     : QListView(*new QListWidgetPrivate(), parent)
1329 {
1330     Q_D(QListWidget);
1331     d->setup();
1332 }
1333
1334 /*!
1335     Destroys the list widget and all its items.
1336 */
1337
1338 QListWidget::~QListWidget()
1339 {
1340 }
1341
1342 /*!
1343     Returns the item that occupies the given \a row in the list if one has been
1344     set; otherwise returns 0.
1345
1346     \sa row()
1347 */
1348
1349 QListWidgetItem *QListWidget::item(int row) const
1350 {
1351     Q_D(const QListWidget);
1352     if (row < 0 || row >= d->model->rowCount())
1353         return 0;
1354     return d->listModel()->at(row);
1355 }
1356
1357 /*!
1358     Returns the row containing the given \a item.
1359
1360     \sa item()
1361 */
1362
1363 int QListWidget::row(const QListWidgetItem *item) const
1364 {
1365     Q_D(const QListWidget);
1366     return d->listModel()->index(const_cast<QListWidgetItem*>(item)).row();
1367 }
1368
1369
1370 /*!
1371     Inserts the \a item at the position in the list given by \a row.
1372
1373     \sa addItem()
1374 */
1375
1376 void QListWidget::insertItem(int row, QListWidgetItem *item)
1377 {
1378     Q_D(QListWidget);
1379     if (item && !item->view)
1380         d->listModel()->insert(row, item);
1381 }
1382
1383 /*!
1384     Inserts an item with the text \a label in the list widget at the position
1385     given by \a row.
1386
1387     \sa addItem()
1388 */
1389
1390 void QListWidget::insertItem(int row, const QString &label)
1391 {
1392     Q_D(QListWidget);
1393     d->listModel()->insert(row, new QListWidgetItem(label));
1394 }
1395
1396 /*!
1397     Inserts items from the list of \a labels into the list, starting at the
1398     given \a row.
1399
1400     \sa insertItem(), addItem()
1401 */
1402
1403 void QListWidget::insertItems(int row, const QStringList &labels)
1404 {
1405     Q_D(QListWidget);
1406     d->listModel()->insert(row, labels);
1407 }
1408
1409 /*!
1410     Removes and returns the item from the given \a row in the list widget;
1411     otherwise returns 0.
1412
1413     Items removed from a list widget will not be managed by Qt, and will need
1414     to be deleted manually.
1415
1416     \sa insertItem(), addItem()
1417 */
1418
1419 QListWidgetItem *QListWidget::takeItem(int row)
1420 {
1421     Q_D(QListWidget);
1422     if (row < 0 || row >= d->model->rowCount())
1423         return 0;
1424     return d->listModel()->take(row);
1425 }
1426
1427 /*!
1428     \property QListWidget::count
1429     \brief the number of items in the list including any hidden items.
1430 */
1431
1432 int QListWidget::count() const
1433 {
1434     Q_D(const QListWidget);
1435     return d->model->rowCount();
1436 }
1437
1438 /*!
1439     Returns the current item.
1440 */
1441 QListWidgetItem *QListWidget::currentItem() const
1442 {
1443     Q_D(const QListWidget);
1444     return d->listModel()->at(currentIndex().row());
1445 }
1446
1447
1448 /*!
1449     Sets the current item to \a item.
1450
1451     Unless the selection mode is \l{QAbstractItemView::}{NoSelection},
1452     the item is also be selected.
1453 */
1454 void QListWidget::setCurrentItem(QListWidgetItem *item)
1455 {
1456     setCurrentRow(row(item));
1457 }
1458
1459 /*!
1460     \since 4.4
1461     Set the current item to \a item, using the given \a command.
1462 */
1463 void QListWidget::setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command)
1464 {
1465     setCurrentRow(row(item), command);
1466 }
1467
1468 /*!
1469     \property QListWidget::currentRow
1470     \brief the row of the current item.
1471
1472     Depending on the current selection mode, the row may also be selected.
1473 */
1474
1475 int QListWidget::currentRow() const
1476 {
1477     return currentIndex().row();
1478 }
1479
1480 void QListWidget::setCurrentRow(int row)
1481 {
1482     Q_D(QListWidget);
1483     QModelIndex index = d->listModel()->index(row);
1484     if (d->selectionMode == SingleSelection)
1485         selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
1486     else if (d->selectionMode == NoSelection)
1487         selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1488     else
1489         selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
1490 }
1491
1492 /*!
1493     \since 4.4
1494
1495     Sets the current row to be the given \a row, using the given \a command,
1496 */
1497 void QListWidget::setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)
1498 {
1499     Q_D(QListWidget);
1500     d->selectionModel->setCurrentIndex(d->listModel()->index(row), command);
1501 }
1502
1503 /*!
1504     Returns a pointer to the item at the coordinates \a p. The coordinates
1505     are relative to the list widget's \l{QAbstractScrollArea::}{viewport()}.
1506
1507 */
1508 QListWidgetItem *QListWidget::itemAt(const QPoint &p) const
1509 {
1510     Q_D(const QListWidget);
1511     return d->listModel()->at(indexAt(p).row());
1512
1513 }
1514
1515 /*!
1516     \fn QListWidgetItem *QListWidget::itemAt(int x, int y) const
1517     \overload
1518
1519     Returns a pointer to the item at the coordinates (\a x, \a y).
1520     The coordinates are relative to the list widget's
1521     \l{QAbstractScrollArea::}{viewport()}.
1522
1523 */
1524
1525
1526 /*!
1527     Returns the rectangle on the viewport occupied by the item at \a item.
1528 */
1529 QRect QListWidget::visualItemRect(const QListWidgetItem *item) const
1530 {
1531     Q_D(const QListWidget);
1532     QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
1533     return visualRect(index);
1534 }
1535
1536 /*!
1537     Sorts all the items in the list widget according to the specified \a order.
1538 */
1539 void QListWidget::sortItems(Qt::SortOrder order)
1540 {
1541     Q_D(QListWidget);
1542     d->sortOrder = order;
1543     d->listModel()->sort(0, order);
1544 }
1545
1546 /*!
1547     \since 4.2
1548     \property QListWidget::sortingEnabled
1549     \brief whether sorting is enabled
1550
1551     If this property is true, sorting is enabled for the list; if the property
1552     is false, sorting is not enabled.
1553
1554     The default value is false.
1555 */
1556 void QListWidget::setSortingEnabled(bool enable)
1557 {
1558     Q_D(QListWidget);
1559     d->sortingEnabled = enable;
1560 }
1561
1562 bool QListWidget::isSortingEnabled() const
1563 {
1564     Q_D(const QListWidget);
1565     return d->sortingEnabled;
1566 }
1567
1568 /*!
1569     \internal
1570 */
1571 Qt::SortOrder QListWidget::sortOrder() const
1572 {
1573     Q_D(const QListWidget);
1574     return d->sortOrder;
1575 }
1576
1577 /*!
1578     Starts editing the \a item if it is editable.
1579 */
1580
1581 void QListWidget::editItem(QListWidgetItem *item)
1582 {
1583     Q_D(QListWidget);
1584     edit(d->listModel()->index(item));
1585 }
1586
1587 /*!
1588     Opens an editor for the given \a item. The editor remains open after
1589     editing.
1590
1591     \sa closePersistentEditor()
1592 */
1593 void QListWidget::openPersistentEditor(QListWidgetItem *item)
1594 {
1595     Q_D(QListWidget);
1596     QModelIndex index = d->listModel()->index(item);
1597     QAbstractItemView::openPersistentEditor(index);
1598 }
1599
1600 /*!
1601     Closes the persistent editor for the given \a item.
1602
1603     \sa openPersistentEditor()
1604 */
1605 void QListWidget::closePersistentEditor(QListWidgetItem *item)
1606 {
1607     Q_D(QListWidget);
1608     QModelIndex index = d->listModel()->index(item);
1609     QAbstractItemView::closePersistentEditor(index);
1610 }
1611
1612 /*!
1613     \since 4.1
1614
1615     Returns the widget displayed in the given \a item.
1616 */
1617 QWidget *QListWidget::itemWidget(QListWidgetItem *item) const
1618 {
1619     Q_D(const QListWidget);
1620     QModelIndex index = d->listModel()->index(item);
1621     return QAbstractItemView::indexWidget(index);
1622 }
1623
1624 /*!
1625     \since 4.1
1626
1627     Sets the \a widget to be displayed in the give \a item.
1628
1629     This function should only be used to display static content in the place of
1630     a list widget item. If you want to display custom dynamic content or
1631     implement a custom editor widget, use QListView and subclass QItemDelegate
1632     instead.
1633
1634     \sa {Delegate Classes}
1635 */
1636 void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)
1637 {
1638     Q_D(QListWidget);
1639     QModelIndex index = d->listModel()->index(item);
1640     QAbstractItemView::setIndexWidget(index, widget);
1641 }
1642
1643 /*!
1644     Returns true if \a item is selected; otherwise returns false.
1645
1646     \obsolete
1647
1648     This function is deprecated. Use QListWidgetItem::isSelected() instead.
1649 */
1650 bool QListWidget::isItemSelected(const QListWidgetItem *item) const
1651 {
1652     Q_D(const QListWidget);
1653     QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
1654     return selectionModel()->isSelected(index);
1655 }
1656
1657 /*!
1658     Selects or deselects the given \a item depending on whether \a select is
1659     true of false.
1660
1661     \obsolete
1662
1663     This function is deprecated. Use QListWidgetItem::setSelected() instead.
1664 */
1665 void QListWidget::setItemSelected(const QListWidgetItem *item, bool select)
1666 {
1667     Q_D(QListWidget);
1668     QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
1669
1670     if (d->selectionMode == SingleSelection) {
1671         selectionModel()->select(index, select
1672                                  ? QItemSelectionModel::ClearAndSelect
1673                                  : QItemSelectionModel::Deselect);
1674     } else if (d->selectionMode != NoSelection) {
1675         selectionModel()->select(index, select
1676                                  ? QItemSelectionModel::Select
1677                                  : QItemSelectionModel::Deselect);
1678     }
1679
1680 }
1681
1682 /*!
1683     Returns a list of all selected items in the list widget.
1684 */
1685
1686 QList<QListWidgetItem*> QListWidget::selectedItems() const
1687 {
1688     Q_D(const QListWidget);
1689     QModelIndexList indexes = selectionModel()->selectedIndexes();
1690     QList<QListWidgetItem*> items;
1691     for (int i = 0; i < indexes.count(); ++i)
1692         items.append(d->listModel()->at(indexes.at(i).row()));
1693     return items;
1694 }
1695
1696 /*!
1697     Finds items with the text that matches the string \a text using the given
1698     \a flags.
1699 */
1700
1701 QList<QListWidgetItem*> QListWidget::findItems(const QString &text, Qt::MatchFlags flags) const
1702 {
1703     Q_D(const QListWidget);
1704     QModelIndexList indexes = d->listModel()->match(model()->index(0, 0, QModelIndex()),
1705                                                 Qt::DisplayRole, text, -1, flags);
1706     QList<QListWidgetItem*> items;
1707     for (int i = 0; i < indexes.size(); ++i)
1708         items.append(d->listModel()->at(indexes.at(i).row()));
1709     return items;
1710 }
1711
1712 /*!
1713     Returns true if the \a item is explicitly hidden; otherwise returns false.
1714
1715     \obsolete
1716
1717     This function is deprecated. Use QListWidgetItem::isHidden() instead.
1718 */
1719 bool QListWidget::isItemHidden(const QListWidgetItem *item) const
1720 {
1721     return isRowHidden(row(item));
1722 }
1723
1724 /*!
1725     If \a hide is true, the \a item will be hidden; otherwise it will be shown.
1726
1727     \obsolete
1728
1729     This function is deprecated. Use QListWidgetItem::setHidden() instead.
1730 */
1731 void QListWidget::setItemHidden(const QListWidgetItem *item, bool hide)
1732 {
1733     setRowHidden(row(item), hide);
1734 }
1735
1736 /*!
1737     Scrolls the view if necessary to ensure that the \a item is visible.
1738
1739     \a hint specifies where the \a item should be located after the operation.
1740 */
1741
1742 void QListWidget::scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint)
1743 {
1744     Q_D(QListWidget);
1745     QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
1746     QListView::scrollTo(index, hint);
1747 }
1748
1749 /*!
1750     Removes all items and selections in the view.
1751
1752     \warning All items will be permanently deleted.
1753 */
1754 void QListWidget::clear()
1755 {
1756     Q_D(QListWidget);
1757     selectionModel()->clear();
1758     d->listModel()->clear();
1759 }
1760
1761 /*!
1762     Returns a list of MIME types that can be used to describe a list of
1763     listwidget items.
1764
1765     \sa mimeData()
1766 */
1767 QStringList QListWidget::mimeTypes() const
1768 {
1769     return d_func()->listModel()->QAbstractListModel::mimeTypes();
1770 }
1771
1772 /*!
1773     Returns an object that contains a serialized description of the specified
1774     \a items. The format used to describe the items is obtained from the
1775     mimeTypes() function.
1776
1777     If the list of items is empty, 0 is returned instead of a serialized empty
1778     list.
1779 */
1780 QMimeData *QListWidget::mimeData(const QList<QListWidgetItem*>) const
1781 {
1782     return d_func()->listModel()->internalMimeData();
1783 }
1784
1785 #ifndef QT_NO_DRAGANDDROP
1786 /*!
1787     Handles \a data supplied by an external drag and drop operation that ended
1788     with the given \a action in the given \a index. Returns true if \a data and
1789     \a action can be handled by the model; otherwise returns false.
1790
1791     \sa supportedDropActions()
1792 */
1793 bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
1794 {
1795     QModelIndex idx;
1796     int row = index;
1797     int column = 0;
1798     if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
1799         // QAbstractListModel::dropMimeData will overwrite on the index if row == -1 and column == -1
1800         idx = model()->index(row, column);
1801         row = -1;
1802         column = -1;
1803     }
1804     return d_func()->listModel()->QAbstractListModel::dropMimeData(data, action , row, column, idx);
1805 }
1806
1807 /*! \reimp */
1808 void QListWidget::dropEvent(QDropEvent *event) {
1809     Q_D(QListWidget);
1810     if (event->source() == this && d->movement != Static) {
1811         QListView::dropEvent(event);
1812         return;
1813     }
1814
1815     if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
1816                                     dragDropMode() == QAbstractItemView::InternalMove)) {
1817         QModelIndex topIndex;
1818         int col = -1;
1819         int row = -1;
1820         if (d->dropOn(event, &row, &col, &topIndex)) {
1821             QList<QModelIndex> selIndexes = selectedIndexes();
1822             QList<QPersistentModelIndex> persIndexes;
1823             for (int i = 0; i < selIndexes.count(); i++)
1824                 persIndexes.append(selIndexes.at(i));
1825
1826             if (persIndexes.contains(topIndex))
1827                 return;
1828             qSort(persIndexes); // The dropped items will remain in the same visual order.
1829
1830             QPersistentModelIndex dropRow = model()->index(row, col, topIndex);
1831
1832             int r = row == -1 ? count() : (dropRow.row() >= 0 ? dropRow.row() : row);
1833             for (int i = 0; i < persIndexes.count(); ++i) {
1834                 const QPersistentModelIndex &pIndex = persIndexes.at(i);
1835                 d->listModel()->move(pIndex.row(), r);
1836                 r = pIndex.row() + 1;   // Dropped items are inserted contiguously and in the right order.
1837             }
1838
1839             event->accept();
1840             // Don't want QAbstractItemView to delete it because it was "moved" we already did it
1841             event->setDropAction(Qt::CopyAction);
1842         }
1843     }
1844
1845     QListView::dropEvent(event);
1846 }
1847
1848 /*!
1849     Returns the drop actions supported by this view.
1850
1851     \sa Qt::DropActions
1852 */
1853 Qt::DropActions QListWidget::supportedDropActions() const
1854 {
1855     Q_D(const QListWidget);
1856     return d->listModel()->QAbstractListModel::supportedDropActions() | Qt::MoveAction;
1857 }
1858 #endif // QT_NO_DRAGANDDROP
1859
1860 /*!
1861     Returns a list of pointers to the items contained in the \a data object. If
1862     the object was not created by a QListWidget in the same process, the list
1863     is empty.
1864 */
1865 QList<QListWidgetItem*> QListWidget::items(const QMimeData *data) const
1866 {
1867     const QListWidgetMimeData *lwd = qobject_cast<const QListWidgetMimeData*>(data);
1868     if (lwd)
1869         return lwd->items;
1870     return QList<QListWidgetItem*>();
1871 }
1872
1873 /*!
1874     Returns the QModelIndex assocated with the given \a item.
1875 */
1876
1877 QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const
1878 {
1879     Q_D(const QListWidget);
1880     return d->listModel()->index(item);
1881 }
1882
1883 /*!
1884     Returns a pointer to the QListWidgetItem assocated with the given \a index.
1885 */
1886
1887 QListWidgetItem *QListWidget::itemFromIndex(const QModelIndex &index) const
1888 {
1889     Q_D(const QListWidget);
1890     if (d->isIndexValid(index))
1891         return d->listModel()->at(index.row());
1892     return 0;
1893 }
1894
1895 /*!
1896     \internal
1897 */
1898 void QListWidget::setModel(QAbstractItemModel * /*model*/)
1899 {
1900     Q_ASSERT(!"QListWidget::setModel() - Changing the model of the QListWidget is not allowed.");
1901 }
1902
1903 /*!
1904     \reimp
1905 */
1906 bool QListWidget::event(QEvent *e)
1907 {
1908     return QListView::event(e);
1909 }
1910
1911 QT_END_NAMESPACE
1912
1913 #include "moc_qlistwidget.cpp"
1914
1915 #endif // QT_NO_LISTWIDGET