/[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 972 by capela, Sun Dec 10 17:07:02 2006 UTC revision 1523 by capela, Sun Nov 25 11:40:47 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       Copyright (C) 2007, Christian Schoenebeck
6    
7     This program is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License     modify it under the terms of the GNU General Public License
# Line 25  Line 26 
26  #include "qsamplerInstrument.h"  #include "qsamplerInstrument.h"
27  #include "qsamplerInstrumentForm.h"  #include "qsamplerInstrumentForm.h"
28    
29    #include "qsamplerOptions.h"
30  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
31    
32  #include <qaction.h>  #include <QApplication>
33  #include <qfileinfo.h>  #include <QMessageBox>
34  #include <qpopupmenu.h>  #include <QMenu>
35    #include <QAction>
36    #include <QCursor>
37    #include <QFileInfo>
38    
39  // Needed for lroundf()  // Needed for lroundf()
40  #include <math.h>  #include <math.h>
41    
42  #ifndef CONFIG_ROUND  #ifndef CONFIG_ROUND
43  static long lroundf ( float fval )  static inline long lroundf ( float x )
44  {  {
45          double fint = 0.0;          if (x >= 0.0f)
46      float  frac = float(::modf(fval, &fint));                  return long(x + 0.5f);
47      long   lint = long(fint);          else
48      if (frac >= +0.5f)                  return long(x - 0.5f);
         lint++;  
     else  
     if (frac <= -0.5f)  
         lint--;  
     return lint;  
49  }  }
50  #endif  #endif
51    
52    using namespace QSampler;
 //----------------------------------------------------------------------  
 // class qsamplerInstrumentGroup -- custom group list view item.  
 //  
   
 // Constructors.  
 qsamplerInstrumentGroup::qsamplerInstrumentGroup (  
         qsamplerInstrumentList *pListView, const QString& sName,  
         QListViewItem *pItemAfter )  
         : QListViewItem(pListView, pItemAfter ? pItemAfter : pListView->lastItem())  
 {  
         QListViewItem::setRenameEnabled(0, true);  
   
         QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  
         QListViewItem::setText(0, sName);  
 }  
   
   
 qsamplerInstrumentGroup::qsamplerInstrumentGroup (  
         qsamplerInstrumentGroup *pGroupItem, const QString& sName )  
         : QListViewItem(pGroupItem, sName)  
 {  
         QListViewItem::setRenameEnabled(0, true);  
   
         QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemGroup.png"));  
 }  
   
   
 // Default destructor.  
 qsamplerInstrumentGroup::~qsamplerInstrumentGroup (void)  
 {  
 }  
53    
54    
55  // Instance accessors.  //-------------------------------------------------------------------------
56  void qsamplerInstrumentGroup::setName ( const QString& sName )  // MidiInstrumentsModel - data model for MIDI prog mappings (used for QTableView)
57  {  //
         QListViewItem::setText(0, sName);  
 }  
   
   
 QString qsamplerInstrumentGroup::name (void) const  
 {  
         return QListViewItem::text(0);  
 }  
   
   
 qsamplerInstrumentGroup *qsamplerInstrumentGroup::groupItem (void) const  
 {  
         QListViewItem *pParent = QListViewItem::parent();  
         while (pParent && pParent->rtti() != qsamplerInstrumentList::Group)  
                 pParent = pParent->parent();  
         return static_cast<qsamplerInstrumentGroup *> (pParent);  
 }  
   
58    
59  qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const  MidiInstrumentsModel::MidiInstrumentsModel ( QObject* pParent)
60            : QAbstractTableModel(pParent)
61  {  {
62          return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());          m_iMidiMap = LSCP_MIDI_MAP_ALL;
63  }  }
64    
65    
66  // To show up whether its open or not.  int MidiInstrumentsModel::rowCount ( const QModelIndex& /*parent*/) const
 void qsamplerInstrumentGroup::setOpen ( bool bOpen )  
67  {  {
68          // Set the proper pixmap of this...          if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
69          if (rtti() == qsamplerInstrumentList::Group) {                  int n = 0;
70                  QListViewItem::setPixmap(0, QPixmap::fromMimeSource(                  for (InstrumentsMap::const_iterator itMap = m_instruments.begin();
71                          bOpen ? "itemGroupOpen.png" : "itemGroup.png"));                                  itMap != m_instruments.end(); ++itMap)
72                            n += (*itMap).size();
73                    return n;
74          }          }
75          // Open it up...          InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
76          QListViewItem::setOpen(bOpen);          if (itMap == m_instruments.end()) return 0;
77            return (*itMap).size();
         // All ancestors should be also visible.  
         if (bOpen && QListViewItem::parent())  
                 QListViewItem::parent()->setOpen(true);  
78  }  }
79    
80    
81  // To virtually distinguish between list view items.  int MidiInstrumentsModel::columnCount ( const QModelIndex& /*parent*/) const
 int qsamplerInstrumentGroup::rtti (void) const  
82  {  {
83          return qsamplerInstrumentList::Group;          return 9;
84  }  }
85    
86    
87  //----------------------------------------------------------------------  QVariant MidiInstrumentsModel::data ( const QModelIndex &index, int role ) const
 // class qsamplerInstrumentItem -- custom file list view item.  
 //  
   
 // Constructors.  
 qsamplerInstrumentItem::qsamplerInstrumentItem (  
         qsamplerInstrumentList *pListView,  
         qsamplerInstrument *pInstrument,  
         QListViewItem *pItemAfter )  
         : qsamplerInstrumentGroup(pListView, pInstrument->name(), pItemAfter)  
 {  
         m_pInstrument = pInstrument;  
   
         update();  
 }  
   
 qsamplerInstrumentItem::qsamplerInstrumentItem (  
         qsamplerInstrumentGroup *pGroupItem,  
         qsamplerInstrument *pInstrument )  
         : qsamplerInstrumentGroup(pGroupItem, pInstrument->name())  
88  {  {
89          m_pInstrument = pInstrument;          if (!index.isValid())
90                    return QVariant();
91    
92          update();          const qsamplerInstrument* pInstr = NULL;
 }  
93    
94            if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
95                    int n = 0;
96                    for (InstrumentsMap::const_iterator itMap = m_instruments.begin();
97                                    itMap != m_instruments.end(); ++itMap) {
98                            n += (*itMap).size();
99                            if (index.row() < n)
100                                    pInstr = &(*itMap)[index.row() + (*itMap).size() - n];
101                    }
102            } else {
103                    // resolve MIDI instrument map
104                    InstrumentsMap::const_iterator itMap = m_instruments.find(m_iMidiMap);
105                    if (itMap == m_instruments.end()) return QVariant();
106                    // resolve instrument in that map
107                    if (index.row() >= (*itMap).size()) return QVariant();
108                    pInstr = &(*itMap)[index.row()];
109            }
110    
111            if (!pInstr)
112                    return QVariant();
113    
114            if (role == Qt::UserRole)
115                    return QVariant::fromValue((void *) pInstr);
116    
117            if (role == Qt::DisplayRole) {
118                    switch (index.column()) {
119                            case 0: return pInstr->name();
120                            case 1: return QVariant::fromValue(pInstr->map());
121                            case 2: return QVariant::fromValue(pInstr->bank());
122                            case 3: return QVariant::fromValue(pInstr->prog());
123                            case 4: return pInstr->engineName();
124                            case 5: return pInstr->instrumentFile();
125                            case 6: return QVariant::fromValue(pInstr->instrumentNr());
126                            case 7: return QString::number(pInstr->volume() * 100.0) + " %";
127                            case 8: {
128                                    switch (pInstr->loadMode()) {
129                                            case 3: return QObject::tr("Persistent");
130                                            case 2: return QObject::tr("On Demand Hold");
131                                            case 1: return QObject::tr("On Demand");
132                                            default: return QVariant();
133                                    }
134                            }
135                            default: return QVariant();
136                    }
137            }
138    
139  // Default destructor.          return QVariant();
 qsamplerInstrumentItem::~qsamplerInstrumentItem (void)  
 {  
         if (m_pInstrument)  
                 delete m_pInstrument;  
140  }  }
141    
142    
143  // To virtually distinguish between list view items.  QVariant MidiInstrumentsModel::headerData (
144  int qsamplerInstrumentItem::rtti (void) const          int section, Qt::Orientation orientation, int role ) const
145  {  {
146          return qsamplerInstrumentList::Item;          if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
147  }                  return QVariant();
   
148    
149  // Payload accessor.          switch (section) {
150  qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const                  case 0: return tr("Name");
151  {                  case 1: return tr("Map");
152          return m_pInstrument;                  case 2: return tr("Bank");
153                    case 3: return tr("Prog");
154                    case 4: return tr("Engine");
155                    case 5: return tr("File");
156                    case 6: return tr("Nr");
157                    case 7: return tr("Vol");
158                    case 8: return tr("Mode");
159                    default: return QVariant();
160            }
161  }  }
162    
163    
164  // Item refreshment.  qsamplerInstrument* MidiInstrumentsModel::addInstrument (
165  void qsamplerInstrumentItem::update (void)          int iMap, int iBank, int iProg )
166  {  {
167          QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));          // Check it there's already one instrument item
168            // with the very same key (bank, program);
169          const QString s = "-";          // if yes, just remove it without prejudice...
170          if (m_pInstrument) {          for (int i = 0; i < m_instruments[iMap].size(); i++) {
171                  setText(0, m_pInstrument->name());                  if (m_instruments[iMap][i].bank() == iBank &&
172                  setText(1, QString::number(m_pInstrument->bank()));                          m_instruments[iMap][i].prog() == iProg) {
173                  setText(2, QString::number(m_pInstrument->program() + 1));                          m_instruments[iMap].removeAt(i);
                 setText(3, m_pInstrument->engineName());  
                 setText(4, QFileInfo(m_pInstrument->instrumentFile()).fileName());  
                 setText(5, QString::number(m_pInstrument->instrumentNr()));  
                 setText(6, QString::number(::lroundf(100.0f * m_pInstrument->volume())));  
                 QString sLoadMode = s;  
                 switch (m_pInstrument->loadMode()) {  
                 case 3:  
                         sLoadMode = QObject::tr("Persistent");  
                         break;  
                 case 2:  
                         sLoadMode = QObject::tr("On Demand Hold");  
                         break;  
                 case 1:  
                         sLoadMode = QObject::tr("On Demand");  
174                          break;                          break;
175                  }                  }
                 setText(7, sLoadMode);  
         } else {  
                 for (int i = 0; i < listView()->columns(); i++)  
                         setText(i, s);  
         }  
 }  
   
   
 //----------------------------------------------------------------------------  
 // qsamplerInstrumentList -- MIDI instrument list view.  
 //  
   
 // Constructor.  
 qsamplerInstrumentList::qsamplerInstrumentList (  
         QWidget *pParent, const char *pszName )  
         : QListView(pParent, pszName)  
 {  
 //  QListView::setRootIsDecorated(true);  
         QListView::setResizeMode(QListView::NoColumn);  
 //      QListView::setAcceptDrops(true);  
         QListView::setDragAutoScroll(true);  
         QListView::setSizePolicy(  
                 QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));  
 //      QListView::setShowToolTips(false);  
         QListView::setSortColumn(-1);  
   
         QListView::addColumn(tr("Name"));  
         QListView::addColumn(tr("Bank"));  
         QListView::addColumn(tr("Prog"));  
         QListView::addColumn(tr("Engine"));  
         QListView::addColumn(tr("File"));  
         QListView::addColumn(tr("Nr"));  
         QListView::addColumn(tr("Vol"));  
         QListView::addColumn(tr("Mode"));  
   
         QListView::setColumnAlignment(1, Qt::AlignHCenter);     // Bank  
         QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Prog  
         QListView::setColumnAlignment(5, Qt::AlignHCenter);     // Nr  
         QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Vol  
   
         QListView::setColumnWidth(0, 60);       // Name  
         QListView::setColumnWidth(0, 120);      // File  
   
         m_pNewGroupAction = new QAction(tr("New &Group"), tr("Ctrl+G"), this);  
         m_pNewItemAction  = new QAction(tr("New &Instrument..."), tr("Ctrl+I"), this);  
         m_pEditItemAction = new QAction(tr("&Edit..."), tr("Ctrl+E"), this);  
         m_pRenameAction   = new QAction(tr("&Rename"), tr("Ctrl+R"), this);  
         m_pDeleteAction   = new QAction(tr("&Delete"), tr("Ctrl+D"), this);  
         m_pRefreshAction  = new QAction(tr("Re&fresh"), tr("Ctrl+F"), this);  
   
         QObject::connect(m_pNewGroupAction,  
                 SIGNAL(activated()),  
                 SLOT(newGroupSlot()));  
         QObject::connect(m_pNewItemAction,  
                 SIGNAL(activated()),  
                 SLOT(newItemSlot()));  
         QObject::connect(m_pEditItemAction,  
                 SIGNAL(activated()),  
                 SLOT(editItemSlot()));  
         QObject::connect(m_pRenameAction,  
                 SIGNAL(activated()),  
                 SLOT(renameSlot()));  
         QObject::connect(m_pDeleteAction,  
                 SIGNAL(activated()),  
                 SLOT(deleteSlot()));  
         QObject::connect(m_pRefreshAction,  
                 SIGNAL(activated()),  
                 SLOT(refresh()));  
   
         QObject::connect(this,  
                 SIGNAL(selectionChanged()),  
                 SLOT(selectionChangedSlot()));  
         QObject::connect(this,  
                 SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),  
                 SLOT(activatedSlot(QListViewItem*)));  
         QObject::connect(this,  
                 SIGNAL(returnPressed(QListViewItem*)),  
                 SLOT(activatedSlot(QListViewItem*)));  
         QObject::connect(this,  
                 SIGNAL(itemRenamed(QListViewItem*,int)),  
                 SLOT(renamedSlot(QListViewItem*)));  
   
         selectionChangedSlot();  
 }  
   
   
 // Default destructor.  
 qsamplerInstrumentList::~qsamplerInstrumentList (void)  
 {  
         delete m_pNewGroupAction;  
         delete m_pNewItemAction;  
         delete m_pEditItemAction;  
         delete m_pRenameAction;  
         delete m_pDeleteAction;  
 }  
   
   
 // Add a new instrument item, optionally under a given group.  
 qsamplerInstrumentItem *qsamplerInstrumentList::addItem (  
         qsamplerInstrument *pInstrument,  
         qsamplerInstrumentGroup *pParentGroup )  
 {  
         qsamplerInstrumentItem *pItem = findItem(pInstrument);  
         if (pItem == NULL) {  
                 if (pParentGroup)  
                         pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);  
                 else  
                         pItem = new qsamplerInstrumentItem(this, pInstrument);  
176          }          }
         QListView::setSelected(pItem, true);  
         return pItem;  
 }  
   
