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