/[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 987 by capela, Tue Dec 19 11:19:55 2006 UTC revision 2064 by capela, Fri Mar 12 16:02:32 2010 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-2010, rncbc aka Rui Nuno Capela. All rights reserved.
5       Copyright (C) 2007, Christian Schoenebeck
6    
7     This program is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License     modify it under the terms of the GNU General Public License
# Line 23  Line 24 
24  #include "qsamplerInstrumentList.h"  #include "qsamplerInstrumentList.h"
25    
26  #include "qsamplerInstrument.h"  #include "qsamplerInstrument.h"
 #include "qsamplerInstrumentForm.h"  
27    
28  #include "qsamplerOptions.h"  #include "qsamplerOptions.h"
29  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
30    
31  #include <qmessagebox.h>  #include <QApplication>
32  #include <qaction.h>  #include <QCursor>
 #include <qfileinfo.h>  
 #include <qpopupmenu.h>  
33    
 // Needed for lroundf()  
 #include <math.h>  
34    
35  #ifndef CONFIG_ROUND  namespace QSampler {
 static inline long lroundf ( float x )  
 {  
         if (x >= 0.0f)  
                 return long(x + 0.5f);  
         else  
                 return long(x - 0.5f);  
 }  
 #endif  
36    
37    //-------------------------------------------------------------------------
38  //----------------------------------------------------------------------  // QSampler::InstrumentListModel - data model for MIDI prog mappings
 // class qsamplerInstrumentGroup -- custom group list view item.  
39  //  //
40    
41  // Constructors.  InstrumentListModel::InstrumentListModel ( QObject *pParent )
42  qsamplerInstrumentGroup::qsamplerInstrumentGroup (          : QAbstractItemModel(pParent)
         qsamplerInstrumentList *pListView, const QString& sName,  
         QListViewItem *pItemAfter )  
         : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())  
43  {  {
44          QListViewItem::setRenameEnabled(0, true);          m_iMidiMap = LSCP_MIDI_MAP_ALL;
45    
46          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));          QAbstractItemModel::reset();
         QListViewItem::setText(0, sName);  
47  }  }
48    
49    InstrumentListModel::~InstrumentListModel (void)
 qsamplerInstrumentGroup::qsamplerInstrumentGroup (  
         qsamplerInstrumentGroup *pGroupItem, const QString& sName )  
         : QListViewItem(pGroupItem, sName)  
50  {  {
51          QListViewItem::setRenameEnabled(0, true);          clear();
   
         QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  
52  }  }
53    
54    
55  // Default destructor.  int InstrumentListModel::rowCount ( const QModelIndex& /*parent*/) const
 qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)  
56  {  {
57  }          int nrows = 0;
58    
59            if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
60                    InstrumentMap::const_iterator itMap = m_instruments.constBegin();
61                    for ( ; itMap != m_instruments.constEnd(); ++itMap)
62                            nrows += (*itMap).size();
63            } else {
64                    InstrumentMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
65                    if (itMap != m_instruments.constEnd())
66                            nrows += (*itMap).size();
67            }
68    
69  // Instance accessors.          return nrows;
 void qsamplerInstrumentGroup::setName ( const QString& sName )  
 {  
         QListViewItem::setText(0, sName);  
70  }  }
71    
72    
73  QString qsamplerInstrumentGroup::name (void) const  int InstrumentListModel::columnCount ( const QModelIndex& /*parent*/) const
74  {  {
75          return QListViewItem::text(0);          return 9;
76  }  }
77    
78    
79  qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const  QVariant InstrumentListModel::data (
80            const QModelIndex &index, int role ) const
81  {  {
82          QListViewItem *pParent = QListViewItem::parent();          if (!index.isValid())
83          while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)                  return QVariant();
                 pParent = pParent->parent();  
         return static_cast<qsamplerInstrumentGroup *> (pParent);  
 }  
