/[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 1523 by capela, Sun Nov 25 11:40:47 2007 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-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>  #include <QApplication>
 #include <QMessageBox>  
 #include <QMenu>  
 #include <QAction>  
32  #include <QCursor>  #include <QCursor>
 #include <QFileInfo>  
33    
 // Needed for lroundf()  
 #include <math.h>  
   
 #ifndef CONFIG_ROUND  
 static inline long lroundf ( float x )  
 {  
         if (x >= 0.0f)  
                 return long(x + 0.5f);  
         else  
                 return long(x - 0.5f);  
 }  
 #endif  
   
 using namespace QSampler;  
34    
35    namespace QSampler {
36    
37  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
38  // MidiInstrumentsModel - data model for MIDI prog mappings (used for QTableView)  // QSampler::InstrumentListModel - data model for MIDI prog mappings
39  //  //
40    
41  MidiInstrumentsModel::MidiInstrumentsModel ( QObject* pParent)  InstrumentListModel::InstrumentListModel ( QObject *pParent )
42          : QAbstractTableModel(pParent)          : QAbstractItemModel(pParent), m_iMidiMap(LSCP_MIDI_MAP_ALL)
43  {  {
44          m_iMidiMap = LSCP_MIDI_MAP_ALL;  //      QAbstractItemModel::reset();
45  }  }
46    
47    InstrumentListModel::~InstrumentListModel (void)
48    {
49            clear();
50    }
51    
52  int MidiInstrumentsModel::rowCount ( const QModelIndex& /*parent*/) const  
53    int InstrumentListModel::rowCount ( const QModelIndex& /*parent*/) const
54  {  {
55            int nrows = 0;
56    
57          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
58                  int n = 0;                  InstrumentMap::const_iterator itMap = m_instruments.constBegin();
59                  for (InstrumentsMap::const_iterator itMap = m_instruments.begin();                  for ( ; itMap != m_instruments.constEnd(); ++itMap)
60                                  itMap != m_instruments.end(); ++itMap)                          nrows += (*itMap).size();
61                          n += (*itMap).size();          } else {
62                  return n;                  InstrumentMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
63                    if (itMap != m_instruments.constEnd())
64                            nrows += (*itMap).size();
65          }          }
66          InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap);  
67          if (itMap == m_instruments.end()) return 0;          return nrows;
         return (*itMap).size();  
68  }  }
69    
70    
71  int MidiInstrumentsModel::columnCount ( const QModelIndex& /*parent*/) const  int InstrumentListModel::columnCount ( const QModelIndex& /*parent*/) const
72  {  {
73          return 9;          return 9;
74  }  }
75    
76    
77  QVariant MidiInstrumentsModel::data ( const QModelIndex &index, int role ) const  QVariant InstrumentListModel::data (
78            const QModelIndex &index, int role ) const
79  {  {
80          if (!index.isValid())          if (!index.isValid())
81                  return QVariant();                  return QVariant();
82            const Instrument *pInstr
83                    = static_cast<Instrument *> (index.internalPointer());
84    
85          const qsamplerInstrument* pInstr = NULL;          if (pInstr && role == Qt::DisplayRole) {
   
         if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {  
                 int n = 0;  
                 for (InstrumentsMap::const_iterator itMap = m_instruments.begin();  
                                 itMap != m_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 = m_instruments.find(m_iMidiMap);  
                 if (itMap == m_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) {  
86                  switch (index.column()) {                  switch (index.column()) {
87                          case 0: return pInstr->name();                          case 0: return pInstr->name();
88                          case 1: return QVariant::fromValue(pInstr->map());                          case 1: return QVariant::fromValue(pInstr->map());
89                          case 2: return QVariant::fromValue(pInstr->bank());                          case 2: return QVariant::fromValue(pInstr->bank());
90                          case 3: return QVariant::fromValue(pInstr->prog());                          case 3: return QVariant::fromValue(pInstr->prog() + 1);
91                          case 4: return pInstr->engineName();                          case 4: return pInstr->engineName();
92                          case 5: return pInstr->instrumentFile();                          case 5: return pInstr->instrumentFile();
93                          case 6: return QVariant::fromValue(pInstr->instrumentNr());                          case 6: return QVariant::fromValue(pInstr->instrumentNr());
94                          case 7: return QString::number(pInstr->volume() * 100.0) + " %";                          case 7: return QString::number(pInstr->volume() * 100.0) + " %";
95                          case 8: {                          case 8: {
96                                  switch (pInstr->loadMode()) {                                  switch (pInstr->loadMode()) {
97                                          case 3: return QObject::tr("Persistent");                                          case 3: return tr("Persistent");
98                                          case 2: return QObject::tr("On Demand Hold");                                          case 2: return tr("On Demand Hold");
99                                          case 1: return QObject::tr("On Demand");                                          case 1: return tr("On Demand");
                                         default: return QVariant();  
100                                  }                                  }
101                          }                          }
102                          default: return QVariant();                          default:
103                                    break;
104                  }                  }
105          }          }
106    
# Line 140  QVariant MidiInstrumentsModel::data ( co Line 108  QVariant MidiInstrumentsModel::data ( co
108  }  }
109    
110    
111  QVariant MidiInstrumentsModel::headerData (  QModelIndex InstrumentListModel::index (
112          int section, Qt::Orientation orientation, int role ) const          int row, int col, const QModelIndex& /*parent*/ ) const
113  {  {
114          if (orientation != Qt::Horizontal || role != Qt::DisplayRole)          const Instrument *pInstr = NULL;
                 return QVariant();  
115    
116          switch (section) {          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
117                  case 0: return tr("Name");                  int nrows = 0;
118                  case 1: return tr("Map");                  InstrumentMap::const_iterator itMap = m_instruments.constBegin();
119                  case 2: return tr("Bank");                  for ( ; itMap != m_instruments.constEnd(); ++itMap) {
120                  case 3: return tr("Prog");                          const InstrumentList& list = *itMap;
121                  case 4: return tr("Engine");                          nrows += list.size();
122                  case 5: return tr("File");                          if (row < nrows) {
123                  case 6: return tr("Nr");                                  pInstr = list.at(row + list.size() - nrows);
124                  case 7: return tr("Vol");                                  break;
125                  case 8: return tr("Mode");                          }
126                  default: return QVariant();                  }
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          }          }
136    
137            if (pInstr)
138                    return createIndex(row, col, (void *) pInstr);
139            else
140                    return QModelIndex();
141  }  }
142    
143    
144  qsamplerInstrument* MidiInstrumentsModel::addInstrument (  QModelIndex InstrumentListModel::parent ( const QModelIndex& /*child*/ ) const
145    {
146            return QModelIndex();
147    }
148    
149    
150    QVariant InstrumentListModel::headerData (
151            int section, Qt::Orientation orientation, int role ) const
152    {
153            if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
154                    switch (section) {
155                            case 0: return tr("Name");
156                            case 1: return tr("Map");
157                            case 2: return tr("Bank");
158                            case 3: return tr("Prog");
159                            case 4: return tr("Engine");
160                            case 5: return tr("File");
161                            case 6: return tr("Nr");
162                            case 7: return tr("Vol");
163                            case 8: return tr("Mode");
164                    }
165            }
166    
167            return QAbstractItemModel::headerData(section, orientation, role);
168    }
169    
170    
171    void InstrumentListModel::setMidiMap ( int iMidiMap )
172    {
173            if (iMidiMap < 0)
174                    iMidiMap = LSCP_MIDI_MAP_ALL;
175    
176            m_iMidiMap = iMidiMap;
177    }
178    
179    
180    int InstrumentListModel::midiMap (void) const
181    {
182            return m_iMidiMap;
183    }
184    
185    
186    const Instrument *InstrumentListModel::addInstrument (
187          int iMap, int iBank, int iProg )          int iMap, int iBank, int iProg )
188  {  {
189          // Check it there's already one instrument item          // Check it there's already one instrument item
190          // with the very same key (bank, program);          // with the very same key (bank, program);
191          // if yes, just remove it without prejudice...          // if yes, just remove it without prejudice...
192          for (int i = 0; i < m_instruments[iMap].size(); i++) {          InstrumentList& list = m_instruments[iMap];
193                  if (m_instruments[iMap][i].bank() == iBank &&  
194                          m_instruments[iMap][i].prog() == iProg) {          int i = 0;
195                          m_instruments[iMap].removeAt(i);          for ( ; 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;                          break;
201                  }                  }
202          }          }
203    
204          // Resolve the appropriate place, we keep the list sorted that way ...          // Resolve the appropriate place, we keep the list sorted that way...
205          int i = 0;          for (i = 0; i < list.size(); ++i) {
206          for (; i < m_instruments[iMap].size(); ++i) {                  const Instrument *pInstr = list.at(i);
207                  if (iBank < m_instruments[iMap][i].bank()                  if (iBank < pInstr->bank()
208                          || (iBank == m_instruments[iMap][i].bank() &&                          || (iBank == pInstr->bank() && iProg < pInstr->prog())) {
                                 iProg < m_instruments[iMap][i].prog())) {  
209                          break;                          break;
210                  }                  }
211          }          }
212    
213          m_instruments[iMap].insert(i, qsamplerInstrument(iMap, iBank, iProg));          Instrument *pInstr = new Instrument(iMap, iBank, iProg);
214          qsamplerInstrument& instr = m_instruments[iMap][i];          if (pInstr->getInstrument()) {
215          if (!instr.getInstrument())                  list.insert(i, pInstr);
216                  m_instruments[iMap].removeAt(i);          } else {
217                    delete pInstr;
218                    pInstr = NULL;
219            }
220    
221          return &instr;          return pInstr;
222  }  }
223    
224    
225  void MidiInstrumentsModel::removeInstrument (  void InstrumentListModel::removeInstrument ( Instrument *pInstrument )
         const qsamplerInstrument& instrument )  
