summaryrefslogtreecommitdiffstats
path: root/src/controlgroupdelegate.cpp
blob: dd629e83d50c0133f3a852b8c8e92518194e5f55 (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
/*
 * controlgroupdelegate.cpp
 *
 *  Created on: 18-Mar-2009
 *      Author: Albert Santoni
 */

#include "configobject.h"
//Need this to know MIDIINPUTTABLEINDEX_CONTROLOBJECTVALUE
#include "controllers/midi/midiinputmappingtablemodel.h"
#include "controlvaluedelegate.h"
#include "controlgroupdelegate.h"

/** Static variable */
QStringList ControlGroupDelegate::m_controlGroups;

ControlGroupDelegate::ControlGroupDelegate(QObject *parent)
         : QItemDelegate(parent)
{
    //This QList is static, so it's shared across all objects of this class. We only want to
    //fill it once then...
    if (m_controlGroups.isEmpty())
    {
        m_controlGroups.append(CONTROLGROUP_CHANNEL1_STRING);
        m_controlGroups.append(CONTROLGROUP_CHANNEL2_STRING);
        m_controlGroups.append(CONTROLGROUP_SAMPLER1_STRING);
        m_controlGroups.append(CONTROLGROUP_SAMPLER2_STRING);
        m_controlGroups.append(CONTROLGROUP_SAMPLER3_STRING);
        m_controlGroups.append(CONTROLGROUP_SAMPLER4_STRING);
        m_controlGroups.append(CONTROLGROUP_MASTER_STRING);
        m_controlGroups.append(CONTROLGROUP_PLAYLIST_STRING);
        m_controlGroups.append(CONTROLGROUP_FLANGER_STRING);
        m_controlGroups.append(CONTROLGROUP_MICROPHONE_STRING);
    }
}

void ControlGroupDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
    if (index.data().canConvert<QString>()) {
        QString value = index.data().value<QString>();

        if (option.state & QStyle::State_Selected)
            painter->fillRect(option.rect, option.palette.highlight());

        //starRating.paint(painter, option.rect, option.palette,
        //                 StarRating::ReadOnly);

        QString text;
        text = value;

        painter->drawText(option.rect, text, QTextOption(Qt::AlignCenter));
        //Note that Qt::AlignCenter does both vertical and horizontal alignment.
    } else {
        QItemDelegate::paint(painter, option, index);
    }
}

QWidget *ControlGroupDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &/* option */,
        const QModelIndex &/* index */) const
{
    QComboBox *editor = new QComboBox(parent);
    editor->addItem(CONTROLGROUP_CHANNEL1_STRING);
    editor->addItem(CONTROLGROUP_CHANNEL2_STRING);
    editor->addItem(CONTROLGROUP_SAMPLER1_STRING);
    editor->addItem(CONTROLGROUP_SAMPLER2_STRING);
    editor->addItem(CONTROLGROUP_SAMPLER3_STRING);
    editor->addItem(CONTROLGROUP_SAMPLER4_STRING);
    editor->addItem(CONTROLGROUP_MASTER_STRING);
    editor->addItem(CONTROLGROUP_PLAYLIST_STRING);
    editor->addItem(CONTROLGROUP_FLANGER_STRING);
    editor->addItem(CONTROLGROUP_MICROPHONE_STRING);

    return editor;
}

void ControlGroupDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();

    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    comboBox->setCurrentIndex(comboBox->findText(value));
}

void ControlGroupDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const
{
    QString midiType = 0;
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    //comboBox->interpretText();

    //Get the text from the combobox and shoot it into the data model.
    QString group = comboBox->currentText();

    model->setData(index, group, Qt::EditRole);

    //Verify that the ControlValue in the next column is valid for the
    //newly selected ControlGroup. For example, switching from "[Channel1]"
    //to "[Master]" means that a ControlValue of "play" is no longer valid.
    //If it isn't, then blank that column's value.
    QModelIndex nextDoor = index.sibling(index.row(), MIDIINPUTTABLEINDEX_CONTROLOBJECTVALUE);
    ControlValueDelegate::verifyControlValueValidity(group, model, nextDoor);
}

void ControlGroupDelegate::updateEditorGeometry(QWidget *editor,
                                           const QStyleOptionViewItem &option,
                                           const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}