| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2018 F4EXB                                                      //
 | 
					
						
							|  |  |  | // written by Edouard Griffiths                                                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:43:33 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "audioselectdialog.h"
 | 
					
						
							|  |  |  | #include "ui_audioselectdialog.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-25 05:44:38 +01:00
										 |  |  | AudioSelectDialog::AudioSelectDialog(const AudioDeviceManager* audioDeviceManager, const QString& deviceName, bool input, QWidget* parent) : | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  |     QDialog(parent), | 
					
						
							|  |  |  |     m_selected(false), | 
					
						
							|  |  |  |     ui(new Ui::AudioSelectDialog), | 
					
						
							|  |  |  |     m_audioDeviceManager(audioDeviceManager), | 
					
						
							|  |  |  |     m_input(input) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ui->setupUi(this); | 
					
						
							|  |  |  |     QTreeWidgetItem *treeItem, *defaultItem, *selectedItem = 0; | 
					
						
							| 
									
										
										
										
											2018-03-27 22:32:04 +02:00
										 |  |  |     bool systemDefault; | 
					
						
							|  |  |  |     int sampleRate; | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // panel
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     defaultItem = new QTreeWidgetItem(ui->audioTree); | 
					
						
							| 
									
										
										
										
											2018-03-27 22:32:04 +02:00
										 |  |  |     defaultItem->setText(1, AudioDeviceManager::m_defaultDeviceName); | 
					
						
							|  |  |  |     bool deviceFound = getDeviceInfos(input, AudioDeviceManager::m_defaultDeviceName, systemDefault, sampleRate); | 
					
						
							|  |  |  |     defaultItem->setText(0, deviceFound ? "__" : "_D"); | 
					
						
							|  |  |  |     defaultItem->setText(2, tr("%1").arg(sampleRate)); | 
					
						
							|  |  |  |     defaultItem->setTextAlignment(2, Qt::AlignRight); | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 14:50:16 +00:00
										 |  |  |     QList<AudioDeviceInfo> devices = input ? m_audioDeviceManager->getInputDevices() : m_audioDeviceManager->getOutputDevices(); | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 14:50:16 +00:00
										 |  |  |     for(QList<AudioDeviceInfo>::const_iterator it = devices.begin(); it != devices.end(); ++it) | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         treeItem = new QTreeWidgetItem(ui->audioTree); | 
					
						
							| 
									
										
										
										
											2018-03-27 22:32:04 +02:00
										 |  |  |         treeItem->setText(1, it->deviceName()); | 
					
						
							|  |  |  |         bool deviceFound = getDeviceInfos(input, it->deviceName(), systemDefault, sampleRate); | 
					
						
							|  |  |  |         treeItem->setText(0, QString(systemDefault ? "S" : "_") + QString(deviceFound ? "_" : "D")); | 
					
						
							|  |  |  |         treeItem->setText(2, tr("%1").arg(sampleRate)); | 
					
						
							|  |  |  |         treeItem->setTextAlignment(2, Qt::AlignRight); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (systemDefault) { | 
					
						
							|  |  |  |             treeItem->setBackground(1, QBrush(qRgb(80,80,80))); | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (deviceName == it->deviceName()) { | 
					
						
							|  |  |  |             selectedItem = treeItem; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 22:32:04 +02:00
										 |  |  |     ui->audioTree->resizeColumnToContents(0); | 
					
						
							|  |  |  |     ui->audioTree->resizeColumnToContents(1); | 
					
						
							|  |  |  |     ui->audioTree->resizeColumnToContents(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  |     if (selectedItem) { | 
					
						
							|  |  |  |         ui->audioTree->setCurrentItem(selectedItem); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         ui->audioTree->setCurrentItem(defaultItem); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AudioSelectDialog::~AudioSelectDialog() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     delete ui; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioSelectDialog::accept() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int deviceIndex = ui->audioTree->indexOfTopLevelItem(ui->audioTree->currentItem()) - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_input) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!m_audioDeviceManager->getInputDeviceName(deviceIndex, m_audioDeviceName)) { | 
					
						
							|  |  |  |             m_audioDeviceName = AudioDeviceManager::m_defaultDeviceName; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!m_audioDeviceManager->getOutputDeviceName(deviceIndex, m_audioDeviceName)) { | 
					
						
							|  |  |  |             m_audioDeviceName = AudioDeviceManager::m_defaultDeviceName; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         qDebug("AudioSelectDialog::accept: output: %d (%s)", deviceIndex, qPrintable(m_audioDeviceName)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_selected = true; | 
					
						
							|  |  |  |     QDialog::accept(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioSelectDialog::reject() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QDialog::reject(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 22:32:04 +02:00
										 |  |  | bool AudioSelectDialog::getDeviceInfos(bool input, const QString& deviceName, bool& systemDefault, int& sampleRate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     bool found; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (input) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         AudioDeviceManager::InputDeviceInfo inDeviceInfo; | 
					
						
							|  |  |  |         found = m_audioDeviceManager->getInputDeviceInfo(deviceName, inDeviceInfo); | 
					
						
							| 
									
										
										
										
											2022-11-17 14:50:16 +00:00
										 |  |  |         systemDefault = deviceName == AudioDeviceInfo::defaultInputDevice().deviceName(); | 
					
						
							| 
									
										
										
										
											2018-03-27 22:32:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (found) { | 
					
						
							|  |  |  |             sampleRate = inDeviceInfo.sampleRate; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             sampleRate = AudioDeviceManager::m_defaultAudioSampleRate; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         AudioDeviceManager::OutputDeviceInfo outDeviceInfo; | 
					
						
							|  |  |  |         found = m_audioDeviceManager->getOutputDeviceInfo(deviceName, outDeviceInfo); | 
					
						
							| 
									
										
										
										
											2022-11-17 14:50:16 +00:00
										 |  |  |         systemDefault = deviceName == AudioDeviceInfo::defaultOutputDevice().deviceName(); | 
					
						
							| 
									
										
										
										
											2018-03-27 22:32:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (found) { | 
					
						
							|  |  |  |             sampleRate = outDeviceInfo.sampleRate; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             sampleRate = AudioDeviceManager::m_defaultAudioSampleRate; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return found; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |