/[svn]/qsampler/trunk/src/qsamplerInstrumentListForm.cpp
ViewVC logotype

Diff of /qsampler/trunk/src/qsamplerInstrumentListForm.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1492 by schoenebeck, Mon Nov 19 21:08:18 2007 UTC revision 2065 by capela, Sat Mar 13 12:44:15 2010 UTC
# Line 1  Line 1 
1  // qsamplerInstrumentListForm.cpp  // qsamplerInstrumentListForm.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2003-2010, rncbc aka Rui Nuno Capela. All rights reserved.
5     Copyright (C) 2007, Christian Schoenebeck     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
# Line 20  Line 20 
20    
21  *****************************************************************************/  *****************************************************************************/
22    
23    #include "qsamplerAbout.h"
24  #include "qsamplerInstrumentListForm.h"  #include "qsamplerInstrumentListForm.h"
25    
26    #include "qsamplerInstrumentList.h"
27    
28  #include "qsamplerInstrumentForm.h"  #include "qsamplerInstrumentForm.h"
29  #include "qsamplerMainForm.h"  
30  #include "qsamplerOptions.h"  #include "qsamplerOptions.h"
31  #include "qsamplerInstrument.h"  #include "qsamplerInstrument.h"
32    #include "qsamplerMainForm.h"
33    
34    #include <QHeaderView>
35    #include <QMessageBox>
36    
 #include <QToolTip>  
