1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(avform): improve code flow

* rename file avsettings.ui -> avform.ui, introducing seamless switching between UI/Code
* switch to "private multi-inheritance" pattern, which has several advantages
This commit is contained in:
Nils Fenner 2016-07-14 10:53:53 +02:00
parent 6d7d9c33a5
commit 0b2dfc0305
No known key found for this signature in database
GPG Key ID: 9591A163FF9BE04C
4 changed files with 72 additions and 77 deletions

View File

@ -31,7 +31,7 @@ FORMS += \
src/widget/form/setpassworddialog.ui \ src/widget/form/setpassworddialog.ui \
src/widget/form/settings/aboutsettings.ui \ src/widget/form/settings/aboutsettings.ui \
src/widget/form/settings/advancedsettings.ui \ src/widget/form/settings/advancedsettings.ui \
src/widget/form/settings/avsettings.ui \ src/widget/form/settings/avform.ui \
src/widget/form/settings/generalsettings.ui \ src/widget/form/settings/generalsettings.ui \
src/widget/form/settings/privacysettings.ui \ src/widget/form/settings/privacysettings.ui \
src/widget/form/removefrienddialog.ui \ src/widget/form/removefrienddialog.ui \

View File

@ -18,7 +18,7 @@
*/ */
#include "avform.h" #include "avform.h"
#include "ui_avsettings.h"
#include "src/audio/audio.h" #include "src/audio/audio.h"
#include "src/persistence/settings.h" #include "src/persistence/settings.h"
#include "src/video/camerasource.h" #include "src/video/camerasource.h"
@ -44,46 +44,43 @@ AVForm::AVForm() :
, camVideoSurface(nullptr) , camVideoSurface(nullptr)
, camera(CameraSource::getInstance()) , camera(CameraSource::getInstance())
{ {
bodyUI = new Ui::AVSettings; setupUi(this);
bodyUI->setupUi(this);
const Audio& audio = Audio::getInstance(); const Audio& audio = Audio::getInstance();
bodyUI->btnPlayTestSound->setToolTip( btnPlayTestSound->setToolTip(
tr("Play a test sound while changing the output volume.")); tr("Play a test sound while changing the output volume."));
auto qcbxIndexChangedInt = static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged); auto qcbxIndexChangedInt = static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged);
connect(bodyUI->inDevCombobox, qcbxIndexChangedInt, this, &AVForm::onAudioInDevChanged); connect(inDevCombobox, qcbxIndexChangedInt, this, &AVForm::onAudioInDevChanged);
connect(bodyUI->outDevCombobox, qcbxIndexChangedInt, this, &AVForm::onAudioOutDevChanged); connect(outDevCombobox, qcbxIndexChangedInt, this, &AVForm::onAudioOutDevChanged);
connect(bodyUI->videoDevCombobox, qcbxIndexChangedInt, this, &AVForm::onVideoDevChanged); connect(videoDevCombobox, qcbxIndexChangedInt, this, &AVForm::onVideoDevChanged);
connect(bodyUI->videoModescomboBox, qcbxIndexChangedInt, this, &AVForm::onVideoModesIndexChanged); connect(videoModescomboBox, qcbxIndexChangedInt, this, &AVForm::onVideoModesIndexChanged);
connect(bodyUI->rescanButton, &QPushButton::clicked, this, [=]() connect(rescanButton, &QPushButton::clicked, this, [=]()
{ {
getAudioInDevices(); getAudioInDevices();
getAudioOutDevices(); getAudioOutDevices();
getVideoDevices(); getVideoDevices();
}); });
bodyUI->playbackSlider->setTracking(false); playbackSlider->setTracking(false);
bodyUI->playbackSlider->installEventFilter(this); playbackSlider->installEventFilter(this);
connect(bodyUI->playbackSlider, &QSlider::valueChanged,
this, &AVForm::onPlaybackValueChanged);
bodyUI->microphoneSlider->setToolTip( microphoneSlider->setToolTip(
tr("Use slider to set the gain of your input device ranging" tr("Use slider to set the gain of your input device ranging"
" from %1dB to %2dB.") " from %1dB to %2dB.")
.arg(audio.minInputGain()) .arg(audio.minInputGain())
.arg(audio.maxInputGain())); .arg(audio.maxInputGain()));
bodyUI->microphoneSlider->setMinimum(qRound(audio.minInputGain()) * 10); microphoneSlider->setMinimum(qRound(audio.minInputGain()) * 10);
bodyUI->microphoneSlider->setMaximum(qRound(audio.maxInputGain()) * 10); microphoneSlider->setMaximum(qRound(audio.maxInputGain()) * 10);
bodyUI->microphoneSlider->setTickPosition(QSlider::TicksBothSides); microphoneSlider->setTickPosition(QSlider::TicksBothSides);
bodyUI->microphoneSlider->setTickInterval( microphoneSlider->setTickInterval(
(qAbs(bodyUI->microphoneSlider->minimum()) + (qAbs(microphoneSlider->minimum()) +
bodyUI->microphoneSlider->maximum()) / 4); microphoneSlider->maximum()) / 4);
bodyUI->microphoneSlider->setTracking(false); microphoneSlider->setTracking(false);
bodyUI->microphoneSlider->installEventFilter(this); microphoneSlider->installEventFilter(this);
connect(bodyUI->microphoneSlider, &QSlider::valueChanged, connect(microphoneSlider, &QSlider::valueChanged,
this, &AVForm::onMicrophoneValueChanged); this, &AVForm::onMicrophoneValueChanged);
for (QComboBox* cb : findChildren<QComboBox*>()) for (QComboBox* cb : findChildren<QComboBox*>())
@ -99,7 +96,6 @@ AVForm::~AVForm()
{ {
killVideoSurface(); killVideoSurface();
Translator::unregister(this); Translator::unregister(this);
delete bodyUI;
} }
void AVForm::hideEvent(QHideEvent* event) void AVForm::hideEvent(QHideEvent* event)
@ -151,7 +147,7 @@ void AVForm::onVideoModesIndexChanged(int index)
qWarning() << "Invalid mode index"; qWarning() << "Invalid mode index";
return; return;
} }
int devIndex = bodyUI->videoDevCombobox->currentIndex(); int devIndex = videoDevCombobox->currentIndex();
if (devIndex < 0 || devIndex >= videoDeviceList.size()) if (devIndex < 0 || devIndex >= videoDeviceList.size())
{ {
qWarning() << "Invalid device index"; qWarning() << "Invalid device index";
@ -267,8 +263,8 @@ void AVForm::selectBestModes(QVector<VideoMode> &allVideoModes)
void AVForm::fillCameraModesComboBox() void AVForm::fillCameraModesComboBox()
{ {
bool previouslyBlocked = bodyUI->videoModescomboBox->blockSignals(true); bool previouslyBlocked = videoModescomboBox->blockSignals(true);
bodyUI->videoModescomboBox->clear(); videoModescomboBox->clear();
for(int i = 0; i < videoModes.size(); i++) for(int i = 0; i < videoModes.size(); i++)
{ {
@ -283,13 +279,13 @@ void AVForm::fillCameraModesComboBox()
else else
str += tr("Default resolution"); str += tr("Default resolution");
bodyUI->videoModescomboBox->addItem(str); videoModescomboBox->addItem(str);
} }
if (videoModes.isEmpty()) if (videoModes.isEmpty())
bodyUI->videoModescomboBox->addItem(tr("Default resolution")); videoModescomboBox->addItem(tr("Default resolution"));
bodyUI->videoModescomboBox->blockSignals(previouslyBlocked); videoModescomboBox->blockSignals(previouslyBlocked);
} }
int AVForm::searchPreferredIndex() int AVForm::searchPreferredIndex()
@ -311,8 +307,8 @@ int AVForm::searchPreferredIndex()
void AVForm::fillScreenModesComboBox() void AVForm::fillScreenModesComboBox()
{ {
bool previouslyBlocked = bodyUI->videoModescomboBox->blockSignals(true); bool previouslyBlocked = videoModescomboBox->blockSignals(true);
bodyUI->videoModescomboBox->clear(); videoModescomboBox->clear();
for(int i = 0; i < videoModes.size(); i++) for(int i = 0; i < videoModes.size(); i++)
{ {
@ -326,10 +322,10 @@ void AVForm::fillScreenModesComboBox()
else else
name = tr("Select region"); name = tr("Select region");
bodyUI->videoModescomboBox->addItem(name); videoModescomboBox->addItem(name);
} }
bodyUI->videoModescomboBox->blockSignals(previouslyBlocked); videoModescomboBox->blockSignals(previouslyBlocked);
} }
void AVForm::updateVideoModes(int curIndex) void AVForm::updateVideoModes(int curIndex)
@ -363,7 +359,7 @@ void AVForm::updateVideoModes(int curIndex)
int preferedIndex = searchPreferredIndex(); int preferedIndex = searchPreferredIndex();
if (preferedIndex != -1) if (preferedIndex != -1)
{ {
bodyUI->videoModescomboBox->setCurrentIndex(preferedIndex); videoModescomboBox->setCurrentIndex(preferedIndex);
return; return;
} }
@ -373,7 +369,7 @@ void AVForm::updateVideoModes(int curIndex)
VideoMode mode(rect); VideoMode mode(rect);
Settings::getInstance().setScreenGrabbed(true); Settings::getInstance().setScreenGrabbed(true);
bodyUI->videoModescomboBox->setCurrentIndex(videoModes.size() - 1); videoModescomboBox->setCurrentIndex(videoModes.size() - 1);
open(devName, mode); open(devName, mode);
return; return;
} }
@ -384,7 +380,7 @@ void AVForm::updateVideoModes(int curIndex)
// If we picked the lowest resolution, the quality would be awful // If we picked the lowest resolution, the quality would be awful
// but if we picked the largest, FPS would be bad and thus quality bad too. // but if we picked the largest, FPS would be bad and thus quality bad too.
int mid = (videoModes.size() - 1) / 2; int mid = (videoModes.size() - 1) / 2;
bodyUI->videoModescomboBox->setCurrentIndex(mid); videoModescomboBox->setCurrentIndex(mid);
} }
void AVForm::onVideoDevChanged(int index) void AVForm::onVideoDevChanged(int index)
@ -398,14 +394,14 @@ void AVForm::onVideoDevChanged(int index)
Settings::getInstance().setScreenGrabbed(false); Settings::getInstance().setScreenGrabbed(false);
QString dev = videoDeviceList[index].first; QString dev = videoDeviceList[index].first;
Settings::getInstance().setVideoDev(dev); Settings::getInstance().setVideoDev(dev);
bool previouslyBlocked = bodyUI->videoModescomboBox->blockSignals(true); bool previouslyBlocked = videoModescomboBox->blockSignals(true);
updateVideoModes(index); updateVideoModes(index);
bodyUI->videoModescomboBox->blockSignals(previouslyBlocked); videoModescomboBox->blockSignals(previouslyBlocked);
if (Settings::getInstance().getScreenGrabbed()) if (Settings::getInstance().getScreenGrabbed())
return; return;
int modeIndex = bodyUI->videoModescomboBox->currentIndex(); int modeIndex = videoModescomboBox->currentIndex();
VideoMode mode = VideoMode(); VideoMode mode = VideoMode();
if (0 < modeIndex && modeIndex < videoModes.size()) if (0 < modeIndex && modeIndex < videoModes.size())
mode = videoModes[modeIndex]; mode = videoModes[modeIndex];
@ -421,16 +417,16 @@ void AVForm::getVideoDevices()
int videoDevIndex = 0; int videoDevIndex = 0;
videoDeviceList = CameraDevice::getDeviceList(); videoDeviceList = CameraDevice::getDeviceList();
//prevent currentIndexChanged to be fired while adding items //prevent currentIndexChanged to be fired while adding items
bodyUI->videoDevCombobox->blockSignals(true); videoDevCombobox->blockSignals(true);
bodyUI->videoDevCombobox->clear(); videoDevCombobox->clear();
for (QPair<QString, QString> device : videoDeviceList) for (QPair<QString, QString> device : videoDeviceList)
{ {
bodyUI->videoDevCombobox->addItem(device.second); videoDevCombobox->addItem(device.second);
if (device.first == settingsInDev) if (device.first == settingsInDev)
videoDevIndex = bodyUI->videoDevCombobox->count()-1; videoDevIndex = videoDevCombobox->count()-1;
} }
bodyUI->videoDevCombobox->setCurrentIndex(videoDevIndex); videoDevCombobox->setCurrentIndex(videoDevIndex);
bodyUI->videoDevCombobox->blockSignals(false); videoDevCombobox->blockSignals(false);
updateVideoModes(videoDevIndex); updateVideoModes(videoDevIndex);
} }
@ -439,15 +435,15 @@ void AVForm::getAudioInDevices()
QStringList deviceNames; QStringList deviceNames;
deviceNames << tr("Disabled") << Audio::inDeviceNames(); deviceNames << tr("Disabled") << Audio::inDeviceNames();
bodyUI->inDevCombobox->blockSignals(true); inDevCombobox->blockSignals(true);
bodyUI->inDevCombobox->clear(); inDevCombobox->clear();
bodyUI->inDevCombobox->addItems(deviceNames); inDevCombobox->addItems(deviceNames);
bodyUI->inDevCombobox->blockSignals(false); inDevCombobox->blockSignals(false);
int idx = Settings::getInstance().getAudioInDevEnabled() int idx = Settings::getInstance().getAudioInDevEnabled()
? deviceNames.indexOf(Settings::getInstance().getInDev()) ? deviceNames.indexOf(Settings::getInstance().getInDev())
: 0; : 0;
bodyUI->inDevCombobox->setCurrentIndex(idx < 0 ? 1 : idx); inDevCombobox->setCurrentIndex(idx < 0 ? 1 : idx);
} }
void AVForm::getAudioOutDevices() void AVForm::getAudioOutDevices()
@ -455,15 +451,15 @@ void AVForm::getAudioOutDevices()
QStringList deviceNames; QStringList deviceNames;
deviceNames << tr("Disabled") << Audio::outDeviceNames(); deviceNames << tr("Disabled") << Audio::outDeviceNames();
bodyUI->outDevCombobox->blockSignals(true); outDevCombobox->blockSignals(true);
bodyUI->outDevCombobox->clear(); outDevCombobox->clear();
bodyUI->outDevCombobox->addItems(deviceNames); outDevCombobox->addItems(deviceNames);
bodyUI->outDevCombobox->blockSignals(false); outDevCombobox->blockSignals(false);
int idx = Settings::getInstance().getAudioOutDevEnabled() int idx = Settings::getInstance().getAudioOutDevEnabled()
? deviceNames.indexOf(Settings::getInstance().getOutDev()) ? deviceNames.indexOf(Settings::getInstance().getOutDev())
: 0; : 0;
bodyUI->outDevCombobox->setCurrentIndex(idx < 0 ? 1 : idx); outDevCombobox->setCurrentIndex(idx < 0 ? 1 : idx);
} }
void AVForm::onAudioInDevChanged(int deviceIndex) void AVForm::onAudioInDevChanged(int deviceIndex)
@ -472,14 +468,14 @@ void AVForm::onAudioInDevChanged(int deviceIndex)
QString deviceName; QString deviceName;
if (deviceIndex > 0) if (deviceIndex > 0)
deviceName = bodyUI->inDevCombobox->itemText(deviceIndex); deviceName = inDevCombobox->itemText(deviceIndex);
Settings::getInstance().setInDev(deviceName); Settings::getInstance().setInDev(deviceName);
Audio& audio = Audio::getInstance(); Audio& audio = Audio::getInstance();
audio.reinitInput(deviceName); audio.reinitInput(deviceName);
bodyUI->microphoneSlider->setEnabled(deviceIndex > 0); microphoneSlider->setEnabled(deviceIndex > 0);
bodyUI->microphoneSlider->setSliderPosition(qRound(audio.inputGain() * 10.0)); microphoneSlider->setSliderPosition(qRound(audio.inputGain() * 10.0));
} }
void AVForm::onAudioOutDevChanged(int deviceIndex) void AVForm::onAudioOutDevChanged(int deviceIndex)
@ -488,22 +484,23 @@ void AVForm::onAudioOutDevChanged(int deviceIndex)
QString deviceName; QString deviceName;
if (deviceIndex > 0) if (deviceIndex > 0)
deviceName = bodyUI->outDevCombobox->itemText(deviceIndex); deviceName = outDevCombobox->itemText(deviceIndex);
Settings::getInstance().setOutDev(deviceName); Settings::getInstance().setOutDev(deviceName);
Audio& audio = Audio::getInstance(); Audio& audio = Audio::getInstance();
audio.reinitOutput(deviceName); audio.reinitOutput(deviceName);
bodyUI->playbackSlider->setEnabled(deviceIndex > 0); playbackSlider->setEnabled(deviceIndex > 0);
bodyUI->playbackSlider->setSliderPosition(qRound(audio.outputVolume() * 100.0)); playbackSlider->setSliderPosition(qRound(audio.outputVolume() * 100.0));
} }
void AVForm::onPlaybackValueChanged(int value) void AVForm::on_playbackSlider_valueChanged(int value)
{ {
Settings::getInstance().setOutVolume(value); Settings::getInstance().setOutVolume(value);
Audio& audio = Audio::getInstance(); Audio& audio = Audio::getInstance();
if (audio.isOutputReady()) { if (audio.isOutputReady())
{
const qreal percentage = value / 100.0; const qreal percentage = value / 100.0;
audio.setOutputVolume(percentage); audio.setOutputVolume(percentage);
@ -524,11 +521,11 @@ void AVForm::createVideoSurface()
{ {
if (camVideoSurface) if (camVideoSurface)
return; return;
camVideoSurface = new VideoSurface(QPixmap(), bodyUI->CamFrame); camVideoSurface = new VideoSurface(QPixmap(), CamFrame);
camVideoSurface->setObjectName(QStringLiteral("CamVideoSurface")); camVideoSurface->setObjectName(QStringLiteral("CamVideoSurface"));
camVideoSurface->setMinimumSize(QSize(160, 120)); camVideoSurface->setMinimumSize(QSize(160, 120));
camVideoSurface->setSource(&camera); camVideoSurface->setSource(&camera);
bodyUI->gridLayout->addWidget(camVideoSurface, 0, 0, 1, 1); gridLayout->addWidget(camVideoSurface, 0, 0, 1, 1);
} }
void AVForm::killVideoSurface() void AVForm::killVideoSurface()
@ -536,7 +533,7 @@ void AVForm::killVideoSurface()
if (!camVideoSurface) if (!camVideoSurface)
return; return;
QLayoutItem *child; QLayoutItem *child;
while ((child = bodyUI->gridLayout->takeAt(0)) != 0) while ((child = gridLayout->takeAt(0)) != 0)
delete child; delete child;
camVideoSurface->close(); camVideoSurface->close();
@ -557,7 +554,7 @@ bool AVForm::eventFilter(QObject *o, QEvent *e)
void AVForm::retranslateUi() void AVForm::retranslateUi()
{ {
bodyUI->retranslateUi(this); Ui::AVForm::retranslateUi(this);
} }
void AVForm::on_btnPlayTestSound_clicked(bool checked) void AVForm::on_btnPlayTestSound_clicked(bool checked)

View File

@ -23,17 +23,16 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QList> #include <QList>
#include "genericsettings.h" #include "genericsettings.h"
#include "ui_avform.h"
#include "src/video/videomode.h" #include "src/video/videomode.h"
namespace Ui {
class AVSettings;
}
class CameraSource; class CameraSource;
class VideoSurface; class VideoSurface;
class AVForm : public GenericForm class AVForm : public GenericForm, private Ui::AVForm
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -61,7 +60,7 @@ private slots:
// audio // audio
void onAudioInDevChanged(int deviceIndex); void onAudioInDevChanged(int deviceIndex);
void onAudioOutDevChanged(int deviceIndex); void onAudioOutDevChanged(int deviceIndex);
void onPlaybackValueChanged(int value); void on_playbackSlider_valueChanged(int value);
void onMicrophoneValueChanged(int value); void onMicrophoneValueChanged(int value);
// camera // camera
@ -80,7 +79,6 @@ private:
void open(const QString &devName, const VideoMode &mode); void open(const QString &devName, const VideoMode &mode);
private: private:
Ui::AVSettings *bodyUI;
bool subscribedToAudioIn; bool subscribedToAudioIn;
bool mPlayTestSound; bool mPlayTestSound;
VideoSurface *camVideoSurface; VideoSurface *camVideoSurface;

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>AVSettings</class> <class>AVForm</class>
<widget class="QWidget" name="AVSettings"> <widget class="QWidget" name="AVForm">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>