--- qsampler/trunk/src/qsamplerInstrumentList.cpp 2007/06/13 21:54:07 1234 +++ qsampler/trunk/src/qsamplerInstrumentList.cpp 2007/10/28 23:30:36 1461 @@ -34,7 +34,8 @@ #include #include #include -#include + +#include // Needed for lroundf() #include @@ -49,11 +50,13 @@ } #endif +using namespace QSampler; //---------------------------------------------------------------------- // class qsamplerInstrumentGroup -- custom group list view item. // +#if 0 // Constructors. qsamplerInstrumentGroup::qsamplerInstrumentGroup ( qsamplerInstrumentList *pListView, const QString& sName, @@ -720,7 +723,161 @@ selectionChangedSlot(); } +#endif + +MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) { + 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() || role != Qt::DisplayRole) return QVariant(); + + 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) + return QVariant::fromValue( + (*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(); + return QVariant::fromValue( + (*itMap)[index.row()] + ); + } + + 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; + + qsamplerInstrument& instr = instruments[iMap][i] = qsamplerInstrument(iMap, iBank, iProg); + + return &instr; +} + +void MidiInstrumentsModel::setMidiMap(int iMidiMap) { + if (iMidiMap < 0) + iMidiMap = LSCP_MIDI_MAP_ALL; + + m_iMidiMap = iMidiMap; +} +int MidiInstrumentsModel::midiMap() const { + return m_iMidiMap; +} + +void MidiInstrumentsModel::refresh() { + instruments.clear(); + + MainForm* pMainForm = MainForm::getInstance(); + if (pMainForm == NULL) + return; + if (pMainForm->client() == NULL) + return; + + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + + // Load the whole bunch of instrument items... + lscp_midi_instrument_t* pInstrs + = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap); + for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) { + const int iMap = pInstrs[iInstr].map; + const int iBank = pInstrs[iInstr].bank; + const int iProg = pInstrs[iInstr].prog; + addInstrument(iMap, iBank, iProg); + // Try to keep it snappy :) + QApplication::processEvents(QEventLoop::ExcludeUserInput); + } + + QApplication::restoreOverrideCursor(); + + 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.")); + } + + //selectionChangedSlot(); +} + + +MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) { +} + +QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { + return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent); +} + +void MidiInstrumentsDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { +} + +void MidiInstrumentsDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { +} + +void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { + if (editor) editor->setGeometry(option.rect); +} // end of qsamplerInstrumentList.cpp