OGS
CheckboxDelegate.cpp
Go to the documentation of this file.
1
15// ** INCLUDES **
16#include "CheckboxDelegate.h"
17
18#include <QApplication>
19#include <QCheckBox>
20#include <QEvent>
21#include <QMouseEvent>
22#include <QPainter>
23#include <QStyleOptionButton>
24
25CheckboxDelegate::CheckboxDelegate(QObject* parent) : QItemDelegate(parent) {}
26
27void CheckboxDelegate::paint(QPainter* painter,
28 const QStyleOptionViewItem& option,
29 const QModelIndex& index) const
30{
31 if (index.isValid())
32 {
33 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
34
35 QStyleOptionButton styleOptionButton;
36 styleOptionButton.state |= QStyle::State_Enabled;
37 if (checked)
38 {
39 styleOptionButton.state |= QStyle::State_On;
40 }
41 else
42 {
43 styleOptionButton.state |= QStyle::State_Off;
44 }
45
46 styleOptionButton.rect = this->checkboxRect(option);
47
48 QApplication::style()->drawControl(QStyle::CE_CheckBox,
49 &styleOptionButton, painter);
50 }
51 else
52 {
53 QItemDelegate::paint(painter, option, index);
54 }
55}
56
57bool CheckboxDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
58 const QStyleOptionViewItem& option,
59 const QModelIndex& index)
60{
61 Q_UNUSED(option);
62
63 if ((event->type() == QEvent::MouseButtonRelease) ||
64 (event->type() == QEvent::MouseButtonDblClick))
65 {
66 auto* mouse_event = static_cast<QMouseEvent*>(event);
67 if (mouse_event->button() != Qt::LeftButton ||
68 !checkboxRect(option).contains(mouse_event->pos()))
69 {
70 return false;
71 }
72 if (event->type() == QEvent::MouseButtonDblClick)
73 {
74 return true;
75 }
76 }
77 else if (event->type() == QEvent::KeyPress)
78 {
79 if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&
80 static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
81 {
82 return false;
83 }
84 }
85 else
86 {
87 return false;
88 }
89
90 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
91 return model->setData(index, !checked, Qt::EditRole);
92}
93
94QSize CheckboxDelegate::sizeHint(const QStyleOptionViewItem& option,
95 const QModelIndex& index) const
96{
97 Q_UNUSED(index);
98
99 QRect rect = checkboxRect(option);
100 return QSize(rect.width(), rect.height());
101}
102
104 const QStyleOptionViewItem& viewItemStyleOptions) const
105{
106 QStyleOptionButton styleOptionButton;
107 QRect rect = QApplication::style()->subElementRect(
108 QStyle::SE_CheckBoxIndicator, &styleOptionButton);
109 QPoint point(viewItemStyleOptions.rect.x() +
110 viewItemStyleOptions.rect.width() / 2 - rect.width() / 2,
111 viewItemStyleOptions.rect.y() +
112 viewItemStyleOptions.rect.height() / 2 -
113 rect.height() / 2);
114 return QRect(point, rect.size());
115}
Definition of the CheckboxDelegate class.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Paints a checkbox. This overrides the default painting of a combo box.
QRect checkboxRect(const QStyleOptionViewItem &viewItemStyleOptions) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
Handles the click events and sets the model data.
CheckboxDelegate(QObject *parent=nullptr)
Constructor.