diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-12-30 02:04:26 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-02-26 18:56:43 +0000 |
commit | 1d8030cc64df2cdfdc0faf3d06ea7d9ed0306948 (patch) | |
tree | 86e7246682e6bb4345a1fd5e5f54352a5eea592e /src/widgets/util | |
parent | bc5917a10a16dd33b8c12c61db981f0328882049 (diff) |
QtWidgets: prevent detach attempts from first()/etc. use [dialogs, kernel, util, widgets]
The algorithm used was:
- If possible, just declare the container const
- Otherwise, for first()/last(), use constFirst()/constLast()
and for front()/back(), to not destroy the use of the STL
API subset, use qAsConst()
Did some caching of function returns here and there, and
converted one 0 to nullptr as a drive-by.
Also saves almost 4KiB in text size on optimized GCC 4.9
Linux AMD64 builds.
Change-Id: I04b7bfd68dc85c22de247cb65a310e1cbbca1e8c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'src/widgets/util')
-rw-r--r-- | src/widgets/util/qcompleter.cpp | 4 | ||||
-rw-r--r-- | src/widgets/util/qscroller.cpp | 27 | ||||
-rw-r--r-- | src/widgets/util/qundostack.cpp | 8 |
3 files changed, 24 insertions, 15 deletions
diff --git a/src/widgets/util/qcompleter.cpp b/src/widgets/util/qcompleter.cpp index 9c61a3a263..9f167f5924 100644 --- a/src/widgets/util/qcompleter.cpp +++ b/src/widgets/util/qcompleter.cpp @@ -755,9 +755,9 @@ void QUnsortedModelEngine::filterOnDemand(int n) const QAbstractItemModel *model = c->proxy->sourceModel(); int lastRow = model->rowCount(curParent) - 1; QIndexMapper im(curMatch.indices.last() + 1, lastRow); - int lastIndex = buildIndices(curParts.last(), curParent, n, im, &curMatch); + int lastIndex = buildIndices(curParts.constLast(), curParent, n, im, &curMatch); curMatch.partial = (lastRow != lastIndex); - saveInCache(curParts.last(), curParent, curMatch); + saveInCache(curParts.constLast(), curParent, curMatch); } QMatchData QUnsortedModelEngine::filter(const QString& part, const QModelIndex& parent, int n) diff --git a/src/widgets/util/qscroller.cpp b/src/widgets/util/qscroller.cpp index 2d6bcf72c6..02e3c2b82a 100644 --- a/src/widgets/util/qscroller.cpp +++ b/src/widgets/util/qscroller.cpp @@ -575,8 +575,11 @@ QPointF QScroller::pixelPerMeter() const if (QGraphicsObject *go = qobject_cast<QGraphicsObject *>(d->target)) { QTransform viewtr; //TODO: the first view isn't really correct - maybe use an additional field in the prepare event? - if (go->scene() && !go->scene()->views().isEmpty()) - viewtr = go->scene()->views().first()->viewportTransform(); + if (const auto *scene = go->scene()) { + const auto views = scene->views(); + if (!views.isEmpty()) + viewtr = views.first()->viewportTransform(); + } QTransform tr = go->deviceTransform(viewtr); if (tr.isScaling()) { QPointF p0 = tr.map(QPointF(0, 0)); @@ -1121,12 +1124,15 @@ void QScrollerPrivate::pushSegment(ScrollType type, qreal deltaTime, qreal stopP return; ScrollSegment s; - if (orientation == Qt::Horizontal && !xSegments.isEmpty()) - s.startTime = xSegments.last().startTime + xSegments.last().deltaTime * xSegments.last().stopProgress; - else if (orientation == Qt::Vertical && !ySegments.isEmpty()) - s.startTime = ySegments.last().startTime + ySegments.last().deltaTime * ySegments.last().stopProgress; - else + if (orientation == Qt::Horizontal && !xSegments.isEmpty()) { + const auto &lastX = xSegments.constLast(); + s.startTime = lastX.startTime + lastX.deltaTime * lastX.stopProgress; + } else if (orientation == Qt::Vertical && !ySegments.isEmpty()) { + const auto &lastY = ySegments.constLast(); + s.startTime = lastY.startTime + lastY.deltaTime * lastY.stopProgress; + } else { s.startTime = monotonicTimer.elapsed(); + } s.startPos = startPos; s.deltaPos = deltaPos; @@ -1463,8 +1469,11 @@ bool QScrollerPrivate::prepareScrolling(const QPointF &position) #ifndef QT_NO_GRAPHICSVIEW if (QGraphicsObject *go = qobject_cast<QGraphicsObject *>(target)) { //TODO: the first view isn't really correct - maybe use an additional field in the prepare event? - if (go->scene() && !go->scene()->views().isEmpty()) - setDpiFromWidget(go->scene()->views().first()); + if (const auto *scene = go->scene()) { + const auto views = scene->views(); + if (!views.isEmpty()) + setDpiFromWidget(views.first()); + } } #endif diff --git a/src/widgets/util/qundostack.cpp b/src/widgets/util/qundostack.cpp index 6643b4429c..6f733f99d5 100644 --- a/src/widgets/util/qundostack.cpp +++ b/src/widgets/util/qundostack.cpp @@ -588,9 +588,9 @@ void QUndoStack::push(QUndoCommand *cmd) QUndoCommand *cur = 0; if (macro) { - QUndoCommand *macro_cmd = d->macro_stack.last(); + QUndoCommand *macro_cmd = d->macro_stack.constLast(); if (!macro_cmd->d->child_list.isEmpty()) - cur = macro_cmd->d->child_list.last(); + cur = macro_cmd->d->child_list.constLast(); } else { if (d->index > 0) cur = d->command_list.at(d->index - 1); @@ -616,7 +616,7 @@ void QUndoStack::push(QUndoCommand *cmd) } } else { if (macro) { - d->macro_stack.last()->d->child_list.append(cmd); + d->macro_stack.constLast()->d->child_list.append(cmd); } else { d->command_list.append(cmd); d->checkUndoLimit(); @@ -963,7 +963,7 @@ void QUndoStack::beginMacro(const QString &text) d->clean_index = -1; // we've deleted the clean state d->command_list.append(cmd); } else { - d->macro_stack.last()->d->child_list.append(cmd); + d->macro_stack.constLast()->d->child_list.append(cmd); } d->macro_stack.append(cmd); |