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
|
#include "library/stardelegate.h"
#include <QPainter>
#include <QtDebug>
#include "library/stareditor.h"
#include "library/starrating.h"
#include "library/tableitemdelegate.h"
#include "moc_stardelegate.cpp"
StarDelegate::StarDelegate(QTableView* pTableView)
: TableItemDelegate(pTableView),
m_pTableView(pTableView),
m_isOneCellInEditMode(false) {
connect(pTableView, &QTableView::entered, this, &StarDelegate::cellEntered);
}
void StarDelegate::paintItem(
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
// let the editor do the painting if this cell is currently being edited
if (index == m_currentEditedCellIndex) {
return;
}
paintItemBackground(painter, option, index);
StarRating starRating = index.data().value<StarRating>();
starRating.paint(painter, option.rect);
}
QSize StarDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(option);
StarRating starRating = index.data().value<StarRating>();
return starRating.sizeHint();
}
QWidget* StarDelegate::createEditor(QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
// Populate the correct colors based on the styling
QStyleOptionViewItem newOption = option;
initStyleOption(&newOption, index);
StarEditor* editor = new StarEditor(parent, m_pTableView, index, newOption);
connect(editor,
&StarEditor::editingFinished,
this,
&StarDelegate::commitAndCloseEditor);
return editor;
}
void StarDelegate::setEditorData(QWidget* editor,
const QModelIndex& index) const {
StarRating starRating = index.data().value<StarRating>();
StarEditor* starEditor = qobject_cast<StarEditor*>(editor);
starEditor->setStarRating(starRating);
}
void StarDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const {
StarEditor* starEditor = qobject_cast<StarEditor*>(editor);
model->setData(index, QVariant::fromValue(starEditor->starRating()));
}
void StarDelegate::commitAndCloseEditor() {
StarEditor* editor = qobject_cast<StarEditor*>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void StarDelegate::cellEntered(const QModelIndex& index) {
// This slot is called if the mouse pointer enters ANY cell on the
// QTableView but the code should only be executed on a column with a
// StarRating.
if (index.data().canConvert(qMetaTypeId<StarRating>())) {
if (m_isOneCellInEditMode) {
m_pTableView->closePersistentEditor(m_currentEditedCellIndex);
}
m_pTableView->openPersistentEditor(index);
m_isOneCellInEditMode = true;
m_currentEditedCellIndex = index;
} else if (m_isOneCellInEditMode) {
m_isOneCellInEditMode = false;
m_pTableView->closePersistentEditor(m_currentEditedCellIndex);
m_currentEditedCellIndex = QModelIndex();
}
}
|