/[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 1013 by capela, Mon Jan 8 16:52:48 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 <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"));          m_iMidiMap = LSCP_MIDI_MAP_ALL;
229          QListView::addColumn(tr("Nr"));  
230          QListView::addColumn(tr("Vol"));  //  QListView::setRootIsDecorated(true);
231          QListView::addColumn(tr("Mode"));          QListView::setAllColumnsShowFocus(true);
232            QListView::setResizeMode(QListView::NoColumn);
233          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Bank  //      QListView::setAcceptDrops(true);
234          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog          QListView::setDragAutoScroll(true);
235          QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr          QListView::setSizePolicy(
236          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol                  QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
237    //      QListView::setShowToolTips(false);
238          QListView::setColumnWidth(0, 60);       // Name          QListView::setSortColumn(-1);
239          QListView::setColumnWidth(0, 120);      // File  
240            QListView::addColumn(tr("Name"));
241          m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);          QListView::addColumn(tr("Map"));
242          m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);          QListView::addColumn(tr("Bank"));
243          m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);          QListView::addColumn(tr("Prog"));
244          m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);          QListView::addColumn(tr("Engine"));
245          m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);          QListView::addColumn(tr("File"));
246          m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), this);          QListView::addColumn(tr("Nr"));
247            QListView::addColumn(tr("Vol"));
248          QObject::connect(m_pNewGroupAction,          QListView::addColumn(tr("Mode"));
249                  SIGNAL(activated()),  
250                  SLOT(newGroupSlot()));          QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Map
251          QObject::connect(m_pNewItemAction,          QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Bank
252                  SIGNAL(activated()),          QListView::setColumnAlignment(3, Qt::AlignHCenter);     // Prog
253                  SLOT(newItemSlot()));          QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Nr
254          QObject::connect(m_pEditItemAction,          QListView::setColumnAlignment(7, Qt::AlignHCenter);     // Vol
255                  SIGNAL(activated()),  
256                  SLOT(editItemSlot()));          QListView::setColumnWidth(0, 120);      // Name
257          QObject::connect(m_pRenameAction,          QListView::setColumnWidth(5, 240);      // File
258                  SIGNAL(activated()),  
259                  SLOT(renameSlot()));          m_pNewGroupAction = new QAction(
260          QObject::connect(m_pDeleteAction,                  QIconSet(QPixmap::fromMimeSource("itemGroupNew.png")),
261                  SIGNAL(activated()),                  tr("New &Group"), tr("Ctrl+G"), this);
262                  SLOT(deleteSlot()));          m_pNewItemAction  = new QAction(
263          QObject::connect(m_pRefreshAction,                  QIconSet(QPixmap::fromMimeSource("itemNew.png")),
264                  SIGNAL(activated()),                  tr("New &Instrument..."), tr("Ins"), this);
265                  SLOT(refresh()));          m_pEditItemAction = new QAction(
266                    QIconSet(QPixmap::fromMimeSource("formEdit.png")),
267          QObject::connect(this,                  tr("&Edit..."), tr("Enter"), this);
268                  SIGNAL(selectionChanged()),          m_pRenameAction   = new QAction(tr("&Rename"), tr("F2"), this);
269                  SLOT(selectionChangedSlot()));          m_pDeleteAction   = new QAction(
270          QObject::connect(this,                  QIconSet(QPixmap::fromMimeSource("formRemove.png")),
271                  SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),                  tr("&Delete"), tr("Del"), this);
272                  SLOT(activatedSlot(QListViewItem*)));          m_pRefreshAction  = new QAction(
273          QObject::connect(this,                  QIconSet(QPixmap::fromMimeSource("formRefresh.png")),
274                  SIGNAL(returnPressed(QListViewItem*)),                  tr("Re&fresh"), tr("F5"), this);
275                  SLOT(activatedSlot(QListViewItem*)));  
276          QObject::connect(this,          m_pNewGroupAction->setToolTip(tr("New Group"));
277                  SIGNAL(itemRenamed(QListViewItem*,int)),          m_pNewItemAction->setToolTip(tr("New Instrument"));
278                  SLOT(renamedSlot(QListViewItem*)));          m_pEditItemAction->setToolTip(tr("Edit"));
279            m_pRenameAction->setToolTip(tr("Rename"));
280          selectionChangedSlot();          m_pDeleteAction->setToolTip(tr("Delete"));
281  }          m_pRefreshAction->setToolTip(tr("Refresh"));
282    
283            QObject::connect(m_pNewGroupAction,
284  // Default destructor.                  SIGNAL(activated()),
285  qsamplerInstrumentList::~qsamplerInstrumentList (void)                  SLOT(newGroupSlot()));
286  {          QObject::connect(m_pNewItemAction,
287          delete m_pNewGroupAction;                  SIGNAL(activated()),
288          delete m_pNewItemAction;                  SLOT(newItemSlot()));
289          delete m_pEditItemAction;          QObject::connect(m_pEditItemAction,
290          delete m_pRenameAction;                  SIGNAL(activated()),
291          delete m_pDeleteAction;                  SLOT(editItemSlot()));
292  }          QObject::connect(m_pRenameAction,
293                    SIGNAL(activated()),
294                    SLOT(renameSlot()));
295  // Add a new instrument item, optionally under a given group.          QObject::connect(m_pDeleteAction,
296  qsamplerInstrumentItem *qsamplerInstrumentList::addItem (                  SIGNAL(activated()),
297          qsamplerInstrument *pInstrument,                  SLOT(deleteSlot()));
298          qsamplerInstrumentGroup *pParentGroup )          QObject::connect(m_pRefreshAction,
299  {                  SIGNAL(activated()),
300          qsamplerInstrumentItem *pItem = findItem(pInstrument);                  SLOT(refresh()));
301          if (pItem == NULL) {  
302                  if (pParentGroup)          QObject::connect(this,
303                          pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);                  SIGNAL(selectionChanged()),
304                  else                  SLOT(selectionChangedSlot()));
305                          pItem = new qsamplerInstrumentItem(this, pInstrument);          QObject::connect(this,
306          }                  SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),
307          QListView::setSelected(pItem, true);                  SLOT(activatedSlot(QListViewItem*)));
308          return pItem;          QObject::connect(this,
309  }                  SIGNAL(returnPressed(QListViewItem*)),
310                    SLOT(activatedSlot(QListViewItem*)));
311            QObject::connect(this,
312  // Add a new instrument group, optionally under another group.                  SIGNAL(itemRenamed(QListViewItem*,int)),
313  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (                  SLOT(renamedSlot(QListViewItem*)));
314          const QString& sName, qsamplerInstrumentGroup *pParentGroup )  
315  {          selectionChangedSlot();
316          qsamplerInstrumentGroup *pGroup = findGroup(sName);  }
317          if (pGroup == NULL) {  
318                  if (pParentGroup)  
319                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);  // Default destructor.
320                  else  qsamplerInstrumentList::~qsamplerInstrumentList (void)
321                          pGroup = new qsamplerInstrumentGroup(this, sName);  {
322          }          delete m_pNewGroupAction;
323          QListView::setSelected(pGroup, true);          delete m_pNewItemAction;
324          return pGroup;          delete m_pEditItemAction;
325  }          delete m_pRenameAction;
326            delete m_pDeleteAction;
327    }
328  // Find a group item, given its name.  
329  qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (  
330          const QString& sName ) const  // Add a new instrument item, optionally under a given group.
331  {  qsamplerInstrumentItem *qsamplerInstrumentList::addItem (
332          // Iterate all over the place to search for the group.          qsamplerInstrument *pInstrument,
333          QListViewItemIterator iter((QListView *) this);          qsamplerInstrumentGroup *pParentGroup )
334          while (iter.current()) {  {
335                  QListViewItem *pItem = iter.current();          // Check it there's already one instrument item
336                  if (pItem->rtti() == Group && pItem->text(0) == sName)          // with the very same key (bank, program);
337                          return static_cast<qsamplerInstrumentGroup *> (pItem);          // if yes, just remove it without prejudice...
338                  ++iter;          qsamplerInstrumentItem *pItem = findItem(pInstrument);
339          }          if (pItem)
340          // Not found.                  delete pItem;
341          return NULL;  
342  }          // Add the new item under proper group one, if any...
343            if (pParentGroup) {
344                    pParentGroup->setOpen(true);
345  // Find a file item, given its name.                  pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);
346  qsamplerInstrumentItem *qsamplerInstrumentList::findItem (          } else {
347          qsamplerInstrument *pInstrument ) const                  pItem = new qsamplerInstrumentItem(this, pInstrument);
348  {          }
349          if (pInstrument == NULL)  
350                  return NULL;          // Set it as current selection...
351            QListView::setSelected(pItem, true);
352          // Iterate all over the place to search for the group.  
353          QListViewItemIterator iter((QListView *) this);          return pItem;
354          while (iter.current()) {  }
355                  QListViewItem *pListItem = iter.current();  
356                  if (pListItem->rtti() == Item) {  
357                          qsamplerInstrumentItem *pItem  // Add a new instrument group, optionally under another group.
358                                  = static_cast<qsamplerInstrumentItem *> (pListItem);  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (
359                          if (pItem && pItem->instrument()          const QString& sName, qsamplerInstrumentGroup *pParentGroup )
360                                  && pItem->instrument()->bank() == pInstrument->bank()  {
361                                  && pItem->instrument()->program() == pInstrument->program())          qsamplerInstrumentGroup *pGroup = findGroup(sName);
362                                  return pItem;          if (pGroup == NULL) {
363                  }                  if (pParentGroup) {
364                  ++iter;                          pParentGroup->setOpen(true);
365          }                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);
366          // Not found.                  } else {
367          return NULL;                          pGroup = new qsamplerInstrumentGroup(this, sName);
368  }                  }
369            }
370            QListView::setSelected(pGroup, true);
371  // Find and return the nearest group item...          return pGroup;
372  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (  }
373          QListViewItem *pItem ) const  
374  {  
375          while (pItem && pItem->rtti() != Group)  // Find a group item, given its name.
376                  pItem = pItem->parent();  qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (
377          return static_cast<qsamplerInstrumentGroup *> (pItem);          const QString& sName ) const
378  }  {
379            // Iterate all over the place to search for the group.
380            QListViewItemIterator iter((QListView *) this);
381  // Add a new group item below the current one.          while (iter.current()) {
382  void qsamplerInstrumentList::newGroupSlot (void)                  QListViewItem *pItem = iter.current();
383  {                  if (pItem->rtti() == Group && pItem->text(0) == sName)
384          qsamplerInstrumentGroup *pParentGroup                          return static_cast<qsamplerInstrumentGroup *> (pItem);
385                  = groupItem(QListView::selectedItem());                  ++iter;
386          qsamplerInstrumentGroup *pNewGroup          }
387                  = addGroup(tr("New Group"), pParentGroup);          // Not found.
388          if (pParentGroup)          return NULL;
389                  pParentGroup->setOpen(true);  }
390          if (pNewGroup)  
391                  pNewGroup->startRename(0);  
392    // Find a file item, given its name.
393          selectionChangedSlot();  qsamplerInstrumentItem *qsamplerInstrumentList::findItem (
394  }          qsamplerInstrument *pInstrument ) const
395    {
396            if (pInstrument == NULL)
397  // Add a new instrument item below the current one.                  return NULL;
398  void qsamplerInstrumentList::newItemSlot (void)  
399  {          // Iterate all over the place to search for the group.
400          qsamplerInstrument *pInstrument = new qsamplerInstrument();          QListViewItemIterator iter((QListView *) this);
401            while (iter.current()) {
402          qsamplerInstrumentForm form(this);                  QListViewItem *pListItem = iter.current();
403          form.setup(pInstrument);                  if (pListItem->rtti() == Item) {
404          if (!form.exec()) {                          qsamplerInstrumentItem *pItem
405                  delete pInstrument;                                  = static_cast<qsamplerInstrumentItem *> (pListItem);
406                  return;                          if (pItem && pItem->instrument()
407          }                                  && pItem->instrument()->map()  == pInstrument->map()
408                                    && pItem->instrument()->bank() == pInstrument->bank()
409          // Check it there's already one for the same key (bank, program)                                  && pItem->instrument()->prog() == pInstrument->prog())
410          qsamplerInstrumentItem *pItem = findItem(pInstrument);                                  return pItem;
411          if (pItem)                  }
412                  delete pItem;                  ++iter;
413            }
414          pInstrument->map();          // Not found.
415          emit instrumentsChanged();          return NULL;
416    }
417          qsamplerInstrumentGroup *pParentGroup  
418                  = groupItem(QListView::selectedItem());  
419          addItem(pInstrument, pParentGroup);  // Find and return the nearest group item...
420          if (pParentGroup)  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (
421                  pParentGroup->setOpen(true);          QListViewItem *pItem ) const
422    {
423          selectionChangedSlot();          while (pItem && pItem->rtti() != Group)
424  }                  pItem = pItem->parent();
425            return static_cast<qsamplerInstrumentGroup *> (pItem);
426    }
427  // Edit current item below the current one.  
428  void qsamplerInstrumentList::editItemSlot (void)  
429  {  // Add a new group item below the current one.
430          QListViewItem *pListItem = QListView::selectedItem();  void qsamplerInstrumentList::newGroupSlot (void)
431          if (pListItem == NULL)  {
432                  return;          qsamplerInstrumentGroup *pNewGroup
433          if (pListItem->rtti() == Item) {                  = addGroup(tr("New Group"), groupItem(QListView::selectedItem()));
434                  qsamplerInstrumentItem *pItem          if (pNewGroup)
435                          = static_cast<qsamplerInstrumentItem *> (pListItem);                  pNewGroup->startRename(0);
436                  if (pItem && pItem->instrument()) {  
437                          qsamplerInstrumentForm form(this);          selectionChangedSlot();
438                          form.setup(pItem->instrument());  }
439                          if (form.exec()) {  
440                                  pItem->instrument()->map();  
441                                  emit instrumentsChanged();  // Map selector.
442                                  pItem->update();  void qsamplerInstrumentList::setMidiMap ( int iMidiMap )
443                          }  {
444                  }          if (iMidiMap < 0)
445          }                  iMidiMap = LSCP_MIDI_MAP_ALL;
446    
447          selectionChangedSlot();          m_iMidiMap = iMidiMap;
448  }  }
449    
450    int qsamplerInstrumentList::midiMap (void) const
451  // Rename current group/item.  {
452  void qsamplerInstrumentList::renameSlot (void)          return m_iMidiMap;
453  {  }
454          QListViewItem *pListItem = QListView::selectedItem();  
455          if (pListItem)  
456                  pListItem->startRename(0);  // List actions accessors.
457    QAction *qsamplerInstrumentList::newGroupAction (void) const
458          selectionChangedSlot();  {
459  }          return m_pNewGroupAction;
460    }
461    
462  // Remove current group/item.  QAction *qsamplerInstrumentList::newItemAction (void) const
463  void qsamplerInstrumentList::deleteSlot (void)  {
464  {          return m_pNewItemAction;
465          QListViewItem *pListItem = QListView::selectedItem();  }
466          if (pListItem) {  
467                  if (pListItem->rtti() == Item) {  QAction *qsamplerInstrumentList::editItemAction (void) const
468                          qsamplerInstrumentItem *pItem  {
469                                  = static_cast<qsamplerInstrumentItem *> (pListItem);          return m_pEditItemAction;
470                          if (pItem && pItem->instrument()) {  }
471                                  pItem->instrument()->unmap();  
472                                  emit instrumentsChanged();  QAction *qsamplerInstrumentList::renameAction (void) const
473                          }  {
474                  }          return m_pRenameAction;
475                  delete pListItem;  }
476          }  
477    QAction *qsamplerInstrumentList::deleteAction (void) const
478          selectionChangedSlot();  {
479  }          return m_pDeleteAction;
480    }
481    
482  // In-place selection slot.  QAction *qsamplerInstrumentList::refreshAction (void) const
483  void qsamplerInstrumentList::selectionChangedSlot (void)  {
484  {          return m_pRefreshAction;
485          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  }
486          QListViewItem *pListItem = QListView::selectedItem();  
487          bool bEnabled = (pMainForm && pMainForm->client());  
488          m_pNewItemAction->setEnabled(bEnabled);  // Add a new instrument item below the current one.
489          bEnabled = (bEnabled && pListItem != NULL);  void qsamplerInstrumentList::newItemSlot (void)
490          m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);  {
491          m_pRenameAction->setEnabled(bEnabled);          qsamplerInstrument *pInstrument = new qsamplerInstrument();
492          m_pDeleteAction->setEnabled(bEnabled);  
493  }          qsamplerInstrumentForm form(this);
494            form.setup(pInstrument);
495            if (!form.exec()) {
496  // In-place activation slot.                  delete pInstrument;
497  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )                  return;
498  {          }
499          // FIXME: Hope the list view item is the one selected.  
500          if (pListItem->rtti() == Item)          // Commit...
501                  editItemSlot();          pInstrument->mapInstrument();
502  }          // add new item to the tree...
503            addItem(pInstrument, groupItem(QListView::selectedItem()));
504            // Notify we've changes...
505  // In-place aliasing slot.          emit instrumentsChanged();
506  void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )  
507  {          selectionChangedSlot();
508          if (pListItem->rtti() == Item) {  }
509                  qsamplerInstrumentItem *pItem  
510                          = static_cast<qsamplerInstrumentItem *> (pListItem);  
511                  if (pItem && pItem->instrument()) {  // Edit current item below the current one.
512                          pItem->instrument()->setName(pListItem->text(0));  void qsamplerInstrumentList::editItemSlot (void)
513                          pItem->instrument()->map();  {
514                          emit instrumentsChanged();          QListViewItem *pListItem = QListView::selectedItem();
515                          pItem->update();          if (pListItem == NULL)
516                  }                  return;
517          }          if (pListItem->rtti() == Item) {
518  }                  qsamplerInstrument *pInstrument = NULL;
519                    qsamplerInstrumentItem *pItem
520                            = static_cast<qsamplerInstrumentItem *> (pListItem);
521  // Context menu request event handler.                  if (pItem)
522  void qsamplerInstrumentList::contextMenuEvent (                          pInstrument = pItem->instrument();
523          QContextMenuEvent *pContextMenuEvent )                  if (pInstrument) {
524  {                          // Save current key values...
525          QPopupMenu menu(this);                          int iMap  = pInstrument->map();
526                            int iBank = pInstrument->bank();
527          // Construct context menu.                          int iProg = pInstrument->prog();
528          m_pNewItemAction->addTo(&menu);                          // Do the edit dance...
529  //      m_pNewGroupAction->addTo(&menu);                          qsamplerInstrumentForm form(this);
530          menu.insertSeparator();                          form.setup(pInstrument);
531          m_pEditItemAction->addTo(&menu);                          if (form.exec()) {
532          m_pRenameAction->addTo(&menu);                                  // Commit...
533          m_pDeleteAction->addTo(&menu);                                  pInstrument->mapInstrument();
534          menu.insertSeparator();                                  // Check whether we changed instrument key...
535          m_pRefreshAction->addTo(&menu);                                  if (iMap  == pInstrument->map()  &&
536                                            iBank == pInstrument->bank() &&
537          menu.exec(pContextMenuEvent->globalPos());                                          iProg == pInstrument->prog()) {
538  }                                          // just update tree item...
539                                            pItem->update();
540                                    } else {
541  // General reloader.                                          // Change item tree, whether applicable...
542  void qsamplerInstrumentList::refresh (void)                                          if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) {
543  {                                                  // Add new brand item into view...
544          clear();                                                  addItem(pInstrument, groupItem(pListItem));
545                                            } else {
546          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();                                                  // Just remove/hide old one.
547          if (pMainForm == NULL)                                                  delete pItem;
548                  return;                                          }
549          if (pMainForm->client() == NULL)                                  }
550                  return;                                  // Notify we've changes...
551                                    emit instrumentsChanged();
552          qsamplerInstrumentItem *pItem = NULL;                          }
553          lscp_midi_instrument_t *pInstrs                  }
554                  = ::lscp_list_midi_instruments(pMainForm->client());          }
555          for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {  
556                  int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;          selectionChangedSlot();
557                  int iProgram = pInstrs[iInstr].program;  }
558                  qsamplerInstrument *pInstrument  
559                          = new qsamplerInstrument(iBank, iProgram);  
560                  if (pInstrument->get())  // Rename current group/item.
561                          pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);  void qsamplerInstrumentList::renameSlot (void)
562          }  {
563            QListViewItem *pListItem = QListView::selectedItem();
564          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {          if (pListItem)
565                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");                  pListItem->startRename(0);
566                  pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));  
567          }          selectionChangedSlot();
568    }
569          selectionChangedSlot();  
570  }  
571    // Remove current group/item.
572    void qsamplerInstrumentList::deleteSlot (void)
573  // end of qsamplerInstrumentList.cpp  {
574            QListViewItem *pListItem = QListView::selectedItem();
575            if (pListItem == NULL)
576                    return;
577    
578            qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
579            if (pMainForm == NULL)
580                    return;
581    
582            // Prompt user if this is for real...
583            qsamplerOptions *pOptions = pMainForm->options();
584            if (pOptions && pOptions->bConfirmRemove) {
585                    if (QMessageBox::warning(this,
586                            QSAMPLER_TITLE ": " + tr("Warning"),
587                            tr("Delete %1:\n\n"
588                            "%2\n\n"
589                            "Are you sure?")
590                            .arg(pListItem->rtti() == Item ? tr("instrument") : tr("group"))
591                            .arg(pListItem->text(0)),
592                            tr("OK"), tr("Cancel")) > 0)
593                            return;
594            }
595    
596            // Unmap instrument entry...
597            if (pListItem->rtti() == Item) {
598                    qsamplerInstrumentItem *pItem
599                            = static_cast<qsamplerInstrumentItem *> (pListItem);
600                    if (pItem && pItem->instrument()) {
601                            pItem->instrument()->unmapInstrument();
602                            emit instrumentsChanged();
603                    }
604            }
605    
606            // Do it for real...
607            delete pListItem;
608    
609            selectionChangedSlot();
610    }
611    
612    
613    // In-place selection slot.
614    void qsamplerInstrumentList::selectionChangedSlot (void)
615    {
616            qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
617            QListViewItem *pListItem = QListView::selectedItem();
618            bool bEnabled = (pMainForm && pMainForm->client());
619            m_pNewItemAction->setEnabled(bEnabled);
620            bEnabled = (bEnabled && pListItem != NULL);
621            m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);
622            m_pRenameAction->setEnabled(bEnabled);
623            m_pDeleteAction->setEnabled(bEnabled);
624    }
625    
626    
627    // In-place activation slot.
628    void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )
629    {
630            // FIXME: Hope the list view item is the one selected.
631            if (pListItem->rtti() == Item)
632                    editItemSlot();
633    }
634    
635    
636    // In-place aliasing slot.
637    void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )
638    {
639            if (pListItem->rtti() == Item) {
640                    qsamplerInstrumentItem *pItem
641                            = static_cast<qsamplerInstrumentItem *> (pListItem);
642                    if (pItem && pItem->instrument()) {
643                            pItem->instrument()->setName(pListItem->text(0));
644                            pItem->instrument()->mapInstrument();
645                            emit instrumentsChanged();
646                            pItem->update();
647                    }
648            }
649    }
650    
651    
652    // Context menu request event handler.
653    void qsamplerInstrumentList::contextMenuEvent (
654            QContextMenuEvent *pContextMenuEvent )
655    {
656            if (!m_pNewItemAction->isEnabled())
657                    return;
658    
659            QPopupMenu menu(this);
660    
661            // Construct context menu.
662            m_pNewItemAction->addTo(&menu);
663    //      m_pNewGroupAction->addTo(&menu);
664            menu.insertSeparator();
665            m_pEditItemAction->addTo(&menu);
666            m_pRenameAction->addTo(&menu);
667            m_pDeleteAction->addTo(&menu);
668            menu.insertSeparator();
669            m_pRefreshAction->addTo(&menu);
670    
671            menu.exec(pContextMenuEvent->globalPos());
672    }
673    
674    
675    // General reloader.
676    void qsamplerInstrumentList::refresh (void)
677    {
678            clear();
679    
680            qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
681            if (pMainForm == NULL)
682                    return;
683            if (pMainForm->client() == NULL)
684                    return;
685    
686            qsamplerInstrumentItem *pItem = NULL;
687            lscp_midi_instrument_t *pInstrs
688                    = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
689            for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
690                    int iMap  = pInstrs[iInstr].map;
691                    int iBank = pInstrs[iInstr].bank;
692                    int iProg = pInstrs[iInstr].prog;
693                    qsamplerInstrument *pInstrument
694                            = new qsamplerInstrument(iMap, iBank, iProg);
695                    if (pInstrument->getInstrument())
696                            pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);
697            }
698    
699            if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
700                    pMainForm->appendMessagesClient("lscp_list_midi_instruments");
701                    pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
702            }
703    
704            selectionChangedSlot();
705    }
706    
707    
708    // end of qsamplerInstrumentList.cpp
709    

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

  ViewVC Help
Powered by ViewVC