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