-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonstateitemdelegate.cpp
93 lines (83 loc) · 3.06 KB
/
buttonstateitemdelegate.cpp
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
#include "buttonstateitemdelegate.h"
#include "buttonstate.h"
#include "buttonstatewidget.h"
#include <QtGui>
void ButtonStateItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.data().canConvert<ButtonState>()) {
ButtonState buttonState = index.data().value<ButtonState>();
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
else if (buttonState.IconMode() == QIcon::Disabled)
buttonState.restore();
buttonState.paint(painter, option.rect, option.palette,
ButtonState::ReadOnly);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
//! [0]
}
//! [1]
QSize ButtonStateItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.data().canConvert<ButtonState>()) {
ButtonState buttonState = index.data().value<ButtonState>();
return buttonState.sizeHint();
} else {
return QStyledItemDelegate::sizeHint(option, index);
}
}
//! [1]
//! [2]
QWidget *ButtonStateItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.data().canConvert<ButtonState>()) {
ButtonStateWidget *editor = new ButtonStateWidget(parent);
editor->setButtonState(index.data().value<ButtonState>());
editor->setPos(index.row(), index.column());
connect(editor, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
return editor;
} else {
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
//! [2]
//! [3]
void ButtonStateItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
if (index.data().canConvert<ButtonState>()) {
ButtonState buttonState = index.data().value<ButtonState>();
ButtonStateWidget *buttonStateWidget = qobject_cast<ButtonStateWidget *>(editor);
buttonStateWidget->setButtonState(buttonState);
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
//! [3]
//! [4]
void ButtonStateItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if (index.data().canConvert<ButtonState>()) {
ButtonStateWidget *buttonStateWidget = qobject_cast<ButtonStateWidget *>(editor);
model->setData(index, qVariantFromValue(buttonStateWidget->buttonState()));
} else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}
//! [4]
//! [5]
void ButtonStateItemDelegate::commitAndCloseEditor()
{
ButtonStateWidget *editor = qobject_cast<ButtonStateWidget *>(sender());
editor->restore();
emit commitData(editor);
emit fieldClicked(editor->Row(), editor->Column());
emit closeEditor(editor);
}