-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathstareditor.cpp
248 lines (231 loc) · 8.29 KB
/
stareditor.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "library/tabledelegates/stareditor.h"
#include <QItemSelectionModel>
#include <QMouseEvent>
#include <QPainter>
#include <QTableView>
#include "library/starrating.h"
#include "library/tabledelegates/tableitemdelegate.h"
#include "moc_stareditor.cpp"
// We enable mouse tracking on the widget so we can follow the cursor even
// when the user doesn't hold down any mouse button. We also turn on
// QWidget's auto-fill background feature to obtain an opaque background.
// (Without the call, the view's background would shine through the editor.)
/// StarEditor inherits QWidget and is used by StarDelegate to let the user
/// edit a star rating in the library using the mouse.
///
/// The class has been adapted from the official "Star Delegate Example",
/// see http://doc.trolltech.com/4.5/itemviews-stardelegate.html
StarEditor::StarEditor(QWidget* parent,
QTableView* pTableView,
const QModelIndex& index,
const QStyleOptionViewItem& option,
bool isKeyboardEditMode)
: QWidget(parent),
m_pTableView(pTableView),
m_index(index),
m_styleOption(option),
m_starCount(StarRating::kMinStarCount),
m_starCountToSave(StarRating::kInvalidStarCount),
m_isKeyboardEditMode(isKeyboardEditMode) {
DEBUG_ASSERT(m_pTableView);
setMouseTracking(true);
installEventFilter(this);
}
QSize StarEditor::sizeHint() const {
return m_starRating.sizeHint();
}
void StarEditor::paintEvent(QPaintEvent*) {
m_styleOption.rect = rect();
if (underMouse()) {
m_styleOption.state |= QStyle::State_MouseOver;
} else {
m_styleOption.state &= ~QStyle::State_MouseOver;
}
QItemSelectionModel* selectionModel = m_pTableView->selectionModel();
if (selectionModel) {
// If the editor cell is selected set the respective flag so we can use the
// palette's 'HighlightedText' font color for the brush StarRating will use
// to fill the star/diamond polygons with.
// Else, unset it and we use the regular color.
if (selectionModel->isSelected(m_index)) {
m_styleOption.state |= QStyle::State_Selected;
} else {
m_styleOption.state &= ~QStyle::State_Selected;
}
// Accordingly, un/set the focus flag.
if (selectionModel->currentIndex() == m_index) {
m_styleOption.state |= QStyle::State_HasFocus;
} else {
m_styleOption.state &= ~QStyle::State_HasFocus;
}
}
QPainter painter(this);
painter.setClipRect(m_styleOption.rect);
// Draw standard item with the table view's style
QStyle* style = this->style();
if (style) {
// Overwrite the background and stars rendered by the StarDelegate
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &m_styleOption, &painter, this);
}
// Set the palette appropriately based on whether the row is selected or
// not. We also have to check if it is inactive or not and use the
// appropriate ColorGroup.
QPalette::ColorGroup cg = m_styleOption.state & QStyle::State_Enabled
? QPalette::Normal
: QPalette::Disabled;
if (cg == QPalette::Normal && !(m_styleOption.state & QStyle::State_Active)) {
cg = QPalette::Inactive;
}
painter.save();
if (m_styleOption.state & QStyle::State_Selected) {
painter.setBrush(m_styleOption.palette.color(cg, QPalette::HighlightedText));
} else {
painter.setBrush(m_styleOption.palette.color(cg, QPalette::Text));
}
m_starRating.paint(&painter, m_styleOption.rect);
painter.restore();
}
bool StarEditor::eventFilter(QObject* obj, QEvent* event) {
switch (event->type()) {
case QEvent::KeyPress: {
VERIFY_OR_DEBUG_ASSERT(m_isKeyboardEditMode) {
// Persistent editors (i.e. those opened using openPersistentEditor)
// should not receive keyboard events via their eventFilter, so we
// shouldn't normally arrive here - but if we do, just forward the events.
return false;
}
// Change rating when certain keys are pressed
// while we are in edit mode.
QKeyEvent* ke = static_cast<QKeyEvent*>(event);
int newRating = m_starRating.starCount();
switch (ke->key()) {
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_PageUp:
case Qt::Key_PageDown: {
// Allow handling of certain keyboard navigation events.
// This logic attempts to match the behavior for text editor cells.
return false;
}
case Qt::Key_Home:
case Qt::Key_End: {
// Only forward Home and End if the Ctrl key is pressed.
// This matches the behavior of normal text editor cells.
if (ke->modifiers() & Qt::ControlModifier) {
return false;
}
return true;
}
case Qt::Key_0: {
newRating = 0;
break;
}
case Qt::Key_1: {
newRating = 1;
break;
}
case Qt::Key_2: {
newRating = 2;
break;
}
case Qt::Key_3: {
newRating = 3;
break;
}
case Qt::Key_4: {
newRating = 4;
break;
}
case Qt::Key_5: {
newRating = 5;
break;
}
case Qt::Key_6: {
newRating = 6;
break;
}
case Qt::Key_7: {
newRating = 7;
break;
}
case Qt::Key_8: {
newRating = 8;
break;
}
case Qt::Key_9: {
newRating = 9;
break;
}
case Qt::Key_Right:
case Qt::Key_Plus: {
newRating++;
break;
}
case Qt::Key_Left:
case Qt::Key_Minus: {
newRating--;
break;
}
}
newRating = math_clamp(newRating, StarRating::kMinStarCount, m_starRating.maxStarCount());
if (newRating != m_starRating.starCount()) {
// Apply star rating if it changed
m_starRating.setStarCount(newRating);
m_starCount = newRating;
update();
}
// Prevent other keys from being handled as global keyboard shortcuts.
// This matches the behavior of the other edit controls.
return true;
}
case QEvent::Leave:
case QEvent::ContextMenu: {
// Note: it seems with Qt5 we do not reliably get a Leave event when
// invoking the track menu via right click, so reset the rating now.
// The event is forwarded to parent QTableView.
m_starCountToSave = StarRating::kInvalidStarCount;
resetRating();
break;
}
case QEvent::MouseButtonPress: {
// Workaround: The MouseButtonPress can cause a focus change that might
// cause a database reload, which then overwrites m_starRating before
// the MouseButtonRelease event has a chance to commit the new value
// to the model.
m_starCountToSave = m_starRating.starCount();
break;
}
case QEvent::MouseButtonRelease: {
if (m_starCountToSave != StarRating::kInvalidStarCount) {
m_starRating.setStarCount(m_starCountToSave);
m_starCountToSave = StarRating::kInvalidStarCount;
}
emit editingFinished();
break;
}
case QEvent::MouseMove: {
// Change rating only if no button is pressed.
// This allows dragging the row also by grabbing the star cell
QMouseEvent* me = static_cast<QMouseEvent*>(event);
if (me->buttons().testFlag(Qt::NoButton)) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const int eventPosition = static_cast<int>(me->position().x());
#else
const int eventPosition = me->x();
#endif
int star = m_starRating.starAtPosition(eventPosition, m_styleOption.rect);
if (star <= StarRating::kInvalidStarCount) {
resetRating();
} else if (star != m_starRating.starCount()) {
// Apply star rating if it changed
m_starRating.setStarCount(star);
update();
}
}
break;
}
default:
break;
}
return QWidget::eventFilter(obj, event);
}