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