/[svn]/qsampler/trunk/src/qsamplerInstrumentList.cpp
ViewVC logotype

Diff of /qsampler/trunk/src/qsamplerInstrumentList.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 969 by capela, Wed Dec 6 19:38:02 2006 UTC revision 1526 by capela, Mon Nov 26 10:58:23 2007 UTC
# Line 1  Line 1 
1  // qsamplerInstrumentList.cpp  // qsamplerInstrumentList.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2003-2005, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved.
5       Copyright (C) 2007, Christian Schoenebeck
6     This program is free software; you can redistribute it and/or  
7     modify it under the terms of the GNU General Public License     This program is free software; you can redistribute it and/or
8     as published by the Free Software Foundation; either version 2     modify it under the terms of the GNU General Public License
9     of the License, or (at your option) any later version.     as published by the Free Software Foundation; either version 2
10       of the License, or (at your option) any later version.
11     This program is distributed in the hope that it will be useful,  
12     but WITHOUT ANY WARRANTY; without even the implied warranty of     This program is distributed in the hope that it will be useful,
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     GNU General Public License for more details.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15       GNU General Public License for more details.
16     You should have received a copy of the GNU General Public License  
17     along with this program; if not, write to the Free Software     You should have received a copy of the GNU General Public License
18     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.     along with this program; if not, write to the Free Software
19       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *****************************************************************************/  
21    *****************************************************************************/
22  #include "qsamplerAbout.h"  
23  #include "qsamplerInstrumentList.h"  #include "qsamplerAbout.h"
24    #include "qsamplerInstrumentList.h"
25  #include "qsamplerInstrument.h"  
26  #include "qsamplerInstrumentForm.h"  #include "qsamplerInstrument.h"
27    #include "qsamplerInstrumentForm.h"
28  #include "qsamplerMainForm.h"  
29    #include "qsamplerOptions.h"
30  #include <qaction.h>  #include "qsamplerMainForm.h"
31  #include <qfileinfo.h>  
32  #include <qpopupmenu.h>  #include <QApplication>
33    #include <QMessageBox>
34  // Needed for lroundf()  #include <QMenu>
35  #include <math.h>  #include <QAction>
36    #include <QCursor>
37    #include <QFileInfo>
38  //----------------------------------------------------------------------  
39  // class qsamplerInstrumentGroup -- custom group list view item.  // Needed for lroundf()
40  //  #include <math.h>
41    
42  // Constructors.  #ifndef CONFIG_ROUND
43  qsamplerInstrumentGroup::qsamplerInstrumentGroup (  static inline long lroundf ( float x )
44          qsamplerInstrumentList *pListView, const QString& sName,  {
45          QListViewItem *pItemAfter )          if (x >= 0.0f)
46          : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())                  return long(x + 0.5f);
47  {          else
48          QListViewItem::setRenameEnabled(0, true);                  return long(x - 0.5f);
49    }
50          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  #endif
51          QListViewItem::setText(0, sName);  
52  }  using namespace QSampler;
53    
54    
55  qsamplerInstrumentGroup::qsamplerInstrumentGroup (  //-------------------------------------------------------------------------
56          qsamplerInstrumentGroup *pGroupItem, const QString& sName )  // MidiInstrumentsModel - data model for MIDI prog mappings (used for QTableView)
57          : QListViewItem(pGroupItem, sName)  //
58  {  
59          QListViewItem::setRenameEnabled(0, true);  MidiInstrumentsModel::MidiInstrumentsModel ( QObject* pParent)
60            : QAbstractTableModel(pParent)
61          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  {
62  }          m_iMidiMap = LSCP_MIDI_MAP_ALL;
63    }
64    
65  // Default destructor.  
66  qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)  int MidiInstrumentsModel::rowCount ( const QModelIndex& /*parent*/) const
67  {  {
68  }          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
69                    int n = 0;
70                    for (InstrumentsMap::const_iterator itMap = m_instruments.begin();
71  // Instance accessors.                                  itMap != m_instruments.end(); ++itMap)
72  void qsamplerInstrumentGroup::setName ( const QString& sName )                          n += (*itMap).size();
73  {                  return n;
74          QListViewItem::setText(0, sName);          }
75  }          InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
76            if (itMap == m_instruments.end()) return 0;
77            return (*itMap).size();
78  QString qsamplerInstrumentGroup::name (void) const  }
79  {  
80          return QListViewItem::text(0);  
81  }  int MidiInstrumentsModel::columnCount ( const QModelIndex& /*parent*/) const
82    {
83            return 9;
84  qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const  }
85  {  
86          QListViewItem *pParent = QListViewItem::parent();  
87          while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)  QVariant MidiInstrumentsModel::data ( const QModelIndex &index, int role ) const
88                  pParent = pParent->parent();  {
89          return static_cast<qsamplerInstrumentGroup *> (pParent);          if (!index.isValid())
90  }                  return QVariant();
91    
92            const qsamplerInstrument* pInstr = NULL;
93  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const  
94  {          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
95          return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());                  int n = 0;
96  }                  for (InstrumentsMap::const_iterator itMap = m_instruments.begin();
97                                    itMap != m_instruments.end(); ++itMap) {
98                            n += (*itMap).size();
99  // To show up whether its open or not.                          if (index.row() < n) {
100  void qsamplerInstrumentGroup::setOpen ( bool bOpen )                                  pInstr = &(*itMap)[index.row() + (*itMap).size() - n];
101  {                                  break;
102          // Set the proper pixmap of this...                          }
103          if (rtti() == qsamplerInstrumentList::Group) {                  }
104                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(          } else {
105                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));                  // resolve MIDI instrument map
106          }                  InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
107          // Open it up...                  if (itMap == m_instruments.end()) return QVariant();
108          QListViewItem::setOpen(bOpen);                  // resolve instrument in that map
109                    if (index.row() >= (*itMap).size()) return QVariant();
110          // All ancestors should be also visible.                  pInstr = &(*itMap)[index.row()];
111          if (bOpen && QListViewItem::parent())          }
112                  QListViewItem::parent()->setOpen(true);  
113  }          if (!pInstr)
114                    return QVariant();
115    
116  // To virtually distinguish between list view items.          if (role == Qt::UserRole)
117  int qsamplerInstrumentGroup::rtti (void) const                  return QVariant::fromValue((void *) pInstr);
118  {  
119          return qsamplerInstrumentList::Group;          if (role == Qt::DisplayRole) {
120  }                  switch (index.column()) {
121                            case 0: return pInstr->name();
122                            case 1: return QVariant::fromValue(pInstr->map());
123  //----------------------------------------------------------------------                          case 2: return QVariant::fromValue(pInstr->bank());
124  // class qsamplerInstrumentItem -- custom file list view item.                          case 3: return QVariant::fromValue(pInstr->prog() + 1);
125  //                          case 4: return pInstr->engineName();
126                            case 5: return pInstr->instrumentFile();
127  // Constructors.                          case 6: return QVariant::fromValue(pInstr->instrumentNr());
128  qsamplerInstrumentItem::qsamplerInstrumentItem (                          case 7: return QString::number(pInstr->volume() * 100.0) + " %";
129          qsamplerInstrumentList *pListView,                          case 8: {
130          qsamplerInstrument *pInstrument,                                  switch (pInstr->loadMode()) {
131          QListViewItem *pItemAfter )                                          case 3: return QObject::tr("Persistent");
132          : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)                                          case 2: return QObject::tr("On Demand Hold");
133  {                                          case 1: return QObject::tr("On Demand");
134          m_pInstrument = pInstrument;                                  }
135                            }
136          update();                          default: return QVariant();
137  }                  }
138            }
139  qsamplerInstrumentItem::qsamplerInstrumentItem (  
140          qsamplerInstrumentGroup *pGroupItem,          return QVariant();
141          qsamplerInstrument *pInstrument )  }
142          : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())  
143  {  
144          m_pInstrument = pInstrument;  QVariant MidiInstrumentsModel::headerData (
145            int section, Qt::Orientation orientation, int role ) const
146          update();  {
147  }          if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
148                    return QVariant();
149    
150  // Default destructor.          switch (section) {
151  qsamplerInstrumentItem::~qsamplerInstrumentItem (void)                  case 0: return tr("Name");
152  {                  case 1: return tr("Map");
153          if (m_pInstrument)                  case 2: return tr("Bank");
154                  delete m_pInstrument;                  case 3: return tr("Prog");
155  }                  case 4: return tr("Engine");
156                    case 5: return tr("File");
157                    case 6: return tr("Nr");
158  // To virtually distinguish between list view items.                  case 7: return tr("Vol");
159  int qsamplerInstrumentItem::rtti (void) const                  case 8: return tr("Mode");
160  {                  default: return QVariant();
161          return qsamplerInstrumentList::Item;          }
162  }  }
163    
164    
165  // Payload accessor.  qsamplerInstrument* MidiInstrumentsModel::addInstrument (
166  qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const          int iMap, int iBank, int iProg )
167  {  {
168          return m_pInstrument;          // Check it there's already one instrument item
169  }          // with the very same key (bank, program);
170            // if yes, just remove it without prejudice...
171            for (int i = 0; i < m_instruments[iMap].size(); i++) {
172  // Item refreshment.                  if (m_instruments[iMap][i].bank() == iBank &&
173  void qsamplerInstrumentItem::update (void)                          m_instruments[iMap][i].prog() == iProg) {
174  {                          m_instruments[iMap].removeAt(i);
175          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));                          break;
176                    }
177          const QString s = "-";          }
178          if (m_pInstrument) {  
179                  setText(0, m_pInstrument->name());          // Resolve the appropriate place, we keep the list sorted that way ...
180                  setText(1, QString::number(m_pInstrument->bank()));          int i = 0;
181                  setText(2, QString::number(m_pInstrument->program()));          for (; i < m_instruments[iMap].size(); ++i) {
182                  setText(3, m_pInstrument->engineName());                  if (iBank < m_instruments[iMap][i].bank()
183                  setText(4, QFileInfo(m_pInstrument->instrumentFile()).fileName());                          || (iBank == m_instruments[iMap][i].bank() &&
184                  setText(5, QString::number(m_pInstrument->instrumentNr()));                                  iProg < m_instruments[iMap][i].prog())) {
185                  setText(6, QString::number(::lroundf(100.0f * m_pInstrument->volume())));                          break;
186                  QString sLoadMode = s;                  }
187                  switch (m_pInstrument->loadMode()) {          }
188                  case 3:  
189                          sLoadMode = QObject::tr("Persistent");          m_instruments[iMap].insert(i, qsamplerInstrument(iMap, iBank, iProg));
190                          break;          qsamplerInstrument& instr = m_instruments[iMap][i];
191                  case 2:          if (!instr.getInstrument())
192                          sLoadMode = QObject::tr("On Demand Hold");                  m_instruments[iMap].removeAt(i);
193                          break;  
194                  case 1:          return &instr;
195                          sLoadMode = QObject::tr("On Demand");  }
196                          break;  
197                  }  
198                  setText(7, sLoadMode);  void MidiInstrumentsModel::removeInstrument (
199          } else {          const qsamplerInstrument& instrument )
200                  for (int i = 0; i < listView()->columns(); i++)  {
201                          setText(i, s);          const int iMap  = instrument.map();
202          }          const int iBank = instrument.bank();
203  }          const int iProg = instrument.prog();
204            for (int i = 0; i < m_instruments[iMap].size(); i++) {
205                    if (m_instruments[iMap][i].bank() == iBank &&
206  //----------------------------------------------------------------------------                          m_instruments[iMap][i].prog() == iProg) {
207  // qsamplerInstrumentList -- MIDI instrument list view.                          m_instruments[iMap].removeAt(i);
208  //                          break;
209                    }
210  // Constructor.          }
211  qsamplerInstrumentList::qsamplerInstrumentList (  }
212          QWidget *pParent, const char *pszName )  
213          : QListView(pParent, pszName)  
214  {  // Reposition the instrument in the model (called when map/bank/prg changed)
215  //  QListView::setRootIsDecorated(true);  void MidiInstrumentsModel::resort ( const qsamplerInstrument& instrument )
216          QListView::setResizeMode(QListView::NoColumn);  {
217  //      QListView::setAcceptDrops(true);          const int iMap  = instrument.map();
218          QListView::setDragAutoScroll(true);          const int iBank = instrument.bank();
219          QListView::setSizePolicy(          const int iProg = instrument.prog();
220                  QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));          // Remove given instrument from its current list
221  //      QListView::setShowToolTips(false);          removeInstrument(instrument);
222          QListView::setSortColumn(-1);          // Re-add the instrument
223            addInstrument(iMap, iBank, iProg);
224          QListView::addColumn(tr("Name"));  }
225          QListView::addColumn(tr("Bank"));  
226          QListView::addColumn(tr("Prog"));  
227          QListView::addColumn(tr("Engine"));  void MidiInstrumentsModel::setMidiMap ( int iMidiMap )
228          QListView::addColumn(tr("File"));  {
229          QListView::addColumn(tr("Nr"));          if (iMidiMap < 0)
230          QListView::addColumn(tr("Vol"));                  iMidiMap = LSCP_MIDI_MAP_ALL;
231          QListView::addColumn(tr("Mode"));  
232            m_iMidiMap = iMidiMap;
233          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Bank  }
234          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog  
235          QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr  
236          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol  int MidiInstrumentsModel::midiMap (void) const
237    {
238          QListView::setColumnWidth(0, 60);       // Name          return m_iMidiMap;
239          QListView::setColumnWidth(0, 120);      // File  }
240    
241          m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);  void MidiInstrumentsModel::refresh (void)
242          m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);  {
243          m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);          m_instruments.clear();
244          m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);  
245          m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);          MainForm* pMainForm = MainForm::getInstance();
246          m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), this);          if (pMainForm == NULL)
247                    return;
248          QObject::connect(m_pNewGroupAction,          if (pMainForm->client() == NULL)
249                  SIGNAL(activated()),                  return;
250                  SLOT(newGroupSlot()));  
251          QObject::connect(m_pNewItemAction,          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
252                  SIGNAL(activated()),  
253                  SLOT(newItemSlot()));          // Load the whole bunch of instrument items...
254          QObject::connect(m_pEditItemAction,          lscp_midi_instrument_t* pInstrs
255                  SIGNAL(activated()),                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
256                  SLOT(editItemSlot()));          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
257          QObject::connect(m_pRenameAction,                  const int iMap  = pInstrs[iInstr].map;
258                  SIGNAL(activated()),                  const int iBank = pInstrs[iInstr].bank;
259                  SLOT(renameSlot()));                  const int iProg = pInstrs[iInstr].prog;
260          QObject::connect(m_pDeleteAction,                  addInstrument(iMap, iBank, iProg);
261                  SIGNAL(activated()),                  // Try to keep it snappy :)
262                  SLOT(deleteSlot()));                  QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
263          QObject::connect(m_pRefreshAction,          }
264                  SIGNAL(activated()),  
265                  SLOT(refresh()));          QApplication::restoreOverrideCursor();
266    
267          QObject::connect(this,          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
268                  SIGNAL(selectionChanged()),                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
269                  SLOT(selectionChangedSlot()));                  pMainForm->appendMessagesError(
270          QObject::connect(this,                          tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
271                  SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),          }
272                  SLOT(activatedSlot(QListViewItem*)));  
273          QObject::connect(this,          // inform the outer world (QTableView) that our data changed
274                  SIGNAL(returnPressed(QListViewItem*)),          QAbstractTableModel::reset();
275                  SLOT(activatedSlot(QListViewItem*)));  }
276          QObject::connect(this,  
277                  SIGNAL(itemRenamed(QListViewItem*,int)),  
278                  SLOT(renamedSlot(QListViewItem*)));  //-------------------------------------------------------------------------
279    // MidiInstrumentsDelegate - table cell renderer for MIDI prog mappings
280          selectionChangedSlot();  // (doesn't actually do anything ATM, but is already there for a future
281  }  // cell editor widget implementation)
282    
283    MidiInstrumentsDelegate::MidiInstrumentsDelegate ( QObject* pParent )
284  // Default destructor.          : QItemDelegate(pParent)
285  qsamplerInstrumentList::~qsamplerInstrumentList (void)  {
286  {  }
287          delete m_pNewGroupAction;  
288          delete m_pNewItemAction;  
289          delete m_pEditItemAction;  QWidget* MidiInstrumentsDelegate::createEditor ( QWidget* pParent,
290          delete m_pRenameAction;          const QStyleOptionViewItem& option, const QModelIndex& index ) const
291          delete m_pDeleteAction;  {
292  }          return QItemDelegate::createEditor(pParent, option, index);
293    //      return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);
294    }
295  // Add a new instrument item, optionally under a given group.  
296  qsamplerInstrumentItem *qsamplerInstrumentList::addItem (  
297          qsamplerInstrument *pInstrument,  void MidiInstrumentsDelegate::setEditorData ( QWidget */*pEditor*/,
298          qsamplerInstrumentGroup *pParentGroup )          const QModelIndex& /*index*/) const
299  {  {
300          qsamplerInstrumentItem *pItem = findItem(pInstrument);  }
301          if (pItem == NULL) {  
302                  if (pParentGroup)  
303                          pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);  void MidiInstrumentsDelegate::setModelData ( QWidget */*pEditor*/,
304                  else          QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const
305                          pItem = new qsamplerInstrumentItem(this, pInstrument);  {
306          }  }
307          QListView::setSelected(pItem, true);  
308          return pItem;  
309  }  void MidiInstrumentsDelegate::updateEditorGeometry ( QWidget *pEditor,
310            const QStyleOptionViewItem& option, const QModelIndex& index) const
311    {
312  // Add a new instrument group, optionally under another group.          QItemDelegate::updateEditorGeometry(pEditor, option, index);
313  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (  //      if (pEditor) pEditor->setGeometry(option.rect);
314          const QString& sName, qsamplerInstrumentGroup *pParentGroup )  }
315  {  
316          qsamplerInstrumentGroup *pGroup = findGroup(sName);  
317          if (pGroup == NULL) {  // end of qsamplerInstrumentList.cpp
                 if (pParentGroup)  
                         pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);  
                 else  
                         pGroup = new qsamplerInstrumentGroup(this, sName);  
         }  
         QListView::setSelected(pGroup, true);  
         return pGroup;  
 }  
   
   
 // Find a group item, given its name.  
 qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (  
         const QString& sName ) const  
 {  
         // Iterate all over the place to search for the group.  
         QListViewItemIterator iter((QListView *) this);  
         while (iter.current()) {  
                 QListViewItem *pItem = iter.current();  
                 if (pItem->rtti() == Group && pItem->text(0) == sName)  
                         return static_cast<qsamplerInstrumentGroup *> (pItem);  
                 ++iter;  
         }  
         // Not found.  
         return NULL;  
 }  
   
   
 // Find a file item, given its name.  
 qsamplerInstrumentItem *qsamplerInstrumentList::findItem (  
         qsamplerInstrument *pInstrument ) const  
 {  
         if (pInstrument == NULL)  
                 return NULL;  
   
         // Iterate all over the place to search for the group.  
         QListViewItemIterator iter((QListView *) this);  
         while (iter.current()) {  
                 QListViewItem *pListItem = iter.current();  
                 if (pListItem->rtti() == Item) {  
                         qsamplerInstrumentItem *pItem  
                                 = static_cast<qsamplerInstrumentItem *> (pListItem);  
                         if (pItem && pItem->instrument()  
                                 && pItem->instrument()->bank() == pInstrument->bank()  
                                 && pItem->instrument()->program() == pInstrument->program())  
                                 return pItem;  
                 }  
                 ++iter;  
         }  
         // Not found.  
         return NULL;  
 }  
   
   
 // Find and return the nearest group item...  
 qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (  
         QListViewItem *pItem ) const  
 {  
         while (pItem && pItem->rtti() != Group)  
                 pItem = pItem->parent();  
         return static_cast<qsamplerInstrumentGroup *> (pItem);  
 }  
   
   
 // 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);  
         if (pNewGroup)  
                 pNewGroup->startRename(0);  
   
         selectionChangedSlot();  
 }  
   
   
 // Add a new instrument item below the current one.  
 void qsamplerInstrumentList::newItemSlot (void)  
 {  
         qsamplerInstrument *pInstrument = new qsamplerInstrument();  
   
         qsamplerInstrumentForm form(this);  
         form.setup(pInstrument);  
         if (!form.exec()) {  
                 delete pInstrument;  
                 return;  
         }  
   
         // Check it there's already one for the same key (bank, program)  
         qsamplerInstrumentItem *pItem = findItem(pInstrument);  
         if (pItem)  
                 delete pItem;  
   
         pInstrument->map();  
         emit instrumentsChanged();  
   
         qsamplerInstrumentGroup *pParentGroup  
                 = groupItem(QListView::selectedItem());  
         addItem(pInstrument, pParentGroup);  
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
   
         selectionChangedSlot();  
 }  
   
   
 // Edit current item below the current one.  
 void qsamplerInstrumentList::editItemSlot (void)  
 {  
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem == NULL)  
                 return;  
         if (pListItem->rtti() == Item) {  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         qsamplerInstrumentForm form(this);  
                         form.setup(pItem->instrument());  
                         if (form.exec()) {  
                                 pItem->instrument()->map();  
                                 emit instrumentsChanged();  
                                 pItem->update();  
                         }  
                 }  
         }  
   
         selectionChangedSlot();  
 }  
   
   
 // Rename current group/item.  
 void qsamplerInstrumentList::renameSlot (void)  
 {  
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem)  
                 pListItem->startRename(0);  
   
         selectionChangedSlot();  
 }  
   
   
 // Remove current group/item.  
 void qsamplerInstrumentList::deleteSlot (void)  
 {  
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem) {  
                 if (pListItem->rtti() == Item) {  
                         qsamplerInstrumentItem *pItem  
                                 = static_cast<qsamplerInstrumentItem *> (pListItem);  
                         if (pItem && pItem->instrument()) {  
                                 pItem->instrument()->unmap();  
                                 emit instrumentsChanged();  
                         }  
                 }  
                 delete pListItem;  
         }  
   
         selectionChangedSlot();  
 }  
   
   
 // In-place selection slot.  
 void qsamplerInstrumentList::selectionChangedSlot (void)  
 {  
         qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  
         QListViewItem *pListItem = QListView::selectedItem();  
         bool bEnabled = (pMainForm && pMainForm->client());  
         m_pNewItemAction->setEnabled(bEnabled);  
         bEnabled = (bEnabled && pListItem != NULL);  
         m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);  
         m_pRenameAction->setEnabled(bEnabled);  
         m_pDeleteAction->setEnabled(bEnabled);  
 }  
   
   
 // In-place activation slot.  
 void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  
 {  
         // FIXME: Hope the list view item is the one selected.  
         if (pListItem->rtti() == Item)  
                 editItemSlot();  
 }  
   
   
 // In-place aliasing slot.  
 void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )  
 {  
         if (pListItem->rtti() == Item) {  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         pItem->instrument()->setName(pListItem->text(0));  
                         pItem->instrument()->map();  
                         emit instrumentsChanged();  
                         pItem->update();  
                 }  
         }  
 }  
   
   
 // Context menu request event handler.  
 void qsamplerInstrumentList::contextMenuEvent (  
         QContextMenuEvent *pContextMenuEvent )  
 {  
         QPopupMenu menu(this);  
   
         // Construct context menu.  
         m_pNewItemAction->addTo(&menu);  
 //      m_pNewGroupAction->addTo(&menu);  
         menu.insertSeparator();  
         m_pEditItemAction->addTo(&menu);  
         m_pRenameAction->addTo(&menu);  
         m_pDeleteAction->addTo(&menu);  
         menu.insertSeparator();  
         m_pRefreshAction->addTo(&menu);  
   
         menu.exec(pContextMenuEvent->globalPos());  
 }  
   
   
 // General reloader.  
 void qsamplerInstrumentList::refresh (void)  
 {  
         clear();  
   
         qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  
         if (pMainForm == NULL)  
                 return;  
         if (pMainForm->client() == NULL)  
                 return;  
   
         qsamplerInstrumentItem *pItem = NULL;  
         lscp_midi_instrument_t *pInstrs  
                 = ::lscp_list_midi_instruments(pMainForm->client());  
         for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {  
                 int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;  
                 int iProgram = pInstrs[iInstr].program;  
                 qsamplerInstrument *pInstrument  
                         = new qsamplerInstrument(iBank, iProgram);  
                 if (pInstrument->get())  
                         pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);  
         }  
   
         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();  
 }  
   
   
 // end of qsamplerInstrumentList.cpp  
   

Legend:
Removed from v.969  
changed lines
  Added in v.1526

  ViewVC Help
Powered by ViewVC