summaryrefslogtreecommitdiffstats
path: root/src/controllers/delegates/midibytedelegate.cpp
blob: 0ee71a8fb719a447d60a1f7e8100d18c4165b23c (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
#include "controllers/delegates/midibytedelegate.h"

#include "controllers/midi/midimessage.h"
#include "controllers/midi/midiutils.h"
#include "moc_midibytedelegate.cpp"
#include "widget/hexspinbox.h"

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

MidiByteDelegate::~MidiByteDelegate() {
}

QWidget* MidiByteDelegate::createEditor(QWidget* parent,
                                        const QStyleOptionViewItem& option,
                                        const QModelIndex& index) const {
    Q_UNUSED(option);
    Q_UNUSED(index);
    HexSpinBox* pSpinBox = new HexSpinBox(parent);
    pSpinBox->setRange(0x00, 0x7F);
    return pSpinBox;
}

QString MidiByteDelegate::displayText(const QVariant& value,
                                      const QLocale& locale) const {
    Q_UNUSED(locale);
    unsigned char control = static_cast<unsigned char>(value.toInt());
    return MidiUtils::formatByteAsHex(control);
}

void MidiByteDelegate::setEditorData(QWidget* editor,
                                     const QModelIndex& index) const {
    int control = index.data(Qt::EditRole).toInt();
    HexSpinBox* pSpinBox = qobject_cast<HexSpinBox*>(editor);
    if (pSpinBox == nullptr) {
        return;
    }
    pSpinBox->setValue(control);
}

void MidiByteDelegate::setModelData(QWidget* editor,
                                    QAbstractItemModel* model,
                                    const QModelIndex& index) const {
    HexSpinBox* pSpinBox = qobject_cast<HexSpinBox*>(editor);
    if (pSpinBox == nullptr) {
        return;
    }
    model->setData(index, pSpinBox->value(), Qt::EditRole);
}