summaryrefslogtreecommitdiffstats
path: root/src/controllers/delegates/midiopcodedelegate.cpp
blob: b17721011d75d117e15d958cdb7428c4075b2827 (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
#include <QComboBox>

#include "controllers/delegates/midiopcodedelegate.h"
#include "controllers/midi/midimessage.h"
#include "controllers/midi/midiutils.h"

MidiOpCodeDelegate::MidiOpCodeDelegate(QObject* pParent)
        : QStyledItemDelegate(pParent) {
}

MidiOpCodeDelegate::~MidiOpCodeDelegate() {
}

QWidget* MidiOpCodeDelegate::createEditor(QWidget* parent,
                                          const QStyleOptionViewItem& option,
                                          const QModelIndex& index) const {
    Q_UNUSED(option);
    Q_UNUSED(index);
    QComboBox* pComboBox = new QComboBox(parent);

    QList<MidiOpCode> choices;
    choices.append(MidiOpCode::NoteOn);
    choices.append(MidiOpCode::NoteOff);
    choices.append(MidiOpCode::ControlChange);
    choices.append(MidiOpCode::PitchBendChange);

    for (const MidiOpCode choice : qAsConst(choices)) {
        pComboBox->addItem(MidiUtils::opCodeToTranslatedString(choice),
                static_cast<uint8_t>(choice));
    }
    return pComboBox;
}

QString MidiOpCodeDelegate::displayText(const QVariant& value,
                                        const QLocale& locale) const {
    Q_UNUSED(locale);
    MidiOpCode opCode = static_cast<MidiOpCode>(value.toInt());
    return MidiUtils::opCodeToTranslatedString(opCode);
}

void MidiOpCodeDelegate::setEditorData(QWidget* editor,
                                       const QModelIndex& index) const {
    int opCode = index.data(Qt::EditRole).toInt();
    QComboBox* pComboBox = qobject_cast<QComboBox*>(editor);
    if (pComboBox == nullptr) {
        return;
    }
    for (int i = 0; i < pComboBox->count(); ++i) {
        if (pComboBox->itemData(i).toInt() == opCode) {
            pComboBox->setCurrentIndex(i);
            break;
        }
    }
}

void MidiOpCodeDelegate::setModelData(QWidget* editor,
                                      QAbstractItemModel* model,
                                      const QModelIndex& index) const {
    QComboBox* pComboBox = qobject_cast<QComboBox*>(editor);
    if (pComboBox == nullptr) {
        return;
    }
    model->setData(index, pComboBox->itemData(pComboBox->currentIndex()),
                   Qt::EditRole);
}