--- qsampler/trunk/src/qsamplerInstrumentList.cpp 2007/11/20 16:48:04 1499 +++ qsampler/trunk/src/qsamplerInstrumentList.cpp 2007/11/22 11:10:44 1509 @@ -56,166 +56,188 @@ // MidiInstrumentsModel - data model for MIDI prog mappings (used for QTableView) // -MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) { - m_iMidiMap = LSCP_MIDI_MAP_ALL; +MidiInstrumentsModel::MidiInstrumentsModel ( QObject* pParent) + : QAbstractTableModel(pParent) +{ + m_iMidiMap = LSCP_MIDI_MAP_ALL; } -int MidiInstrumentsModel::rowCount(const QModelIndex& /*parent*/) const { - if (m_iMidiMap == LSCP_MIDI_MAP_ALL) { - int n = 0; - for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap) - n += (*itMap).size(); - return n; - } - InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap); - if (itMap == instruments.end()) return 0; - return (*itMap).size(); -} - -int MidiInstrumentsModel::columnCount(const QModelIndex& /*parent*/) const { - return 9; -} - -QVariant MidiInstrumentsModel::data(const QModelIndex &index, int role) const { - if (!index.isValid()) return QVariant(); - - const qsamplerInstrument* pInstr = NULL; - - if (m_iMidiMap == LSCP_MIDI_MAP_ALL) { - int n = 0; - for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap) { - n += (*itMap).size(); - if (index.row() < n) - pInstr = &(*itMap)[index.row() + (*itMap).size() - n]; - } - } else { - // resolve MIDI instrument map - InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap); - if (itMap == instruments.end()) return QVariant(); - // resolve instrument in that map - if (index.row() >= (*itMap).size()) return QVariant(); - pInstr = &(*itMap)[index.row()]; - } - - if (!pInstr) return QVariant(); - - if (role == Qt::UserRole) { - return QVariant::fromValue((void*)pInstr); - } - - if (role == Qt::DisplayRole) { - switch (index.column()) { - case 0: return pInstr->name(); - case 1: return QVariant::fromValue(pInstr->map()); - case 2: return QVariant::fromValue(pInstr->bank()); - case 3: return QVariant::fromValue(pInstr->prog()); - case 4: return pInstr->engineName(); - case 5: return pInstr->instrumentFile(); - case 6: return QVariant::fromValue(pInstr->instrumentNr()); - case 7: return QString::number(pInstr->volume() * 100.0) + " %"; - case 8: { - switch (pInstr->loadMode()) { - case 3: return QObject::tr("Persistent"); - case 2: return QObject::tr("On Demand Hold"); - case 1: return QObject::tr("On Demand"); - default: return QVariant(); - } - } - default: return QVariant(); - } - } - - return QVariant(); -} - -QVariant MidiInstrumentsModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (orientation != Qt::Horizontal || role != Qt::DisplayRole) - return QVariant(); - - switch (section) { - case 0: return tr("Name"); - case 1: return tr("Map"); - case 2: return tr("Bank"); - case 3: return tr("Prog"); - case 4: return tr("Engine"); - case 5: return tr("File"); - case 6: return tr("Nr"); - case 7: return tr("Vol"); - case 8: return tr("Mode"); - default: return QVariant(); - } -} - -qsamplerInstrument* MidiInstrumentsModel::addInstrument(int iMap, int iBank, int iProg) { - // Check it there's already one instrument item - // with the very same key (bank, program); - // if yes, just remove it without prejudice... - for (int i = 0; i < instruments[iMap].size(); i++) { - if ( - instruments[iMap][i].bank() == iBank && - instruments[iMap][i].prog() == iProg - ) { - instruments[iMap].removeAt(i); - break; - } - } - - // resolve the appropriate place, we keep the list sorted that way ... - int i = 0; - for (; i < instruments[iMap].size(); i++) - if ( - iBank > instruments[iMap][i].bank() || - ( iBank == instruments[iMap][i].bank() && - iProg > instruments[iMap][i].prog() ) - ) break; - - instruments[iMap].insert(i, qsamplerInstrument(iMap, iBank, iProg)); - qsamplerInstrument& instr = instruments[iMap][i]; - if (!instr.getInstrument()) - instruments[iMap].removeAt(i); - - return &instr; -} - -void MidiInstrumentsModel::removeInstrument(const qsamplerInstrument& instrument) { - const int iMap = instrument.map(); - const int iBank = instrument.bank(); - const int iProg = instrument.prog(); - for (int i = 0; i < instruments[iMap].size(); i++) { - if ( - instruments[iMap][i].bank() == iBank && - instruments[iMap][i].prog() == iProg - ) { - instruments[iMap].removeAt(i); - break; - } - } -} - -// reposition the instrument in the model (called when map/bank/prg changed) -void MidiInstrumentsModel::resort(const qsamplerInstrument instrument) { - const int iMap = instrument.map(); - const int iBank = instrument.bank(); - const int iProg = instrument.prog(); - // remove given instrument from its current list - removeInstrument(instrument); - // re-add the instrument - addInstrument(iMap, iBank, iProg); -} - -void MidiInstrumentsModel::setMidiMap(int iMidiMap) { - if (iMidiMap < 0) - iMidiMap = LSCP_MIDI_MAP_ALL; - - m_iMidiMap = iMidiMap; + +int MidiInstrumentsModel::rowCount ( const QModelIndex& /*parent*/) const +{ + if (m_iMidiMap == LSCP_MIDI_MAP_ALL) { + int n = 0; + for (InstrumentsMap::const_iterator itMap = m_instruments.begin(); + itMap != m_instruments.end(); ++itMap) + n += (*itMap).size(); + return n; + } + InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap); + if (itMap == m_instruments.end()) return 0; + return (*itMap).size(); +} + + +int MidiInstrumentsModel::columnCount ( const QModelIndex& /*parent*/) const +{ + return 9; +} + + +QVariant MidiInstrumentsModel::data ( const QModelIndex &index, int role ) const +{ + if (!index.isValid()) + return QVariant(); + + const qsamplerInstrument* pInstr = NULL; + + if (m_iMidiMap == LSCP_MIDI_MAP_ALL) { + int n = 0; + for (InstrumentsMap::const_iterator itMap = m_instruments.begin(); + itMap != m_instruments.end(); ++itMap) { + n += (*itMap).size(); + if (index.row() < n) + pInstr = &(*itMap)[index.row() + (*itMap).size() - n]; + } + } else { + // resolve MIDI instrument map + InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap); + if (itMap == m_instruments.end()) return QVariant(); + // resolve instrument in that map + if (index.row() >= (*itMap).size()) return QVariant(); + pInstr = &(*itMap)[index.row()]; + } + + if (!pInstr) + return QVariant(); + + if (role == Qt::UserRole) + return QVariant::fromValue((void *) pInstr); + + if (role == Qt::DisplayRole) { + switch (index.column()) { + case 0: return pInstr->name(); + case 1: return QVariant::fromValue(pInstr->map()); + case 2: return QVariant::fromValue(pInstr->bank()); + case 3: return QVariant::fromValue(pInstr->prog()); + case 4: return pInstr->engineName(); + case 5: return pInstr->instrumentFile(); + case 6: return QVariant::fromValue(pInstr->instrumentNr()); + case 7: return QString::number(pInstr->volume() * 100.0) + " %"; + case 8: { + switch (pInstr->loadMode()) { + case 3: return QObject::tr("Persistent"); + case 2: return QObject::tr("On Demand Hold"); + case 1: return QObject::tr("On Demand"); + default: return QVariant(); + } + } + default: return QVariant(); + } + } + + return QVariant(); } -int MidiInstrumentsModel::midiMap() const { - return m_iMidiMap; + +QVariant MidiInstrumentsModel::headerData ( + int section, Qt::Orientation orientation, int role ) const +{ + if (orientation != Qt::Horizontal || role != Qt::DisplayRole) + return QVariant(); + + switch (section) { + case 0: return tr("Name"); + case 1: return tr("Map"); + case 2: return tr("Bank"); + case 3: return tr("Prog"); + case 4: return tr("Engine"); + case 5: return tr("File"); + case 6: return tr("Nr"); + case 7: return tr("Vol"); + case 8: return tr("Mode"); + default: return QVariant(); + } } -void MidiInstrumentsModel::refresh() { - instruments.clear(); + +qsamplerInstrument* MidiInstrumentsModel::addInstrument ( + int iMap, int iBank, int iProg ) +{ + // Check it there's already one instrument item + // with the very same key (bank, program); + // if yes, just remove it without prejudice... + for (int i = 0; i < m_instruments[iMap].size(); i++) { + if (m_instruments[iMap][i].bank() == iBank && + m_instruments[iMap][i].prog() == iProg) { + m_instruments[iMap].removeAt(i); + break; + } + } + + // resolve the appropriate place, we keep the list sorted that way ... + int i = 0; + for (; i < m_instruments[iMap].size(); i++) + if (iBank > m_instruments[iMap][i].bank() + || (iBank == m_instruments[iMap][i].bank() && + iProg > m_instruments[iMap][i].prog())) + break; + + m_instruments[iMap].insert(i, qsamplerInstrument(iMap, iBank, iProg)); + qsamplerInstrument& instr = m_instruments[iMap][i]; + if (!instr.getInstrument()) + m_instruments[iMap].removeAt(i); + + return &instr; +} + + +void MidiInstrumentsModel::removeInstrument ( + const qsamplerInstrument& instrument ) +{ + const int iMap = instrument.map(); + const int iBank = instrument.bank(); + const int iProg = instrument.prog(); + for (int i = 0; i < m_instruments[iMap].size(); i++) { + if (m_instruments[iMap][i].bank() == iBank && + m_instruments[iMap][i].prog() == iProg) { + m_instruments[iMap].removeAt(i); + break; + } + } +} + + +// Reposition the instrument in the model (called when map/bank/prg changed) +void MidiInstrumentsModel::resort ( const qsamplerInstrument instrument ) +{ + const int iMap = instrument.map(); + const int iBank = instrument.bank(); + const int iProg = instrument.prog(); + // remove given instrument from its current list + removeInstrument(instrument); + // re-add the instrument + addInstrument(iMap, iBank, iProg); +} + + +void MidiInstrumentsModel::setMidiMap ( int iMidiMap ) +{ + if (iMidiMap < 0) + iMidiMap = LSCP_MIDI_MAP_ALL; + + m_iMidiMap = iMidiMap; +} + + +int MidiInstrumentsModel::midiMap (void) const +{ + return m_iMidiMap; +} + +void MidiInstrumentsModel::refresh (void) +{ + m_instruments.clear(); MainForm* pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -241,7 +263,8 @@ if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) { pMainForm->appendMessagesClient("lscp_list_midi_instruments"); - pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry.")); + pMainForm->appendMessagesError( + tr("Could not get current list of MIDI instrument mappings.\n\nSorry.")); } // inform the outer world (QTableView) that our data changed @@ -254,23 +277,37 @@ // (doesn't actually do anything ATM, but is already there for a future // cell editor widget implementation) -MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) { +MidiInstrumentsDelegate::MidiInstrumentsDelegate ( QObject* pParent ) + : QItemDelegate(pParent) +{ } -QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { - return QItemDelegate::createEditor(parent, option, index); - //return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent); + +QWidget* MidiInstrumentsDelegate::createEditor ( QWidget* pParent, + const QStyleOptionViewItem& option, const QModelIndex& index ) const +{ + return QItemDelegate::createEditor(pParent, option, index); +// return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent); } -void MidiInstrumentsDelegate::setEditorData(QWidget* /*editor*/, const QModelIndex& /*index*/) const { + +void MidiInstrumentsDelegate::setEditorData ( QWidget */*pEditor*/, + const QModelIndex& /*index*/) const +{ } -void MidiInstrumentsDelegate::setModelData(QWidget* /*editor*/, QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const { + +void MidiInstrumentsDelegate::setModelData ( QWidget */*pEditor*/, + QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const +{ } -void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { - QItemDelegate::updateEditorGeometry(editor, option, index); - //if (editor) editor->setGeometry(option.rect); + +void MidiInstrumentsDelegate::updateEditorGeometry ( QWidget *pEditor, + const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + QItemDelegate::updateEditorGeometry(pEditor, option, index); +// if (pEditor) pEditor->setGeometry(option.rect); }