![]() |
Mixxx
|
00001 /* 00002 * midichanneledelegate.cpp 00003 * 00004 * Created on: 1-Feb-2009 00005 * Author: alb 00006 */ 00007 00008 #include <QtCore> 00009 #include <QtGui> 00010 #include "midichanneldelegate.h" 00011 00012 MidiChannelDelegate::MidiChannelDelegate(QObject *parent) 00013 : QItemDelegate(parent) 00014 { 00015 } 00016 00017 void MidiChannelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, 00018 const QModelIndex &index) const 00019 { 00020 if (index.data().canConvert<int>()) { 00021 int channel = index.data().value<int>(); 00022 //Convert to natural numbers (starts at 1 instead of 0.) 00023 channel++; 00024 00025 if (option.state & QStyle::State_Selected) 00026 painter->fillRect(option.rect, option.palette.highlight()); 00027 00028 QString text = QString("%1").arg(channel); 00029 00030 painter->drawText(option.rect, text, QTextOption(Qt::AlignCenter)); 00031 //Note that Qt::AlignCenter does both vertical and horizontal alignment. 00032 } else { 00033 QItemDelegate::paint(painter, option, index); 00034 } 00035 } 00036 00037 QWidget *MidiChannelDelegate::createEditor(QWidget *parent, 00038 const QStyleOptionViewItem &/* option */, 00039 const QModelIndex &/* index */) const 00040 { 00041 QSpinBox *editor = new QSpinBox(parent); 00042 editor->setMinimum(1); 00043 editor->setMaximum(16); 00044 00045 return editor; 00046 } 00047 00048 void MidiChannelDelegate::setEditorData(QWidget *editor, 00049 const QModelIndex &index) const 00050 { 00051 int channel = index.model()->data(index, Qt::EditRole).toInt(); 00052 00053 //Convert the channel to natural numbers (1-16). The actual MIDI messages 00054 //address them as 0-15 as per the spec, but all user documentation for every 00055 //MIDI device on the planet refers to the channels as 1-16. 00056 channel++; 00057 00058 QSpinBox *spinBox = static_cast<QSpinBox*>(editor); 00059 spinBox->setValue(channel); 00060 } 00061 00062 void MidiChannelDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, 00063 const QModelIndex &index) const 00064 { 00065 QSpinBox *spinBox = static_cast<QSpinBox*>(editor); 00066 spinBox->interpretText(); 00067 int channel = spinBox->value(); 00068 channel--; //Convert the MIDI channel back into the 0-15 range. 00069 model->setData(index, channel, Qt::EditRole); 00070 } 00071 00072 void MidiChannelDelegate::updateEditorGeometry(QWidget *editor, 00073 const QStyleOptionViewItem &option, 00074 const QModelIndex &/* index */) const 00075 { 00076 editor->setGeometry(option.rect); 00077 }