226  {  {
227          const int iMap  = instrument.map();          const int iMap = pInstrument->map();
228          const int iBank = instrument.bank();  
229          const int iProg = instrument.prog();          if (m_instruments.contains(iMap)) {
230          for (int i = 0; i < m_instruments[iMap].size(); i++) {                  InstrumentList& list = m_instruments[iMap];
231                  if (m_instruments[iMap][i].bank() == iBank &&                  for (int i = 0; i < list.size(); ++i) {
232                          m_instruments[iMap][i].prog() == iProg) {                          if (pInstrument == list.at(i)) {
233                          m_instruments[iMap].removeAt(i);                                  delete pInstrument;
234                          break;                                  list.removeAt(i);
235                                    break;
236                            }
237                  }                  }
238          }          }
239  }  }
240    
241    
242  // Reposition the instrument in the model (called when map/bank/prg changed)  void InstrumentListModel::updateInstrument ( Instrument *pInstrument )
 void MidiInstrumentsModel::resort ( const qsamplerInstrument& instrument )  
243  {  {
244          const int iMap  = instrument.map();          pInstrument->getInstrument();
         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);  
245  }  }
246    
247    
248  void MidiInstrumentsModel::setMidiMap ( int iMidiMap )  // Reposition the instrument in the model (called when map/bank/prg changed)
249    void InstrumentListModel::resortInstrument ( Instrument *pInstrument )
250  {  {
251          if (iMidiMap < 0)          const int iMap  = pInstrument->map();
252                  iMidiMap = LSCP_MIDI_MAP_ALL;          const int iBank = pInstrument->bank();
253            const int iProg = pInstrument->prog();
         m_iMidiMap = iMidiMap;  
 }  
254    
255            // Remove given instrument from its current list...
256            removeInstrument(pInstrument);
257    
258  int MidiInstrumentsModel::midiMap (void) const          // Re-add the instrument...
259  {          addInstrument(iMap, iBank, iProg);
         return m_iMidiMap;  
260  }  }
261    
 void MidiInstrumentsModel::refresh (void)  
 {  
         m_instruments.clear();  
262    
263          MainForm* pMainForm = MainForm::getInstance();  void InstrumentListModel::refresh (void)
264    {
265            MainForm *pMainForm = MainForm::getInstance();
266          if (pMainForm == NULL)          if (pMainForm == NULL)
267                  return;                  return;
268          if (pMainForm->client() == NULL)          if (pMainForm->client() == NULL)
# Line 249  void MidiInstrumentsModel::refresh (void Line 270  void MidiInstrumentsModel::refresh (void
270    
271          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
272    
273            clear();
274    
275          // Load the whole bunch of instrument items...          // Load the whole bunch of instrument items...
276          lscp_midi_instrument_t* pInstrs          lscp_midi_instrument_t *pInstrs
277                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
278          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
279                  const int iMap  = pInstrs[iInstr].map;                  const int iMap  = pInstrs[iInstr].map;
# Line 268  void MidiInstrumentsModel::refresh (void Line 291  void MidiInstrumentsModel::refresh (void
291                  pMainForm->appendMessagesError(                  pMainForm->appendMessagesError(
292                          tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));                          tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
293          }          }
294    }
295    
296    void InstrumentListModel::beginReset (void)
297    {
298    #if QT_VERSION >= 0x040600
299            QAbstractItemModel::beginResetModel();
300    #endif
301    }
302    
303    void InstrumentListModel::endReset (void)
304    {
305    #if QT_VERSION >= 0x040600
306            QAbstractItemModel::endResetModel();
307    #else
308            QAbstractItemModel::reset();
309    #endif
310    }
311    
312    
313    // Map clear.
314    void InstrumentListModel::clear (void)
315    {
316            InstrumentMap::iterator itMap = m_instruments.begin();
317            for ( ; itMap != m_instruments.end(); ++itMap) {
318                    InstrumentList& list = itMap.value();
319                    qDeleteAll(list);
320                    list.clear();
321            }
322    
323          // inform the outer world (QTableView) that our data changed          m_instruments.clear();
         QAbstractTableModel::reset();  
324  }  }
325    
326    
327  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
328  // MidiInstrumentsDelegate - table cell renderer for MIDI prog mappings  // QSampler::InstrumentListView - list view for MIDI prog mappings
329  // (doesn't actually do anything ATM, but is already there for a future  //
 // cell editor widget implementation)  
