/[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 1234 by capela, Wed Jun 13 21:54:07 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>  #include <qpopupmenu.h>
38    
39  // Needed for lroundf()  // Needed for lroundf()
40  #include <math.h>  #include <math.h>
41    
42  #ifdef __BORLANDC__  #ifndef CONFIG_ROUND
43  static long lroundf ( float fval )  static inline long lroundf ( float x )
44  {  {
45          double fint = 0.0;          if (x >= 0.0f)
46      float  frac = float(::modf(fval, &fint));                  return long(x + 0.5f);
47      long   lint = long(fint);          else
48      if (frac >= +0.5f)                  return long(x - 0.5f);
         lint++;  
     else  
     if (frac <= -0.5f)  
         lint--;  
     return lint;  
49  }  }
50  #endif  #endif
51    
# Line 192  void qsamplerInstrumentItem::update (voi Line 192  void qsamplerInstrumentItem::update (voi
192          const QString s = "-";          const QString s = "-";
193          if (m_pInstrument) {          if (m_pInstrument) {
194                  setText(0, m_pInstrument->name());                  setText(0, m_pInstrument->name());
195                  setText(1, QString::number(m_pInstrument->bank()));                  setText(1, QString::number(m_pInstrument->map()));
196                  setText(2, QString::number(m_pInstrument->program() + 1));                  setText(2, QString::number(m_pInstrument->bank()));
197                  setText(3, m_pInstrument->engineName());                  setText(3, QString::number(m_pInstrument->prog() + 1));
198                  setText(4, QFileInfo(m_pInstrument->instrumentFile()).fileName());                  setText(4, m_pInstrument->engineName());
199                  setText(5, QString::number(m_pInstrument->instrumentNr()));                  setText(5, QFileInfo(m_pInstrument->instrumentFile()).fileName());
200                  setText(6, QString::number(::lroundf(100.0f * m_pInstrument->volume())));                  setText(6, QString::number(m_pInstrument->instrumentNr()));
201                    setText(7, QString::number(::lroundf(100.0f * m_pInstrument->volume())));
202                  QString sLoadMode = s;                  QString sLoadMode = s;
203                  switch (m_pInstrument->loadMode()) {                  switch (m_pInstrument->loadMode()) {
204                  case 3:                  case 3:
# Line 210  void qsamplerInstrumentItem::update (voi Line 211  void qsamplerInstrumentItem::update (voi
211                          sLoadMode = QObject::tr("On Demand");                          sLoadMode = QObject::tr("On Demand");
212                          break;                          break;
213                  }                  }
214                  setText(7, sLoadMode);                  setText(8, sLoadMode);
215          } else {          } else {
216                  for (int i = 0; i < listView()->columns(); i++)                  for (int i = 0; i < listView()->columns(); i++)
217                          setText(i, s);                          setText(i, s);
# Line 227  qsamplerInstrumentList::qsamplerInstrume Line 228  qsamplerInstrumentList::qsamplerInstrume
228          QWidget *pParent, const char *pszName )          QWidget *pParent, const char *pszName )
229          : QListView(pParent, pszName)          : QListView(pParent, pszName)
230  {  {
231            m_iMidiMap = LSCP_MIDI_MAP_ALL;
232    
233  //  QListView::setRootIsDecorated(true);  //  QListView::setRootIsDecorated(true);
234            QListView::setAllColumnsShowFocus(true);
235          QListView::setResizeMode(QListView::NoColumn);          QListView::setResizeMode(QListView::NoColumn);
236  //      QListView::setAcceptDrops(true);  //      QListView::setAcceptDrops(true);
237          QListView::setDragAutoScroll(true);          QListView::setDragAutoScroll(true);
# Line 237  qsamplerInstrumentList::qsamplerInstrume Line 241  qsamplerInstrumentList::qsamplerInstrume
241          QListView::setSortColumn(-1);          QListView::setSortColumn(-1);
242    
243          QListView::addColumn(tr("Name"));          QListView::addColumn(tr("Name"));
244            QListView::addColumn(tr("Map"));
245          QListView::addColumn(tr("Bank"));          QListView::addColumn(tr("Bank"));
246          QListView::addColumn(tr("Prog"));          QListView::addColumn(tr("Prog"));
247          QListView::addColumn(tr("Engine"));          QListView::addColumn(tr("Engine"));
# Line 245  qsamplerInstrumentList::qsamplerInstrume Line 250  qsamplerInstrumentList::qsamplerInstrume
250          QListView::addColumn(tr("Vol"));          QListView::addColumn(tr("Vol"));
251          QListView::addColumn(tr("Mode"));          QListView::addColumn(tr("Mode"));
252    
253          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Bank          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Map
254          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Bank
255          QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr          QListView::setColumnAlignment(3, Qt::AlignHCenter);     // Prog
256          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Nr
257            QListView::setColumnAlignment(7, Qt::AlignHCenter);     // Vol
258          QListView::setColumnWidth(0, 60);       // Name  
259          QListView::setColumnWidth(0, 120);      // File          QListView::setColumnWidth(0, 120);      // Name
260            QListView::setColumnWidth(5, 240);      // File
261          m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);  
262          m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);          m_pNewGroupAction = new QAction(
263          m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);                  QIconSet(QPixmap::fromMimeSource("itemGroupNew.png")),
264          m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);                  tr("New &Group"), tr("Ctrl+G"), this);
265          m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);          m_pNewItemAction  = new QAction(
266          m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), this);                  QIconSet(QPixmap::fromMimeSource("itemNew.png")),
267                    tr("New &Instrument..."), tr("Ins"), this);
268            m_pEditItemAction = new QAction(
269                    QIconSet(QPixmap::fromMimeSource("formEdit.png")),
270                    tr("&Edit..."), tr("Enter"), this);
271            m_pRenameAction   = new QAction(tr("&Rename"), tr("F2"), this);
272            m_pDeleteAction   = new QAction(
273                    QIconSet(QPixmap::fromMimeSource("formRemove.png")),
274                    tr("&Delete"), tr("Del"), this);
275            m_pRefreshAction  = new QAction(
276                    QIconSet(QPixmap::fromMimeSource("formRefresh.png")),
277                    tr("Re&fresh"), tr("F5"), this);
278    
279            m_pNewGroupAction->setToolTip(tr("New Group"));
280            m_pNewItemAction->setToolTip(tr("New Instrument"));
281            m_pEditItemAction->setToolTip(tr("Edit"));
282            m_pRenameAction->setToolTip(tr("Rename"));
283            m_pDeleteAction->setToolTip(tr("Delete"));
284            m_pRefreshAction->setToolTip(tr("Refresh"));
285    
286          QObject::connect(m_pNewGroupAction,          QObject::connect(m_pNewGroupAction,
287                  SIGNAL(activated()),                  SIGNAL(activated()),
# Line 312  qsamplerInstrumentItem *qsamplerInstrume Line 335  qsamplerInstrumentItem *qsamplerInstrume
335          qsamplerInstrument *pInstrument,          qsamplerInstrument *pInstrument,
336          qsamplerInstrumentGroup *pParentGroup )          qsamplerInstrumentGroup *pParentGroup )
337  {  {
338            // Check it there's already one instrument item
339            // with the very same key (bank, program);
340            // if yes, just remove it without prejudice...
341          qsamplerInstrumentItem *pItem = findItem(pInstrument);          qsamplerInstrumentItem *pItem = findItem(pInstrument);
342          if (pItem == NULL) {          if (pItem) {
343                  if (pParentGroup)                  // If exactly the same, just update view and bail out...
344                          pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);                  if (pItem->instrument() == pInstrument) {
345                  else                          pItem->update();
346                          pItem = new qsamplerInstrumentItem(this, pInstrument);                          return pItem;
347                    }
348                    // Remove it, as instrument keys must be unique.
349                    delete pItem;
350          }          }
351    
352            // Add the new item under proper group one, if any...
353            if (pParentGroup) {
354                    pParentGroup->setOpen(true);
355                    pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);
356            } else {
357                    pItem = new qsamplerInstrumentItem(this, pInstrument);
358            }
359    
360            // Set it as current selection...
361          QListView::setSelected(pItem, true);          QListView::setSelected(pItem, true);
362    
363          return pItem;          return pItem;
364  }  }
365    
# Line 330  qsamplerInstrumentGroup *qsamplerInstrum Line 370  qsamplerInstrumentGroup *qsamplerInstrum
370  {  {
371          qsamplerInstrumentGroup *pGroup = findGroup(sName);          qsamplerInstrumentGroup *pGroup = findGroup(sName);
372          if (pGroup == NULL) {          if (pGroup == NULL) {
373                  if (pParentGroup)                  if (pParentGroup) {
374                            pParentGroup->setOpen(true);
375                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);
376                  else                  } else {
377                          pGroup = new qsamplerInstrumentGroup(this, sName);                          pGroup = new qsamplerInstrumentGroup(this, sName);
378                    }
379          }          }
380          QListView::setSelected(pGroup, true);          QListView::setSelected(pGroup, true);
381          return pGroup;          return pGroup;
# Line 372  qsamplerInstrumentItem *qsamplerInstrume Line 414  qsamplerInstrumentItem *qsamplerInstrume
414                          qsamplerInstrumentItem *pItem                          qsamplerInstrumentItem *pItem
415                                  = static_cast<qsamplerInstrumentItem *> (pListItem);                                  = static_cast<qsamplerInstrumentItem *> (pListItem);
416                          if (pItem && pItem->instrument()                          if (pItem && pItem->instrument()
417                                    && pItem->instrument()->map()  == pInstrument->map()
418                                  && pItem->instrument()->bank() == pInstrument->bank()                                  && pItem->instrument()->bank() == pInstrument->bank()
419                                  && pItem->instrument()->program() == pInstrument->program())                                  && pItem->instrument()->prog() == pInstrument->prog())
420                                  return pItem;                                  return pItem;
421                  }                  }
422                  ++iter;                  ++iter;
# Line 396  qsamplerInstrumentGroup *qsamplerInstrum Line 439  qsamplerInstrumentGroup *qsamplerInstrum
439  // Add a new group item below the current one.  // Add a new group item below the current one.
440  void qsamplerInstrumentList::newGroupSlot (void)  void qsamplerInstrumentList::newGroupSlot (void)
441  {  {
         qsamplerInstrumentGroup *pParentGroup  
                 = groupItem(QListView::selectedItem());  
442          qsamplerInstrumentGroup *pNewGroup          qsamplerInstrumentGroup *pNewGroup
443                  = addGroup(tr("New Group"), pParentGroup);                  = addGroup(tr("New Group"), groupItem(QListView::selectedItem()));
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
444          if (pNewGroup)          if (pNewGroup)
445                  pNewGroup->startRename(0);                  pNewGroup->startRename(0);
446    
# Line 409  void qsamplerInstrumentList::newGroupSlo Line 448  void qsamplerInstrumentList::newGroupSlo
448  }  }
449    
450    
451    // Map selector.
452    void qsamplerInstrumentList::setMidiMap ( int iMidiMap )
453    {
454            if (iMidiMap < 0)
455                    iMidiMap = LSCP_MIDI_MAP_ALL;
456    
457            m_iMidiMap = iMidiMap;
458    }
459    
460    int qsamplerInstrumentList::midiMap (void) const
461    {
462            return m_iMidiMap;
463    }
464    
465    
466    // List actions accessors.
467    QAction *qsamplerInstrumentList::newGroupAction (void) const
468    {
469            return m_pNewGroupAction;
470    }
471    
472    QAction *qsamplerInstrumentList::newItemAction (void) const
473    {
474            return m_pNewItemAction;
475    }
476    
477    QAction *qsamplerInstrumentList::editItemAction (void) const
478    {
479            return m_pEditItemAction;
480    }
481    
482    QAction *qsamplerInstrumentList::renameAction (void) const
483    {
484            return m_pRenameAction;
485    }
486    
487    QAction *qsamplerInstrumentList::deleteAction (void) const
488    {
489            return m_pDeleteAction;
490    }
491    
492    QAction *qsamplerInstrumentList::refreshAction (void) const
493    {
494            return m_pRefreshAction;
495    }
496    
497    
498  // Add a new instrument item below the current one.  // Add a new instrument item below the current one.
499  void qsamplerInstrumentList::newItemSlot (void)  void qsamplerInstrumentList::newItemSlot (void)
500  {  {
# Line 421  void qsamplerInstrumentList::newItemSlot Line 507  void qsamplerInstrumentList::newItemSlot
507                  return;                  return;
508          }          }
509    
510          // Check it there's already one instrument item          // Commit...
511          // with the very same key (bank, program);          pInstrument->mapInstrument();
512          // if yes, just remove it without prejudice...          // add new item to the tree...
513          qsamplerInstrumentItem *pItem = findItem(pInstrument);          addItem(pInstrument, groupItem(QListView::selectedItem()));
514          if (pItem)          // Notify we've changes...
                 delete pItem;  
   
         pInstrument->map();  
