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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1528 - (hide annotations) (download)
Mon Nov 26 18:24:38 2007 UTC (12 years, 10 months ago) by capela
File size: 9418 byte(s)
* Qt4 migration: instrument map context menu is back in business.

1 capela 1464 // qsamplerInstrumentListForm.cpp
2     //
3     /****************************************************************************
4     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
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20    
21     *****************************************************************************/
22    
23 capela 1513 #include "qsamplerAbout.h"
24 schoenebeck 1461 #include "qsamplerInstrumentListForm.h"
25    
26 schoenebeck 1492 #include "qsamplerInstrumentForm.h"
27 capela 1513
28 schoenebeck 1461 #include "qsamplerOptions.h"
29     #include "qsamplerInstrument.h"
30 capela 1513 #include "qsamplerMainForm.h"
31 schoenebeck 1461
32 capela 1519 #include <QHeaderView>
33 capela 1528 #include <QMessageBox>
34 schoenebeck 1461
35 capela 1519
36 schoenebeck 1461 namespace QSampler {
37    
38 capela 1504 InstrumentListForm::InstrumentListForm (
39     QWidget* pParent, Qt::WindowFlags wflags )
40     : QMainWindow(pParent, wflags)
41 capela 1473 {
42 capela 1509 m_ui.setupUi(this);
43 schoenebeck 1461
44 capela 1509 // Setup toolbar widgets.
45     m_pMapComboBox = new QComboBox(m_ui.InstrumentToolbar);
46     m_pMapComboBox->setMinimumWidth(120);
47     m_pMapComboBox->setEnabled(false);
48     m_pMapComboBox->setToolTip(tr("Instrument Map"));
49     m_ui.InstrumentToolbar->addWidget(m_pMapComboBox);
50 schoenebeck 1461
51 capela 1509 m_ui.InstrumentToolbar->addSeparator();
52     m_ui.InstrumentToolbar->addAction(m_ui.newInstrumentAction);
53     m_ui.InstrumentToolbar->addAction(m_ui.editInstrumentAction);
54     m_ui.InstrumentToolbar->addAction(m_ui.deleteInstrumentAction);
55     m_ui.InstrumentToolbar->addSeparator();
56     m_ui.InstrumentToolbar->addAction(m_ui.refreshInstrumentsAction);
57 schoenebeck 1461
58 capela 1519 int iRowHeight = m_ui.InstrumentTable->fontMetrics().height() + 4;
59     m_ui.InstrumentTable->verticalHeader()->setDefaultSectionSize(iRowHeight);
60    
61 capela 1509 m_ui.InstrumentTable->setModel(&m_model);
62     m_ui.InstrumentTable->setItemDelegate(&m_delegate);
63 capela 1519 m_ui.InstrumentTable->verticalHeader()->hide();
64 schoenebeck 1461
65 capela 1526 QHeaderView *pHeader = m_ui.InstrumentTable->horizontalHeader();
66     pHeader->setDefaultAlignment(Qt::AlignLeft);
67     pHeader->setMovable(false);
68     pHeader->setStretchLastSection(true);
69     pHeader->resizeSection(0, 120); // Name
70     m_ui.InstrumentTable->resizeColumnToContents(1); // Map
71     m_ui.InstrumentTable->resizeColumnToContents(2); // Bank
72     m_ui.InstrumentTable->resizeColumnToContents(3); // Prog
73     m_ui.InstrumentTable->resizeColumnToContents(4); // Engine
74     pHeader->resizeSection(5, 240); // File
75     m_ui.InstrumentTable->resizeColumnToContents(6); // Nr
76 capela 1528 pHeader->resizeSection(7, 60); // Vol
77 capela 1526
78 capela 1528 // Enable custom context menu...
79     m_ui.InstrumentTable->setContextMenuPolicy(Qt::CustomContextMenu);
80    
81 schoenebeck 1461 QObject::connect(m_pMapComboBox,
82     SIGNAL(activated(int)),
83     SLOT(activateMap(int)));
84 capela 1466 QObject::connect(
85 capela 1528 m_ui.InstrumentTable,
86     SIGNAL(customContextMenuRequested(const QPoint&)),
87     SLOT(contextMenu(const QPoint&)));
88 capela 1504 QObject::connect(
89 capela 1509 m_ui.InstrumentTable,
90 capela 1528 SIGNAL(pressed(const QModelIndex&)),
91     SLOT(stabilizeForm()));
92     QObject::connect(
93     m_ui.InstrumentTable,
94 schoenebeck 1492 SIGNAL(activated(const QModelIndex&)),
95 capela 1509 SLOT(editInstrument(const QModelIndex&)));
96 capela 1504 QObject::connect(
97 capela 1509 m_ui.newInstrumentAction,
98 schoenebeck 1492 SIGNAL(triggered()),
99 capela 1509 SLOT(newInstrument()));
100 capela 1504 QObject::connect(
101 capela 1509 m_ui.deleteInstrumentAction,
102 schoenebeck 1492 SIGNAL(triggered()),
103 capela 1509 SLOT(deleteInstrument()));
104 capela 1504 QObject::connect(
105 capela 1509 m_ui.editInstrumentAction,
106 schoenebeck 1492 SIGNAL(triggered()),
107 capela 1509 SLOT(editInstrument()));
108 capela 1528 QObject::connect(
109     m_ui.refreshInstrumentsAction,
110     SIGNAL(triggered()),
111     SLOT(refreshInstruments()));
112 capela 1509
113     MainForm *pMainForm = MainForm::getInstance();
114     if (pMainForm) {
115     QObject::connect(&m_model,
116     SIGNAL(instrumentsChanged()),
117     pMainForm, SLOT(sessionDirty()));
118     }
119 capela 1528
120     // Things must be stable from the start.
121     stabilizeForm();
122 schoenebeck 1461 }
123    
124 capela 1504
125     InstrumentListForm::~InstrumentListForm (void)
126     {
127 schoenebeck 1461 delete m_pMapComboBox;
128     }
129    
130    
131     // Notify our parent that we're emerging.
132     void InstrumentListForm::showEvent ( QShowEvent *pShowEvent )
133     {
134 capela 1504 MainForm* pMainForm = MainForm::getInstance();
135     if (pMainForm)
136     pMainForm->stabilizeForm();
137 schoenebeck 1461
138     QWidget::showEvent(pShowEvent);
139     }
140    
141    
142     // Notify our parent that we're closing.
143     void InstrumentListForm::hideEvent ( QHideEvent *pHideEvent )
144     {
145     QWidget::hideEvent(pHideEvent);
146    
147 capela 1504 MainForm* pMainForm = MainForm::getInstance();
148     if (pMainForm)
149     pMainForm->stabilizeForm();
150 schoenebeck 1461 }
151    
152    
153 capela 1504 // Just about to notify main-window that we're closing.
154     void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ )
155     {
156     QWidget::hide();
157    
158     MainForm *pMainForm = MainForm::getInstance();
159     if (pMainForm)
160     pMainForm->stabilizeForm();
161     }
162    
163    
164 schoenebeck 1461 // Refresh all instrument list and views.
165     void InstrumentListForm::refreshInstruments (void)
166     {
167     MainForm* pMainForm = MainForm::getInstance();
168     if (pMainForm == NULL)
169     return;
170    
171     qsamplerOptions *pOptions = pMainForm->options();
172     if (pOptions == NULL)
173     return;
174    
175     // Get/save current map selection...
176 capela 1499 int iMap = m_pMapComboBox->currentIndex();
177 schoenebeck 1461 if (iMap < 0 || m_pMapComboBox->count() < 2)
178     iMap = pOptions->iMidiMap + 1;
179    
180     // Populate maps list.
181     m_pMapComboBox->clear();
182 capela 1499 m_pMapComboBox->addItem(tr("(All)"));
183     m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames());
184 schoenebeck 1461
185     // Adjust to saved selection...
186     if (iMap < 0 || iMap >= m_pMapComboBox->count())
187     iMap = 0;
188 capela 1499 m_pMapComboBox->setCurrentIndex(iMap);
189 schoenebeck 1461 m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1);
190    
191     activateMap(iMap);
192     }
193    
194    
195     // Refresh instrument maps selector.
196     void InstrumentListForm::activateMap ( int iMap )
197     {
198     MainForm* pMainForm = MainForm::getInstance();
199     if (pMainForm == NULL)
200     return;
201    
202     qsamplerOptions *pOptions = pMainForm->options();
203     if (pOptions == NULL)
204     return;
205    
206     int iMidiMap = iMap - 1;
207     if (iMidiMap >= 0)
208     pOptions->iMidiMap = iMidiMap;
209    
210 capela 1509 m_model.setMidiMap(iMidiMap);
211     m_model.refresh();
212 capela 1528
213     stabilizeForm();
214 schoenebeck 1461 }
215    
216 capela 1504
217     void InstrumentListForm::editInstrument (void)
218     {
219 capela 1509 editInstrument(m_ui.InstrumentTable->currentIndex());
220 schoenebeck 1492 }
221    
222 capela 1504
223     void InstrumentListForm::editInstrument ( const QModelIndex& index )
224     {
225 schoenebeck 1492 if (!index.isValid() || !index.data(Qt::UserRole).isValid())
226     return;
227    
228 capela 1504 qsamplerInstrument* pInstrument
229     = static_cast<qsamplerInstrument *> (
230     index.data(Qt::UserRole).value<void *> ());
231 schoenebeck 1492
232 capela 1504 if (pInstrument == NULL)
233     return;
234 schoenebeck 1492
235     // Save current key values...
236     qsamplerInstrument oldInstrument(*pInstrument);
237     // Do the edit dance...
238     InstrumentForm form(this);
239     form.setup(pInstrument);
240     if (form.exec()) {
241     // Commit...
242     pInstrument->mapInstrument();
243     // Check whether we changed instrument key...
244     if (oldInstrument.map() == pInstrument->map() &&
245     oldInstrument.bank() == pInstrument->bank() &&
246     oldInstrument.prog() == pInstrument->prog()) {
247 capela 1523 // Just update tree item...
248 schoenebeck 1492 //pItem->update();
249     } else {
250     // Unmap old instance...
251     oldInstrument.unmapInstrument();
252     // correct the position of the instrument in the model
253 capela 1509 m_model.resort(*pInstrument);
254 schoenebeck 1492 }
255     // Notify we've changes...
256 capela 1509 emit m_model.reset();
257 schoenebeck 1492 }
258     }
259    
260 capela 1504
261     void InstrumentListForm::newInstrument (void)
262     {
263 schoenebeck 1492 qsamplerInstrument instrument;
264    
265     InstrumentForm form(this);
266     form.setup(&instrument);
267 capela 1504 if (!form.exec())
268     return;
269 schoenebeck 1492
270     // Commit...
271     instrument.mapInstrument();
272     // add new item to the table model
273 capela 1509 m_model.resort(instrument);
274 schoenebeck 1492 // Notify we've changes...
275     //emit model.reset();
276     //FIXME: call above didnt really refresh, so we use this for now ...
277     refreshInstruments();
278     }
279    
280    
281 capela 1504 void InstrumentListForm::deleteInstrument (void)
282     {
283 capela 1509 const QModelIndex& index = m_ui.InstrumentTable->currentIndex();
284 capela 1504 if (!index.isValid() || !index.data(Qt::UserRole).isValid())
285     return;
286    
287 capela 1528 qsamplerInstrument *pInstrument =
288 capela 1504 static_cast<qsamplerInstrument*> (
289     index.data(Qt::UserRole).value<void *> ());
290 schoenebeck 1492
291 capela 1504 if (pInstrument == NULL)
292     return;
293 schoenebeck 1492
294 capela 1528 MainForm *pMainForm = MainForm::getInstance();
295     if (pMainForm == NULL)
296     return;
297    
298     // Prompt user if this is for real...
299     qsamplerOptions *pOptions = pMainForm->options();
300     if (pOptions && pOptions->bConfirmRemove) {
301     if (QMessageBox::warning(this,
302     QSAMPLER_TITLE ": " + tr("Warning"),
303     tr("About to delete instrument map entry:\n\n"
304     "%1\n\n"
305     "Are you sure?")
306     .arg(pInstrument->name()),
307     tr("OK"), tr("Cancel")) > 0)
308     return;
309     }
310    
311 schoenebeck 1492 pInstrument->unmapInstrument();
312     // let the instrument vanish from the table model
313 capela 1509 m_model.removeInstrument(*pInstrument);
314 schoenebeck 1492 // Notify we've changes...
315 capela 1509 emit m_model.reset();
316 schoenebeck 1492 }
317    
318 capela 1528
319     // Update form actions enablement...
320     void InstrumentListForm::stabilizeForm (void)
321     {
322     MainForm *pMainForm = MainForm::getInstance();
323    
324     bool bEnabled = (pMainForm && pMainForm->client());
325     m_ui.newInstrumentAction->setEnabled(bEnabled);
326     const QModelIndex& index = m_ui.InstrumentTable->currentIndex();
327     bEnabled = (bEnabled && index.isValid());
328     m_ui.editInstrumentAction->setEnabled(bEnabled);
329     m_ui.deleteInstrumentAction->setEnabled(bEnabled);
330     }
331    
332    
333     // Handle custom context menu here...
334     void InstrumentListForm::contextMenu ( const QPoint& pos )
335     {
336     if (!m_ui.newInstrumentAction->isEnabled())
337     return;
338    
339     m_ui.contextMenu->exec(
340     (m_ui.InstrumentTable->viewport())->mapToGlobal(pos));
341     }
342    
343    
344 schoenebeck 1461 } // namespace QSampler
345 capela 1464
346    
347     // end of qsamplerInstrumentListForm.cpp

  ViewVC Help
Powered by ViewVC