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>bb10
parent
31ffc7bf2a
commit
7940bc32a1
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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()};
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue