/[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 1492 by schoenebeck, Mon Nov 19 21:08:18 2007 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-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     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
# Line 24  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 {
36  static inline long lroundf ( float x )  
37    //-------------------------------------------------------------------------
38    // QSampler::InstrumentListModel - data model for MIDI prog mappings
39    //
40    
41    InstrumentListModel::InstrumentListModel ( QObject *pParent )
42            : QAbstractItemModel(pParent), m_iMidiMap(LSCP_MIDI_MAP_ALL)
43    {
44            QAbstractItemModel::reset();
45    }
46    
47    InstrumentListModel::~InstrumentListModel (void)
48    {
49            clear();
50    }
51    
52    
53    int InstrumentListModel::rowCount ( const QModelIndex& /*parent*/) const
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            return nrows;
68    }
69    
70    
71    int InstrumentListModel::columnCount ( const QModelIndex& /*parent*/) const
72    {
73            return 9;
74    }
75    
76    
77    QVariant InstrumentListModel::data (
78            const QModelIndex &index, int role ) const
79    {
80            if (!index.isValid())
81                    return QVariant();
82    
83            const Instrument *pInstr
84                    = static_cast<Instrument *> (index.internalPointer());
85    
86            if (pInstr && role == Qt::DisplayRole) {
87                    switch (index.column()) {
88                            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    QModelIndex InstrumentListModel::index (
113            int row, int col, const QModelIndex& /*parent*/ ) const
114  {  {
115          if (x >= 0.0f)          const Instrument *pInstr = NULL;
116                  return long(x + 0.5f);  
117            if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
118                    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            }
138    
139            if (pInstr)
140                    return createIndex(row, col, (void *) pInstr);
141          else          else
142                  return long(x - 0.5f);                  return QModelIndex();
143  }  }
 #endif  
144    
 using namespace QSampler;  
145    
146    QModelIndex InstrumentListModel::parent ( const QModelIndex& /*child*/ ) const
147    {
148            return QModelIndex();
149    }
150    
 //-------------------------------------------------------------------------  
 // MidiInstrumentsModel - data model for MIDI prog mappings (used for QTableView)  
 //  
151    
152  MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) {  QVariant InstrumentListModel::headerData (
153      m_iMidiMap = LSCP_MIDI_MAP_ALL;          int section, Qt::Orientation orientation, int role ) const
154    {
155            if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
156                    switch (section) {
157                            case 0: return tr("Name");
158                            case 1: return tr("Map");
159                            case 2: return tr("Bank");
160                            case 3: return tr("Prog");
161                            case 4: return tr("Engine");
162                            case 5: return tr("File");
163                            case 6: return tr("Nr");
164                            case 7: return tr("Vol");
165                            case 8: return tr("Mode");
166                    }
167            }
168    
169            return QAbstractItemModel::headerData(section, orientation, role);
170    }
171    
172    
173    void InstrumentListModel::setMidiMap ( int iMidiMap )
174    {
175            if (iMidiMap < 0)
176                    iMidiMap = LSCP_MIDI_MAP_ALL;
177    
178            m_iMidiMap = iMidiMap;
179    }
180    
181    
182    int InstrumentListModel::midiMap (void) const
183    {
184            return m_iMidiMap;
185    }
186    
187    
188    const Instrument *InstrumentListModel::addInstrument (
189            int iMap, int iBank, int iProg )
190    {
191            // Check it there's already one instrument item
192            // with the very same key (bank, program);
193            // if yes, just remove it without prejudice...
194            InstrumentList& list = m_instruments[iMap];
195            for (int i = 0; i < list.size(); ++i) {
196                    const Instrument *pInstr = list.at(i);
197                    if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
198                            delete pInstr;
199                            list.removeAt(i);
200                            break;
201                    }
202            }
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;
211                    }
212            }
213    
214            Instrument *pInstr = new Instrument(iMap, iBank, iProg);
215            if (pInstr->getInstrument()) {
216                    list.insert(i, pInstr);
217            } else {
218                    delete pInstr;
219                    pInstr = NULL;
220            }
221    
222            return pInstr;
223  }  }
224    
225  int MidiInstrumentsModel::rowCount(const QModelIndex& /*parent*/) const {  
226      if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {  void InstrumentListModel::removeInstrument ( const Instrument *pInstrument )
227          int n = 0;  {
228          for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap)          const int iMap  = pInstrument->map();
229              n += (*itMap).size();          const int iBank = pInstrument->bank();
230          return n;          const int iProg = pInstrument->prog();
231      }  
232      InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);          if (m_instruments.contains(iMap)) {
233      if (itMap == instruments.end()) return 0;                  InstrumentList& list = m_instruments[iMap];
234      return (*itMap).size();                  for (int i = 0; i < list.size(); ++i) {
235  }                          const Instrument *pInstr = list.at(i);
236                            if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
237  int MidiInstrumentsModel::columnCount(const QModelIndex& /*parent*/) const {                                  delete pInstr;
238      return 9;                                  list.removeAt(i);
239  }                                  break;
240                            }
241  QVariant MidiInstrumentsModel::data(const QModelIndex &index, int role) const {                  }
242      if (!index.isValid()) return QVariant();          }
   
     const qsamplerInstrument* pInstr = NULL;  
   
     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)  
                 pInstr = &(*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();  
         pInstr = &(*itMap)[index.row()];  
     }  
   
     if (!pInstr) return QVariant();  
   
     if (role == Qt::UserRole) {  
         return QVariant::fromValue((void*)pInstr);  
     }  
   
     if (role == Qt::DisplayRole) {  
         switch (index.column()) {  
             case 0: return pInstr->name();  
             case 1: return QVariant::fromValue(pInstr->map());  
             case 2: return QVariant::fromValue(pInstr->bank());  
             case 3: return QVariant::fromValue(pInstr->prog());  
             case 4: return pInstr->engineName();  
             case 5: return pInstr->instrumentFile();  
             case 6: return QVariant::fromValue(pInstr->instrumentNr());  
             case 7: return QString::number(pInstr->volume() * 100.0) + " %";  
             case 8: {  
                 switch (pInstr->loadMode()) {  
                     case 3: return QObject::tr("Persistent");  
                     case 2: return QObject::tr("On Demand Hold");  
                     case 1: return QObject::tr("On Demand");  
                     default: return QVariant();  
                 }  
             }  
             default: return QVariant();  
         }  
     }  
   
     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;  
   
     instruments[iMap].insert(i, qsamplerInstrument(iMap, iBank, iProg));  
     qsamplerInstrument& instr = instruments[iMap][i];  
     if (!instr.getInstrument())  
         instruments[iMap].removeAt(i);  
   
     return &instr;  
 }  
   
 void MidiInstrumentsModel::removeInstrument(const qsamplerInstrument& instrument) {  
     const int iMap  = instrument.map();  
     const int iBank = instrument.bank();  
     const int iProg = instrument.prog();  
     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;  
         }  
     }  
 }  
   
 // reposition the instrument in the model (called when map/bank/prg changed)  
 void MidiInstrumentsModel::resort(const qsamplerInstrument instrument) {  
     const int iMap  = instrument.map();  
     const int iBank = instrument.bank();  
     const int iProg = instrument.prog();  
     // remove given instrument from its current list  
     removeInstrument(instrument);  
     // re-add the instrument  
     addInstrument(iMap, iBank, iProg);  
 }  
   
 void MidiInstrumentsModel::setMidiMap(int iMidiMap) {  
     if (iMidiMap < 0)  
         iMidiMap = LSCP_MIDI_MAP_ALL;  
   
     m_iMidiMap = iMidiMap;  
243  }  }
244    
245  int MidiInstrumentsModel::midiMap() const {  
246      return m_iMidiMap;  // Reposition the instrument in the model (called when map/bank/prg changed)
247    void InstrumentListModel::updateInstrument ( const Instrument *pInstrument )
248    {
249            const int iMap  = pInstrument->map();
250            const int iBank = pInstrument->bank();
251            const int iProg = pInstrument->prog();
252    
253            // Remove given instrument from its current list...
254            removeInstrument(pInstrument);
255    
256            // Re-add the instrument...
257            addInstrument(iMap, iBank, iProg);
258  }  }
259    
 void MidiInstrumentsModel::refresh() {  
         instruments.clear();  
260    
261          MainForm* pMainForm = MainForm::getInstance();  void InstrumentListModel::refresh (void)
262    {
263            MainForm *pMainForm = MainForm::getInstance();
264          if (pMainForm == NULL)          if (pMainForm == NULL)
265                  return;                  return;
266          if (pMainForm->client() == NULL)          if (pMainForm->client() == NULL)
# Line 227  void MidiInstrumentsModel::refresh() { Line 268  void MidiInstrumentsModel::refresh() {
268    
269          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
270    
271            clear();
272    
273          // Load the whole bunch of instrument items...          // Load the whole bunch of instrument items...
274          lscp_midi_instrument_t* pInstrs          lscp_midi_instrument_t *pInstrs
275                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
276          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
277                  const int iMap  = pInstrs[iInstr].map;                  const int iMap  = pInstrs[iInstr].map;
# Line 236  void MidiInstrumentsModel::refresh() { Line 279  void MidiInstrumentsModel::refresh() {
279                  const int iProg = pInstrs[iInstr].prog;                  const int iProg = pInstrs[iInstr].prog;
280                  addInstrument(iMap, iBank, iProg);                  addInstrument(iMap, iBank, iProg);
281                  // Try to keep it snappy :)                  // Try to keep it snappy :)
282                  QApplication::processEvents(QEventLoop::ExcludeUserInput);                  QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
283          }          }
284    
285          QApplication::restoreOverrideCursor();          QApplication::restoreOverrideCursor();
286    
287          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
288                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
289                  pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));                  pMainForm->appendMessagesError(
290                            tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
291            }
292    }
293    
294    void InstrumentListModel::beginReset (void)
295    {
296    #if QT_VERSION >= 0x040600
297            QAbstractItemModel::beginResetModel();
298    #endif
299    }
300    
301    void InstrumentListModel::endReset (void)
302    {
303    #if QT_VERSION >= 0x040600
304            QAbstractItemModel::endResetModel();
305    #else
306            QAbstractItemModel::reset();
307    #endif
308    }
309    
310    
311    // Map clear.
312    void InstrumentListModel::clear (void)
313    {
314            InstrumentMap::iterator itMap = m_instruments.begin();
315            for ( ; itMap != m_instruments.end(); ++itMap) {
316                    InstrumentList& list = itMap.value();
317                    qDeleteAll(list);
318                    list.clear();
319          }          }
320    
321          // inform the outer world (QTableView) that our data changed          m_instruments.clear();
         QAbstractTableModel::reset();  
322  }  }
323    
324    
325  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
326  // MidiInstrumentsDelegate - table cell renderer for MIDI prog mappings  // QSampler::InstrumentListView - list view for MIDI prog mappings
327  // (doesn't actually do anything ATM, but is already there for a future  //
328  // cell editor widget implementation)  
329    // Constructor.
330    InstrumentListView::InstrumentListView ( QWidget *pParent )
331            : QTreeView(pParent)
332    {
333            m_pListModel = new InstrumentListModel(this);
334    
335  MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) {          QTreeView::setModel(m_pListModel);
336    
337            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  QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {  
358      return QItemDelegate::createEditor(parent, option, index);  // Destructor.
359      //return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);  InstrumentListView::~InstrumentListView (void)
360    {
361            delete m_pListModel;
362  }  }
363    
364  void MidiInstrumentsDelegate::setEditorData(QWidget* /*editor*/, const QModelIndex& /*index*/) const {  
365    void InstrumentListView::setMidiMap ( int iMidiMap )
366    {
367            m_pListModel->setMidiMap(iMidiMap);
368  }  }
369    
370  void MidiInstrumentsDelegate::setModelData(QWidget* /*editor*/, QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const {  
371    int InstrumentListView::midiMap (void) const
372    {
373            return m_pListModel->midiMap();
374  }  }
375    
376  void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {  
377      QItemDelegate::updateEditorGeometry(editor, option, index);  const Instrument *InstrumentListView::addInstrument (
378      //if (editor) editor->setGeometry(option.rect);          int iMap, int iBank, int iProg )
379    {
380            m_pListModel->beginReset();
381            const Instrument *pInstrument
382                    = m_pListModel->addInstrument(iMap, iBank, iProg);
383            m_pListModel->endReset();
384    
385            return pInstrument;
386  }  }
387    
388    
389    void InstrumentListView::removeInstrument ( const Instrument *pInstrument )
390    {
391            m_pListModel->beginReset();
392            m_pListModel->removeInstrument(pInstrument);
393            m_pListModel->endReset();
394    }
395    
396    
397    // Reposition the instrument in the model (called when map/bank/prg changed)
398    void InstrumentListView::updateInstrument ( const Instrument *pInstrument )
399    {
400            m_pListModel->beginReset();
401            m_pListModel->updateInstrument(pInstrument);
402            m_pListModel->endReset();}
403    
404    
405    // Refreshener.
406    void InstrumentListView::refresh (void)
407    {
408            m_pListModel->beginReset();
409            m_pListModel->refresh();
410            m_pListModel->endReset();
411    }
412    
413    
414    } // namespace QSampler
415    
416    
417  // end of qsamplerInstrumentList.cpp  // end of qsamplerInstrumentList.cpp

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

  ViewVC Help
Powered by ViewVC