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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1470 - (hide annotations) (download)
Sun Nov 4 23:37:47 2007 UTC (16 years, 4 months ago) by schoenebeck
File size: 17347 byte(s)
* Qt4 migration: fixed ghost signal connections

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

  ViewVC Help
Powered by ViewVC