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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2070 - (hide annotations) (download)
Mon Mar 15 17:06:46 2010 UTC (14 years, 1 month ago) by capela
File size: 10793 byte(s)
- Still trying to fix instrument list view/model for Qt >= 4.6. (part 4/n)

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 capela 2065 : QAbstractItemModel(pParent), m_iMidiMap(LSCP_MIDI_MAP_ALL)
43 capela 1509 {
44 capela 2068 // QAbstractItemModel::reset();
45 schoenebeck 1461 }
46 capela 971
47 capela 2064 InstrumentListModel::~InstrumentListModel (void)
48     {
49     clear();
50     }
51 capela 1509
52 capela 2064
53     int InstrumentListModel::rowCount ( const QModelIndex& /*parent*/) const
54 capela 1509 {
55 capela 2064 int nrows = 0;
56    
57 capela 1509 if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
58 capela 2064 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 capela 1509 }
66 capela 2064
67     return nrows;
68 schoenebeck 1461 }
69    
70 capela 1509
71 capela 2064 int InstrumentListModel::columnCount ( const QModelIndex& /*parent*/) const
72 capela 1509 {
73     return 9;
74 schoenebeck 1461 }
75    
76    
77 capela 2064 QVariant InstrumentListModel::data (
78     const QModelIndex &index, int role ) const
79 capela 1509 {
80     if (!index.isValid())
81     return QVariant();
82 capela 2064 const Instrument *pInstr
83     = static_cast<Instrument *> (index.internalPointer());
84 schoenebeck 1461
85 capela 2064 if (pInstr && role == Qt::DisplayRole) {
86 capela 1509 switch (index.column()) {
87     case 0: return pInstr->name();
88     case 1: return QVariant::fromValue(pInstr->map());
89     case 2: return QVariant::fromValue(pInstr->bank());
90 capela 1526 case 3: return QVariant::fromValue(pInstr->prog() + 1);
91 capela 1509 case 4: return pInstr->engineName();
92     case 5: return pInstr->instrumentFile();
93     case 6: return QVariant::fromValue(pInstr->instrumentNr());
94     case 7: return QString::number(pInstr->volume() * 100.0) + " %";
95     case 8: {
96     switch (pInstr->loadMode()) {
97 capela 2064 case 3: return tr("Persistent");
98     case 2: return tr("On Demand Hold");
99     case 1: return tr("On Demand");
100 capela 1509 }
101     }
102 capela 2064 default:
103     break;
104 capela 1509 }
105     }
106    
107     return QVariant();
108 schoenebeck 1461 }
109    
110    
111 capela 2064 QModelIndex InstrumentListModel::index (
112     int row, int col, const QModelIndex& /*parent*/ ) const
113 capela 1509 {
114 capela 2064 const Instrument *pInstr = NULL;
115 capela 1509
116 capela 2064 if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
117     int nrows = 0;
118     InstrumentMap::const_iterator itMap = m_instruments.constBegin();
119     for ( ; itMap != m_instruments.constEnd(); ++itMap) {
120     const InstrumentList& list = *itMap;
121     nrows += list.size();
122     if (row < nrows) {
123     pInstr = list.at(row + list.size() - nrows);
124     break;
125     }
126     }
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 capela 1509 }
136 capela 2064
137     if (pInstr)
138     return createIndex(row, col, (void *) pInstr);
139     else
140     return QModelIndex();
141 schoenebeck 1461 }
142    
143    
144 capela 2065 QModelIndex InstrumentListModel::parent ( const QModelIndex& /*child*/ ) const
145 capela 2064 {
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 capela 1509 int iMap, int iBank, int iProg )
188     {
189     // Check it there's already one instrument item
190     // with the very same key (bank, program);
191     // if yes, just remove it without prejudice...
192 capela 2064 InstrumentList& list = m_instruments[iMap];
193     for (int i = 0; i < list.size(); ++i) {
194     const Instrument *pInstr = list.at(i);
195     if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
196     delete pInstr;
197     list.removeAt(i);
198 capela 1509 break;
199     }
200     }
201 schoenebeck 1461
202 capela 2064 // Resolve the appropriate place, we keep the list sorted that way...
203 capela 1509 int i = 0;
204 capela 2064 for ( ; i < list.size(); ++i) {
205     const Instrument *pInstr = list.at(i);
206     if (iBank < pInstr->bank()
207     || (iBank == pInstr->bank() && iProg < pInstr->prog())) {
208 capela 1509 break;
209 capela 1523 }
210     }
211 schoenebeck 1461
212 capela 2064 Instrument *pInstr = new Instrument(iMap, iBank, iProg);
213     if (pInstr->getInstrument()) {
214     list.insert(i, pInstr);
215     } else {
216     delete pInstr;
217     pInstr = NULL;
218     }
219 capela 1509
220 capela 2064 return pInstr;
221 schoenebeck 1461 }
222    
223 capela 1509
224 capela 2070 void InstrumentListModel::removeInstrument ( Instrument *pInstrument )
225 capela 1509 {
226 capela 2064 const int iMap = pInstrument->map();
227     const int iBank = pInstrument->bank();
228     const int iProg = pInstrument->prog();
229    
230     if (m_instruments.contains(iMap)) {
231     InstrumentList& list = m_instruments[iMap];
232     for (int i = 0; i < list.size(); ++i) {
233     const Instrument *pInstr = list.at(i);
234     if (pInstr->bank() == iBank && pInstr->prog() == iProg) {
235     delete pInstr;
236     list.removeAt(i);
237     break;
238     }
239 capela 1509 }
240     }
241 schoenebeck 1492 }
242    
243 capela 1509
244 capela 2070 void InstrumentListModel::updateInstrument ( Instrument *pInstrument )
245     {
246     pInstrument->getInstrument();
247     }
248    
249    
250 capela 1509 // Reposition the instrument in the model (called when map/bank/prg changed)
251 capela 2070 void InstrumentListModel::resortInstrument ( Instrument *pInstrument )
252 capela 1509 {
253 capela 2064 const int iMap = pInstrument->map();
254     const int iBank = pInstrument->bank();
255     const int iProg = pInstrument->prog();
256 schoenebeck 1492
257 capela 2064 // Remove given instrument from its current list...
258     removeInstrument(pInstrument);
259 schoenebeck 1461
260 capela 2064 // Re-add the instrument...
261     addInstrument(iMap, iBank, iProg);
262 schoenebeck 1461 }
263    
264 capela 1509
265 capela 2064 void InstrumentListModel::refresh (void)
266 capela 1509 {
267 capela 2064 MainForm *pMainForm = MainForm::getInstance();
268 schoenebeck 1461 if (pMainForm == NULL)
269     return;
270     if (pMainForm->client() == NULL)
271     return;
272    
273     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
274    
275 capela 2064 clear();
276    
277 schoenebeck 1461 // Load the whole bunch of instrument items...
278 capela 2064 lscp_midi_instrument_t *pInstrs
279 schoenebeck 1461 = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
280     for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
281     const int iMap = pInstrs[iInstr].map;
282     const int iBank = pInstrs[iInstr].bank;
283     const int iProg = pInstrs[iInstr].prog;
284     addInstrument(iMap, iBank, iProg);
285     // Try to keep it snappy :)
286 capela 1499 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
287 schoenebeck 1461 }
288    
289     QApplication::restoreOverrideCursor();
290    
291     if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
292     pMainForm->appendMessagesClient("lscp_list_midi_instruments");
293 capela 1509 pMainForm->appendMessagesError(
294     tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
295 schoenebeck 1461 }
296 capela 2064 }
297 schoenebeck 1461
298 capela 2064 void InstrumentListModel::beginReset (void)
299     {
300     #if QT_VERSION >= 0x040600
301     QAbstractItemModel::beginResetModel();
302     #endif
303 schoenebeck 1461 }
304    
305 capela 2064 void InstrumentListModel::endReset (void)
306     {
307     #if QT_VERSION >= 0x040600
308     QAbstractItemModel::endResetModel();
309     #else
310     QAbstractItemModel::reset();
311     #endif
312     }
313 schoenebeck 1461
314 capela 2064
315     // Map clear.
316     void InstrumentListModel::clear (void)
317     {
318     InstrumentMap::iterator itMap = m_instruments.begin();
319     for ( ; itMap != m_instruments.end(); ++itMap) {
320     InstrumentList& list = itMap.value();
321     qDeleteAll(list);
322     list.clear();
323     }
324    
325     m_instruments.clear();
326     }
327    
328    
329 schoenebeck 1492 //-------------------------------------------------------------------------
330 capela 2064 // QSampler::InstrumentListView - list view for MIDI prog mappings
331     //
332 schoenebeck 1492
333 capela 2064 // Constructor.
334     InstrumentListView::InstrumentListView ( QWidget *pParent )
335     : QTreeView(pParent)
336 capela 1509 {
337 capela 2064 m_pListModel = new InstrumentListModel(this);
338    
339     QTreeView::setModel(m_pListModel);
340 capela 2065
341     QTreeView::setRootIsDecorated(false);
342     QTreeView::setUniformRowHeights(true);
343     QTreeView::setAlternatingRowColors(true);
344     QTreeView::setSelectionBehavior(QAbstractItemView::SelectRows);
345     QTreeView::setSelectionMode(QAbstractItemView::SingleSelection);
346 capela 2069 QTreeView::setItemsExpandable(false);
347    
348 capela 2065 QHeaderView *pHeader = QTreeView::header();
349     pHeader->setDefaultAlignment(Qt::AlignLeft);
350     pHeader->setMovable(false);
351     pHeader->setStretchLastSection(true);
352     pHeader->resizeSection(0, 120); // Name
353     QTreeView::resizeColumnToContents(1); // Map
354     QTreeView::resizeColumnToContents(2); // Bank
355     QTreeView::resizeColumnToContents(3); // Prog
356     QTreeView::resizeColumnToContents(4); // Engine
357     pHeader->resizeSection(5, 240); // File
358     QTreeView::resizeColumnToContents(6); // Nr
359     pHeader->resizeSection(7, 60); // Vol
360 schoenebeck 1461 }
361    
362 capela 1509
363 capela 2064 // Destructor.
364     InstrumentListView::~InstrumentListView (void)
365 capela 1509 {
366 capela 2064 delete m_pListModel;
367 schoenebeck 1461 }
368    
369 capela 1509
370 capela 2064 void InstrumentListView::setMidiMap ( int iMidiMap )
371 capela 1509 {
372 capela 2064 m_pListModel->setMidiMap(iMidiMap);
373 schoenebeck 1461 }
374    
375 capela 1509
376 capela 2064 int InstrumentListView::midiMap (void) const
377 capela 1509 {
378 capela 2064 return m_pListModel->midiMap();
379 schoenebeck 1461 }
380    
381 capela 1509
382 capela 2064 const Instrument *InstrumentListView::addInstrument (
383     int iMap, int iBank, int iProg )
384 capela 1509 {
385 capela 2064 m_pListModel->beginReset();
386     const Instrument *pInstrument
387     = m_pListModel->addInstrument(iMap, iBank, iProg);
388     m_pListModel->endReset();
389    
390     return pInstrument;
391 schoenebeck 1461 }
392    
393 capela 1464
394 capela 2070 void InstrumentListView::removeInstrument ( Instrument *pInstrument )
395 capela 2064 {
396     m_pListModel->beginReset();
397     m_pListModel->removeInstrument(pInstrument);
398     m_pListModel->endReset();
399     }
400    
401    
402 capela 2070 void InstrumentListView::updateInstrument ( Instrument *pInstrument )
403 capela 2064 {
404     m_pListModel->beginReset();
405     m_pListModel->updateInstrument(pInstrument);
406 capela 2068 m_pListModel->endReset();
407     }
408 capela 2064
409    
410 capela 2070 // Reposition the instrument in the model (called when map/bank/prg changed)
411     void InstrumentListView::resortInstrument ( Instrument *pInstrument )
412     {
413     m_pListModel->beginReset();
414     m_pListModel->resortInstrument(pInstrument);
415     m_pListModel->endReset();
416     }
417    
418    
419 capela 2064 // Refreshener.
420     void InstrumentListView::refresh (void)
421     {
422     m_pListModel->beginReset();
423     m_pListModel->refresh();
424     m_pListModel->endReset();
425     }
426    
427    
428     } // namespace QSampler
429    
430    
431 capela 971 // end of qsamplerInstrumentList.cpp

  ViewVC Help
Powered by ViewVC