/[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 1461 by schoenebeck, Sun Oct 28 23:30:36 2007 UTC revision 2069 by capela, Mon Mar 15 10:45:35 2010 UTC
# Line 1  Line 1 
1  // qsamplerInstrumentList.cpp  // qsamplerInstrumentList.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2003-2007, 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 <qapplication.h>  #include <QApplication>
32  #include <qmessagebox.h>  #include <QCursor>
 #include <qeventloop.h>  
 #include <qaction.h>  
 #include <qcursor.h>  
 #include <qfileinfo.h>  
   
 #include <QMenu>  
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  using namespace QSampler;  //-------------------------------------------------------------------------
38    // QSampler::InstrumentListModel - data model for MIDI prog mappings
 //----------------------------------------------------------------------  
 // class qsamplerInstrumentGroup -- custom group list view item.  
39  //  //
40    
41  #if 0  InstrumentListModel::InstrumentListModel ( QObject *pParent )
42  // Constructors.          : QAbstractItemModel(pParent), m_iMidiMap(LSCP_MIDI_MAP_ALL)
 qsamplerInstrumentGroup::qsamplerInstrumentGroup (  
         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  // To virtually distinguish between list view items.          m_iMidiMap = iMidiMap;
 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          const QString s = "-";          // if yes, just remove it without prejudice...
192          if (m_pInstrument) {          InstrumentList& list = m_instruments[iMap];
193                  setText(0, m_pInstrument->name());          for (int i = 0; i < list.size(); ++i) {
194                  setText(1, QString::number(m_pInstrument->map()));                  const Instrument *pInstr = list.at(i);
195                  setText(2, QString::number(m_pInstrument->bank()));                  if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
196                  setText(3, QString::number(m_pInstrument->prog() + 1));                          delete pInstr;
197                  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");  
                         break;  
                 case 1:  
                         sLoadMode = QObject::tr("On Demand");  
198                          break;                          break;
199                  }                  }
                 setText(8, sLoadMode);  
         } else {  
                 for (int i = 0; i < listView()->columns(); i++)  
                         setText(i, s);  
200          }          }
 }  
   
201    
202  //----------------------------------------------------------------------------          // Resolve the appropriate place, we keep the list sorted that way...
203  // qsamplerInstrumentList -- MIDI instrument list view.          int i = 0;
204  //          for ( ; i < list.size(); ++i) {
205                    const Instrument *pInstr = list.at(i);
206  // Constructor.                  if (iBank < pInstr->bank()
207  qsamplerInstrumentList::qsamplerInstrumentList (                          || (iBank == pInstr->bank() && iProg < pInstr->prog())) {
208          QWidget *pParent, const char *pszName )                          break;
         : QListView(pParent, pszName)  
 {  
         m_iMidiMap = LSCP_MIDI_MAP_ALL;  
   
 //  QListView::setRootIsDecorated(true);  
         QListView::setAllColumnsShowFocus(true);  
         QListView::setResizeMode(QListView::NoColumn);  
 //      QListView::setAcceptDrops(true);  
         QListView::setDragAutoScroll(true);  
         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(  
                 QIconSet(QPixmap::fromMimeSource("itemGroupNew.png")),  
                 tr("New &Group"), tr("Ctrl+G"), this);  
         m_pNewItemAction  = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("itemNew.png")),  
                 tr("New &Instrument..."), tr("Ins"), this);  
         m_pEditItemAction = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("formEdit.png")),  
                 tr("&Edit..."), tr("Enter"), this);  
         m_pRenameAction   = new QAction(tr("&Rename"), tr("F2"), this);  
         m_pDeleteAction   = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("formRemove.png")),  
                 tr("&Delete"), tr("Del"), this);  
         m_pRefreshAction  = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("formRefresh.png")),  
                 tr("Re&fresh"), tr("F5"), this);  
   
         m_pNewGroupAction->setToolTip(tr("New Group"));  
         m_pNewItemAction->setToolTip(tr("New Instrument"));  
         m_pEditItemAction->setToolTip(tr("Edit"));  
         m_pRenameAction->setToolTip(tr("Rename"));  
         m_pDeleteAction->setToolTip(tr("Delete"));  
         m_pRefreshAction->setToolTip(tr("Refresh"));  
   
         QObject::connect(m_pNewGroupAction,  
                 SIGNAL(activated()),  
                 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 )  
 {  
         // 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) {  
                 // If exactly the same, just update view and bail out...  
                 if (pItem->instrument() == pInstrument) {  
                         pItem->update();  
                         return pItem;  
209                  }                  }
                 // Remove it, as instrument keys must be unique.  
                 delete pItem;  
