/[svn]/qsampler/trunk/src/qsamplerInstrumentForm.cpp
ViewVC logotype

Contents of /qsampler/trunk/src/qsamplerInstrumentForm.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1526 - (show annotations) (download)
Mon Nov 26 10:58:23 2007 UTC (16 years, 4 months ago) by capela
File size: 11199 byte(s)
* Fixed crash on (All) instrument map list; instrument program number
  list display fix (off by one); header section
  widths are now arranged as in the Qt3 version.

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 "qsamplerAbout.h"
24 #include "qsamplerInstrumentForm.h"
25
26 #include "qsamplerOptions.h"
27 #include "qsamplerChannel.h"
28 #include "qsamplerMainForm.h"
29
30 #include <QFileDialog>
31 #include <QMessageBox>
32
33 // 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
47 namespace QSampler {
48
49 InstrumentForm::InstrumentForm ( QWidget* pParent )
50 : QDialog(pParent)
51 {
52 m_ui.setupUi(this);
53
54 // Initialize locals.
55 m_pInstrument = NULL;
56
57 m_iDirtySetup = 0;
58 m_iDirtyCount = 0;
59 m_iDirtyName = 0;
60
61 // Try to restore normal window positioning.
62 adjustSize();
63
64
65 QObject::connect(m_ui.MapComboBox,
66 SIGNAL(activated(int)),
67 SLOT(changed()));
68 QObject::connect(m_ui.BankSpinBox,
69 SIGNAL(valueChanged(int)),
70 SLOT(changed()));
71 QObject::connect(m_ui.ProgSpinBox,
72 SIGNAL(valueChanged(int)),
73 SLOT(changed()));
74 QObject::connect(m_ui.NameLineEdit,
75 SIGNAL(textChanged(const QString&)),
76 SLOT(nameChanged(const QString&)));
77 QObject::connect(m_ui.EngineNameComboBox,
78 SIGNAL(activated(int)),
79 SLOT(changed()));
80 QObject::connect(m_ui.InstrumentFileComboBox,
81 SIGNAL(activated(const QString&)),
82 SLOT(updateInstrumentName()));
83 QObject::connect(m_ui.InstrumentFileToolButton,
84 SIGNAL(clicked()),
85 SLOT(openInstrumentFile()));
86 QObject::connect(m_ui.InstrumentNrComboBox,
87 SIGNAL(activated(int)),
88 SLOT(instrumentNrChanged()));
89 QObject::connect(m_ui.VolumeSpinBox,
90 SIGNAL(valueChanged(int)),
91 SLOT(changed()));
92 QObject::connect(m_ui.LoadModeComboBox,
93 SIGNAL(activated(int)),
94 SLOT(changed()));
95 QObject::connect(m_ui.OkPushButton,
96 SIGNAL(clicked()),
97 SLOT(accept()));
98 QObject::connect(m_ui.CancelPushButton,
99 SIGNAL(clicked()),
100 SLOT(reject()));
101 }
102
103
104 InstrumentForm::~InstrumentForm (void)
105 {
106 }
107
108
109 // Channel dialog setup formal initializer.
110 void InstrumentForm::setup ( qsamplerInstrument *pInstrument )
111 {
112 m_pInstrument = pInstrument;
113
114 m_iDirtySetup = 0;
115 m_iDirtyCount = 0;
116 m_iDirtyName = 0;
117
118 if (m_pInstrument == NULL)
119 return;
120
121 // Check if we're up and connected.
122 MainForm* pMainForm = MainForm::getInstance();
123 if (pMainForm == NULL)
124 return;
125 if (pMainForm->client() == NULL)
126 return;
127
128 qsamplerOptions *pOptions = pMainForm->options();
129 if (pOptions == NULL)
130 return;
131
132 // It can be a brand new channel, remember?
133 bool bNew = (m_pInstrument->bank() < 0 || m_pInstrument->prog() < 0);
134 if (!bNew) {
135 m_pInstrument->getInstrument();
136 m_iDirtyName++;
137 }
138
139 // Avoid nested changes.
140 m_iDirtySetup++;
141
142 // Load combo box history...
143 pOptions->loadComboBoxHistory(m_ui.InstrumentFileComboBox);
144
145 // Populate maps list.
146 m_ui.MapComboBox->clear();
147 m_ui.MapComboBox->insertItems(0, qsamplerInstrument::getMapNames());
148
149 // Populate Engines list.
150 const char **ppszEngines
151 = ::lscp_list_available_engines(pMainForm->client());
152 if (ppszEngines) {
153 m_ui.EngineNameComboBox->clear();
154 for (int iEngine = 0; ppszEngines[iEngine]; iEngine++)
155 m_ui.EngineNameComboBox->addItem(ppszEngines[iEngine]);
156 }
157 else pMainForm->appendMessagesClient("lscp_list_available_engines");
158
159 // Read proper instrument information,
160 // and populate the instrument form fields.
161
162 // Instrument map name...
163 int iMap = (bNew ? pOptions->iMidiMap : m_pInstrument->map());
164 if (iMap < 0)
165 iMap = 0;
166 const QString& sMapName = qsamplerInstrument::getMapName(iMap);
167 if (!sMapName.isEmpty()) {
168 m_ui.MapComboBox->setCurrentIndex(
169 m_ui.MapComboBox->findText(sMapName,
170 Qt::MatchExactly | Qt::MatchCaseSensitive));
171 }
172
173 // It might be no maps around...
174 bool bMapEnabled = (m_ui.MapComboBox->count() > 0);
175 m_ui.MapTextLabel->setEnabled(bMapEnabled);
176 m_ui.MapComboBox->setEnabled(bMapEnabled);
177
178 // Instrument bank/program...
179 int iBank = (bNew ? pOptions->iMidiBank : m_pInstrument->bank());
180 int iProg = (bNew ? pOptions->iMidiProg : m_pInstrument->prog()) + 1;
181 if (bNew && iProg > 128) {
182 iProg = 1;
183 iBank++;
184 }
185 m_ui.BankSpinBox->setValue(iBank);
186 m_ui.ProgSpinBox->setValue(iProg);
187
188 // Instrument name...
189 m_ui.NameLineEdit->setText(m_pInstrument->name());
190
191 // Engine name...
192 QString sEngineName = m_pInstrument->engineName();
193 if (sEngineName.isEmpty() || bNew)
194 sEngineName = pOptions->sEngineName;
195 if (sEngineName.isEmpty())
196 sEngineName = qsamplerChannel::noEngineName();
197 if (m_ui.EngineNameComboBox->findText(sEngineName,
198 Qt::MatchExactly | Qt::MatchCaseSensitive) < 0) {
199 m_ui.EngineNameComboBox->addItem(sEngineName);
200 }
201 m_ui.EngineNameComboBox->setCurrentIndex(
202 m_ui.EngineNameComboBox->findText(sEngineName,
203 Qt::MatchExactly | Qt::MatchCaseSensitive));
204
205 // Instrument filename and index...
206 QString sInstrumentFile = m_pInstrument->instrumentFile();
207 if (sInstrumentFile.isEmpty())
208 sInstrumentFile = qsamplerChannel::noInstrumentName();
209 m_ui.InstrumentFileComboBox->setEditText(sInstrumentFile);
210 m_ui.InstrumentNrComboBox->clear();
211 m_ui.InstrumentNrComboBox->insertItems(0,
212 qsamplerChannel::getInstrumentList(sInstrumentFile,
213 pOptions->bInstrumentNames));
214 m_ui.InstrumentNrComboBox->setCurrentIndex(m_pInstrument->instrumentNr());
215
216 // Instrument volume....
217 int iVolume = (bNew ? pOptions->iVolume :
218 ::lroundf(100.0f * m_pInstrument->volume()));
219 m_ui.VolumeSpinBox->setValue(iVolume);
220
221 // Instrument load mode...
222 int iLoadMode = (bNew ? pOptions->iLoadMode :
223 m_pInstrument->loadMode());
224 m_ui.LoadModeComboBox->setCurrentIndex(iLoadMode);
225
226 // Done.
227 m_iDirtySetup--;
228 stabilizeForm();
229
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 qsamplerOptions *pOptions = pMainForm->options();
255 if (pOptions == NULL)
256 return;
257
258 // FIXME: the instrument file filters should be restricted,
259 // depending on the current engine.
260 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 );
265
266 if (sInstrumentFile.isEmpty())
267 return;
268
269 m_ui.InstrumentFileComboBox->setEditText(sInstrumentFile);
270 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 qsamplerOptions *pOptions = pMainForm->options();
282 if (pOptions == NULL)
283 return;
284
285 // TODO: this better idea would be to use libgig
286 // to retrieve the REAL instrument names.
287 m_ui.InstrumentNrComboBox->clear();
288 m_ui.InstrumentNrComboBox->insertItems(0,
289 qsamplerChannel::getInstrumentList(
290 m_ui.InstrumentFileComboBox->currentText(),
291 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 if (m_ui.NameLineEdit->text().isEmpty() || m_iDirtyName == 0) {
305 m_ui.NameLineEdit->setText(m_ui.InstrumentNrComboBox->currentText());
306 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 qsamplerOptions *pOptions = pMainForm->options();
326 if (pOptions == NULL)
327 return;
328
329 if (m_iDirtyCount > 0) {
330 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 }
340
341 // Save default engine name, instrument directory and history...
342 pOptions->sInstrumentDir = QFileInfo(
343 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
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 if (m_iDirtyCount > 0 && m_ui.OkPushButton->isEnabled()) {
364 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 tr("Apply"), tr("Discard"), tr("Cancel"))) {
369 case 0: // Apply...
370 accept();
371 return;
372 case 1: // Discard
373 break;
374 default: // Cancel.
375 bReject = false;
376 break;
377 }
378 }
379
380 if (bReject)
381 QDialog::reject();
382 }
383
384
385 // Dirty up settings.
386 void InstrumentForm::changed (void)
387 {
388 if (m_iDirtySetup > 0)
389 return;
390
391 m_iDirtyCount++;
392 stabilizeForm();
393 }
394
395
396 // Stabilize current form state.
397 void InstrumentForm::stabilizeForm (void)
398 {
399 bool bValid =
400 !m_ui.NameLineEdit->text().isEmpty() &&
401 m_ui.EngineNameComboBox->currentIndex() >= 0 &&
402 m_ui.EngineNameComboBox->currentText() !=
403 qsamplerChannel::noEngineName();
404
405 const QString& sPath = m_ui.InstrumentFileComboBox->currentText();
406 bValid = bValid && !sPath.isEmpty() && QFileInfo(sPath).exists();
407
408 m_ui.OkPushButton->setEnabled(m_iDirtyCount > 0 && bValid);
409 }
410
411 } // namespace QSampler
412
413
414 // end of qsamplerInstrumentForm.cpp

  ViewVC Help
Powered by ViewVC