OGS
CheckboxDelegate.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ** INCLUDES **
5#include "CheckboxDelegate.h"
6
7#include <QApplication>
8#include <QCheckBox>
9#include <QEvent>
10#include <QMouseEvent>
11#include <QPainter>
12#include <QStyleOptionButton>
13
14CheckboxDelegate::CheckboxDelegate(QObject* parent) : QItemDelegate(parent) {}
15
16void CheckboxDelegate::paint(QPainter* painter,
17 const QStyleOptionViewItem& option,
18 const QModelIndex& index) const
19{
20 if (index.isValid())
21 {
22 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
23
24 QStyleOptionButton styleOptionButton;
25 styleOptionButton.state |= QStyle::State_Enabled;
26 if (checked)
27 {
28 styleOptionButton.state |= QStyle::State_On;
29 }
30 else
31 {
32 styleOptionButton.state |= QStyle::State_Off;
33 }
34
35 styleOptionButton.rect = this->checkboxRect(option);
36
37 QApplication::style()->drawControl(QStyle::CE_CheckBox,
38 &styleOptionButton, painter);
39 }
40 else
41 {
42 QItemDelegate::paint(painter, option, index);
43 }
44}
45
46bool CheckboxDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
47 const QStyleOptionViewItem& option,
48 const QModelIndex& index)
49{
50 Q_UNUSED(option);
51
52 if ((event->type() == QEvent::MouseButtonRelease) ||
53 (event->type() == QEvent::MouseButtonDblClick))
54 {
55 auto* mouse_event = static_cast<QMouseEvent*>(event);
56 if (mouse_event->button() != Qt::LeftButton ||
57 !checkboxRect(option).contains(mouse_event->pos()))
58 {
59 return false;
60 }
61 if (event->type() == QEvent::MouseButtonDblClick)
62 {
63 return true;
64 }
65 }
66 else if (event->type() == QEvent::KeyPress)
67 {
68 if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&
69 static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
70 {
71 return false;
72 }
73 }
74 else
75 {
76 return false;
77 }
78
79 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
80 return model->setData(index, !checked, Qt::EditRole);
81}
82
83QSize CheckboxDelegate::sizeHint(const QStyleOptionViewItem& option,
84 const QModelIndex& index) const
85{
86 Q_UNUSED(index);
87
88 QRect rect = checkboxRect(option);
89 return QSize(rect.width(), rect.height());
90}
91
93 const QStyleOptionViewItem& viewItemStyleOptions) const
94{
95 QStyleOptionButton styleOptionButton;
96 QRect rect = QApplication::style()->subElementRect(
97 QStyle::SE_CheckBoxIndicator, &styleOptionButton);
98 QPoint point(viewItemStyleOptions.rect.x() +
99 viewItemStyleOptions.rect.width() / 2 - rect.width() / 2,
100 viewItemStyleOptions.rect.y() +
101 viewItemStyleOptions.rect.height() / 2 -
102 rect.height() / 2);
103 return QRect(point, rect.size());
104}
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.