--- qsampler/trunk/src/qsamplerInstrumentList.cpp 2006/12/19 11:19:55 987 +++ qsampler/trunk/src/qsamplerInstrumentList.cpp 2007/06/13 21:54:07 1234 @@ -1,7 +1,7 @@ // qsamplerInstrumentList.cpp // /**************************************************************************** - Copyright (C) 2003-2005, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -28,8 +28,11 @@ #include "qsamplerOptions.h" #include "qsamplerMainForm.h" +#include #include +#include #include +#include #include #include @@ -225,7 +228,10 @@ QWidget *pParent, const char *pszName ) : QListView(pParent, pszName) { + m_iMidiMap = LSCP_MIDI_MAP_ALL; + // QListView::setRootIsDecorated(true); + QListView::setAllColumnsShowFocus(true); QListView::setResizeMode(QListView::NoColumn); // QListView::setAcceptDrops(true); QListView::setDragAutoScroll(true); @@ -253,12 +259,29 @@ QListView::setColumnWidth(0, 120); // Name QListView::setColumnWidth(5, 240); // File - m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this); - m_pNewItemAction = new QAction(tr("New &Instrument..."), tr("Ins"), this); - m_pEditItemAction = new QAction(tr("&Edit..."), tr("Enter"), this); + m_pNewGroupAction = new QAction( + QIconSet(QPixmap::fromMimeSource("itemGroupNew.png")), + tr("New &Group"), tr("Ctrl+G"), this); + m_pNewItemAction = new QAction( + QIconSet(QPixmap::fromMimeSource("itemNew.png")), + tr("New &Instrument..."), tr("Ins"), this); + m_pEditItemAction = new QAction( + QIconSet(QPixmap::fromMimeSource("formEdit.png")), + tr("&Edit..."), tr("Enter"), this); m_pRenameAction = new QAction(tr("&Rename"), tr("F2"), this); - m_pDeleteAction = new QAction(tr("&Delete"), tr("Del"), this); - m_pRefreshAction = new QAction(tr("Re&fresh"), tr("F5"), this); + m_pDeleteAction = new QAction( + QIconSet(QPixmap::fromMimeSource("formRemove.png")), + tr("&Delete"), tr("Del"), this); + m_pRefreshAction = new QAction( + QIconSet(QPixmap::fromMimeSource("formRefresh.png")), + tr("Re&fresh"), tr("F5"), this); + + m_pNewGroupAction->setToolTip(tr("New Group")); + m_pNewItemAction->setToolTip(tr("New Instrument")); + m_pEditItemAction->setToolTip(tr("Edit")); + m_pRenameAction->setToolTip(tr("Rename")); + m_pDeleteAction->setToolTip(tr("Delete")); + m_pRefreshAction->setToolTip(tr("Refresh")); QObject::connect(m_pNewGroupAction, SIGNAL(activated()), @@ -312,14 +335,31 @@ qsamplerInstrument *pInstrument, qsamplerInstrumentGroup *pParentGroup ) { + // Check it there's already one instrument item + // with the very same key (bank, program); + // if yes, just remove it without prejudice... qsamplerInstrumentItem *pItem = findItem(pInstrument); - if (pItem == NULL) { - if (pParentGroup) - pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument); - else - pItem = new qsamplerInstrumentItem(this, pInstrument); + if (pItem) { + // If exactly the same, just update view and bail out... + if (pItem->instrument() == pInstrument) { + pItem->update(); + return pItem; + } + // Remove it, as instrument keys must be unique. + delete pItem; } + + // Add the new item under proper group one, if any... + if (pParentGroup) { + pParentGroup->setOpen(true); + pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument); + } else { + pItem = new qsamplerInstrumentItem(this, pInstrument); + } + + // Set it as current selection... QListView::setSelected(pItem, true); + return pItem; } @@ -330,10 +370,12 @@ { qsamplerInstrumentGroup *pGroup = findGroup(sName); if (pGroup == NULL) { - if (pParentGroup) + if (pParentGroup) { + pParentGroup->setOpen(true); pGroup = new qsamplerInstrumentGroup(pParentGroup, sName); - else + } else { pGroup = new qsamplerInstrumentGroup(this, sName); + } } QListView::setSelected(pGroup, true); return pGroup; @@ -397,12 +439,8 @@ // Add a new group item below the current one. void qsamplerInstrumentList::newGroupSlot (void) { - qsamplerInstrumentGroup *pParentGroup - = groupItem(QListView::selectedItem()); qsamplerInstrumentGroup *pNewGroup - = addGroup(tr("New Group"), pParentGroup); - if (pParentGroup) - pParentGroup->setOpen(true); + = addGroup(tr("New Group"), groupItem(QListView::selectedItem())); if (pNewGroup) pNewGroup->startRename(0); @@ -410,6 +448,53 @@ } +// Map selector. +void qsamplerInstrumentList::setMidiMap ( int iMidiMap ) +{ + if (iMidiMap < 0) + iMidiMap = LSCP_MIDI_MAP_ALL; + + m_iMidiMap = iMidiMap; +} + +int qsamplerInstrumentList::midiMap (void) const +{ + return m_iMidiMap; +} + + +// List actions accessors. +QAction *qsamplerInstrumentList::newGroupAction (void) const +{ + return m_pNewGroupAction; +} + +QAction *qsamplerInstrumentList::newItemAction (void) const +{ + return m_pNewItemAction; +} + +QAction *qsamplerInstrumentList::editItemAction (void) const +{ + return m_pEditItemAction; +} + +QAction *qsamplerInstrumentList::renameAction (void) const +{ + return m_pRenameAction; +} + +QAction *qsamplerInstrumentList::deleteAction (void) const +{ + return m_pDeleteAction; +} + +QAction *qsamplerInstrumentList::refreshAction (void) const +{ + return m_pRefreshAction; +} + + // Add a new instrument item below the current one. void qsamplerInstrumentList::newItemSlot (void) { @@ -422,22 +507,13 @@ return; } - // Check it there's already one instrument item - // with the very same key (bank, program); - // if yes, just remove it without prejudice... - qsamplerInstrumentItem *pItem = findItem(pInstrument); - if (pItem) - delete pItem; - + // Commit... pInstrument->mapInstrument(); + // add new item to the tree... + addItem(pInstrument, groupItem(QListView::selectedItem())); + // Notify we've changes... emit instrumentsChanged(); - qsamplerInstrumentGroup *pParentGroup - = groupItem(QListView::selectedItem()); - addItem(pInstrument, pParentGroup); - if (pParentGroup) - pParentGroup->setOpen(true); - selectionChangedSlot(); } @@ -449,15 +525,40 @@ if (pListItem == NULL) return; if (pListItem->rtti() == Item) { + qsamplerInstrument *pInstrument = NULL; qsamplerInstrumentItem *pItem = static_cast (pListItem); - if (pItem && pItem->instrument()) { + if (pItem) + pInstrument = pItem->instrument(); + if (pInstrument) { + // Save current key values... + qsamplerInstrument oldInstrument(*pInstrument); + // Do the edit dance... qsamplerInstrumentForm form(this); - form.setup(pItem->instrument()); + form.setup(pInstrument); if (form.exec()) { - pItem->instrument()->mapInstrument(); + // Commit... + pInstrument->mapInstrument(); + // Check whether we changed instrument key... + 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... + addItem(pInstrument, groupItem(pListItem)); + } else { + // Just remove/hide old one. + delete pItem; + } + } + // Notify we've changes... emit instrumentsChanged(); - pItem->update(); } } } @@ -537,7 +638,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(); } @@ -592,9 +693,12 @@ if (pMainForm->client() == NULL) return; + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + + // Load the whole bunch of instrument items... qsamplerInstrumentItem *pItem = NULL; lscp_midi_instrument_t *pInstrs - = ::lscp_list_midi_instruments(pMainForm->client(), LSCP_MIDI_MAP_ALL); + = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap); for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) { int iMap = pInstrs[iInstr].map; int iBank = pInstrs[iInstr].bank; @@ -603,8 +707,12 @@ = new qsamplerInstrument(iMap, iBank, iProg); if (pInstrument->getInstrument()) pItem = new qsamplerInstrumentItem(this, pInstrument, pItem); + // Try to keep it snappy :) + QApplication::eventLoop()->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."));