/[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 1464 by capela, Thu Nov 1 17:14:21 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 "qsamplerMainForm.h"  #include "qsamplerInstrumentList.h"
27    
28    #include "qsamplerInstrumentForm.h"
29    
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) : QMainWindow(parent, flags) {  //-------------------------------------------------------------------------
41      ui.setupUi(this);  // QSampler::InstrumentListForm -- Instrument map list form implementation.
42    //
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.  
     InstrumentToolbar = addToolBar(tr("MIDI Instruments"));  
     m_pMapComboBox = new QComboBox(InstrumentToolbar);  
     m_pMapComboBox->setMinimumWidth(120);  
     m_pMapComboBox->setEnabled(false);  
     QToolTip::add(m_pMapComboBox, tr("Instrument Map"));  
   
     InstrumentToolbar->addSeparator();  
     ui.newInstrumentAction->addTo(InstrumentToolbar);  
     ui.editInstrumentAction->addTo(InstrumentToolbar);  
     ui.deleteInstrumentAction->addTo(InstrumentToolbar);  
     InstrumentToolbar->addSeparator();  
     ui.refreshInstrumentsAction->addTo(InstrumentToolbar);  
49    
50      ui.InstrumentTable->setModel(&model);          // Setup toolbar widgets.
51      //ui.InstrumentTable->setDelegate(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(
72                    m_pInstrumentListView,
73                    SIGNAL(customContextMenuRequested(const QPoint&)),
74                    SLOT(contextMenu(const QPoint&)));
75            QObject::connect(
76                    m_pInstrumentListView,
77                    SIGNAL(activated(const QModelIndex&)),
78                    SLOT(stabilizeForm()));
79            QObject::connect(
80                    m_pInstrumentListView,
81                    SIGNAL(doubleClicked(const QModelIndex&)),
82                    SLOT(editInstrument(const QModelIndex&)));
83            QObject::connect(
84                    m_ui.newInstrumentAction,
85                    SIGNAL(triggered()),
86                    SLOT(newInstrument()));
87            QObject::connect(
88                    m_ui.deleteInstrumentAction,
89                    SIGNAL(triggered()),
90                    SLOT(deleteInstrument()));
91            QObject::connect(
92                    m_ui.editInstrumentAction,
93                    SIGNAL(triggered()),
94                    SLOT(editInstrument()));
95            QObject::connect(
96                    m_ui.refreshInstrumentsAction,
97                    SIGNAL(triggered()),
98                    SLOT(refreshInstruments()));
99    
100      connect(          // Things must be stable from the start.
101          ui.refreshInstrumentsAction,          stabilizeForm();
         SIGNAL(triggered()), SLOT(refreshInstruments(void))  
     );  
102  }  }
103    
104  InstrumentListForm::~InstrumentListForm() {  
105    InstrumentListForm::~InstrumentListForm (void)
106    {
107            delete m_pInstrumentListView;
108          delete m_pMapComboBox;          delete m_pMapComboBox;
109  }  }
110    
# Line 77  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 90  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 103  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 134  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 142  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    
198    void InstrumentListForm::editInstrument (void)
199    {
200            editInstrument(m_pInstrumentListView->currentIndex());
201    }
202    
203    
204    void InstrumentListForm::editInstrument ( const QModelIndex& index )
205    {
206            if (!index.isValid())
207                    return;
208    
209            Instrument *pInstrument
210                    = static_cast<Instrument *> (index.internalPointer());
211            if (pInstrument == NULL)
212                    return;
213    
214            // Save current key values...
215            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...
222            InstrumentForm form(this);
223            form.setup(&instrument);
224            if (form.exec()) {
225                    // Commit...
226                    instrument.mapInstrument();
227                    // Check whether we changed instrument key...
228                    if (instrument.map()  == iMap  &&
229                            instrument.bank() == iBank &&
230                            instrument.prog() == iProg) {
231                            // Just update tree item...
232                            //pItem->update();
233                    } else {
234                            // Unmap old instance...
235                            Instrument(iMap, iBank, iProg).unmapInstrument();
236                            // correct the position of the instrument in the model
237                            m_pInstrumentListView->updateInstrument(pInstrument);
238                    }
239            }
240    
241            stabilizeForm();
242    }
243    
244    
245    void InstrumentListForm::newInstrument (void)
246    {
247            Instrument instrument;
248    
249            InstrumentForm form(this);
250            form.setup(&instrument);
251            if (!form.exec())
252                    return;
253    
254            // Commit...
255            instrument.mapInstrument();
256    
257            // add new item to the table model
258            m_pInstrumentListView->addInstrument(
259                    instrument.map(),
260                    instrument.bank(),
261                    instrument.prog());
262    
263            stabilizeForm();
264    }
265    
266    
267    void InstrumentListForm::deleteInstrument (void)
268    {
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            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();
297    
298            // let the instrument vanish from the table model
299            m_pInstrumentListView->removeInstrument(pInstrument);
300    
301            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.1464  
changed lines
  Added in v.2065

  ViewVC Help
Powered by ViewVC