diff options
Diffstat (limited to 'src/widgets/itemviews/qlistwidget.cpp')
-rw-r--r-- | src/widgets/itemviews/qlistwidget.cpp | 93 |
1 files changed, 73 insertions, 20 deletions
diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index 72e0a67a64..6e0bdb3f22 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -227,6 +227,22 @@ bool QListModel::setData(const QModelIndex &index, const QVariant &value, int ro return true; } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +bool QListModel::clearItemData(const QModelIndex &index) +{ + if (!checkIndex(index, CheckIndexOption::IndexIsValid)) + return false; + QListWidgetItem *item = items.at(index.row()); + const auto beginIter = item->d->values.cbegin(); + const auto endIter = item->d->values.cend(); + if (std::all_of(beginIter, endIter, [](const QWidgetItemData& data) -> bool { return !data.value.isValid(); })) + return true; //it's already cleared + item->d->values.clear(); + emit dataChanged(index, index, QVector<int>{}); + return true; +} +#endif + QMap<int, QVariant> QListModel::itemData(const QModelIndex &index) const { QMap<int, QVariant> roles; @@ -277,6 +293,30 @@ bool QListModel::removeRows(int row, int count, const QModelIndex &parent) return true; } +/*! + \since 5.13 + \reimp +*/ +bool QListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) +{ + if (sourceRow < 0 + || sourceRow + count - 1 >= rowCount(sourceParent) + || destinationChild <= 0 + || destinationChild > rowCount(destinationParent) + || sourceRow == destinationChild - 1 + || count <= 0) { + return false; + } + if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild)) + return false; + destinationChild--; + const int fromRow = destinationChild < sourceRow ? (sourceRow + count - 1) : sourceRow; + while (count--) + items.move(fromRow, destinationChild); + endMoveRows(); + return true; +} + Qt::ItemFlags QListModel::flags(const QModelIndex &index) const { if (!index.isValid() || index.row() >= items.count() || index.model() != this) @@ -289,7 +329,7 @@ void QListModel::sort(int column, Qt::SortOrder order) if (column != 0) return; - emit layoutAboutToBeChanged(); + emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint); QVector < QPair<QListWidgetItem*,int> > sorting(items.count()); for (int i = 0; i < items.count(); ++i) { @@ -313,7 +353,7 @@ void QListModel::sort(int column, Qt::SortOrder order) } changePersistentIndexList(fromIndexes, toIndexes); - emit layoutChanged(); + emit layoutChanged({}, QAbstractItemModel::VerticalSortHint); } /** @@ -594,14 +634,14 @@ Qt::DropActions QListModel::supportedDropActions() const \sa type() */ -QListWidgetItem::QListWidgetItem(QListWidget *view, int type) - : rtti(type), view(view), d(new QListWidgetItemPrivate(this)), +QListWidgetItem::QListWidgetItem(QListWidget *listview, int type) + : rtti(type), view(listview), d(new QListWidgetItemPrivate(this)), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled) { - if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) + if (QListModel *model = listModel()) model->insert(model->rowCount(), this); } @@ -621,16 +661,18 @@ QListWidgetItem::QListWidgetItem(QListWidget *view, int type) \sa type() */ -QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *view, int type) - : rtti(type), view(0), d(new QListWidgetItemPrivate(this)), +QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *listview, int type) + : rtti(type), view(listview), d(new QListWidgetItemPrivate(this)), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled) { - setData(Qt::DisplayRole, text); - this->view = view; - if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) + { + QSignalBlocker b(view); + setData(Qt::DisplayRole, text); + } + if (QListModel *model = listModel()) model->insert(model->rowCount(), this); } @@ -652,17 +694,19 @@ QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *view, int typ \sa type() */ QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text, - QListWidget *view, int type) - : rtti(type), view(0), d(new QListWidgetItemPrivate(this)), + QListWidget *listview, int type) + : rtti(type), view(listview), d(new QListWidgetItemPrivate(this)), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled) { - setData(Qt::DisplayRole, text); - setData(Qt::DecorationRole, icon); - this->view = view; - if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) + { + QSignalBlocker b(view); + setData(Qt::DisplayRole, text); + setData(Qt::DecorationRole, icon); + } + if (QListModel *model = listModel()) model->insert(model->rowCount(), this); } @@ -671,7 +715,7 @@ QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text, */ QListWidgetItem::~QListWidgetItem() { - if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) + if (QListModel *model = listModel()) model->remove(this); delete d; } @@ -708,7 +752,7 @@ void QListWidgetItem::setData(int role, const QVariant &value) } if (!found) d->values.append(QWidgetItemData(role, value)); - if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : nullptr)) { + if (QListModel *model = listModel()) { const QVector<int> roles((role == Qt::DisplayRole) ? QVector<int>({Qt::DisplayRole, Qt::EditRole}) : QVector<int>({role})); @@ -775,7 +819,7 @@ void QListWidgetItem::write(QDataStream &out) const \sa data(), flags() */ QListWidgetItem::QListWidgetItem(const QListWidgetItem &other) - : rtti(Type), view(0), + : rtti(Type), view(nullptr), d(new QListWidgetItemPrivate(this)), itemFlags(other.itemFlags) { @@ -797,6 +841,15 @@ QListWidgetItem &QListWidgetItem::operator=(const QListWidgetItem &other) return *this; } +/*! + \internal + returns the QListModel if a view is set + */ +QListModel *QListWidgetItem::listModel() const +{ + return (view ? qobject_cast<QListModel*>(view->model()) : nullptr); +} + #ifndef QT_NO_DATASTREAM /*! @@ -958,7 +1011,7 @@ QDataStream &operator>>(QDataStream &in, QListWidgetItem &item) void QListWidgetItem::setFlags(Qt::ItemFlags aflags) { itemFlags = aflags; - if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) + if (QListModel *model = listModel()) model->itemChanged(this); } |