-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwlibrarytableview.cpp
428 lines (383 loc) · 15.5 KB
/
wlibrarytableview.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "widget/wlibrarytableview.h"
#include <QApplication>
#include <QFocusEvent>
#include <QFontMetrics>
#include <QHeaderView>
#include <QHelpEvent>
#include <QScrollBar>
#include <QToolTip>
#include "moc_wlibrarytableview.cpp"
#include "util/math.h"
class QFocusEvent;
namespace {
// number of entries in the model cache
constexpr int kModelCacheSize = 1000;
} // namespace
WLibraryTableView::WLibraryTableView(QWidget* parent,
UserSettingsPointer pConfig)
: QTableView(parent),
m_prevRow(-1),
m_prevColumn(-1),
m_pConfig(pConfig),
m_modelStateCache(kModelCacheSize) {
// Setup properties for table
// Editing starts when clicking on an already selected item.
setEditTriggers(QAbstractItemView::SelectedClicked|QAbstractItemView::EditKeyPressed);
//Enable selection by rows and extended selection (ctrl/shift click)
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::ExtendedSelection);
setWordWrap(false);
setShowGrid(false);
setCornerButtonEnabled(false);
setSortingEnabled(true);
// Used by delegates (e.g. StarDelegate) to tell when the mouse enters a
// cell.
setMouseTracking(true);
//Work around a Qt bug that lets you make your columns so wide you
//can't reach the divider to make them small again.
setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
verticalHeader()->hide();
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setAlternatingRowColors(true);
connect(verticalScrollBar(),
&QScrollBar::valueChanged,
this,
&WLibraryTableView::scrollValueChanged);
setTabKeyNavigation(false);
}
WLibraryTableView::~WLibraryTableView() {
}
void WLibraryTableView::moveSelection(int delta) {
QAbstractItemModel* pModel = model();
if (pModel == nullptr) {
return;
}
while (delta != 0) {
QItemSelectionModel* currentSelection = selectionModel();
if (currentSelection->selectedRows().length() > 0) {
if (delta > 0) {
// i is positive, so we want to move the highlight down
int row = currentSelection->selectedRows().last().row();
if (row + 1 < pModel->rowCount()) {
selectRow(row + 1);
} else {
// we wrap around at the end of the list so it is faster to get
// to the top of the list again
selectRow(0);
}
delta--;
} else {
// i is negative, so move down
int row = currentSelection->selectedRows().first().row();
if (row - 1 >= 0) {
selectRow(row - 1);
} else {
selectRow(pModel->rowCount() - 1);
}
delta++;
}
} else {
// no selection, so select the first or last element depending on delta
if (delta > 0) {
selectRow(0);
delta--;
} else {
selectRow(pModel->rowCount() - 1);
delta++;
}
}
}
}
void WLibraryTableView::saveTrackModelState(
const QAbstractItemModel* model, const QString& key) {
//qDebug() << "saveTrackModelState:" << model << key;
VERIFY_OR_DEBUG_ASSERT(model) {
return;
}
VERIFY_OR_DEBUG_ASSERT(!key.isEmpty()) {
return;
}
ModelState* state = m_modelStateCache.take(key);
if (!state) {
state = new ModelState();
}
state->verticalScrollPosition = verticalScrollBar()->value();
state->horizontalScrollPosition = horizontalScrollBar()->value();
state->selectedRows = selectionModel()->selectedRows();
const QModelIndex currIndex = selectionModel()->currentIndex();
if (currIndex.isValid()) {
state->currentIndex = currIndex;
} else {
state->currentIndex = QModelIndex();
}
m_modelStateCache.insert(key, state, 1);
}
bool WLibraryTableView::restoreTrackModelState(
const QAbstractItemModel* model, const QString& key) {
//qDebug() << "restoreTrackModelState:" << model << key;
//qDebug() << m_modelStateCache.keys();
if (model == nullptr) {
return false;
}
ModelState* state = m_modelStateCache.take(key);
if (!state) {
// No previous state for model key,
// reset scroll bars and current index
verticalScrollBar()->setValue(0);
horizontalScrollBar()->setValue(0);
setCurrentIndex(QModelIndex());
return false;
}
verticalScrollBar()->setValue(state->verticalScrollPosition);
horizontalScrollBar()->setValue(state->horizontalScrollPosition);
auto* pSelection = selectionModel();
pSelection->clearSelection();
QModelIndexList selectedRows = state->selectedRows;
if (!selectedRows.isEmpty()) {
for (auto index : std::as_const(selectedRows)) {
pSelection->select(index,
QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
}
QModelIndex currIndex = state->currentIndex;
restoreCurrentIndex(currIndex);
// reinsert the state into the cache
m_modelStateCache.insert(key, state, 1);
return true;
}
void WLibraryTableView::saveCurrentIndex() {
QItemSelectionModel* pSelectionModel = selectionModel();
if (!pSelectionModel) {
return;
}
QModelIndexList indices = pSelectionModel->selectedRows();
if (indices.isEmpty()) {
return;
}
m_prevRow = indices.first().row();
m_prevColumn = currentIndex().isValid() ? currentIndex().column() : columnAt(0);
}
void WLibraryTableView::restoreCurrentIndex(const QModelIndex& index) {
QItemSelectionModel* pSelectionModel = selectionModel();
if (!pSelectionModel) {
return;
}
int row = index.isValid() ? index.row() : m_prevRow;
int col = index.isValid() ? index.column() : m_prevColumn;
if (model()->rowCount() == 0 || row < 0 || col < 0) {
// nothing to select
return;
}
if (model()->rowCount() < row + 1) {
// select last row
row = model()->rowCount() - 1;
}
if (isColumnHidden(col)) {
// select first column
col = columnAt(0);
}
QModelIndex idx = model()->index(row, col);
if (idx.isValid()) {
pSelectionModel->setCurrentIndex(idx, QItemSelectionModel::NoUpdate);
scrollTo(idx);
}
m_prevRow = -1;
m_prevColumn = -1;
}
void WLibraryTableView::setTrackTableFont(const QFont& font) {
setFont(font);
QFontMetrics metrics(font);
verticalHeader()->setMinimumSectionSize(metrics.height());
// Resize the 'Played' checkbox and the BPM lock icon.
// Note: this works well for library font sizes up to ~200% of the original
// system font's size (that set with Qt5 Settings respectively). Above that,
// the indicators start to overlap the item text because the painter rectangle
// is not resized (also depending on the system font size).
setStyleSheet(QStringLiteral(
"WTrackTableView::indicator,"
"#LibraryBPMButton::indicator {"
"height: %1px;"
"width: %1px;}")
.arg(metrics.height() * 0.7));
}
void WLibraryTableView::setTrackTableRowHeight(int rowHeight) {
verticalHeader()->setDefaultSectionSize(math_max(
rowHeight, verticalHeader()->minimumSectionSize()));
}
void WLibraryTableView::setSelectedClick(bool enable) {
if (enable) {
setEditTriggers(QAbstractItemView::SelectedClicked|QAbstractItemView::EditKeyPressed);
} else {
setEditTriggers(QAbstractItemView::EditKeyPressed);
}
}
void WLibraryTableView::saveCurrentViewState() {
const QAbstractItemModel* currentModel = model();
QString key = getModelStateKey();
if (!currentModel || key.isEmpty()) {
return;
}
saveTrackModelState(currentModel, key);
}
bool WLibraryTableView::restoreCurrentViewState() {
const QAbstractItemModel* currentModel = model();
QString key = getModelStateKey();
if (!currentModel || key.isEmpty()) {
return false;
}
return restoreTrackModelState(currentModel, key);
}
void WLibraryTableView::focusInEvent(QFocusEvent* event) {
QTableView::focusInEvent(event);
if (event->reason() == Qt::TabFocusReason ||
event->reason() == Qt::BacktabFocusReason ||
event->reason() == Qt::OtherFocusReason) {
// On FocusIn caused by a tab action with no focused item, select the
// current or first track which can then instantly be loaded to a deck.
// This is especially helpful if the table has only one track, which can
// not be selected with up/down buttons, either physical or emulated via
// [Library],MoveVertical controls. See #9548
if (model() && model()->rowCount() > 0) {
if (selectionModel()->hasSelection()) {
DEBUG_ASSERT(!selectionModel()->selectedIndexes().isEmpty());
if (!currentIndex().isValid() ||
!selectionModel()->isSelected(currentIndex())) {
// Reselect the first selected index
selectRow(selectionModel()->selectedIndexes().first().row());
}
} else {
if (!currentIndex().isValid()) {
// Select the first row if no row is focused
selectRow(0);
DEBUG_ASSERT(currentIndex().row() == 0);
// Unfortunately, even though we now have a valid currentIndex(),
// that would still not be selected by now if we're here because of
// the first Qt::BacktabFocusReason to tracks in this session. (Qt 5.12.8)
// So let's use the currentIndex below.
}
// Select the row of the currently focused index.
// For some reason selectRow(currentIndex().row()) would not
// select for the first Qt::BacktabFocusReason in a session
// even though currentIndex() is valid.
selectionModel()->select(currentIndex(),
QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
DEBUG_ASSERT(currentIndex().isValid());
DEBUG_ASSERT(selectionModel()->isSelected(currentIndex()));
// scrollTo() doesn't always seem to work!?
scrollTo(currentIndex());
}
}
}
QModelIndex WLibraryTableView::moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers) {
QAbstractItemModel* pModel = model();
if (pModel) {
switch (cursorAction) {
// The up and down cursor keys should wrap the list around. This
// behavior also applies to the `[Library],MoveVertical` action that is
// usually bound to the library browse encoder on controllers. Otherwise
// browsing a key-sorted library list requires either a serious workout
// or the user needs to reach for the mouse or keyboard when moving
// between 12/C#m/E and 1/G#m/B. This is very similar to
// `moveSelection()`, except that it doesn't actually modify the
// selection. It simply returns a new cursor that the keyboard event
// handler in `QAbstractItemView` uses to either move the cursor, move
// the selection, or extend the selection depending on which modifier
// keys are held down.
// Note: Shift modifier prevents wrap-around.
case QAbstractItemView::MoveUp:
case QAbstractItemView::MoveDown: {
const QModelIndex current = currentIndex();
if (current.isValid()) {
const int row = currentIndex().row();
const int column = currentIndex().column();
if (cursorAction == QAbstractItemView::MoveDown) {
if (row + 1 < pModel->rowCount()) {
return pModel->index(row + 1, column);
} else if (!modifiers.testFlag(Qt::ShiftModifier)) {
return pModel->index(0, column);
}
} else {
if (row - 1 >= 0) {
return pModel->index(row - 1, column);
} else if (!modifiers.testFlag(Qt::ShiftModifier)) {
return pModel->index(pModel->rowCount() - 1, column);
}
}
} else {
// If the cursor does not yet exist (because the view has not
// yet been interacted with) then this selects the first or last
// row
const int row = cursorAction == QAbstractItemView::MoveUp
? pModel->rowCount() - 1
: 0;
// Selecting a hidden column doesn't work, so we'll need to find
// the first non-hidden column here
int column = 0;
while (isColumnHidden(column) && column < pModel->columnCount()) {
column++;
}
return pModel->index(row, column);
}
} break;
// Make the home and end keys move to the first and last row rather than
// the first and last column (QAbstractItemView default)
case QAbstractItemView::MoveHome:
case QAbstractItemView::MoveEnd: {
const QModelIndex current = currentIndex();
// We don't want to change the selected column if a column has
// already been selected
int column = current.column();
if (!current.isValid()) {
// Selecting a hidden column doesn't work, so we'll need to find
// the first non-hidden column here
int column = 0;
while (isColumnHidden(column) && column < pModel->columnCount()) {
column++;
}
}
if (cursorAction == QAbstractItemView::MoveHome) {
return pModel->index(0, column);
} else {
return pModel->index(pModel->rowCount() - 1, column);
}
} break;
case QAbstractItemView::MoveLeft:
case QAbstractItemView::MoveRight:
if (modifiers & Qt::ControlModifier) {
// Ignore, so it can be handled by WLibrary::keyEvent
// to navigate to the sidebar
return currentIndex();
}
break;
default:
break;
}
}
return QTableView::moveCursor(cursorAction, modifiers);
}
void WLibraryTableView::dataChanged(
const QModelIndex& topLeft,
const QModelIndex& bottomRight,
const QVector<int>& roles) {
for (const auto& role : roles) {
// Note: At this point the tooltip is already showing
// "Fetching image ..." or still in an effect progress.
// QToolTip::isVisible() is false for the later.
if (role == Qt::ToolTipRole) {
QPoint globalPos = QCursor::pos();
QWidget* pViewPort = QApplication::widgetAt(globalPos);
if (pViewPort) {
QPoint viewPortPos = pViewPort->mapFromGlobal(globalPos);
if (indexAt(viewPortPos) == topLeft) {
QHelpEvent toolTipEvent(QEvent::ToolTip,
pViewPort->mapFromGlobal(globalPos),
globalPos);
viewportEvent(&toolTipEvent);
}
}
}
}
QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
};