1 |
// qsamplerInstrumentForm.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 "qsamplerInstrumentForm.h" |
24 |
|
25 |
#include "qsamplerAbout.h" |
26 |
#include "qsamplerMainForm.h" |
27 |
|
28 |
#include <QFileDialog> |
29 |
#include <QMessageBox> |
30 |
|
31 |
// Needed for lroundf() |
32 |
#include <math.h> |
33 |
|
34 |
|
35 |
namespace QSampler { |
36 |
|
37 |
#ifndef CONFIG_ROUND |
38 |
static inline long lroundf ( float x ) |
39 |
{ |
40 |
if (x >= 0.0f) |
41 |
return long(x + 0.5f); |
42 |
else |
43 |
return long(x - 0.5f); |
44 |
} |
45 |
#endif |
46 |
|
47 |
InstrumentForm::InstrumentForm ( QWidget* pParent ) |
48 |
: QDialog(pParent) |
49 |
{ |
50 |
ui.setupUi(this); |
51 |
|
52 |
// Initialize locals. |
53 |
m_pInstrument = NULL; |
54 |
|
55 |
m_iDirtySetup = 0; |
56 |
m_iDirtyCount = 0; |
57 |
m_iDirtyName = 0; |
58 |
|
59 |
// Try to restore normal window positioning. |
60 |
adjustSize(); |
61 |
|
62 |
|
63 |
QObject::connect(ui.MapComboBox, |
64 |
SIGNAL(activated(int)), |
65 |
SLOT(changed())); |
66 |
QObject::connect(ui.BankSpinBox, |
67 |
SIGNAL(valueChanged(int)), |
68 |
SLOT(changed())); |
69 |
QObject::connect(ui.ProgSpinBox, |
70 |
SIGNAL(valueChanged(int)), |
71 |
SLOT(changed())); |
72 |
QObject::connect(ui.NameLineEdit, |
73 |
SIGNAL(textChanged(const QString&)), |
74 |
SLOT(nameChanged(const QString&))); |
75 |
QObject::connect(ui.EngineNameComboBox, |
76 |
SIGNAL(activated(int)), |
77 |
SLOT(changed())); |
78 |
QObject::connect(ui.InstrumentFileComboBox, |
79 |
SIGNAL(activated(const QString&)), |
80 |
SLOT(updateInstrumentName())); |
81 |
QObject::connect(ui.InstrumentFileToolButton, |
82 |
SIGNAL(clicked()), |
83 |
SLOT(openInstrumentFile())); |
84 |
QObject::connect(ui.InstrumentNrComboBox, |
85 |
SIGNAL(activated(int)), |
86 |
SLOT(instrumentNrChanged())); |
87 |
QObject::connect(ui.VolumeSpinBox, |
88 |
SIGNAL(valueChanged(int)), |
89 |
SLOT(changed())); |
90 |
QObject::connect(ui.LoadModeComboBox, |
91 |
SIGNAL(activated(int)), |
92 |
SLOT(changed())); |
93 |
QObject::connect(ui.OkPushButton, |
94 |
SIGNAL(clicked()), |
95 |
SLOT(accept())); |
96 |
QObject::connect(ui.CancelPushButton, |
97 |
SIGNAL(clicked()), |
98 |
SLOT(reject())); |
99 |
} |
100 |
|
101 |
|
102 |
InstrumentForm::~InstrumentForm (void) |
103 |
{ |
104 |
} |
105 |
|
106 |
|
107 |
// Channel dialog setup formal initializer. |
108 |
void InstrumentForm::setup ( qsamplerInstrument *pInstrument ) |
109 |
{ |
110 |
m_pInstrument = pInstrument; |
111 |
|
112 |
m_iDirtySetup = 0; |
113 |
m_iDirtyCount = 0; |
114 |
m_iDirtyName = 0; |
115 |
|
116 |
if (m_pInstrument == NULL) |
117 |
return; |
118 |
|
119 |
// Check if we're up and connected. |
120 |
MainForm* pMainForm = MainForm::getInstance(); |
121 |
if (pMainForm == NULL) |
122 |
return; |
123 |
if (pMainForm->client() == NULL) |
124 |
return; |
125 |
|
126 |
qsamplerOptions *pOptions = pMainForm->options(); |
127 |
if (pOptions == NULL) |
128 |
return; |
129 |
|
130 |
// It can be a brand new channel, remember? |
131 |
bool bNew = (m_pInstrument->bank() < 0 || m_pInstrument->prog() < 0); |
132 |
if (!bNew) { |
133 |
m_pInstrument->getInstrument(); |
134 |
m_iDirtyName++; |
135 |
} |
136 |
|
137 |
// Avoid nested changes. |
138 |
m_iDirtySetup++; |
139 |
|
140 |
// Load combo box history... |
141 |
pOptions->loadComboBoxHistory(ui.InstrumentFileComboBox); |
142 |
|
143 |
// Populate maps list. |
144 |
ui.MapComboBox->clear(); |
145 |
ui.MapComboBox->insertItems(0, qsamplerInstrument::getMapNames()); |
146 |
|
147 |
// Populate Engines list. |
148 |
const char **ppszEngines = ::lscp_list_available_engines(pMainForm->client()); |
149 |
if (ppszEngines) { |
150 |
ui.EngineNameComboBox->clear(); |
151 |
for (int iEngine = 0; ppszEngines[iEngine]; iEngine++) |
152 |
ui.EngineNameComboBox->addItem(ppszEngines[iEngine]); |
153 |
} |
154 |
else pMainForm->appendMessagesClient("lscp_list_available_engines"); |
155 |
|
156 |
// Read proper instrument information, |
157 |
// and populate the instrument form fields. |
158 |
|
159 |
// Instrument map name... |
160 |
int iMap = (bNew ? pOptions->iMidiMap : m_pInstrument->map()); |
161 |
if (iMap < 0) |
162 |
iMap = 0; |
163 |
const QString& sMapName = qsamplerInstrument::getMapName(iMap); |
164 |
if (!sMapName.isEmpty()) { |
165 |
ui.MapComboBox->setItemText( |
166 |
ui.MapComboBox->currentIndex(), |
167 |
sMapName); |
168 |
} |
169 |
// It might be no maps around... |
170 |
bool bMapEnabled = (ui.MapComboBox->count() > 0); |
171 |
ui.MapTextLabel->setEnabled(bMapEnabled); |
172 |
ui.MapComboBox->setEnabled(bMapEnabled); |
173 |
|
174 |
// Instrument bank/program... |
175 |
int iBank = (bNew ? pOptions->iMidiBank : m_pInstrument->bank()); |
176 |
int iProg = (bNew ? pOptions->iMidiProg : m_pInstrument->prog()) + 1; |
177 |
if (bNew && iProg > 128) { |
178 |
iProg = 1; |
179 |
iBank++; |
180 |
} |
181 |
ui.BankSpinBox->setValue(iBank); |
182 |
ui.ProgSpinBox->setValue(iProg); |
183 |
|
184 |
// Instrument name... |
185 |
ui.NameLineEdit->setText(m_pInstrument->name()); |
186 |
|
187 |
// Engine name... |
188 |
QString sEngineName = m_pInstrument->engineName(); |
189 |
if (sEngineName.isEmpty() || bNew) |
190 |
sEngineName = pOptions->sEngineName; |
191 |
if (sEngineName.isEmpty()) |
192 |
sEngineName = qsamplerChannel::noEngineName(); |
193 |
if (ui.EngineNameComboBox->findText(sEngineName, |
194 |
Qt::MatchExactly | Qt::MatchCaseSensitive) < 0) { |
195 |
ui.EngineNameComboBox->addItem(sEngineName); |
196 |
} |
197 |
ui.EngineNameComboBox->setCurrentIndex( |
198 |
ui.EngineNameComboBox->findText(sEngineName, |
199 |
Qt::MatchExactly | Qt::MatchCaseSensitive)); |
200 |
|
201 |
// Instrument filename and index... |
202 |
QString sInstrumentFile = m_pInstrument->instrumentFile(); |
203 |
if (sInstrumentFile.isEmpty()) |
204 |
sInstrumentFile = qsamplerChannel::noInstrumentName(); |
205 |
ui.InstrumentFileComboBox->setItemText( |
206 |
ui.InstrumentFileComboBox->currentIndex(), |
207 |
sInstrumentFile); |
208 |
ui.InstrumentNrComboBox->clear(); |
209 |
ui.InstrumentNrComboBox->insertItems(0, |
210 |
qsamplerChannel::getInstrumentList(sInstrumentFile, |
211 |
pOptions->bInstrumentNames)); |
212 |
ui.InstrumentNrComboBox->setCurrentIndex(m_pInstrument->instrumentNr()); |
213 |
|
214 |
// Instrument volume.... |
215 |
int iVolume = (bNew ? pOptions->iVolume : |
216 |
::lroundf(100.0f * m_pInstrument->volume())); |
217 |
ui.VolumeSpinBox->setValue(iVolume); |
218 |
|
219 |
// Instrument load mode... |
220 |
int iLoadMode = (bNew ? pOptions->iLoadMode : |
221 |
m_pInstrument->loadMode()); |
222 |
ui.LoadModeComboBox->setCurrentIndex(iLoadMode); |
223 |
|
224 |
// Done. |
225 |
m_iDirtySetup--; |
226 |
stabilizeForm(); |
227 |
|
228 |
// Done. |
229 |
m_iDirtySetup--; |
230 |
stabilizeForm(); |
231 |
} |
232 |
|
233 |
|
234 |
// Special case for name change, |
235 |
void InstrumentForm::nameChanged ( const QString& /* sName */ ) |
236 |
{ |
237 |
if (m_iDirtySetup > 0) |
238 |
return; |
239 |
|
240 |
m_iDirtyName++; |
241 |
changed(); |
242 |
} |
243 |
|
244 |
|
245 |
// Browse and open an instrument file. |
246 |
void InstrumentForm::openInstrumentFile (void) |
247 |
{ |
248 |
MainForm* pMainForm = MainForm::getInstance(); |
249 |
if (pMainForm == NULL) |
250 |
return; |
251 |
|
252 |
qsamplerOptions *pOptions = pMainForm->options(); |
253 |
if (pOptions == NULL) |
254 |
return; |
255 |
|
256 |
// FIXME: the instrument file filters should be restricted, |
257 |
// depending on the current engine. |
258 |
QString sInstrumentFile = QFileDialog::getOpenFileName(this, |
259 |
QSAMPLER_TITLE ": " + tr("Instrument files"), // Caption. |
260 |
pOptions->sInstrumentDir, // Start here. |
261 |
tr("Instrument files") + " (*.gig *.dls)" // Filter (GIG and DLS files) |
262 |
); |
263 |
|
264 |
if (sInstrumentFile.isEmpty()) |
265 |
return; |
266 |
|
267 |
ui.InstrumentFileComboBox->setItemText( |
268 |
ui.InstrumentFileComboBox->currentIndex(), |
269 |
sInstrumentFile); |
270 |
|
271 |
updateInstrumentName(); |
272 |
} |
273 |
|
274 |
|
275 |
// Refresh the actual instrument name. |
276 |
void InstrumentForm::updateInstrumentName (void) |
277 |
{ |
278 |
MainForm* pMainForm = MainForm::getInstance(); |
279 |
if (pMainForm == NULL) |
280 |
return; |
281 |
|
282 |
qsamplerOptions *pOptions = pMainForm->options(); |
283 |
if (pOptions == NULL) |
284 |
return; |
285 |
|
286 |
// TODO: this better idea would be to use libgig |
287 |
// to retrieve the REAL instrument names. |
288 |
ui.InstrumentNrComboBox->clear(); |
289 |
ui.InstrumentNrComboBox->insertItems(0, |
290 |
qsamplerChannel::getInstrumentList( |
291 |
ui.InstrumentFileComboBox->currentText(), |
292 |
pOptions->bInstrumentNames) |
293 |
); |
294 |
|
295 |
instrumentNrChanged(); |
296 |
} |
297 |
|
298 |
|
299 |
// Special case for instrumnet index change, |
300 |
void InstrumentForm::instrumentNrChanged (void) |
301 |
{ |
302 |
if (m_iDirtySetup > 0) |
303 |
return; |
304 |
|
305 |
if (ui.NameLineEdit->text().isEmpty() || m_iDirtyName == 0) { |
306 |
ui.NameLineEdit->setText(ui.InstrumentNrComboBox->currentText()); |
307 |
m_iDirtyName = 0; |
308 |
} |
309 |
|
310 |
changed(); |
311 |
} |
312 |
|
313 |
|
314 |
// Accept settings (OK button slot). |
315 |
void InstrumentForm::accept (void) |
316 |
{ |
317 |
if (m_pInstrument == NULL) |
318 |
return; |
319 |
|
320 |
MainForm* pMainForm = MainForm::getInstance(); |
321 |
if (pMainForm == NULL) |
322 |
return; |
323 |
if (pMainForm->client() == NULL) |
324 |
return; |
325 |
|
326 |
qsamplerOptions *pOptions = pMainForm->options(); |
327 |
if (pOptions == NULL) |
328 |
return; |
329 |
|
330 |
if (m_iDirtyCount > 0) { |
331 |
m_pInstrument->setMap(ui.MapComboBox->currentIndex()); |
332 |
m_pInstrument->setBank(ui.BankSpinBox->value()); |
333 |
m_pInstrument->setProg(ui.ProgSpinBox->value() - 1); |
334 |
m_pInstrument->setName(ui.NameLineEdit->text()); |
335 |
m_pInstrument->setEngineName(ui.EngineNameComboBox->currentText()); |
336 |
m_pInstrument->setInstrumentFile(ui.InstrumentFileComboBox->currentText()); |
337 |
m_pInstrument->setInstrumentNr(ui.InstrumentNrComboBox->currentIndex()); |
338 |
m_pInstrument->setVolume(0.01f * float(ui.VolumeSpinBox->value())); |
339 |
m_pInstrument->setLoadMode(ui.LoadModeComboBox->currentIndex()); |
340 |
} |
341 |
|
342 |
// Save default engine name, instrument directory and history... |
343 |
pOptions->sInstrumentDir = QFileInfo( |
344 |
ui.InstrumentFileComboBox->currentText()).dir().absolutePath(); |
345 |
pOptions->sEngineName = ui.EngineNameComboBox->currentText(); |
346 |
pOptions->iMidiMap = ui.MapComboBox->currentIndex(); |
347 |
pOptions->iMidiBank = ui.BankSpinBox->value(); |
348 |
pOptions->iMidiProg = ui.ProgSpinBox->value(); |
349 |
pOptions->iVolume = ui.VolumeSpinBox->value(); |
350 |
pOptions->iLoadMode = ui.LoadModeComboBox->currentIndex(); |
351 |
pOptions->saveComboBoxHistory(ui.InstrumentFileComboBox); |
352 |
|
353 |
// Just go with dialog acceptance. |
354 |
QDialog::accept(); |
355 |
} |
356 |
|
357 |
|
358 |
// Reject settings (Cancel button slot). |
359 |
void InstrumentForm::reject (void) |
360 |
{ |
361 |
bool bReject = true; |
362 |
|
363 |
// Check if there's any pending changes... |
364 |
if (m_iDirtyCount > 0 && ui.OkPushButton->isEnabled()) { |
365 |
switch (QMessageBox::warning(this, |
366 |
QSAMPLER_TITLE ": " + tr("Warning"), |
367 |
tr("Some channel settings have been changed.\n\n" |
368 |
"Do you want to apply the changes?"), |
369 |
tr("Apply"), tr("Discard"), tr("Cancel"))) { |
370 |
case 0: // Apply... |
371 |
accept(); |
372 |
return; |
373 |
case 1: // Discard |
374 |
break; |
375 |
default: // Cancel. |
376 |
bReject = false; |
377 |
break; |
378 |
} |
379 |
} |
380 |
|
381 |
if (bReject) |
382 |
QDialog::reject(); |
383 |
} |
384 |
|
385 |
|
386 |
// Dirty up settings. |
387 |
void InstrumentForm::changed (void) |
388 |
{ |
389 |
if (m_iDirtySetup > 0) |
390 |
return; |
391 |
|
392 |
m_iDirtyCount++; |
393 |
stabilizeForm(); |
394 |
} |
395 |
|
396 |
|
397 |
// Stabilize current form state. |
398 |
void InstrumentForm::stabilizeForm (void) |
399 |
{ |
400 |
bool bValid = !ui.NameLineEdit->text().isEmpty(); |
401 |
|
402 |
const QString& sPath = ui.InstrumentFileComboBox->currentText(); |
403 |
bValid = bValid && !sPath.isEmpty() && QFileInfo(sPath).exists(); |
404 |
|
405 |
ui.OkPushButton->setEnabled(m_iDirtyCount > 0 && bValid); |
406 |
} |
407 |
|
408 |
} // namespace QSampler |
409 |
|
410 |
|
411 |
// end of qsamplerInstrumentForm.cpp |