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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1513 - (hide annotations) (download)
Fri Nov 23 09:32:06 2007 UTC (16 years, 4 months ago) by capela
File size: 11156 byte(s)
* Qt4 migration: more cleanups and channel strip visual fixes.

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

  ViewVC Help
Powered by ViewVC