2
* midichanneledelegate.cpp
4
* Created on: 1-Feb-2009
10
#include "midichanneldelegate.h"
12
MidiChannelDelegate::MidiChannelDelegate(QObject *parent)
13
: QItemDelegate(parent)
17
void MidiChannelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
18
const QModelIndex &index) const
20
if (index.data().canConvert<int>()) {
21
int channel = index.data().value<int>();
22
//Convert to natural numbers (starts at 1 instead of 0.)
25
if (option.state & QStyle::State_Selected)
26
painter->fillRect(option.rect, option.palette.highlight());
28
QString text = QString("%1").arg(channel);
30
painter->drawText(option.rect, text, QTextOption(Qt::AlignCenter));
31
//Note that Qt::AlignCenter does both vertical and horizontal alignment.
33
QItemDelegate::paint(painter, option, index);
37
QWidget *MidiChannelDelegate::createEditor(QWidget *parent,
38
const QStyleOptionViewItem &/* option */,
39
const QModelIndex &/* index */) const
41
QSpinBox *editor = new QSpinBox(parent);
42
editor->setMinimum(1);
43
editor->setMaximum(16);
48
void MidiChannelDelegate::setEditorData(QWidget *editor,
49
const QModelIndex &index) const
51
int channel = index.model()->data(index, Qt::EditRole).toInt();
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.
58
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
59
spinBox->setValue(channel);
62
void MidiChannelDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
63
const QModelIndex &index) const
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);
72
void MidiChannelDelegate::updateEditorGeometry(QWidget *editor,
73
const QStyleOptionViewItem &option,
74
const QModelIndex &/* index */) const
76
editor->setGeometry(option.rect);