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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/***************************************************************************
* Copyright (C) 2012-2019 by Ilya Kotov *
* forkotov02@ya.ru *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QApplication>
#include <QMouseEvent>
#include "radioitemdelegate_p.h"
RadioItemDelegate::RadioItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
void RadioItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (hasRadioButton(index))
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
QStyleOptionButton buttonOption;
buttonOption.rect = option.rect;
buttonOption.text = index.data(Qt::DisplayRole).toString();
buttonOption.state |= QStyle::State_Enabled;
if (index.data(Qt::CheckStateRole) == Qt::Checked)
{
buttonOption.state |= QStyle::State_On;
buttonOption.state &=~ QStyle::State_Off;
}
else
{
buttonOption.state |= QStyle::State_Off;
buttonOption.state &=~ QStyle::State_On;
}
buttonOption.palette = opt.palette;
if(opt.state.testFlag(QStyle::State_Selected))
{
buttonOption.palette.setBrush(QPalette::WindowText,
opt.palette.brush(QPalette::HighlightedText));
qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter);
}
qApp->style()->drawControl(QStyle::CE_RadioButton, &buttonOption, painter);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
QSize RadioItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSize size = QStyledItemDelegate::sizeHint(option, index);
if (hasRadioButton(index))
{
int buttonHeight = qApp->style()->pixelMetric(QStyle::PM_ExclusiveIndicatorHeight, &option);
size.setHeight(qMax(size.height(), buttonHeight));
}
return size;
}
bool RadioItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
if (event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::KeyPress)
{
if (hasRadioButton(index))
{
if(event->type() == QEvent::MouseButtonRelease)
{
QRect checkRect = qApp->style()->subElementRect(QStyle::SE_RadioButtonIndicator, &option);
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if(!checkRect.contains(mouseEvent->pos()))
return true;
}
if (!index.data(Qt::CheckStateRole).toBool())
{
model->setData(index, Qt::Checked, Qt::CheckStateRole);
QModelIndex parentItem = index.parent();
for (int i = 0; i < model->rowCount(parentItem); ++i)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
QModelIndex childIndex = model->index(i, 0, parentItem);
#else
QModelIndex childIndex = parentItem.child(i, 0);
#endif
if (childIndex != index)
model->setData(childIndex, Qt::Unchecked, Qt::CheckStateRole);
}
}
return true;
}
}
return QStyledItemDelegate::editorEvent(event,model,option,index);
}
bool RadioItemDelegate::hasRadioButton(const QModelIndex &index) const
{
return index.flags().testFlag(Qt::ItemIsUserCheckable) && index.data(RadioButtonRole).toBool();
}
|