/[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 1499 by capela, Tue Nov 20 16:48:04 2007 UTC revision 2064 by capela, Fri Mar 12 16:02:32 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    
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    //
43    
44    InstrumentListForm::InstrumentListForm (
45            QWidget* pParent, Qt::WindowFlags wflags )
46            : QMainWindow(pParent, wflags)
47  {  {
48      ui.setupUi(this);          m_ui.setupUi(this);
49    
50            // Setup toolbar widgets.
51            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    
66            QHeaderView *pHeader = m_pInstrumentListView->header();
67            pHeader->setDefaultAlignment(Qt::AlignLeft);
68            pHeader->setMovable(false);
69            pHeader->setStretchLastSection(true);
70            pHeader->resizeSection(0, 120);                                         // Name
71            m_pInstrumentListView->resizeColumnToContents(1);       // Map
72            m_pInstrumentListView->resizeColumnToContents(2);       // Bank
73            m_pInstrumentListView->resizeColumnToContents(3);       // Prog
74            m_pInstrumentListView->resizeColumnToContents(4);       // Engine
75            pHeader->resizeSection(5, 240);                                         // File
76            m_pInstrumentListView->resizeColumnToContents(6);       // Nr
77            pHeader->resizeSection(7, 60);                                          // Vol
78    
79      ui.newInstrumentAction->setText(tr("New &Instrument..."));          // Enable custom context menu...
80      ui.newInstrumentAction->setShortcut(Qt::Key_Insert);          m_pInstrumentListView->setContextMenuPolicy(Qt::CustomContextMenu);
     ui.editInstrumentAction->setText(tr("&Edit..."));  
     ui.editInstrumentAction->setShortcut(Qt::Key_Enter);  
     ui.deleteInstrumentAction->setText(tr("&Delete"));  
     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);  
     m_pMapComboBox->setToolTip(tr("Instrument Map"));  
     ui.InstrumentToolbar->addWidget(m_pMapComboBox);  
   
     ui.InstrumentToolbar->addSeparator();  
     ui.InstrumentToolbar->addAction(ui.newInstrumentAction);  
     ui.InstrumentToolbar->addAction(ui.editInstrumentAction);  
     ui.InstrumentToolbar->addAction(ui.deleteInstrumentAction);  
     ui.InstrumentToolbar->addSeparator();  
     ui.InstrumentToolbar->addAction(ui.refreshInstrumentsAction);  
81    
82      ui.InstrumentTable->setModel(&model);          QMainWindow::setCentralWidget(m_pInstrumentListView);
     ui.InstrumentTable->setItemDelegate(&delegate);  
83    
84          QObject::connect(m_pMapComboBox,          QObject::connect(m_pMapComboBox,
85                  SIGNAL(activated(int)),                  SIGNAL(activated(int)),
86                  SLOT(activateMap(int)));                  SLOT(activateMap(int)));
   
87          QObject::connect(          QObject::connect(
88                  ui.refreshInstrumentsAction,                  m_pInstrumentListView,
89                  SIGNAL(triggered()),                  SIGNAL(customContextMenuRequested(const QPoint&)),
90                  SLOT(refreshInstruments(void))                  SLOT(contextMenu(const QPoint&)));
91      );          QObject::connect(
92                    m_pInstrumentListView,
93          connect(                  SIGNAL(pressed(const QModelIndex&)),
94                  ui.InstrumentTable,                  SLOT(stabilizeForm()));
95            QObject::connect(
96                    m_pInstrumentListView,
97                  SIGNAL(activated(const QModelIndex&)),                  SIGNAL(activated(const QModelIndex&)),
98                  SLOT(editInstrument(const QModelIndex&))                  SLOT(editInstrument(const QModelIndex&)));
99          );          QObject::connect(
100          connect(                  m_ui.newInstrumentAction,
                 ui.newInstrumentAction,  
101                  SIGNAL(triggered()),                  SIGNAL(triggered()),
102                  SLOT(newInstrument())                  SLOT(newInstrument()));
103          );          QObject::connect(
104          connect(                  m_ui.deleteInstrumentAction,
                 ui.deleteInstrumentAction,  
105                  SIGNAL(triggered()),                  SIGNAL(triggered()),
106                  SLOT(deleteInstrument())                  SLOT(deleteInstrument()));
107          );          QObject::connect(
108          connect(                  m_ui.editInstrumentAction,
                 ui.editInstrumentAction,  
109                  SIGNAL(triggered()),                  SIGNAL(triggered()),
110                  SLOT(editInstrument())                  SLOT(editInstrument()));
111          );          QObject::connect(
112                    m_ui.refreshInstrumentsAction,
113                    SIGNAL(triggered()),
114                    SLOT(refreshInstruments()));
115    
116            // Things must be stable from the start.
117            stabilizeForm();
118  }  }
119    
120  InstrumentListForm::~InstrumentListForm() {  
121    InstrumentListForm::~InstrumentListForm (void)
122    {
123            delete m_pInstrumentListView;
124          delete m_pMapComboBox;          delete m_pMapComboBox;
125  }  }
126    
# Line 101  InstrumentListForm::~InstrumentListForm( Line 128  InstrumentListForm::~InstrumentListForm(
128  // Notify our parent that we're emerging.  // Notify our parent that we're emerging.
129  void InstrumentListForm::showEvent ( QShowEvent *pShowEvent )  void InstrumentListForm::showEvent ( QShowEvent *pShowEvent )
130  {  {
131          //MainForm* pMainForm = MainForm::getInstance();          MainForm* pMainForm = MainForm::getInstance();
132          //if (pMainForm)          if (pMainForm)
133          //      pMainForm->stabilizeForm();                  pMainForm->stabilizeForm();
134    
135          QWidget::showEvent(pShowEvent);          QWidget::showEvent(pShowEvent);
136  }  }
# Line 114  void InstrumentListForm::hideEvent ( QHi Line 141  void InstrumentListForm::hideEvent ( QHi
141  {  {
142          QWidget::hideEvent(pHideEvent);          QWidget::hideEvent(pHideEvent);
143    
144          //MainForm* pMainForm = MainForm::getInstance();          MainForm* pMainForm = MainForm::getInstance();
145          //if (pMainForm)          if (pMainForm)
146          //      pMainForm->stabilizeForm();                  pMainForm->stabilizeForm();
147    }
148    
149    
150    // Just about to notify main-window that we're closing.
151    void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ )
152    {
153            QWidget::hide();
154    
155            MainForm *pMainForm = MainForm::getInstance();
156            if (pMainForm)
157                    pMainForm->stabilizeForm();
158  }  }
159    
160    
# Line 127  void InstrumentListForm::refreshInstrume Line 165  void InstrumentListForm::refreshInstrume
165          if (pMainForm == NULL)          if (pMainForm == NULL)
166                  return;                  return;
167    
168          qsamplerOptions *pOptions = pMainForm->options();          Options *pOptions = pMainForm->options();
169          if (pOptions == NULL)          if (pOptions == NULL)
170                  return;                  return;
171    
# Line 139  void InstrumentListForm::refreshInstrume Line 177  void InstrumentListForm::refreshInstrume
177          // Populate maps list.          // Populate maps list.
178          m_pMapComboBox->clear();          m_pMapComboBox->clear();
179          m_pMapComboBox->addItem(tr("(All)"));          m_pMapComboBox->addItem(tr("(All)"));
180          m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames());          m_pMapComboBox->insertItems(1, Instrument::getMapNames());
181    
182          // Adjust to saved selection...          // Adjust to saved selection...
183          if (iMap < 0 || iMap >= m_pMapComboBox->count())          if (iMap < 0 || iMap >= m_pMapComboBox->count())
# Line 158  void InstrumentListForm::activateMap ( i Line 196  void InstrumentListForm::activateMap ( i
196          if (pMainForm == NULL)          if (pMainForm == NULL)
197                  return;                  return;
198    
199          qsamplerOptions *pOptions = pMainForm->options();          Options *pOptions = pMainForm->options();
200          if (pOptions == NULL)          if (pOptions == NULL)
201                  return;                  return;
202    
# Line 166  void InstrumentListForm::activateMap ( i Line 204  void InstrumentListForm::activateMap ( i
204          if (iMidiMap >= 0)          if (iMidiMap >= 0)
205                  pOptions->iMidiMap = iMidiMap;                  pOptions->iMidiMap = iMidiMap;
206    
207          model.setMidiMap(iMidiMap);          m_pInstrumentListView->setMidiMap(iMidiMap);
208          model.refresh();          m_pInstrumentListView->refresh();
209    
210            stabilizeForm();
211  }  }
212    
213  void InstrumentListForm::editInstrument() {  
214          const QModelIndex index = ui.InstrumentTable->currentIndex();  void InstrumentListForm::editInstrument (void)
215          editInstrument(index);  {
216            editInstrument(m_pInstrumentListView->currentIndex());
217  }  }
218    
219  void InstrumentListForm::editInstrument(const QModelIndex& index) {  
220          if (!index.isValid() || !index.data(Qt::UserRole).isValid())  void InstrumentListForm::editInstrument ( const QModelIndex& index )
221    {
222            if (!index.isValid())
223                  return;                  return;
224    
225          qsamplerInstrument* pInstrument =          Instrument *pInstrument
226                  (qsamplerInstrument*) index.data(Qt::UserRole).value<void*>();                  = static_cast<Instrument *> (index.internalPointer());
227            if (pInstrument == NULL)
228                    return;
229    
230          if (!pInstrument) return;          if (pInstrument == NULL)
231                    return;
232    
233          // Save current key values...          // Save current key values...
234          qsamplerInstrument oldInstrument(*pInstrument);          Instrument oldInstrument(
235                    pInstrument->map(),
236                    pInstrument->bank(),
237                    pInstrument->prog());
238    
239          // Do the edit dance...          // Do the edit dance...
240          InstrumentForm form(this);          InstrumentForm form(this);
241          form.setup(pInstrument);          form.setup(pInstrument);
# Line 196  void InstrumentListForm::editInstrument( Line 246  void InstrumentListForm::editInstrument(
246                  if (oldInstrument.map()  == pInstrument->map()  &&                  if (oldInstrument.map()  == pInstrument->map()  &&
247                          oldInstrument.bank() == pInstrument->bank() &&                          oldInstrument.bank() == pInstrument->bank() &&
248                          oldInstrument.prog() == pInstrument->prog()) {                          oldInstrument.prog() == pInstrument->prog()) {
249                          // just update tree item...                          // Just update tree item...
250                          //pItem->update();                          //pItem->update();
251                  } else {                  } else {
252                          // Unmap old instance...                          // Unmap old instance...
253                          oldInstrument.unmapInstrument();                          oldInstrument.unmapInstrument();
254                          // correct the position of the instrument in the model                          // correct the position of the instrument in the model
255                          model.resort(*pInstrument);                          m_pInstrumentListView->updateInstrument(pInstrument);
256                  }                  }
                 // Notify we've changes...  
                 emit model.reset();  
257          }          }
258  }  }
259    
260  void InstrumentListForm::newInstrument() {  
261          qsamplerInstrument instrument;  void InstrumentListForm::newInstrument (void)
262    {
263            Instrument instrument;
264    
265          InstrumentForm form(this);          InstrumentForm form(this);
266          form.setup(&instrument);          form.setup(&instrument);
267          if (!form.exec()) return;          if (!form.exec())
268                    return;
269    
270          // Commit...          // Commit...
271          instrument.mapInstrument();          instrument.mapInstrument();
272    
273          // add new item to the table model          // add new item to the table model
274          model.resort(instrument);          m_pInstrumentListView->addInstrument(
275          // Notify we've changes...                  instrument.map(),
276          //emit model.reset();                  instrument.bank(),
277          //FIXME: call above didnt really refresh, so we use this for now ...                  instrument.prog());
         refreshInstruments();  
278  }  }
279    
 void InstrumentListForm::deleteInstrument() {  
         const QModelIndex index = ui.InstrumentTable->currentIndex();  
         if (!index.isValid() || !index.data(Qt::UserRole).isValid()) return;  
280    
281          qsamplerInstrument* pInstrument =  void InstrumentListForm::deleteInstrument (void)
282                  (qsamplerInstrument*) index.data(Qt::UserRole).value<void*>();  {
283            const QModelIndex& index = m_pInstrumentListView->currentIndex();
284            if (!index.isValid())
285                    return;
286    
287            Instrument *pInstrument
288                    = static_cast<Instrument *> (index.internalPointer());
289            if (pInstrument == NULL)
290                    return;
291    
292            MainForm *pMainForm = MainForm::getInstance();
293            if (pMainForm == NULL)
294                    return;
295    
296          if (!pInstrument) return;          // Prompt user if this is for real...
297            Options *pOptions = pMainForm->options();
298            if (pOptions && pOptions->bConfirmRemove) {
299                    if (QMessageBox::warning(this,
300                            QSAMPLER_TITLE ": " + tr("Warning"),
301                            tr("About to delete instrument map entry:\n\n"
302                            "%1\n\n"
303                            "Are you sure?")
304                            .arg(pInstrument->name()),
305                            QMessageBox::Ok | QMessageBox::Cancel)
306                            == QMessageBox::Cancel)
307                            return;
308            }
309    
310          pInstrument->unmapInstrument();          pInstrument->unmapInstrument();
311    
312          // let the instrument vanish from the table model          // let the instrument vanish from the table model
313          model.removeInstrument(*pInstrument);          m_pInstrumentListView->removeInstrument(pInstrument);
314          // Notify we've changes...  }
315          emit model.reset();  
316    
317    // Update form actions enablement...
318    void InstrumentListForm::stabilizeForm (void)
319    {
320            MainForm *pMainForm = MainForm::getInstance();
321    
322            bool bEnabled = (pMainForm && pMainForm->client());
323            m_ui.newInstrumentAction->setEnabled(bEnabled);
324            const QModelIndex& index = m_pInstrumentListView->currentIndex();
325            bEnabled = (bEnabled && index.isValid());
326            m_ui.editInstrumentAction->setEnabled(bEnabled);
327            m_ui.deleteInstrumentAction->setEnabled(bEnabled);
328  }  }
329    
330    
331    // Handle custom context menu here...
332    void InstrumentListForm::contextMenu ( const QPoint& pos )
333    {
334            if (!m_ui.newInstrumentAction->isEnabled())
335                    return;
336    
337            m_ui.contextMenu->exec(
338                    (m_pInstrumentListView->viewport())->mapToGlobal(pos));
339    }
340    
341    
342  } // namespace QSampler  } // namespace QSampler
343    
344    

Legend:
Removed from v.1499  
changed lines
  Added in v.2064

  ViewVC Help
Powered by ViewVC