177    
178  // Add a new instrument group, optionally under another group.          // Resolve the appropriate place, we keep the list sorted that way ...
179  qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (          int i = 0;
180          const QString& sName, qsamplerInstrumentGroup *pParentGroup )          for (; i < m_instruments[iMap].size(); ++i) {
181  {                  if (iBank < m_instruments[iMap][i].bank()
182          qsamplerInstrumentGroup *pGroup = findGroup(sName);                          || (iBank == m_instruments[iMap][i].bank() &&
183          if (pGroup == NULL) {                                  iProg < m_instruments[iMap][i].prog())) {
184                  if (pParentGroup)                          break;
185                          pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);                  }
                 else  
                         pGroup = new qsamplerInstrumentGroup(this, sName);  
186          }          }
         QListView::setSelected(pGroup, true);  
         return pGroup;  
 }  
187    
188            m_instruments[iMap].insert(i, qsamplerInstrument(iMap, iBank, iProg));
189            qsamplerInstrument& instr = m_instruments[iMap][i];
190            if (!instr.getInstrument())
191                    m_instruments[iMap].removeAt(i);
192    
193  // Find a group item, given its name.          return &instr;
 qsamplerInstrumentGroup *qsamplerInstrumentList::findGroup (  
         const QString& sName ) const  
 {  
         // Iterate all over the place to search for the group.  
         QListViewItemIterator iter((QListView *) this);  
         while (iter.current()) {  
                 QListViewItem *pItem = iter.current();  
                 if (pItem->rtti() == Group && pItem->text(0) == sName)  
                         return static_cast<qsamplerInstrumentGroup *> (pItem);  
                 ++iter;  
         }  
         // Not found.  
         return NULL;  
194  }  }
195    
196    
197  // Find a file item, given its name.  void MidiInstrumentsModel::removeInstrument (
198  qsamplerInstrumentItem *qsamplerInstrumentList::findItem (          const qsamplerInstrument& instrument )
199          qsamplerInstrument *pInstrument ) const  {
200  {          const int iMap  = instrument.map();
201          if (pInstrument == NULL)          const int iBank = instrument.bank();
202                  return NULL;          const int iProg = instrument.prog();
203            for (int i = 0; i < m_instruments[iMap].size(); i++) {
204          // Iterate all over the place to search for the group.                  if (m_instruments[iMap][i].bank() == iBank &&
205          QListViewItemIterator iter((QListView *) this);                          m_instruments[iMap][i].prog() == iProg) {
206          while (iter.current()) {                          m_instruments[iMap].removeAt(i);
207                  QListViewItem *pListItem = iter.current();                          break;
                 if (pListItem->rtti() == Item) {  
                         qsamplerInstrumentItem *pItem  
                                 = static_cast<qsamplerInstrumentItem *> (pListItem);  
                         if (pItem && pItem->instrument()  
                                 && pItem->instrument()->bank() == pInstrument->bank()  
                                 && pItem->instrument()->program() == pInstrument->program())  
                                 return pItem;  
208                  }                  }
                 ++iter;  
209          }          }
         // Not found.  
         return NULL;  
210  }  }
211    
212    
213  // Find and return the nearest group item...  // Reposition the instrument in the model (called when map/bank/prg changed)
214  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (  void MidiInstrumentsModel::resort ( const qsamplerInstrument& instrument )
         QListViewItem *pItem ) const  
