![]() |
Mixxx
|
00001 /* 00002 * midinodelegate.cpp 00003 * 00004 * Created on: 1-Feb-2009 00005 * Author: alb 00006 */ 00007 00008 #include <QtCore> 00009 #include <QtGui> 00010 #include "widget/hexspinbox.h" 00011 #include "midinodelegate.h" 00012 00013 MidiNoDelegate::MidiNoDelegate(QObject *parent) 00014 : QItemDelegate(parent) 00015 { 00016 } 00017 00018 void MidiNoDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, 00019 const QModelIndex &index) const 00020 { 00021 if (index.data().canConvert<int>()) { 00022 int midino = index.data().value<int>(); 00023 00024 if (option.state & QStyle::State_Selected) 00025 painter->fillRect(option.rect, option.palette.highlight()); 00026 00027 QString text = QString("0x") + QString("%1").arg(midino, 00028 2, //Field width (makes "F" become "0F") 00029 16, 00030 QLatin1Char('0')).toUpper(); 00031 00032 painter->drawText(option.rect, text, QTextOption(Qt::AlignCenter)); 00033 //Note that Qt::AlignCenter does both vertical and horizontal alignment. 00034 } else { 00035 QItemDelegate::paint(painter, option, index); 00036 } 00037 } 00038 00039 QWidget *MidiNoDelegate::createEditor(QWidget *parent, 00040 const QStyleOptionViewItem &/* option */, 00041 const QModelIndex & index ) const 00042 { 00043 HexSpinBox *editor = new HexSpinBox(parent); 00044 editor->setMinimum(0); 00045 editor->setMaximum(127); 00046 00047 return editor; 00048 } 00049 00050 void MidiNoDelegate::setEditorData(QWidget *editor, 00051 const QModelIndex &index) const 00052 { 00053 int value = index.model()->data(index, Qt::EditRole).toInt(); 00054 00055 HexSpinBox *spinBox = static_cast<HexSpinBox*>(editor); 00056 spinBox->setValue(value); 00057 spinBox->interpretText(); 00058 } 00059 00060 void MidiNoDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, 00061 const QModelIndex &index) const 00062 { 00063 HexSpinBox *spinBox = static_cast<HexSpinBox*>(editor); 00064 spinBox->interpretText(); 00065 int value = spinBox->value(); 00066 00067 model->setData(index, value, Qt::EditRole); 00068 } 00069 00070 void MidiNoDelegate::updateEditorGeometry(QWidget *editor, 00071 const QStyleOptionViewItem &option, 00072 const QModelIndex &/* index */) const 00073 { 00074 editor->setGeometry(option.rect); 00075 }