1 |
// 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 |
#include "qsamplerAbout.h" |
24 |
#include "qsamplerInstrumentListForm.h" |
25 |
|
26 |
#include "qsamplerInstrumentForm.h" |
27 |
|
28 |
#include "qsamplerOptions.h" |
29 |
#include "qsamplerInstrument.h" |
30 |
#include "qsamplerMainForm.h" |
31 |
|
32 |
|
33 |
namespace QSampler { |
34 |
|
35 |
InstrumentListForm::InstrumentListForm ( |
36 |
QWidget* pParent, Qt::WindowFlags wflags ) |
37 |
: QMainWindow(pParent, wflags) |
38 |
{ |
39 |
m_ui.setupUi(this); |
40 |
|
41 |
// Setup toolbar widgets. |
42 |
m_pMapComboBox = new QComboBox(m_ui.InstrumentToolbar); |
43 |
m_pMapComboBox->setMinimumWidth(120); |
44 |
m_pMapComboBox->setEnabled(false); |
45 |
m_pMapComboBox->setToolTip(tr("Instrument Map")); |
46 |
m_ui.InstrumentToolbar->addWidget(m_pMapComboBox); |
47 |
|
48 |
m_ui.InstrumentToolbar->addSeparator(); |
49 |
m_ui.InstrumentToolbar->addAction(m_ui.newInstrumentAction); |
50 |
m_ui.InstrumentToolbar->addAction(m_ui.editInstrumentAction); |
51 |
m_ui.InstrumentToolbar->addAction(m_ui.deleteInstrumentAction); |
52 |
m_ui.InstrumentToolbar->addSeparator(); |
53 |
m_ui.InstrumentToolbar->addAction(m_ui.refreshInstrumentsAction); |
54 |
|
55 |
m_ui.InstrumentTable->setModel(&m_model); |
56 |
m_ui.InstrumentTable->setItemDelegate(&m_delegate); |
57 |
|
58 |
QObject::connect(m_pMapComboBox, |
59 |
SIGNAL(activated(int)), |
60 |
SLOT(activateMap(int))); |
61 |
QObject::connect( |
62 |
m_ui.refreshInstrumentsAction, |
63 |
SIGNAL(triggered()), |
64 |
SLOT(refreshInstruments(void))); |
65 |
QObject::connect( |
66 |
m_ui.InstrumentTable, |
67 |
SIGNAL(activated(const QModelIndex&)), |
68 |
SLOT(editInstrument(const QModelIndex&))); |
69 |
QObject::connect( |
70 |
m_ui.newInstrumentAction, |
71 |
SIGNAL(triggered()), |
72 |
SLOT(newInstrument())); |
73 |
QObject::connect( |
74 |
m_ui.deleteInstrumentAction, |
75 |
SIGNAL(triggered()), |
76 |
SLOT(deleteInstrument())); |
77 |
QObject::connect( |
78 |
m_ui.editInstrumentAction, |
79 |
SIGNAL(triggered()), |
80 |
SLOT(editInstrument())); |
81 |
|
82 |
MainForm *pMainForm = MainForm::getInstance(); |
83 |
if (pMainForm) { |
84 |
QObject::connect(&m_model, |
85 |
SIGNAL(instrumentsChanged()), |
86 |
pMainForm, SLOT(sessionDirty())); |
87 |
} |
88 |
} |
89 |
|
90 |
|
91 |
InstrumentListForm::~InstrumentListForm (void) |
92 |
{ |
93 |
delete m_pMapComboBox; |
94 |
} |
95 |
|
96 |
|
97 |
// Notify our parent that we're emerging. |
98 |
void InstrumentListForm::showEvent ( QShowEvent *pShowEvent ) |
99 |
{ |
100 |
MainForm* pMainForm = MainForm::getInstance(); |
101 |
if (pMainForm) |
102 |
pMainForm->stabilizeForm(); |
103 |
|
104 |
QWidget::showEvent(pShowEvent); |
105 |
} |
106 |
|
107 |
|
108 |
// Notify our parent that we're closing. |
109 |
void InstrumentListForm::hideEvent ( QHideEvent *pHideEvent ) |
110 |
{ |
111 |
QWidget::hideEvent(pHideEvent); |
112 |
|
113 |
MainForm* pMainForm = MainForm::getInstance(); |
114 |
if (pMainForm) |
115 |
pMainForm->stabilizeForm(); |
116 |
} |
117 |
|
118 |
|
119 |
// Just about to notify main-window that we're closing. |
120 |
void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ ) |
121 |
{ |
122 |
QWidget::hide(); |
123 |
|
124 |
MainForm *pMainForm = MainForm::getInstance(); |
125 |
if (pMainForm) |
126 |
pMainForm->stabilizeForm(); |
127 |
} |
128 |
|
129 |
|
130 |
// Refresh all instrument list and views. |
131 |
void InstrumentListForm::refreshInstruments (void) |
132 |
{ |
133 |
MainForm* pMainForm = MainForm::getInstance(); |
134 |
if (pMainForm == NULL) |
135 |
return; |
136 |
|
137 |
qsamplerOptions *pOptions = pMainForm->options(); |
138 |
if (pOptions == NULL) |
139 |
return; |
140 |
|
141 |
// Get/save current map selection... |
142 |
int iMap = m_pMapComboBox->currentIndex(); |
143 |
if (iMap < 0 || m_pMapComboBox->count() < 2) |
144 |
iMap = pOptions->iMidiMap + 1; |
145 |
|
146 |
// Populate maps list. |
147 |
m_pMapComboBox->clear(); |
148 |
m_pMapComboBox->addItem(tr("(All)")); |
149 |
m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames()); |
150 |
|
151 |
// Adjust to saved selection... |
152 |
if (iMap < 0 || iMap >= m_pMapComboBox->count()) |
153 |
iMap = 0; |
154 |
m_pMapComboBox->setCurrentIndex(iMap); |
155 |
m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1); |
156 |
|
157 |
activateMap(iMap); |
158 |
} |
159 |
|
160 |
|
161 |
// Refresh instrument maps selector. |
162 |
void InstrumentListForm::activateMap ( int iMap ) |
163 |
{ |
164 |
MainForm* pMainForm = MainForm::getInstance(); |
165 |
if (pMainForm == NULL) |
166 |
return; |
167 |
|
168 |
qsamplerOptions *pOptions = pMainForm->options(); |
169 |
if (pOptions == NULL) |
170 |
return; |
171 |
|
172 |
int iMidiMap = iMap - 1; |
173 |
if (iMidiMap >= 0) |
174 |
pOptions->iMidiMap = iMidiMap; |
175 |
|
176 |
m_model.setMidiMap(iMidiMap); |
177 |
m_model.refresh(); |
178 |
} |
179 |
|
180 |
|
181 |
void InstrumentListForm::editInstrument (void) |
182 |
{ |
183 |
editInstrument(m_ui.InstrumentTable->currentIndex()); |
184 |
} |
185 |
|
186 |
|
187 |
void InstrumentListForm::editInstrument ( const QModelIndex& index ) |
188 |
{ |
189 |
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
190 |
return; |
191 |
|
192 |
qsamplerInstrument* pInstrument |
193 |
= static_cast<qsamplerInstrument *> ( |
194 |
index.data(Qt::UserRole).value<void *> ()); |
195 |
|
196 |
if (pInstrument == NULL) |
197 |
return; |
198 |
|
199 |
// Save current key values... |
200 |
qsamplerInstrument oldInstrument(*pInstrument); |
201 |
// Do the edit dance... |
202 |
InstrumentForm form(this); |
203 |
form.setup(pInstrument); |
204 |
if (form.exec()) { |
205 |
// Commit... |
206 |
pInstrument->mapInstrument(); |
207 |
// Check whether we changed instrument key... |
208 |
if (oldInstrument.map() == pInstrument->map() && |
209 |
oldInstrument.bank() == pInstrument->bank() && |
210 |
oldInstrument.prog() == pInstrument->prog()) { |
211 |
// just update tree item... |
212 |
//pItem->update(); |
213 |
} else { |
214 |
// Unmap old instance... |
215 |
oldInstrument.unmapInstrument(); |
216 |
// correct the position of the instrument in the model |
217 |
m_model.resort(*pInstrument); |
218 |
} |
219 |
// Notify we've changes... |
220 |
emit m_model.reset(); |
221 |
} |
222 |
} |
223 |
|
224 |
|
225 |
void InstrumentListForm::newInstrument (void) |
226 |
{ |
227 |
qsamplerInstrument instrument; |
228 |
|
229 |
InstrumentForm form(this); |
230 |
form.setup(&instrument); |
231 |
if (!form.exec()) |
232 |
return; |
233 |
|
234 |
// Commit... |
235 |
instrument.mapInstrument(); |
236 |
// add new item to the table model |
237 |
m_model.resort(instrument); |
238 |
// Notify we've changes... |
239 |
//emit model.reset(); |
240 |
//FIXME: call above didnt really refresh, so we use this for now ... |
241 |
refreshInstruments(); |
242 |
} |
243 |
|
244 |
|
245 |
void InstrumentListForm::deleteInstrument (void) |
246 |
{ |
247 |
const QModelIndex& index = m_ui.InstrumentTable->currentIndex(); |
248 |
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
249 |
return; |
250 |
|
251 |
qsamplerInstrument* pInstrument = |
252 |
static_cast<qsamplerInstrument*> ( |
253 |
index.data(Qt::UserRole).value<void *> ()); |
254 |
|
255 |
if (pInstrument == NULL) |
256 |
return; |
257 |
|
258 |
pInstrument->unmapInstrument(); |
259 |
// let the instrument vanish from the table model |
260 |
m_model.removeInstrument(*pInstrument); |
261 |
// Notify we've changes... |
262 |
emit m_model.reset(); |
263 |
} |
264 |
|
265 |
} // namespace QSampler |
266 |
|
267 |
|
268 |
// end of qsamplerInstrumentListForm.cpp |