![]() |
Mixxx
|
00001 /* 00002 * midistatusdelegate.cpp 00003 * 00004 * Created on: 1-Feb-2009 00005 * Author: alb 00006 */ 00007 00008 #include <QtCore> 00009 #include <QtGui> 00010 #include "midimessage.h" 00011 #include "midistatusdelegate.h" 00012 00013 #define MIDISTATUS_STRING_NOTE_ON "Note On" 00014 #define MIDISTATUS_STRING_NOTE_OFF "Note Off" 00015 #define MIDISTATUS_STRING_CTRL "CC" 00016 #define MIDISTATUS_STRING_PITCH "Pitch CC" 00017 00018 00019 MidiStatusDelegate::MidiStatusDelegate(QObject *parent) 00020 : QItemDelegate(parent) 00021 { 00022 } 00023 00024 void MidiStatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, 00025 const QModelIndex &index) const 00026 { 00027 if (index.data().canConvert<int>()) { 00028 int status = index.data().value<int>(); 00029 00030 //Throw away the channel bits (low nibble). 00031 status &= 0xF0; 00032 00033 if (option.state & QStyle::State_Selected) 00034 painter->fillRect(option.rect, option.palette.highlight()); 00035 00036 QString text; 00037 if (status == MIDI_STATUS_NOTE_ON) //These come from the MidiStatusByte enum (midimessage.h) 00038 text = tr(MIDISTATUS_STRING_NOTE_ON); 00039 else if (status == MIDI_STATUS_NOTE_OFF) 00040 text = tr(MIDISTATUS_STRING_NOTE_OFF); 00041 else if (status == MIDI_STATUS_CC) 00042 text = tr(MIDISTATUS_STRING_CTRL); 00043 else if (status == MIDI_STATUS_PITCH_BEND) 00044 text = tr(MIDISTATUS_STRING_PITCH); 00045 else 00046 text = tr("Unknown") + " (0x" + QString::number(status, 16) + ")"; 00047 00048 painter->drawText(option.rect, text, QTextOption(Qt::AlignCenter)); 00049 //Note that Qt::AlignCenter does both vertical and horizontal alignment. 00050 } else { 00051 QItemDelegate::paint(painter, option, index); 00052 } 00053 } 00054 00055 QWidget *MidiStatusDelegate::createEditor(QWidget *parent, 00056 const QStyleOptionViewItem &/* option */, 00057 const QModelIndex &/* index */) const 00058 { 00059 QComboBox *editor = new QComboBox(parent); 00060 editor->addItem(tr(MIDISTATUS_STRING_NOTE_ON)); 00061 editor->addItem(tr(MIDISTATUS_STRING_NOTE_OFF)); 00062 editor->addItem(tr(MIDISTATUS_STRING_CTRL)); 00063 editor->addItem(tr(MIDISTATUS_STRING_PITCH)); 00064 00065 return editor; 00066 } 00067 00068 void MidiStatusDelegate::setEditorData(QWidget *editor, 00069 const QModelIndex &index) const 00070 { 00071 int status = index.model()->data(index, Qt::EditRole).toInt(); 00072 int comboIdx = 0; 00073 00074 //Throw away the channel bits (low nibble). 00075 status &= 0xF0; 00076 00077 QComboBox *comboBox = static_cast<QComboBox*>(editor); 00078 switch (status) 00079 { 00080 case MIDI_STATUS_NOTE_ON: 00081 comboIdx = 0; 00082 break; 00083 case MIDI_STATUS_NOTE_OFF: 00084 comboIdx = 1; 00085 break; 00086 case MIDI_STATUS_CC: 00087 comboIdx = 2; 00088 break; 00089 case MIDI_STATUS_PITCH_BEND: 00090 comboIdx = 3; 00091 break; 00092 } 00093 comboBox->setCurrentIndex(comboIdx); 00094 } 00095 00096 void MidiStatusDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, 00097 const QModelIndex &index) const 00098 { 00099 int midiStatus = 0; 00100 QComboBox *comboBox = static_cast<QComboBox*>(editor); 00101 //comboBox->interpretText(); 00102 //Get the text from the combobox and turn it into a MidiMessage integer. 00103 QString text = comboBox->currentText(); 00104 if (text == tr(MIDISTATUS_STRING_NOTE_ON)) //These come from the MidiStatusByte enum (midimessage.h) 00105 midiStatus = MIDI_STATUS_NOTE_ON; 00106 else if (text == tr(MIDISTATUS_STRING_NOTE_OFF)) 00107 midiStatus = MIDI_STATUS_NOTE_OFF; 00108 else if (text == tr(MIDISTATUS_STRING_CTRL)) 00109 midiStatus = MIDI_STATUS_CC; 00110 else if (text == tr(MIDISTATUS_STRING_PITCH)) 00111 midiStatus = MIDI_STATUS_PITCH_BEND; 00112 00113 model->setData(index, midiStatus, Qt::EditRole); 00114 } 00115 00116 void MidiStatusDelegate::updateEditorGeometry(QWidget *editor, 00117 const QStyleOptionViewItem &option, 00118 const QModelIndex &/* index */) const 00119 { 00120 editor->setGeometry(option.rect); 00121 }