diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-02-21 13:25:43 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-03-06 09:19:58 +0000 |
commit | 51bec76d1041ef809aa0adeec041f14f6124b5ab (patch) | |
tree | d321bbff5e07f18a7812ae0c9066c2db31b7f2bb /src/widgets/widgets | |
parent | d5fe06a94ebbd2652034acfb64992ea4e744a1fe (diff) |
QLineEditPrivate: introduce SideWidgetLocation class
... as a replacement for QPair<enum, int>, and move some
repsonsibilities into it.
This avoids the repeated use of the magic number -1 to indicate
invalid locations and does away with the confusing .first and .second,
replacing them instead with proper names, .position and .index.
Change-Id: If904c5333cecf8ce3d5160ca4be9264a13a2b72a
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'src/widgets/widgets')
-rw-r--r-- | src/widgets/widgets/qlineedit_p.cpp | 27 | ||||
-rw-r--r-- | src/widgets/widgets/qlineedit_p.h | 11 |
2 files changed, 22 insertions, 16 deletions
diff --git a/src/widgets/widgets/qlineedit_p.cpp b/src/widgets/widgets/qlineedit_p.cpp index 791fba49aa..1d81d1fcb5 100644 --- a/src/widgets/widgets/qlineedit_p.cpp +++ b/src/widgets/widgets/qlineedit_p.cpp @@ -485,21 +485,21 @@ void QLineEditPrivate::positionSideWidgets() } } -QLineEditPrivate::PositionIndexPair QLineEditPrivate::findSideWidget(const QAction *a) const +QLineEditPrivate::SideWidgetLocation QLineEditPrivate::findSideWidget(const QAction *a) const { int i = 0; for (const auto &e : leadingSideWidgets) { if (a == e.action) - return PositionIndexPair(QLineEdit::LeadingPosition, i); + return {QLineEdit::LeadingPosition, i}; ++i; } i = 0; for (const auto &e : trailingSideWidgets) { if (a == e.action) - return PositionIndexPair(QLineEdit::TrailingPosition, i); + return {QLineEdit::TrailingPosition, i}; ++i; } - return PositionIndexPair(QLineEdit::LeadingPosition, -1); + return {QLineEdit::LeadingPosition, -1}; } QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineEdit::ActionPosition position, int flags) @@ -534,11 +534,10 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE #endif } // If there is a 'before' action, it takes preference - PositionIndexPair positionIndex = before ? findSideWidget(before) : PositionIndexPair(position, -1); - SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets; - if (positionIndex.second < 0) - positionIndex.second = int(list.size()); - list.insert(list.begin() + positionIndex.second, SideWidgetEntry(w, newAction, flags)); + const auto location = before ? findSideWidget(before) : SideWidgetLocation{position, -1}; + SideWidgetEntryList &list = location.position == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets; + list.insert(location.isValid() ? list.begin() + location.index : list.end(), + SideWidgetEntry(w, newAction, flags)); positionSideWidgets(); w->show(); return w; @@ -548,12 +547,12 @@ void QLineEditPrivate::removeAction(QAction *action) { #if QT_CONFIG(action) Q_Q(QLineEdit); - const PositionIndexPair positionIndex = findSideWidget(action); - if (positionIndex.second == -1) + const auto location = findSideWidget(action); + if (!location.isValid()) return; - SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets; - SideWidgetEntry entry = list[positionIndex.second]; - list.erase(list.begin() + positionIndex.second); + SideWidgetEntryList &list = location.position == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets; + SideWidgetEntry entry = list[location.index]; + list.erase(list.begin() + location.index); if (entry.flags & SideWidgetCreatedByWidgetAction) static_cast<QWidgetAction *>(entry.action)->releaseWidget(entry.widget); else diff --git a/src/widgets/widgets/qlineedit_p.h b/src/widgets/widgets/qlineedit_p.h index a903c003e6..2d695f8411 100644 --- a/src/widgets/widgets/qlineedit_p.h +++ b/src/widgets/widgets/qlineedit_p.h @@ -235,15 +235,22 @@ public: int effectiveRightTextMargin() const; private: - typedef QPair<QLineEdit::ActionPosition, int> PositionIndexPair; + struct SideWidgetLocation { + QLineEdit::ActionPosition position; + int index; - PositionIndexPair findSideWidget(const QAction *a) const; + bool isValid() const { return index >= 0; } + }; + friend class QTypeInfo<SideWidgetLocation>; + + SideWidgetLocation findSideWidget(const QAction *a) const; SideWidgetEntryList leadingSideWidgets; SideWidgetEntryList trailingSideWidgets; int lastTextSize; }; Q_DECLARE_TYPEINFO(QLineEditPrivate::SideWidgetEntry, Q_PRIMITIVE_TYPE); +Q_DECLARE_TYPEINFO(QLineEditPrivate::SideWidgetLocation, Q_PRIMITIVE_TYPE); #endif // QT_NO_LINEEDIT |