84    
85            const Instrument *pInstr
86                    = static_cast<Instrument *> (index.internalPointer());
87    
88  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const          if (pInstr && role == Qt::DisplayRole) {
89  {                  switch (index.column()) {
90          return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());                          case 0: return pInstr->name();
91                            case 1: return QVariant::fromValue(pInstr->map());
92                            case 2: return QVariant::fromValue(pInstr->bank());
93                            case 3: return QVariant::fromValue(pInstr->prog() + 1);
94                            case 4: return pInstr->engineName();
95                            case 5: return pInstr->instrumentFile();
96                            case 6: return QVariant::fromValue(pInstr->instrumentNr());
97                            case 7: return QString::number(pInstr->volume() * 100.0) + " %";
98                            case 8: {
99                                    switch (pInstr->loadMode()) {
100                                            case 3: return tr("Persistent");
101                                            case 2: return tr("On Demand Hold");
102                                            case 1: return tr("On Demand");
103                                    }
104                            }
105                            default:
106                                    break;
107                    }
108            }
109    
110            return QVariant();
111  }  }
112    
113    
114  // To show up whether its open or not.  QModelIndex InstrumentListModel::index (
115  void qsamplerInstrumentGroup::setOpen ( bool bOpen )          int row, int col, const QModelIndex& /*parent*/ ) const
116  {  {
117          // Set the proper pixmap of this...          const Instrument *pInstr = NULL;
118          if (rtti() == qsamplerInstrumentList::Group) {  
119                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
120                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));                  int nrows = 0;
121                    InstrumentMap::const_iterator itMap = m_instruments.constBegin();
122                    for ( ; itMap != m_instruments.constEnd(); ++itMap) {
123                            const InstrumentList& list = *itMap;
124                            nrows += list.size();
125                            if (row < nrows) {
126                                    pInstr = list.at(row + list.size() - nrows);
127                                    break;
128                            }
129                    }
130            } else {
131                    // Resolve MIDI instrument map...
132                    InstrumentMap::const_iterator itMap     = m_instruments.find(m_iMidiMap);
133                    if (itMap != m_instruments.constEnd()) {
134                            const InstrumentList& list = *itMap;
135                            // resolve instrument in that map
136                            if (row < list.size())
137                                    pInstr = list.at(row);
138                    }
139          }          }
         // Open it up...  
         QListViewItem::setOpen(bOpen);  
140    
141          // All ancestors should be also visible.          if (pInstr)
142          if (bOpen && QListViewItem::parent())                  return createIndex(row, col, (void *) pInstr);
143                  QListViewItem::parent()->setOpen(true);          else
144                    return QModelIndex();
145  }  }
146    
147    
148  // To virtually distinguish between list view items.  QModelIndex InstrumentListModel::parent ( const QModelIndex& child ) const
 int qsamplerInstrumentGroup::rtti (void) const  
149  {  {
150          return qsamplerInstrumentList::Group;          return QModelIndex();
151  }  }
152    
153    
154  //----------------------------------------------------------------------  QVariant InstrumentListModel::headerData (
155  // class qsamplerInstrumentItem -- custom file list view item.          int section, Qt::Orientation orientation, int role ) const
 //  
   
 // Constructors.  
 qsamplerInstrumentItem::qsamplerInstrumentItem (  
         qsamplerInstrumentList *pListView,  
         qsamplerInstrument *pInstrument,  
         QListViewItem *pItemAfter )  
         : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)  
156  {  {
157          m_pInstrument = pInstrument;          if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
158                    switch (section) {
159          update();                          case 0: return tr("Name");
160  }                          case 1: return tr("Map");
161                            case 2: return tr("Bank");
162  qsamplerInstrumentItem::qsamplerInstrumentItem (                          case 3: return tr("Prog");
163          qsamplerInstrumentGroup *pGroupItem,                          case 4: return tr("Engine");
164          qsamplerInstrument *pInstrument )                          case 5: return tr("File");
165          : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())                          case 6: return tr("Nr");
166  {                          case 7: return tr("Vol");
167          m_pInstrument = pInstrument;                          case 8: return tr("Mode");
168                    }
169            }
170    
171          update();          return QAbstractItemModel::headerData(section, orientation, role);
172  }  }
173    
174    
175  // Default destructor.  void InstrumentListModel::setMidiMap ( int iMidiMap )
 qsamplerInstrumentItem::~qsamplerInstrumentItem (void)  
