-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstardelegate.cpp
107 lines (98 loc) · 2.86 KB
/
stardelegate.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "stardelegate.h"
/**
* @brief StarDelegate::paint
* @param painter
* @param option
* @param index
*/
void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if ((index.data()).canConvert<StarRating>()) {
StarRating starRating = index.data().value<StarRating>();
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
starRating.paint(painter, option.rect, option.palette,
StarRating::ReadOnly);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
/**
* @brief StarDelegate::sizeHint
* @param option
* @param index
* @return
*/
QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if ((index.data()).canConvert<StarRating>()) {
StarRating starRating = index.data().value<StarRating>();
return starRating.sizeHint();
} else {
return QStyledItemDelegate::sizeHint(option, index);
}
}
/**
* @brief StarDelegate::createEditor
* @param parent
* @param option
* @param index
* @return
*/
QWidget *StarDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if ((index.data()).canConvert<StarRating>()) {
StarEditor *editor = new StarEditor(parent);
editor->setAllocInt(this->allocDisp);
connect(editor, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
return editor;
} else {
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
/**
* @brief StarDelegate::setEditorData
* @param editor
* @param index
*/
void StarDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
if ((index.data()).canConvert<StarRating>()) {
StarRating starRating = index.data().value<StarRating>();
StarEditor *starEditor = qobject_cast<StarEditor *>(editor);
starEditor->setStarRating(starRating);
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
/**
* @brief StarDelegate::setModelData
* @param editor
* @param model
* @param index
*/
void StarDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if ((index.data()).canConvert<StarRating>()) {
StarEditor *starEditor = qobject_cast<StarEditor *>(editor);
model->setData(index, qVariantFromValue(starEditor->starRating()));
} else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}
/**
* @brief StarDelegate::commitAndCloseEditor
*/
void StarDelegate::commitAndCloseEditor()
{
StarEditor *editor = qobject_cast<StarEditor *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}