1#include <CheckBox.h>
2
3#include <QEvent>
4#include <QStyleOptionButton>
5#include <QStylePainter>
6
7#include <array>
8
9namespace
10{
11static 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
15CheckBox::CheckBox(QWidget *parent)
16 : QCheckBox(parent)
17{
18}
19
20CheckBox::CheckBox(const QString &text, QWidget *parent)
21 : QCheckBox(text, parent)
22{
23}
24
25QString 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
37bool CheckBox::event(QEvent *e)
38{
39 if (e->type() == QEvent::Wheel)
40 return false;
41
42 return QCheckBox::event(e);
43}
44
45void 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