/[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 987 by capela, Tue Dec 19 11:19:55 2006 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-2005, 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 <qmessagebox.h>
32  #include <qpopupmenu.h>  #include <qaction.h>
33    #include <qfileinfo.h>
34  // Needed for lroundf()  #include <qpopupmenu.h>
35  #include <math.h>  
36    // Needed for lroundf()
37    #include <math.h>
38  //----------------------------------------------------------------------  
39  // class qsamplerInstrumentGroup -- custom group list view item.  #ifndef CONFIG_ROUND
40  //  static inline long lroundf ( float x )
41    {
42  // Constructors.          if (x >= 0.0f)
43  qsamplerInstrumentGroup::qsamplerInstrumentGroup (                  return long(x + 0.5f);
44          qsamplerInstrumentList *pListView, const QString& sName,          else
45          QListViewItem *pItemAfter )                  return long(x - 0.5f);
46          : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())  }
47  {  #endif
48          QListViewItem::setRenameEnabled(0, true);  
49    
50          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  //----------------------------------------------------------------------
51          QListViewItem::setText(0, sName);  // class qsamplerInstrumentGroup -- custom group list view item.
52  }  //
53    
54    // Constructors.
55  qsamplerInstrumentGroup::qsamplerInstrumentGroup (  qsamplerInstrumentGroup::qsamplerInstrumentGroup (
56          qsamplerInstrumentGroup *pGroupItem, const QString& sName )          qsamplerInstrumentList *pListView, const QString& sName,
57          : QListViewItem(pGroupItem, sName)          QListViewItem *pItemAfter )
58  {          : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())
59          QListViewItem::setRenameEnabled(0, true);  {
60            QListViewItem::setRenameEnabled(0, true);
61          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  
62  }          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));
63            QListViewItem::setText(0, sName);
64    }
65  // Default destructor.  
66  qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)  
67  {  qsamplerInstrumentGroup::qsamplerInstrumentGroup (
68  }          qsamplerInstrumentGroup *pGroupItem, const QString& sName )
69            : QListViewItem(pGroupItem, sName)
70    {
71  // Instance accessors.          QListViewItem::setRenameEnabled(0, true);
72  void qsamplerInstrumentGroup::setName ( const QString& sName )  
73  {          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));
74          QListViewItem::setText(0, sName);  }
75  }  
76    
77    // Default destructor.
78  QString qsamplerInstrumentGroup::name (void) const  qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)
79  {  {
80          return QListViewItem::text(0);  }
81  }  
82    
83    // Instance accessors.
84  qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const  void qsamplerInstrumentGroup::setName ( const QString& sName )
85  {  {
86          QListViewItem *pParent = QListViewItem::parent();          QListViewItem::setText(0, sName);
87          while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)  }
88                  pParent = pParent->parent();  
89          return static_cast<qsamplerInstrumentGroup *> (pParent);  
90  }  QString qsamplerInstrumentGroup::name (void) const
91    {
92            return QListViewItem::text(0);
93  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const  }
94  {  
95          return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());  
96  }  qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const
97    {
98            QListViewItem *pParent = QListViewItem::parent();
99  // To show up whether its open or not.          while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)
100  void qsamplerInstrumentGroup::setOpen ( bool bOpen )                  pParent = pParent->parent();
101  {          return static_cast<qsamplerInstrumentGroup *> (pParent);
102          // Set the proper pixmap of this...  }
103          if (rtti() == qsamplerInstrumentList::Group) {  
104                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(  
105                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const
106          }  {
107          // Open it up...          return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());
108          QListViewItem::setOpen(bOpen);  }
109    
110          // All ancestors should be also visible.  
111          if (bOpen && QListViewItem::parent())  // To show up whether its open or not.
112                  QListViewItem::parent()->setOpen(true);  void qsamplerInstrumentGroup::setOpen ( bool bOpen )
113  }  {
114            // Set the proper pixmap of this...
115            if (rtti() == qsamplerInstrumentList::Group) {
116  // To virtually distinguish between list view items.                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(
117  int qsamplerInstrumentGroup::rtti (void) const                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));
118  {          }
119          return qsamplerInstrumentList::Group;          // Open it up...
120  }          QListViewItem::setOpen(bOpen);
121    
122            // All ancestors should be also visible.
123  //----------------------------------------------------------------------          if (bOpen && QListViewItem::parent())
124  // class qsamplerInstrumentItem -- custom file list view item.                  QListViewItem::parent()->setOpen(true);
125  //  }
126    
127  // Constructors.  
128  qsamplerInstrumentItem::qsamplerInstrumentItem (  // To virtually distinguish between list view items.
129          qsamplerInstrumentList *pListView,  int qsamplerInstrumentGroup::rtti (void) const
130          qsamplerInstrument *pInstrument,  {
131          QListViewItem *pItemAfter )          return qsamplerInstrumentList::Group;
132          : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)  }
133  {  
134          m_pInstrument = pInstrument;  
135    //----------------------------------------------------------------------
136          update();  // class qsamplerInstrumentItem -- custom file list view item.
137  }  //
138    
139  qsamplerInstrumentItem::qsamplerInstrumentItem (  // Constructors.
140          qsamplerInstrumentGroup *pGroupItem,  qsamplerInstrumentItem::qsamplerInstrumentItem (
141          qsamplerInstrument *pInstrument )          qsamplerInstrumentList *pListView,
142          : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())          qsamplerInstrument *pInstrument,
143  {          QListViewItem *pItemAfter )
144          m_pInstrument = pInstrument;          : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)
145    {
146          update();          m_pInstrument = pInstrument;
147  }  
148            update();
149    }
150  // Default destructor.  
151  qsamplerInstrumentItem::~qsamplerInstrumentItem (void)  qsamplerInstrumentItem::qsamplerInstrumentItem (
152  {          qsamplerInstrumentGroup *pGroupItem,
153          if (m_pInstrument)          qsamplerInstrument *pInstrument )
154                  delete m_pInstrument;          : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())
155  }  {
156            m_pInstrument = pInstrument;
157    
158  // To virtually distinguish between list view items.          update();
159  int qsamplerInstrumentItem::rtti (void) const  }
160  {  
161          return qsamplerInstrumentList::Item;  
162  }  // Default destructor.
163    qsamplerInstrumentItem::~qsamplerInstrumentItem (void)
164    {
165  // Payload accessor.          if (m_pInstrument)
166  qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const                  delete m_pInstrument;
167  {  }
168          return m_pInstrument;  
169  }  
170    // To virtually distinguish between list view items.
171    int qsamplerInstrumentItem::rtti (void) const
172  // Item refreshment.  {
173  void qsamplerInstrumentItem::update (void)          return qsamplerInstrumentList::Item;
174  {  }
175          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));  
176    
177          const QString s = "-";  // Payload accessor.
178          if (m_pInstrument) {  qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const
179                  setText(0, m_pInstrument->name());  {
180                  setText(1, QString::number(m_pInstrument->bank()));          return m_pInstrument;
181                  setText(2, QString::number(m_pInstrument->program()));  }
182                  setText(3, m_pInstrument->engineName());  
183                  setText(4, QFileInfo(m_pInstrument->instrumentFile()).fileName());  
184                  setText(5, QString::number(m_pInstrument->instrumentNr()));  // Item refreshment.
185                  setText(6, QString::number(::lroundf(100.0f * m_pInstrument->volume())));  void qsamplerInstrumentItem::update (void)
186                  QString sLoadMode = s;  {
187                  switch (m_pInstrument->loadMode()) {          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));
188                  case 3:  
189                          sLoadMode = QObject::tr("Persistent");          const QString s = "-";
190                          break;          if (m_pInstrument) {
191                  case 2:                  setText(0, m_pInstrument->name());
192                          sLoadMode = QObject::tr("On Demand Hold");                  setText(1, QString::number(m_pInstrument->map()));
193                          break;                  setText(2, QString::number(m_pInstrument->bank()));
194                  case 1:                  setText(3, QString::number(m_pInstrument->prog() + 1));
195                          sLoadMode = QObject::tr("On Demand");                  setText(4, m_pInstrument->engineName());
196                          break;                  setText(5, QFileInfo(m_pInstrument->instrumentFile()).fileName());
197                  }                  setText(6, QString::number(m_pInstrument->instrumentNr()));
198                  setText(7, sLoadMode);                  setText(7, QString::number(::lroundf(100.0f * m_pInstrument->volume())));
199          } else {                  QString sLoadMode = s;
200                  for (int i = 0; i < listView()->columns(); i++)                  switch (m_pInstrument->loadMode()) {
201                          setText(i, s);                  case 3:
202          }                          sLoadMode = QObject::tr("Persistent");
203  }                          break;
204                    case 2:
205                            sLoadMode = QObject::tr("On Demand Hold");
206  //----------------------------------------------------------------------------                          break;
207  // qsamplerInstrumentList -- MIDI instrument list view.                  case 1:
208  //                          sLoadMode = QObject::tr("On Demand");
209                            break;
210  // Constructor.                  }
211  qsamplerInstrumentList::qsamplerInstrumentList (                  setText(8, sLoadMode);
212          QWidget *pParent, const char *pszName )          } else {
213          : QListView(pParent, pszName)                  for (int i = 0; i < listView()->columns(); i++)
214  {                          setText(i, s);
215  //  QListView::setRootIsDecorated(true);          }
216          QListView::setResizeMode(QListView::NoColumn);  }
217  //      QListView::setAcceptDrops(true);  
218          QListView::setDragAutoScroll(true);  
219          QListView::setSizePolicy(  //----------------------------------------------------------------------------
220                  QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));  // qsamplerInstrumentList -- MIDI instrument list view.
221  //      QListView::setShowToolTips(false);  //
222          QListView::setSortColumn(-1);  
223    // Constructor.
224          QListView::addColumn(tr("Name"));  qsamplerInstrumentList::qsamplerInstrumentList (
225          QListView::addColumn(tr("Bank"));          QWidget *pParent, const char *pszName )
226          QListView::addColumn(tr("Prog"));          : QListView(pParent, pszName)
227          QListView::addColumn(tr("Engine"));  {
228          QListView::addColumn(tr("File"));  //  QListView::setRootIsDecorated(true);
229          QListView::addColumn(tr("Nr"));          QListView::setResizeMode(QListView::NoColumn);
230          QListView::addColumn(tr("Vol"));  //      QListView::setAcceptDrops(true);
231          QListView::addColumn(tr("Mode"));          QListView::setDragAutoScroll(true);
232            QListView::setSizePolicy(
233          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Bank                  QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
234          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog  //      QListView::setShowToolTips(false);
235          QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr          QListView::setSortColumn(-1);
236          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol  
237            QListView::addColumn(tr("Name"));
238          QListView::setColumnWidth(0, 60);       // Name          QListView::addColumn(tr("Map"));
239          QListView::setColumnWidth(0, 120);      // File          QListView::addColumn(tr("Bank"));
240            QListView::addColumn(tr("Prog"));
241          m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);          QListView::addColumn(tr("Engine"));
242          m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);          QListView::addColumn(tr("File"));
243          m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);          QListView::addColumn(tr("Nr"));
244          m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);          QListView::addColumn(tr("Vol"));
245          m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);          QListView::addColumn(tr("Mode"));
246          m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), this);  
247            QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Map
248          QObject::connect(m_pNewGroupAction,          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Bank
249                  SIGNAL(activated()),          QListView::setColumnAlignment(3, Qt::AlignHCenter);     // Prog
250                  SLOT(newGroupSlot()));          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Nr
251          QObject::connect(m_pNewItemAction,          QListView::setColumnAlignment(7, Qt::AlignHCenter);     // Vol
252                  SIGNAL(activated()),  
253                  SLOT(newItemSlot()));          QListView::setColumnWidth(0, 120);      // Name
254          QObject::connect(m_pEditItemAction,          QListView::setColumnWidth(5, 240);      // File
255                  SIGNAL(activated()),  
256                  SLOT(editItemSlot()));          m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);
257          QObject::connect(m_pRenameAction,          m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ins"), this);
258                  SIGNAL(activated()),          m_pEditItemAction = new QAction(tr("&Edit..."), tr("Enter"), this);
259                  SLOT(renameSlot()));          m_pRenameAction   = new QAction(tr("&Rename"), tr("F2"), this);
260          QObject::connect(m_pDeleteAction,          m_pDeleteAction   = new QAction(tr("&Delete"), tr("Del"), this);
261                  SIGNAL(activated()),          m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("F5"), this);
262                  SLOT(deleteSlot()));  
263          QObject::connect(m_pRefreshAction,          QObject::connect(m_pNewGroupAction,
264                  SIGNAL(activated()),                  SIGNAL(activated()),
265                  SLOT(refresh()));                  SLOT(newGroupSlot()));
266            QObject::connect(m_pNewItemAction,
267          QObject::connect(this,                  SIGNAL(activated()),
268                  SIGNAL(selectionChanged()),                  SLOT(newItemSlot()));
269                  SLOT(selectionChangedSlot()));          QObject::connect(m_pEditItemAction,
270          QObject::connect(this,                  SIGNAL(activated()),
271                  SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),                  SLOT(editItemSlot()));
272                  SLOT(activatedSlot(QListViewItem*)));          QObject::connect(m_pRenameAction,
273          QObject::connect(this,                  SIGNAL(activated()),
274                  SIGNAL(returnPressed(QListViewItem*)),                  SLOT(renameSlot()));
275                  SLOT(activatedSlot(QListViewItem*)));          QObject::connect(m_pDeleteAction,
276          QObject::connect(this,                  SIGNAL(activated()),
277                  SIGNAL(itemRenamed(QListViewItem*,int)),                  SLOT(deleteSlot()));
278                  SLOT(renamedSlot(QListViewItem*)));          QObject::connect(m_pRefreshAction,
279                    SIGNAL(activated()),
280          selectionChangedSlot();                  SLOT(refresh()));
281  }  
282            QObject::connect(this,
283                    SIGNAL(selectionChanged()),
284  // Default destructor.                  SLOT(selectionChangedSlot()));
285  qsamplerInstrumentList::~qsamplerInstrumentList (void)          QObject::connect(this,
286  {                  SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),
287          delete m_pNewGroupAction;                  SLOT(activatedSlot(QListViewItem*)));
288          delete m_pNewItemAction;          QObject::connect(this,
289          delete m_pEditItemAction;                  SIGNAL(returnPressed(QListViewItem*)),
290          delete m_pRenameAction;                  SLOT(activatedSlot(QListViewItem*)));
291          delete m_pDeleteAction;          QObject::connect(this,
292  }                  SIGNAL(itemRenamed(QListViewItem*,int)),
293                    SLOT(renamedSlot(QListViewItem*)));
294    
295  // Add a new instrument item, optionally under a given group.          selectionChangedSlot();
296  qsamplerInstrumentItem *qsamplerInstrumentList::addItem (  }
297          qsamplerInstrument *pInstrument,  
298          qsamplerInstrumentGroup *pParentGroup )  
299  {  // Default destructor.
300          qsamplerInstrumentItem *pItem = findItem(pInstrument);  qsamplerInstrumentList::~qsamplerInstrumentList (void)
301          if (pItem == NULL) {  {
302                  if (pParentGroup)          delete m_pNewGroupAction;
303                          pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);          delete m_pNewItemAction;
304                  else          delete m_pEditItemAction;
305                          pItem = new qsamplerInstrumentItem(this, pInstrument);          delete m_pRenameAction;
306          }          delete m_pDeleteAction;
307          QListView::setSelected(pItem, true);  }
308          return pItem;  
309  }  
310    // Add a new instrument item, optionally under a given group.
311    qsamplerInstrumentItem *qsamplerInstrumentList::addItem (
312  // Add a new instrument group, optionally under another group.          qsamplerInstrument *pInstrument,
313  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (          qsamplerInstrumentGroup *pParentGroup )
314          const QString& sName, qsamplerInstrumentGroup *pParentGroup )  {
315  {          qsamplerInstrumentItem *pItem = findItem(pInstrument);
316          qsamplerInstrumentGroup *pGroup = findGroup(sName);          if (pItem == NULL) {
317          if (pGroup == NULL) {                  if (pParentGroup)
318                  if (pParentGroup)                          pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);
319                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);                  else
320                  else                          pItem = new qsamplerInstrumentItem(this, pInstrument);
321                          pGroup = new qsamplerInstrumentGroup(this, sName);          }
322          }          QListView::setSelected(pItem, true);
323          QListView::setSelected(pGroup, true);          return pItem;
324          return pGroup;  }
325  }  
326    
327    // Add a new instrument group, optionally under another group.
328  // Find a group item, given its name.  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (
329  qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (          const QString& sName, qsamplerInstrumentGroup *pParentGroup )
330          const QString& sName ) const  {
331  {          qsamplerInstrumentGroup *pGroup = findGroup(sName);
332          // Iterate all over the place to search for the group.          if (pGroup == NULL) {
333          QListViewItemIterator iter((QListView *) this);                  if (pParentGroup)
334          while (iter.current()) {                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);
335                  QListViewItem *pItem = iter.current();                  else
336                  if (pItem->rtti() == Group && pItem->text(0) == sName)                          pGroup = new qsamplerInstrumentGroup(this, sName);
337                          return static_cast<qsamplerInstrumentGroup *> (pItem);          }
338                  ++iter;          QListView::setSelected(pGroup, true);
339          }          return pGroup;
340          // Not found.  }
341          return NULL;  
342  }  
343    // Find a group item, given its name.
344    qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (
345  // Find a file item, given its name.          const QString& sName ) const
346  qsamplerInstrumentItem *qsamplerInstrumentList::findItem (  {
347          qsamplerInstrument *pInstrument ) const          // Iterate all over the place to search for the group.
348  {          QListViewItemIterator iter((QListView *) this);
349          if (pInstrument == NULL)          while (iter.current()) {
350                  return NULL;                  QListViewItem *pItem = iter.current();
351                    if (pItem->rtti() == Group && pItem->text(0) == sName)
352          // Iterate all over the place to search for the group.                          return static_cast<qsamplerInstrumentGroup *> (pItem);
353          QListViewItemIterator iter((QListView *) this);                  ++iter;
354          while (iter.current()) {          }
355                  QListViewItem *pListItem = iter.current();          // Not found.
356                  if (pListItem->rtti() == Item) {          return NULL;
357                          qsamplerInstrumentItem *pItem  }
358                                  = static_cast<qsamplerInstrumentItem *> (pListItem);  
359                          if (pItem && pItem->instrument()  
360                                  && pItem->instrument()->bank() == pInstrument->bank()  // Find a file item, given its name.
361                                  && pItem->instrument()->program() == pInstrument->program())  qsamplerInstrumentItem *qsamplerInstrumentList::findItem (
362                                  return pItem;          qsamplerInstrument *pInstrument ) const
363                  }  {
364                  ++iter;          if (pInstrument == NULL)
365          }                  return NULL;
366          // Not found.  
367          return NULL;          // Iterate all over the place to search for the group.
368  }          QListViewItemIterator iter((QListView *) this);
369            while (iter.current()) {
370                    QListViewItem *pListItem = iter.current();
371  // Find and return the nearest group item...                  if (pListItem->rtti() == Item) {
372  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (                          qsamplerInstrumentItem *pItem
373          QListViewItem *pItem ) const                                  = static_cast<qsamplerInstrumentItem *> (pListItem);
374  {                          if (pItem && pItem->instrument()
375          while (pItem && pItem->rtti() != Group)                                  && pItem->instrument()->map()  == pInstrument->map()
376                  pItem = pItem->parent();                                  && pItem->instrument()->bank() == pInstrument->bank()
377          return static_cast<qsamplerInstrumentGroup *> (pItem);                                  && pItem->instrument()->prog() == pInstrument->prog())
378  }                                  return pItem;
379                    }
380                    ++iter;
381  // Add a new group item below the current one.          }
382  void qsamplerInstrumentList::newGroupSlot (void)          // Not found.
383  {          return NULL;
384          qsamplerInstrumentGroup *pParentGroup  }
385                  = groupItem(QListView::selectedItem());  
386          qsamplerInstrumentGroup *pNewGroup  
387                  = addGroup(tr("New Group"), pParentGroup);  // Find and return the nearest group item...
388          if (pParentGroup)  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (
389                  pParentGroup->setOpen(true);          QListViewItem *pItem ) const
390          if (pNewGroup)  {
391                  pNewGroup->startRename(0);          while (pItem && pItem->rtti() != Group)
392                    pItem = pItem->parent();
393          selectionChangedSlot();          return static_cast<qsamplerInstrumentGroup *> (pItem);
394  }  }
395    
396    
397  // Add a new instrument item below the current one.  // Add a new group item below the current one.
398  void qsamplerInstrumentList::newItemSlot (void)  void qsamplerInstrumentList::newGroupSlot (void)
399  {  {
400          qsamplerInstrument *pInstrument = new qsamplerInstrument();          qsamplerInstrumentGroup *pParentGroup
401                    = groupItem(QListView::selectedItem());
402          qsamplerInstrumentForm form(this);          qsamplerInstrumentGroup *pNewGroup
403          form.setup(pInstrument);                  = addGroup(tr("New Group"), pParentGroup);
404          if (!form.exec()) {          if (pParentGroup)
405                  delete pInstrument;                  pParentGroup->setOpen(true);
406                  return;          if (pNewGroup)
407          }                  pNewGroup->startRename(0);
408    
409          // Check it there's already one for the same key (bank, program)          selectionChangedSlot();
410          qsamplerInstrumentItem *pItem = findItem(pInstrument);  }
411          if (pItem)  
412                  delete pItem;  
413    // Add a new instrument item below the current one.
414          pInstrument->map();  void qsamplerInstrumentList::newItemSlot (void)
415          emit instrumentsChanged();  {
416            qsamplerInstrument *pInstrument = new qsamplerInstrument();
417          qsamplerInstrumentGroup *pParentGroup  
418                  = groupItem(QListView::selectedItem());          qsamplerInstrumentForm form(this);
419          addItem(pInstrument, pParentGroup);          form.setup(pInstrument);
420          if (pParentGroup)          if (!form.exec()) {
421                  pParentGroup->setOpen(true);                  delete pInstrument;
422                    return;
423          selectionChangedSlot();          }
424  }  
425            // Check it there's already one instrument item
426            // with the very same key (bank, program);
427  // Edit current item below the current one.          // if yes, just remove it without prejudice...
428  void qsamplerInstrumentList::editItemSlot (void)          qsamplerInstrumentItem *pItem = findItem(pInstrument);
429  {          if (pItem)
430          QListViewItem *pListItem = QListView::selectedItem();                  delete pItem;
431          if (pListItem == NULL)  
432                  return;          pInstrument->mapInstrument();
433          if (pListItem->rtti() == Item) {          emit instrumentsChanged();
434                  qsamplerInstrumentItem *pItem  
435                          = static_cast<qsamplerInstrumentItem *> (pListItem);          qsamplerInstrumentGroup *pParentGroup
436                  if (pItem && pItem->instrument()) {                  = groupItem(QListView::selectedItem());
437                          qsamplerInstrumentForm form(this);          addItem(pInstrument, pParentGroup);
438                          form.setup(pItem->instrument());          if (pParentGroup)
439                          if (form.exec()) {                  pParentGroup->setOpen(true);
440                                  pItem->instrument()->map();  
441                                  emit instrumentsChanged();          selectionChangedSlot();
442                                  pItem->update();  }
443                          }  
444                  }  
445          }  // Edit current item below the current one.
446    void qsamplerInstrumentList::editItemSlot (void)
447          selectionChangedSlot();  {
448  }          QListViewItem *pListItem = QListView::selectedItem();
449            if (pListItem == NULL)
450                    return;
451  // Rename current group/item.          if (pListItem->rtti() == Item) {
452  void qsamplerInstrumentList::renameSlot (void)                  qsamplerInstrumentItem *pItem
453  {                          = static_cast<qsamplerInstrumentItem *> (pListItem);
454          QListViewItem *pListItem = QListView::selectedItem();                  if (pItem && pItem->instrument()) {
455          if (pListItem)                          qsamplerInstrumentForm form(this);
456                  pListItem->startRename(0);                          form.setup(pItem->instrument());
457                            if (form.exec()) {
458          selectionChangedSlot();                                  pItem->instrument()->mapInstrument();
459  }                                  emit instrumentsChanged();
460                                    pItem->update();
461                            }
462  // Remove current group/item.                  }
463  void qsamplerInstrumentList::deleteSlot (void)          }
464  {  
465          QListViewItem *pListItem = QListView::selectedItem();          selectionChangedSlot();
466          if (pListItem) {  }
467                  if (pListItem->rtti() == Item) {  
468                          qsamplerInstrumentItem *pItem  
469                                  = static_cast<qsamplerInstrumentItem *> (pListItem);  // Rename current group/item.
470                          if (pItem && pItem->instrument()) {  void qsamplerInstrumentList::renameSlot (void)
471                                  pItem->instrument()->unmap();  {
472                                  emit instrumentsChanged();          QListViewItem *pListItem = QListView::selectedItem();
473                          }          if (pListItem)
474                  }                  pListItem->startRename(0);
475                  delete pListItem;  
476          }          selectionChangedSlot();
477    }
478          selectionChangedSlot();  
479  }  
480    // Remove current group/item.
481    void qsamplerInstrumentList::deleteSlot (void)
482  // In-place selection slot.  {
483  void qsamplerInstrumentList::selectionChangedSlot (void)          QListViewItem *pListItem = QListView::selectedItem();
484  {          if (pListItem == NULL)
485          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();                  return;
486          QListViewItem *pListItem = QListView::selectedItem();  
487          bool bEnabled = (pMainForm && pMainForm->client());          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
488          m_pNewItemAction->setEnabled(bEnabled);          if (pMainForm == NULL)
489          bEnabled = (bEnabled && pListItem != NULL);                  return;
490          m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);  
491          m_pRenameAction->setEnabled(bEnabled);          // Prompt user if this is for real...
492          m_pDeleteAction->setEnabled(bEnabled);          qsamplerOptions *pOptions = pMainForm->options();
493  }          if (pOptions && pOptions->bConfirmRemove) {
494                    if (QMessageBox::warning(this,
495                            QSAMPLER_TITLE ": " + tr("Warning"),
496  // In-place activation slot.                          tr("Delete %1:\n\n"
497  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )                          "%2\n\n"
498  {                          "Are you sure?")
499          // FIXME: Hope the list view item is the one selected.                          .arg(pListItem->rtti() == Item ? tr("instrument") : tr("group"))
500          if (pListItem->rtti() == Item)                          .arg(pListItem->text(0)),
501                  editItemSlot();                          tr("OK"), tr("Cancel")) > 0)
502  }                          return;
503            }
504    
505  // In-place aliasing slot.          // Unmap instrument entry...
506  void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )          if (pListItem->rtti() == Item) {
507  {                  qsamplerInstrumentItem *pItem
508          if (pListItem->rtti() == Item) {                          = static_cast<qsamplerInstrumentItem *> (pListItem);
509                  qsamplerInstrumentItem *pItem                  if (pItem && pItem->instrument()) {
510                          = static_cast<qsamplerInstrumentItem *> (pListItem);                          pItem->instrument()->unmapInstrument();
511                  if (pItem && pItem->instrument()) {                          emit instrumentsChanged();
512                          pItem->instrument()->setName(pListItem->text(0));                  }
513                          pItem->instrument()->map();          }
514                          emit instrumentsChanged();  
515                          pItem->update();          // Do it for real...
516                  }          delete pListItem;
517          }  
518  }          selectionChangedSlot();
519    }
520    
521  // Context menu request event handler.  
522  void qsamplerInstrumentList::contextMenuEvent (  // In-place selection slot.
523          QContextMenuEvent *pContextMenuEvent )  void qsamplerInstrumentList::selectionChangedSlot (void)
524  {  {
525          QPopupMenu menu(this);          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
526            QListViewItem *pListItem = QListView::selectedItem();
527          // Construct context menu.          bool bEnabled = (pMainForm && pMainForm->client());
528          m_pNewItemAction->addTo(&menu);          m_pNewItemAction->setEnabled(bEnabled);
529  //      m_pNewGroupAction->addTo(&menu);          bEnabled = (bEnabled && pListItem != NULL);
530          menu.insertSeparator();          m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);
531          m_pEditItemAction->addTo(&menu);          m_pRenameAction->setEnabled(bEnabled);
532          m_pRenameAction->addTo(&menu);          m_pDeleteAction->setEnabled(bEnabled);
533          m_pDeleteAction->addTo(&menu);  }
534          menu.insertSeparator();  
535          m_pRefreshAction->addTo(&menu);  
536    // In-place activation slot.
537          menu.exec(pContextMenuEvent->globalPos());  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )
538  }  {
539            // FIXME: Hope the list view item is the one selected.
540            if (pListItem->rtti() == Item)
541  // General reloader.                  editItemSlot();
542  void qsamplerInstrumentList::refresh (void)  }
543  {  
544          clear();  
545    // In-place aliasing slot.
546          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )
547          if (pMainForm == NULL)  {
548                  return;          if (pListItem->rtti() == Item) {
549          if (pMainForm->client() == NULL)                  qsamplerInstrumentItem *pItem
550                  return;                          = static_cast<qsamplerInstrumentItem *> (pListItem);
551                    if (pItem && pItem->instrument()) {
552          qsamplerInstrumentItem *pItem = NULL;                          pItem->instrument()->setName(pListItem->text(0));
553          lscp_midi_instrument_t *pInstrs                          pItem->instrument()->mapInstrument();
554                  = ::lscp_list_midi_instruments(pMainForm->client());                          emit instrumentsChanged();
555          for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {                          pItem->update();
556                  int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;                  }
557                  int iProgram = pInstrs[iInstr].program;          }
558                  qsamplerInstrument *pInstrument  }
559                          = new qsamplerInstrument(iBank, iProgram);  
560                  if (pInstrument->get())  
561                          pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);  // Context menu request event handler.
562          }  void qsamplerInstrumentList::contextMenuEvent (
563            QContextMenuEvent *pContextMenuEvent )
564          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {  {
565                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");          if (!m_pNewItemAction->isEnabled())
566                  pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));                  return;
567          }  
568            QPopupMenu menu(this);
569          selectionChangedSlot();  
570  }          // Construct context menu.
571            m_pNewItemAction->addTo(&menu);
572    //      m_pNewGroupAction->addTo(&menu);
573  // end of qsamplerInstrumentList.cpp          menu.insertSeparator();
574            m_pEditItemAction->addTo(&menu);
575            m_pRenameAction->addTo(&menu);
576            m_pDeleteAction->addTo(&menu);
577            menu.insertSeparator();
578            m_pRefreshAction->addTo(&menu);
579    
580            menu.exec(pContextMenuEvent->globalPos());
581    }
582    
583    
584    // General reloader.
585    void qsamplerInstrumentList::refresh (void)
586    {
587            clear();
588    
589            qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
590            if (pMainForm == NULL)
591                    return;
592            if (pMainForm->client() == NULL)
593                    return;
594    
595            qsamplerInstrumentItem *pItem = NULL;
596            lscp_midi_instrument_t *pInstrs
597                    = ::lscp_list_midi_instruments(pMainForm->client(), LSCP_MIDI_MAP_ALL);
598            for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
599                    int iMap  = pInstrs[iInstr].map;
600                    int iBank = pInstrs[iInstr].bank;
601                    int iProg = pInstrs[iInstr].prog;
602                    qsamplerInstrument *pInstrument
603                            = new qsamplerInstrument(iMap, iBank, iProg);
604                    if (pInstrument->getInstrument())
605                            pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);
606            }
607    
608            if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
609                    pMainForm->appendMessagesClient("lscp_list_midi_instruments");
610                    pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
611            }
612    
613            selectionChangedSlot();
614    }
615    
616    
617    // end of qsamplerInstrumentList.cpp
618    

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

  ViewVC Help
Powered by ViewVC