/[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 972 by capela, Sun Dec 10 17:07:02 2006 UTC revision 2071 by capela, Mon Mar 15 18:21:28 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"
29  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
30    
31  #include <qaction.h>  #include <QApplication>
32  #include <qfileinfo.h>  #include <QCursor>
 #include <qpopupmenu.h>  
   
 // Needed for lroundf()  
 #include <math.h>  
   
 #ifndef CONFIG_ROUND  
 static long lroundf ( float fval )  
 {  
         double fint = 0.0;  
     float  frac = float(::modf(fval, &fint));  
     long   lint = long(fint);  
     if (frac >= +0.5f)  
         lint++;  
     else  
     if (frac <= -0.5f)  
         lint--;  
     return lint;  
 }  
 #endif  
33    
34    
35  //----------------------------------------------------------------------  namespace QSampler {
36  // class qsamplerInstrumentGroup -- custom group list view item.  
37    //-------------------------------------------------------------------------
38    // QSampler::InstrumentListModel - data model for MIDI prog mappings
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();
82                  pParent = pParent->parent();          const Instrument *pInstr
83          return static_cast<qsamplerInstrumentGroup *> (pParent);                  = static_cast<Instrument *> (index.internalPointer());
 }  
84    
85            if (pInstr && role == Qt::DisplayRole) {
86                    switch (index.column()) {
87                            case 0: return pInstr->name();
88                            case 1: return QVariant::fromValue(pInstr->map());
89                            case 2: return QVariant::fromValue(pInstr->bank());
90                            case 3: return QVariant::fromValue(pInstr->prog() + 1);
91                            case 4: return pInstr->engineName();
92                            case 5: return pInstr->instrumentFile();
93                            case 6: return QVariant::fromValue(pInstr->instrumentNr());
94                            case 7: return QString::number(pInstr->volume() * 100.0) + " %";
95                            case 8: {
96                                    switch (pInstr->loadMode()) {
97                                            case 3: return tr("Persistent");
98                                            case 2: return tr("On Demand Hold");
99                                            case 1: return tr("On Demand");
100                                    }
101                            }
102                            default:
103                                    break;
104                    }
105            }
106    
107  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const          return QVariant();
 {  
         return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());  
108  }  }
109    
110    
111  // To show up whether its open or not.  QModelIndex InstrumentListModel::index (
112  void qsamplerInstrumentGroup::setOpen ( bool bOpen )          int row, int col, const QModelIndex& /*parent*/ ) const
113  {  {
114          // Set the proper pixmap of this...          const Instrument *pInstr = NULL;
115          if (rtti() == qsamplerInstrumentList::Group) {  
116                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
117                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));                  int nrows = 0;
118                    InstrumentMap::const_iterator itMap = m_instruments.constBegin();
119                    for ( ; itMap != m_instruments.constEnd(); ++itMap) {
120                            const InstrumentList& list = *itMap;
121                            nrows += list.size();
122                            if (row < nrows) {
123                                    pInstr = list.at(row + list.size() - nrows);
124                                    break;
125                            }
126                    }
127            } else {
128                    // Resolve MIDI instrument map...
129                    InstrumentMap::const_iterator itMap     = m_instruments.find(m_iMidiMap);
130                    if (itMap != m_instruments.constEnd()) {
131                            const InstrumentList& list = *itMap;
132                            if (row < list.size())
133                                    pInstr = list.at(row);
134                    }
135          }          }
         // Open it up...  
         QListViewItem::setOpen(bOpen);  
136    
137          // All ancestors should be also visible.          if (pInstr)
138          if (bOpen && QListViewItem::parent())                  return createIndex(row, col, (void *) pInstr);
139                  QListViewItem::parent()->setOpen(true);          else
140                    return QModelIndex();
141  }  }
142    
143    
144  // To virtually distinguish between list view items.  QModelIndex InstrumentListModel::parent ( const QModelIndex& /*child*/ ) const
 int qsamplerInstrumentGroup::rtti (void) const  