176  {  {
177          if (m_pInstrument)          if (iMidiMap < 0)
178                  delete m_pInstrument;                  iMidiMap = LSCP_MIDI_MAP_ALL;
 }  
   
179    
180  // To virtually distinguish between list view items.          m_iMidiMap = iMidiMap;
 int qsamplerInstrumentItem::rtti (void) const  
 {  
         return qsamplerInstrumentList::Item;  
181  }  }
182    
183    
184  // Payload accessor.  int InstrumentListModel::midiMap (void) const
 qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const  
185  {  {
186          return m_pInstrument;          return m_iMidiMap;
187  }  }
188    
189    
190  // Item refreshment.  const Instrument *InstrumentListModel::addInstrument (
191  void qsamplerInstrumentItem::update (void)          int iMap, int iBank, int iProg )
192  {  {
193          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));          // Check it there's already one instrument item
194            // with the very same key (bank, program);
195          const QString s = "-";          // if yes, just remove it without prejudice...
196          if (m_pInstrument) {          InstrumentList& list = m_instruments[iMap];
197                  setText(0, m_pInstrument->name());          for (int i = 0; i < list.size(); ++i) {
198                  setText(1, QString::number(m_pInstrument->map()));                  const Instrument *pInstr = list.at(i);
199                  setText(2, QString::number(m_pInstrument->bank()));                  if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
200                  setText(3, QString::number(m_pInstrument->prog() + 1));                          delete pInstr;
201                  setText(4, m_pInstrument->engineName());                          list.removeAt(i);
                 setText(5, QFileInfo(m_pInstrument->instrumentFile()).fileName());  
                 setText(6, QString::number(m_pInstrument->instrumentNr()));  
                 setText(7, QString::number(::lroundf(100.0f * m_pInstrument->volume())));  
                 QString sLoadMode = s;  
                 switch (m_pInstrument->loadMode()) {  
                 case 3:  
                         sLoadMode = QObject::tr("Persistent");  
                         break;  
                 case 2:  
                         sLoadMode = QObject::tr("On Demand Hold");  
202                          break;                          break;
203                  case 1:                  }
204                          sLoadMode = QObject::tr("On Demand");          }
205    
206            // Resolve the appropriate place, we keep the list sorted that way...
207            int i = 0;
208            for ( ; i < list.size(); ++i) {
209                    const Instrument *pInstr = list.at(i);
210                    if (iBank < pInstr->bank()
211                            || (iBank == pInstr->bank() && iProg < pInstr->prog())) {
212                          break;                          break;
213                  }                  }
214                  setText(8, sLoadMode);          }
215    
216            Instrument *pInstr = new Instrument(iMap, iBank, iProg);
217            if (pInstr->getInstrument()) {
218                    list.insert(i, pInstr);
219          } else {          } else {
220                  for (int i = 0; i < listView()->columns(); i++)                  delete pInstr;
221                          setText(i, s);                  pInstr = NULL;
222          }          }
223    
224            return pInstr;
225  }  }
226    
227    
228  //----------------------------------------------------------------------------  void InstrumentListModel::removeInstrument ( const Instrument *pInstrument )
229  // qsamplerInstrumentList -- MIDI instrument list view.  {
230  //          const int iMap  = pInstrument->map();
231            const int iBank = pInstrument->bank();
232            const int iProg = pInstrument->prog();
233    
234  // Constructor.          if (m_instruments.contains(iMap)) {
235  qsamplerInstrumentList::qsamplerInstrumentList (                  InstrumentList& list = m_instruments[iMap];
236          QWidget *pParent, const char *pszName )                  for (int i = 0; i < list.size(); ++i) {
237          : QListView(pParent, pszName)                          const Instrument *pInstr = list.at(i);
238  {                          if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
239  //  QListView::setRootIsDecorated(true);                                  delete pInstr;
240          QListView::setResizeMode(QListView::NoColumn);                                  list.removeAt(i);
241  //      QListView::setAcceptDrops(true);                                  break;
242          QListView::setDragAutoScroll(true);                          }
         QListView::setSizePolicy(  
                 QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));  
 //      QListView::setShowToolTips(false);  
         QListView::setSortColumn(-1);  
   
         QListView::addColumn(tr("Name"));  
         QListView::addColumn(tr("Map"));  
         QListView::addColumn(tr("Bank"));  
         QListView::addColumn(tr("Prog"));  
         QListView::addColumn(tr("Engine"));  
         QListView::addColumn(tr("File"));  
         QListView::addColumn(tr("Nr"));  
         QListView::addColumn(tr("Vol"));  
         QListView::addColumn(tr("Mode"));  
   
         QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Map  
         QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Bank  
         QListView::setColumnAlignment(3, Qt::AlignHCenter);     // Prog  
         QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Nr  
         QListView::setColumnAlignment(7, Qt::AlignHCenter);     // Vol  
   
         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_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);  
   
         QObject::connect(m_pNewGroupAction,  
                 SIGNAL(activated()),  
                 SLOT(newGroupSlot()));  
         QObject::connect(m_pNewItemAction,  
                 SIGNAL(activated()),  
                 SLOT(newItemSlot()));  
         QObject::connect(m_pEditItemAction,  
                 SIGNAL(activated()),  
                 SLOT(editItemSlot()));  
         QObject::connect(m_pRenameAction,  
                 SIGNAL(activated()),  
                 SLOT(renameSlot()));  
         QObject::connect(m_pDeleteAction,  
                 SIGNAL(activated()),  
                 SLOT(deleteSlot()));  
         QObject::connect(m_pRefreshAction,  
                 SIGNAL(activated()),  
                 SLOT(refresh()));  
   
         QObject::connect(this,  
                 SIGNAL(selectionChanged()),  
                 SLOT(selectionChangedSlot()));  
         QObject::connect(this,  
                 SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),  
                 SLOT(activatedSlot(QListViewItem*)));  
         QObject::connect(this,  
                 SIGNAL(returnPressed(QListViewItem*)),  
                 SLOT(activatedSlot(QListViewItem*)));  
         QObject::connect(this,  
                 SIGNAL(itemRenamed(QListViewItem*,int)),  
                 SLOT(renamedSlot(QListViewItem*)));  
   
         selectionChangedSlot();  
 }  
   
   
 // Default destructor.  
 qsamplerInstrumentList::~qsamplerInstrumentList (void)  
 {  
         delete m_pNewGroupAction;  
         delete m_pNewItemAction;  
         delete m_pEditItemAction;  
         delete m_pRenameAction;  
         delete m_pDeleteAction;  
 }  
   
   
 // Add a new instrument item, optionally under a given group.  
 qsamplerInstrumentItem *qsamplerInstrumentList::addItem (  
         qsamplerInstrument *pInstrument,  
         qsamplerInstrumentGroup *pParentGroup )  
 {  
         qsamplerInstrumentItem *pItem = findItem(pInstrument);  
         if (pItem == NULL) {  
                 if (pParentGroup)  
                         pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);  
                 else  
                         pItem = new qsamplerInstrumentItem(this, pInstrument);  
         }  
         QListView::setSelected(pItem, true);  
         return pItem;  
 }  
   
   
 // Add a new instrument group, optionally under another group.  
 qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (  
         const QString& sName, qsamplerInstrumentGroup *pParentGroup )  
 {  
         qsamplerInstrumentGroup *pGroup = findGroup(sName);  
         if (pGroup == NULL) {  
                 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()->map()  == pInstrument->map()  
                                 && pItem->instrument()->bank() == pInstrument->bank()  
                                 && pItem->instrument()->prog() == pInstrument->prog())  
                                 return pItem;  
243                  }                  }
                 ++iter;  
