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

Legend:
Removed from v.962  
changed lines
  Added in v.1015

  ViewVC Help
Powered by ViewVC