210          }          }
211    
212          // Add the new item under proper group one, if any...          Instrument *pInstr = new Instrument(iMap, iBank, iProg);
213          if (pParentGroup) {          if (pInstr->getInstrument()) {
214                  pParentGroup->setOpen(true);                  list.insert(i, pInstr);
                 pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);  
215          } else {          } else {
216                  pItem = new qsamplerInstrumentItem(this, pInstrument);                  delete pInstr;
217                    pInstr = NULL;
218          }          }
219    
220          // Set it as current selection...          return pInstr;
         QListView::setSelected(pItem, true);  
   
         return pItem;  
221  }  }
222    
223    
224  // Add a new instrument group, optionally under another group.  void InstrumentListModel::removeInstrument ( const Instrument *pInstrument )
 qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (  
         const QString& sName, qsamplerInstrumentGroup *pParentGroup )  
225  {  {
226          qsamplerInstrumentGroup *pGroup = findGroup(sName);          const int iMap  = pInstrument->map();
227          if (pGroup == NULL) {          const int iBank = pInstrument->bank();
228                  if (pParentGroup) {          const int iProg = pInstrument->prog();
                         pParentGroup->setOpen(true);  
                         pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);  
                 } else {  
                         pGroup = new qsamplerInstrumentGroup(this, sName);  
                 }  
         }  
         QListView::setSelected(pGroup, true);  
         return pGroup;  
 }  
   
229    
230  // Find a group item, given its name.          if (m_instruments.contains(iMap)) {
231  qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (                  InstrumentList& list = m_instruments[iMap];
232          const QString& sName ) const                  for (int i = 0; i < list.size(); ++i) {
233  {                          const Instrument *pInstr = list.at(i);
234          // Iterate all over the place to search for the group.                          if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
235          QListViewItemIterator iter((QListView *) this);                                  delete pInstr;
236          while (iter.current()) {                                  list.removeAt(i);
237                  QListViewItem *pItem = iter.current();                                  break;
238                  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;  
239                  }                  }
                 ++iter;  
240          }          }
         // Not found.  
         return NULL;  
241  }  }
242    
243    
244  // Find and return the nearest group item...  // Reposition the instrument in the model (called when map/bank/prg changed)
245  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (  void InstrumentListModel::updateInstrument ( const Instrument *pInstrument )
         QListViewItem *pItem ) const  
246  {  {
247          while (pItem && pItem->rtti() != Group)          const int iMap  = pInstrument->map();
248                  pItem = pItem->parent();          const int iBank = pInstrument->bank();
249          return static_cast<qsamplerInstrumentGroup *> (pItem);          const int iProg = pInstrument->prog();
 }  
   
250    
251  // Add a new group item below the current one.          // Remove given instrument from its current list...
252  void qsamplerInstrumentList::newGroupSlot (void)          removeInstrument(pInstrument);
 {  
         qsamplerInstrumentGroup *pNewGroup  
                 = addGroup(tr("New Group"), groupItem(QListView::selectedItem()));  
         if (pNewGroup)  
                 pNewGroup->startRename(0);  
253    
254          selectionChangedSlot();          // Re-add the instrument...
255            addInstrument(iMap, iBank, iProg);
256  }  }
257    
258    
259  // Map selector.  void InstrumentListModel::refresh (void)
 void qsamplerInstrumentList::setMidiMap ( int iMidiMap )  
260  {  {
261          if (iMidiMap < 0)          MainForm *pMainForm = MainForm::getInstance();
262                  iMidiMap = LSCP_MIDI_MAP_ALL;          if (pMainForm == NULL)
263                    return;
264          m_iMidiMap = iMidiMap;          if (pMainForm->client() == NULL)
265  }                  return;
   
 int qsamplerInstrumentList::midiMap (void) const  
 {  
         return m_iMidiMap;  
 }  
   
266    
267  // List actions accessors.          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 QAction *qsamplerInstrumentList::newGroupAction (void) const  
 {  
         return m_pNewGroupAction;  
 }  
268    
269  QAction *qsamplerInstrumentList::newItemAction (void) const          clear();
 {  
         return m_pNewItemAction;  
 }  
270    
271  QAction *qsamplerInstrumentList::editItemAction (void) const          // Load the whole bunch of instrument items...
272  {          lscp_midi_instrument_t *pInstrs
273          return m_pEditItemAction;                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
274  }          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
275                    const int iMap  = pInstrs[iInstr].map;
276                    const int iBank = pInstrs[iInstr].bank;
277                    const int iProg = pInstrs[iInstr].prog;
278                    addInstrument(iMap, iBank, iProg);
279                    // Try to keep it snappy :)
280                    QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
281            }
282    
283  QAction *qsamplerInstrumentList::renameAction (void) const          QApplication::restoreOverrideCursor();
 {  
         return m_pRenameAction;  
 }  
284    
285  QAction *qsamplerInstrumentList::deleteAction (void) const          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
286  {                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
287          return m_pDeleteAction;                  pMainForm->appendMessagesError(
288                            tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
289            }
290  }  }
291    
292  QAction *qsamplerInstrumentList::refreshAction (void) const  void InstrumentListModel::beginReset (void)
293  {  {
294          return m_pRefreshAction;  #if QT_VERSION >= 0x040600
295            QAbstractItemModel::beginResetModel();
296    #endif
297  }  }
298    
299    void InstrumentListModel::endReset (void)
 // Add a new instrument item below the current one.  
 void qsamplerInstrumentList::newItemSlot (void)  
300  {  {
301          qsamplerInstrument *pInstrument = new qsamplerInstrument();  #if QT_VERSION >= 0x040600
302            QAbstractItemModel::endResetModel();
303          qsamplerInstrumentForm form(this);  #else
304          form.setup(pInstrument);          QAbstractItemModel::reset();
305          if (!form.exec()) {  #endif
                 delete pInstrument;  
                 return;  
         }  
   
         // Commit...  
         pInstrument->mapInstrument();  
         // add new item to the tree...  
         addItem(pInstrument, groupItem(QListView::selectedItem()));  
         // Notify we've changes...  
         emit instrumentsChanged();  
   
         selectionChangedSlot();  
306  }  }
307    
308    
309  // Edit current item below the current one.  // Map clear.
310  void qsamplerInstrumentList::editItemSlot (void)  void InstrumentListModel::clear (void)
311  {  {
312          QListViewItem *pListItem = QListView::selectedItem();          InstrumentMap::iterator itMap = m_instruments.begin();
313          if (pListItem == NULL)          for ( ; itMap != m_instruments.end(); ++itMap) {
314                  return;                  InstrumentList& list = itMap.value();
315          if (pListItem->rtti() == Item) {                  qDeleteAll(list);
316                  qsamplerInstrument *pInstrument = NULL;                  list.clear();
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem)  
                         pInstrument = pItem->instrument();  
                 if (pInstrument) {  
                         // Save current key values...  
                         qsamplerInstrument oldInstrument(*pInstrument);  
                         // Do the edit dance...  
                         qsamplerInstrumentForm form(this);  
                         form.setup(pInstrument);  
                         if (form.exec()) {  
                                 // Commit...  
                                 pInstrument->mapInstrument();  
                                 // Check whether we changed instrument key...  
                                 if (oldInstrument.map()  == pInstrument->map()  &&  
                                         oldInstrument.bank() == pInstrument->bank() &&  
                                         oldInstrument.prog() == pInstrument->prog()) {  
                                         // just update tree item...  
                                         pItem->update();  
                                 } else {  
                                         // Unmap old instance...  
                                         oldInstrument.unmapInstrument();  
                                         // Change item tree, whether applicable...  
                                         if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) {  
                                                 // Add new brand item into view...  
                                                 addItem(pInstrument, groupItem(pListItem));  
                                         } else {  
                                                 // Just remove/hide old one.  
                                                 delete pItem;  
                                         }  
                                 }  
                                 // Notify we've changes...  
                                 emit instrumentsChanged();  
                         }  
                 }  
317          }          }
318    
319          selectionChangedSlot();          m_instruments.clear();
320  }  }
321    
322    
323  // Rename current group/item.  //-------------------------------------------------------------------------
324  void qsamplerInstrumentList::renameSlot (void)  // QSampler::InstrumentListView - list view for MIDI prog mappings
325  {  //
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem)  
                 pListItem->startRename(0);  
   
         selectionChangedSlot();  
 }  
   
