/[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 1558 by capela, Thu Dec 6 09:35:33 2007 UTC
# Line 29  Line 29 
29  #include "qsamplerOptions.h"  #include "qsamplerOptions.h"
30  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
31    
32  #include <qapplication.h>  #include <QApplication>
33  #include <qmessagebox.h>  #include <QMessageBox>
 #include <qeventloop.h>  
 #include <qaction.h>  
 #include <qcursor.h>  
 #include <qfileinfo.h>  
   
34  #include <QMenu>  #include <QMenu>
35    #include <QAction>
36    #include <QCursor>
37    #include <QFileInfo>
38    
39  // Needed for lroundf()  // Needed for lroundf()
40  #include <math.h>  #include <math.h>
# Line 55  using namespace QSampler; Line 53  using namespace QSampler;
53    
54    
55  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
56  // MidiInstrumentsModel - data model for MIDI prog mappings (used for QTableView)  // QSampler::MidiInstrumentsModel - data model for MIDI prog mappings
57  //  //                                  (used for QTableView)
58    
59    MidiInstrumentsModel::MidiInstrumentsModel ( QObject* pParent)
60            : QAbstractTableModel(pParent)
61    {
62            m_iMidiMap = LSCP_MIDI_MAP_ALL;
63    }
64    
65    
66    int MidiInstrumentsModel::rowCount ( const QModelIndex& /*parent*/) const
67    {
68            if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
69                    int n = 0;
70                    for (InstrumentsMap::const_iterator itMap = m_instruments.begin();
71                                    itMap != m_instruments.end(); ++itMap)
72                            n += (*itMap).size();
73                    return n;
74            }
75            InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
76            if (itMap == m_instruments.end()) return 0;
77            return (*itMap).size();
78    }
79    
80    
81    int MidiInstrumentsModel::columnCount ( const QModelIndex& /*parent*/) const
82    {
83            return 9;
84    }
85    
86    
87    QVariant MidiInstrumentsModel::data ( const QModelIndex &index, int role ) const
88    {
89            if (!index.isValid())
90                    return QVariant();
91    
92            const Instrument* pInstr = NULL;
93    
94            if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
95                    int n = 0;
96                    for (InstrumentsMap::const_iterator itMap = m_instruments.begin();
97                                    itMap != m_instruments.end(); ++itMap) {
98                            n += (*itMap).size();
99                            if (index.row() < n) {
100                                    pInstr = &(*itMap)[index.row() + (*itMap).size() - n];
101                                    break;
102                            }
103                    }
104            } else {
105                    // resolve MIDI instrument map
106                    InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
107                    if (itMap == m_instruments.end()) return QVariant();
108                    // resolve instrument in that map
109                    if (index.row() >= (*itMap).size()) return QVariant();
110                    pInstr = &(*itMap)[index.row()];
111            }
112    
113            if (!pInstr)
114                    return QVariant();
115    
116            if (role == Qt::UserRole)
117                    return QVariant::fromValue((void *) pInstr);
118    
119            if (role == Qt::DisplayRole) {
120                    switch (index.column()) {
121                            case 0: return pInstr->name();
122                            case 1: return QVariant::fromValue(pInstr->map());
123                            case 2: return QVariant::fromValue(pInstr->bank());
124                            case 3: return QVariant::fromValue(pInstr->prog() + 1);
125                            case 4: return pInstr->engineName();
126                            case 5: return pInstr->instrumentFile();
127                            case 6: return QVariant::fromValue(pInstr->instrumentNr());
128                            case 7: return QString::number(pInstr->volume() * 100.0) + " %";
129                            case 8: {
130                                    switch (pInstr->loadMode()) {
131                                            case 3: return QObject::tr("Persistent");
132                                            case 2: return QObject::tr("On Demand Hold");
133                                            case 1: return QObject::tr("On Demand");
134                                    }
135                            }
136                            default: return QVariant();
137                    }
138            }
139    
140            return QVariant();
141    }
142    
143    
144    QVariant MidiInstrumentsModel::headerData (
145            int section, Qt::Orientation orientation, int role ) const
146    {
147            if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
148                    return QVariant();
149    
150            switch (section) {
151                    case 0: return tr("Name");
152                    case 1: return tr("Map");
153                    case 2: return tr("Bank");
154                    case 3: return tr("Prog");
155                    case 4: return tr("Engine");
156                    case 5: return tr("File");
157                    case 6: return tr("Nr");
158                    case 7: return tr("Vol");
159                    case 8: return tr("Mode");
160                    default: return QVariant();
161            }
162    }
163    
164  MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) {  
165      m_iMidiMap = LSCP_MIDI_MAP_ALL;  Instrument* MidiInstrumentsModel::addInstrument (
166            int iMap, int iBank, int iProg )
167    {
168            // Check it there's already one instrument item
169            // with the very same key (bank, program);
170            // if yes, just remove it without prejudice...
171            for (int i = 0; i < m_instruments[iMap].size(); i++) {
172                    if (m_instruments[iMap][i].bank() == iBank &&
173                            m_instruments[iMap][i].prog() == iProg) {
174                            m_instruments[iMap].removeAt(i);
175                            break;
176                    }
177            }
178    
179            // Resolve the appropriate place, we keep the list sorted that way ...
180            int i = 0;
181            for (; i < m_instruments[iMap].size(); ++i) {
182                    if (iBank < m_instruments[iMap][i].bank()
183                            || (iBank == m_instruments[iMap][i].bank() &&
184                                    iProg < m_instruments[iMap][i].prog())) {
185                            break;
186                    }
187            }
188    
189            m_instruments[iMap].insert(i, Instrument(iMap, iBank, iProg));
190            Instrument& instr = m_instruments[iMap][i];
191            if (!instr.getInstrument())
192                    m_instruments[iMap].removeAt(i);
193    
194            return &instr;
195  }  }
196    
197  int MidiInstrumentsModel::rowCount(const QModelIndex& /*parent*/) const {  
198      if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {  void MidiInstrumentsModel::removeInstrument (
199          int n = 0;          const Instrument& instrument )
200          for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap)  {
201              n += (*itMap).size();          const int iMap  = instrument.map();
202          return n;          const int iBank = instrument.bank();
203      }          const int iProg = instrument.prog();
204      InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);          for (int i = 0; i < m_instruments[iMap].size(); i++) {
205      if (itMap == instruments.end()) return 0;                  if (m_instruments[iMap][i].bank() == iBank &&
206      return (*itMap).size();                          m_instruments[iMap][i].prog() == iProg) {
207  }                          m_instruments[iMap].removeAt(i);
208                            break;
209  int MidiInstrumentsModel::columnCount(const QModelIndex& /*parent*/) const {                  }
210      return 9;          }
 }  
   
 QVariant MidiInstrumentsModel::data(const QModelIndex &index, int role) const {  
     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;  
211  }  }
212    
213  int MidiInstrumentsModel::midiMap() const {  
214      return m_iMidiMap;  // Reposition the instrument in the model (called when map/bank/prg changed)
215    void MidiInstrumentsModel::resort ( const Instrument& instrument )
216    {
217            const int iMap  = instrument.map();
218            const int iBank = instrument.bank();
219            const int iProg = instrument.prog();
220            // Remove given instrument from its current list
221            removeInstrument(instrument);
222            // Re-add the instrument
223            addInstrument(iMap, iBank, iProg);
224  }  }
225    
226  void MidiInstrumentsModel::refresh() {  
227          instruments.clear();  void MidiInstrumentsModel::setMidiMap ( int iMidiMap )
228    {
229            if (iMidiMap < 0)
230                    iMidiMap = LSCP_MIDI_MAP_ALL;
231    
232            m_iMidiMap = iMidiMap;
233    }
234    
235    
236    int MidiInstrumentsModel::midiMap (void) const
237    {
238            return m_iMidiMap;
239    }
240    
241    void MidiInstrumentsModel::refresh (void)
242    {
243            m_instruments.clear();
244    
245          MainForm* pMainForm = MainForm::getInstance();          MainForm* pMainForm = MainForm::getInstance();
246          if (pMainForm == NULL)          if (pMainForm == NULL)
# Line 236  void MidiInstrumentsModel::refresh() { Line 259  void MidiInstrumentsModel::refresh() {
259                  const int iProg = pInstrs[iInstr].prog;                  const int iProg = pInstrs[iInstr].prog;
260                  addInstrument(iMap, iBank, iProg);                  addInstrument(iMap, iBank, iProg);
261                  // Try to keep it snappy :)                  // Try to keep it snappy :)
262                  QApplication::processEvents(QEventLoop::ExcludeUserInput);                  QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
263          }          }
264    
265          QApplication::restoreOverrideCursor();          QApplication::restoreOverrideCursor();
266    
267          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
268                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
269                  pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));                  pMainForm->appendMessagesError(
270                            tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
271          }          }
272    
273          // inform the outer world (QTableView) that our data changed          // inform the outer world (QTableView) that our data changed
# Line 252  void MidiInstrumentsModel::refresh() { Line 276  void MidiInstrumentsModel::refresh() {
276    
277    
278  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
279  // MidiInstrumentsDelegate - table cell renderer for MIDI prog mappings  // QSampler::MidiInstrumentsDelegate - table cell renderer for MIDI prog
280  // (doesn't actually do anything ATM, but is already there for a future  // mappings (doesn't actually do anything ATM, but is already there for a
281  // cell editor widget implementation)  // future cell editor widget implementation)
282    
283  MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) {  MidiInstrumentsDelegate::MidiInstrumentsDelegate ( QObject* pParent )
284            : QItemDelegate(pParent)
285    {
286  }  }
287    
288  QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {  
289      return QItemDelegate::createEditor(parent, option, index);  QWidget* MidiInstrumentsDelegate::createEditor ( QWidget* pParent,
290      //return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);          const QStyleOptionViewItem& option, const QModelIndex& index ) const
291    {
292            return QItemDelegate::createEditor(pParent, option, index);
293    //      return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);
294  }  }
295    
296  void MidiInstrumentsDelegate::setEditorData(QWidget* /*editor*/, const QModelIndex& /*index*/) const {  
297    void MidiInstrumentsDelegate::setEditorData ( QWidget */*pEditor*/,
298            const QModelIndex& /*index*/) const
299    {
300  }  }
301    
302  void MidiInstrumentsDelegate::setModelData(QWidget* /*editor*/, QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const {  
303    void MidiInstrumentsDelegate::setModelData ( QWidget */*pEditor*/,
304            QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const
305    {
306  }  }
307    
308  void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {  
309      QItemDelegate::updateEditorGeometry(editor, option, index);  void MidiInstrumentsDelegate::updateEditorGeometry ( QWidget *pEditor,
310      //if (editor) editor->setGeometry(option.rect);          const QStyleOptionViewItem& option, const QModelIndex& index) const
311    {
312            QItemDelegate::updateEditorGeometry(pEditor, option, index);
313    //      if (pEditor) pEditor->setGeometry(option.rect);
314  }  }
315    
316    

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

  ViewVC Help
Powered by ViewVC