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

Legend:
Removed from v.1499  
changed lines
  Added in v.1509

  ViewVC Help
Powered by ViewVC