/[svn]/qsampler/trunk/src/qsamplerInstrumentList.cpp
ViewVC logotype

Annotation of /qsampler/trunk/src/qsamplerInstrumentList.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2064 - (hide annotations) (download)
Fri Mar 12 16:02:32 2010 UTC (14 years, 1 month ago) by capela
File size: 9751 byte(s)
* Attempt to fix broken instrument list model/view for Qt >= 4.6.

1 capela 971 // qsamplerInstrumentList.cpp
2     //
3     /****************************************************************************
4 capela 2064 Copyright (C) 2003-2010, rncbc aka Rui Nuno Capela. All rights reserved.
5 capela 1464 Copyright (C) 2007, Christian Schoenebeck
6 capela 971
7     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20    
21     *****************************************************************************/
22    
23     #include "qsamplerAbout.h"
24     #include "qsamplerInstrumentList.h"
25    
26     #include "qsamplerInstrument.h"
27    
28 capela 987 #include "qsamplerOptions.h"
29 capela 971 #include "qsamplerMainForm.h"
30    
31 capela 1499 #include <QApplication>
32     #include <QCursor>
33 schoenebeck 1461
34 capela 971
35 capela 2064 namespace QSampler {
36 capela 971
37 schoenebeck 1492 //-------------------------------------------------------------------------
38 capela 2064 // QSampler::InstrumentListModel - data model for MIDI prog mappings
39     //
40 capela 971
41 capela 2064 InstrumentListModel::InstrumentListModel ( QObject *pParent )
42     : QAbstractItemModel(pParent)
43 capela 1509 {
44     m_iMidiMap = LSCP_MIDI_MAP_ALL;
45 capela 2064
46     QAbstractItemModel::reset();
47 schoenebeck 1461 }
48 capela 971
49 capela 2064 InstrumentListModel::~InstrumentListModel (void)
50     {
51     clear();
52     }
53 capela 1509
54 capela 2064
55     int InstrumentListModel::rowCount ( const QModelIndex& /*parent*/) const
56 capela 1509 {
57 capela 2064 int nrows = 0;
58    
59 capela 1509 if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
60 capela 2064 InstrumentMap::const_iterator itMap = m_instruments.constBegin();
61     for ( ; itMap != m_instruments.constEnd(); ++itMap)
62     nrows += (*itMap).size();
63     } else {
64     InstrumentMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
65     if (itMap != m_instruments.constEnd())
66     nrows += (*itMap).size();
67 capela 1509 }
68 capela 2064
69     return nrows;
70 schoenebeck 1461 }
71    
72 capela 1509
73 capela 2064 int InstrumentListModel::columnCount ( const QModelIndex& /*parent*/) const
74 capela 1509 {
75     return 9;
76 schoenebeck 1461 }
77    
78    
79 capela 2064 QVariant InstrumentListModel::data (
80     const QModelIndex &index, int role ) const
81 capela 1509 {
82     if (!index.isValid())
83     return QVariant();
84 schoenebeck 1492
85 capela 2064 const Instrument *pInstr
86     = static_cast<Instrument *> (index.internalPointer());
87 schoenebeck 1461
88 capela 2064 if (pInstr && role == Qt::DisplayRole) {
89 capela 1509 switch (index.column()) {
90     case 0: return pInstr->name();
91     case 1: return QVariant::fromValue(pInstr->map());
92     case 2: return QVariant::fromValue(pInstr->bank());
93 capela 1526 case 3: return QVariant::fromValue(pInstr->prog() + 1);
94 capela 1509 case 4: return pInstr->engineName();
95     case 5: return pInstr->instrumentFile();
96     case 6: return QVariant::fromValue(pInstr->instrumentNr());
97     case 7: return QString::number(pInstr->volume() * 100.0) + " %";
98     case 8: {
99     switch (pInstr->loadMode()) {
100 capela 2064 case 3: return tr("Persistent");
101     case 2: return tr("On Demand Hold");
102     case 1: return tr("On Demand");
103 capela 1509 }
104     }
105 capela 2064 default:
106     break;
107 capela 1509 }
108     }
109    
110     return QVariant();
111 schoenebeck 1461 }
112    
113    
114 capela 2064 QModelIndex InstrumentListModel::index (
115     int row, int col, const QModelIndex& /*parent*/ ) const
116 capela 1509 {
117 capela 2064 const Instrument *pInstr = NULL;
118 capela 1509
119 capela 2064 if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
120     int nrows = 0;
121     InstrumentMap::const_iterator itMap = m_instruments.constBegin();
122     for ( ; itMap != m_instruments.constEnd(); ++itMap) {
123     const InstrumentList& list = *itMap;
124     nrows += list.size();
125     if (row < nrows) {
126     pInstr = list.at(row + list.size() - nrows);
127     break;
128     }
129     }
130     } else {
131     // Resolve MIDI instrument map...
132     InstrumentMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
133     if (itMap != m_instruments.constEnd()) {
134     const InstrumentList& list = *itMap;
135     // resolve instrument in that map
136     if (row < list.size())
137     pInstr = list.at(row);
138     }
139 capela 1509 }
140 capela 2064
141     if (pInstr)
142     return createIndex(row, col, (void *) pInstr);
143     else
144     return QModelIndex();
145 schoenebeck 1461 }
146    
147    
148 capela 2064 QModelIndex InstrumentListModel::parent ( const QModelIndex& child ) const
149     {
150     return QModelIndex();
151     }
152    
153    
154     QVariant InstrumentListModel::headerData (
155     int section, Qt::Orientation orientation, int role ) const
156     {
157     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
158     switch (section) {
159     case 0: return tr("Name");
160     case 1: return tr("Map");
161     case 2: return tr("Bank");
162     case 3: return tr("Prog");
163     case 4: return tr("Engine");
164     case 5: return tr("File");
165     case 6: return tr("Nr");
166     case 7: return tr("Vol");
167     case 8: return tr("Mode");
168     }
169     }
170    
171     return QAbstractItemModel::headerData(section, orientation, role);
172     }
173    
174    
175     void InstrumentListModel::setMidiMap ( int iMidiMap )
176     {
177     if (iMidiMap < 0)
178     iMidiMap = LSCP_MIDI_MAP_ALL;
179    
180     m_iMidiMap = iMidiMap;
181     }
182    
183    
184     int InstrumentListModel::midiMap (void) const
185     {
186     return m_iMidiMap;
187     }
188    
189    
190     const Instrument *InstrumentListModel::addInstrument (
191 capela 1509 int iMap, int iBank, int iProg )
192     {
193     // Check it there's already one instrument item
194     // with the very same key (bank, program);
195     // if yes, just remove it without prejudice...
196 capela 2064 InstrumentList& list = m_instruments[iMap];
197     for (int i = 0; i < list.size(); ++i) {
198     const Instrument *pInstr = list.at(i);
199     if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
200     delete pInstr;
201     list.removeAt(i);
202 capela 1509 break;
203     }
204     }
205 schoenebeck 1461
206 capela 2064 // Resolve the appropriate place, we keep the list sorted that way...
207 capela 1509 int i = 0;
208 capela 2064 for ( ; i < list.size(); ++i) {
209     const Instrument *pInstr = list.at(i);
210     if (iBank < pInstr->bank()
211     || (iBank == pInstr->bank() && iProg < pInstr->prog())) {
212 capela 1509 break;
213 capela 1523 }
214     }
215 schoenebeck 1461
216 capela 2064 Instrument *pInstr = new Instrument(iMap, iBank, iProg);
217     if (pInstr->getInstrument()) {
218     list.insert(i, pInstr);
219     } else {
220     delete pInstr;
221     pInstr = NULL;
222     }
223 capela 1509
224 capela 2064 return pInstr;
225 schoenebeck 1461 }
226    
227 capela 1509
228 capela 2064 void InstrumentListModel::removeInstrument ( const Instrument *pInstrument )
229 capela 1509 {
230 capela 2064 const int iMap = pInstrument->map();
231     const int iBank = pInstrument->bank();
232     const int iProg = pInstrument->prog();
233    
234     if (m_instruments.contains(iMap)) {
235     InstrumentList& list = m_instruments[iMap];
236     for (int i = 0; i < list.size(); ++i) {
237     const Instrument *pInstr = list.at(i);
238     if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
239     delete pInstr;
240     list.removeAt(i);
241     break;
242     }
243 capela 1509 }
244     }
245 schoenebeck 1492 }
246    
247 capela 1509
248     // Reposition the instrument in the model (called when map/bank/prg changed)
249 capela 2064 void InstrumentListModel::updateInstrument ( const Instrument *pInstrument )
250 capela 1509 {
251 capela 2064 const int iMap = pInstrument->map();
252     const int iBank = pInstrument->bank();
253     const int iProg = pInstrument->prog();
254 schoenebeck 1492
255 capela 2064 // Remove given instrument from its current list...
256     removeInstrument(pInstrument);
257 schoenebeck 1461
258 capela 2064 // Re-add the instrument...
259     addInstrument(iMap, iBank, iProg);
260 schoenebeck 1461 }
261    
262 capela 1509
263 capela 2064 void InstrumentListModel::refresh (void)
264 capela 1509 {
265 capela 2064 MainForm *pMainForm = MainForm::getInstance();
266 schoenebeck 1461 if (pMainForm == NULL)
267     return;
268     if (pMainForm->client() == NULL)
269     return;
270    
271     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
272    
273 capela 2064 clear();
274    
275 schoenebeck 1461 // Load the whole bunch of instrument items...
276 capela 2064 lscp_midi_instrument_t *pInstrs
277 schoenebeck 1461 = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
278     for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
279     const int iMap = pInstrs[iInstr].map;
280     const int iBank = pInstrs[iInstr].bank;
281     const int iProg = pInstrs[iInstr].prog;
282     addInstrument(iMap, iBank, iProg);
283     // Try to keep it snappy :)
284 capela 1499 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
285 schoenebeck 1461 }
286    
287     QApplication::restoreOverrideCursor();
288    
289     if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
290     pMainForm->appendMessagesClient("lscp_list_midi_instruments");
291 capela 1509 pMainForm->appendMessagesError(
292     tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
293 schoenebeck 1461 }
294 capela 2064 }
295 schoenebeck 1461
296 capela 2064 void InstrumentListModel::beginReset (void)
297     {
298     #if QT_VERSION >= 0x040600
299     QAbstractItemModel::beginResetModel();
300     #endif
301 schoenebeck 1461 }
302    
303 capela 2064 void InstrumentListModel::endReset (void)
304     {
305     #if QT_VERSION >= 0x040600
306     QAbstractItemModel::endResetModel();
307     #else
308     QAbstractItemModel::reset();
309     #endif
310     }
311 schoenebeck 1461
312 capela 2064
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     m_instruments.clear();
324     }
325    
326    
327 schoenebeck 1492 //-------------------------------------------------------------------------
328 capela 2064 // QSampler::InstrumentListView - list view for MIDI prog mappings
329     //
330 schoenebeck 1492
331 capela 2064 // Constructor.
332     InstrumentListView::InstrumentListView ( QWidget *pParent )
333     : QTreeView(pParent)
334 capela 1509 {
335 capela 2064 m_pListModel = new InstrumentListModel(this);
336    
337     QTreeView::setModel(m_pListModel);
338 schoenebeck 1461 }
339    
340 capela 1509
341 capela 2064 // Destructor.
342     InstrumentListView::~InstrumentListView (void)
343 capela 1509 {
344 capela 2064 delete m_pListModel;
345 schoenebeck 1461 }
346    
347 capela 1509
348 capela 2064 void InstrumentListView::setMidiMap ( int iMidiMap )
349 capela 1509 {
350 capela 2064 m_pListModel->setMidiMap(iMidiMap);
351 schoenebeck 1461 }
352    
353 capela 1509
354 capela 2064 int InstrumentListView::midiMap (void) const
355 capela 1509 {
356 capela 2064 return m_pListModel->midiMap();
357 schoenebeck 1461 }
358    
359 capela 1509
360 capela 2064 const Instrument *InstrumentListView::addInstrument (
361     int iMap, int iBank, int iProg )
362 capela 1509 {
363 capela 2064 m_pListModel->beginReset();
364     const Instrument *pInstrument
365     = m_pListModel->addInstrument(iMap, iBank, iProg);
366     m_pListModel->endReset();
367    
368     return pInstrument;
369 schoenebeck 1461 }
370    
371 capela 1464
372 capela 2064 void InstrumentListView::removeInstrument ( const Instrument *pInstrument )
373     {
374     m_pListModel->beginReset();
375     m_pListModel->removeInstrument(pInstrument);
376     m_pListModel->endReset();
377     }
378    
379    
380     // Reposition the instrument in the model (called when map/bank/prg changed)
381     void InstrumentListView::updateInstrument ( const Instrument *pInstrument )
382     {
383     m_pListModel->beginReset();
384     m_pListModel->updateInstrument(pInstrument);
385     m_pListModel->endReset();}
386    
387    
388     // Refreshener.
389     void InstrumentListView::refresh (void)
390     {
391     m_pListModel->beginReset();
392     m_pListModel->refresh();
393     m_pListModel->endReset();
394     }
395    
396    
397     } // namespace QSampler
398    
399    
400 capela 971 // end of qsamplerInstrumentList.cpp

  ViewVC Help
Powered by ViewVC