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
|
#include <QPainter>
#include <QPushButton>
#include <QTableView>
#include "control/controlproxy.h"
#include "library/previewbuttondelegate.h"
#include "library/trackmodel.h"
#include "mixer/playerinfo.h"
#include "mixer/playermanager.h"
#include "track/track.h"
#include "widget/wlibrarytableview.h"
PreviewButtonDelegate::PreviewButtonDelegate(WLibraryTableView* parent, int column)
: TableItemDelegate(parent),
m_pTableView(parent),
m_isOneCellInEditMode(false),
m_column(column) {
m_pPreviewDeckPlay = new ControlProxy(
PlayerManager::groupForPreviewDeck(0), "play", this);
m_pPreviewDeckPlay->connectValueChanged(this, &PreviewButtonDelegate::previewDeckPlayChanged);
m_pCueGotoAndPlay = new ControlProxy(
PlayerManager::groupForPreviewDeck(0), "cue_gotoandplay", this);
// This assumes that the parent is wtracktableview
connect(this,
&PreviewButtonDelegate::loadTrackToPlayer,
parent,
&WLibraryTableView::loadTrackToPlayer);
// The button needs to be parented to receive the parent styles.
m_pButton = make_parented<LibraryPreviewButton>(m_pTableView);
m_pButton->setCheckable(true);
m_pButton->setChecked(false);
// We need to hide the button that it is not painted by the QObject tree
m_pButton->hide();
connect(m_pTableView, &QTableView::entered, this, &PreviewButtonDelegate::cellEntered);
}
PreviewButtonDelegate::~PreviewButtonDelegate() {
}
QWidget* PreviewButtonDelegate::createEditor(QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(option);
QPushButton* btn = new LibraryPreviewButton(parent);
btn->setCheckable(true);
// Prevent being focused by Tab key or emulated Tab sent by library controls
// Avoids a Tab loop caused by setLibraryFocus() when the mouse pointer
// is above the button (Prev.btn > Table > Prev.btn > ...) and also
// keeps the table focus border when the hovered button's row is selected
btn->setFocusPolicy(Qt::NoFocus);
bool playing = m_pPreviewDeckPlay->toBool();
// Check-state is whether the track is loaded (index.data()) and whether
// it's playing.
btn->setChecked(index.data().toBool() && playing);
connect(btn,
&QPushButton::clicked,
this,
&PreviewButtonDelegate::buttonClicked);
connect(this,
&PreviewButtonDelegate::buttonSetChecked,
btn,
&QPushButton::setChecked);
return btn;
}
void PreviewButtonDelegate::setEditorData(QWidget* editor,
const QModelIndex& index) const {
Q_UNUSED(editor);
Q_UNUSED(index);
}
void PreviewButtonDelegate::setModelData(QWidget* editor,
QAbstractItemModel* model,
const QModelIndex& index) const {
Q_UNUSED(editor);
Q_UNUSED(model);
Q_UNUSED(index);
}
void PreviewButtonDelegate::paintItem(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
paintItemBackground(painter, option, index);
// Let the editor paint in this case
if (index == m_currentEditedCellIndex) {
return;
}
if (!m_pButton) {
return;
}
// We only need m_pButton to have the right width/height, since we are
// calling its render method directly. Every resize/translate of a widget
// causes Qt to flush the backing store, so we need to avoid this whenever
// possible.
if (option.rect.size() != m_pButton->size()) {
m_pButton->setFixedSize(option.rect.size());
}
// Check-state is whether the track is loaded (index.data()) and whether
// it's playing.
m_pButton->setChecked(index.data().toBool() && m_pPreviewDeckPlay->toBool());
// Render button at the desired position.
painter->translate(option.rect.topLeft());
// Avoid QWidget::render and call the equivalent of QPushButton::paintEvent
// directly.
m_pButton->paint(painter);
}
void PreviewButtonDelegate::updateEditorGeometry(QWidget* editor,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
QSize PreviewButtonDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(option);
Q_UNUSED(index);
if (!m_pButton) {
return QSize();
}
return m_pButton->sizeHint();
}
void PreviewButtonDelegate::cellEntered(const QModelIndex& index) {
if (!m_pTableView) {
return;
}
// this slot is called if the mouse pointer enters ANY cell on
// the QTableView but the code should only be executed on a button
if (index.column() == m_column) {
if (m_isOneCellInEditMode) {
m_pTableView->closePersistentEditor(m_currentEditedCellIndex);
}
m_pTableView->openPersistentEditor(index);
m_isOneCellInEditMode = true;
m_currentEditedCellIndex = index;
} else if (m_isOneCellInEditMode) { // close editor if the mouse leaves the button
m_isOneCellInEditMode = false;
m_pTableView->closePersistentEditor(m_currentEditedCellIndex);
m_currentEditedCellIndex = QModelIndex();
}
}
void PreviewButtonDelegate::buttonClicked() {
if (!m_pTableView) {
return;
}
TrackModel *pTrackModel = dynamic_cast<TrackModel*>(m_pTableView->model());
if (!pTrackModel) {
return;
}
QString group = PlayerManager::groupForPreviewDeck(0);
TrackPointer pOldTrack = PlayerInfo::instance().getTrackInfo(group);
bool playing = m_pPreviewDeckPlay->toBool();
TrackPointer pTrack = pTrackModel->getTrack(m_currentEditedCellIndex);
if (pTrack && pTrack != pOldTrack) {
emit loadTrackToPlayer(pTrack, group, true);
} else if (pTrack == pOldTrack && !playing) {
// Since the Preview deck might be hidden
// Starting at cue is a predictable behavior
m_pCueGotoAndPlay->set(1.0);
} else {
m_pPreviewDeckPlay->set(0.0);
}
}
void PreviewButtonDelegate::previewDeckPlayChanged(double v) {
m_pTableView->update();
if (m_isOneCellInEditMode) {
TrackModel *pTrackModel = dynamic_cast<TrackModel*>(m_pTableView->model());
if (!pTrackModel) {
return;
}
QString group = PlayerManager::groupForPreviewDeck(0);
TrackPointer pPreviewTrack = PlayerInfo::instance().getTrackInfo(group);
TrackPointer pTrack = pTrackModel->getTrack(m_currentEditedCellIndex);
if (pTrack && pTrack == pPreviewTrack) {
emit buttonSetChecked(v > 0.0);
}
}
}
|