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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1464 - (hide annotations) (download)
Thu Nov 1 17:14:21 2007 UTC (16 years, 5 months ago) by capela
File size: 9639 byte(s)
- Qt4 migration: missing copyright headers update.

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

  ViewVC Help
Powered by ViewVC