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

Annotation of /qsampler/trunk/src/qsamplerDeviceForm.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: 16465 byte(s)
- Qt4 migration: missing copyright headers update.

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

  ViewVC Help
Powered by ViewVC