1 | #include <CheckBox.h> |
2 | |
3 | #include <QEvent> |
4 | #include <QStyleOptionButton> |
5 | #include <QStylePainter> |
6 | |
7 | #include <array> |
8 | |
9 | namespace |
10 | { |
11 | static std::array<const char *, 6> indicators { ":/icons/qcb" , ":/icons/qcb_c" , ":/icons/qcb_i" , |
12 | ":/icons/qcb_d" , ":/icons/qcb_d_c" , ":/icons/qcb_d_i" }; |
13 | } |
14 | |
15 | CheckBox::CheckBox(QWidget *parent) |
16 | : QCheckBox(parent) |
17 | { |
18 | } |
19 | |
20 | CheckBox::CheckBox(const QString &text, QWidget *parent) |
21 | : QCheckBox(text, parent) |
22 | { |
23 | } |
24 | |
25 | QString CheckBox::getIndicator(QStyle::State state) const |
26 | { |
27 | if (state & QStyle::State_Off) |
28 | return QString::fromUtf8((state & QStyle::State_Enabled) ? indicators[0] : indicators[3]); |
29 | else if (state & QStyle::State_On) |
30 | return QString::fromUtf8((state & QStyle::State_Enabled) ? indicators[1] : indicators[4]); |
31 | else if (state & QStyle::State_NoChange) |
32 | return QString::fromUtf8((state & QStyle::State_Enabled) ? indicators[2] : indicators[5]); |
33 | |
34 | return QString(); |
35 | } |
36 | |
37 | bool CheckBox::event(QEvent *e) |
38 | { |
39 | if (e->type() == QEvent::Wheel) |
40 | return false; |
41 | |
42 | return QCheckBox::event(e); |
43 | } |
44 | |
45 | void CheckBox::paintEvent(QPaintEvent *) |
46 | { |
47 | QStylePainter painter(this); |
48 | |
49 | QStyleOptionButton option; |
50 | |
51 | initStyleOption(&option); |
52 | |
53 | painter.drawControl(QStyle::CE_CheckBox, option); |
54 | |
55 | const QRect rect = style()->subElementRect(QStyle::SE_CheckBoxIndicator, &option, this); |
56 | |
57 | painter.drawPixmap(rect, QIcon(getIndicator(option.state)).pixmap(rect.size())); |
58 | } |
59 | |