Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/midi/midioptiondelegate.cpp

Go to the documentation of this file.
00001 /*
00002  * midioptiondelegate.cpp
00003  *
00004  *  Created on: 11-April-2009
00005  *      Author: asantoni
00006  */
00007 
00008 #include <QtCore>
00009 #include <QtGui>
00010 #include "mixxxcontrol.h"
00011 #include "midioptiondelegate.h"
00012 
00013 #define MIDI_OPT_STRING_NORMAL      "Normal"
00014 #define MIDI_OPT_STRING_INVERT      "Invert"
00015 #define MIDI_OPT_STRING_ROT64       "Rot64"
00016 #define MIDI_OPT_STRING_ROT64_INV   "Rot64Invert"
00017 #define MIDI_OPT_STRING_ROT64_FAST  "Rot64Fast"
00018 #define MIDI_OPT_STRING_DIFF        "Difference"
00019 #define MIDI_OPT_STRING_BUTTON      "Button"     // Button Down (!=00) and Button Up (00) events happen together
00020 #define MIDI_OPT_STRING_SWITCH      "Switch"     // Button Down (!=00) and Button Up (00) events happen seperately
00021 #define MIDI_OPT_STRING_HERC_JOG    "HercJog"    // Generic hercules wierd range correction
00022 #define MIDI_OPT_STRING_SPREAD64    "Spread64"   // Accelerated difference from 64
00023 #define MIDI_OPT_STRING_SELECTKNOB  "SelectKnob" // Relative knob which can be turned forever and outputs a signed value.
00024 #define MIDI_OPT_STRING_SOFT_TAKEOVER   "SoftTakeover"  // Prevents sudden changes when hardware & software MixxxControl value differ
00025 #define MIDI_OPT_STRING_SCRIPT      "Script"     // Maps a MIDI control to a custom MixxxScript function
00026 
00027 MidiOptionDelegate::MidiOptionDelegate(QObject *parent)
00028          : QItemDelegate(parent)
00029 {
00030 }
00031 
00032 void MidiOptionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
00033                          const QModelIndex &index) const
00034 {
00035     if (index.data().canConvert<int>()) {
00036         int midioption = index.data().value<int>();
00037         
00038         if (option.state & QStyle::State_Selected)
00039             painter->fillRect(option.rect, option.palette.highlight());
00040 
00041         QString text;
00042         switch (midioption) {
00043            case MIDI_OPT_NORMAL: text = MIDI_OPT_STRING_NORMAL; break;
00044            case MIDI_OPT_INVERT: text = MIDI_OPT_STRING_INVERT; break;
00045            case MIDI_OPT_ROT64:  text = MIDI_OPT_STRING_ROT64; break;           
00046            case MIDI_OPT_ROT64_INV: text = MIDI_OPT_STRING_ROT64_INV; break;
00047            case MIDI_OPT_ROT64_FAST: text = MIDI_OPT_STRING_ROT64_FAST; break;
00048            case MIDI_OPT_DIFF: text = MIDI_OPT_STRING_DIFF; break;
00049            case MIDI_OPT_BUTTON: text = MIDI_OPT_STRING_BUTTON; break;
00050            case MIDI_OPT_SWITCH: text = MIDI_OPT_STRING_SWITCH; break;                                                       
00051            case MIDI_OPT_HERC_JOG: text = MIDI_OPT_STRING_HERC_JOG; break;
00052            case MIDI_OPT_SPREAD64: text = MIDI_OPT_STRING_SPREAD64; break;
00053            case MIDI_OPT_SELECTKNOB: text = MIDI_OPT_STRING_SELECTKNOB; break;
00054            case MIDI_OPT_SOFT_TAKEOVER: text = MIDI_OPT_STRING_SOFT_TAKEOVER; break;
00055            case MIDI_OPT_SCRIPT: text = MIDI_OPT_STRING_SCRIPT; break;
00056            default: text = QString("%1").arg(midioption); break;                           
00057         }
00058 
00059         painter->drawText(option.rect, text, QTextOption(Qt::AlignCenter));
00060         //Note that Qt::AlignCenter does both vertical and horizontal alignment.
00061     } else {
00062         QItemDelegate::paint(painter, option, index);
00063     }
00064 }
00065 
00066 QWidget *MidiOptionDelegate::createEditor(QWidget *parent,
00067         const QStyleOptionViewItem &/* option */,
00068         const QModelIndex &/* index */) const
00069 {
00070     QComboBox *editor = new QComboBox(parent);
00071     editor->addItem(MIDI_OPT_STRING_NORMAL);
00072     editor->addItem(MIDI_OPT_STRING_INVERT);
00073     editor->addItem(MIDI_OPT_STRING_ROT64);
00074     editor->addItem(MIDI_OPT_STRING_ROT64_INV);
00075     editor->addItem(MIDI_OPT_STRING_ROT64_FAST);
00076     editor->addItem(MIDI_OPT_STRING_DIFF);
00077     editor->addItem(MIDI_OPT_STRING_BUTTON);
00078     editor->addItem(MIDI_OPT_STRING_SWITCH);
00079     editor->addItem(MIDI_OPT_STRING_HERC_JOG);
00080     editor->addItem(MIDI_OPT_STRING_SPREAD64);
00081     editor->addItem(MIDI_OPT_STRING_SELECTKNOB);
00082     editor->addItem(MIDI_OPT_STRING_SOFT_TAKEOVER);
00083     editor->addItem(MIDI_OPT_STRING_SCRIPT);
00084 
00085     return editor;
00086 }
00087 
00088 void MidiOptionDelegate::setEditorData(QWidget *editor,
00089                                     const QModelIndex &index) const
00090 {
00091     int midioption = index.model()->data(index, Qt::EditRole).toInt();
00092     int comboIdx = 0;
00093     
00094     QComboBox *comboBox = static_cast<QComboBox*>(editor);
00095     switch (midioption)
00096     {
00097         case MIDI_OPT_NORMAL:       comboIdx = 0; break;
00098         case MIDI_OPT_INVERT:       comboIdx = 1; break;
00099         case MIDI_OPT_ROT64:        comboIdx = 2; break;           
00100         case MIDI_OPT_ROT64_INV:    comboIdx = 3; break;
00101         case MIDI_OPT_ROT64_FAST:   comboIdx = 4; break;
00102         case MIDI_OPT_DIFF:         comboIdx = 5; break;
00103         case MIDI_OPT_BUTTON:       comboIdx = 6; break;
00104         case MIDI_OPT_SWITCH:       comboIdx = 7; break;                                                       
00105         case MIDI_OPT_HERC_JOG:     comboIdx = 8; break;
00106         case MIDI_OPT_SPREAD64:     comboIdx = 9; break;
00107         case MIDI_OPT_SELECTKNOB:   comboIdx = 10; break;
00108         case MIDI_OPT_SOFT_TAKEOVER:comboIdx = 11; break;      
00109         case MIDI_OPT_SCRIPT:       comboIdx = 12; break;
00110         default: comboIdx = 0; break;      
00111     }
00112     comboBox->setCurrentIndex(comboIdx);
00113 }
00114 
00115 void MidiOptionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
00116                                     const QModelIndex &index) const
00117 {
00118     int midioption = 0;
00119     QComboBox *comboBox = static_cast<QComboBox*>(editor);
00120     //comboBox->interpretText();
00121     //Get the text from the combobox and turn it into a MidiMessage integer.
00122     QString text = comboBox->currentText();
00123     // Sigh, it sucks that we can't switch() on a string...
00124     if (text == MIDI_OPT_STRING_NORMAL) //These come from the MidiOption enum (mixxxcontrol.h)
00125         midioption = MIDI_OPT_NORMAL;
00126     else if (text == MIDI_OPT_STRING_INVERT)
00127         midioption = MIDI_OPT_INVERT;
00128     else if (text == MIDI_OPT_STRING_ROT64)
00129         midioption = MIDI_OPT_ROT64;
00130     else if (text == MIDI_OPT_STRING_ROT64_INV)
00131         midioption = MIDI_OPT_ROT64_INV;
00132     else if (text == MIDI_OPT_STRING_ROT64_FAST)
00133         midioption = MIDI_OPT_ROT64_FAST;
00134     else if (text == MIDI_OPT_STRING_DIFF)
00135         midioption = MIDI_OPT_DIFF;
00136     else if (text == MIDI_OPT_STRING_BUTTON)
00137         midioption = MIDI_OPT_BUTTON;
00138     else if (text == MIDI_OPT_STRING_SWITCH)
00139         midioption = MIDI_OPT_SWITCH;
00140     else if (text == MIDI_OPT_STRING_HERC_JOG)
00141         midioption = MIDI_OPT_HERC_JOG;
00142     else if (text == MIDI_OPT_STRING_SPREAD64)
00143         midioption = MIDI_OPT_SPREAD64;
00144     else if (text == MIDI_OPT_STRING_SELECTKNOB)
00145         midioption = MIDI_OPT_SELECTKNOB;
00146     else if (text == MIDI_OPT_STRING_SOFT_TAKEOVER)
00147         midioption = MIDI_OPT_SOFT_TAKEOVER;
00148     else if (text == MIDI_OPT_STRING_SCRIPT)
00149         midioption = MIDI_OPT_SCRIPT;
00150     model->setData(index, midioption, Qt::EditRole);
00151 }
00152 
00153 void MidiOptionDelegate::updateEditorGeometry(QWidget *editor,
00154                                            const QStyleOptionViewItem &option,
00155                                            const QModelIndex &/* index */) const
00156 {
00157     editor->setGeometry(option.rect);
00158 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines