/[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 1013 by capela, Mon Jan 8 16:52:48 2007 UTC revision 1492 by schoenebeck, Mon Nov 19 21:08:18 2007 UTC
# Line 2  Line 2 
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2003-2007, 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 28  Line 29 
29  #include "qsamplerOptions.h"  #include "qsamplerOptions.h"
30  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
31    
32    #include <qapplication.h>
33  #include <qmessagebox.h>  #include <qmessagebox.h>
34    #include <qeventloop.h>
35  #include <qaction.h>  #include <qaction.h>
36    #include <qcursor.h>
37  #include <qfileinfo.h>  #include <qfileinfo.h>
38  #include <qpopupmenu.h>  
39    #include <QMenu>
40    
41  // Needed for lroundf()  // Needed for lroundf()
42  #include <math.h>  #include <math.h>
# Line 46  static inline long lroundf ( float x ) Line 51  static inline long lroundf ( float x )
51  }  }
52  #endif  #endif
53    
54    using namespace QSampler;
55    
 //----------------------------------------------------------------------  
 // 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)  
 {  
 }  
   
   
 // Instance accessors.  
 void qsamplerInstrumentGroup::setName ( const QString& sName )  
 {  
         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);  
 }  
   
   
 qsamplerInstrumentList *qsamplerInstrumentGroup::listView (void) const  
 {  
         return static_cast<qsamplerInstrumentList *> (QListViewItem::listView());  
 }  
   
   
 // To show up whether its open or not.  
 void qsamplerInstrumentGroup::setOpen ( bool bOpen )  
 {  
         // Set the proper pixmap of this...  
         if (rtti() == qsamplerInstrumentList::Group) {  
                 QListViewItem::setPixmap(0, QPixmap::fromMimeSource(  
                         bOpen ? "itemGroupOpen.png" : "itemGroup.png"));  
         }  
         // Open it up...  
         QListViewItem::setOpen(bOpen);  
   
         // All ancestors should be also visible.  
         if (bOpen && QListViewItem::parent())  
                 QListViewItem::parent()->setOpen(true);  
 }  
   
   
 // To virtually distinguish between list view items.  
 int qsamplerInstrumentGroup::rtti (void) const  
 {  
         return qsamplerInstrumentList::Group;  
 }  
   
   
 //----------------------------------------------------------------------  
 // 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())  
 {  
         m_pInstrument = pInstrument;  
   
         update();  
 }  
   
   
 // Default destructor.  
 qsamplerInstrumentItem::~qsamplerInstrumentItem (void)  
 {  
         if (m_pInstrument)  
                 delete m_pInstrument;  
 }  
56    
57    //-------------------------------------------------------------------------
58  // To virtually distinguish between list view items.  // MidiInstrumentsModel - data model for MIDI prog mappings (used for QTableView)
 int qsamplerInstrumentItem::rtti (void) const  
 {  
         return qsamplerInstrumentList::Item;  
 }  
   
   
 // Payload accessor.  
 qsamplerInstrument *qsamplerInstrumentItem::instrument (void) const  
 {  
         return m_pInstrument;  
 }  
   
   
 // Item refreshment.  
 void qsamplerInstrumentItem::update (void)  
 {  
         QListViewItem::setPixmap(0, QPixmap::fromMimeSource("itemFile.png"));  
   
         const QString s = "-";  
         if (m_pInstrument) {  
                 setText(0, m_pInstrument->name());  
                 setText(1, QString::number(m_pInstrument->map()));  
                 setText(2, QString::number(m_pInstrument->bank()));  
                 setText(3, QString::number(m_pInstrument->prog() + 1));  
                 setText(4, m_pInstrument->engineName());  
                 setText(5, QFileInfo(m_pInstrument->instrumentFile()).fileName());  
                 setText(6, QString::number(m_pInstrument->instrumentNr()));  
                 setText(7, 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");  
                         break;  
                 }  
                 setText(8, sLoadMode);  
         } else {  
                 for (int i = 0; i < listView()->columns(); i++)  
                         setText(i, s);  
         }  
 }  
   
   
 //----------------------------------------------------------------------------  
 // qsamplerInstrumentList -- MIDI instrument list view.  
