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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2066 - (hide annotations) (download)
Sat Mar 13 19:15:24 2010 UTC (10 years, 6 months ago) by capela
File size: 8366 byte(s)
- Still trying to fix instrument list view/model for Qt >= 4.6.

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

  ViewVC Help
Powered by ViewVC