diff options
Diffstat (limited to 'src/widgets/itemviews')
-rw-r--r-- | src/widgets/itemviews/qabstractitemview.cpp | 39 | ||||
-rw-r--r-- | src/widgets/itemviews/qabstractitemview.h | 1 | ||||
-rw-r--r-- | src/widgets/itemviews/qabstractitemview_p.h | 22 | ||||
-rw-r--r-- | src/widgets/itemviews/qdirmodel.cpp | 19 | ||||
-rw-r--r-- | src/widgets/itemviews/qfileiconprovider.cpp | 13 | ||||
-rw-r--r-- | src/widgets/itemviews/qheaderview.cpp | 134 | ||||
-rw-r--r-- | src/widgets/itemviews/qheaderview_p.h | 11 | ||||
-rw-r--r-- | src/widgets/itemviews/qitemeditorfactory.cpp | 7 | ||||
-rw-r--r-- | src/widgets/itemviews/qlistview.cpp | 45 | ||||
-rw-r--r-- | src/widgets/itemviews/qlistview_p.h | 13 | ||||
-rw-r--r-- | src/widgets/itemviews/qlistwidget.cpp | 4 | ||||
-rw-r--r-- | src/widgets/itemviews/qtableview.cpp | 30 | ||||
-rw-r--r-- | src/widgets/itemviews/qtablewidget.cpp | 33 | ||||
-rw-r--r-- | src/widgets/itemviews/qtreeview.cpp | 8 | ||||
-rw-r--r-- | src/widgets/itemviews/qtreewidget.cpp | 63 | ||||
-rw-r--r-- | src/widgets/itemviews/qtreewidget.h | 3 | ||||
-rw-r--r-- | src/widgets/itemviews/qwidgetitemdata_p.h | 2 |
17 files changed, 286 insertions, 161 deletions
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index ad7be840d0..edfb5c0f57 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -54,6 +54,7 @@ #include <private/qabstractitemview_p.h> #include <private/qabstractitemmodel_p.h> #include <private/qguiapplication_p.h> +#include <private/qscrollbar_p.h> #ifndef QT_NO_ACCESSIBILITY #include <qaccessible.h> #endif @@ -427,6 +428,11 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged() \since 4.2 \enum QAbstractItemView::ScrollMode + Describes how the scrollbar should behave. When setting the scroll mode + to ScrollPerPixel the single step size will adjust automatically unless + it was set explicitly using \l{QAbstractSlider::}{setSingleStep()}. + The automatic adjustment can be restored by setting the single step size to -1. + \value ScrollPerItem The view will scroll the contents one item at a time. \value ScrollPerPixel The view will scroll the contents one pixel at a time. */ @@ -758,7 +764,7 @@ void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel) Q_ASSERT(selectionModel); Q_D(QAbstractItemView); - if (selectionModel->model() != d->model) { + if (Q_UNLIKELY(selectionModel->model() != d->model)) { qWarning("QAbstractItemView::setSelectionModel() failed: " "Trying to set a selection model, which works on " "a different model than the view."); @@ -1113,7 +1119,7 @@ void QAbstractItemView::reset() void QAbstractItemView::setRootIndex(const QModelIndex &index) { Q_D(QAbstractItemView); - if (index.isValid() && index.model() != d->model) { + if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) { qWarning("QAbstractItemView::setRootIndex failed : index must be from the currently set model"); return; } @@ -1166,9 +1172,9 @@ void QAbstractItemView::selectAll() void QAbstractItemView::edit(const QModelIndex &index) { Q_D(QAbstractItemView); - if (!d->isIndexValid(index)) + if (Q_UNLIKELY(!d->isIndexValid(index))) qWarning("edit: index was invalid"); - if (!edit(index, AllEditTriggers, 0)) + if (Q_UNLIKELY(!edit(index, AllEditTriggers, 0))) qWarning("edit: editing failed"); } @@ -1235,6 +1241,10 @@ void QAbstractItemView::setVerticalScrollMode(ScrollMode mode) return; QModelIndex topLeft = indexAt(QPoint(0, 0)); d->verticalScrollMode = mode; + if (mode == ScrollPerItem) + verticalScrollBar()->d_func()->itemviewChangeSingleStep(1); // setSingleStep(-1) => step with 1 + else + verticalScrollBar()->setSingleStep(-1); // Ensure that the view can update single step updateGeometries(); // update the scroll bars scrollTo(topLeft, QAbstractItemView::PositionAtTop); } @@ -1257,7 +1267,13 @@ QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode() const void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode) { Q_D(QAbstractItemView); + if (mode == d->horizontalScrollMode) + return; d->horizontalScrollMode = mode; + if (mode == ScrollPerItem) + horizontalScrollBar()->d_func()->itemviewChangeSingleStep(1); // setSingleStep(-1) => step with 1 + else + horizontalScrollBar()->setSingleStep(-1); // Ensure that the view can update single step updateGeometries(); // update the scroll bars } @@ -4274,9 +4290,8 @@ QModelIndex QAbstractItemViewPrivate::indexForEditor(QWidget *editor) const void QAbstractItemViewPrivate::removeEditor(QWidget *editor) { - QEditorIndexHash::iterator it = editorIndexHash.find(editor); - if (it != editorIndexHash.end()) - { + const auto it = editorIndexHash.constFind(editor); + if (it != editorIndexHash.cend()) { indexEditorHash.remove(it.value()); editorIndexHash.erase(it); } @@ -4335,11 +4350,11 @@ QItemViewPaintPairs QAbstractItemViewPrivate::draggablePaintPairs(const QModelIn QRect &rect = *r; const QRect viewportRect = viewport->rect(); QItemViewPaintPairs ret; - for (int i = 0; i < indexes.count(); ++i) { - const QModelIndex &index = indexes.at(i); + for (const auto &index : indexes) { const QRect current = q->visualRect(index); if (current.intersects(viewportRect)) { - ret += qMakePair(current, index); + QItemViewPaintPair p = { current, index }; + ret += p; rect |= current; } } @@ -4359,8 +4374,8 @@ QPixmap QAbstractItemViewPrivate::renderToPixmap(const QModelIndexList &indexes, QStyleOptionViewItem option = viewOptionsV1(); option.state |= QStyle::State_Selected; for (int j = 0; j < paintPairs.count(); ++j) { - option.rect = paintPairs.at(j).first.translated(-r->topLeft()); - const QModelIndex ¤t = paintPairs.at(j).second; + option.rect = paintPairs.at(j).rect.translated(-r->topLeft()); + const QModelIndex ¤t = paintPairs.at(j).index; adjustViewOptionsForIndex(&option, current); delegateForIndex(current)->paint(&painter, option, current); } diff --git a/src/widgets/itemviews/qabstractitemview.h b/src/widgets/itemviews/qabstractitemview.h index ff1848b149..e32c5dad2f 100644 --- a/src/widgets/itemviews/qabstractitemview.h +++ b/src/widgets/itemviews/qabstractitemview.h @@ -362,6 +362,7 @@ private: friend class QTreeViewPrivate; // needed to compile with MSVC friend class QListModeViewBase; friend class QListViewPrivate; + friend class QAbstractSlider; }; Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemView::EditTriggers) diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h index bb88b25652..2b28d8db84 100644 --- a/src/widgets/itemviews/qabstractitemview_p.h +++ b/src/widgets/itemviews/qabstractitemview_p.h @@ -73,7 +73,13 @@ struct QEditorInfo { typedef QHash<QWidget *, QPersistentModelIndex> QEditorIndexHash; typedef QHash<QPersistentModelIndex, QEditorInfo> QIndexEditorHash; -typedef QPair<QRect, QModelIndex> QItemViewPaintPair; +struct QItemViewPaintPair { + QRect rect; + QModelIndex index; +}; +template <> +class QTypeInfo<QItemViewPaintPair> : public QTypeInfoMerger<QItemViewPaintPair, QRect, QModelIndex> {}; + typedef QVector<QItemViewPaintPair> QItemViewPaintPairs; class QEmptyModel : public QAbstractItemModel @@ -165,21 +171,21 @@ public: virtual QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const; inline bool canDrop(QDropEvent *event) { - QModelIndex index; - int col = -1; - int row = -1; const QMimeData *mime = event->mimeData(); // Drag enter event shall always be accepted, if mime type and action match. // Whether the data can actually be dropped will be checked in drag move. - if (event->type() == QEvent::DragEnter) { + if (event->type() == QEvent::DragEnter && (event->dropAction() & model->supportedDropActions())) { const QStringList modelTypes = model->mimeTypes(); - for (int i = 0; i < modelTypes.count(); ++i) - if (mime->hasFormat(modelTypes.at(i)) - && (event->dropAction() & model->supportedDropActions())) + for (const auto &modelType : modelTypes) { + if (mime->hasFormat(modelType)) return true; + } } + QModelIndex index; + int col = -1; + int row = -1; if (dropOn(event, &row, &col, &index)) { return model->canDropMimeData(mime, dragDropMode == QAbstractItemView::InternalMove ? Qt::MoveAction : event->dropAction(), diff --git a/src/widgets/itemviews/qdirmodel.cpp b/src/widgets/itemviews/qdirmodel.cpp index 0c157c940f..8a7ab3c63c 100644 --- a/src/widgets/itemviews/qdirmodel.cpp +++ b/src/widgets/itemviews/qdirmodel.cpp @@ -34,7 +34,6 @@ #include "qdirmodel.h" #ifndef QT_NO_DIRMODEL -#include <qstack.h> #include <qfile.h> #include <qfilesystemmodel.h> #include <qurl.h> @@ -49,6 +48,9 @@ #include <private/qabstractitemmodel_p.h> #include <qdebug.h> +#include <stack> +#include <vector> + /*! \enum QDirModel::Roles \value FileIconRole @@ -163,14 +165,15 @@ void QDirModelPrivate::clear(QDirNode *parent) const void QDirModelPrivate::invalidate() { - QStack<const QDirNode*> nodes; + std::stack<const QDirNode*, std::vector<const QDirNode*> > nodes; nodes.push(&root); while (!nodes.empty()) { - const QDirNode *current = nodes.pop(); + const QDirNode *current = nodes.top(); + nodes.pop(); current->stat = false; - const QVector<QDirNode> children = current->children; - for (int i = 0; i < children.count(); ++i) - nodes.push(&children.at(i)); + const QVector<QDirNode> &children = current->children; + for (const auto &child : children) + nodes.push(&child); } } @@ -1022,7 +1025,7 @@ bool QDirModel::rmdir(const QModelIndex &index) return false; QDirModelPrivate::QDirNode *n = d_func()->node(index); - if (!n->info.isDir()) { + if (Q_UNLIKELY(!n->info.isDir())) { qWarning("rmdir: the node is not a directory"); return false; } @@ -1172,7 +1175,7 @@ QDirModelPrivate::QDirNode *QDirModelPrivate::node(int row, QDirNode *parent) co if (isDir && !p->populated) populate(p); // will also resolve symlinks - if (row >= p->children.count()) { + if (Q_UNLIKELY(row >= p->children.count())) { qWarning("node: the row does not exist"); return 0; } diff --git a/src/widgets/itemviews/qfileiconprovider.cpp b/src/widgets/itemviews/qfileiconprovider.cpp index f43bcd5d5a..18b9755477 100644 --- a/src/widgets/itemviews/qfileiconprovider.cpp +++ b/src/widgets/itemviews/qfileiconprovider.cpp @@ -52,10 +52,6 @@ # endif #endif -#if defined(Q_OS_UNIX) && !defined(QT_NO_STYLE_GTK) -# include <private/qgtkstyle_p_p.h> -#endif - QT_BEGIN_NAMESPACE static bool isCacheable(const QFileInfo &fi); @@ -358,15 +354,6 @@ QIcon QFileIconProvider::icon(const QFileInfo &info) const { Q_D(const QFileIconProvider); -#if defined(Q_OS_UNIX) && !defined(QT_NO_STYLE_GTK) - const QByteArray desktopEnvironment = QGuiApplicationPrivate::platformIntegration()->services()->desktopEnvironment(); - if (desktopEnvironment != QByteArrayLiteral("KDE")) { - QIcon gtkIcon = QGtkStylePrivate::getFilesystemIcon(info); - if (!gtkIcon.isNull()) - return gtkIcon; - } -#endif - QIcon retIcon = d->getIcon(info); if (!retIcon.isNull()) return retIcon; diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index 338627c79f..24b31f9eaf 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -753,9 +753,6 @@ void QHeaderView::moveSection(int from, int to) return; } - if (stretchLastSection() && to == d->lastVisibleVisualIndex()) - d->lastSectionSize = sectionSize(from); - d->initializeIndexMapping(); int *visualIndices = d->visualIndices.data(); @@ -788,6 +785,12 @@ void QHeaderView::moveSection(int from, int to) d->viewport->update(); emit sectionMoved(logical, from, to); + + if (stretchLastSection()) { + const int lastSectionVisualIdx = visualIndex(d->lastSectionLogicalIdx); + if (from >= lastSectionVisualIdx || to >= lastSectionVisualIdx) + d->maybeRestorePrevLastSectionAndStretchLast(); + } } /*! @@ -839,6 +842,12 @@ void QHeaderView::swapSections(int first, int second) d->viewport->update(); emit sectionMoved(firstLogical, first, second); emit sectionMoved(secondLogical, second, first); + + if (stretchLastSection()) { + const int lastSectionVisualIdx = visualIndex(d->lastSectionLogicalIdx); + if (first >= lastSectionVisualIdx || second >= lastSectionVisualIdx) + d->maybeRestorePrevLastSectionAndStretchLast(); + } } /*! @@ -877,7 +886,7 @@ void QHeaderView::resizeSection(int logical, int size) d->executePostedLayout(); d->invalidateCachedSizeHint(); - if (stretchLastSection() && visual == d->lastVisibleVisualIndex()) + if (stretchLastSection() && logical == d->lastSectionLogicalIdx) d->lastSectionSize = size; d->createSectionItems(visual, visual, size, d->headerSectionResizeMode(visual)); @@ -1000,11 +1009,16 @@ void QHeaderView::setSectionHidden(int logicalIndex, bool hide) if (hide == d->isVisualIndexHidden(visual)) return; if (hide) { + const bool isHidingLastSection = (stretchLastSection() && logicalIndex == d->lastSectionLogicalIdx); + if (isHidingLastSection) + d->restoreSizeOnPrevLastSection(); // Restore here/now to get the right restore size. int size = d->headerSectionSize(visual); if (!d->hasAutoResizeSections()) resizeSection(logicalIndex, 0); d->hiddenSectionSize.insert(logicalIndex, size); d->setVisualIndexHidden(visual, true); + if (isHidingLastSection) + d->setNewLastSection(d->lastVisibleVisualIndex()); if (d->hasAutoResizeSections()) d->doDelayedResizeSections(); } else { @@ -1012,6 +1026,12 @@ void QHeaderView::setSectionHidden(int logicalIndex, bool hide) d->hiddenSectionSize.remove(logicalIndex); d->setVisualIndexHidden(visual, false); resizeSection(logicalIndex, size); + + const bool newLastSection = (stretchLastSection() && visual > visualIndex(d->lastSectionLogicalIdx)); + if (newLastSection) { + d->restoreSizeOnPrevLastSection(); + d->setNewLastSection(visual); + } } } @@ -1473,13 +1493,17 @@ bool QHeaderView::stretchLastSection() const void QHeaderView::setStretchLastSection(bool stretch) { Q_D(QHeaderView); + const bool changedStretchMode = (d->stretchLastSection != stretch); d->stretchLastSection = stretch; if (d->state != QHeaderViewPrivate::NoState) return; - if (stretch) + if (stretch) { + d->setNewLastSection(d->lastVisibleVisualIndex()); resizeSections(); - else if (count()) - resizeSection(count() - 1, d->defaultSectionSize); + } else { + if (changedStretchMode) + d->restoreSizeOnPrevLastSection(); + } } /*! @@ -1834,6 +1858,21 @@ void QHeaderView::sectionsInserted(const QModelIndex &parent, int insertAt = logicalFirst; int insertCount = logicalLast - logicalFirst + 1; + bool lastSectionActualChange = false; + if (stretchLastSection()) { + + int visualIndexForStretch = d->lastSectionLogicalIdx; + if (d->lastSectionLogicalIdx >= 0 && d->lastSectionLogicalIdx < d->visualIndices.size()) + visualIndexForStretch = d->visualIndices[d->lastSectionLogicalIdx]; // We cannot call visualIndex since it executes executePostedLayout() + // and it is likely to bypass initializeSections() and we may end up here again. Doing the insert twice. + + if (d->lastSectionLogicalIdx < 0 || insertAt >= visualIndexForStretch) + lastSectionActualChange = true; + + if (d->lastSectionLogicalIdx >= logicalFirst) + d->lastSectionLogicalIdx += insertCount; // We do not want to emit resize before we have fixed the count + } + QHeaderViewPrivate::SectionItem section(d->defaultSectionSize, d->globalResizeMode); d->sectionStartposRecalc = true; @@ -1890,6 +1929,9 @@ void QHeaderView::sectionsInserted(const QModelIndex &parent, d->doDelayedResizeSections(); emit sectionCountChanged(oldCount, count()); + if (lastSectionActualChange) + d->maybeRestorePrevLastSectionAndStretchLast(); + // if the new sections were not updated by resizing, we need to update now if (!d->hasAutoResizeSections()) d->viewport->update(); @@ -2000,6 +2042,16 @@ void QHeaderViewPrivate::_q_sectionsRemoved(const QModelIndex &parent, clear(); invalidateCachedSizeHint(); emit q->sectionCountChanged(oldCount, q->count()); + + if (q->stretchLastSection()) { + const bool lastSectionRemoved = lastSectionLogicalIdx >= logicalFirst && lastSectionLogicalIdx <= logicalLast; + if (lastSectionRemoved) + setNewLastSection(lastVisibleVisualIndex()); + else + lastSectionLogicalIdx = logicalIndex(lastVisibleVisualIndex()); // Just update the last log index. + doDelayedResizeSections(); + } + viewport->update(); } @@ -2078,8 +2130,8 @@ void QHeaderView::initializeSections() } else if (newCount != oldCount) { const int min = qBound(0, oldCount, newCount - 1); initializeSections(min, newCount - 1); - if (stretchLastSection()) // we've already gotten the size hint - d->lastSectionSize = sectionSize(logicalIndex(d->sectionCount() - 1)); + if (stretchLastSection()) // we've already gotten the size hint + d->maybeRestorePrevLastSectionAndStretchLast(); //make sure we update the hidden sections if (newCount < oldCount) @@ -2960,8 +3012,7 @@ QRegion QHeaderView::visualRegionForSelection(const QItemSelection &selection) c int right = 0; int rangeLeft, rangeRight; - for (int i = 0; i < selection.count(); ++i) { - QItemSelectionRange r = selection.at(i); + for (const auto &r : selection) { if (r.parent().isValid() || !r.isValid()) continue; // we only know about toplevel items and we don't want invalid ranges // FIXME an item inside the range may be the leftmost or rightmost @@ -2994,8 +3045,7 @@ QRegion QHeaderView::visualRegionForSelection(const QItemSelection &selection) c int bottom = 0; int rangeTop, rangeBottom; - for (int i = 0; i < selection.count(); ++i) { - QItemSelectionRange r = selection.at(i); + for (const auto &r : selection) { if (r.parent().isValid() || !r.isValid()) continue; // we only know about toplevel items // FIXME an item inside the range may be the leftmost or rightmost @@ -3173,6 +3223,42 @@ int QHeaderViewPrivate::lastVisibleVisualIndex() const return -1; } +void QHeaderViewPrivate::restoreSizeOnPrevLastSection() +{ + Q_Q(QHeaderView); + if (lastSectionLogicalIdx < 0) + return; + int resizeLogIdx = lastSectionLogicalIdx; + lastSectionLogicalIdx = -1; // We do not want resize to catch it as the last section. + q->resizeSection(resizeLogIdx, lastSectionSize); +} + +void QHeaderViewPrivate::setNewLastSection(int visualIndexForLastSection) +{ + Q_Q(QHeaderView); + lastSectionSize = -1; + lastSectionLogicalIdx = q->logicalIndex(visualIndexForLastSection); + lastSectionSize = headerSectionSize(visualIndexForLastSection); // pick size directly since ... + // q->sectionSize(lastSectionLogicalIdx) may do delayed resize and stretch it before we get the value. +} + +void QHeaderViewPrivate::maybeRestorePrevLastSectionAndStretchLast() +{ + Q_Q(const QHeaderView); + if (!q->stretchLastSection()) + return; + + int nowLastVisualSection = lastVisibleVisualIndex(); + if (lastSectionLogicalIdx == q->logicalIndex(nowLastVisualSection)) + return; + + // restore old last section. + restoreSizeOnPrevLastSection(); + setNewLastSection(nowLastVisualSection); + doDelayedResizeSections(); // Do stretch of last section soon (but not now). +} + + /*! \internal Go through and resize all of the sections applying stretchLastSection, @@ -3201,13 +3287,12 @@ void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool resizeRecursionBlock = true; invalidateCachedSizeHint(); - - const int lastVisibleSection = lastVisibleVisualIndex(); + const int lastSectionVisualIdx = q->visualIndex(lastSectionLogicalIdx); // find stretchLastSection if we have it int stretchSection = -1; if (stretchLastSection && !useGlobalMode) - stretchSection = lastVisibleVisualIndex(); + stretchSection = lastSectionVisualIdx; // count up the number of stretched sections and how much space left for them int lengthToStretch = (orientation == Qt::Horizontal ? viewport->width() : viewport->height()); @@ -3276,7 +3361,7 @@ void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool ? QHeaderView::Stretch : newSectionResizeMode); if (resizeMode == QHeaderView::Stretch && stretchSectionLength != -1) { - if (i == lastVisibleSection) + if (i == lastSectionVisualIdx) newSectionLength = qMax(stretchSectionLength, lastSectionSize); else newSectionLength = stretchSectionLength; @@ -3665,10 +3750,12 @@ void QHeaderViewPrivate::write(QDataStream &out) const out << sectionItems; out << resizeContentsPrecision; out << customDefaultSectionSize; + out << lastSectionSize; } bool QHeaderViewPrivate::read(QDataStream &in) { + Q_Q(QHeaderView); int orient, order, align, global; int sortIndicatorSectionIn; bool sortIndicatorShownIn; @@ -3687,7 +3774,6 @@ bool QHeaderViewPrivate::read(QDataStream &in) int minimumSectionSizeIn; QVector<SectionItem> sectionItemsIn; - in >> orient; in >> order; @@ -3780,6 +3866,18 @@ bool QHeaderViewPrivate::read(QDataStream &in) updateDefaultSectionSizeFromStyle(); } + lastSectionSize = -1; + int inLastSectionSize; + in >> inLastSectionSize; + if (in.status() == QDataStream::Ok) + lastSectionSize = inLastSectionSize; + + lastSectionLogicalIdx = -1; + if (stretchLastSection) { + lastSectionLogicalIdx = q->logicalIndex(lastVisibleVisualIndex()); + doDelayedResizeSections(); + } + return true; } diff --git a/src/widgets/itemviews/qheaderview_p.h b/src/widgets/itemviews/qheaderview_p.h index 7f92d2a81b..d1982d4777 100644 --- a/src/widgets/itemviews/qheaderview_p.h +++ b/src/widgets/itemviews/qheaderview_p.h @@ -90,6 +90,7 @@ public: minimumSectionSize(-1), maximumSectionSize(-1), lastSectionSize(0), + lastSectionLogicalIdx(-1), // Only trust when we stretch last section sectionIndicatorOffset(0), sectionIndicator(0), globalResizeMode(QHeaderView::Interactive), @@ -99,6 +100,9 @@ public: int lastVisibleVisualIndex() const; + void restoreSizeOnPrevLastSection(); + void setNewLastSection(int visualIndexForLastSection); + void maybeRestorePrevLastSectionAndStretchLast(); int sectionHandleAt(int position); void setupSectionIndicator(int section, int position); void updateSectionIndicator(int section, int position); @@ -281,7 +285,8 @@ public: int defaultSectionSize; int minimumSectionSize; int maximumSectionSize; - int lastSectionSize; // $$$ + int lastSectionSize; + int lastSectionLogicalIdx; // Only trust if we stretch LastSection int sectionIndicatorOffset; Qt::Alignment defaultAlignment; QLabel *sectionIndicator; @@ -327,8 +332,8 @@ public: inline int headerLength() const { // for debugging int len = 0; - for (int i = 0; i < sectionItems.count(); ++i) - len += sectionItems.at(i).size; + for (const auto §ion : sectionItems) + len += section.size; return len; } diff --git a/src/widgets/itemviews/qitemeditorfactory.cpp b/src/widgets/itemviews/qitemeditorfactory.cpp index e966c83fe7..3287d5dc99 100644 --- a/src/widgets/itemviews/qitemeditorfactory.cpp +++ b/src/widgets/itemviews/qitemeditorfactory.cpp @@ -47,6 +47,7 @@ #include <qapplication.h> #include <qdebug.h> +#include <algorithm> QT_BEGIN_NAMESPACE @@ -197,12 +198,12 @@ QItemEditorFactory::~QItemEditorFactory() */ void QItemEditorFactory::registerEditor(int userType, QItemEditorCreatorBase *creator) { - QHash<int, QItemEditorCreatorBase *>::iterator it = creatorMap.find(userType); - if (it != creatorMap.end()) { + const auto it = creatorMap.constFind(userType); + if (it != creatorMap.cend()) { QItemEditorCreatorBase *oldCreator = it.value(); Q_ASSERT(oldCreator); creatorMap.erase(it); - if (!creatorMap.values().contains(oldCreator)) + if (std::find(creatorMap.cbegin(), creatorMap.cend(), oldCreator) == creatorMap.cend()) delete oldCreator; // if it is no more in use we can delete it } diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp index 9c79509874..6e432a9ad7 100644 --- a/src/widgets/itemviews/qlistview.cpp +++ b/src/widgets/itemviews/qlistview.cpp @@ -46,11 +46,14 @@ #include <qscrollbar.h> #include <qrubberband.h> #include <private/qlistview_p.h> +#include <private/qscrollbar_p.h> #include <qdebug.h> #ifndef QT_NO_ACCESSIBILITY #include <qaccessible.h> #endif +#include <algorithm> + QT_BEGIN_NAMESPACE extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event); @@ -392,7 +395,7 @@ int QListView::spacing() const void QListView::setBatchSize(int batchSize) { Q_D(QListView); - if (batchSize <= 0) { + if (Q_UNLIKELY(batchSize <= 0)) { qWarning("Invalid batchSize (%d)", batchSize); return; } @@ -649,12 +652,13 @@ QItemViewPaintPairs QListViewPrivate::draggablePaintPairs(const QModelIndexList QRect &rect = *r; const QRect viewportRect = viewport->rect(); QItemViewPaintPairs ret; - const QSet<QModelIndex> visibleIndexes = intersectingSet(viewportRect.translated(q->horizontalOffset(), q->verticalOffset())).toList().toSet(); - for (int i = 0; i < indexes.count(); ++i) { - const QModelIndex &index = indexes.at(i); - if (visibleIndexes.contains(index)) { + QVector<QModelIndex> visibleIndexes = intersectingSet(viewportRect.translated(q->horizontalOffset(), q->verticalOffset())); + std::sort(visibleIndexes.begin(), visibleIndexes.end()); + for (const auto &index : indexes) { + if (std::binary_search(visibleIndexes.cbegin(), visibleIndexes.cend(), index)) { const QRect current = q->visualRect(index); - ret += qMakePair(current, index); + QItemViewPaintPair p = { current, index }; + ret += p; rect |= current; } } @@ -1396,16 +1400,16 @@ QRegion QListView::visualRegionForSelection(const QItemSelection &selection) con int c = d->column; QRegion selectionRegion; const QRect &viewportRect = d->viewport->rect(); - for (int i = 0; i < selection.count(); ++i) { - if (!selection.at(i).isValid()) + for (const auto &elem : selection) { + if (!elem.isValid()) continue; - QModelIndex parent = selection.at(i).topLeft().parent(); + QModelIndex parent = elem.topLeft().parent(); //we only display the children of the root in a listview //we're not interested in the other model indexes if (parent != d->root) continue; - int t = selection.at(i).topLeft().row(); - int b = selection.at(i).bottomRight().row(); + int t = elem.topLeft().row(); + int b = elem.bottomRight().row(); if (d->viewMode == IconMode || d->isWrapping()) { // in non-static mode, we have to go through all selected items for (int r = t; r <= b; ++r) { const QRect &rect = visualRect(d->model->index(r, c, parent)); @@ -1842,6 +1846,16 @@ bool QListViewPrivate::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QMo } #endif +void QListViewPrivate::removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex ¤t) const +{ + auto isCurrentOrDisabled = [=](const QModelIndex &index) { + return !isIndexEnabled(index) || index == current; + }; + indexes->erase(std::remove_if(indexes->begin(), indexes->end(), + isCurrentOrDisabled), + indexes->end()); +} + /* * Common ListView Implementation */ @@ -1867,7 +1881,7 @@ void QCommonListViewBase::paintDragDrop(QPainter *painter) void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step) { - horizontalScrollBar()->setSingleStep(step.width() + spacing()); + horizontalScrollBar()->d_func()->itemviewChangeSingleStep(step.width() + spacing()); horizontalScrollBar()->setPageStep(viewport()->width()); // If both scroll bars are set to auto, we might end up in a situation with enough space @@ -1897,7 +1911,7 @@ void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step) void QCommonListViewBase::updateVerticalScrollBar(const QSize &step) { - verticalScrollBar()->setSingleStep(step.height() + spacing()); + verticalScrollBar()->d_func()->itemviewChangeSingleStep(step.height() + spacing()); verticalScrollBar()->setPageStep(viewport()->height()); // If both scroll bars are set to auto, we might end up in a situation with enough space @@ -2769,9 +2783,8 @@ bool QIconModeViewBase::filterDropEvent(QDropEvent *e) } QPoint start = dd->pressedPosition; QPoint delta = (dd->movement == QListView::Snap ? snapToGrid(end) - snapToGrid(start) : end - start); - QList<QModelIndex> indexes = dd->selectionModel->selectedIndexes(); - for (int i = 0; i < indexes.count(); ++i) { - QModelIndex index = indexes.at(i); + const QList<QModelIndex> indexes = dd->selectionModel->selectedIndexes(); + for (const auto &index : indexes) { QRect rect = dd->rectForIndex(index); viewport()->update(dd->mapToViewport(rect, false)); QPoint dest = rect.topLeft() + delta; diff --git a/src/widgets/itemviews/qlistview_p.h b/src/widgets/itemviews/qlistview_p.h index 62fa45e640..2aa34256d2 100644 --- a/src/widgets/itemviews/qlistview_p.h +++ b/src/widgets/itemviews/qlistview_p.h @@ -64,9 +64,6 @@ class QListViewItem public: inline QListViewItem() : x(-1), y(-1), w(0), h(0), indexHint(-1), visited(0xffff) {} - inline QListViewItem(const QListViewItem &other) - : x(other.x), y(other.y), w(other.w), h(other.h), - indexHint(other.indexHint), visited(other.visited) {} inline QListViewItem(QRect r, int i) : x(r.x()), y(r.y()), w(qMin(r.width(), SHRT_MAX)), h(qMin(r.height(), SHRT_MAX)), indexHint(i), visited(0xffff) {} @@ -375,15 +372,7 @@ public: } inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(modelIndex(row)); } - inline void removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex ¤t) const { - QVector<QModelIndex>::iterator it = indexes->begin(); - while (it != indexes->end()) { - if (!isIndexEnabled(*it) || (*it) == current) - indexes->erase(it); - else - ++it; - } - } + void removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex ¤t) const; void scrollElasticBandBy(int dx, int dy); diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index d4d22c8bef..275fc2a1f1 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -1908,7 +1908,7 @@ QList<QListWidgetItem*> QListWidget::items(const QMimeData *data) const } /*! - Returns the QModelIndex assocated with the given \a item. + Returns the QModelIndex associated with the given \a item. */ QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const @@ -1918,7 +1918,7 @@ QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const } /*! - Returns a pointer to the QListWidgetItem assocated with the given \a index. + Returns a pointer to the QListWidgetItem associated with the given \a index. */ QListWidgetItem *QListWidget::itemFromIndex(const QModelIndex &index) const diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp index ee0d41ce15..88090f1e45 100644 --- a/src/widgets/itemviews/qtableview.cpp +++ b/src/widgets/itemviews/qtableview.cpp @@ -46,6 +46,7 @@ #include <qabstractbutton.h> #include <private/qtableview_p.h> #include <private/qheaderview_p.h> +#include <private/qscrollbar_p.h> #ifndef QT_NO_ACCESSIBILITY #include <qaccessible.h> #endif @@ -661,7 +662,7 @@ void QTableViewPrivate::trimHiddenSelections(QItemSelectionRange *range) const */ void QTableViewPrivate::setSpan(int row, int column, int rowSpan, int columnSpan) { - if (row < 0 || column < 0 || rowSpan <= 0 || columnSpan <= 0) { + if (Q_UNLIKELY(row < 0 || column < 0 || rowSpan <= 0 || columnSpan <= 0)) { qWarning("QTableView::setSpan: invalid span given: (%d, %d, %d, %d)", row, column, rowSpan, columnSpan); return; @@ -680,7 +681,7 @@ void QTableViewPrivate::setSpan(int row, int column, int rowSpan, int columnSpan sp->m_right = column + columnSpan - 1; spans.updateSpan(sp, old_height); return; - } else if (rowSpan == 1 && columnSpan == 1) { + } else if (Q_UNLIKELY(rowSpan == 1 && columnSpan == 1)) { qWarning("QTableView::setSpan: single cell span won't be added"); return; } @@ -1369,9 +1370,6 @@ void QTableView::paintEvent(QPaintEvent *event) uint x = horizontalHeader->length() - horizontalHeader->offset() - (rightToLeft ? 0 : 1); uint y = verticalHeader->length() - verticalHeader->offset() - 1; - const QRegion region = event->region().translated(offset); - const QVector<QRect> rects = region.rects(); - //firstVisualRow is the visual index of the first visible row. lastVisualRow is the visual index of the last visible Row. //same goes for ...VisualColumn int firstVisualRow = qMax(verticalHeader->visualIndexAt(0),0); @@ -1390,13 +1388,15 @@ void QTableView::paintEvent(QPaintEvent *event) QBitArray drawn((lastVisualRow - firstVisualRow + 1) * (lastVisualColumn - firstVisualColumn + 1)); + const QRegion region = event->region().translated(offset); + if (d->hasSpans()) { d->drawAndClipSpans(region, &painter, option, &drawn, firstVisualRow, lastVisualRow, firstVisualColumn, lastVisualColumn); } - for (int i = 0; i < rects.size(); ++i) { - QRect dirtyArea = rects.at(i); + const QVector<QRect> rects = region.rects(); + for (auto dirtyArea : rects) { dirtyArea.setBottom(qMin(dirtyArea.bottom(), int(y))); if (rightToLeft) { dirtyArea.setLeft(qMax(dirtyArea.left(), d->viewport->width() - int(x))); @@ -1951,8 +1951,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co bool horizontalMoved = horizontalHeader()->sectionsMoved(); if ((verticalMoved && horizontalMoved) || (d->hasSpans() && (verticalMoved || horizontalMoved))) { - for (int i = 0; i < selection.count(); ++i) { - QItemSelectionRange range = selection.at(i); + for (const auto &range : selection) { if (range.parent() != d->root || !range.isValid()) continue; for (int r = range.top(); r <= range.bottom(); ++r) @@ -1963,8 +1962,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co } } } else if (horizontalMoved) { - for (int i = 0; i < selection.count(); ++i) { - QItemSelectionRange range = selection.at(i); + for (const auto &range : selection) { if (range.parent() != d->root || !range.isValid()) continue; int top = rowViewportPosition(range.top()); @@ -1979,8 +1977,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co } } } else if (verticalMoved) { - for (int i = 0; i < selection.count(); ++i) { - QItemSelectionRange range = selection.at(i); + for (const auto &range : selection) { if (range.parent() != d->root || !range.isValid()) continue; int left = columnViewportPosition(range.left()); @@ -1996,8 +1993,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co } } else { // nothing moved const int gridAdjust = showGrid() ? 1 : 0; - for (int i = 0; i < selection.count(); ++i) { - QItemSelectionRange range = selection.at(i); + for (auto range : selection) { if (range.parent() != d->root || !range.isValid()) continue; d->trimHiddenSelections(&range); @@ -2166,7 +2162,7 @@ void QTableView::updateGeometries() } else { // ScrollPerPixel horizontalScrollBar()->setPageStep(vsize.width()); horizontalScrollBar()->setRange(0, horizontalLength - vsize.width()); - horizontalScrollBar()->setSingleStep(qMax(vsize.width() / (columnsInViewport + 1), 2)); + horizontalScrollBar()->d_func()->itemviewChangeSingleStep(qMax(vsize.width() / (columnsInViewport + 1), 2)); } // vertical scroll bar @@ -2194,7 +2190,7 @@ void QTableView::updateGeometries() } else { // ScrollPerPixel verticalScrollBar()->setPageStep(vsize.height()); verticalScrollBar()->setRange(0, verticalLength - vsize.height()); - verticalScrollBar()->setSingleStep(qMax(vsize.height() / (rowsInViewport + 1), 2)); + verticalScrollBar()->d_func()->itemviewChangeSingleStep(qMax(vsize.height() / (rowsInViewport + 1), 2)); } d->geometryRecursionBlock = false; diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp index cd38f4b282..6238835f04 100644 --- a/src/widgets/itemviews/qtablewidget.cpp +++ b/src/widgets/itemviews/qtablewidget.cpp @@ -1388,9 +1388,10 @@ void QTableWidgetItem::setData(int role, const QVariant &value) QVariant QTableWidgetItem::data(int role) const { role = (role == Qt::EditRole ? Qt::DisplayRole : role); - for (int i = 0; i < values.count(); ++i) - if (values.at(i).role == role) - return values.at(i).value; + for (const auto &value : values) { + if (value.role == role) + return value.value; + } return QVariant(); } @@ -1958,7 +1959,7 @@ void QTableWidget::setItem(int row, int column, QTableWidgetItem *item) { Q_D(QTableWidget); if (item) { - if (item->view != 0) { + if (Q_UNLIKELY(item->view)) { qWarning("QTableWidget: cannot insert an item that is already owned by another QTableWidget"); } else { item->view = this; @@ -2359,10 +2360,9 @@ QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const QList<QTableWidgetItem*> QTableWidget::selectedItems() const { Q_D(const QTableWidget); - QModelIndexList indexes = selectionModel()->selectedIndexes(); + const QModelIndexList indexes = selectionModel()->selectedIndexes(); QList<QTableWidgetItem*> items; - for (int i = 0; i < indexes.count(); ++i) { - QModelIndex index = indexes.at(i); + for (const auto &index : indexes) { if (isIndexHidden(index)) continue; QTableWidgetItem *item = d->tableModel()->item(index); @@ -2639,7 +2639,7 @@ QList<QTableWidgetItem*> QTableWidget::items(const QMimeData *data) const } /*! - Returns the QModelIndex assocated with the given \a item. + Returns the QModelIndex associated with the given \a item. */ QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const @@ -2649,7 +2649,7 @@ QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const } /*! - Returns a pointer to the QTableWidgetItem assocated with the given \a index. + Returns a pointer to the QTableWidgetItem associated with the given \a index. */ QTableWidgetItem *QTableWidget::itemFromIndex(const QModelIndex &index) const @@ -2682,22 +2682,21 @@ void QTableWidget::dropEvent(QDropEvent *event) { int col = -1; int row = -1; if (d->dropOn(event, &row, &col, &topIndex)) { - QModelIndexList indexes = selectedIndexes(); + const QModelIndexList indexes = selectedIndexes(); int top = INT_MAX; int left = INT_MAX; - for (int i = 0; i < indexes.count(); ++i) { - top = qMin(indexes.at(i).row(), top); - left = qMin(indexes.at(i).column(), left); + for (const auto &index : indexes) { + top = qMin(index.row(), top); + left = qMin(index.column(), left); } QList<QTableWidgetItem *> taken; const int indexesCount = indexes.count(); taken.reserve(indexesCount); - for (int i = 0; i < indexesCount; ++i) - taken.append(takeItem(indexes.at(i).row(), indexes.at(i).column())); + for (const auto &index : indexes) + taken.append(takeItem(index.row(), index.column())); - for (int i = 0; i < indexes.count(); ++i) { - QModelIndex index = indexes.at(i); + for (const auto &index : indexes) { int r = index.row() - top + topIndex.row(); int c = index.column() - left + topIndex.column(); setItem(r, c, taken.takeFirst()); diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp index 57092a7cdc..b11c54ffb8 100644 --- a/src/widgets/itemviews/qtreeview.cpp +++ b/src/widgets/itemviews/qtreeview.cpp @@ -45,6 +45,7 @@ #include <qpen.h> #include <qdebug.h> #include <QMetaMethod> +#include <private/qscrollbar_p.h> #ifndef QT_NO_ACCESSIBILITY #include <qaccessible.h> #endif @@ -2392,8 +2393,7 @@ QRegion QTreeView::visualRegionForSelection(const QItemSelection &selection) con QRegion selectionRegion; const QRect &viewportRect = d->viewport->rect(); - for (int i = 0; i < selection.count(); ++i) { - QItemSelectionRange range = selection.at(i); + for (const auto &range : selection) { if (!range.isValid()) continue; QModelIndex parent = range.parent(); @@ -3698,7 +3698,7 @@ void QTreeViewPrivate::updateScrollBars() } vbar->setRange(0, contentsHeight - viewportSize.height()); vbar->setPageStep(viewportSize.height()); - vbar->setSingleStep(qMax(viewportSize.height() / (itemsInViewport + 1), 2)); + vbar->d_func()->itemviewChangeSingleStep(qMax(viewportSize.height() / (itemsInViewport + 1), 2)); } const int columnCount = header->count(); @@ -3724,7 +3724,7 @@ void QTreeViewPrivate::updateScrollBars() viewportSize = maxSize; hbar->setPageStep(viewportSize.width()); hbar->setRange(0, qMax(horizontalLength - viewportSize.width(), 0)); - hbar->setSingleStep(qMax(viewportSize.width() / (columnsInViewport + 1), 2)); + hbar->d_func()->itemviewChangeSingleStep(qMax(viewportSize.width() / (columnsInViewport + 1), 2)); } } diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp index 676893ebf0..9dbf7df5fb 100644 --- a/src/widgets/itemviews/qtreewidget.cpp +++ b/src/widgets/itemviews/qtreewidget.cpp @@ -725,9 +725,9 @@ QMimeData *QTreeModel::internalMimeData() const QMimeData *QTreeModel::mimeData(const QModelIndexList &indexes) const { QList<QTreeWidgetItem*> items; - for (int i = 0; i < indexes.count(); ++i) { - if (indexes.at(i).column() == 0) // only one item per row - items << item(indexes.at(i)); + for (const auto &index : indexes) { + if (index.column() == 0) // only one item per row + items << item(index); } // cachedIndexes is a little hack to avoid copying from QModelIndexList to @@ -1735,7 +1735,7 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value) default: if (column < values.count()) { bool found = false; - QVector<QWidgetItemData> column_values = values.at(column); + const QVector<QWidgetItemData> column_values = values.at(column); for (int i = 0; i < column_values.count(); ++i) { if (column_values.at(i).role == role) { if (column_values.at(i).value == value) @@ -1785,9 +1785,10 @@ QVariant QTreeWidgetItem::data(int column, int role) const default: if (column >= 0 && column < values.size()) { const QVector<QWidgetItemData> &column_values = values.at(column); - for (int i = 0; i < column_values.count(); ++i) - if (column_values.at(i).role == role) - return column_values.at(i).value; + for (const auto &column_value : column_values) { + if (column_value.role == role) + return column_value.value; + } } } return QVariant(); @@ -2136,8 +2137,8 @@ QVariant QTreeWidgetItem::childrenCheckState(int column) const return QVariant(); bool checkedChildren = false; bool uncheckedChildren = false; - for (int i = 0; i < children.count(); ++i) { - QVariant value = children.at(i)->data(column, Qt::CheckStateRole); + for (const auto *child : children) { + QVariant value = child->data(column, Qt::CheckStateRole); if (!value.isValid()) return QVariant(); @@ -3018,13 +3019,13 @@ void QTreeWidget::setItemSelected(const QTreeWidgetItem *item, bool select) QList<QTreeWidgetItem*> QTreeWidget::selectedItems() const { Q_D(const QTreeWidget); - QModelIndexList indexes = selectionModel()->selectedIndexes(); + const QModelIndexList indexes = selectionModel()->selectedIndexes(); QList<QTreeWidgetItem*> items; items.reserve(indexes.count()); QSet<QTreeWidgetItem *> seen; seen.reserve(indexes.count()); - for (int i = 0; i < indexes.count(); ++i) { - QTreeWidgetItem *item = d->item(indexes.at(i)); + for (const auto &index : indexes) { + QTreeWidgetItem *item = d->item(index); if (isItemHidden(item) || seen.contains(item)) continue; seen.insert(item); @@ -3278,16 +3279,15 @@ QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const Q_D(const QTreeWidget); if (d->treeModel()->cachedIndexes.isEmpty()) { QList<QModelIndex> indexes; - for (int i = 0; i < items.count(); ++i) { - QTreeWidgetItem *item = items.at(i); - if (!item) { + for (const auto *item : items) { + if (Q_UNLIKELY(!item)) { qWarning("QTreeWidget::mimeData: Null-item passed"); return 0; } for (int c = 0; c < item->values.count(); ++c) { const QModelIndex index = indexFromItem(item, c); - if (!index.isValid()) { + if (Q_UNLIKELY(!index.isValid())) { qWarning() << "QTreeWidget::mimeData: No index associated with item :" << item; return 0; } @@ -3340,18 +3340,29 @@ QList<QTreeWidgetItem*> QTreeWidget::items(const QMimeData *data) const } /*! - Returns the QModelIndex assocated with the given \a item in the given \a column. + Returns the QModelIndex associated with the given \a item in the given \a column. + + \note In Qt versions prior to 5.7, this function took a non-\c{const} \a item. \sa itemFromIndex(), topLevelItem() */ -QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem *item, int column) const +QModelIndex QTreeWidget::indexFromItem(const QTreeWidgetItem *item, int column) const { Q_D(const QTreeWidget); return d->index(item, column); } /*! - Returns a pointer to the QTreeWidgetItem assocated with the given \a index. + \overload + \internal +*/ +QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem *item, int column) const +{ + return indexFromItem(const_cast<const QTreeWidgetItem *>(item), column); +} + +/*! + Returns a pointer to the QTreeWidgetItem associated with the given \a index. \sa indexFromItem() */ @@ -3371,12 +3382,12 @@ void QTreeWidget::dropEvent(QDropEvent *event) { int col = -1; int row = -1; if (d->dropOn(event, &row, &col, &topIndex)) { - QList<QModelIndex> idxs = selectedIndexes(); + const QList<QModelIndex> idxs = selectedIndexes(); QList<QPersistentModelIndex> indexes; const int indexesCount = idxs.count(); indexes.reserve(indexesCount); - for (int i = 0; i < indexesCount; i++) - indexes.append(idxs.at(i)); + for (const auto &idx : idxs) + indexes.append(idx); if (indexes.contains(topIndex)) return; @@ -3386,12 +3397,12 @@ void QTreeWidget::dropEvent(QDropEvent *event) { // Remove the items QList<QTreeWidgetItem *> taken; - for (int i = 0; i < indexes.count(); ++i) { - QTreeWidgetItem *parent = itemFromIndex(indexes.at(i)); + for (const auto &index : indexes) { + QTreeWidgetItem *parent = itemFromIndex(index); if (!parent || !parent->parent()) { - taken.append(takeTopLevelItem(indexes.at(i).row())); + taken.append(takeTopLevelItem(index.row())); } else { - taken.append(parent->parent()->takeChild(indexes.at(i).row())); + taken.append(parent->parent()->takeChild(index.row())); } } diff --git a/src/widgets/itemviews/qtreewidget.h b/src/widgets/itemviews/qtreewidget.h index 995528fe37..e39593c6c1 100644 --- a/src/widgets/itemviews/qtreewidget.h +++ b/src/widgets/itemviews/qtreewidget.h @@ -350,7 +350,8 @@ protected: virtual Qt::DropActions supportedDropActions() const; QList<QTreeWidgetItem*> items(const QMimeData *data) const; - QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; + QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const; + QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; // ### Qt 6: remove QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const; void dropEvent(QDropEvent *event) Q_DECL_OVERRIDE; diff --git a/src/widgets/itemviews/qwidgetitemdata_p.h b/src/widgets/itemviews/qwidgetitemdata_p.h index d8c1fc2ff7..82427b338a 100644 --- a/src/widgets/itemviews/qwidgetitemdata_p.h +++ b/src/widgets/itemviews/qwidgetitemdata_p.h @@ -53,7 +53,7 @@ class QWidgetItemData { public: inline QWidgetItemData() : role(-1) {} - inline QWidgetItemData(int r, QVariant v) : role(r), value(v) {} + inline QWidgetItemData(int r, const QVariant &v) : role(r), value(v) {} int role; QVariant value; inline bool operator==(const QWidgetItemData &other) const { return role == other.role && value == other.value; } |