summaryrefslogtreecommitdiffstats
path: root/src/library/previewbuttondelegate.cpp
blob: e8417800ffaf1f0dbdbe63f7a574d63308e3d401 (plain)
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
#include <QPainter>
#include <QPushButton>
#include <QTableView>

#include "library/previewbuttondelegate.h"
#include "library/trackmodel.h"
#include "mixer/playerinfo.h"
#include "mixer/playermanager.h"
#include "track/track.h"
#include "control/controlproxy.h"

PreviewButtonDelegate::PreviewButtonDelegate(QTableView* 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(SLOT(previewDeckPlayChanged(double)));

    m_pCueGotoAndPlay = new ControlProxy(
            PlayerManager::groupForPreviewDeck(0), "cue_gotoandplay", this);

    // This assumes that the parent is wtracktableview
    connect(this, SIGNAL(loadTrackToPlayer(TrackPointer, QString, bool)),
            parent, SIGNAL(loadTrackToPlayer(TrackPointer, QString, bool)));

    // The button needs to be parented to receive the parent styles
    m_pButton = make_parented<QPushButton>(parent);
    m_pButton->setObjectName("LibraryPreviewButton");
    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();
    // Set visible to stop resizing in the background 
    // which may lead to a crash when already referenced by a painter obeject 
    // during the render() call. 
    m_pButton->setAttribute(Qt::WA_WState_Visible);

    connect(m_pTableView, SIGNAL(entered(QModelIndex)),
            this, SLOT(cellEntered(QModelIndex)));
}

PreviewButtonDelegate::~PreviewButtonDelegate() {
}

QWidget* PreviewButtonDelegate::createEditor(QWidget* parent,
                                             const QStyleOptionViewItem& option,
                                             const QModelIndex& index) const {
    Q_UNUSED(option);
    QPushButton* btn = new QPushButton(parent);
    btn->setObjectName("LibraryPreviewButton");
    btn->setCheckable(true);
    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, SIGNAL(clicked()),
            this, SLOT(buttonClicked()));
    connect(this, SIGNAL(buttonSetChecked(bool)),
            btn, SLOT(setChecked(bool)));
    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 {
    // Let the editor paint in this case
    if (index == m_currentEditedCellIndex) {
        return;
    }

    if (!m_pButton) {
        return;
    }

    m_pButton->setGeometry(option.rect);
    bool playing = m_pPreviewDeckPlay->toBool();
    // Check-state is whether the track is loaded (index.data()) and whether
    // it's playing.
    m_pButton->setChecked(index.data().toBool() && playing);

    // Render button at the desired position
    painter->translate(option.rect.topLeft());
    m_pButton->render(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));
        }
    }
}