59  //  //
60    
61  // Constructor.  MidiInstrumentsModel::MidiInstrumentsModel(QObject* parent) : QAbstractTableModel(parent) {
62  qsamplerInstrumentList::qsamplerInstrumentList (      m_iMidiMap = LSCP_MIDI_MAP_ALL;
         QWidget *pParent, const char *pszName )  
         : QListView(pParent, pszName)  
 {  
         m_iMidiMap = LSCP_MIDI_MAP_ALL;  
   
 //  QListView::setRootIsDecorated(true);  
         QListView::setAllColumnsShowFocus(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("Map"));  
         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);     // Map  
         QListView::setColumnAlignment(2, Qt::AlignHCenter);     // Bank  
         QListView::setColumnAlignment(3, Qt::AlignHCenter);     // Prog  
         QListView::setColumnAlignment(6, Qt::AlignHCenter);     // Nr  
         QListView::setColumnAlignment(7, Qt::AlignHCenter);     // Vol  
   
         QListView::setColumnWidth(0, 120);      // Name  
         QListView::setColumnWidth(5, 240);      // File  
   
         m_pNewGroupAction = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("itemGroupNew.png")),  
                 tr("New &Group"), tr("Ctrl+G"), this);  
         m_pNewItemAction  = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("itemNew.png")),  
                 tr("New &Instrument..."), tr("Ins"), this);  
         m_pEditItemAction = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("formEdit.png")),  
                 tr("&Edit..."), tr("Enter"), this);  
         m_pRenameAction   = new QAction(tr("&Rename"), tr("F2"), this);  
         m_pDeleteAction   = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("formRemove.png")),  
                 tr("&Delete"), tr("Del"), this);  
         m_pRefreshAction  = new QAction(  
                 QIconSet(QPixmap::fromMimeSource("formRefresh.png")),  
                 tr("Re&fresh"), tr("F5"), this);  
   
         m_pNewGroupAction->setToolTip(tr("New Group"));  
         m_pNewItemAction->setToolTip(tr("New Instrument"));  
         m_pEditItemAction->setToolTip(tr("Edit"));  
         m_pRenameAction->setToolTip(tr("Rename"));  
         m_pDeleteAction->setToolTip(tr("Delete"));  
         m_pRefreshAction->setToolTip(tr("Refresh"));  
   
         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 )  
 {  
         // 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;  
   
         // Add the new item under proper group one, if any...  
         if (pParentGroup) {  
                 pParentGroup->setOpen(true);  
                 pItem = new qsamplerInstrumentItem(pParentGroup, pInstrument);  
         } else {  
                 pItem = new qsamplerInstrumentItem(this, pInstrument);  
         }  
   
         // Set it as current selection...  
         QListView::setSelected(pItem, true);  
   
         return pItem;  
 }  
   
   
 // Add a new instrument group, optionally under another group.  
 qsamplerInstrumentGroup *qsamplerInstrumentList::addGroup (  
         const QString& sName, qsamplerInstrumentGroup *pParentGroup )  
 {  
         qsamplerInstrumentGroup *pGroup = findGroup(sName);  
         if (pGroup == NULL) {  
                 if (pParentGroup) {  
                         pParentGroup->setOpen(true);  
                         pGroup = new qsamplerInstrumentGroup(pParentGroup, sName);  
                 } else {  
                         pGroup = new qsamplerInstrumentGroup(this, sName);  
                 }  
         }  
         QListView::setSelected(pGroup, true);  
         return pGroup;  
 }  
   
   
 // Find a group item, given its name.  
 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;  
 }  
   
   
 // Find a file item, given its name.  
 qsamplerInstrumentItem *qsamplerInstrumentList::findItem (  
         qsamplerInstrument *pInstrument ) const  
 {  
         if (pInstrument == NULL)  
                 return NULL;  
   
         // Iterate all over the place to search for the group.  
         QListViewItemIterator iter((QListView *) this);  
         while (iter.current()) {  
                 QListViewItem *pListItem = iter.current();  
                 if (pListItem->rtti() == Item) {  
                         qsamplerInstrumentItem *pItem  
                                 = static_cast<qsamplerInstrumentItem *> (pListItem);  
                         if (pItem && pItem->instrument()  
                                 && pItem->instrument()->map()  == pInstrument->map()  
                                 && pItem->instrument()->bank() == pInstrument->bank()  
                                 && pItem->instrument()->prog() == pInstrument->prog())  
                                 return pItem;  
                 }  
                 ++iter;  
         }  
         // Not found.  
         return NULL;  
63  }  }
64    
65    int MidiInstrumentsModel::rowCount(const QModelIndex& /*parent*/) const {
66  // Find and return the nearest group item...      if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
67  qsamplerInstrumentGroup *qsamplerInstrumentList::groupItem (          int n = 0;
68          QListViewItem *pItem ) const          for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap)
69  {              n += (*itMap).size();
70          while (pItem && pItem->rtti() != Group)          return n;
71                  pItem = pItem->parent();      }
72          return static_cast<qsamplerInstrumentGroup *> (pItem);      InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);
73        if (itMap == instruments.end()) return 0;
74        return (*itMap).size();
75    }
76    
77    int MidiInstrumentsModel::columnCount(const QModelIndex& /*parent*/) const {
78        return 9;
79    }
80    
81    QVariant MidiInstrumentsModel::data(const QModelIndex &index, int role) const {
82        if (!index.isValid()) return QVariant();
83    
84        const qsamplerInstrument* pInstr = NULL;
85    
86        if (m_iMidiMap == LSCP_MIDI_MAP_ALL) {
87            int n = 0;
88            for (InstrumentsMap::const_iterator itMap = instruments.begin(); itMap != instruments.end(); ++itMap) {
89                n += (*itMap).size();
90                if (index.row() < n)
91                    pInstr = &(*itMap)[index.row() + (*itMap).size() - n];
92            }
93        } else {
94            // resolve MIDI instrument map
95            InstrumentsMap::const_iterator itMap = instruments.find(m_iMidiMap);
96            if (itMap == instruments.end()) return QVariant();
97            // resolve instrument in that map
98            if (index.row() >= (*itMap).size()) return QVariant();
99            pInstr = &(*itMap)[index.row()];
100        }
101    
102        if (!pInstr) return QVariant();
103    
104        if (role == Qt::UserRole) {
105            return QVariant::fromValue((void*)pInstr);
106        }
107    
108        if (role == Qt::DisplayRole) {
109            switch (index.column()) {
110                case 0: return pInstr->name();
111                case 1: return QVariant::fromValue(pInstr->map());
112                case 2: return QVariant::fromValue(pInstr->bank());
113                case 3: return QVariant::fromValue(pInstr->prog());
114                case 4: return pInstr->engineName();
115                case 5: return pInstr->instrumentFile();
116                case 6: return QVariant::fromValue(pInstr->instrumentNr());
117                case 7: return QString::number(pInstr->volume() * 100.0) + " %";
118                case 8: {
119                    switch (pInstr->loadMode()) {
120                        case 3: return QObject::tr("Persistent");
121                        case 2: return QObject::tr("On Demand Hold");
122                        case 1: return QObject::tr("On Demand");
123                        default: return QVariant();
124                    }
125                }
126                default: return QVariant();
127            }
128        }
129    
130        return QVariant();
131    }
132    
133    QVariant MidiInstrumentsModel::headerData(int section, Qt::Orientation orientation, int role) const {
134        if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
135            return QVariant();
136    
137        switch (section) {
138            case 0: return tr("Name");
139            case 1: return tr("Map");
140            case 2: return tr("Bank");
141            case 3: return tr("Prog");
142            case 4: return tr("Engine");
143            case 5: return tr("File");
144            case 6: return tr("Nr");
145            case 7: return tr("Vol");
146            case 8: return tr("Mode");
147            default: return QVariant();
148        }
149    }
150    
151    qsamplerInstrument* MidiInstrumentsModel::addInstrument(int iMap, int iBank, int iProg) {
152        // Check it there's already one instrument item
153        // with the very same key (bank, program);
154        // if yes, just remove it without prejudice...
155        for (int i = 0; i < instruments[iMap].size(); i++) {
156            if (
157                instruments[iMap][i].bank() == iBank &&
158                instruments[iMap][i].prog() == iProg
159            ) {
160                instruments[iMap].removeAt(i);
161                break;
162            }
163        }
164    
165        // resolve the appropriate place, we keep the list sorted that way ...
166        int i = 0;
167        for (; i < instruments[iMap].size(); i++)
168            if (
169                iBank > instruments[iMap][i].bank() ||
170                ( iBank == instruments[iMap][i].bank() &&
171                  iProg > instruments[iMap][i].prog() )
172            ) break;
173    
174        instruments[iMap].insert(i, qsamplerInstrument(iMap, iBank, iProg));
175        qsamplerInstrument& instr = instruments[iMap][i];
176        if (!instr.getInstrument())
177            instruments[iMap].removeAt(i);
178    
179        return &instr;
180    }
181    
182    void MidiInstrumentsModel::removeInstrument(const qsamplerInstrument& instrument) {
183        const int iMap  = instrument.map();
184        const int iBank = instrument.bank();
185        const int iProg = instrument.prog();
186        for (int i = 0; i < instruments[iMap].size(); i++) {
187            if (
188                instruments[iMap][i].bank() == iBank &&
189                instruments[iMap][i].prog() == iProg
190            ) {
191                instruments[iMap].removeAt(i);
192                break;
193            }
194        }
195    }
196    
197    // reposition the instrument in the model (called when map/bank/prg changed)
198    void MidiInstrumentsModel::resort(const qsamplerInstrument instrument) {
199        const int iMap  = instrument.map();
200        const int iBank = instrument.bank();
201        const int iProg = instrument.prog();
202        // remove given instrument from its current list
203        removeInstrument(instrument);
204        // re-add the instrument
205        addInstrument(iMap, iBank, iProg);
206    }
207    
208    void MidiInstrumentsModel::setMidiMap(int iMidiMap) {
209        if (iMidiMap < 0)
210            iMidiMap = LSCP_MIDI_MAP_ALL;
211    
212        m_iMidiMap = iMidiMap;
213  }  }
214    
215    int MidiInstrumentsModel::midiMap() const {
216  // Add a new group item below the current one.      return m_iMidiMap;
 void qsamplerInstrumentList::newGroupSlot (void)  
 {  
         qsamplerInstrumentGroup *pNewGroup  
                 = addGroup(tr("New Group"), groupItem(QListView::selectedItem()));  
         if (pNewGroup)  
                 pNewGroup->startRename(0);  
   
         selectionChangedSlot();  
 }  
   
   
 // Map selector.  
 void qsamplerInstrumentList::setMidiMap ( int iMidiMap )  
 {  
         if (iMidiMap < 0)  
                 iMidiMap = LSCP_MIDI_MAP_ALL;  
   
         m_iMidiMap = iMidiMap;  
217  }  }
218    
219  int qsamplerInstrumentList::midiMap (void) const  void MidiInstrumentsModel::refresh() {
220  {          instruments.clear();
         return m_iMidiMap;  
 }  
   
   
 // List actions accessors.  
 QAction *qsamplerInstrumentList::newGroupAction (void) const  
 {  
         return m_pNewGroupAction;  
 }  
   
 QAction *qsamplerInstrumentList::newItemAction (void) const  
 {  
         return m_pNewItemAction;  
 }  
   
 QAction *qsamplerInstrumentList::editItemAction (void) const  
 {  
         return m_pEditItemAction;  
 }  
221    
222  QAction *qsamplerInstrumentList::renameAction (void) const          MainForm* pMainForm = MainForm::getInstance();
223  {          if (pMainForm == NULL)
         return m_pRenameAction;  
 }  
   
 QAction *qsamplerInstrumentList::deleteAction (void) const  
 {  
         return m_pDeleteAction;  
 }  
   
 QAction *qsamplerInstrumentList::refreshAction (void) const  
 {  
         return m_pRefreshAction;  
 }  
   
   
 // Add a new instrument item below the current one.  
 void qsamplerInstrumentList::newItemSlot (void)  
 {  
         qsamplerInstrument *pInstrument = new qsamplerInstrument();  
   
         qsamplerInstrumentForm form(this);  
         form.setup(pInstrument);  
         if (!form.exec()) {  
                 delete pInstrument;  
                 return;  
         }  
   
         // Commit...  
         pInstrument->mapInstrument();  
         // add new item to the tree...  
         addItem(pInstrument, groupItem(QListView::selectedItem()));  
         // Notify we've changes...  
         emit instrumentsChanged();  
   
         selectionChangedSlot();  
 }  
   
   
 // Edit current item below the current one.  
 void qsamplerInstrumentList::editItemSlot (void)  
 {  
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem == NULL)  
224                  return;                  return;
225          if (pListItem->rtti() == Item) {          if (pMainForm->client() == NULL)
                 qsamplerInstrument *pInstrument = NULL;  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem)  
                         pInstrument = pItem->instrument();  
                 if (pInstrument) {  
                         // Save current key values...  
                         int iMap  = pInstrument->map();  
                         int iBank = pInstrument->bank();  
                         int iProg = pInstrument->prog();  
                         // Do the edit dance...  
                         qsamplerInstrumentForm form(this);  
                         form.setup(pInstrument);  
                         if (form.exec()) {  
                                 // Commit...  
                                 pInstrument->mapInstrument();  
                                 // Check whether we changed instrument key...  
                                 if (iMap  == pInstrument->map()  &&  
                                         iBank == pInstrument->bank() &&  
                                         iProg == pInstrument->prog()) {  
                                         // just update tree item...  
                                         pItem->update();  
                                 } else {  
                                         // Change item tree, whether applicable...  
                                         if (m_iMidiMap < 0 || m_iMidiMap == pInstrument->map()) {  
                                                 // Add new brand item into view...  
                                                 addItem(pInstrument, groupItem(pListItem));  
                                         } else {  
                                                 // Just remove/hide old one.  
                                                 delete pItem;  
                                         }  
                                 }  
                                 // Notify we've changes...  
                                 emit instrumentsChanged();  
                         }  
                 }  
         }  
   
         selectionChangedSlot();  
 }  
   
   
 // Rename current group/item.  
 void qsamplerInstrumentList::renameSlot (void)  
 {  
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem)  
                 pListItem->startRename(0);  
   
         selectionChangedSlot();  
 }  
   
   
 // Remove current group/item.  
 void qsamplerInstrumentList::deleteSlot (void)  
 {  
         QListViewItem *pListItem = QListView::selectedItem();  
         if (pListItem == NULL)  
226                  return;                  return;
227    
228          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();          QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
         if (pMainForm == NULL)  
                 return;  
229    
230          // Prompt user if this is for real...          // Load the whole bunch of instrument items...
231          qsamplerOptions *pOptions = pMainForm->options();          lscp_midi_instrument_t* pInstrs
232          if (pOptions && pOptions->bConfirmRemove) {                  = ::lscp_list_midi_instruments(pMainForm->client(), m_iMidiMap);
233                  if (QMessageBox::warning(this,          for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {
234                          QSAMPLER_TITLE ": " + tr("Warning"),                  const int iMap  = pInstrs[iInstr].map;
235                          tr("Delete %1:\n\n"                  const int iBank = pInstrs[iInstr].bank;
236                          "%2\n\n"                  const int iProg = pInstrs[iInstr].prog;
237                          "Are you sure?")                  addInstrument(iMap, iBank, iProg);
238                          .arg(pListItem->rtti() == Item ? tr("instrument") : tr("group"))                  // Try to keep it snappy :)
239                          .arg(pListItem->text(0)),                  QApplication::processEvents(QEventLoop::ExcludeUserInput);
                         tr("OK"), tr("Cancel")) > 0)  
                         return;  
240          }          }
241    
242          // Unmap instrument entry...          QApplication::restoreOverrideCursor();
         if (pListItem->rtti() == Item) {  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         pItem->instrument()->unmapInstrument();  
                         emit instrumentsChanged();  
                 }  
         }  
