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

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

  ViewVC Help
Powered by ViewVC