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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1474 - (show annotations) (download)
Mon Nov 5 20:47:38 2007 UTC (16 years, 4 months ago) by schoenebeck
File size: 10724 byte(s)
* Qt4 migration: fixed another bunch of ghost connections, fixed engine
  combo box in channel form, fixed stdout ouptut in message window (a lot
  of white lines were shown), show channel strip on the work space (and not
  in the nirvana of the desktop universe)

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

  ViewVC Help
Powered by ViewVC