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

  ViewVC Help
Powered by ViewVC