244          }          }
         // Not found.  
         return NULL;  
245  }  }
246    
247    
248  // Find and return the nearest group item...  // Reposition the instrument in the model (called when map/bank/prg changed)
249  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (  void InstrumentListModel::updateInstrument ( const Instrument *pInstrument )
         QListViewItem *pItem ) const  
250  {  {
251          while (pItem && pItem->rtti() != Group)          const int iMap  = pInstrument->map();
252                  pItem = pItem->parent();          const int iBank = pInstrument->bank();
253          return static_cast<qsamplerInstrumentGroup *> (pItem);          const int iProg = pInstrument->prog();
 }  
   
254    
255  // Add a new group item below the current one.          // Remove given instrument from its current list...
256  void qsamplerInstrumentList::newGroupSlot (void)          removeInstrument(pInstrument);
 {  
         qsamplerInstrumentGroup *pParentGroup  
                 = groupItem(QListView::selectedItem());  
         qsamplerInstrumentGroup *pNewGroup  
                 = addGroup(tr("New Group"), pParentGroup);  
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
         if (pNewGroup)  
                 pNewGroup->startRename(0);  
257    
258          selectionChangedSlot();          // Re-add the instrument...
259            addInstrument(iMap, iBank, iProg);
260  }  }
261    
262    
263  // Add a new instrument item below the current one.  void InstrumentListModel::refresh (void)
 void qsamplerInstrumentList::newItemSlot (void)  
