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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1464 - (show annotations) (download)
Thu Nov 1 17:14:21 2007 UTC (16 years, 5 months ago) by capela
File size: 24476 byte(s)
- Qt4 migration: missing copyright headers update.

1 // qsamplerInstrumentList.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2003-2007, 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 #include "qsamplerInstrumentForm.h"
28
29 #include "qsamplerOptions.h"
30 #include "qsamplerMainForm.h"
31
32 #include <qapplication.h>
33 #include <qmessagebox.h>
34 #include <qeventloop.h>
35 #include <qaction.h>
36 #include <qcursor.h>
37 #include <qfileinfo.h>
38
39 #include <QMenu>
40
41 // Needed for lroundf()
42 #include <math.h>
43
44 #ifndef CONFIG_ROUND
45 static inline long lroundf ( float x )
46 {
47 if (x >= 0.0f)
48 return long(x + 0.5f);
49 else
50 return long(x - 0.5f);
51 }
52 #endif
53
54 using namespace QSampler;
55
56 //----------------------------------------------------------------------
57 // class qsamplerInstrumentGroup -- custom group list view item.
58 //
59
60 #if 0
61 // Constructors.
62 qsamplerInstrumentGroup::qsamplerInstrumentGroup (
63 qsamplerInstrumentList *pListView, const QString& sName,
64 QListViewItem *pItemAfter )
65 : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())
66 {
67 QListViewItem::setRenameEnabled(0, true);
68
69 QListViewItem::setPixmap(0, QPixmap(":/icons/itemGroup.png"));
70 QListViewItem::setText(0, sName);
71 }
72
73
74 qsamplerInstrumentGroup::qsamplerInstrumentGroup (
75 qsamplerInstrumentGroup *pGroupItem, const QString& sName )
76 : QListViewItem(pGroupItem, sName)
77 {
78 QListViewItem::setRenameEnabled(0, true);
79
80 QListViewItem::setPixmap(0, QPixmap(":/icons/itemGroup.png"));
81 }
82
83
84 // Default destructor.
85 qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)
86 {
87 }
88
89
90 // Instance accessors.
91 void qsamplerInstrumentGroup::setName ( const QString& sName )
92 {
93 QListViewItem::setText(0, sName);
94 }
95
96
97 QString qsamplerInstrumentGroup::name (void) const
98 {
99 return QListViewItem::text(0);
100 }
101
102
103 qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const
104 {
105 QListViewItem *pParent = QListViewItem::parent();
106 while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)
107 pParent = pParent->parent();
108 return static_cast<qsamplerInstrumentGroup *> (pParent);
109 }
110
111
112 qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const
113 {
114 return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());
115 }
116
117
118 // To show up whether its open or not.
119 void qsamplerInstrumentGroup::setOpen ( bool bOpen )
120 {
121 // Set the proper pixmap of this...
122 if (rtti() == qsamplerInstrumentList::Group) {
123 QListViewItem::setPixmap(0, QPixmap(bOpen ?
124 ":/icons/itemGroupOpen.png" : ":/icons/itemGroup.png"));
125 }
126 // Open it up...
127 QListViewItem::setOpen(bOpen);
128
129 // All ancestors should be also visible.
130 if (bOpen && QListViewItem::parent())
131 QListViewItem::parent()->setOpen(true);
132 }
133
134
135 // To virtually distinguish between list view items.
136 int qsamplerInstrumentGroup::rtti (void) const
137 {
138 return qsamplerInstrumentList::Group;
139 }
140
141
142 //----------------------------------------------------------------------
143 // class qsamplerInstrumentItem -- custom file list view item.
144 //
145
146 // Constructors.
147 qsamplerInstrumentItem::qsamplerInstrumentItem (
148 qsamplerInstrumentList *pListView,
149 qsamplerInstrument *pInstrument,
150 QListViewItem *pItemAfter )
151 : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)
152 {
153 m_pInstrument = pInstrument;
154
155 update();
156 }
157
158 qsamplerInstrumentItem::qsamplerInstrumentItem (
159 qsamplerInstrumentGroup *pGroupItem,
160 qsamplerInstrument *pInstrument )
161 : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())
162 {
163 m_pInstrument = pInstrument;
164
165 update();
166 }
167
168
169 // Default destructor.
170 qsamplerInstrumentItem::~qsamplerInstrumentItem (void)
171 {
172 if (m_pInstrument)
173 delete m_pInstrument;
174 }
175
176
177 // To virtually distinguish between list view items.
178 int qsamplerInstrumentItem::rtti (void) const
179 {
180 return qsamplerInstrumentList::Item;
181 }
182
183
184 // Payload accessor.
185 qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const
186 {
187 return m_pInstrument;
188 }
189
190
191 // Item refreshment.
192 void qsamplerInstrumentItem::update (void)
193 {
194 QListViewItem::setPixmap(0, QPixmap(":/icons/itemFile.png"));
195
196 const QString s = "-";
197 if (m_pInstrument) {
198 setText(0, m_pInstrument->name());
199 setText(1, QString::number(m_pInstrument->map()));
200 setText(2, QString::number(m_pInstrument->bank()));
201 setText(3, QString::number(m_pInstrument->prog() + 1));
202 setText(4, m_pInstrument->engineName());
203 setText(5, QFileInfo(m_pInstrument->instrumentFile()).fileName());
204 setText(6, QString::number(m_pInstrument->instrumentNr()));
205 setText(7, QString::number(::lroundf(100.0f * m_pInstrument->volume())));
206 QString sLoadMode = s;
207 switch (m_pInstrument->loadMode()) {
208 case 3:
209 sLoadMode = QObject::tr("Persistent");
210 break;
211 case 2:
212 sLoadMode = QObject::tr("On Demand Hold");
213 break;
214 case 1:
215 sLoadMode = QObject::tr("On Demand");
216 break;
217 }
218 setText(8, sLoadMode);
219 } else {
220 for (int i = 0; i < listView()->columns(); i++)
221 setText(i, s);
222 }
223 }
224
225
226 //----------------------------------------------------------------------------
227 // qsamplerInstrumentList -- MIDI instrument list view.
228 //
229
230 // Constructor.
231 qsamplerInstrumentList::qsamplerInstrumentList (
232 QWidget *pParent, const char *pszName )
233 : QListView(pParent, pszName)
234 {
235 m_iMidiMap = LSCP_MIDI_MAP_ALL;
236
237 // QListView::setRootIsDecorated(true);
238 QListView::setAllColumnsShowFocus(true);
239 QListView::setResizeMode(QListView::NoColumn);
240 // QListView::setAcceptDrops(true);
241 QListView::setDragAutoScroll(true);
242 QListView::setSizePolicy(
243 QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
244 // QListView::setShowToolTips(false);
245 QListView::setSortColumn(-1);
246
247 QListView::addColumn(tr("Name"));
248 QListView::addColumn(tr("Map"));
249 QListView::addColumn(tr("Bank"));
250 QListView::addColumn(tr("Prog"));
251 QListView::addColumn(tr("Engine"));
252 QListView::addColumn(tr("File"));
253 QListView::addColumn(tr("Nr"));
254 QListView::addColumn(tr("Vol"));
255 QListView::addColumn(tr("Mode"));
256
257 QListView::setColumnAlignment(1, Qt::AlignHCenter); // Map
258 QListView::setColumnAlignment(2, Qt::AlignHCenter); // Bank
259 QListView::setColumnAlignment(3, Qt::AlignHCenter); // Prog
260 QListView::setColumnAlignment(6, Qt::AlignHCenter); // Nr
261 QListView::setColumnAlignment(7, Qt::AlignHCenter); // Vol
262
263 QListView::setColumnWidth(0, 120); // Name
264 QListView::setColumnWidth(5, 240); // File
265
266 m_pNewGroupAction = new QAction(
267 QIcon(":/icons/itemGroupNew.png"),
268 tr("New &Group"), tr("Ctrl+G"), this);
269 m_pNewItemAction = new QAction(
270 QIcon(":/icons/itemNew.png"),
271 tr("New &Instrument..."), tr("Ins"), this);
272 m_pEditItemAction = new QAction(
273 QIcon(":/icons/formEdit.png"),
274 tr("&Edit..."), tr("Enter"), this);
275 m_pRenameAction = new QAction(tr("&Rename"), tr("F2"), this);
276 m_pDeleteAction = new QAction(
277 QIcon(":/icons/formRemove.png"),
278 tr("&Delete"), tr("Del"), this);
279 m_pRefreshAction = new QAction(
280 QIcon(":/icons/formRefresh.png"),
281 tr("Re&fresh"), tr("F5"), this);
282
283 m_pNewGroupAction->setToolTip(tr("New Group"));
284 m_pNewItemAction->setToolTip(tr("New Instrument"));
285 m_pEditItemAction->setToolTip(tr("Edit"));
286 m_pRenameAction->setToolTip(tr("Rename"));
287 m_pDeleteAction->setToolTip(tr("Delete"));
288 m_pRefreshAction->setToolTip(tr("Refresh"));
289
290 QObject::connect(m_pNewGroupAction,
291 SIGNAL(activated()),
292 SLOT(newGroupSlot()));
293 QObject::connect(m_pNewItemAction,
294 SIGNAL(activated()),
295 SLOT(newItemSlot()));
296 QObject::connect(m_pEditItemAction,
297 SIGNAL(activated()),
298 SLOT(editItemSlot()));
299 QObject::connect(m_pRenameAction,
300 SIGNAL(activated()),
301 SLOT(renameSlot()));
302 QObject::connect(m_pDeleteAction,
303 SIGNAL(activated()),
304 SLOT(deleteSlot()));
305 QObject::connect(m_pRefreshAction,
306 SIGNAL(activated()),
307 SLOT(refresh()));
308
309 QObject::connect(this,
310 SIGNAL(selectionChanged()),
311 SLOT(selectionChangedSlot()));
312 QObject::connect(this,
313 SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),
314 SLOT(activatedSlot(QListViewItem*)));
315 QObject::connect(this,
316 SIGNAL(returnPressed(QListViewItem*)),
317 SLOT(activatedSlot(QListViewItem*)));
318 QObject::connect(this,
319 SIGNAL(itemRenamed(QListViewItem*,int)),
320 SLOT(renamedSlot(QListViewItem*)));
321
322 selectionChangedSlot();
323 }
324
325
326 // Default destructor.
327 qsamplerInstrumentList::~qsamplerInstrumentList (void)
328 {
329 delete m_pNewGroupAction;
330 delete m_pNewItemAction;
331 delete m_pEditItemAction;
332 delete m_pRenameAction;
333 delete m_pDeleteAction;
334 }
335
336
337 // Add a new instrument item, optionally under a given group.
338 qsamplerInstrumentItem *qsamplerInstrumentList::addItem (
339 qsamplerInstrument *pInstrument,
340 qsamplerInstrumentGroup *pParentGroup )
341 {
342 // Check it there's already one instrument item
343 // with the very same key (bank, program);
344 // if yes, just remove it without prejudice...
345 qsamplerInstrumentItem *pItem = findItem(pInstrument);
346 if (pItem) {
347 // If exactly the same, just update view and bail out...
348 if (pItem->instrument() == pInstrument) {
349 pItem->update();
350 return pItem;
351 }
352 // Remove it, as instrument keys must be unique.
353 delete pItem;
354 }
355
356 // Add the new item under proper group one, if any...
357 if (pParentGroup) {
358 pParentGroup->setOpen(true);
359 pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);
360 } else {
361 pItem = new qsamplerInstrumentItem(this, pInstrument);
362 }
363
364 // Set it as current selection...
365 QListView::setSelected(pItem, true);
366
367 return pItem;
368 }
369
370
371 // Add a new instrument group, optionally under another group.
372 qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (
373 const QString& sName, qsamplerInstrumentGroup *pParentGroup )
374 {
375 qsamplerInstrumentGroup *pGroup = findGroup(sName);
376 if (pGroup == NULL) {
377 if (pParentGroup) {
378 pParentGroup->setOpen(true);
379 pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);
380 } else {
381 pGroup = new qsamplerInstrumentGroup(this, sName);
382 }
383 }
384 QListView::setSelected(pGroup, true);
385 return pGroup;
386 }
387
388
389 // Find a group item, given its name.
390 qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (
391 const QString& sName ) const
392 {
393 // Iterate all over the place to search for the group.
394 QListViewItemIterator iter((QListView *) this);
395 while (iter.current()) {
396 QListViewItem *pItem = iter.current();
397 if (pItem->rtti() == Group && pItem->text(0) == sName)
398 return static_cast<qsamplerInstrumentGroup *> (pItem);
399 ++iter;
400 }
401 // Not found.
402 return NULL;
403 }
404
405
406 // Find a file item, given its name.
407 qsamplerInstrumentItem *qsamplerInstrumentList::findItem (
408 qsamplerInstrument *pInstrument ) const
409 {
410 if (pInstrument == NULL)
411 return NULL;
412
413 // Iterate all over the place to search for the group.
414 QListViewItemIterator iter((QListView *) this);
415 while (iter.current()) {
416 QListViewItem *pListItem = iter.current();
417 if (pListItem->rtti() == Item) {
418 qsamplerInstrumentItem *pItem
419 = static_cast<qsamplerInstrumentItem *> (pListItem);
420 if (pItem && pItem->instrument()
421 && pItem->instrument()->map() == pInstrument->map()
422 && pItem->instrument()->bank() == pInstrument->bank()
423 && pItem->instrument()->prog() == pInstrument->prog())
424 return pItem;
425 }
426 ++iter;
427 }
428 // Not found.
429 return NULL;
430 }
431
432
433 // Find and return the nearest group item...
434 qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (
435 QListViewItem *pItem ) const
436 {
437 while (pItem && pItem->rtti() != Group)
438 pItem = pItem->parent();
439 return static_cast<qsamplerInstrumentGroup *> (pItem);
440 }
441
442
443 // Add a new group item below the current one.
444 void qsamplerInstrumentList::newGroupSlot (void)
445 {
446 qsamplerInstrumentGroup *pNewGroup
447 = addGroup(tr("New Group"), groupItem(QListView::selectedItem()));
448 if (pNewGroup)
449 pNewGroup->startRename(0);
450
451 selectionChangedSlot();
452 }
453
454
455 // Map selector.
456 void qsamplerInstrumentList::setMidiMap ( int iMidiMap )
457 {
458 if (iMidiMap < 0)
459 iMidiMap = LSCP_MIDI_MAP_ALL;
460
461 m_iMidiMap = iMidiMap;
462 }
463
464 int qsamplerInstrumentList::midiMap (void) const
465 {
466 return m_iMidiMap;
467 }
468
469
470 // List actions accessors.
471 QAction *qsamplerInstrumentList::newGroupAction (void) const
472 {
473 return m_pNewGroupAction;
474 }
475
476 QAction *qsamplerInstrumentList::newItemAction (void) const
477 {
478 return m_pNewItemAction;
479 }
480
481 QAction *qsamplerInstrumentList::editItemAction (void) const
482 {
483 return m_pEditItemAction;
484 }
485
486 QAction *qsamplerInstrumentList::renameAction (void) const
487 {
488 return m_pRenameAction;
489 }
490
491 QAction *qsamplerInstrumentList::deleteAction (void) const
492 {
493 return m_pDeleteAction;
494 }
495
496 QAction *qsamplerInstrumentList::refreshAction (void) const
497 {
498 return m_pRefreshAction;
499 }
500
501
502 // Add a new instrument item below the current one.
503 void qsamplerInstrumentList::newItemSlot (void)
504 {
505 qsamplerInstrument *pInstrument = new qsamplerInstrument();
506
507 qsamplerInstrumentForm form(this);
508 form.setup(pInstrument);
509 if (!form.exec()) {
510 delete pInstrument;
511 return;
512 }
513
514 // Commit...
515 pInstrument->mapInstrument();
516 // add new item to the tree...
517 addItem(pInstrument, groupItem(QListView::selectedItem()));
518 // Notify we've changes...
519 emit instrumentsChanged();
520
521 selectionChangedSlot();
522 }
523
524
525 // Edit current item below the current one.
526 void qsamplerInstrumentList::editItemSlot (void)
527 {
528 QListViewItem *pListItem = QListView::selectedItem();
529 if (pListItem == NULL)
530 return;
531 if (pListItem->rtti() == Item) {
532 qsamplerInstrument *pInstrument = NULL;
533 qsamplerInstrumentItem *pItem
534 = static_cast<qsamplerInstrumentItem *> (pListItem);
535 if (pItem)
536 pInstrument = pItem->instrument();
537 if (pInstrument) {
538 // Save current key values...
539 qsamplerInstrument oldInstrument(*pInstrument);
540 // Do the edit dance...
541 qsamplerInstrumentForm form(this);
542 form.setup(pInstrument);
543 if (form.exec()) {
544 // Commit...
545 pInstrument->mapInstrument();
546 // Check whether we changed instrument key...
547 if (oldInstrument.map() == pInstrument->map() &&
548 oldInstrument.bank() == pInstrument->bank() &&
549 oldInstrument.prog() == pInstrument->prog()) {
550 // just update tree item...
551 pItem->update();
552 } else {
553 // Unmap old instance...
554 oldInstrument.unmapInstrument();
555 // Change item tree, whether applicable...
556 if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) {
557 // Add new brand item into view...
558 addItem(pInstrument, groupItem(pListItem));
559 } else {
560 // Just remove/hide old one.
561 delete pItem;
562 }
563 }
564 // Notify we've changes...
565 emit instrumentsChanged();
566 }
567 }
568 }
569
570 selectionChangedSlot();
571 }
572
573
574 // Rename current group/item.
575 void qsamplerInstrumentList::renameSlot (void)
576 {
577 QListViewItem *pListItem = QListView::selectedItem();
578 if (pListItem)
579 pListItem->startRename(0);
580
581 selectionChangedSlot();
582 }
583
584
585 // Remove current group/item.
586 void qsamplerInstrumentList::deleteSlot (void)
587 {
588 QListViewItem *pListItem = QListView::selectedItem();
589 if (pListItem == NULL)
590 return;
591
592 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
593 if (pMainForm == NULL)
594 return;
595
596 // Prompt user if this is for real...
597 qsamplerOptions *pOptions = pMainForm->options();
598 if (pOptions && pOptions->bConfirmRemove) {
599 if (QMessageBox::warning(this,
600 QSAMPLER_TITLE ": " + tr("Warning"),
601 tr("Delete %1:\n\n"
602 "%2\n\n"
603 "Are you sure?")
604 .arg(pListItem->rtti() == Item ? tr("instrument") : tr("group"))
605 .arg(pListItem->text(0)),
606 tr("OK"), tr("Cancel")) > 0)
607 return;
608 }
609
610 // Unmap instrument entry...
611 if (pListItem->rtti() == Item) {
612 qsamplerInstrumentItem *pItem
613 = static_cast<qsamplerInstrumentItem *> (pListItem);
614 if (pItem && pItem->instrument()) {
615 pItem->instrument()->unmapInstrument();
616 emit instrumentsChanged();
617 }
618 }
619
620 // Do it for real...
621 delete pListItem;
622
623 selectionChangedSlot();
624 }
625
626
627 // In-place selection slot.
628 void qsamplerInstrumentList::selectionChangedSlot (void)
629 {
630 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
631 QListViewItem *pListItem = QListView::selectedItem();
632 bool bEnabled = (pMainForm && pMainForm->client());
633 m_pNewItemAction->setEnabled(bEnabled);
634 bEnabled = (bEnabled && pListItem != NULL);
635 m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);
636 m_pRenameAction->setEnabled(bEnabled);
637 m_pDeleteAction->setEnabled(bEnabled);
638 }
639
640
641 // In-place activation slot.
642 void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )
643 {
644 // FIXME: Hope the list view item is the one selected.
645 if (pListItem && pListItem->rtti() == Item)
646 editItemSlot();
647 }
648
649
650 // In-place aliasing slot.
651 void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )
652 {
653 if (pListItem->rtti() == Item) {
654 qsamplerInstrumentItem *pItem
655 = static_cast<qsamplerInstrumentItem *> (pListItem);
656 if (pItem && pItem->instrument()) {
657 pItem->instrument()->setName(pListItem->text(0));
658 pItem->instrument()->mapInstrument();
659 emit instrumentsChanged();
660 pItem->update();
661 }
662 }
663 }
664
665
666 // Context menu request event handler.
667 void qsamplerInstrumentList::contextMenuEvent (
668 QContextMenuEvent *pContextMenuEvent )
669 {
670 if (!m_pNewItemAction->isEnabled())
671 return;
672
673 QPopupMenu menu(this);
674
675 // Construct context menu.
676 m_pNewItemAction->addTo(&menu);
677 // m_pNewGroupAction->addTo(&menu);
678 menu.insertSeparator();
679 m_pEditItemAction->addTo(&menu);
680 m_pRenameAction->addTo(&menu);
681 m_pDeleteAction->addTo(&menu);
682 menu.insertSeparator();
683 m_pRefreshAction->addTo(&menu);
684
685 menu.exec(pContextMenuEvent->globalPos());
686 }
687
688
689 // General reloader.
690 void qsamplerInstrumentList::refresh (void)
691 {
692 clear();
693
694 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
695 if (pMainForm == NULL)
696 return;
697 if (pMainForm->client() == NULL)
698 return;
699
700 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
701
702 // Load the whole bunch of instrument items...
703 qsamplerInstrumentItem *pItem = NULL;
704 lscp_midi_instrument_t *pInstrs
705 = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
706 for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
707 int iMap = pInstrs[iInstr].map;
708 int iBank = pInstrs[iInstr].bank;
709 int iProg = pInstrs[iInstr].prog;
710 qsamplerInstrument *pInstrument
711 = new qsamplerInstrument(iMap, iBank, iProg);
712 if (pInstrument->getInstrument())
713 pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);
714 // Try to keep it snappy :)
715 QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
716 }
717
718 QApplication::restoreOverrideCursor();
719
720 if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
721 pMainForm->appendMessagesClient("lscp_list_midi_instruments");
722 pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
723 }
724
725 selectionChangedSlot();
726 }
727 #endif
728
729 MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) {
730 m_iMidiMap = LSCP_MIDI_MAP_ALL;
731 }
732
733 int MidiInstrumentsModel::rowCount(const QModelIndex& /*parent*/) const {
734 if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
735 int n = 0;
736 for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap)
737 n += (*itMap).size();
738 return n;
739 }
740 InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);
741 if (itMap == instruments.end()) return 0;
742 return (*itMap).size();
743 }
744
745 int MidiInstrumentsModel::columnCount(const QModelIndex& /*parent*/) const {
746 return 9;
747 }
748
749 QVariant MidiInstrumentsModel::data(const QModelIndex &index, int role) const {
750 if (!index.isValid() || role != Qt::DisplayRole) return QVariant();
751
752 if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
753 int n = 0;
754 for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap) {
755 n += (*itMap).size();
756 if (index.row() < n)
757 return QVariant::fromValue(
758 (*itMap)[index.row() + (*itMap).size() - n]
759 );
760 }
761 } else {
762 // resolve MIDI instrument map
763 InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);
764 if (itMap == instruments.end()) return QVariant();
765 // resolve instrument in that map
766 if (index.row() >= (*itMap).size()) return QVariant();
767 return QVariant::fromValue(
768 (*itMap)[index.row()]
769 );
770 }
771
772 return QVariant();
773 }
774
775 QVariant MidiInstrumentsModel::headerData(int section, Qt::Orientation orientation, int role) const {
776 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
777 return QVariant();
778
779 switch (section) {
780 case 0: return tr("Name");
781 case 1: return tr("Map");
782 case 2: return tr("Bank");
783 case 3: return tr("Prog");
784 case 4: return tr("Engine");
785 case 5: return tr("File");
786 case 6: return tr("Nr");
787 case 7: return tr("Vol");
788 case 8: return tr("Mode");
789 default: return QVariant();
790 }
791 }
792
793 qsamplerInstrument* MidiInstrumentsModel::addInstrument(int iMap, int iBank, int iProg) {
794 // Check it there's already one instrument item
795 // with the very same key (bank, program);
796 // if yes, just remove it without prejudice...
797 for (int i = 0; i < instruments[iMap].size(); i++) {
798 if (
799 instruments[iMap][i].bank() == iBank &&
800 instruments[iMap][i].prog() == iProg
801 ) {
802 instruments[iMap].removeAt(i);
803 break;
804 }
805 }
806
807 // resolve the appropriate place, we keep the list sorted that way ...
808 int i = 0;
809 for (; i < instruments[iMap].size(); i++)
810 if (
811 iBank > instruments[iMap][i].bank() ||
812 ( iBank == instruments[iMap][i].bank() &&
813 iProg > instruments[iMap][i].prog() )
814 ) break;
815
816 qsamplerInstrument& instr = instruments[iMap][i] = qsamplerInstrument(iMap, iBank, iProg);
817
818 return &instr;
819 }
820
821 void MidiInstrumentsModel::setMidiMap(int iMidiMap) {
822 if (iMidiMap < 0)
823 iMidiMap = LSCP_MIDI_MAP_ALL;
824
825 m_iMidiMap = iMidiMap;
826 }
827
828 int MidiInstrumentsModel::midiMap() const {
829 return m_iMidiMap;
830 }
831
832 void MidiInstrumentsModel::refresh() {
833 instruments.clear();
834
835 MainForm* pMainForm = MainForm::getInstance();
836 if (pMainForm == NULL)
837 return;
838 if (pMainForm->client() == NULL)
839 return;
840
841 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
842
843 // Load the whole bunch of instrument items...
844 lscp_midi_instrument_t* pInstrs
845 = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
846 for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
847 const int iMap = pInstrs[iInstr].map;
848 const int iBank = pInstrs[iInstr].bank;
849 const int iProg = pInstrs[iInstr].prog;
850 addInstrument(iMap, iBank, iProg);
851 // Try to keep it snappy :)
852 QApplication::processEvents(QEventLoop::ExcludeUserInput);
853 }
854
855 QApplication::restoreOverrideCursor();
856
857 if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
858 pMainForm->appendMessagesClient("lscp_list_midi_instruments");
859 pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
860 }
861
862 //selectionChangedSlot();
863 }
864
865
866 MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) {
867 }
868
869 QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
870 return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);
871 }
872
873 void MidiInstrumentsDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
874 }
875
876 void MidiInstrumentsDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {
877 }
878
879 void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
880 if (editor) editor->setGeometry(option.rect);
881 }
882
883
884 // end of qsamplerInstrumentList.cpp

  ViewVC Help
Powered by ViewVC