145  {  {
146          return qsamplerInstrumentList::Group;          return QModelIndex();
147  }  }
148    
149    
150  //----------------------------------------------------------------------  QVariant InstrumentListModel::headerData (
151  // 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)  
152  {  {
153          m_pInstrument = pInstrument;          if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
154                    switch (section) {
155          update();                          case 0: return tr("Name");
156  }                          case 1: return tr("Map");
157                            case 2: return tr("Bank");
158  qsamplerInstrumentItem::qsamplerInstrumentItem (                          case 3: return tr("Prog");
159          qsamplerInstrumentGroup *pGroupItem,                          case 4: return tr("Engine");
160          qsamplerInstrument *pInstrument )                          case 5: return tr("File");
161          : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())                          case 6: return tr("Nr");
162  {                          case 7: return tr("Vol");
163          m_pInstrument = pInstrument;                          case 8: return tr("Mode");
164                    }
165            }
166    
167          update();          return QAbstractItemModel::headerData(section, orientation, role);
168  }  }
169    
170    
171  // Default destructor.  void InstrumentListModel::setMidiMap ( int iMidiMap )
 qsamplerInstrumentItem::~qsamplerInstrumentItem (void)  
172  {  {
173          if (m_pInstrument)          if (iMidiMap < 0)
174                  delete m_pInstrument;                  iMidiMap = LSCP_MIDI_MAP_ALL;
 }  
175    
176            m_iMidiMap = iMidiMap;
 // To virtually distinguish between list view items.  
 int qsamplerInstrumentItem::rtti (void) const  
 {  
         return qsamplerInstrumentList::Item;  
177  }  }
178    
179    
180  // Payload accessor.  int InstrumentListModel::midiMap (void) const
 qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const  
181  {  {
182          return m_pInstrument;          return m_iMidiMap;
183  }  }
184    
185    
186  // Item refreshment.  const Instrument *InstrumentListModel::addInstrument (
187  void qsamplerInstrumentItem::update (void)          int iMap, int iBank, int iProg )
188  {  {
189          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));          // Check it there's already one instrument item
190            // with the very same key (bank, program);
191            // if yes, just remove it without prejudice...
192            InstrumentList& list = m_instruments[iMap];
193    
194          const QString s = "-";          int i = 0;
195          if (m_pInstrument) {          for ( ; i < list.size(); ++i) {
196                  setText(0, m_pInstrument->name());                  const Instrument *pInstr = list.at(i);
197                  setText(1, QString::number(m_pInstrument->bank()));                  if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
198                  setText(2, QString::number(m_pInstrument->program() + 1));                          delete pInstr;
199                  setText(3, m_pInstrument->engineName());                          list.removeAt(i);
                 setText(4, QFileInfo(m_pInstrument->instrumentFile()).fileName());  
                 setText(5, QString::number(m_pInstrument->instrumentNr()));  
                 setText(6, 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            for (i = 0; i < list.size(); ++i) {
206                    const Instrument *pInstr = list.at(i);
207                    if (iBank < pInstr->bank()
208                            || (iBank == pInstr->bank() && iProg < pInstr->prog())) {
209                          break;                          break;
210                  }                  }
211                  setText(7, sLoadMode);          }
212    
213            Instrument *pInstr = new Instrument(iMap, iBank, iProg);
214            if (pInstr->getInstrument()) {
215                    list.insert(i, pInstr);
216          } else {          } else {
217                  for (int i = 0; i < listView()->columns(); i++)                  delete pInstr;
218                          setText(i, s);                  pInstr = NULL;
219          }          }
220    
221            return pInstr;
222  }  }
223    
224    
225  //----------------------------------------------------------------------------  void InstrumentListModel::removeInstrument ( Instrument *pInstrument )
226  // qsamplerInstrumentList -- MIDI instrument list view.  {
227  //          const int iMap = pInstrument->map();
228    
229  // Constructor.          if (m_instruments.contains(iMap)) {
230  qsamplerInstrumentList::qsamplerInstrumentList (                  InstrumentList& list = m_instruments[iMap];
231          QWidget *pParent, const char *pszName )                  for (int i = 0; i < list.size(); ++i) {
232          : QListView(pParent, pszName)                          if (pInstrument == list.at(i)) {
233  {                                  delete pInstrument;
234  //  QListView::setRootIsDecorated(true);                                  list.removeAt(i);
235          QListView::setResizeMode(QListView::NoColumn);                                  break;
236  //      QListView::setAcceptDrops(true);                          }
         QListView::setDragAutoScroll(true);  
         QListView::setSizePolicy(  
                 QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));  
 //      QListView::setShowToolTips(false);  
         QListView::setSortColumn(-1);  
   
         QListView::addColumn(tr("Name"));  
         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);     // Bank  
         QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog  
         QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr  
         QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol  
   
         QListView::setColumnWidth(0, 60);       // Name  
         QListView::setColumnWidth(0, 120);      // File  
   
         m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);  
         m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);  
         m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);  
         m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);  
         m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);  
         m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), 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()->bank() == pInstrument->bank()  
                                 && pItem->instrument()->program() == pInstrument->program())  
                                 return pItem;  