515          emit instrumentsChanged();          emit instrumentsChanged();
516    
         qsamplerInstrumentGroup *pParentGroup  
                 = groupItem(QListView::selectedItem());  
         addItem(pInstrument, pParentGroup);  
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
   
517          selectionChangedSlot();          selectionChangedSlot();
518  }  }
519    
# Line 448  void qsamplerInstrumentList::editItemSlo Line 525  void qsamplerInstrumentList::editItemSlo
525          if (pListItem == NULL)          if (pListItem == NULL)
526                  return;                  return;
527          if (pListItem->rtti() == Item) {          if (pListItem->rtti() == Item) {
528                    qsamplerInstrument *pInstrument = NULL;
529                  qsamplerInstrumentItem *pItem                  qsamplerInstrumentItem *pItem
530                          = static_cast<qsamplerInstrumentItem *> (pListItem);                          = static_cast<qsamplerInstrumentItem *> (pListItem);
531                  if (pItem && pItem->instrument()) {                  if (pItem)
532                            pInstrument = pItem->instrument();
533                    if (pInstrument) {
534                            // Save current key values...
535                            qsamplerInstrument oldInstrument(*pInstrument);
536                            // Do the edit dance...
537                          qsamplerInstrumentForm form(this);                          qsamplerInstrumentForm form(this);
538                          form.setup(pItem->instrument());                          form.setup(pInstrument);
539                          if (form.exec()) {                          if (form.exec()) {
540                                  pItem->instrument()->map();                                  // Commit...
541                                    pInstrument->mapInstrument();
542                                    // Check whether we changed instrument key...
543                                    if (oldInstrument.map()  == pInstrument->map()  &&
544                                            oldInstrument.bank() == pInstrument->bank() &&
545                                            oldInstrument.prog() == pInstrument->prog()) {
546                                            // just update tree item...
547                                            pItem->update();
548                                    } else {
549                                            // Unmap old instance...
550                                            oldInstrument.unmapInstrument();
551                                            // Change item tree, whether applicable...
552                                            if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) {
553                                                    // Add new brand item into view...
554                                                    addItem(pInstrument, groupItem(pListItem));
555                                            } else {
556                                                    // Just remove/hide old one.
557                                                    delete pItem;
558                                            }
559                                    }
560                                    // Notify we've changes...
561                                  emit instrumentsChanged();                                  emit instrumentsChanged();
                                 pItem->update();  
562                          }                          }
563                  }                  }
564          }          }
# Line 480  void qsamplerInstrumentList::renameSlot Line 582  void qsamplerInstrumentList::renameSlot
582  void qsamplerInstrumentList::deleteSlot (void)  void qsamplerInstrumentList::deleteSlot (void)
583  {  {
584          QListViewItem *pListItem = QListView::selectedItem();          QListViewItem *pListItem = QListView::selectedItem();
585          if (pListItem) {          if (pListItem == NULL)
586                  if (pListItem->rtti() == Item) {                  return;
587                          qsamplerInstrumentItem *pItem  
588                                  = static_cast<qsamplerInstrumentItem *> (pListItem);          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
589                          if (pItem && pItem->instrument()) {          if (pMainForm == NULL)
590                                  pItem->instrument()->unmap();                  return;
591                                  emit instrumentsChanged();  
592                          }          // Prompt user if this is for real...
593            qsamplerOptions *pOptions = pMainForm->options();
594            if (pOptions && pOptions->bConfirmRemove) {
595                    if (QMessageBox::warning(this,
596                            QSAMPLER_TITLE ": " + tr("Warning"),
597                            tr("Delete %1:\n\n"
598                            "%2\n\n"
599                            "Are you sure?")
600                            .arg(pListItem->rtti() == Item ? tr("instrument") : tr("group"))
601                            .arg(pListItem->text(0)),
602                            tr("OK"), tr("Cancel")) > 0)
603                            return;
604            }
605    
606            // Unmap instrument entry...
607            if (pListItem->rtti() == Item) {
608                    qsamplerInstrumentItem *pItem
609                            = static_cast<qsamplerInstrumentItem *> (pListItem);
610                    if (pItem && pItem->instrument()) {
611                            pItem->instrument()->unmapInstrument();
612                            emit instrumentsChanged();
613                  }                  }
                 delete pListItem;  
614          }          }
615    
616            // Do it for real...
617            delete pListItem;
618    
619          selectionChangedSlot();          selectionChangedSlot();
620  }  }
621    
# Line 514  void qsamplerInstrumentList::selectionCh Line 638  void qsamplerInstrumentList::selectionCh
638  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )
639  {  {
640          // FIXME: Hope the list view item is the one selected.          // FIXME: Hope the list view item is the one selected.
641          if (pListItem->rtti() == Item)          if (pListItem && pListItem->rtti() == Item)
642                  editItemSlot();                  editItemSlot();
643  }  }
644    
# Line 527  void qsamplerInstrumentList::renamedSlot Line 651  void qsamplerInstrumentList::renamedSlot
651                          = static_cast<qsamplerInstrumentItem *> (pListItem);                          = static_cast<qsamplerInstrumentItem *> (pListItem);
652                  if (pItem && pItem->instrument()) {                  if (pItem && pItem->instrument()) {
653                          pItem->instrument()->setName(pListItem->text(0));                          pItem->instrument()->setName(pListItem->text(0));
654                          pItem->instrument()->map();                          pItem->instrument()->mapInstrument();
655                          emit instrumentsChanged();                          emit instrumentsChanged();
656                          pItem->update();                          pItem->update();
657                  }                  }
# Line 569  void qsamplerInstrumentList::refresh (vo Line 693  void qsamplerInstrumentList::refresh (vo
693          if (pMainForm->client() == NULL)          if (pMainForm->client() == NULL)
694                  return;                  return;
695    
696            QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
697    
698            // Load the whole bunch of instrument items...
699          qsamplerInstrumentItem *pItem = NULL;          qsamplerInstrumentItem *pItem = NULL;
700          lscp_midi_instrument_t *pInstrs          lscp_midi_instrument_t *pInstrs
701                  = ::lscp_list_midi_instruments(pMainForm->client());                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
702          for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
703                  int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;                  int iMap  = pInstrs[iInstr].map;
704                  int iProgram = pInstrs[iInstr].program;                  int iBank = pInstrs[iInstr].bank;
705                    int iProg = pInstrs[iInstr].prog;
706                  qsamplerInstrument *pInstrument                  qsamplerInstrument *pInstrument
707                          = new qsamplerInstrument(iBank, iProgram);                          = new qsamplerInstrument(iMap, iBank, iProg);
708                  if (pInstrument->get())                  if (pInstrument->getInstrument())
709                          pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);                          pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);
710                    // Try to keep it snappy :)
711                    QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
712          }          }
713    
714            QApplication::restoreOverrideCursor();
715    
716          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
717                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
718                  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."));

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

  ViewVC Help
Powered by ViewVC