264  {  {
265          qsamplerInstrument *pInstrument = new qsamplerInstrument();          MainForm *pMainForm = MainForm::getInstance();
266            if (pMainForm == NULL)
267          qsamplerInstrumentForm form(this);                  return;
268          form.setup(pInstrument);          if (pMainForm->client() == NULL)
         if (!form.exec()) {  
                 delete pInstrument;  
269                  return;                  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;  
270    
271          pInstrument->mapInstrument();          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
         emit instrumentsChanged();  
272    
273          qsamplerInstrumentGroup *pParentGroup          clear();
                 = groupItem(QListView::selectedItem());  
         addItem(pInstrument, pParentGroup);  
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
274    
275          selectionChangedSlot();          // Load the whole bunch of instrument items...
276  }          lscp_midi_instrument_t *pInstrs
277                    = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
278            for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
279                    const int iMap  = pInstrs[iInstr].map;
280                    const int iBank = pInstrs[iInstr].bank;
281                    const int iProg = pInstrs[iInstr].prog;
282                    addInstrument(iMap, iBank, iProg);
283                    // Try to keep it snappy :)
284                    QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
285            }
286    
287            QApplication::restoreOverrideCursor();
288    
289  // Edit current item below the current one.          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
290  void qsamplerInstrumentList::editItemSlot (void)                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
291  {                  pMainForm->appendMessagesError(
292          QListViewItem *pListItem = QListView::selectedItem();                          tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
         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()->mapInstrument();  
                                 emit instrumentsChanged();  
                                 pItem->update();  
                         }  
                 }  
293          }          }
   
         selectionChangedSlot();  
294  }  }
295    
296    void InstrumentListModel::beginReset (void)
 // Rename current group/item.  
 void qsamplerInstrumentList::renameSlot (void)  
297  {  {
298          QListViewItem *pListItem = QListView::selectedItem();  #if QT_VERSION >= 0x040600
299          if (pListItem)          QAbstractItemModel::beginResetModel();
300                  pListItem->startRename(0);  #endif
301    }
302    
303          selectionChangedSlot();  void InstrumentListModel::endReset (void)
304    {
305    #if QT_VERSION >= 0x040600
306            QAbstractItemModel::endResetModel();
307    #else
308            QAbstractItemModel::reset();
309    #endif
310  }  }
311    
312    
313  // Remove current group/item.  // Map clear.
314  void qsamplerInstrumentList::deleteSlot (void)  void InstrumentListModel::clear (void)
315  {  {
316          QListViewItem *pListItem = QListView::selectedItem();          InstrumentMap::iterator itMap = m_instruments.begin();
317          if (pListItem == NULL)          for ( ; itMap != m_instruments.end(); ++itMap) {
318                  return;                  InstrumentList& list = itMap.value();
319                    qDeleteAll(list);
320                    list.clear();
321            }
322    
323          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();          m_instruments.clear();
324          if (pMainForm == NULL)  }
                 return;  
325    
         // Prompt user if this is for real...  
         qsamplerOptions *pOptions = pMainForm->options();  
         if (pOptions && pOptions->bConfirmRemove) {  
                 if (QMessageBox::warning(this,  
                         QSAMPLER_TITLE ": " + tr("Warning"),  
                         tr("Delete %1:\n\n"  
                         "%2\n\n"  
                         "Are you sure?")  
                         .arg(pListItem->rtti() == Item ? tr("instrument") : tr("group"))  
                         .arg(pListItem->text(0)),  
                         tr("OK"), tr("Cancel")) > 0)  
                         return;  
         }  
   
         // Unmap instrument entry...  
         if (pListItem->rtti() == Item) {  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         pItem->instrument()->unmapInstrument();  
                         emit instrumentsChanged();  
                 }  
         }  
