~mr-unwell2006/mixxx/features_vamp

« back to all changes in this revision

Viewing changes to mixxx/src/midi/midichanneldelegate.cpp

  • Committer: Varun Jewalikar
  • Date: 2012-05-30 15:45:54 UTC
  • mfrom: (2684.18.247 mixxx-trunk)
  • Revision ID: mr.unwell2006@gmail.com-20120530154554-sh6i8m265v43gqtd
merge with the trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * midichanneledelegate.cpp
3
 
 *
4
 
 *  Created on: 1-Feb-2009
5
 
 *      Author: alb
6
 
 */
7
 
 
8
 
#include <QtCore>
9
 
#include <QtGui>
10
 
#include "midichanneldelegate.h"
11
 
 
12
 
MidiChannelDelegate::MidiChannelDelegate(QObject *parent)
13
 
         : QItemDelegate(parent)
14
 
{
15
 
}
16
 
 
17
 
void MidiChannelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
18
 
                         const QModelIndex &index) const
19
 
{
20
 
    if (index.data().canConvert<int>()) {
21
 
        int channel = index.data().value<int>();
22
 
        //Convert to natural numbers (starts at 1 instead of 0.)
23
 
        channel++;
24
 
 
25
 
        if (option.state & QStyle::State_Selected)
26
 
            painter->fillRect(option.rect, option.palette.highlight());
27
 
 
28
 
        QString text = QString("%1").arg(channel);
29
 
 
30
 
        painter->drawText(option.rect, text, QTextOption(Qt::AlignCenter));
31
 
        //Note that Qt::AlignCenter does both vertical and horizontal alignment.
32
 
    } else {
33
 
        QItemDelegate::paint(painter, option, index);
34
 
    }
35
 
}
36
 
 
37
 
QWidget *MidiChannelDelegate::createEditor(QWidget *parent,
38
 
        const QStyleOptionViewItem &/* option */,
39
 
        const QModelIndex &/* index */) const
40
 
{
41
 
    QSpinBox *editor = new QSpinBox(parent);
42
 
    editor->setMinimum(1);
43
 
    editor->setMaximum(16);
44
 
 
45
 
    return editor;
46
 
}
47
 
 
48
 
void MidiChannelDelegate::setEditorData(QWidget *editor,
49
 
                                    const QModelIndex &index) const
50
 
{
51
 
    int channel = index.model()->data(index, Qt::EditRole).toInt();
52
 
    
53
 
    //Convert the channel to natural numbers (1-16). The actual MIDI messages
54
 
    //address them as 0-15 as per the spec, but all user documentation for every
55
 
    //MIDI device on the planet refers to the channels as 1-16.
56
 
    channel++;
57
 
    
58
 
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
59
 
    spinBox->setValue(channel);
60
 
}
61
 
 
62
 
void MidiChannelDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
63
 
                                    const QModelIndex &index) const
64
 
{
65
 
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
66
 
    spinBox->interpretText();
67
 
    int channel = spinBox->value();
68
 
    channel--; //Convert the MIDI channel back into the 0-15 range.
69
 
    model->setData(index, channel, Qt::EditRole);
70
 
}
71
 
 
72
 
void MidiChannelDelegate::updateEditorGeometry(QWidget *editor,
73
 
                                           const QStyleOptionViewItem &option,
74
 
                                           const QModelIndex &/* index */) const
75
 
{
76
 
    editor->setGeometry(option.rect);
77
 
}