source: trunk/src/gui/itemviews/qlistwidget.cpp@ 273

Last change on this file since 273 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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