/[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 971 by capela, Thu Dec 7 10:36:26 2006 UTC revision 1463 by capela, Thu Nov 1 13:01:27 2007 UTC
# Line 1  Line 1 
1  // qsamplerInstrumentList.cpp  // qsamplerInstrumentList.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2003-2005, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved.
5    
6     This program is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or
7     modify it under the terms of the GNU General Public License     modify it under the terms of the GNU General Public License
# Line 25  Line 25 
25  #include "qsamplerInstrument.h"  #include "qsamplerInstrument.h"
26  #include "qsamplerInstrumentForm.h"  #include "qsamplerInstrumentForm.h"
27    
28    #include "qsamplerOptions.h"
29  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
30    
31    #include <qapplication.h>
32    #include <qmessagebox.h>
33    #include <qeventloop.h>
34  #include <qaction.h>  #include <qaction.h>
35    #include <qcursor.h>
36  #include <qfileinfo.h>  #include <qfileinfo.h>
37  #include <qpopupmenu.h>  
38    #include <QMenu>
39    
40  // Needed for lroundf()  // Needed for lroundf()
41  #include <math.h>  #include <math.h>
42    
43  #ifdef __BORLANDC__  #ifndef CONFIG_ROUND
44  static long lroundf ( float fval )  static inline long lroundf ( float x )
45  {  {
46          double fint = 0.0;          if (x >= 0.0f)
47      float  frac = float(::modf(fval, &fint));                  return long(x + 0.5f);
48      long   lint = long(fint);          else
49      if (frac >= +0.5f)                  return long(x - 0.5f);
         lint++;  
     else  
     if (frac <= -0.5f)  
         lint--;  
     return lint;  
50  }  }
51  #endif  #endif
52    
53    using namespace QSampler;
54    
55  //----------------------------------------------------------------------  //----------------------------------------------------------------------
56  // class qsamplerInstrumentGroup -- custom group list view item.  // class qsamplerInstrumentGroup -- custom group list view item.
57  //  //
58    
59    #if 0
60  // Constructors.  // Constructors.
61  qsamplerInstrumentGroup::qsamplerInstrumentGroup (  qsamplerInstrumentGroup::qsamplerInstrumentGroup (
62          qsamplerInstrumentList *pListView, const QString& sName,          qsamplerInstrumentList *pListView, const QString& sName,
# Line 62  qsamplerInstrumentGroup::qsamplerInstrum Line 65  qsamplerInstrumentGroup::qsamplerInstrum
65  {  {
66          QListViewItem::setRenameEnabled(0, true);          QListViewItem::setRenameEnabled(0, true);
67    
68          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));          QListViewItem::setPixmap(0, QPixmap(":/icons/itemGroup.png"));
69          QListViewItem::setText(0, sName);          QListViewItem::setText(0, sName);
70  }  }
71    
# Line 73  qsamplerInstrumentGroup::qsamplerInstrum Line 76  qsamplerInstrumentGroup::qsamplerInstrum
76  {  {
77          QListViewItem::setRenameEnabled(0, true);          QListViewItem::setRenameEnabled(0, true);
78    
79          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));          QListViewItem::setPixmap(0, QPixmap(":/icons/itemGroup.png"));
80  }  }
81    
82    
# Line 116  void qsamplerInstrumentGroup::setOpen ( Line 119  void qsamplerInstrumentGroup::setOpen (
119  {  {
120          // Set the proper pixmap of this...          // Set the proper pixmap of this...
121          if (rtti() == qsamplerInstrumentList::Group) {          if (rtti() == qsamplerInstrumentList::Group) {
122                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(                  QListViewItem::setPixmap(0, QPixmap(bOpen ?
123                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));                          ":/icons/itemGroupOpen.png" : ":/icons/itemGroup.png"));
124          }          }
125          // Open it up...          // Open it up...
126          QListViewItem::setOpen(bOpen);          QListViewItem::setOpen(bOpen);
# Line 187  qsamplerInstrument *qsamplerInstrumentIt Line 190  qsamplerInstrument *qsamplerInstrumentIt
190  // Item refreshment.  // Item refreshment.
191  void qsamplerInstrumentItem::update (void)  void qsamplerInstrumentItem::update (void)
192  {  {
193          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));          QListViewItem::setPixmap(0, QPixmap(":/icons/itemFile.png"));
194    
195          const QString s = "-";          const QString s = "-";
196          if (m_pInstrument) {          if (m_pInstrument) {
197                  setText(0, m_pInstrument->name());                  setText(0, m_pInstrument->name());
198                  setText(1, QString::number(m_pInstrument->bank()));                  setText(1, QString::number(m_pInstrument->map()));
199                  setText(2, QString::number(m_pInstrument->program() + 1));                  setText(2, QString::number(m_pInstrument->bank()));
200                  setText(3, m_pInstrument->engineName());                  setText(3, QString::number(m_pInstrument->prog() + 1));
201                  setText(4, QFileInfo(m_pInstrument->instrumentFile()).fileName());                  setText(4, m_pInstrument->engineName());
202                  setText(5, QString::number(m_pInstrument->instrumentNr()));                  setText(5, QFileInfo(m_pInstrument->instrumentFile()).fileName());
203                  setText(6, QString::number(::lroundf(100.0f * m_pInstrument->volume())));                  setText(6, QString::number(m_pInstrument->instrumentNr()));
204                    setText(7, QString::number(::lroundf(100.0f * m_pInstrument->volume())));
205                  QString sLoadMode = s;                  QString sLoadMode = s;
206                  switch (m_pInstrument->loadMode()) {                  switch (m_pInstrument->loadMode()) {
207                  case 3:                  case 3:
# Line 210  void qsamplerInstrumentItem::update (voi Line 214  void qsamplerInstrumentItem::update (voi
214                          sLoadMode = QObject::tr("On Demand");                          sLoadMode = QObject::tr("On Demand");
215                          break;                          break;
216                  }                  }
217                  setText(7, sLoadMode);                  setText(8, sLoadMode);
218          } else {          } else {
219                  for (int i = 0; i < listView()->columns(); i++)                  for (int i = 0; i < listView()->columns(); i++)
220                          setText(i, s);                          setText(i, s);
# Line 227  qsamplerInstrumentList::qsamplerInstrume Line 231  qsamplerInstrumentList::qsamplerInstrume
231          QWidget *pParent, const char *pszName )          QWidget *pParent, const char *pszName )
232          : QListView(pParent, pszName)          : QListView(pParent, pszName)
233  {  {
234            m_iMidiMap = LSCP_MIDI_MAP_ALL;
235    
236  //  QListView::setRootIsDecorated(true);  //  QListView::setRootIsDecorated(true);
237            QListView::setAllColumnsShowFocus(true);
238          QListView::setResizeMode(QListView::NoColumn);          QListView::setResizeMode(QListView::NoColumn);
239  //      QListView::setAcceptDrops(true);  //      QListView::setAcceptDrops(true);
240          QListView::setDragAutoScroll(true);          QListView::setDragAutoScroll(true);
# Line 237  qsamplerInstrumentList::qsamplerInstrume Line 244  qsamplerInstrumentList::qsamplerInstrume
244          QListView::setSortColumn(-1);          QListView::setSortColumn(-1);
245    
246          QListView::addColumn(tr("Name"));          QListView::addColumn(tr("Name"));
247            QListView::addColumn(tr("Map"));
248          QListView::addColumn(tr("Bank"));          QListView::addColumn(tr("Bank"));
249          QListView::addColumn(tr("Prog"));          QListView::addColumn(tr("Prog"));
250          QListView::addColumn(tr("Engine"));          QListView::addColumn(tr("Engine"));
# Line 245  qsamplerInstrumentList::qsamplerInstrume Line 253  qsamplerInstrumentList::qsamplerInstrume
253          QListView::addColumn(tr("Vol"));          QListView::addColumn(tr("Vol"));
254          QListView::addColumn(tr("Mode"));          QListView::addColumn(tr("Mode"));
255    
256          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Bank          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Map
257          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Bank
258          QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr          QListView::setColumnAlignment(3, Qt::AlignHCenter);     // Prog
259          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Nr
260            QListView::setColumnAlignment(7, Qt::AlignHCenter);     // Vol
261          QListView::setColumnWidth(0, 60);       // Name  
262          QListView::setColumnWidth(0, 120);      // File          QListView::setColumnWidth(0, 120);      // Name
263            QListView::setColumnWidth(5, 240);      // File
264          m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);  
265          m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);          m_pNewGroupAction = new QAction(
266          m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);                  QIcon(":/icons/itemGroupNew.png"),
267          m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);                  tr("New &Group"), tr("Ctrl+G"), this);
268          m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);          m_pNewItemAction  = new QAction(
269          m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), this);                  QIcon(":/icons/itemNew.png"),
270                    tr("New &Instrument..."), tr("Ins"), this);
271            m_pEditItemAction = new QAction(
272                    QIcon(":/icons/formEdit.png"),
273                    tr("&Edit..."), tr("Enter"), this);
274            m_pRenameAction   = new QAction(tr("&Rename"), tr("F2"), this);
275            m_pDeleteAction   = new QAction(
276                    QIcon(":/icons/formRemove.png"),
277                    tr("&Delete"), tr("Del"), this);
278            m_pRefreshAction  = new QAction(
279                    QIcon(":/icons/formRefresh.png"),
280                    tr("Re&fresh"), tr("F5"), this);
281    
282            m_pNewGroupAction->setToolTip(tr("New Group"));
283            m_pNewItemAction->setToolTip(tr("New Instrument"));
284            m_pEditItemAction->setToolTip(tr("Edit"));
285            m_pRenameAction->setToolTip(tr("Rename"));
286            m_pDeleteAction->setToolTip(tr("Delete"));
287            m_pRefreshAction->setToolTip(tr("Refresh"));
288    
289          QObject::connect(m_pNewGroupAction,          QObject::connect(m_pNewGroupAction,
290                  SIGNAL(activated()),                  SIGNAL(activated()),
# Line 312  qsamplerInstrumentItem *qsamplerInstrume Line 338  qsamplerInstrumentItem *qsamplerInstrume
338          qsamplerInstrument *pInstrument,          qsamplerInstrument *pInstrument,
339          qsamplerInstrumentGroup *pParentGroup )          qsamplerInstrumentGroup *pParentGroup )
340  {  {
341            // Check it there's already one instrument item
342            // with the very same key (bank, program);
343            // if yes, just remove it without prejudice...
344          qsamplerInstrumentItem *pItem = findItem(pInstrument);          qsamplerInstrumentItem *pItem = findItem(pInstrument);
345          if (pItem == NULL) {          if (pItem) {
346                  if (pParentGroup)                  // If exactly the same, just update view and bail out...
347                          pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);                  if (pItem->instrument() == pInstrument) {
348                  else                          pItem->update();
349                          pItem = new qsamplerInstrumentItem(this, pInstrument);                          return pItem;
350                    }
351                    // Remove it, as instrument keys must be unique.
352                    delete pItem;
353          }          }
354    
355            // Add the new item under proper group one, if any...
356            if (pParentGroup) {
357                    pParentGroup->setOpen(true);
358                    pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);
359            } else {
360                    pItem = new qsamplerInstrumentItem(this, pInstrument);
361            }
362    
363            // Set it as current selection...
364          QListView::setSelected(pItem, true);          QListView::setSelected(pItem, true);
365    
366          return pItem;          return pItem;
367  }  }
368    
# Line 330  qsamplerInstrumentGroup *qsamplerInstrum Line 373  qsamplerInstrumentGroup *qsamplerInstrum
373  {  {
374          qsamplerInstrumentGroup *pGroup = findGroup(sName);          qsamplerInstrumentGroup *pGroup = findGroup(sName);
375          if (pGroup == NULL) {          if (pGroup == NULL) {
376                  if (pParentGroup)                  if (pParentGroup) {
377                            pParentGroup->setOpen(true);
378                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);
379                  else                  } else {
380                          pGroup = new qsamplerInstrumentGroup(this, sName);                          pGroup = new qsamplerInstrumentGroup(this, sName);
381                    }
382          }          }
383          QListView::setSelected(pGroup, true);          QListView::setSelected(pGroup, true);
384          return pGroup;          return pGroup;
# Line 372  qsamplerInstrumentItem *qsamplerInstrume Line 417  qsamplerInstrumentItem *qsamplerInstrume
417                          qsamplerInstrumentItem *pItem                          qsamplerInstrumentItem *pItem
418                                  = static_cast<qsamplerInstrumentItem *> (pListItem);                                  = static_cast<qsamplerInstrumentItem *> (pListItem);
419                          if (pItem && pItem->instrument()                          if (pItem && pItem->instrument()
420                                    && pItem->instrument()->map()  == pInstrument->map()
421                                  && pItem->instrument()->bank() == pInstrument->bank()                                  && pItem->instrument()->bank() == pInstrument->bank()
422                                  && pItem->instrument()->program() == pInstrument->program())                                  && pItem->instrument()->prog() == pInstrument->prog())
423                                  return pItem;                                  return pItem;
424                  }                  }
425                  ++iter;                  ++iter;
# Line 396  qsamplerInstrumentGroup *qsamplerInstrum Line 442  qsamplerInstrumentGroup *qsamplerInstrum
442  // Add a new group item below the current one.  // Add a new group item below the current one.
443  void qsamplerInstrumentList::newGroupSlot (void)  void qsamplerInstrumentList::newGroupSlot (void)
444  {  {
         qsamplerInstrumentGroup *pParentGroup  
                 = groupItem(QListView::selectedItem());  
445          qsamplerInstrumentGroup *pNewGroup          qsamplerInstrumentGroup *pNewGroup
446                  = addGroup(tr("New Group"), pParentGroup);                  = addGroup(tr("New Group"), groupItem(QListView::selectedItem()));
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
447          if (pNewGroup)          if (pNewGroup)
448                  pNewGroup->startRename(0);                  pNewGroup->startRename(0);
449    
# Line 409  void qsamplerInstrumentList::newGroupSlo Line 451  void qsamplerInstrumentList::newGroupSlo
451  }  }
452    
453    
454    // Map selector.
455    void qsamplerInstrumentList::setMidiMap ( int iMidiMap )
456    {
457            if (iMidiMap < 0)
458                    iMidiMap = LSCP_MIDI_MAP_ALL;
459    
460            m_iMidiMap = iMidiMap;
461    }
462    
463    int qsamplerInstrumentList::midiMap (void) const
464    {
465            return m_iMidiMap;
466    }
467    
468    
469    // List actions accessors.
470    QAction *qsamplerInstrumentList::newGroupAction (void) const
471    {
472            return m_pNewGroupAction;
473    }
474    
475    QAction *qsamplerInstrumentList::newItemAction (void) const
476    {
477            return m_pNewItemAction;
478    }
479    
480    QAction *qsamplerInstrumentList::editItemAction (void) const
481    {
482            return m_pEditItemAction;
483    }
484    
485    QAction *qsamplerInstrumentList::renameAction (void) const
486    {
487            return m_pRenameAction;
488    }
489    
490    QAction *qsamplerInstrumentList::deleteAction (void) const
491    {
492            return m_pDeleteAction;
493    }
494    
495    QAction *qsamplerInstrumentList::refreshAction (void) const
496    {
497            return m_pRefreshAction;
498    }
499    
500    
501  // Add a new instrument item below the current one.  // Add a new instrument item below the current one.
502  void qsamplerInstrumentList::newItemSlot (void)  void qsamplerInstrumentList::newItemSlot (void)
503  {  {
# Line 421  void qsamplerInstrumentList::newItemSlot Line 510  void qsamplerInstrumentList::newItemSlot
510                  return;                  return;
511          }          }
512    
513          // Check it there's already one instrument item          // Commit...
514          // with the very same key (bank, program);          pInstrument->mapInstrument();
515          // if yes, just remove it without prejudice...          // add new item to the tree...
516          qsamplerInstrumentItem *pItem = findItem(pInstrument);          addItem(pInstrument, groupItem(QListView::selectedItem()));
517          if (pItem)          // Notify we've changes...
                 delete pItem;  
   
         pInstrument->map();  
