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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1461 - (show annotations) (download)
Sun Oct 28 23:30:36 2007 UTC (16 years, 5 months ago) by schoenebeck
File size: 8576 byte(s)
* started to port QSampler to Qt4 (NOTE: this version is yet broken, use
  the latest tarball release 0.1.5 until the Qt4 port is completed)

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

  ViewVC Help
Powered by ViewVC