237                  }                  }
                 ++iter;  
238          }          }
         // Not found.  
         return NULL;  
239  }  }
240    
241    
242  // Find and return the nearest group item...  void InstrumentListModel::updateInstrument ( Instrument *pInstrument )
 qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (  
         QListViewItem *pItem ) const  
243  {  {
244          while (pItem && pItem->rtti() != Group)          pInstrument->getInstrument();
                 pItem = pItem->parent();  
         return static_cast<qsamplerInstrumentGroup *> (pItem);  
245  }  }
246    
247    
248  // Add a new group item below the current one.  // Reposition the instrument in the model (called when map/bank/prg changed)
249  void qsamplerInstrumentList::newGroupSlot (void)  void InstrumentListModel::resortInstrument ( Instrument *pInstrument )
250  {  {
251          qsamplerInstrumentGroup *pParentGroup          const int iMap  = pInstrument->map();
252                  = groupItem(QListView::selectedItem());          const int iBank = pInstrument->bank();
253          qsamplerInstrumentGroup *pNewGroup          const int iProg = pInstrument->prog();
254                  = addGroup(tr("New Group"), pParentGroup);  
255          if (pParentGroup)          // Remove given instrument from its current list...
256                  pParentGroup->setOpen(true);          removeInstrument(pInstrument);
         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;
         }  
270    
271          // 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;  
272    
273          pInstrument->map();          clear();
         emit instrumentsChanged();  
274    
275          qsamplerInstrumentGroup *pParentGroup          // Load the whole bunch of instrument items...
276                  = groupItem(QListView::selectedItem());          lscp_midi_instrument_t *pInstrs
277          addItem(pInstrument, pParentGroup);                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
278          if (pParentGroup)          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
279                  pParentGroup->setOpen(true);                  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          selectionChangedSlot();          QApplication::restoreOverrideCursor();
 }  
288    
289            if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
290                    pMainForm->appendMessagesClient("lscp_list_midi_instruments");
291                    pMainForm->appendMessagesError(
292                            tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
293            }
294    }
295    
296  // Edit current item below the current one.  void InstrumentListModel::beginReset (void)
 void qsamplerInstrumentList::editItemSlot (void)  
297  {  {
298          QListViewItem *pListItem = QListView::selectedItem();  #if QT_VERSION >= 0x040600
299          if (pListItem == NULL)          QAbstractItemModel::beginResetModel();
300                  return;  #endif
301          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();  
                         }  
                 }  
         }  
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  // Rename current group/item.  // Map clear.
314  void qsamplerInstrumentList::renameSlot (void)  void InstrumentListModel::clear (void)
315  {  {
316          QListViewItem *pListItem = QListView::selectedItem();          InstrumentMap::iterator itMap = m_instruments.begin();
317          if (pListItem)          for ( ; itMap != m_instruments.end(); ++itMap) {
318                  pListItem->startRename(0);                  InstrumentList& list = itMap.value();
319                    qDeleteAll(list);
320                    list.clear();
321            }
322    
323          selectionChangedSlot();          m_instruments.clear();
324  }  }
325    
326    
327  // Remove current group/item.  //-------------------------------------------------------------------------
328  void qsamplerInstrumentList::deleteSlot (void)  // QSampler::InstrumentListView - list view for MIDI prog mappings
329    //
330    
331    // Constructor.
332    InstrumentListView::InstrumentListView ( QWidget *pParent )
333            : QTreeView(pParent)
334  {  {
335          QListViewItem *pListItem = QListView::selectedItem();          m_pListModel = new InstrumentListModel(this);
336          if (pListItem) {  
337                  if (pListItem->rtti() == Item) {          QTreeView::setModel(m_pListModel);
                         qsamplerInstrumentItem *pItem  
                                 = static_cast<qsamplerInstrumentItem *> (pListItem);  
                         if (pItem && pItem->instrument()) {  
                                 pItem->instrument()->unmap();  
                                 emit instrumentsChanged();  
                         }  
                 }  
                 delete pListItem;  
         }  
338    
339          selectionChangedSlot();          QTreeView::setRootIsDecorated(false);
340            QTreeView::setUniformRowHeights(true);
341            QTreeView::setAlternatingRowColors(true);
342            QTreeView::setSelectionBehavior(QAbstractItemView::SelectRows);
343            QTreeView::setSelectionMode(QAbstractItemView::SingleSelection);
344            QTreeView::setItemsExpandable(false);
345    
346            QHeaderView *pHeader = QTreeView::header();
347            pHeader->setDefaultAlignment(Qt::AlignLeft);
348            pHeader->setMovable(false);
349            pHeader->setStretchLastSection(true);
350            pHeader->resizeSection(0, 120);                 // Name
351            QTreeView::resizeColumnToContents(1);   // Map
352            QTreeView::resizeColumnToContents(2);   // Bank
353            QTreeView::resizeColumnToContents(3);   // Prog
354            QTreeView::resizeColumnToContents(4);   // Engine
355            pHeader->resizeSection(5, 240);                 // File
356            QTreeView::resizeColumnToContents(6);   // Nr
357            pHeader->resizeSection(7, 60);                  // Vol
358  }  }
359    
360    
361  // In-place selection slot.  // Destructor.
362  void qsamplerInstrumentList::selectionChangedSlot (void)  InstrumentListView::~InstrumentListView (void)
363  {  {
364          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);  
365  }  }
366    
367    
368  // In-place activation slot.  void InstrumentListView::setMidiMap ( int iMidiMap )
 void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  