326    
327  // Remove current group/item.  // Constructor.
328  void qsamplerInstrumentList::deleteSlot (void)  InstrumentListView::InstrumentListView ( QWidget *pParent )
329            : QTreeView(pParent)
330  {  {
331          QListViewItem *pListItem = QListView::selectedItem();          m_pListModel = new InstrumentListModel(this);
         if (pListItem == NULL)  
                 return;  
   
         qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  
         if (pMainForm == NULL)  
                 return;  
332    
333          // Prompt user if this is for real...          QTreeView::setModel(m_pListModel);
         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();  
                 }  
         }  
334    
335          // Do it for real...          QTreeView::setRootIsDecorated(false);
336          delete pListItem;          QTreeView::setUniformRowHeights(true);
337            QTreeView::setAlternatingRowColors(true);
338            QTreeView::setSelectionBehavior(QAbstractItemView::SelectRows);
339            QTreeView::setSelectionMode(QAbstractItemView::SingleSelection);
340            QTreeView::setItemsExpandable(false);
341    
342          selectionChangedSlot();          QHeaderView *pHeader = QTreeView::header();
343            pHeader->setDefaultAlignment(Qt::AlignLeft);
344            pHeader->setMovable(false);
345            pHeader->setStretchLastSection(true);
346            pHeader->resizeSection(0, 120);                 // Name
347            QTreeView::resizeColumnToContents(1);   // Map
348            QTreeView::resizeColumnToContents(2);   // Bank
349            QTreeView::resizeColumnToContents(3);   // Prog
350            QTreeView::resizeColumnToContents(4);   // Engine
351            pHeader->resizeSection(5, 240);                 // File
352            QTreeView::resizeColumnToContents(6);   // Nr
353            pHeader->resizeSection(7, 60);                  // Vol
354  }  }
355    
356    
357  // In-place selection slot.  // Destructor.
358  void qsamplerInstrumentList::selectionChangedSlot (void)  InstrumentListView::~InstrumentListView (void)
359  {  {
360          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);  
361  }  }
362    
363    
364  // In-place activation slot.  void InstrumentListView::setMidiMap ( int iMidiMap )
 void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  
365  {  {
366          // FIXME: Hope the list view item is the one selected.          m_pListModel->setMidiMap(iMidiMap);
         if (pListItem && pListItem->rtti() == Item)  
                 editItemSlot();  
367  }  }
368    
369    
370  // In-place aliasing slot.  int InstrumentListView::midiMap (void) const
 void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )  
