-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunitslineedit.cpp
More file actions
60 lines (48 loc) · 1.61 KB
/
unitslineedit.cpp
File metadata and controls
60 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "unitslineedit.h"
#include <QDebug>
#include <QFontMetrics>
#include <QPainter>
#include <QStyleOptionFrame>
UnitsLineEdit::UnitsLineEdit(QWidget *parent) : UnitsLineEdit(QString(), QString(), parent)
{
}
UnitsLineEdit::UnitsLineEdit(const QString &text, const QString &units, QWidget *parent) : QLineEdit(text, parent)
{
setUnits(units);
}
UnitsLineEdit::~UnitsLineEdit()
{
}
void UnitsLineEdit::setUnits(const QString &units)
{
unitString = units;
QFontMetrics fm(font());
int unitsWidth = fm.horizontalAdvance(unitString);
setTextMargins(0, 0, unitsWidth + 2, 0);
}
QString UnitsLineEdit::units()
{
return unitString;
}
void UnitsLineEdit::paintEvent(QPaintEvent *event)
{
if (readOnlyFlag != isReadOnly())
{
setStyleSheet("QLineEdit[readOnly=\"true\"] {background-color: " + palette().color(QPalette::Window).name() + ";}");
readOnlyFlag = isReadOnly();
}
QLineEdit::paintEvent(event);
QStyleOptionFrame panel;
initStyleOption(&panel);
QRect contentRect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
QFontMetrics fm(font());
int unitsWidth = fm.horizontalAdvance(unitString);
static const int defaultPadding = 4; // Hard coded value from QLineEditPrivate
contentRect.setLeft(contentRect.x() + contentRect.width() - unitsWidth - defaultPadding);
contentRect.setWidth(unitsWidth);
QPainter painter(this);
// painter.setPen(palette().text().color());
painter.setPen(palette().placeholderText().color());
painter.setFont(font());
painter.drawText(contentRect, Qt::AlignVCenter, unitString);
}