-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathToggleSwitch.cpp
58 lines (49 loc) · 1.42 KB
/
ToggleSwitch.cpp
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
#include "ToggleSwitch.h"
#include <QDebug>
#include <QStyleOption>
#include <QApplication>
CToggleSwitch::CToggleSwitch(QWidget* parent)
: Super(parent)
{
qDebug() << "CToggleSwitch()";
connect(this, &CToggleSwitch::toggled, [this](bool Checked){
Q_UNUSED(Checked);
updateText();
});
updateText();
setText("Uwe");
}
void CToggleSwitch::setOnText(const QString& OnText)
{
m_OnText = OnText;
updateText();
}
void CToggleSwitch::setOffText(const QString& OffText)
{
m_OffText = OffText;
updateText();
}
void CToggleSwitch::updateText()
{
qDebug() << "updateText";
setText(isChecked() ? m_OnText : m_OffText);
m_SizeHint = QSize();
updateGeometry();
}
QSize CToggleSwitch::sizeHint() const
{
if (m_SizeHint.isValid())
return m_SizeHint;
ensurePolished();
QFontMetrics fm = fontMetrics();
QStyleOptionButton opt;
initStyleOption(&opt);
const auto& Text = (m_OnText.size() > m_OffText.size()) ? m_OnText : m_OffText;
QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false,
Text).size();
if (!opt.icon.isNull())
sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
m_SizeHint = (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this)
.expandedTo(QApplication::globalStrut()));
return m_SizeHint;
}