![]() |
Mixxx
|
00001 00007 /*************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 00016 #include "dlgprefsounditem.h" 00017 #include "sounddevice.h" 00018 #include "soundmanagerconfig.h" 00019 00029 DlgPrefSoundItem::DlgPrefSoundItem(QWidget *parent, AudioPathType type, 00030 QList<SoundDevice*> &devices, bool isInput, 00031 unsigned int index /* = 0 */) 00032 : QWidget(parent) 00033 , m_type(type) 00034 , m_index(index) 00035 , m_devices(devices) 00036 , m_isInput(isInput) 00037 , m_savedDevice("") 00038 , m_savedChannel(0) { 00039 setupUi(this); 00040 if (AudioPath::isIndexed(type)) { 00041 typeLabel->setText( 00042 QString("%1 %2").arg(AudioPath::getTrStringFromType(type)).arg(index + 1)); 00043 } else { 00044 typeLabel->setText(AudioPath::getTrStringFromType(type)); 00045 } 00046 deviceComboBox->addItem(tr("None"), "None"); 00047 connect(deviceComboBox, SIGNAL(currentIndexChanged(int)), 00048 this, SLOT(deviceChanged(int))); 00049 connect(channelComboBox, SIGNAL(currentIndexChanged(int)), 00050 this, SIGNAL(settingChanged())); 00051 refreshDevices(m_devices); 00052 } 00053 00054 DlgPrefSoundItem::~DlgPrefSoundItem() { 00055 00056 } 00057 00062 void DlgPrefSoundItem::refreshDevices(const QList<SoundDevice*> &devices) { 00063 m_devices = devices; 00064 QString oldDev = deviceComboBox->itemData(deviceComboBox->currentIndex()).toString(); 00065 deviceComboBox->setCurrentIndex(0); 00066 // not using combobox->clear means we can leave in "None" so it 00067 // doesn't flicker when you switch APIs... cleaner Mixxx :) bkgood 00068 while (deviceComboBox->count() > 1) { 00069 deviceComboBox->removeItem(deviceComboBox->count() - 1); 00070 } 00071 foreach (SoundDevice *device, m_devices) { 00072 deviceComboBox->addItem(device->getDisplayName(), device->getInternalName()); 00073 } 00074 int newIndex = deviceComboBox->findData(oldDev); 00075 if (newIndex != -1) { 00076 deviceComboBox->setCurrentIndex(newIndex); 00077 } 00078 } 00079 00084 void DlgPrefSoundItem::deviceChanged(int index) { 00085 channelComboBox->clear(); 00086 QString selection = deviceComboBox->itemData(index).toString(); 00087 unsigned int numChannels = 0; 00088 if (selection == "None") { 00089 goto emitAndReturn; 00090 } else { 00091 foreach (SoundDevice *device, m_devices) { 00092 if (device->getInternalName() == selection) { 00093 if (m_isInput) { 00094 numChannels = device->getNumInputChannels(); 00095 } else { 00096 numChannels = device->getNumOutputChannels(); 00097 } 00098 } 00099 } 00100 } 00101 if (numChannels == 0) { 00102 goto emitAndReturn; 00103 } else { 00104 unsigned char channelsForType = 00105 AudioPath::channelsNeededForType(m_type); 00106 for (unsigned int i = 1; i + (channelsForType - 1) <= numChannels; ++i) { 00107 if (channelsForType == 1) { 00108 channelComboBox->addItem( 00109 QString(tr("Channel %1")).arg(i), i - 1); 00110 } else { 00111 channelComboBox->addItem( 00112 QString(tr("Channels %1 - %2")).arg(i).arg(i + 1), i - 1); 00113 } 00114 // i-1 because want the data part to be what goes into audiopath's 00115 // channelbase which is zero-based -- bkgood 00116 } 00117 } 00118 emitAndReturn: 00119 emit(settingChanged()); 00120 } 00121 00129 void DlgPrefSoundItem::loadPath(const SoundManagerConfig &config) { 00130 if (m_isInput) { 00131 QMultiHash<QString, AudioInput> inputs(config.getInputs()); 00132 foreach (QString devName, inputs.uniqueKeys()) { 00133 foreach (AudioInput in, inputs.values(devName)) { 00134 if (in.getType() == m_type && in.getIndex() == m_index) { 00135 setDevice(devName); 00136 setChannel(in.getChannelGroup().getChannelBase()); 00137 return; // we're just using the first one found, leave 00138 // multiples to a more advanced dialog -- bkgood 00139 } 00140 } 00141 } 00142 } else { 00143 QMultiHash<QString, AudioOutput> outputs(config.getOutputs()); 00144 foreach (QString devName, outputs.uniqueKeys()) { 00145 foreach (AudioOutput out, outputs.values(devName)) { 00146 if (out.getType() == m_type && out.getIndex() == m_index) { 00147 setDevice(devName); 00148 setChannel(out.getChannelGroup().getChannelBase()); 00149 return; // we're just using the first one found, leave 00150 // multiples to a more advanced dialog -- bkgood 00151 } 00152 } 00153 } 00154 } 00155 // if we've gotten here without returning, we didn't find a path applicable 00156 // to us so set some defaults -- bkgood 00157 setDevice("None"); // this will blank the channel combo box 00158 } 00159 00165 void DlgPrefSoundItem::writePath(SoundManagerConfig *config) const { 00166 SoundDevice *device = getDevice(); 00167 if (device == NULL) { 00168 return; 00169 } // otherwise, this will have a valid audiopath 00170 if (m_isInput) { 00171 config->addInput( 00172 device->getInternalName(), 00173 AudioInput( 00174 m_type, 00175 channelComboBox->itemData(channelComboBox->currentIndex()).toUInt(), 00176 m_index 00177 ) 00178 ); 00179 } else { 00180 config->addOutput( 00181 device->getInternalName(), 00182 AudioOutput( 00183 m_type, 00184 channelComboBox->itemData(channelComboBox->currentIndex()).toUInt(), 00185 m_index 00186 ) 00187 ); 00188 } 00189 } 00190 00194 void DlgPrefSoundItem::save() { 00195 m_savedDevice = deviceComboBox->itemData(deviceComboBox->currentIndex()).toString(); 00196 m_savedChannel = channelComboBox->itemData(channelComboBox->currentIndex()).toUInt(); 00197 } 00198 00202 void DlgPrefSoundItem::reload() { 00203 int newDevice = deviceComboBox->findData(m_savedDevice); 00204 if (newDevice > -1) { 00205 deviceComboBox->setCurrentIndex(newDevice); 00206 } 00207 int newChannel = channelComboBox->findData(m_savedChannel); 00208 if (newChannel > -1) { 00209 channelComboBox->setCurrentIndex(newChannel); 00210 } 00211 } 00212 00217 SoundDevice* DlgPrefSoundItem::getDevice() const { 00218 QString selection = deviceComboBox->itemData(deviceComboBox->currentIndex()).toString(); 00219 if (selection == "None") { 00220 return NULL; 00221 } 00222 foreach (SoundDevice *device, m_devices) { 00223 if (selection == device->getInternalName()) { 00224 return device; 00225 } 00226 } 00227 // looks like something became invalid ??? 00228 deviceComboBox->setCurrentIndex(0); // set it to none 00229 return NULL; 00230 } 00231 00236 void DlgPrefSoundItem::setDevice(const QString &deviceName) { 00237 int index = deviceComboBox->findData(deviceName); 00238 if (index != -1) { 00239 deviceComboBox->setCurrentIndex(index); 00240 } else { 00241 deviceComboBox->setCurrentIndex(0); // None 00242 } 00243 } 00244 00249 void DlgPrefSoundItem::setChannel(unsigned int channel) { 00250 int index = channelComboBox->findData(channel); 00251 if (index != -1) { 00252 channelComboBox->setCurrentIndex(index); 00253 } else { 00254 channelComboBox->setCurrentIndex(0); // 1 00255 } 00256 }