215  {  {
216          while (pItem && pItem->rtti() != Group)          const int iMap  = instrument.map();
217                  pItem = pItem->parent();          const int iBank = instrument.bank();
218          return static_cast<qsamplerInstrumentGroup *> (pItem);          const int iProg = instrument.prog();
219            // Remove given instrument from its current list
220            removeInstrument(instrument);
221            // Re-add the instrument
222            addInstrument(iMap, iBank, iProg);
223  }  }
224    
225    
226  // Add a new group item below the current one.  void MidiInstrumentsModel::setMidiMap ( int iMidiMap )
 void qsamplerInstrumentList::newGroupSlot (void)  
227  {  {
228          qsamplerInstrumentGroup *pParentGroup          if (iMidiMap < 0)
229                  = groupItem(QListView::selectedItem());                  iMidiMap = LSCP_MIDI_MAP_ALL;
         qsamplerInstrumentGroup *pNewGroup  
                 = addGroup(tr("New Group"), pParentGroup);  
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
         if (pNewGroup)  
                 pNewGroup->startRename(0);  
230    
231          selectionChangedSlot();          m_iMidiMap = iMidiMap;
232  }  }
233    
234    
235  // Add a new instrument item below the current one.  int MidiInstrumentsModel::midiMap (void) const
 void qsamplerInstrumentList::newItemSlot (void)  
