/[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 969 by capela, Wed Dec 6 19:38:02 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
8     as published by the Free Software Foundation; either version 2     as published by the Free Software Foundation; either version 2
9     of the License, or (at your option) any later version.     of the License, or (at your option) any later version.
10    
11     This program is distributed in the hope that it will be useful,     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.     GNU General Public License for more details.
15    
16     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19    
20  *****************************************************************************/  *****************************************************************************/
21    
22  #include "qsamplerAbout.h"  #include "qsamplerAbout.h"
23  #include "qsamplerInstrumentList.h"  #include "qsamplerInstrumentList.h"
24    
25  #include "qsamplerInstrument.h"  #include "qsamplerInstrument.h"
26  #include "qsamplerInstrumentForm.h"  #include "qsamplerInstrumentForm.h"
27    
28  #include "qsamplerMainForm.h"  #include "qsamplerOptions.h"
29    #include "qsamplerMainForm.h"
30  #include <qaction.h>  
31  #include <qfileinfo.h>  #include <qapplication.h>
32  #include <qpopupmenu.h>  #include <qmessagebox.h>
33    #include <qeventloop.h>
34  // Needed for lroundf()  #include <qaction.h>
35  #include <math.h>  #include <qcursor.h>
36    #include <qfileinfo.h>
37    #include <qpopupmenu.h>
38  //----------------------------------------------------------------------  
39  // class qsamplerInstrumentGroup -- custom group list view item.  // Needed for lroundf()
40  //  #include <math.h>
41    
42  // Constructors.  #ifndef CONFIG_ROUND
43  qsamplerInstrumentGroup::qsamplerInstrumentGroup (  static inline long lroundf ( float x )
44          qsamplerInstrumentList *pListView, const QString& sName,  {
45          QListViewItem *pItemAfter )          if (x >= 0.0f)
46          : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())                  return long(x + 0.5f);
47  {          else
48          QListViewItem::setRenameEnabled(0, true);                  return long(x - 0.5f);
49    }
50          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  #endif
51          QListViewItem::setText(0, sName);  
52  }  
53    //----------------------------------------------------------------------
54    // class qsamplerInstrumentGroup -- custom group list view item.
55  qsamplerInstrumentGroup::qsamplerInstrumentGroup (  //
56          qsamplerInstrumentGroup *pGroupItem, const QString& sName )  
57          : QListViewItem(pGroupItem, sName)  // Constructors.
58  {  qsamplerInstrumentGroup::qsamplerInstrumentGroup (
59          QListViewItem::setRenameEnabled(0, true);          qsamplerInstrumentList *pListView, const QString& sName,
60            QListViewItem *pItemAfter )
61          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));          : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())
62  }  {
63            QListViewItem::setRenameEnabled(0, true);
64    
65  // Default destructor.          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));
66  qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)          QListViewItem::setText(0, sName);
67  {  }
68  }  
69    
70    qsamplerInstrumentGroup::qsamplerInstrumentGroup (
71  // Instance accessors.          qsamplerInstrumentGroup *pGroupItem, const QString& sName )
72  void qsamplerInstrumentGroup::setName ( const QString& sName )          : QListViewItem(pGroupItem, sName)
73  {  {
74          QListViewItem::setText(0, sName);          QListViewItem::setRenameEnabled(0, true);
75  }  
76            QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));
77    }
78  QString qsamplerInstrumentGroup::name (void) const  
79  {  
80          return QListViewItem::text(0);  // Default destructor.
81  }  qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)
82    {
83    }
84  qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const  
85  {  
86          QListViewItem *pParent = QListViewItem::parent();  // Instance accessors.
87          while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)  void qsamplerInstrumentGroup::setName ( const QString& sName )
88                  pParent = pParent->parent();  {
89          return static_cast<qsamplerInstrumentGroup *> (pParent);          QListViewItem::setText(0, sName);
90  }  }
91    
92    
93  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const  QString qsamplerInstrumentGroup::name (void) const
94  {  {
95          return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());          return QListViewItem::text(0);
96  }  }
97    
98    
99  // To show up whether its open or not.  qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const
100  void qsamplerInstrumentGroup::setOpen ( bool bOpen )  {
101  {          QListViewItem *pParent = QListViewItem::parent();
102          // Set the proper pixmap of this...          while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)
103          if (rtti() == qsamplerInstrumentList::Group) {                  pParent = pParent->parent();
104                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(          return static_cast<qsamplerInstrumentGroup *> (pParent);
105                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));  }
106          }  
107          // Open it up...  
108          QListViewItem::setOpen(bOpen);  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const
109    {
110          // All ancestors should be also visible.          return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());
111          if (bOpen && QListViewItem::parent())  }
112                  QListViewItem::parent()->setOpen(true);  
113  }  
114    // To show up whether its open or not.
115    void qsamplerInstrumentGroup::setOpen ( bool bOpen )
116  // To virtually distinguish between list view items.  {
117  int qsamplerInstrumentGroup::rtti (void) const          // Set the proper pixmap of this...
118  {          if (rtti() == qsamplerInstrumentList::Group) {
119          return qsamplerInstrumentList::Group;                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(
120  }                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));
121            }
122            // Open it up...
123  //----------------------------------------------------------------------          QListViewItem::setOpen(bOpen);
124  // class qsamplerInstrumentItem -- custom file list view item.  
125  //          // All ancestors should be also visible.
126            if (bOpen && QListViewItem::parent())
127  // Constructors.                  QListViewItem::parent()->setOpen(true);
128  qsamplerInstrumentItem::qsamplerInstrumentItem (  }
129          qsamplerInstrumentList *pListView,  
130          qsamplerInstrument *pInstrument,  
131          QListViewItem *pItemAfter )  // To virtually distinguish between list view items.
132          : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)  int qsamplerInstrumentGroup::rtti (void) const
133  {  {
134          m_pInstrument = pInstrument;          return qsamplerInstrumentList::Group;
135    }
136          update();  
137  }  
138    //----------------------------------------------------------------------
139  qsamplerInstrumentItem::qsamplerInstrumentItem (  // class qsamplerInstrumentItem -- custom file list view item.
140          qsamplerInstrumentGroup *pGroupItem,  //
141          qsamplerInstrument *pInstrument )  
142          : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())  // Constructors.
143  {  qsamplerInstrumentItem::qsamplerInstrumentItem (
144          m_pInstrument = pInstrument;          qsamplerInstrumentList *pListView,
145            qsamplerInstrument *pInstrument,
146          update();          QListViewItem *pItemAfter )
147  }          : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)
148    {
149            m_pInstrument = pInstrument;
150  // Default destructor.  
151  qsamplerInstrumentItem::~qsamplerInstrumentItem (void)          update();
152  {  }
153          if (m_pInstrument)  
154                  delete m_pInstrument;  qsamplerInstrumentItem::qsamplerInstrumentItem (
155  }          qsamplerInstrumentGroup *pGroupItem,
156            qsamplerInstrument *pInstrument )
157            : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())
158  // To virtually distinguish between list view items.  {
159  int qsamplerInstrumentItem::rtti (void) const          m_pInstrument = pInstrument;
160  {  
161          return qsamplerInstrumentList::Item;          update();
162  }  }
163    
164    
165  // Payload accessor.  // Default destructor.
166  qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const  qsamplerInstrumentItem::~qsamplerInstrumentItem (void)
167  {  {
168          return m_pInstrument;          if (m_pInstrument)
169  }                  delete m_pInstrument;
170    }
171    
172  // Item refreshment.  
173  void qsamplerInstrumentItem::update (void)  // To virtually distinguish between list view items.
174  {  int qsamplerInstrumentItem::rtti (void) const
175          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));  {
176            return qsamplerInstrumentList::Item;
177          const QString s = "-";  }
178          if (m_pInstrument) {  
179                  setText(0, m_pInstrument->name());  
180                  setText(1, QString::number(m_pInstrument->bank()));  // Payload accessor.
181                  setText(2, QString::number(m_pInstrument->program()));  qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const
182                  setText(3, m_pInstrument->engineName());  {
183                  setText(4, QFileInfo(m_pInstrument->instrumentFile()).fileName());          return m_pInstrument;
184                  setText(5, QString::number(m_pInstrument->instrumentNr()));  }
185                  setText(6, QString::number(::lroundf(100.0f * m_pInstrument->volume())));  
186                  QString sLoadMode = s;  
187                  switch (m_pInstrument->loadMode()) {  // Item refreshment.
188                  case 3:  void qsamplerInstrumentItem::update (void)
189                          sLoadMode = QObject::tr("Persistent");  {
190                          break;          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));
191                  case 2:  
192                          sLoadMode = QObject::tr("On Demand Hold");          const QString s = "-";
193                          break;          if (m_pInstrument) {
194                  case 1:                  setText(0, m_pInstrument->name());
195                          sLoadMode = QObject::tr("On Demand");                  setText(1, QString::number(m_pInstrument->map()));
196                          break;                  setText(2, QString::number(m_pInstrument->bank()));
197                  }                  setText(3, QString::number(m_pInstrument->prog() + 1));
198                  setText(7, sLoadMode);                  setText(4, m_pInstrument->engineName());
199          } else {                  setText(5, QFileInfo(m_pInstrument->instrumentFile()).fileName());
200                  for (int i = 0; i < listView()->columns(); i++)                  setText(6, QString::number(m_pInstrument->instrumentNr()));
201                          setText(i, s);                  setText(7, QString::number(::lroundf(100.0f * m_pInstrument->volume())));
202          }                  QString sLoadMode = s;
203  }                  switch (m_pInstrument->loadMode()) {
204                    case 3:
205                            sLoadMode = QObject::tr("Persistent");
206  //----------------------------------------------------------------------------                          break;
207  // qsamplerInstrumentList -- MIDI instrument list view.                  case 2:
208  //                          sLoadMode = QObject::tr("On Demand Hold");
209                            break;
210  // Constructor.                  case 1:
211  qsamplerInstrumentList::qsamplerInstrumentList (                          sLoadMode = QObject::tr("On Demand");
212          QWidget *pParent, const char *pszName )                          break;
213          : QListView(pParent, pszName)                  }
214  {                  setText(8, sLoadMode);
215  //  QListView::setRootIsDecorated(true);          } else {
216          QListView::setResizeMode(QListView::NoColumn);                  for (int i = 0; i < listView()->columns(); i++)
217  //      QListView::setAcceptDrops(true);                          setText(i, s);
218          QListView::setDragAutoScroll(true);          }
219          QListView::setSizePolicy(  }
220                  QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));  
221  //      QListView::setShowToolTips(false);  
222          QListView::setSortColumn(-1);  //----------------------------------------------------------------------------
223    // qsamplerInstrumentList -- MIDI instrument list view.
224          QListView::addColumn(tr("Name"));  //
225          QListView::addColumn(tr("Bank"));  
226          QListView::addColumn(tr("Prog"));  // Constructor.
227          QListView::addColumn(tr("Engine"));  qsamplerInstrumentList::qsamplerInstrumentList (
228          QListView::addColumn(tr("File"));          QWidget *pParent, const char *pszName )
229          QListView::addColumn(tr("Nr"));          : QListView(pParent, pszName)
230          QListView::addColumn(tr("Vol"));  {
231          QListView::addColumn(tr("Mode"));          m_iMidiMap = LSCP_MIDI_MAP_ALL;
232    
233          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Bank  //  QListView::setRootIsDecorated(true);
234          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog          QListView::setAllColumnsShowFocus(true);
235          QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr          QListView::setResizeMode(QListView::NoColumn);
236          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol  //      QListView::setAcceptDrops(true);
237            QListView::setDragAutoScroll(true);
238          QListView::setColumnWidth(0, 60);       // Name          QListView::setSizePolicy(
239          QListView::setColumnWidth(0, 120);      // File                  QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
240    //      QListView::setShowToolTips(false);
241          m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);          QListView::setSortColumn(-1);
242          m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);  
243          m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);          QListView::addColumn(tr("Name"));
244          m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);          QListView::addColumn(tr("Map"));
245          m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);          QListView::addColumn(tr("Bank"));
246          m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), this);          QListView::addColumn(tr("Prog"));
247            QListView::addColumn(tr("Engine"));
248          QObject::connect(m_pNewGroupAction,          QListView::addColumn(tr("File"));
249                  SIGNAL(activated()),          QListView::addColumn(tr("Nr"));
250                  SLOT(newGroupSlot()));          QListView::addColumn(tr("Vol"));
251          QObject::connect(m_pNewItemAction,          QListView::addColumn(tr("Mode"));
252                  SIGNAL(activated()),  
253                  SLOT(newItemSlot()));          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Map
254          QObject::connect(m_pEditItemAction,          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Bank
255                  SIGNAL(activated()),          QListView::setColumnAlignment(3, Qt::AlignHCenter);     // Prog
256                  SLOT(editItemSlot()));          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Nr
257          QObject::connect(m_pRenameAction,          QListView::setColumnAlignment(7, Qt::AlignHCenter);     // Vol
258                  SIGNAL(activated()),  
259                  SLOT(renameSlot()));          QListView::setColumnWidth(0, 120);      // Name
260          QObject::connect(m_pDeleteAction,          QListView::setColumnWidth(5, 240);      // File
261                  SIGNAL(activated()),  
262                  SLOT(deleteSlot()));          m_pNewGroupAction = new QAction(
263          QObject::connect(m_pRefreshAction,                  QIconSet(QPixmap::fromMimeSource("itemGroupNew.png")),
264                  SIGNAL(activated()),                  tr("New &Group"), tr("Ctrl+G"), this);
265                  SLOT(refresh()));          m_pNewItemAction  = new QAction(
266                    QIconSet(QPixmap::fromMimeSource("itemNew.png")),
267          QObject::connect(this,                  tr("New &Instrument..."), tr("Ins"), this);
268                  SIGNAL(selectionChanged()),          m_pEditItemAction = new QAction(
269                  SLOT(selectionChangedSlot()));                  QIconSet(QPixmap::fromMimeSource("formEdit.png")),
270          QObject::connect(this,                  tr("&Edit..."), tr("Enter"), this);
271                  SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),          m_pRenameAction   = new QAction(tr("&Rename"), tr("F2"), this);
272                  SLOT(activatedSlot(QListViewItem*)));          m_pDeleteAction   = new QAction(
273          QObject::connect(this,                  QIconSet(QPixmap::fromMimeSource("formRemove.png")),
274                  SIGNAL(returnPressed(QListViewItem*)),                  tr("&Delete"), tr("Del"), this);
275                  SLOT(activatedSlot(QListViewItem*)));          m_pRefreshAction  = new QAction(
276          QObject::connect(this,                  QIconSet(QPixmap::fromMimeSource("formRefresh.png")),
277                  SIGNAL(itemRenamed(QListViewItem*,int)),                  tr("Re&fresh"), tr("F5"), this);
278                  SLOT(renamedSlot(QListViewItem*)));  
279            m_pNewGroupAction->setToolTip(tr("New Group"));
280          selectionChangedSlot();          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  // Default destructor.          m_pRefreshAction->setToolTip(tr("Refresh"));
285  qsamplerInstrumentList::~qsamplerInstrumentList (void)  
286  {          QObject::connect(m_pNewGroupAction,
287          delete m_pNewGroupAction;                  SIGNAL(activated()),
288          delete m_pNewItemAction;                  SLOT(newGroupSlot()));
289          delete m_pEditItemAction;          QObject::connect(m_pNewItemAction,
290          delete m_pRenameAction;                  SIGNAL(activated()),
291          delete m_pDeleteAction;                  SLOT(newItemSlot()));
292  }          QObject::connect(m_pEditItemAction,
293                    SIGNAL(activated()),
294                    SLOT(editItemSlot()));
295  // Add a new instrument item, optionally under a given group.          QObject::connect(m_pRenameAction,
296  qsamplerInstrumentItem *qsamplerInstrumentList::addItem (                  SIGNAL(activated()),
297          qsamplerInstrument *pInstrument,                  SLOT(renameSlot()));
298          qsamplerInstrumentGroup *pParentGroup )          QObject::connect(m_pDeleteAction,
299  {                  SIGNAL(activated()),
300          qsamplerInstrumentItem *pItem = findItem(pInstrument);                  SLOT(deleteSlot()));
301          if (pItem == NULL) {          QObject::connect(m_pRefreshAction,
302                  if (pParentGroup)                  SIGNAL(activated()),
303                          pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);                  SLOT(refresh()));
304                  else  
305                          pItem = new qsamplerInstrumentItem(this, pInstrument);          QObject::connect(this,
306          }                  SIGNAL(selectionChanged()),
307          QListView::setSelected(pItem, true);                  SLOT(selectionChangedSlot()));
308          return pItem;          QObject::connect(this,
309  }                  SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),
310                    SLOT(activatedSlot(QListViewItem*)));
311            QObject::connect(this,
312  // Add a new instrument group, optionally under another group.                  SIGNAL(returnPressed(QListViewItem*)),
313  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (                  SLOT(activatedSlot(QListViewItem*)));
314          const QString& sName, qsamplerInstrumentGroup *pParentGroup )          QObject::connect(this,
315  {                  SIGNAL(itemRenamed(QListViewItem*,int)),
316          qsamplerInstrumentGroup *pGroup = findGroup(sName);                  SLOT(renamedSlot(QListViewItem*)));
317          if (pGroup == NULL) {  
318                  if (pParentGroup)          selectionChangedSlot();
319                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);  }
320                  else  
321                          pGroup = new qsamplerInstrumentGroup(this, sName);  
322          }  // Default destructor.
323          QListView::setSelected(pGroup, true);  qsamplerInstrumentList::~qsamplerInstrumentList (void)
324          return pGroup;  {
325  }          delete m_pNewGroupAction;
326            delete m_pNewItemAction;
327            delete m_pEditItemAction;
328  // Find a group item, given its name.          delete m_pRenameAction;
329  qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (          delete m_pDeleteAction;
330          const QString& sName ) const  }
331  {  
332          // Iterate all over the place to search for the group.  
333          QListViewItemIterator iter((QListView *) this);  // Add a new instrument item, optionally under a given group.
334          while (iter.current()) {  qsamplerInstrumentItem *qsamplerInstrumentList::addItem (
335                  QListViewItem *pItem = iter.current();          qsamplerInstrument *pInstrument,
336                  if (pItem->rtti() == Group && pItem->text(0) == sName)          qsamplerInstrumentGroup *pParentGroup )
337                          return static_cast<qsamplerInstrumentGroup *> (pItem);  {
338                  ++iter;          // Check it there's already one instrument item
339          }          // with the very same key (bank, program);
340          // Not found.          // if yes, just remove it without prejudice...
341          return NULL;          qsamplerInstrumentItem *pItem = findItem(pInstrument);
342  }          if (pItem) {
343                    // If exactly the same, just update view and bail out...
344                    if (pItem->instrument() == pInstrument) {
345  // Find a file item, given its name.                          pItem->update();
346  qsamplerInstrumentItem *qsamplerInstrumentList::findItem (                          return pItem;
347          qsamplerInstrument *pInstrument ) const                  }
348  {                  // Remove it, as instrument keys must be unique.
349          if (pInstrument == NULL)                  delete pItem;
350                  return NULL;          }
351    
352          // Iterate all over the place to search for the group.          // Add the new item under proper group one, if any...
353          QListViewItemIterator iter((QListView *) this);          if (pParentGroup) {
354          while (iter.current()) {                  pParentGroup->setOpen(true);
355                  QListViewItem *pListItem = iter.current();                  pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);
356                  if (pListItem->rtti() == Item) {          } else {
357                          qsamplerInstrumentItem *pItem                  pItem = new qsamplerInstrumentItem(this, pInstrument);
358                                  = static_cast<qsamplerInstrumentItem *> (pListItem);          }
359                          if (pItem && pItem->instrument()  
360                                  && pItem->instrument()->bank() == pInstrument->bank()          // Set it as current selection...
361                                  && pItem->instrument()->program() == pInstrument->program())          QListView::setSelected(pItem, true);
362                                  return pItem;  
363                  }          return pItem;
364                  ++iter;  }
365          }  
366          // Not found.  
367          return NULL;  // Add a new instrument group, optionally under another group.
368  }  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (
369            const QString& sName, qsamplerInstrumentGroup *pParentGroup )
370    {
371  // Find and return the nearest group item...          qsamplerInstrumentGroup *pGroup = findGroup(sName);
372  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (          if (pGroup == NULL) {
373          QListViewItem *pItem ) const                  if (pParentGroup) {
374  {                          pParentGroup->setOpen(true);
375          while (pItem && pItem->rtti() != Group)                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);
376                  pItem = pItem->parent();                  } else {
377          return static_cast<qsamplerInstrumentGroup *> (pItem);                          pGroup = new qsamplerInstrumentGroup(this, sName);
378  }                  }
379            }
380            QListView::setSelected(pGroup, true);
381  // Add a new group item below the current one.          return pGroup;
382  void qsamplerInstrumentList::newGroupSlot (void)  }
383  {  
384          qsamplerInstrumentGroup *pParentGroup  
385                  = groupItem(QListView::selectedItem());  // Find a group item, given its name.
386          qsamplerInstrumentGroup *pNewGroup  qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (
387                  = addGroup(tr("New Group"), pParentGroup);          const QString& sName ) const
388          if (pParentGroup)  {
389                  pParentGroup->setOpen(true);          // Iterate all over the place to search for the group.
390          if (pNewGroup)          QListViewItemIterator iter((QListView *) this);
391                  pNewGroup->startRename(0);          while (iter.current()) {
392                    QListViewItem *pItem = iter.current();
393          selectionChangedSlot();                  if (pItem->rtti() == Group && pItem->text(0) == sName)
394  }                          return static_cast<qsamplerInstrumentGroup *> (pItem);
395                    ++iter;
396            }
397  // Add a new instrument item below the current one.          // Not found.
398  void qsamplerInstrumentList::newItemSlot (void)          return NULL;
399  {  }
400          qsamplerInstrument *pInstrument = new qsamplerInstrument();  
401    
402          qsamplerInstrumentForm form(this);  // Find a file item, given its name.
403          form.setup(pInstrument);  qsamplerInstrumentItem *qsamplerInstrumentList::findItem (
404          if (!form.exec()) {          qsamplerInstrument *pInstrument ) const
405                  delete pInstrument;  {
406                  return;          if (pInstrument == NULL)
407          }                  return NULL;
408    
409          // Check it there's already one for the same key (bank, program)          // Iterate all over the place to search for the group.
410          qsamplerInstrumentItem *pItem = findItem(pInstrument);          QListViewItemIterator iter((QListView *) this);
411          if (pItem)          while (iter.current()) {
412                  delete pItem;                  QListViewItem *pListItem = iter.current();
413                    if (pListItem->rtti() == Item) {
414          pInstrument->map();                          qsamplerInstrumentItem *pItem
415          emit instrumentsChanged();                                  = static_cast<qsamplerInstrumentItem *> (pListItem);
416                            if (pItem && pItem->instrument()
417          qsamplerInstrumentGroup *pParentGroup                                  && pItem->instrument()->map()  == pInstrument->map()
418                  = groupItem(QListView::selectedItem());                                  && pItem->instrument()->bank() == pInstrument->bank()
419          addItem(pInstrument, pParentGroup);                                  && pItem->instrument()->prog() == pInstrument->prog())
420          if (pParentGroup)                                  return pItem;
421                  pParentGroup->setOpen(true);                  }
422                    ++iter;
423          selectionChangedSlot();          }
424  }          // Not found.
425            return NULL;
426    }
427  // Edit current item below the current one.  
428  void qsamplerInstrumentList::editItemSlot (void)  
429  {  // Find and return the nearest group item...
430          QListViewItem *pListItem = QListView::selectedItem();  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (
431          if (pListItem == NULL)          QListViewItem *pItem ) const
432                  return;  {
433          if (pListItem->rtti() == Item) {          while (pItem && pItem->rtti() != Group)
434                  qsamplerInstrumentItem *pItem                  pItem = pItem->parent();
435                          = static_cast<qsamplerInstrumentItem *> (pListItem);          return static_cast<qsamplerInstrumentGroup *> (pItem);
436                  if (pItem && pItem->instrument()) {  }
437                          qsamplerInstrumentForm form(this);  
438                          form.setup(pItem->instrument());  
439                          if (form.exec()) {  // Add a new group item below the current one.
440                                  pItem->instrument()->map();  void qsamplerInstrumentList::newGroupSlot (void)
441                                  emit instrumentsChanged();  {
442                                  pItem->update();          qsamplerInstrumentGroup *pNewGroup
443                          }                  = addGroup(tr("New Group"), groupItem(QListView::selectedItem()));
444                  }          if (pNewGroup)
445          }                  pNewGroup->startRename(0);
446    
447          selectionChangedSlot();          selectionChangedSlot();
448  }  }
449    
450    
451  // Rename current group/item.  // Map selector.
452  void qsamplerInstrumentList::renameSlot (void)  void qsamplerInstrumentList::setMidiMap ( int iMidiMap )
453  {  {
454          QListViewItem *pListItem = QListView::selectedItem();          if (iMidiMap < 0)
455          if (pListItem)                  iMidiMap = LSCP_MIDI_MAP_ALL;
456                  pListItem->startRename(0);  
457            m_iMidiMap = iMidiMap;
458          selectionChangedSlot();  }
459  }  
460    int qsamplerInstrumentList::midiMap (void) const
461    {
462  // Remove current group/item.          return m_iMidiMap;
463  void qsamplerInstrumentList::deleteSlot (void)  }
464  {  
465          QListViewItem *pListItem = QListView::selectedItem();  
466          if (pListItem) {  // List actions accessors.
467                  if (pListItem->rtti() == Item) {  QAction *qsamplerInstrumentList::newGroupAction (void) const
468                          qsamplerInstrumentItem *pItem  {
469                                  = static_cast<qsamplerInstrumentItem *> (pListItem);          return m_pNewGroupAction;
470                          if (pItem && pItem->instrument()) {  }
471                                  pItem->instrument()->unmap();  
472                                  emit instrumentsChanged();  QAction *qsamplerInstrumentList::newItemAction (void) const
473                          }  {
474                  }          return m_pNewItemAction;
475                  delete pListItem;  }
476          }  
477    QAction *qsamplerInstrumentList::editItemAction (void) const
478          selectionChangedSlot();  {
479  }          return m_pEditItemAction;
480    }
481    
482  // In-place selection slot.  QAction *qsamplerInstrumentList::renameAction (void) const
483  void qsamplerInstrumentList::selectionChangedSlot (void)  {
484  {          return m_pRenameAction;
485          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  }
486          QListViewItem *pListItem = QListView::selectedItem();  
487          bool bEnabled = (pMainForm && pMainForm->client());  QAction *qsamplerInstrumentList::deleteAction (void) const
488          m_pNewItemAction->setEnabled(bEnabled);  {
489          bEnabled = (bEnabled && pListItem != NULL);          return m_pDeleteAction;
490          m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);  }
491          m_pRenameAction->setEnabled(bEnabled);  
492          m_pDeleteAction->setEnabled(bEnabled);  QAction *qsamplerInstrumentList::refreshAction (void) const
493  }  {
494            return m_pRefreshAction;
495    }
496  // In-place activation slot.  
497  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  
498  {  // Add a new instrument item below the current one.
499          // FIXME: Hope the list view item is the one selected.  void qsamplerInstrumentList::newItemSlot (void)
500          if (pListItem->rtti() == Item)  {
501                  editItemSlot();          qsamplerInstrument *pInstrument = new qsamplerInstrument();
502  }  
503            qsamplerInstrumentForm form(this);
504            form.setup(pInstrument);
505  // In-place aliasing slot.          if (!form.exec()) {
506  void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )                  delete pInstrument;
507  {                  return;
508          if (pListItem->rtti() == Item) {          }
509                  qsamplerInstrumentItem *pItem  
510                          = static_cast<qsamplerInstrumentItem *> (pListItem);          // Commit...
511                  if (pItem && pItem->instrument()) {          pInstrument->mapInstrument();
512                          pItem->instrument()->setName(pListItem->text(0));          // add new item to the tree...
513                          pItem->instrument()->map();          addItem(pInstrument, groupItem(QListView::selectedItem()));
514                          emit instrumentsChanged();          // Notify we've changes...
515                          pItem->update();          emit instrumentsChanged();
516                  }  
517          }          selectionChangedSlot();
518  }  }
519    
520    
521  // Context menu request event handler.  // Edit current item below the current one.
522  void qsamplerInstrumentList::contextMenuEvent (  void qsamplerInstrumentList::editItemSlot (void)
523          QContextMenuEvent *pContextMenuEvent )  {
524  {          QListViewItem *pListItem = QListView::selectedItem();
525          QPopupMenu menu(this);          if (pListItem == NULL)
526                    return;
527          // Construct context menu.          if (pListItem->rtti() == Item) {
528          m_pNewItemAction->addTo(&menu);                  qsamplerInstrument *pInstrument = NULL;
529  //      m_pNewGroupAction->addTo(&menu);                  qsamplerInstrumentItem *pItem
530          menu.insertSeparator();                          = static_cast<qsamplerInstrumentItem *> (pListItem);
531          m_pEditItemAction->addTo(&menu);                  if (pItem)
532          m_pRenameAction->addTo(&menu);                          pInstrument = pItem->instrument();
533          m_pDeleteAction->addTo(&menu);                  if (pInstrument) {
534          menu.insertSeparator();                          // Save current key values...
535          m_pRefreshAction->addTo(&menu);                          qsamplerInstrument oldInstrument(*pInstrument);
536                            // Do the edit dance...
537          menu.exec(pContextMenuEvent->globalPos());                          qsamplerInstrumentForm form(this);
538  }                          form.setup(pInstrument);
539                            if (form.exec()) {
540                                    // Commit...
541  // General reloader.                                  pInstrument->mapInstrument();
542  void qsamplerInstrumentList::refresh (void)                                  // Check whether we changed instrument key...
543  {                                  if (oldInstrument.map()  == pInstrument->map()  &&
544          clear();                                          oldInstrument.bank() == pInstrument->bank() &&
545                                            oldInstrument.prog() == pInstrument->prog()) {
546          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();                                          // just update tree item...
547          if (pMainForm == NULL)                                          pItem->update();
548                  return;                                  } else {
549          if (pMainForm->client() == NULL)                                          // Unmap old instance...
550                  return;                                          oldInstrument.unmapInstrument();
551                                            // Change item tree, whether applicable...
552          qsamplerInstrumentItem *pItem = NULL;                                          if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) {
553          lscp_midi_instrument_t *pInstrs                                                  // Add new brand item into view...
554                  = ::lscp_list_midi_instruments(pMainForm->client());                                                  addItem(pInstrument, groupItem(pListItem));
555          for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {                                          } else {
556                  int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;                                                  // Just remove/hide old one.
557                  int iProgram = pInstrs[iInstr].program;                                                  delete pItem;
558                  qsamplerInstrument *pInstrument                                          }
559                          = new qsamplerInstrument(iBank, iProgram);                                  }
560                  if (pInstrument->get())                                  // Notify we've changes...
561                          pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);                                  emit instrumentsChanged();
562          }                          }
563                    }
564          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {          }
565                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");  
566                  pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));          selectionChangedSlot();
567          }  }
568    
569          selectionChangedSlot();  
570  }  // Rename current group/item.
571    void qsamplerInstrumentList::renameSlot (void)
572    {
573  // end of qsamplerInstrumentList.cpp          QListViewItem *pListItem = QListView::selectedItem();
574            if (pListItem)
575                    pListItem->startRename(0);
576    
577            selectionChangedSlot();
578    }
579    
580    
581    // Remove current group/item.
582    void qsamplerInstrumentList::deleteSlot (void)
583    {
584            QListViewItem *pListItem = QListView::selectedItem();
585            if (pListItem == NULL)
586                    return;
587    
588            qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
589            if (pMainForm == NULL)
590                    return;
591    
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                    }
614            }
615    
616            // Do it for real...
617            delete pListItem;
618    
619            selectionChangedSlot();
620    }
621    
622    
623    // In-place selection slot.
624    void qsamplerInstrumentList::selectionChangedSlot (void)
625    {
626            qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
627            QListViewItem *pListItem = QListView::selectedItem();
628            bool bEnabled = (pMainForm && pMainForm->client());
629            m_pNewItemAction->setEnabled(bEnabled);
630            bEnabled = (bEnabled && pListItem != NULL);
631            m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);
632            m_pRenameAction->setEnabled(bEnabled);
633            m_pDeleteAction->setEnabled(bEnabled);
634    }
635    
636    
637    // In-place activation slot.
638    void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )
639    {
640            // FIXME: Hope the list view item is the one selected.
641            if (pListItem && pListItem->rtti() == Item)
642                    editItemSlot();
643    }
644    
645    
646    // In-place aliasing slot.
647    void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )
648    {
649            if (pListItem->rtti() == Item) {
650                    qsamplerInstrumentItem *pItem
651                            = static_cast<qsamplerInstrumentItem *> (pListItem);
652                    if (pItem && pItem->instrument()) {
653                            pItem->instrument()->setName(pListItem->text(0));
654                            pItem->instrument()->mapInstrument();
655                            emit instrumentsChanged();
656                            pItem->update();
657                    }
658            }
659    }
660    
661    
662    // Context menu request event handler.
663    void qsamplerInstrumentList::contextMenuEvent (
664            QContextMenuEvent *pContextMenuEvent )
665    {
666            if (!m_pNewItemAction->isEnabled())
667                    return;
668    
669            QPopupMenu menu(this);
670    
671            // Construct context menu.
672            m_pNewItemAction->addTo(&menu);
673    //      m_pNewGroupAction->addTo(&menu);
674            menu.insertSeparator();
675            m_pEditItemAction->addTo(&menu);
676            m_pRenameAction->addTo(&menu);
677            m_pDeleteAction->addTo(&menu);
678            menu.insertSeparator();
679            m_pRefreshAction->addTo(&menu);
680    
681            menu.exec(pContextMenuEvent->globalPos());
682    }
683    
684    
685    // General reloader.
686    void qsamplerInstrumentList::refresh (void)
687    {
688            clear();
689    
690            qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
691            if (pMainForm == NULL)
692                    return;
693            if (pMainForm->client() == NULL)
694                    return;
695    
696            QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
697    
698            // Load the whole bunch of instrument items...
699            qsamplerInstrumentItem *pItem = NULL;
700            lscp_midi_instrument_t *pInstrs
701                    = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
702            for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
703                    int iMap  = pInstrs[iInstr].map;
704                    int iBank = pInstrs[iInstr].bank;
705                    int iProg = pInstrs[iInstr].prog;
706                    qsamplerInstrument *pInstrument
707                            = new qsamplerInstrument(iMap, iBank, iProg);
708                    if (pInstrument->getInstrument())
709                            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())) {
717                    pMainForm->appendMessagesClient("lscp_list_midi_instruments");
718                    pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
719            }
720    
721            selectionChangedSlot();
722    }
723    
724    
725    // end of qsamplerInstrumentList.cpp
726    

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

  ViewVC Help
Powered by ViewVC