330    
331  MidiInstrumentsDelegate::MidiInstrumentsDelegate ( QObject* pParent )  // Constructor.
332          : QItemDelegate(pParent)  InstrumentListView::InstrumentListView ( QWidget *pParent )
333            : QTreeView(pParent)
334  {  {
335            m_pListModel = new InstrumentListModel(this);
336    
337            QTreeView::setModel(m_pListModel);
338    
339            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  QWidget* MidiInstrumentsDelegate::createEditor ( QWidget* pParent,  // Destructor.
362          const QStyleOptionViewItem& option, const QModelIndex& index ) const  InstrumentListView::~InstrumentListView (void)
363  {  {
364          return QItemDelegate::createEditor(pParent, option, index);          delete m_pListModel;
 //      return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);  
365  }  }
366    
367    
368  void MidiInstrumentsDelegate::setEditorData ( QWidget */*pEditor*/,  void InstrumentListView::setMidiMap ( int iMidiMap )
         const QModelIndex& /*index*/) const  
369  {  {
370            m_pListModel->setMidiMap(iMidiMap);
371  }  }
372    
373    
374  void MidiInstrumentsDelegate::setModelData ( QWidget */*pEditor*/,  int InstrumentListView::midiMap (void) const
         QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const  
375  {  {
376            return m_pListModel->midiMap();
377  }  }
378    
379    
380  void MidiInstrumentsDelegate::updateEditorGeometry ( QWidget *pEditor,  const Instrument *InstrumentListView::addInstrument (
381          const QStyleOptionViewItem& option, const QModelIndex& index) const          int iMap, int iBank, int iProg )
382  {  {
383          QItemDelegate::updateEditorGeometry(pEditor, option, index);          m_pListModel->beginReset();
384  //      if (pEditor) pEditor->setGeometry(option.rect);          const Instrument *pInstrument
385                    = m_pListModel->addInstrument(iMap, iBank, iProg);
386            m_pListModel->endReset();
387    
388            return pInstrument;
389  }  }
390    
391    
392    void InstrumentListView::removeInstrument ( Instrument *pInstrument )
393    {
394            m_pListModel->beginReset();
395            m_pListModel->removeInstrument(pInstrument);
396            m_pListModel->endReset();
397    }
398    
399    
400    void InstrumentListView::updateInstrument ( Instrument *pInstrument )
401    {
402            m_pListModel->beginReset();
403            m_pListModel->updateInstrument(pInstrument);
404            m_pListModel->endReset();
405    }
406    
407    
408    // Reposition the instrument in the model (called when map/bank/prg changed)
409    void InstrumentListView::resortInstrument ( Instrument *pInstrument )
410    {
411            m_pListModel->beginReset();
412            m_pListModel->resortInstrument(pInstrument);
413            m_pListModel->endReset();
414    }
415    
416    
417    // Refreshener.
418    void InstrumentListView::refresh (void)
419    {
420            m_pListModel->beginReset();
421            m_pListModel->refresh();
422            m_pListModel->endReset();
423    }
424    
425    
426    } // namespace QSampler
427    
428    
429  // end of qsamplerInstrumentList.cpp  // end of qsamplerInstrumentList.cpp

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

  ViewVC Help
Powered by ViewVC