--- qsampler/trunk/src/qsamplerInstrumentList.cpp 2007/01/08 19:10:42 1015 +++ qsampler/trunk/src/qsamplerInstrumentList.cpp 2007/11/01 17:14:21 1464 @@ -2,6 +2,7 @@ // /**************************************************************************** Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2007, Christian Schoenebeck This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -34,7 +35,8 @@ #include #include #include -#include + +#include // Needed for lroundf() #include @@ -49,11 +51,13 @@ } #endif +using namespace QSampler; //---------------------------------------------------------------------- // class qsamplerInstrumentGroup -- custom group list view item. // +#if 0 // Constructors. qsamplerInstrumentGroup::qsamplerInstrumentGroup ( qsamplerInstrumentList *pListView, const QString& sName, @@ -62,7 +66,7 @@ { QListViewItem::setRenameEnabled(0, true); - QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png")); + QListViewItem::setPixmap(0, QPixmap(":/icons/itemGroup.png")); QListViewItem::setText(0, sName); } @@ -73,7 +77,7 @@ { QListViewItem::setRenameEnabled(0, true); - QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png")); + QListViewItem::setPixmap(0, QPixmap(":/icons/itemGroup.png")); } @@ -116,8 +120,8 @@ { // Set the proper pixmap of this... if (rtti() == qsamplerInstrumentList::Group) { - QListViewItem::setPixmap(0, QPixmap::fromMimeSource( - bOpen ? "itemGroupOpen.png" : "itemGroup.png")); + QListViewItem::setPixmap(0, QPixmap(bOpen ? + ":/icons/itemGroupOpen.png" : ":/icons/itemGroup.png")); } // Open it up... QListViewItem::setOpen(bOpen); @@ -187,7 +191,7 @@ // Item refreshment. void qsamplerInstrumentItem::update (void) { - QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png")); + QListViewItem::setPixmap(0, QPixmap(":/icons/itemFile.png")); const QString s = "-"; if (m_pInstrument) { @@ -260,20 +264,20 @@ QListView::setColumnWidth(5, 240); // File m_pNewGroupAction = new QAction( - QIconSet(QPixmap::fromMimeSource("itemGroupNew.png")), + QIcon(":/icons/itemGroupNew.png"), tr("New &Group"), tr("Ctrl+G"), this); m_pNewItemAction = new QAction( - QIconSet(QPixmap::fromMimeSource("itemNew.png")), + QIcon(":/icons/itemNew.png"), tr("New &Instrument..."), tr("Ins"), this); m_pEditItemAction = new QAction( - QIconSet(QPixmap::fromMimeSource("formEdit.png")), + QIcon(":/icons/formEdit.png"), tr("&Edit..."), tr("Enter"), this); m_pRenameAction = new QAction(tr("&Rename"), tr("F2"), this); m_pDeleteAction = new QAction( - QIconSet(QPixmap::fromMimeSource("formRemove.png")), + QIcon(":/icons/formRemove.png"), tr("&Delete"), tr("Del"), this); m_pRefreshAction = new QAction( - QIconSet(QPixmap::fromMimeSource("formRefresh.png")), + QIcon(":/icons/formRefresh.png"), tr("Re&fresh"), tr("F5"), this); m_pNewGroupAction->setToolTip(tr("New Group")); @@ -532,9 +536,7 @@ pInstrument = pItem->instrument(); if (pInstrument) { // Save current key values... - int iMap = pInstrument->map(); - int iBank = pInstrument->bank(); - int iProg = pInstrument->prog(); + qsamplerInstrument oldInstrument(*pInstrument); // Do the edit dance... qsamplerInstrumentForm form(this); form.setup(pInstrument); @@ -542,12 +544,14 @@ // Commit... pInstrument->mapInstrument(); // Check whether we changed instrument key... - if (iMap == pInstrument->map() && - iBank == pInstrument->bank() && - iProg == pInstrument->prog()) { + if (oldInstrument.map() == pInstrument->map() && + oldInstrument.bank() == pInstrument->bank() && + oldInstrument.prog() == pInstrument->prog()) { // just update tree item... pItem->update(); } else { + // Unmap old instance... + oldInstrument.unmapInstrument(); // Change item tree, whether applicable... if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) { // Add new brand item into view... @@ -638,7 +642,7 @@ void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem ) { // FIXME: Hope the list view item is the one selected. - if (pListItem->rtti() == Item) + if (pListItem && pListItem->rtti() == Item) editItemSlot(); } @@ -720,7 +724,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; +} -// end of qsamplerInstrumentList.cpp +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