518          emit instrumentsChanged();          emit instrumentsChanged();
519    
         qsamplerInstrumentGroup *pParentGroup  
                 = groupItem(QListView::selectedItem());  
         addItem(pInstrument, pParentGroup);  
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
   
520          selectionChangedSlot();          selectionChangedSlot();
521  }  }
522    
# Line 448  void qsamplerInstrumentList::editItemSlo Line 528  void qsamplerInstrumentList::editItemSlo
528          if (pListItem == NULL)          if (pListItem == NULL)
529                  return;                  return;
530          if (pListItem->rtti() == Item) {          if (pListItem->rtti() == Item) {
531                    qsamplerInstrument *pInstrument = NULL;
532                  qsamplerInstrumentItem *pItem                  qsamplerInstrumentItem *pItem
533                          = static_cast<qsamplerInstrumentItem *> (pListItem);                          = static_cast<qsamplerInstrumentItem *> (pListItem);
534                  if (pItem && pItem->instrument()) {                  if (pItem)
535                            pInstrument = pItem->instrument();
536                    if (pInstrument) {
537                            // Save current key values...
538                            qsamplerInstrument oldInstrument(*pInstrument);
539                            // Do the edit dance...
540                          qsamplerInstrumentForm form(this);                          qsamplerInstrumentForm form(this);
541                          form.setup(pItem->instrument());                          form.setup(pInstrument);
542                          if (form.exec()) {                          if (form.exec()) {
543                                  pItem->instrument()->map();                                  // Commit...
544                                    pInstrument->mapInstrument();
545                                    // Check whether we changed instrument key...
546                                    if (oldInstrument.map()  == pInstrument->map()  &&
547                                            oldInstrument.bank() == pInstrument->bank() &&
548                                            oldInstrument.prog() == pInstrument->prog()) {
549                                            // just update tree item...
550                                            pItem->update();
551                                    } else {
552                                            // Unmap old instance...
553                                            oldInstrument.unmapInstrument();
554                                            // Change item tree, whether applicable...
555                                            if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) {
556                                                    // Add new brand item into view...
557                                                    addItem(pInstrument, groupItem(pListItem));
558                                            } else {
559                                                    // Just remove/hide old one.
560                                                    delete pItem;
561                                            }
562                                    }
563                                    // Notify we've changes...
564                                  emit instrumentsChanged();                                  emit instrumentsChanged();
                                 pItem->update();  
565                          }                          }
566                  }                  }
567          }          }
# Line 480  void qsamplerInstrumentList::renameSlot Line 585  void qsamplerInstrumentList::renameSlot
585  void qsamplerInstrumentList::deleteSlot (void)  void qsamplerInstrumentList::deleteSlot (void)
586  {  {
587          QListViewItem *pListItem = QListView::selectedItem();          QListViewItem *pListItem = QListView::selectedItem();
588          if (pListItem) {          if (pListItem == NULL)
589                  if (pListItem->rtti() == Item) {                  return;
590                          qsamplerInstrumentItem *pItem  
591                                  = static_cast<qsamplerInstrumentItem *> (pListItem);          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
592                          if (pItem && pItem->instrument()) {          if (pMainForm == NULL)
593                                  pItem->instrument()->unmap();                  return;
594                                  emit instrumentsChanged();  
595                          }          // Prompt user if this is for real...
596            qsamplerOptions *pOptions = pMainForm->options();
597            if (pOptions && pOptions->bConfirmRemove) {
598                    if (QMessageBox::warning(this,
599                            QSAMPLER_TITLE ": " + tr("Warning"),
600                            tr("Delete %1:\n\n"
601                            "%2\n\n"
602                            "Are you sure?")
603                            .arg(pListItem->rtti() == Item ? tr("instrument") : tr("group"))
604                            .arg(pListItem->text(0)),
605                            tr("OK"), tr("Cancel")) > 0)
606                            return;
607            }
608    
609            // Unmap instrument entry...
610            if (pListItem->rtti() == Item) {
611                    qsamplerInstrumentItem *pItem
612                            = static_cast<qsamplerInstrumentItem *> (pListItem);
613                    if (pItem && pItem->instrument()) {
614                            pItem->instrument()->unmapInstrument();
615                            emit instrumentsChanged();
616                  }                  }
                 delete pListItem;  
617          }          }
618    
619            // Do it for real...
620            delete pListItem;
621    
622          selectionChangedSlot();          selectionChangedSlot();
623  }  }
624    
# Line 514  void qsamplerInstrumentList::selectionCh Line 641  void qsamplerInstrumentList::selectionCh
641  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )
642  {  {
643          // FIXME: Hope the list view item is the one selected.          // FIXME: Hope the list view item is the one selected.
644          if (pListItem->rtti() == Item)          if (pListItem && pListItem->rtti() == Item)
645                  editItemSlot();                  editItemSlot();
646  }  }
647    
# Line 527  void qsamplerInstrumentList::renamedSlot Line 654  void qsamplerInstrumentList::renamedSlot
654                          = static_cast<qsamplerInstrumentItem *> (pListItem);                          = static_cast<qsamplerInstrumentItem *> (pListItem);
655                  if (pItem && pItem->instrument()) {                  if (pItem && pItem->instrument()) {
656                          pItem->instrument()->setName(pListItem->text(0));                          pItem->instrument()->setName(pListItem->text(0));
657                          pItem->instrument()->map();                          pItem->instrument()->mapInstrument();
658                          emit instrumentsChanged();                          emit instrumentsChanged();
659                          pItem->update();                          pItem->update();
660                  }                  }
# Line 569  void qsamplerInstrumentList::refresh (vo Line 696  void qsamplerInstrumentList::refresh (vo
696          if (pMainForm->client() == NULL)          if (pMainForm->client() == NULL)
697                  return;                  return;
698    
699            QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
700    
701            // Load the whole bunch of instrument items...
702          qsamplerInstrumentItem *pItem = NULL;          qsamplerInstrumentItem *pItem = NULL;
703          lscp_midi_instrument_t *pInstrs          lscp_midi_instrument_t *pInstrs
704                  = ::lscp_list_midi_instruments(pMainForm->client());                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
705          for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
706                  int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;                  int iMap  = pInstrs[iInstr].map;
707                  int iProgram = pInstrs[iInstr].program;                  int iBank = pInstrs[iInstr].bank;
708                    int iProg = pInstrs[iInstr].prog;
709                  qsamplerInstrument *pInstrument                  qsamplerInstrument *pInstrument
710                          = new qsamplerInstrument(iBank, iProgram);                          = new qsamplerInstrument(iMap, iBank, iProg);
711                  if (pInstrument->get())                  if (pInstrument->getInstrument())
712                          pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);                          pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);
713                    // Try to keep it snappy :)
714                    QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
715          }          }
716    
717            QApplication::restoreOverrideCursor();
718    
719          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
720                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
721                  pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));                  pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
# Line 588  void qsamplerInstrumentList::refresh (vo Line 723  void qsamplerInstrumentList::refresh (vo
723    
724          selectionChangedSlot();          selectionChangedSlot();
725  }  }
726    #endif
727    
728    MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) {
729        m_iMidiMap = LSCP_MIDI_MAP_ALL;
730    }
731    
732    int MidiInstrumentsModel::rowCount(const QModelIndex& /*parent*/) const {
733        if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
734            int n = 0;
735            for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap)
736                n += (*itMap).size();
737            return n;
738        }
739        InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);
740        if (itMap == instruments.end()) return 0;
741        return (*itMap).size();
742    }
743    
744    int MidiInstrumentsModel::columnCount(const QModelIndex& /*parent*/) const {
745        return 9;
746    }
747    
748    QVariant MidiInstrumentsModel::data(const QModelIndex &index, int role) const {
749        if (!index.isValid() || role != Qt::DisplayRole) return QVariant();
750    
751        if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
752            int n = 0;
753            for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap) {
754                n += (*itMap).size();
755                if (index.row() < n)
756                    return QVariant::fromValue(
757                        (*itMap)[index.row() + (*itMap).size() - n]
758                    );
759            }
760        } else {
761            // resolve MIDI instrument map
762            InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);
763            if (itMap == instruments.end()) return QVariant();
764            // resolve instrument in that map
765            if (index.row() >= (*itMap).size()) return QVariant();
766            return QVariant::fromValue(
767                (*itMap)[index.row()]
768            );
769        }
770    
771        return QVariant();
772    }
773    
774    QVariant MidiInstrumentsModel::headerData(int section, Qt::Orientation orientation, int role) const {
775        if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
776            return QVariant();
777    
778        switch (section) {
779            case 0: return tr("Name");
780            case 1: return tr("Map");
781            case 2: return tr("Bank");
782            case 3: return tr("Prog");
783            case 4: return tr("Engine");
784            case 5: return tr("File");
785            case 6: return tr("Nr");
786            case 7: return tr("Vol");
787            case 8: return tr("Mode");
788            default: return QVariant();
789        }
790    }
791    
792    qsamplerInstrument* MidiInstrumentsModel::addInstrument(int iMap, int iBank, int iProg) {
793        // Check it there's already one instrument item
794        // with the very same key (bank, program);
795        // if yes, just remove it without prejudice...
796        for (int i = 0; i < instruments[iMap].size(); i++) {
797            if (
798                instruments[iMap][i].bank() == iBank &&
799                instruments[iMap][i].prog() == iProg
800            ) {
801                instruments[iMap].removeAt(i);
802                break;
803            }
804        }
805    
806        // resolve the appropriate place, we keep the list sorted that way ...
807        int i = 0;
808        for (; i < instruments[iMap].size(); i++)
809            if (
810                iBank > instruments[iMap][i].bank() ||
811                ( iBank == instruments[iMap][i].bank() &&
812                  iProg > instruments[iMap][i].prog() )
813            ) break;
814    
815        qsamplerInstrument& instr = instruments[iMap][i] = qsamplerInstrument(iMap, iBank, iProg);
816    
817        return &instr;
818    }
819    
820    void MidiInstrumentsModel::setMidiMap(int iMidiMap) {
821        if (iMidiMap < 0)
822            iMidiMap = LSCP_MIDI_MAP_ALL;
823    
824        m_iMidiMap = iMidiMap;
825    }
826    
827    int MidiInstrumentsModel::midiMap() const {
828        return m_iMidiMap;
829    }
830    
831    void MidiInstrumentsModel::refresh() {
832            instruments.clear();
833    
834            MainForm* pMainForm = MainForm::getInstance();
835            if (pMainForm == NULL)
836                    return;
837            if (pMainForm->client() == NULL)
838                    return;
839    
840            QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
841    
842            // Load the whole bunch of instrument items...
843            lscp_midi_instrument_t* pInstrs
844                    = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
845            for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
846                    const int iMap  = pInstrs[iInstr].map;
847                    const int iBank = pInstrs[iInstr].bank;
848                    const int iProg = pInstrs[iInstr].prog;
849                    addInstrument(iMap, iBank, iProg);
850                    // Try to keep it snappy :)
851                    QApplication::processEvents(QEventLoop::ExcludeUserInput);
852            }
853    
854            QApplication::restoreOverrideCursor();
855    
856            if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
857                    pMainForm->appendMessagesClient("lscp_list_midi_instruments");
858                    pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
859            }
860    
861            //selectionChangedSlot();
862    }
863    
864    
865    MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) {
866    }
867    
868    QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
869        return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);
870    }
871    
872    void MidiInstrumentsDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
873    }
874    
875    void MidiInstrumentsDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {
876    }
877    
878    void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
879        if (editor) editor->setGeometry(option.rect);
880    }
881    
882  // end of qsamplerInstrumentList.cpp  // end of qsamplerInstrumentList.cpp
883    

Legend:
Removed from v.971  
changed lines
  Added in v.1463

  ViewVC Help
Powered by ViewVC