371  {  {
372          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();  
                 }  
         }  
373  }  }
374    
375    
376  // Context menu request event handler.  const Instrument *InstrumentListView::addInstrument (
377  void qsamplerInstrumentList::contextMenuEvent (          int iMap, int iBank, int iProg )
         QContextMenuEvent *pContextMenuEvent )  
378  {  {
379          if (!m_pNewItemAction->isEnabled())          m_pListModel->beginReset();
380                  return;          const Instrument *pInstrument
381                    = m_pListModel->addInstrument(iMap, iBank, iProg);
382          QPopupMenu menu(this);          m_pListModel->endReset();
383    
384          // Construct context menu.          return pInstrument;
         m_pNewItemAction->addTo(&menu);  
 //      m_pNewGroupAction->addTo(&menu);  
         menu.insertSeparator();  
         m_pEditItemAction->addTo(&menu);  
         m_pRenameAction->addTo(&menu);  
         m_pDeleteAction->addTo(&menu);  
         menu.insertSeparator();  
         m_pRefreshAction->addTo(&menu);  
   
         menu.exec(pContextMenuEvent->globalPos());  
385  }  }
386    
387    
388  // General reloader.  void InstrumentListView::removeInstrument ( const Instrument *pInstrument )
 void qsamplerInstrumentList::refresh (void)  
389  {  {
390          clear();          m_pListModel->beginReset();
391            m_pListModel->removeInstrument(pInstrument);
392          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();          m_pListModel->endReset();
         if (pMainForm == NULL)  
                 return;  
         if (pMainForm->client() == NULL)  
                 return;  
   
         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));  
   
         // Load the whole bunch of instrument items...  
         qsamplerInstrumentItem *pItem = NULL;  
         lscp_midi_instrument_t *pInstrs  
                 = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);  
         for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {  
                 int iMap  = pInstrs[iInstr].map;  
                 int iBank = pInstrs[iInstr].bank;  
                 int iProg = pInstrs[iInstr].prog;  
                 qsamplerInstrument *pInstrument  
                         = new qsamplerInstrument(iMap, iBank, iProg);  
                 if (pInstrument->getInstrument())  
                         pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);  
                 // Try to keep it snappy :)  
                 QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput);  
         }  
   
         QApplication::restoreOverrideCursor();  
   
         if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {  
                 pMainForm->appendMessagesClient("lscp_list_midi_instruments");  
                 pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));  
         }  
   
         selectionChangedSlot();  
393  }  }
 #endif  
