1
// qsamplerInstrumentForm.cpp
3
/****************************************************************************
4
Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved.
5
Copyright (C) 2007, Christian Schoenebeck
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.
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.
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.
21
*****************************************************************************/
23
#include "qsamplerAbout.h"
24
#include "qsamplerInstrumentForm.h"
26
#include "qsamplerOptions.h"
27
#include "qsamplerChannel.h"
28
#include "qsamplerMainForm.h"
30
#include <QFileDialog>
31
#include <QMessageBox>
33
// Needed for lroundf()
37
static inline long lroundf ( float x )
40
return long(x + 0.5f);
42
return long(x - 0.5f);
49
//-------------------------------------------------------------------------
50
// QSampler::InstrumentForm -- Instrument map item form implementation.
53
InstrumentForm::InstrumentForm ( QWidget* pParent )
65
// Try to restore normal window positioning.
69
QObject::connect(m_ui.MapComboBox,
70
SIGNAL(activated(int)),
72
QObject::connect(m_ui.BankSpinBox,
73
SIGNAL(valueChanged(int)),
75
QObject::connect(m_ui.ProgSpinBox,
76
SIGNAL(valueChanged(int)),
78
QObject::connect(m_ui.NameLineEdit,
79
SIGNAL(textChanged(const QString&)),
80
SLOT(nameChanged(const QString&)));
81
QObject::connect(m_ui.EngineNameComboBox,
82
SIGNAL(activated(int)),
84
QObject::connect(m_ui.InstrumentFileComboBox,
85
SIGNAL(activated(const QString&)),
86
SLOT(updateInstrumentName()));
87
QObject::connect(m_ui.InstrumentFileToolButton,
89
SLOT(openInstrumentFile()));
90
QObject::connect(m_ui.InstrumentNrComboBox,
91
SIGNAL(activated(int)),
92
SLOT(instrumentNrChanged()));
93
QObject::connect(m_ui.VolumeSpinBox,
94
SIGNAL(valueChanged(int)),
96
QObject::connect(m_ui.LoadModeComboBox,
97
SIGNAL(activated(int)),
99
QObject::connect(m_ui.OkPushButton,
102
QObject::connect(m_ui.CancelPushButton,
108
InstrumentForm::~InstrumentForm (void)
113
// Channel dialog setup formal initializer.
114
void InstrumentForm::setup ( Instrument *pInstrument )
116
m_pInstrument = pInstrument;
122
if (m_pInstrument == NULL)
125
// Check if we're up and connected.
126
MainForm* pMainForm = MainForm::getInstance();
127
if (pMainForm == NULL)
129
if (pMainForm->client() == NULL)
132
Options *pOptions = pMainForm->options();
133
if (pOptions == NULL)
136
// It can be a brand new channel, remember?
137
bool bNew = (m_pInstrument->bank() < 0 || m_pInstrument->prog() < 0);
139
m_pInstrument->getInstrument();
143
// Avoid nested changes.
146
// Load combo box history...
147
pOptions->loadComboBoxHistory(m_ui.InstrumentFileComboBox);
149
// Populate maps list.
150
m_ui.MapComboBox->clear();
151
m_ui.MapComboBox->insertItems(0, Instrument::getMapNames());
153
// Populate Engines list.
154
const char **ppszEngines
155
= ::lscp_list_available_engines(pMainForm->client());
157
m_ui.EngineNameComboBox->clear();
158
for (int iEngine = 0; ppszEngines[iEngine]; iEngine++)
159
m_ui.EngineNameComboBox->addItem(ppszEngines[iEngine]);
161
else pMainForm->appendMessagesClient("lscp_list_available_engines");
163
// Read proper instrument information,
164
// and populate the instrument form fields.
166
// Instrument map name...
167
int iMap = (bNew ? pOptions->iMidiMap : m_pInstrument->map());
170
const QString& sMapName = Instrument::getMapName(iMap);
171
if (!sMapName.isEmpty()) {
172
m_ui.MapComboBox->setCurrentIndex(
173
m_ui.MapComboBox->findText(sMapName,
174
Qt::MatchExactly | Qt::MatchCaseSensitive));
177
// It might be no maps around...
178
bool bMapEnabled = (m_ui.MapComboBox->count() > 0);
179
m_ui.MapTextLabel->setEnabled(bMapEnabled);
180
m_ui.MapComboBox->setEnabled(bMapEnabled);
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) {
189
m_ui.BankSpinBox->setValue(iBank);
190
m_ui.ProgSpinBox->setValue(iProg);
192
// Instrument name...
193
m_ui.NameLineEdit->setText(m_pInstrument->name());
196
QString sEngineName = m_pInstrument->engineName();
197
if (sEngineName.isEmpty() || bNew)
198
sEngineName = pOptions->sEngineName;
199
if (sEngineName.isEmpty())
200
sEngineName = Channel::noEngineName();
201
if (m_ui.EngineNameComboBox->findText(sEngineName,
202
Qt::MatchExactly | Qt::MatchCaseSensitive) < 0) {
203
m_ui.EngineNameComboBox->addItem(sEngineName);
205
m_ui.EngineNameComboBox->setCurrentIndex(
206
m_ui.EngineNameComboBox->findText(sEngineName,
207
Qt::MatchExactly | Qt::MatchCaseSensitive));
209
// Instrument filename and index...
210
QString sInstrumentFile = m_pInstrument->instrumentFile();
211
if (sInstrumentFile.isEmpty())
212
sInstrumentFile = Channel::noInstrumentName();
213
m_ui.InstrumentFileComboBox->setEditText(sInstrumentFile);
214
m_ui.InstrumentNrComboBox->clear();
215
m_ui.InstrumentNrComboBox->insertItems(0,
216
Channel::getInstrumentList(sInstrumentFile,
217
pOptions->bInstrumentNames));
218
m_ui.InstrumentNrComboBox->setCurrentIndex(m_pInstrument->instrumentNr());
220
// Instrument volume....
221
int iVolume = (bNew ? pOptions->iVolume :
222
::lroundf(100.0f * m_pInstrument->volume()));
223
m_ui.VolumeSpinBox->setValue(iVolume);
225
// Instrument load mode...
226
int iLoadMode = (bNew ? pOptions->iLoadMode :
227
m_pInstrument->loadMode());
228
m_ui.LoadModeComboBox->setCurrentIndex(iLoadMode);
240
// Special case for name change,
241
void InstrumentForm::nameChanged ( const QString& /* sName */ )
243
if (m_iDirtySetup > 0)
251
// Browse and open an instrument file.
252
void InstrumentForm::openInstrumentFile (void)
254
MainForm* pMainForm = MainForm::getInstance();
255
if (pMainForm == NULL)
258
Options *pOptions = pMainForm->options();
259
if (pOptions == NULL)
262
// FIXME: the instrument file filters should be restricted,
263
// depending on the current engine.
264
QString sInstrumentFile = QFileDialog::getOpenFileName(this,
265
QSAMPLER_TITLE ": " + tr("Instrument files"), // Caption.
266
pOptions->sInstrumentDir, // Start here.
267
tr("Instrument files") + " (*.gig *.dls)" // Filter (GIG and DLS files)
270
if (sInstrumentFile.isEmpty())
273
m_ui.InstrumentFileComboBox->setEditText(sInstrumentFile);
274
updateInstrumentName();
278
// Refresh the actual instrument name.
279
void InstrumentForm::updateInstrumentName (void)
281
MainForm* pMainForm = MainForm::getInstance();
282
if (pMainForm == NULL)
285
Options *pOptions = pMainForm->options();
286
if (pOptions == NULL)
289
// TODO: this better idea would be to use libgig
290
// to retrieve the REAL instrument names.
291
m_ui.InstrumentNrComboBox->clear();
292
m_ui.InstrumentNrComboBox->insertItems(0,
293
Channel::getInstrumentList(
294
m_ui.InstrumentFileComboBox->currentText(),
295
pOptions->bInstrumentNames)
298
instrumentNrChanged();
302
// Special case for instrumnet index change,
303
void InstrumentForm::instrumentNrChanged (void)
305
if (m_iDirtySetup > 0)
308
if (m_ui.NameLineEdit->text().isEmpty() || m_iDirtyName == 0) {
309
m_ui.NameLineEdit->setText(m_ui.InstrumentNrComboBox->currentText());
317
// Accept settings (OK button slot).
318
void InstrumentForm::accept (void)
320
if (m_pInstrument == NULL)
323
MainForm* pMainForm = MainForm::getInstance();
324
if (pMainForm == NULL)
326
if (pMainForm->client() == NULL)
329
Options *pOptions = pMainForm->options();
330
if (pOptions == NULL)
333
if (m_iDirtyCount > 0) {
334
m_pInstrument->setMap(m_ui.MapComboBox->currentIndex());
335
m_pInstrument->setBank(m_ui.BankSpinBox->value());
336
m_pInstrument->setProg(m_ui.ProgSpinBox->value() - 1);
337
m_pInstrument->setName(m_ui.NameLineEdit->text());
338
m_pInstrument->setEngineName(m_ui.EngineNameComboBox->currentText());
339
m_pInstrument->setInstrumentFile(m_ui.InstrumentFileComboBox->currentText());
340
m_pInstrument->setInstrumentNr(m_ui.InstrumentNrComboBox->currentIndex());
341
m_pInstrument->setVolume(0.01f * float(m_ui.VolumeSpinBox->value()));
342
m_pInstrument->setLoadMode(m_ui.LoadModeComboBox->currentIndex());
345
// Save default engine name, instrument directory and history...
346
pOptions->sInstrumentDir = QFileInfo(
347
m_ui.InstrumentFileComboBox->currentText()).dir().absolutePath();
348
pOptions->sEngineName = m_ui.EngineNameComboBox->currentText();
349
pOptions->iMidiMap = m_ui.MapComboBox->currentIndex();
350
pOptions->iMidiBank = m_ui.BankSpinBox->value();
351
pOptions->iMidiProg = m_ui.ProgSpinBox->value();
352
pOptions->iVolume = m_ui.VolumeSpinBox->value();
353
pOptions->iLoadMode = m_ui.LoadModeComboBox->currentIndex();
354
pOptions->saveComboBoxHistory(m_ui.InstrumentFileComboBox);
356
// Just go with dialog acceptance.
361
// Reject settings (Cancel button slot).
362
void InstrumentForm::reject (void)
366
// Check if there's any pending changes...
367
if (m_iDirtyCount > 0 && m_ui.OkPushButton->isEnabled()) {
368
switch (QMessageBox::warning(this,
369
QSAMPLER_TITLE ": " + tr("Warning"),
370
tr("Some channel settings have been changed.\n\n"
371
"Do you want to apply the changes?"),
372
tr("Apply"), tr("Discard"), tr("Cancel"))) {
389
// Dirty up settings.
390
void InstrumentForm::changed (void)
392
if (m_iDirtySetup > 0)
400
// Stabilize current form state.
401
void InstrumentForm::stabilizeForm (void)
404
!m_ui.NameLineEdit->text().isEmpty() &&
405
m_ui.EngineNameComboBox->currentIndex() >= 0 &&
406
m_ui.EngineNameComboBox->currentText() !=
407
Channel::noEngineName();
409
const QString& sPath = m_ui.InstrumentFileComboBox->currentText();
410
bValid = bValid && !sPath.isEmpty() && QFileInfo(sPath).exists();
412
m_ui.OkPushButton->setEnabled(m_iDirtyCount > 0 && bValid);
415
} // namespace QSampler
418
// end of qsamplerInstrumentForm.cpp