37    
38  namespace QSampler {  namespace QSampler {
39    
40  InstrumentListForm::InstrumentListForm ( QWidget* parent, Qt::WindowFlags flags )  //-------------------------------------------------------------------------
41          : QMainWindow(parent, flags)  // QSampler::InstrumentListForm -- Instrument map list form implementation.
42  {  //
     ui.setupUi(this);  
43    
44      ui.newInstrumentAction->setText(tr("New &Instrument..."));  InstrumentListForm::InstrumentListForm (
45      ui.newInstrumentAction->setShortcut(Qt::Key_Insert);          QWidget* pParent, Qt::WindowFlags wflags )
46      ui.editInstrumentAction->setText(tr("&Edit..."));          : QMainWindow(pParent, wflags)
47      ui.editInstrumentAction->setShortcut(Qt::Key_Enter);  {
48      ui.deleteInstrumentAction->setText(tr("&Delete"));          m_ui.setupUi(this);
     ui.deleteInstrumentAction->setShortcut(Qt::Key_Delete);  
     ui.refreshInstrumentsAction->setText(tr("&Refresh"));  
     ui.refreshInstrumentsAction->setShortcut(Qt::Key_F5);  
   
     // Setup toolbar widgets.  
     m_pMapComboBox = new QComboBox(ui.InstrumentToolbar);  
     m_pMapComboBox->setMinimumWidth(120);  
     m_pMapComboBox->setEnabled(false);  
     QToolTip::add(m_pMapComboBox, tr("Instrument Map"));  
     ui.InstrumentToolbar->addWidget(m_pMapComboBox);  
   
     ui.InstrumentToolbar->addSeparator();  
     ui.newInstrumentAction->addTo(ui.InstrumentToolbar);  
     ui.editInstrumentAction->addTo(ui.InstrumentToolbar);  
     ui.deleteInstrumentAction->addTo(ui.InstrumentToolbar);  
     ui.InstrumentToolbar->addSeparator();  
     ui.refreshInstrumentsAction->addTo(ui.InstrumentToolbar);  
49    
50      ui.InstrumentTable->setModel(&model);          // Setup toolbar widgets.
51      ui.InstrumentTable->setItemDelegate(&delegate);          m_pMapComboBox = new QComboBox(m_ui.instrumentToolbar);
52            m_pMapComboBox->setMinimumWidth(120);
53            m_pMapComboBox->setEnabled(false);
54            m_pMapComboBox->setToolTip(tr("Instrument Map"));
55            m_ui.instrumentToolbar->addWidget(m_pMapComboBox);
56    
57            m_ui.instrumentToolbar->addSeparator();
58            m_ui.instrumentToolbar->addAction(m_ui.newInstrumentAction);
59            m_ui.instrumentToolbar->addAction(m_ui.editInstrumentAction);
60            m_ui.instrumentToolbar->addAction(m_ui.deleteInstrumentAction);
61            m_ui.instrumentToolbar->addSeparator();
62            m_ui.instrumentToolbar->addAction(m_ui.refreshInstrumentsAction);
63    
64            m_pInstrumentListView = new InstrumentListView(this);
65            m_pInstrumentListView->setContextMenuPolicy(Qt::CustomContextMenu);
66            QMainWindow::setCentralWidget(m_pInstrumentListView);
67    
68          QObject::connect(m_pMapComboBox,          QObject::connect(m_pMapComboBox,
69                  SIGNAL(activated(int)),                  SIGNAL(activated(int)),
70                  SLOT(activateMap(int)));                  SLOT(activateMap(int)));
   
71          QObject::connect(          QObject::connect(
72                  ui.refreshInstrumentsAction,                  m_pInstrumentListView,
73                  SIGNAL(triggered()),                  SIGNAL(customContextMenuRequested(const QPoint&)),
74                  SLOT(refreshInstruments(void))                  SLOT(contextMenu(const QPoint&)));
75      );          QObject::connect(
76                    m_pInstrumentListView,
         connect(  
                 ui.InstrumentTable,  
77                  SIGNAL(activated(const QModelIndex&)),                  SIGNAL(activated(const QModelIndex&)),
78                  SLOT(editInstrument(const QModelIndex&))                  SLOT(stabilizeForm()));
79          );          QObject::connect(
80          connect(                  m_pInstrumentListView,
81                  ui.newInstrumentAction,                  SIGNAL(doubleClicked(const QModelIndex&)),
82                    SLOT(editInstrument(const QModelIndex&)));
83            QObject::connect(
84                    m_ui.newInstrumentAction,
85                  SIGNAL(triggered()),                  SIGNAL(triggered()),
86                  SLOT(newInstrument())                  SLOT(newInstrument()));
87          );          QObject::connect(
88          connect(                  m_ui.deleteInstrumentAction,
                 ui.deleteInstrumentAction,  
89                  SIGNAL(triggered()),                  SIGNAL(triggered()),
90                  SLOT(deleteInstrument())                  SLOT(deleteInstrument()));
91          );          QObject::connect(
92          connect(                  m_ui.editInstrumentAction,
                 ui.editInstrumentAction,  
93                  SIGNAL(triggered()),                  SIGNAL(triggered()),
94                  SLOT(editInstrument())                  SLOT(editInstrument()));
95          );          QObject::connect(
96                    m_ui.refreshInstrumentsAction,
97                    SIGNAL(triggered()),
98                    SLOT(refreshInstruments()));
99    
100            // Things must be stable from the start.
101            stabilizeForm();
102  }  }
103    
104  InstrumentListForm::~InstrumentListForm() {  
105    InstrumentListForm::~InstrumentListForm (void)
106    {
107            delete m_pInstrumentListView;
108          delete m_pMapComboBox;          delete m_pMapComboBox;
109  }  }
110    
# Line 102  InstrumentListForm::~InstrumentListForm( Line 112  InstrumentListForm::~InstrumentListForm(
112  // Notify our parent that we're emerging.  // Notify our parent that we're emerging.
113  void InstrumentListForm::showEvent ( QShowEvent *pShowEvent )  void InstrumentListForm::showEvent ( QShowEvent *pShowEvent )
114  {  {
115          //MainForm* pMainForm = MainForm::getInstance();          MainForm* pMainForm = MainForm::getInstance();
116          //if (pMainForm)          if (pMainForm)
117          //      pMainForm->stabilizeForm();                  pMainForm->stabilizeForm();
118    
119          QWidget::showEvent(pShowEvent);          QWidget::showEvent(pShowEvent);
120  }  }
# Line 115  void InstrumentListForm::hideEvent ( QHi Line 125  void InstrumentListForm::hideEvent ( QHi
125  {  {
126          QWidget::hideEvent(pHideEvent);          QWidget::hideEvent(pHideEvent);
127    
128          //MainForm* pMainForm = MainForm::getInstance();          MainForm* pMainForm = MainForm::getInstance();
129          //if (pMainForm)          if (pMainForm)
130          //      pMainForm->stabilizeForm();                  pMainForm->stabilizeForm();
131    }
132    
133    
134    // Just about to notify main-window that we're closing.
135    void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ )
136    {
137            QWidget::hide();
138    
139            MainForm *pMainForm = MainForm::getInstance();
140            if (pMainForm)
141                    pMainForm->stabilizeForm();
142  }  }
143    
144    
# Line 128  void InstrumentListForm::refreshInstrume Line 149  void InstrumentListForm::refreshInstrume
149          if (pMainForm == NULL)          if (pMainForm == NULL)
150                  return;                  return;
151    
152          qsamplerOptions *pOptions = pMainForm->options();          Options *pOptions = pMainForm->options();
153          if (pOptions == NULL)          if (pOptions == NULL)
154                  return;                  return;
155    
156          // Get/save current map selection...          // Get/save current map selection...
157          int iMap = m_pMapComboBox->currentItem();          int iMap = m_pMapComboBox->currentIndex();
158          if (iMap < 0 || m_pMapComboBox->count() < 2)          if (iMap < 0 || m_pMapComboBox->count() < 2)
159                  iMap = pOptions->iMidiMap + 1;                  iMap = pOptions->iMidiMap + 1;
160    
161          // Populate maps list.          // Populate maps list.
162          m_pMapComboBox->clear();          m_pMapComboBox->clear();
163          m_pMapComboBox->insertItem(tr("(All)"));          m_pMapComboBox->addItem(tr("(All)"));
164          m_pMapComboBox->insertStringList(qsamplerInstrument::getMapNames());          m_pMapComboBox->insertItems(1, Instrument::getMapNames());
165    
166          // Adjust to saved selection...          // Adjust to saved selection...
167          if (iMap < 0 || iMap >= m_pMapComboBox->count())          if (iMap < 0 || iMap >= m_pMapComboBox->count())
168                  iMap = 0;                  iMap = 0;
169          m_pMapComboBox->setCurrentItem(iMap);          m_pMapComboBox->setCurrentIndex(iMap);
170          m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1);          m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1);
171    
172          activateMap(iMap);          activateMap(iMap);
# Line 159  void InstrumentListForm::activateMap ( i Line 180  void InstrumentListForm::activateMap ( i
180          if (pMainForm == NULL)          if (pMainForm == NULL)
181                  return;                  return;
182    
183          qsamplerOptions *pOptions = pMainForm->options();          Options *pOptions = pMainForm->options();
184          if (pOptions == NULL)          if (pOptions == NULL)
185                  return;                  return;
186    
# Line 167  void InstrumentListForm::activateMap ( i Line 188  void InstrumentListForm::activateMap ( i
188          if (iMidiMap >= 0)          if (iMidiMap >= 0)
189                  pOptions->iMidiMap = iMidiMap;                  pOptions->iMidiMap = iMidiMap;
190    
191          model.setMidiMap(iMidiMap);          m_pInstrumentListView->setMidiMap(iMidiMap);
192          model.refresh();          m_pInstrumentListView->refresh();
193    
194            stabilizeForm();
195  }  }
196    
197  void InstrumentListForm::editInstrument() {  
198          const QModelIndex index = ui.InstrumentTable->currentIndex();  void InstrumentListForm::editInstrument (void)
199          editInstrument(index);  {
200            editInstrument(m_pInstrumentListView->currentIndex());
201  }  }
202    
 void InstrumentListForm::editInstrument(const QModelIndex& index) {  
         if (!index.isValid() || !index.data(Qt::UserRole).isValid())  
                 return;  
203    
204          qsamplerInstrument* pInstrument =  void InstrumentListForm::editInstrument ( const QModelIndex& index )
205                  (qsamplerInstrument*) index.data(Qt::UserRole).value<void*>();  {
206            if (!index.isValid())
207                    return;
208    
209          if (!pInstrument) return;          Instrument *pInstrument
210                    = static_cast<Instrument *> (index.internalPointer());
211            if (pInstrument == NULL)
212                    return;
213    
214          // Save current key values...          // Save current key values...
215          qsamplerInstrument oldInstrument(*pInstrument);          int iMap  = pInstrument->map();
216            int iBank = pInstrument->bank();
217            int iProg = pInstrument->prog();
218    
219            Instrument instrument(iMap, iBank, iProg);
220    
221          // Do the edit dance...          // Do the edit dance...
222          InstrumentForm form(this);          InstrumentForm form(this);
223          form.setup(pInstrument);          form.setup(&instrument);
224          if (form.exec()) {          if (form.exec()) {
225                  // Commit...                  // Commit...
226                  pInstrument->mapInstrument();                  instrument.mapInstrument();
227                  // Check whether we changed instrument key...                  // Check whether we changed instrument key...
228                  if (oldInstrument.map()  == pInstrument->map()  &&                  if (instrument.map()  == iMap  &&
229                          oldInstrument.bank() == pInstrument->bank() &&                          instrument.bank() == iBank &&
230                          oldInstrument.prog() == pInstrument->prog()) {                          instrument.prog() == iProg) {
231                          // just update tree item...                          // Just update tree item...
232                          //pItem->update();                          //pItem->update();
233                  } else {                  } else {
234                          // Unmap old instance...                          // Unmap old instance...
235                          oldInstrument.unmapInstrument();                          Instrument(iMap, iBank, iProg).unmapInstrument();
236                          // correct the position of the instrument in the model                          // correct the position of the instrument in the model
237                          model.resort(*pInstrument);                          m_pInstrumentListView->updateInstrument(pInstrument);
238                  }                  }
                 // Notify we've changes...  
                 emit model.reset();  
239          }          }
240    
241            stabilizeForm();
242  }  }
243    
244  void InstrumentListForm::newInstrument() {  
245          qsamplerInstrument instrument;  void InstrumentListForm::newInstrument (void)
246    {
247            Instrument instrument;
248    
249          InstrumentForm form(this);          InstrumentForm form(this);
250          form.setup(&instrument);          form.setup(&instrument);
251          if (!form.exec()) return;          if (!form.exec())
252                    return;
253    
254          // Commit...          // Commit...
255          instrument.mapInstrument();          instrument.mapInstrument();
256    
257          // add new item to the table model          // add new item to the table model
258          model.resort(instrument);          m_pInstrumentListView->addInstrument(
259          // Notify we've changes...                  instrument.map(),
260          //emit model.reset();                  instrument.bank(),
261          //FIXME: call above didnt really refresh, so we use this for now ...                  instrument.prog());
262          refreshInstruments();  
263            stabilizeForm();
264  }  }
265    
 void InstrumentListForm::deleteInstrument() {  
         const QModelIndex index = ui.InstrumentTable->currentIndex();  
         if (!index.isValid() || !index.data(Qt::UserRole).isValid()) return;  
266    
267          qsamplerInstrument* pInstrument =  void InstrumentListForm::deleteInstrument (void)
268                  (qsamplerInstrument*) index.data(Qt::UserRole).value<void*>();  {
269            const QModelIndex& index = m_pInstrumentListView->currentIndex();
270            if (!index.isValid())
271                    return;
272    
273            Instrument *pInstrument
274                    = static_cast<Instrument *> (index.internalPointer());
275            if (pInstrument == NULL)
276                    return;
277    
278          if (!pInstrument) return;          MainForm *pMainForm = MainForm::getInstance();
279            if (pMainForm == NULL)
280                    return;
281    
282            // Prompt user if this is for real...
283            Options *pOptions = pMainForm->options();
284            if (pOptions && pOptions->bConfirmRemove) {
285                    if (QMessageBox::warning(this,
286                            QSAMPLER_TITLE ": " + tr("Warning"),
287                            tr("About to delete instrument map entry:\n\n"
288                            "%1\n\n"
289                            "Are you sure?")
290                            .arg(pInstrument->name()),
291                            QMessageBox::Ok | QMessageBox::Cancel)
292                            == QMessageBox::Cancel)
293                            return;
294            }
295    
296          pInstrument->unmapInstrument();          pInstrument->unmapInstrument();
297    
298          // let the instrument vanish from the table model          // let the instrument vanish from the table model
299          model.removeInstrument(*pInstrument);          m_pInstrumentListView->removeInstrument(pInstrument);
300          // Notify we've changes...  
301          emit model.reset();          stabilizeForm();
302    }
303    
304    
305    // Update form actions enablement...
306    void InstrumentListForm::stabilizeForm (void)
307    {
308            MainForm *pMainForm = MainForm::getInstance();
309    
310            bool bEnabled = (pMainForm && pMainForm->client());
311            m_ui.newInstrumentAction->setEnabled(bEnabled);
312            const QModelIndex& index = m_pInstrumentListView->currentIndex();
313            bEnabled = (bEnabled && index.isValid());
314            m_ui.editInstrumentAction->setEnabled(bEnabled);
315            m_ui.deleteInstrumentAction->setEnabled(bEnabled);
316    }
317    
318    
319    // Handle custom context menu here...
320    void InstrumentListForm::contextMenu ( const QPoint& pos )
321    {
322            if (!m_ui.newInstrumentAction->isEnabled())
323                    return;
324    
325            m_ui.contextMenu->exec(
326                    (m_pInstrumentListView->viewport())->mapToGlobal(pos));
327  }  }
328    
329    
330  } // namespace QSampler  } // namespace QSampler
331    
332    

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

  ViewVC Help
Powered by ViewVC