369  {  {
370          // FIXME: Hope the list view item is the one selected.          m_pListModel->setMidiMap(iMidiMap);
         if (pListItem->rtti() == Item)  
                 editItemSlot();  
371  }  }
372    
373    
374  // In-place aliasing slot.  int InstrumentListView::midiMap (void) const
 void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )  
375  {  {
376          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()->map();  
                         emit instrumentsChanged();  
                         pItem->update();  
                 }  
         }  
377  }  }
378    
379    
380  // Context menu request event handler.  const Instrument *InstrumentListView::addInstrument (
381  void qsamplerInstrumentList::contextMenuEvent (          int iMap, int iBank, int iProg )
         QContextMenuEvent *pContextMenuEvent )  
382  {  {
383          if (!m_pNewItemAction->isEnabled())          m_pListModel->beginReset();
384                  return;          const Instrument *pInstrument
385                    = m_pListModel->addInstrument(iMap, iBank, iProg);
386            m_pListModel->endReset();
387    
388          QPopupMenu menu(this);          return pInstrument;
389    }
390    
         // 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);  
391    
392          menu.exec(pContextMenuEvent->globalPos());  void InstrumentListView::removeInstrument ( Instrument *pInstrument )
393    {
394            m_pListModel->beginReset();
395            m_pListModel->removeInstrument(pInstrument);
396            m_pListModel->endReset();
397  }  }
398    
399    
400  // General reloader.  void InstrumentListView::updateInstrument ( Instrument *pInstrument )
 void qsamplerInstrumentList::refresh (void)  
401  {  {
402          clear();          m_pListModel->beginReset();
403            m_pListModel->updateInstrument(pInstrument);
404            m_pListModel->endReset();
405    }
406    
         qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  
         if (pMainForm == NULL)  
                 return;  
         if (pMainForm->client() == NULL)  
                 return;  
407    
408          qsamplerInstrumentItem *pItem = NULL;  // Reposition the instrument in the model (called when map/bank/prg changed)
409          lscp_midi_instrument_t *pInstrs  void InstrumentListView::resortInstrument ( Instrument *pInstrument )
410                  = ::lscp_list_midi_instruments(pMainForm->client());  {
411          for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {          m_pListModel->beginReset();
412                  int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;          m_pListModel->resortInstrument(pInstrument);
413                  int iProgram = pInstrs[iInstr].program;          m_pListModel->endReset();
414                  qsamplerInstrument *pInstrument  }
                         = new qsamplerInstrument(iBank, iProgram);  
                 if (pInstrument->get())  
                         pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);  
         }  
415    
         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."));  
         }  
416    
417          selectionChangedSlot();  // Refreshener.
418    void InstrumentListView::refresh (void)
419    {
420            m_pListModel->beginReset();
421            m_pListModel->refresh();
422            m_pListModel->endReset();
423  }  }
424    
425    
426  // end of qsamplerInstrumentList.cpp  } // namespace QSampler
427    
428    
429    // end of qsamplerInstrumentList.cpp

Legend:
Removed from v.972  
changed lines
  Added in v.2071

  ViewVC Help
Powered by ViewVC