326    
327          // Do it for real...  //-------------------------------------------------------------------------
328          delete pListItem;  // QSampler::InstrumentListView - list view for MIDI prog mappings
329    //
330    
331    // Constructor.
332    InstrumentListView::InstrumentListView ( QWidget *pParent )
333            : QTreeView(pParent)
334    {
335            m_pListModel = new InstrumentListModel(this);
336    
337          selectionChangedSlot();          QTreeView::setModel(m_pListModel);
338  }  }
339    
340    
341  // In-place selection slot.  // Destructor.
342  void qsamplerInstrumentList::selectionChangedSlot (void)  InstrumentListView::~InstrumentListView (void)
343  {  {
344          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();          delete m_pListModel;
         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);  
345  }  }
346    
347    
348  // In-place activation slot.  void InstrumentListView::setMidiMap ( int iMidiMap )
 void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  
349  {  {
350          // FIXME: Hope the list view item is the one selected.          m_pListModel->setMidiMap(iMidiMap);
         if (pListItem->rtti() == Item)  
                 editItemSlot();  
351  }  }
352    
353    
354  // In-place aliasing slot.  int InstrumentListView::midiMap (void) const
 void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )  
355  {  {
356          if (pListItem->rtti() == Item) {          return m_pListModel->midiMap();
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         pItem->instrument()->setName(pListItem->text(0));  
                         pItem->instrument()->mapInstrument();  
                         emit instrumentsChanged();  
                         pItem->update();  
                 }  
         }  
357  }  }
358    
359    
360  // Context menu request event handler.  const Instrument *InstrumentListView::addInstrument (
361  void qsamplerInstrumentList::contextMenuEvent (          int iMap, int iBank, int iProg )
         QContextMenuEvent *pContextMenuEvent )  
362  {  {
363          if (!m_pNewItemAction->isEnabled())          m_pListModel->beginReset();
364                  return;          const Instrument *pInstrument
365                    = m_pListModel->addInstrument(iMap, iBank, iProg);
366            m_pListModel->endReset();
367    
368          QPopupMenu menu(this);          return pInstrument;
369    }
370    
         // 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);  
371    
372          menu.exec(pContextMenuEvent->globalPos());  void InstrumentListView::removeInstrument ( const Instrument *pInstrument )
373    {
374            m_pListModel->beginReset();
375            m_pListModel->removeInstrument(pInstrument);
376            m_pListModel->endReset();
377  }  }
378    
379    
380  // General reloader.  // Reposition the instrument in the model (called when map/bank/prg changed)
381  void qsamplerInstrumentList::refresh (void)  void InstrumentListView::updateInstrument ( const Instrument *pInstrument )
382  {  {
383          clear();          m_pListModel->beginReset();
384            m_pListModel->updateInstrument(pInstrument);
385            m_pListModel->endReset();}
386    
         qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  
         if (pMainForm == NULL)  
                 return;  
         if (pMainForm->client() == NULL)  
                 return;  
387    
388          qsamplerInstrumentItem *pItem = NULL;  // Refreshener.
389          lscp_midi_instrument_t *pInstrs  void InstrumentListView::refresh (void)
390                  = ::lscp_list_midi_instruments(pMainForm->client(), LSCP_MIDI_MAP_ALL);  {
391          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {          m_pListModel->beginReset();
392                  int iMap  = pInstrs[iInstr].map;          m_pListModel->refresh();
393                  int iBank = pInstrs[iInstr].bank;          m_pListModel->endReset();
394                  int iProg = pInstrs[iInstr].prog;  }
                 qsamplerInstrument *pInstrument  
                         = new qsamplerInstrument(iMap, iBank, iProg);  
                 if (pInstrument->getInstrument())  
                         pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);  
         }  
395    
         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."));  
         }  
396    
397          selectionChangedSlot();  } // namespace QSampler
 }  
398    
399    
400  // end of qsamplerInstrumentList.cpp  // end of qsamplerInstrumentList.cpp
   

Legend:
Removed from v.987  
changed lines
  Added in v.2064

  ViewVC Help
Powered by ViewVC