243    
244          // Do it for real...          if (pInstrs == NULL && ::lscp_client_get_errno(pMainForm->client())) {
245          delete pListItem;                  pMainForm->appendMessagesClient("lscp_list_midi_instruments");
246                    pMainForm->appendMessagesError(tr("Could not get current list of MIDI instrument mappings.\n\nSorry."));
247            }
248    
249          selectionChangedSlot();          // inform the outer world (QTableView) that our data changed
250            QAbstractTableModel::reset();
251  }  }
252    
253    
254  // In-place selection slot.  //-------------------------------------------------------------------------
255  void qsamplerInstrumentList::selectionChangedSlot (void)  // MidiInstrumentsDelegate - table cell renderer for MIDI prog mappings
256  {  // (doesn't actually do anything ATM, but is already there for a future
257          qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();  // cell editor widget implementation)
         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);  
 }  
   
258    
259  // In-place activation slot.  MidiInstrumentsDelegate::MidiInstrumentsDelegate(QObject* parent) : QItemDelegate(parent) {
 void qsamplerInstrumentList::activatedSlot ( QListViewItem *pListItem )  
 {  
         // FIXME: Hope the list view item is the one selected.  
         if (pListItem->rtti() == Item)  
                 editItemSlot();  
260  }  }
261    
262    QWidget* MidiInstrumentsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
263  // In-place aliasing slot.      return QItemDelegate::createEditor(parent, option, index);
264  void qsamplerInstrumentList::renamedSlot ( QListViewItem *pListItem )      //return new QLabel(index.model()->data(index, Qt::DisplayRole).toString(), parent);
 {  
         if (pListItem->rtti() == Item) {  
                 qsamplerInstrumentItem *pItem  
                         = static_cast<qsamplerInstrumentItem *> (pListItem);  
                 if (pItem && pItem->instrument()) {  
                         pItem->instrument()->setName(pListItem->text(0));  
                         pItem->instrument()->mapInstrument();  
                         emit instrumentsChanged();  
                         pItem->update();  
                 }  
         }  
265  }  }
266    
267    void MidiInstrumentsDelegate::setEditorData(QWidget* /*editor*/, const QModelIndex& /*index*/) const {
 // Context menu request event handler.  
 void qsamplerInstrumentList::contextMenuEvent (  
         QContextMenuEvent *pContextMenuEvent )  
 {  
         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());  
268  }  }
269    
270    void MidiInstrumentsDelegate::setModelData(QWidget* /*editor*/, QAbstractItemModel* /*model*/, const QModelIndex& /*index*/) const {
271    }
272    
273  // General reloader.  void MidiInstrumentsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
274  void qsamplerInstrumentList::refresh (void)      QItemDelegate::updateEditorGeometry(editor, option, index);
275  {      //if (editor) editor->setGeometry(option.rect);
         clear();  
   
         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(), m_iMidiMap);  
         for (int iInstr = 0; pInstrs && pInstrs[iInstr].map >= 0; ++iInstr) {  
                 int iMap  = pInstrs[iInstr].map;  
                 int iBank = pInstrs[iInstr].bank;  
                 int iProg = pInstrs[iInstr].prog;  
                 qsamplerInstrument *pInstrument  
                         = new qsamplerInstrument(iMap, iBank, iProg);  
                 if (pInstrument->getInstrument())  
                         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();  
276  }  }
277    
278    
279  // end of qsamplerInstrumentList.cpp  // end of qsamplerInstrumentList.cpp
   

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

  ViewVC Help
Powered by ViewVC