236  {  {
237          qsamplerInstrument *pInstrument = new qsamplerInstrument();          return m_iMidiMap;
   
         qsamplerInstrumentForm form(this);  
         form.setup(pInstrument);  
         if (!form.exec()) {  
                 delete pInstrument;  
                 return;  
         }  
   
         // Check it there's already one instrument item  
         // with the very same key (bank, program);  
         // if yes, just remove it without prejudice...  
         qsamplerInstrumentItem *pItem = findItem(pInstrument);  
         if (pItem)  
                 delete pItem;  
   
         pInstrument->map();  
         emit instrumentsChanged();  
   
         qsamplerInstrumentGroup *pParentGroup  
                 = groupItem(QListView::selectedItem());  
         addItem(pInstrument, pParentGroup);  
         if (pParentGroup)  
                 pParentGroup->setOpen(true);  
   
         selectionChangedSlot();  
238  }  }
239    
240    void MidiInstrumentsModel::refresh (void)
 // Edit current item below the current one.  
 void qsamplerInstrumentList::editItemSlot (void)  
241  {  {
242          QListViewItem *pListItem = QListView::selectedItem();          m_instruments.clear();
         if (pListItem == NULL)  
                 return;  
         if (pListItem->rtti() == Item) {  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         qsamplerInstrumentForm form(this);  
                         form.setup(pItem->instrument());  
                         if (form.exec()) {  
                                 pItem->instrument()->map();  
                                 emit instrumentsChanged();  
                                 pItem->update();  
                         }  
                 }  
         }  
   
         selectionChangedSlot();  
 }  
243    
244            MainForm* pMainForm = MainForm::getInstance();
245            if (pMainForm == NULL)
246                    return;
247            if (pMainForm->client() == NULL)
248                    return;
249    
250  // Rename current group/item.          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 void qsamplerInstrumentList::renameSlot (void)  
 {  
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem)  
                 pListItem->startRename(0);  
251    
252          selectionChangedSlot();          // Load the whole bunch of instrument items...
253  }          lscp_midi_instrument_t* pInstrs
254                    = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
255            for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
256                    const int iMap  = pInstrs[iInstr].map;
257                    const int iBank = pInstrs[iInstr].bank;
258                    const int iProg = pInstrs[iInstr].prog;
259                    addInstrument(iMap, iBank, iProg);
260                    // Try to keep it snappy :)
261                    QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
262            }
263    
264            QApplication::restoreOverrideCursor();
265    
266  // Remove current group/item.          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
267  void qsamplerInstrumentList::deleteSlot (void)                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
268  {                  pMainForm->appendMessagesError(
269          QListViewItem *pListItem = QListView::selectedItem();                          tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
         if (pListItem) {  
                 if (pListItem->rtti() == Item) {  
                         qsamplerInstrumentItem *pItem  
                                 = static_cast<qsamplerInstrumentItem *> (pListItem);  
                         if (pItem && pItem->instrument()) {  
                                 pItem->instrument()->unmap();  
                                 emit instrumentsChanged();  
                         }  
                 }  
                 delete pListItem;  
270          }          }
271    
272          selectionChangedSlot();          // inform the outer world (QTableView) that our data changed
273            QAbstractTableModel::reset();
274  }  }
275    
276    
277  // In-place selection slot.  //-------------------------------------------------------------------------
278  void qsamplerInstrumentList::selectionChangedSlot (void)  // MidiInstrumentsDelegate - table cell renderer for MIDI prog mappings
279    // (doesn't actually do anything ATM, but is already there for a future
280    // cell editor widget implementation)
281    
282    MidiInstrumentsDelegate::MidiInstrumentsDelegate ( QObject* pParent )
283            : QItemDelegate(pParent)
284  {  {
         qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  
         QListViewItem *pListItem = QListView::selectedItem();  
         bool bEnabled = (pMainForm && pMainForm->client());  
         m_pNewItemAction->setEnabled(bEnabled);  
         bEnabled = (bEnabled && pListItem != NULL);  
         m_pEditItemAction->setEnabled(bEnabled && pListItem->rtti() == Item);  
         m_pRenameAction->setEnabled(bEnabled);  
         m_pDeleteAction->setEnabled(bEnabled);  
285  }  }
286    
287    
288  // In-place activation slot.  QWidget* MidiInstrumentsDelegate::createEditor ( QWidget* pParent,
289  void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )          const QStyleOptionViewItem& option, const QModelIndex& index ) const
290  {  {
291          // FIXME: Hope the list view item is the one selected.          return QItemDelegate::createEditor(pParent, option, index);
292          if (pListItem->rtti() == Item)  //      return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);
                 editItemSlot();  
293  }  }
294    
295    
296  // In-place aliasing slot.  void MidiInstrumentsDelegate::setEditorData ( QWidget */*pEditor*/,
297  void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )          const QModelIndex& /*index*/) const
298  {  {
         if (pListItem->rtti() == Item) {  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         pItem->instrument()->setName(pListItem->text(0));  
                         pItem->instrument()->map();  
                         emit instrumentsChanged();  
                         pItem->update();  
                 }  
         }  
299  }  }
300    
301    
302  // Context menu request event handler.  void MidiInstrumentsDelegate::setModelData ( QWidget */*pEditor*/,
303  void qsamplerInstrumentList::contextMenuEvent (          QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const
         QContextMenuEvent *pContextMenuEvent )  
