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

Contents of /qsampler/trunk/src/qsamplerDeviceForm.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: 15407 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 "qsamplerDeviceForm.h"
2
3 #include "qsamplerAbout.h"
4
5 namespace QSampler {
6
7 DeviceForm::DeviceForm(QWidget* parent, Qt::WFlags f) : QDialog(parent, f) {
8 ui.setupUi(this);
9
10 // Initialize locals.
11 m_iDirtySetup = 0;
12 m_iDirtyCount = 0;
13 m_bNewDevice = false;
14 m_deviceType = qsamplerDevice::None;
15 m_pAudioItems = NULL;
16 m_pMidiItems = NULL;
17 // No exclusive mode as default.
18 m_deviceTypeMode = qsamplerDevice::None;
19
20 ui.DeviceParamTable->setModel(&deviceParamModel);
21 ui.DeviceParamTable->setItemDelegate(&deviceParamDelegate);
22
23 ui.DevicePortParamTable->setModel(&devicePortParamModel);
24 ui.DevicePortParamTable->setItemDelegate(&devicePortParamDelegate);
25
26 // This an outsider (from designer), but rather important.
27 //QObject::connect(DeviceParamTable, SIGNAL(valueChanged(int,int)),
28 // this, SLOT(changeDeviceParam(int,int)));
29 //QObject::connect(DevicePortParamTable, SIGNAL(valueChanged(int,int)),
30 // this, SLOT(changeDevicePortParam(int,int)));
31
32 // Initial contents.
33 refreshDevices();
34 // Try to restore normal window positioning.
35 adjustSize();
36 }
37
38 DeviceForm::~DeviceForm() {
39 }
40
41
42 // Notify our parent that we're emerging.
43 void DeviceForm::showEvent ( QShowEvent *pShowEvent )
44 {
45 MainForm* pMainForm = MainForm::getInstance();
46 if (pMainForm)
47 pMainForm->stabilizeForm();
48
49 stabilizeForm();
50
51 QWidget::showEvent(pShowEvent);
52 }
53
54
55 // Notify our parent that we're closing.
56 void DeviceForm::hideEvent ( QHideEvent *pHideEvent )
57 {
58 QWidget::hideEvent(pHideEvent);
59
60 MainForm *pMainForm = MainForm::getInstance();
61 if (pMainForm)
62 pMainForm->stabilizeForm();
63
64 // Signal special whether we changed the device set.
65 if (m_iDirtyCount > 0) {
66 m_iDirtyCount = 0;
67 emit devicesChanged();
68 }
69 }
70
71
72 // Set device type spacial exclusive mode.
73 void DeviceForm::setDeviceTypeMode (
74 qsamplerDevice::qsamplerDeviceType deviceTypeMode )
75 {
76 // If it has not changed, do nothing.
77 if (m_deviceTypeMode == deviceTypeMode)
78 return;
79
80 m_deviceTypeMode = deviceTypeMode;
81
82 // OK. Do a whole refresh around.
83 refreshDevices();
84 }
85
86
87 // Device driver name setup formal initializer.
88 void DeviceForm::setDriverName ( const QString& sDriverName )
89 {
90 if (ui.DriverNameComboBox->findText(sDriverName) == 0) {
91 ui.DriverNameComboBox->insertItem(sDriverName);
92 }
93 ui.DriverNameComboBox->setCurrentText(sDriverName);
94 }
95
96
97 // Set current selected device by type and id.
98 void DeviceForm::setDevice ( qsamplerDevice *pDevice )
99 {
100 // In case no device is given...
101 qsamplerDevice::qsamplerDeviceType deviceType = m_deviceTypeMode;
102 if (pDevice)
103 deviceType = pDevice->deviceType();
104
105 // Get the device view root item...
106 qsamplerDeviceItem *pRootItem = NULL;
107 switch (deviceType) {
108 case qsamplerDevice::Audio:
109 pRootItem = m_pAudioItems;
110 break;
111 case qsamplerDevice::Midi:
112 pRootItem = m_pMidiItems;
113 break;
114 case qsamplerDevice::None:
115 break;
116 }
117
118 // Is the root present?
119 if (pRootItem == NULL)
120 return;
121
122 // So there's no device huh?
123 if (pDevice == NULL) {
124 ui.DeviceListView->setCurrentItem(pRootItem);
125 return;
126 }
127
128 // For each child, test for identity...
129 for (int i = 0; i < pRootItem->childCount(); i++) {
130 qsamplerDeviceItem* pDeviceItem =
131 (qsamplerDeviceItem*) pRootItem->child(i);
132
133 // If identities match, select as current device item.
134 if (pDeviceItem->device().deviceID() == pDevice->deviceID()) {
135 pDeviceItem->setSelected(true);
136 break;
137 }
138 }
139 }
140
141
142
143 // Create a new device from current table view.
144 void DeviceForm::createDevice (void)
145 {
146 MainForm *pMainForm = MainForm::getInstance();
147 if (pMainForm == NULL)
148 return;
149
150 QTreeWidgetItem *pItem = ui.DeviceListView->currentItem();
151 if (pItem == NULL || pItem->type() != QSAMPLER_DEVICE_ITEM)
152 return;
153
154 // About a brand new device instance...
155 qsamplerDevice device(((qsamplerDeviceItem *) pItem)->device());
156 if (device.createDevice()) {
157 // Now it depends on the device type...
158 qsamplerDeviceItem *pRootItem = NULL;
159 switch (device.deviceType()) {
160 case qsamplerDevice::Audio:
161 pRootItem = m_pAudioItems;
162 break;
163 case qsamplerDevice::Midi:
164 pRootItem = m_pMidiItems;
165 break;
166 case qsamplerDevice::None:
167 break;
168 }
169 // Append the new device item.
170 qsamplerDeviceItem *pDeviceItem = new qsamplerDeviceItem(pRootItem,
171 device.deviceType(), device.deviceID());
172 // Just make it the new selection...
173 pDeviceItem->setSelected(true);
174 // Main session should be marked dirty.
175 pMainForm->sessionDirty();
176 m_iDirtyCount++;
177 }
178 }
179
180
181 // Delete current device in table view.
182 void DeviceForm::deleteDevice (void)
183 {
184 MainForm *pMainForm = MainForm::getInstance();
185 if (pMainForm == NULL)
186 return;
187
188 QTreeWidgetItem* pItem = ui.DeviceListView->currentItem();
189 if (pItem == NULL || pItem->type() != QSAMPLER_DEVICE_ITEM)
190 return;
191
192 qsamplerDevice& device = ((qsamplerDeviceItem *) pItem)->device();
193
194 // Prompt user if this is for real...
195 qsamplerOptions *pOptions = pMainForm->options();
196 if (pOptions && pOptions->bConfirmRemove) {
197 if (QMessageBox::warning(this,
198 QSAMPLER_TITLE ": " + tr("Warning"),
199 tr("Delete device:\n\n"
200 "%1\n\n"
201 "Are you sure?")
202 .arg(device.deviceName()),
203 tr("OK"), tr("Cancel")) > 0)
204 return;
205 }
206
207 // Go and destroy...
208 if (device.deleteDevice()) {
209 // Remove it from the device view...
210 delete pItem;
211 // Main session should be marked dirty.
212 pMainForm->sessionDirty();
213 m_iDirtyCount++;
214 }
215 }
216
217
218 // Refresh all device list and views.
219 void DeviceForm::refreshDevices (void)
220 {
221 MainForm *pMainForm = MainForm::getInstance();
222 if (pMainForm == NULL)
223 return;
224
225 // Avoid nested changes.
226 m_iDirtySetup++;
227
228 //
229 // (Re)Load complete device configuration data ...
230 //
231 m_pAudioItems = NULL;
232 m_pMidiItems = NULL;
233 ui.DeviceListView->clear();
234 if (pMainForm->client()) {
235 int *piDeviceIDs;
236 // Grab and pop Audio devices...
237 if (m_deviceTypeMode == qsamplerDevice::None ||
238 m_deviceTypeMode == qsamplerDevice::Audio) {
239 m_pAudioItems = new qsamplerDeviceItem(ui.DeviceListView,
240 qsamplerDevice::Audio);
241 }
242 if (m_pAudioItems) {
243 piDeviceIDs = qsamplerDevice::getDevices(pMainForm->client(),
244 qsamplerDevice::Audio);
245 for (int i = 0; piDeviceIDs && piDeviceIDs[i] >= 0; i++) {
246 new qsamplerDeviceItem(m_pAudioItems,
247 qsamplerDevice::Audio, piDeviceIDs[i]);
248 }
249 m_pAudioItems->setExpanded(true);
250 }
251 // Grab and pop MIDI devices...
252 if (m_deviceTypeMode == qsamplerDevice::None ||
253 m_deviceTypeMode == qsamplerDevice::Midi) {
254 m_pMidiItems = new qsamplerDeviceItem(ui.DeviceListView,
255 qsamplerDevice::Midi);
256 }
257 if (m_pMidiItems) {
258 piDeviceIDs = qsamplerDevice::getDevices(pMainForm->client(),
259 qsamplerDevice::Midi);
260 for (int i = 0; piDeviceIDs && piDeviceIDs[i] >= 0; i++) {
261 new qsamplerDeviceItem(m_pMidiItems,
262 qsamplerDevice::Midi, piDeviceIDs[i]);
263 }
264 m_pMidiItems->setExpanded(true);
265 }
266 }
267
268 // Done.
269 m_iDirtySetup--;
270
271 // Show something.
272 selectDevice();
273 }
274
275
276 // Driver selection slot.
277 void DeviceForm::selectDriver ( const QString& sDriverName )
278 {
279 if (m_iDirtySetup > 0)
280 return;
281
282 //
283 // Driver name has changed for a new device...
284 //
285
286 QTreeWidgetItem* pItem = ui.DeviceListView->currentItem();
287 if (pItem == NULL || pItem->type() != QSAMPLER_DEVICE_ITEM)
288 return;
289
290 qsamplerDevice& device = ((qsamplerDeviceItem *) pItem)->device();
291
292 // Driver change is only valid for scratch devices...
293 if (m_bNewDevice) {
294 m_iDirtySetup++;
295 device.setDriver(sDriverName);
296 deviceParamModel.refresh(device.params(), m_bNewDevice);
297 m_iDirtySetup--;
298 // Done.
299 stabilizeForm();
300 }
301 }
302
303
304 // Device selection slot.
305 void DeviceForm::selectDevice (void)
306 {
307 MainForm *pMainForm = MainForm::getInstance();
308 if (pMainForm == NULL)
309 return;
310
311 if (m_iDirtySetup > 0)
312 return;
313
314 //
315 // Device selection has changed...
316 //
317
318 QTreeWidgetItem* pItem = ui.DeviceListView->currentItem();
319 if (pItem == NULL || pItem->type() != QSAMPLER_DEVICE_ITEM) {
320 m_deviceType = qsamplerDevice::None;
321 ui.DeviceNameTextLabel->setText(QString::null);
322 deviceParamModel.clear();
323 ui.DevicePortComboBox->clear();
324 devicePortParamModel.clear();
325 ui.DevicePortTextLabel->setEnabled(false);
326 ui.DevicePortComboBox->setEnabled(false);
327 ui.DevicePortParamTable->setEnabled(false);
328 stabilizeForm();
329 return;
330 }
331
332 qsamplerDevice& device = ((qsamplerDeviceItem *) pItem)->device();
333
334 m_iDirtySetup++;
335 // Flag whether this is a new device.
336 m_bNewDevice = (device.deviceID() < 0);
337
338 // Fill the device/driver heading...
339 ui.DeviceNameTextLabel->setText(device.deviceName());
340 // The driver combobox is only rebuilt if device type has changed...
341 if (device.deviceType() != m_deviceType) {
342 ui.DriverNameComboBox->clear();
343 ui.DriverNameComboBox->insertStringList(
344 qsamplerDevice::getDrivers(pMainForm->client(), device.deviceType()));
345 m_deviceType = device.deviceType();
346 }
347 // Do we need a driver name?
348 if (m_bNewDevice || device.driverName().isEmpty())
349 device.setDriver(ui.DriverNameComboBox->currentText());
350 setDriverName(device.driverName());
351 ui.DriverNameTextLabel->setEnabled(m_bNewDevice);
352 ui.DriverNameComboBox->setEnabled(m_bNewDevice);
353 // Fill the device parameter table...
354 deviceParamModel.refresh(device.params(), m_bNewDevice);
355 // And now the device port/channel parameter table...
356 switch (device.deviceType()) {
357 case qsamplerDevice::Audio:
358 ui.DevicePortTextLabel->setText(tr("Ch&annel:"));
359 break;
360 case qsamplerDevice::Midi:
361 ui.DevicePortTextLabel->setText(tr("P&ort:"));
362 break;
363 case qsamplerDevice::None:
364 break;
365 }
366 ui.DevicePortComboBox->clear();
367 devicePortParamModel.clear();
368 if (m_bNewDevice) {
369 ui.DevicePortTextLabel->setEnabled(false);
370 ui.DevicePortComboBox->setEnabled(false);
371 ui.DevicePortParamTable->setEnabled(false);
372 } else {
373 QPixmap pixmap;
374 switch (device.deviceType()) {
375 case qsamplerDevice::Audio:
376 pixmap = QPixmap(":/qsampler/pixmaps/audio2.png");
377 break;
378 case qsamplerDevice::Midi:
379 pixmap = QPixmap(":/qsampler/pixmaps/midi2.png");
380 break;
381 case qsamplerDevice::None:
382 break;
383 }
384 qsamplerDevicePortList& ports = device.ports();
385 qsamplerDevicePort *pPort;
386 for (pPort = ports.first(); pPort; pPort = ports.next()) {
387 ui.DevicePortComboBox->insertItem(pixmap, device.deviceTypeName()
388 + ' ' + device.driverName()
389 + ' ' + pPort->portName());
390 }
391 bool bEnabled = (ports.count() > 0);
392 ui.DevicePortTextLabel->setEnabled(bEnabled);
393 ui.DevicePortComboBox->setEnabled(bEnabled);
394 ui.DevicePortParamTable->setEnabled(bEnabled);
395 }
396 // Done.
397 m_iDirtySetup--;
398
399 // Make the device port/channel selection effective.
400 selectDevicePort(ui.DevicePortComboBox->currentItem());
401 }
402
403
404 // Device port/channel selection slot.
405 void DeviceForm::selectDevicePort ( int iPort )
406 {
407 if (m_iDirtySetup > 0)
408 return;
409
410 //
411 // Device port/channel selection has changed...
412 //
413
414 QTreeWidgetItem* pItem = ui.DeviceListView->currentItem();
415 if (pItem == NULL || pItem->type() != QSAMPLER_DEVICE_ITEM)
416 return;
417
418 qsamplerDevice& device = ((qsamplerDeviceItem *) pItem)->device();
419 qsamplerDevicePort *pPort = device.ports().at(iPort);
420 if (pPort) {
421 m_iDirtySetup++;
422 devicePortParamModel.refresh(pPort->params(), false);
423 m_iDirtySetup--;
424 }
425 // Done.
426 stabilizeForm();
427 }
428
429
430 // Device parameter value change slot.
431 void DeviceForm::changeDeviceParam ( int iRow, int iCol )
432 {
433 if (m_iDirtySetup > 0)
434 return;
435 if (iRow < 0 || iCol < 0)
436 return;
437
438 //
439 // Device parameter change...
440 //
441
442 QTreeWidgetItem* pItem = ui.DeviceListView->currentItem();
443 if (pItem == NULL || pItem->type() != QSAMPLER_DEVICE_ITEM)
444 return;
445
446 qsamplerDevice& device = ((qsamplerDeviceItem *) pItem)->device();
447
448 // Table 1st column has the parameter name;
449 //const QString sParam = ui.DeviceParamTable->text(iRow, 0);
450 //const QString sValue = ui.DeviceParamTable->text(iRow, iCol);
451 const QString sParam = deviceParamModel.data(deviceParamModel.index(iRow, 0), Qt::DisplayRole).value<DeviceParameterRow>().name;
452 const QString sValue = deviceParamModel.data(deviceParamModel.index(iRow, iCol), Qt::DisplayRole).value<DeviceParameterRow>().param.value;
453 // Set the local device parameter value.
454 if (device.setParam(sParam, sValue)) {
455 selectDevice();
456 } else {
457 stabilizeForm();
458 }
459
460 // Main session should be dirtier...
461 MainForm *pMainForm = MainForm::getInstance();
462 if (pMainForm)
463 pMainForm->sessionDirty();
464 }
465
466
467 // Device port/channel parameter value change slot.
468 void DeviceForm::changeDevicePortParam ( int iRow, int iCol )
469 {
470 if (m_iDirtySetup > 0)
471 return;
472 if (iRow < 0 || iCol < 0)
473 return;
474
475 //
476 // Device port/channel parameter change...
477 //
478
479 QTreeWidgetItem* pItem = ui.DeviceListView->currentItem();
480 if (pItem == NULL || pItem->type() != QSAMPLER_DEVICE_ITEM)
481 return;
482
483 qsamplerDevice& device = ((qsamplerDeviceItem *) pItem)->device();
484
485 int iPort = ui.DevicePortComboBox->currentItem();
486 qsamplerDevicePort *pPort = device.ports().at(iPort);
487 if (pPort == NULL)
488 return;
489
490 // Table 1st column has the parameter name;
491 //const QString sParam = ui.DevicePortParamTable->text(iRow, 0);
492 //const QString sValue = ui.DevicePortParamTable->text(iRow, iCol);
493 const QString sParam = devicePortParamModel.data(devicePortParamModel.index(iRow, 0), Qt::DisplayRole).value<DeviceParameterRow>().name;
494 const QString sValue = devicePortParamModel.data(devicePortParamModel.index(iRow, iCol), Qt::DisplayRole).value<DeviceParameterRow>().param.value;
495
496 // Set the local device port/channel parameter value.
497 pPort->setParam(sParam, sValue);
498 // Done.
499 stabilizeForm();
500
501 // Main session should be dirtier...
502 MainForm* pMainForm = MainForm::getInstance();
503 if (pMainForm)
504 pMainForm->sessionDirty();
505 }
506
507
508 // Device list view context menu handler.
509 void DeviceForm::contextMenu ( QTreeWidgetItem* pItem, const QPoint& pos, int )
510 {
511 MainForm *pMainForm = MainForm::getInstance();
512 if (pMainForm == NULL)
513 return;
514
515 int iItemID;
516
517 // Build the device context menu...
518 QMenu* pContextMenu = new QMenu(this);
519
520 bool bClient = (pMainForm->client() != NULL);
521 bool bEnabled = (pItem != NULL);
522 iItemID = pContextMenu->insertItem(
523 QIconSet(QPixmap(":/qsampler/pixmaps/deviceCreate.png")),
524 tr("&Create device"), this, SLOT(createDevice()));
525 pContextMenu->setItemEnabled(iItemID, bEnabled || (bClient && m_bNewDevice));
526 iItemID = pContextMenu->insertItem(
527 QIconSet(QPixmap(":/qsampler/pixmaps/deviceDelete.png")),
528 tr("&Delete device"), this, SLOT(deleteDevice()));
529 pContextMenu->setItemEnabled(iItemID, bEnabled && !m_bNewDevice);
530 pContextMenu->insertSeparator();
531 iItemID = pContextMenu->insertItem(
532 QIconSet(QPixmap(":/qsampler/pixmaps/formRefresh.png")),
533 tr("&Refresh"), this, SLOT(refreshDevices()));
534 pContextMenu->setItemEnabled(iItemID, bClient);
535
536 pContextMenu->exec(pos);
537
538 delete pContextMenu;
539 }
540
541
542 // Stabilize current form state.
543 void DeviceForm::stabilizeForm (void)
544 {
545 MainForm* pMainForm = MainForm::getInstance();
546 QTreeWidgetItem* pItem = ui.DeviceListView->currentItem();
547 bool bClient = (pMainForm && pMainForm->client() != NULL);
548 bool bEnabled = (pItem != NULL);
549 ui.DeviceNameTextLabel->setEnabled(bEnabled && !m_bNewDevice);
550 ui.DriverNameTextLabel->setEnabled(bEnabled && m_bNewDevice);
551 ui.DriverNameComboBox->setEnabled(bEnabled && m_bNewDevice);
552 ui.DeviceParamTable->setEnabled(bEnabled);
553 ui.RefreshDevicesPushButton->setEnabled(bClient);
554 ui.CreateDevicePushButton->setEnabled(bEnabled || (bClient && m_bNewDevice));
555 ui.DeleteDevicePushButton->setEnabled(bEnabled && !m_bNewDevice);
556 }
557
558 } // namespace QSampler

  ViewVC Help
Powered by ViewVC