394    
 MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) {  
     m_iMidiMap = LSCP_MIDI_MAP_ALL;  
 }  
   
 int MidiInstrumentsModel::rowCount(const QModelIndex& /*parent*/) const {  
     if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {  
         int n = 0;  
         for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap)  
             n += (*itMap).size();  
         return n;  
     }  
     InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);  
     if (itMap == instruments.end()) return 0;  
     return (*itMap).size();  
 }  
   
 int MidiInstrumentsModel::columnCount(const QModelIndex& /*parent*/) const {  
     return 9;  
 }  
   
 QVariant MidiInstrumentsModel::data(const QModelIndex &index, int role) const {  
     if (!index.isValid() || role != Qt::DisplayRole) return QVariant();  
   
     if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {  
         int n = 0;  
         for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap) {  
             n += (*itMap).size();  
             if (index.row() < n)  
                 return QVariant::fromValue(  
                     (*itMap)[index.row() + (*itMap).size() - n]  
                 );  
         }  
     } else {  
         // resolve MIDI instrument map  
         InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);  
         if (itMap == instruments.end()) return QVariant();  
         // resolve instrument in that map  
         if (index.row() >= (*itMap).size()) return QVariant();  
         return QVariant::fromValue(  
             (*itMap)[index.row()]  
         );  
     }  
   
     return QVariant();  
 }  
   
 QVariant MidiInstrumentsModel::headerData(int section, Qt::Orientation orientation, int role) const {  
     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)  
         return QVariant();  
   
     switch (section) {  
         case 0: return tr("Name");  
         case 1: return tr("Map");  
         case 2: return tr("Bank");  
         case 3: return tr("Prog");  
         case 4: return tr("Engine");  
         case 5: return tr("File");  
         case 6: return tr("Nr");  
         case 7: return tr("Vol");  
         case 8: return tr("Mode");  
         default: return QVariant();  
     }  
 }  
   
 qsamplerInstrument* MidiInstrumentsModel::addInstrument(int iMap, int iBank, int iProg) {  
     // Check it there's already one instrument item  
     // with the very same key (bank, program);  
     // if yes, just remove it without prejudice...  
     for (int i = 0; i < instruments[iMap].size(); i++) {  
         if (  
             instruments[iMap][i].bank() == iBank &&  
             instruments[iMap][i].prog() == iProg  
         ) {  
             instruments[iMap].removeAt(i);  
             break;  
         }  
     }  
   
     // resolve the appropriate place, we keep the list sorted that way ...  
     int i = 0;  
     for (; i < instruments[iMap].size(); i++)  
         if (  
             iBank > instruments[iMap][i].bank() ||  
             ( iBank == instruments[iMap][i].bank() &&  
               iProg > instruments[iMap][i].prog() )  
         ) break;  
   
     qsamplerInstrument& instr = instruments[iMap][i] = qsamplerInstrument(iMap, iBank, iProg);  
   
     return &instr;  
 }  
   
 void MidiInstrumentsModel::setMidiMap(int iMidiMap) {  
     if (iMidiMap < 0)  
         iMidiMap = LSCP_MIDI_MAP_ALL;  
   
     m_iMidiMap = iMidiMap;  
 }  
   
 int MidiInstrumentsModel::midiMap() const {  
     return m_iMidiMap;  
 }  
   
 void MidiInstrumentsModel::refresh() {  
         instruments.clear();  
   
         MainForm* pMainForm = MainForm::getInstance();  
         if (pMainForm == NULL)  
                 return;  
         if (pMainForm->client() == NULL)  
                 return;  
   
         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));  
395    
396          // Load the whole bunch of instrument items...  // Reposition the instrument in the model (called when map/bank/prg changed)
397          lscp_midi_instrument_t* pInstrs  void InstrumentListView::updateInstrument ( const Instrument *pInstrument )
398                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);  {
399          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {          m_pListModel->beginReset();
400                  const int iMap  = pInstrs[iInstr].map;          m_pListModel->updateInstrument(pInstrument);
401                  const int iBank = pInstrs[iInstr].bank;          m_pListModel->endReset();
                 const int iProg = pInstrs[iInstr].prog;  
                 addInstrument(iMap, iBank, iProg);  
                 // Try to keep it snappy :)  
                 QApplication::processEvents(QEventLoop::ExcludeUserInput);  
         }  
   
         QApplication::restoreOverrideCursor();  
   
         if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {  
                 pMainForm->appendMessagesClient("lscp_list_midi_instruments");  
                 pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));  
         }  
   
         //selectionChangedSlot();  
402  }  }
403    
404    
405  MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) {  // Refreshener.
406  }  void InstrumentListView::refresh (void)
407    {
408  QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {          m_pListModel->beginReset();
409      return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);          m_pListModel->refresh();
410            m_pListModel->endReset();
411  }  }
412    
 void MidiInstrumentsDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {  
 }  
413    
414  void MidiInstrumentsDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {  } // namespace QSampler
 }  
415    
 void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {  
     if (editor) editor->setGeometry(option.rect);  
 }  
416    
417  // end of qsamplerInstrumentList.cpp  // end of qsamplerInstrumentList.cpp
   

Legend:
Removed from v.1461  
changed lines
  Added in v.2069

  ViewVC Help
Powered by ViewVC