1 |
// qsamplerInstrumentList.cpp |
2 |
// |
3 |
/**************************************************************************** |
4 |
Copyright (C) 2003-2010, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
Copyright (C) 2007, Christian Schoenebeck |
6 |
|
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 |
#include "qsamplerOptions.h" |
29 |
#include "qsamplerMainForm.h" |
30 |
|
31 |
#include <QApplication> |
32 |
#include <QCursor> |
33 |
|
34 |
|
35 |
namespace QSampler { |
36 |
|
37 |
//------------------------------------------------------------------------- |
38 |
// QSampler::InstrumentListModel - data model for MIDI prog mappings |
39 |
// |
40 |
|
41 |
InstrumentListModel::InstrumentListModel ( QObject *pParent ) |
42 |
: QAbstractItemModel(pParent) |
43 |
{ |
44 |
m_iMidiMap = LSCP_MIDI_MAP_ALL; |
45 |
|
46 |
QAbstractItemModel::reset(); |
47 |
} |
48 |
|
49 |
InstrumentListModel::~InstrumentListModel (void) |
50 |
{ |
51 |
clear(); |
52 |
} |
53 |
|
54 |
|
55 |
int InstrumentListModel::rowCount ( const QModelIndex& /*parent*/) const |
56 |
{ |
57 |
int nrows = 0; |
58 |
|
59 |
if (m_iMidiMap == LSCP_MIDI_MAP_ALL) { |
60 |
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 |
} |
68 |
|
69 |
return nrows; |
70 |
} |
71 |
|
72 |
|
73 |
int InstrumentListModel::columnCount ( const QModelIndex& /*parent*/) const |
74 |
{ |
75 |
return 9; |
76 |
} |
77 |
|
78 |
|
79 |
QVariant InstrumentListModel::data ( |
80 |
const QModelIndex &index, int role ) const |
81 |
{ |
82 |
if (!index.isValid()) |
83 |
return QVariant(); |
84 |
|
85 |
const Instrument *pInstr |
86 |
= static_cast<Instrument *> (index.internalPointer()); |
87 |
|
88 |
if (pInstr && role == Qt::DisplayRole) { |
89 |
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 |
case 3: return QVariant::fromValue(pInstr->prog() + 1); |
94 |
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 |
case 3: return tr("Persistent"); |
101 |
case 2: return tr("On Demand Hold"); |
102 |
case 1: return tr("On Demand"); |
103 |
} |
104 |
} |
105 |
default: |
106 |
break; |
107 |
} |
108 |
} |
109 |
|
110 |
return QVariant(); |
111 |
} |
112 |
|
113 |
|
114 |
QModelIndex InstrumentListModel::index ( |
115 |
int row, int col, const QModelIndex& /*parent*/ ) const |
116 |
{ |
117 |
const Instrument *pInstr = NULL; |
118 |
|
119 |
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 |
} |
140 |
|
141 |
if (pInstr) |
142 |
return createIndex(row, col, (void *) pInstr); |
143 |
else |
144 |
return QModelIndex(); |
145 |
} |
146 |
|
147 |
|
148 |
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 |
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 |
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 |
break; |
203 |
} |
204 |
} |
205 |
|
206 |
// Resolve the appropriate place, we keep the list sorted that way... |
207 |
int i = 0; |
208 |
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 |
break; |
213 |
} |
214 |
} |
215 |
|
216 |
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 |
|
224 |
return pInstr; |
225 |
} |
226 |
|
227 |
|
228 |
void InstrumentListModel::removeInstrument ( const Instrument *pInstrument ) |
229 |
{ |
230 |
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 |
} |
244 |
} |
245 |
} |
246 |
|
247 |
|
248 |
// Reposition the instrument in the model (called when map/bank/prg changed) |
249 |
void InstrumentListModel::updateInstrument ( const Instrument *pInstrument ) |
250 |
{ |
251 |
const int iMap = pInstrument->map(); |
252 |
const int iBank = pInstrument->bank(); |
253 |
const int iProg = pInstrument->prog(); |
254 |
|
255 |
// Remove given instrument from its current list... |
256 |
removeInstrument(pInstrument); |
257 |
|
258 |
// Re-add the instrument... |
259 |
addInstrument(iMap, iBank, iProg); |
260 |
} |
261 |
|
262 |
|
263 |
void InstrumentListModel::refresh (void) |
264 |
{ |
265 |
MainForm *pMainForm = MainForm::getInstance(); |
266 |
if (pMainForm == NULL) |
267 |
return; |
268 |
if (pMainForm->client() == NULL) |
269 |
return; |
270 |
|
271 |
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
272 |
|
273 |
clear(); |
274 |
|
275 |
// Load the whole bunch of instrument items... |
276 |
lscp_midi_instrument_t *pInstrs |
277 |
= ::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 |
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
285 |
} |
286 |
|
287 |
QApplication::restoreOverrideCursor(); |
288 |
|
289 |
if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) { |
290 |
pMainForm->appendMessagesClient("lscp_list_midi_instruments"); |
291 |
pMainForm->appendMessagesError( |
292 |
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 |
m_instruments.clear(); |
324 |
} |
325 |
|
326 |
|
327 |
//------------------------------------------------------------------------- |
328 |
// QSampler::InstrumentListView - list view for MIDI prog mappings |
329 |
// |
330 |
|
331 |
// Constructor. |
332 |
InstrumentListView::InstrumentListView ( QWidget *pParent ) |
333 |
: QTreeView(pParent) |
334 |
{ |
335 |
m_pListModel = new InstrumentListModel(this); |
336 |
|
337 |
QTreeView::setModel(m_pListModel); |
338 |
} |
339 |
|
340 |
|
341 |
// Destructor. |
342 |
InstrumentListView::~InstrumentListView (void) |
343 |
{ |
344 |
delete m_pListModel; |
345 |
} |
346 |
|
347 |
|
348 |
void InstrumentListView::setMidiMap ( int iMidiMap ) |
349 |
{ |
350 |
m_pListModel->setMidiMap(iMidiMap); |
351 |
} |
352 |
|
353 |
|
354 |
int InstrumentListView::midiMap (void) const |
355 |
{ |
356 |
return m_pListModel->midiMap(); |
357 |
} |
358 |
|
359 |
|
360 |
const Instrument *InstrumentListView::addInstrument ( |
361 |
int iMap, int iBank, int iProg ) |
362 |
{ |
363 |
m_pListModel->beginReset(); |
364 |
const Instrument *pInstrument |
365 |
= m_pListModel->addInstrument(iMap, iBank, iProg); |
366 |
m_pListModel->endReset(); |
367 |
|
368 |
return pInstrument; |
369 |
} |
370 |
|
371 |
|
372 |
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 |
// end of qsamplerInstrumentList.cpp |