diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-05-26 07:22:13 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2019-07-30 13:05:11 +0300 |
commit | 7940bc32a1a4256a9ab4c032f2123c9fea8c6202 (patch) | |
tree | 1e8a31a8355efac9e3d53ff872c0ef7711ef0ae5 | |
parent | 31ffc7bf2a0efe40116764a8c84939d2df4cdeae (diff) |
QLineEdit: use QMargins internally instead of 4 x int
Replace the four ints xTextMargin with one QMargins member, and the
effectiveXTextMargin() function with effectiveTextMargins(). The
left and right effective margins were always used together, so this
in no case leads to extra work, compared to separate functions.
Change-Id: I46d061919ffac297142213ccb4033caa0288b554
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
-rw-r--r-- | src/widgets/widgets/qlineedit.cpp | 40 | ||||
-rw-r--r-- | src/widgets/widgets/qlineedit_p.cpp | 17 | ||||
-rw-r--r-- | src/widgets/widgets/qlineedit_p.h | 11 |
3 files changed, 28 insertions, 40 deletions
diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index 0fedb65f0c..28d18872ce 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -684,11 +684,12 @@ QSize QLineEdit::sizeHint() const ensurePolished(); QFontMetrics fm(font()); const int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this); + const QMargins tm = d->effectiveTextMargins(); int h = qMax(fm.height(), qMax(14, iconSize - 2)) + 2 * QLineEditPrivate::verticalMargin - + d->topTextMargin + d->bottomTextMargin + + tm.top() + tm.bottom() + d->topmargin + d->bottommargin; int w = fm.horizontalAdvance(QLatin1Char('x')) * 17 + 2 * QLineEditPrivate::horizontalMargin - + d->effectiveLeftTextMargin() + d->effectiveRightTextMargin() + + tm.left() + tm.right() + d->leftmargin + d->rightmargin; // "some" QStyleOptionFrame opt; initStyleOption(&opt); @@ -708,11 +709,12 @@ QSize QLineEdit::minimumSizeHint() const Q_D(const QLineEdit); ensurePolished(); QFontMetrics fm = fontMetrics(); + const QMargins tm = d->effectiveTextMargins(); int h = fm.height() + qMax(2 * QLineEditPrivate::verticalMargin, fm.leading()) - + d->topTextMargin + d->bottomTextMargin + + tm.top() + tm.bottom() + d->topmargin + d->bottommargin; int w = fm.maxWidth() - + d->effectiveLeftTextMargin() + d->effectiveRightTextMargin() + + tm.left() + tm.right() + d->leftmargin + d->rightmargin; QStyleOptionFrame opt; initStyleOption(&opt); @@ -1131,13 +1133,7 @@ bool QLineEdit::hasAcceptableInput() const */ void QLineEdit::setTextMargins(int left, int top, int right, int bottom) { - Q_D(QLineEdit); - d->leftTextMargin = left; - d->topTextMargin = top; - d->rightTextMargin = right; - d->bottomTextMargin = bottom; - updateGeometry(); - update(); + setTextMargins({left, top, right, bottom}); } /*! @@ -1148,7 +1144,10 @@ void QLineEdit::setTextMargins(int left, int top, int right, int bottom) */ void QLineEdit::setTextMargins(const QMargins &margins) { - setTextMargins(margins.left(), margins.top(), margins.right(), margins.bottom()); + Q_D(QLineEdit); + d->textMargins = margins; + updateGeometry(); + update(); } /*! @@ -1159,15 +1158,15 @@ void QLineEdit::setTextMargins(const QMargins &margins) */ void QLineEdit::getTextMargins(int *left, int *top, int *right, int *bottom) const { - Q_D(const QLineEdit); + QMargins m = textMargins(); if (left) - *left = d->leftTextMargin; + *left = m.left(); if (top) - *top = d->topTextMargin; + *top = m.top(); if (right) - *right = d->rightTextMargin; + *right = m.right(); if (bottom) - *bottom = d->bottomTextMargin; + *bottom = m.bottom(); } /*! @@ -1179,7 +1178,7 @@ void QLineEdit::getTextMargins(int *left, int *top, int *right, int *bottom) con QMargins QLineEdit::textMargins() const { Q_D(const QLineEdit); - return QMargins(d->leftTextMargin, d->topTextMargin, d->rightTextMargin, d->bottomTextMargin); + return d->textMargins; } /*! @@ -1950,10 +1949,7 @@ void QLineEdit::paintEvent(QPaintEvent *) initStyleOption(&panel); style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this); QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); - r.setX(r.x() + d->effectiveLeftTextMargin()); - r.setY(r.y() + d->topTextMargin); - r.setRight(r.right() - d->effectiveRightTextMargin()); - r.setBottom(r.bottom() - d->bottomTextMargin); + r = r.marginsRemoved(d->effectiveTextMargins()); p.setClipRect(r); QFontMetrics fm = fontMetrics(); diff --git a/src/widgets/widgets/qlineedit_p.cpp b/src/widgets/widgets/qlineedit_p.cpp index 7d580e50a5..d2b5f87906 100644 --- a/src/widgets/widgets/qlineedit_p.cpp +++ b/src/widgets/widgets/qlineedit_p.cpp @@ -254,10 +254,7 @@ QRect QLineEditPrivate::adjustedContentsRect() const QStyleOptionFrame opt; q->initStyleOption(&opt); QRect r = q->style()->subElementRect(QStyle::SE_LineEditContents, &opt, q); - r.setX(r.x() + effectiveLeftTextMargin()); - r.setY(r.y() + topTextMargin); - r.setRight(r.right() - effectiveRightTextMargin()); - r.setBottom(r.bottom() - bottomTextMargin); + r = r.marginsRemoved(effectiveTextMargins()); return r; } @@ -672,14 +669,12 @@ static int effectiveTextMargin(int defaultMargin, const QLineEditPrivate::SideWi return e.widget->isVisibleTo(e.widget->parentWidget()); })); } -int QLineEditPrivate::effectiveLeftTextMargin() const +QMargins QLineEditPrivate::effectiveTextMargins() const { - return effectiveTextMargin(leftTextMargin, leftSideWidgetList(), sideWidgetParameters()); -} - -int QLineEditPrivate::effectiveRightTextMargin() const -{ - return effectiveTextMargin(rightTextMargin, rightSideWidgetList(), sideWidgetParameters()); + return {effectiveTextMargin(textMargins.left(), leftSideWidgetList(), sideWidgetParameters()), + textMargins.top(), + effectiveTextMargin(textMargins.right(), rightSideWidgetList(), sideWidgetParameters()), + textMargins.bottom()}; } diff --git a/src/widgets/widgets/qlineedit_p.h b/src/widgets/widgets/qlineedit_p.h index 3f98aab901..a11fea6bbe 100644 --- a/src/widgets/widgets/qlineedit_p.h +++ b/src/widgets/widgets/qlineedit_p.h @@ -67,6 +67,7 @@ #endif #include "QtCore/qpointer.h" #include "QtCore/qmimedata.h" +#include <QtCore/qmargins.h> #include "private/qwidgetlinecontrol_p.h" @@ -153,7 +154,7 @@ public: : control(0), frame(1), contextMenuEnabled(1), cursorVisible(0), dragEnabled(0), clickCausedFocus(0), edited(0), hscroll(0), vscroll(0), alignment(Qt::AlignLeading | Qt::AlignVCenter), - leftTextMargin(0), topTextMargin(0), rightTextMargin(0), bottomTextMargin(0), + textMargins{0, 0, 0, 0}, lastTextSize(0), mouseYThreshold(0) { } @@ -233,10 +234,7 @@ public: void _q_textChanged(const QString &); void _q_clearButtonClicked(); - int leftTextMargin; // use effectiveLeftTextMargin() in case of icon. - int topTextMargin; - int rightTextMargin; // use effectiveRightTextMargin() in case of icon. - int bottomTextMargin; + QMargins textMargins; // use effectiveTextMargins() in case of icon. QString placeholderText; @@ -252,8 +250,7 @@ public: inline const SideWidgetEntryList &rightSideWidgetList() const { return q_func()->layoutDirection() == Qt::LeftToRight ? trailingSideWidgets : leadingSideWidgets; } - int effectiveLeftTextMargin() const; - int effectiveRightTextMargin() const; + QMargins effectiveTextMargins() const; private: struct SideWidgetLocation { |