304  {  {
         if (!m_pNewItemAction->isEnabled())  
                 return;  
   
         QPopupMenu menu(this);  
   
         // Construct context menu.  
         m_pNewItemAction->addTo(&menu);  
 //      m_pNewGroupAction->addTo(&menu);  
         menu.insertSeparator();  
         m_pEditItemAction->addTo(&menu);  
         m_pRenameAction->addTo(&menu);  
         m_pDeleteAction->addTo(&menu);  
         menu.insertSeparator();  
         m_pRefreshAction->addTo(&menu);  
   
         menu.exec(pContextMenuEvent->globalPos());  
305  }  }
306    
307    
308  // General reloader.  void MidiInstrumentsDelegate::updateEditorGeometry ( QWidget *pEditor,
309  void qsamplerInstrumentList::refresh (void)          const QStyleOptionViewItem& option, const QModelIndex& index) const
310  {  {
311          clear();          QItemDelegate::updateEditorGeometry(pEditor, option, index);
312    //      if (pEditor) pEditor->setGeometry(option.rect);
         qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  
         if (pMainForm == NULL)  
                 return;  
         if (pMainForm->client() == NULL)  
                 return;  
   
         qsamplerInstrumentItem *pItem = NULL;  
         lscp_midi_instrument_t *pInstrs  
                 = ::lscp_list_midi_instruments(pMainForm->client());  
         for (int iInstr = 0; pInstrs && pInstrs[iInstr].program >= 0; ++iInstr) {  
                 int iBank = (pInstrs[iInstr].bank_msb << 7) | pInstrs[iInstr].bank_lsb;  
                 int iProgram = pInstrs[iInstr].program;  
                 qsamplerInstrument *pInstrument  
                         = new qsamplerInstrument(iBank, iProgram);  
                 if (pInstrument->get())  
                         pItem = new qsamplerInstrumentItem(this, pInstrument, pItem);  
         }  
   
         if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {  
                 pMainForm->appendMessagesClient("lscp_list_midi_instruments");  
                 pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));  
         }  
   
         selectionChangedSlot();  
313  }  }
314    
315    
316  // end of qsamplerInstrumentList.cpp  // end of qsamplerInstrumentList.cpp
   

Legend:
Removed from v.972  
changed lines
  Added in v.1523

  ViewVC Help
Powered by ViewVC