yolobs-studio/UI/window-basic-adv-audio.cpp

297 lines
8.2 KiB
C++
Raw Normal View History

2016-02-23 23:16:51 +00:00
#include <QVBoxLayout>
2017-06-29 19:01:10 +00:00
#include <QHBoxLayout>
2016-02-23 23:16:51 +00:00
#include <QGridLayout>
#include <QScrollArea>
2017-06-29 19:01:10 +00:00
#include <QPushButton>
2016-02-23 23:16:51 +00:00
#include <QLabel>
#include "window-basic-adv-audio.hpp"
#include "window-basic-main.hpp"
2018-12-16 16:14:58 +00:00
#include "item-widget-helpers.hpp"
2016-02-23 23:16:51 +00:00
#include "adv-audio-control.hpp"
#include "obs-app.hpp"
#include "qt-wrappers.hpp"
Q_DECLARE_METATYPE(OBSSource);
OBSBasicAdvAudio::OBSBasicAdvAudio(QWidget *parent)
: QDialog(parent),
sourceAddedSignal(obs_get_signal_handler(), "source_activate",
2019-09-22 21:19:10 +00:00
OBSSourceAdded, this),
2016-02-23 23:16:51 +00:00
sourceRemovedSignal(obs_get_signal_handler(), "source_deactivate",
2020-03-25 08:07:22 +00:00
OBSSourceRemoved, this),
showInactive(false)
2016-02-23 23:16:51 +00:00
{
QScrollArea *scrollArea;
QVBoxLayout *vlayout;
QWidget *widget;
QLabel *label;
2017-04-19 19:54:15 +00:00
int idx = 0;
2016-02-23 23:16:51 +00:00
mainLayout = new QGridLayout;
mainLayout->setContentsMargins(0, 0, 0, 0);
2020-03-25 08:07:22 +00:00
label = new QLabel("");
mainLayout->addWidget(label, 0, idx++);
2016-02-23 23:16:51 +00:00
label = new QLabel(QTStr("Basic.AdvAudio.Name"));
2019-09-22 21:19:10 +00:00
label->setStyleSheet("font-weight: bold;");
2017-04-19 19:54:15 +00:00
mainLayout->addWidget(label, 0, idx++);
2020-03-25 08:07:22 +00:00
label = new QLabel(QTStr("Basic.Stats.Status"));
label->setStyleSheet("font-weight: bold;");
mainLayout->addWidget(label, 0, idx++);
2016-02-23 23:16:51 +00:00
label = new QLabel(QTStr("Basic.AdvAudio.Volume"));
2019-09-22 21:19:10 +00:00
label->setStyleSheet("font-weight: bold;");
2017-04-19 19:54:15 +00:00
mainLayout->addWidget(label, 0, idx++);
2016-02-23 23:16:51 +00:00
label = new QLabel(QTStr("Basic.AdvAudio.Mono"));
2019-09-22 21:19:10 +00:00
label->setStyleSheet("font-weight: bold;");
2017-04-19 19:54:15 +00:00
mainLayout->addWidget(label, 0, idx++);
2019-07-27 12:47:10 +00:00
label = new QLabel(QTStr("Basic.AdvAudio.Balance"));
2019-09-22 21:19:10 +00:00
label->setStyleSheet("font-weight: bold;");
2017-04-19 19:54:15 +00:00
mainLayout->addWidget(label, 0, idx++);
2016-02-23 23:16:51 +00:00
label = new QLabel(QTStr("Basic.AdvAudio.SyncOffset"));
2019-09-22 21:19:10 +00:00
label->setStyleSheet("font-weight: bold;");
2017-04-19 19:54:15 +00:00
mainLayout->addWidget(label, 0, idx++);
2018-02-19 19:54:37 +00:00
#if defined(_WIN32) || defined(__APPLE__) || HAVE_PULSEAUDIO
2017-04-19 19:54:15 +00:00
label = new QLabel(QTStr("Basic.AdvAudio.Monitoring"));
2019-09-22 21:19:10 +00:00
label->setStyleSheet("font-weight: bold;");
2017-04-19 19:54:15 +00:00
mainLayout->addWidget(label, 0, idx++);
#endif
2016-02-23 23:16:51 +00:00
label = new QLabel(QTStr("Basic.AdvAudio.AudioTracks"));
2019-09-22 21:19:10 +00:00
label->setStyleSheet("font-weight: bold;");
2017-04-19 19:54:15 +00:00
mainLayout->addWidget(label, 0, idx++);
2016-02-23 23:16:51 +00:00
controlArea = new QWidget;
controlArea->setLayout(mainLayout);
controlArea->setSizePolicy(QSizePolicy::Preferred,
2019-09-22 21:19:10 +00:00
QSizePolicy::Preferred);
2016-02-23 23:16:51 +00:00
vlayout = new QVBoxLayout;
vlayout->addWidget(controlArea);
//vlayout->setAlignment(controlArea, Qt::AlignTop);
widget = new QWidget;
widget->setLayout(vlayout);
widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
scrollArea = new QScrollArea;
scrollArea->setWidget(widget);
scrollArea->setWidgetResizable(true);
2017-06-29 19:01:10 +00:00
QPushButton *closeButton = new QPushButton(QTStr("Close"));
2020-03-25 08:07:22 +00:00
activeOnly = new QCheckBox();
activeOnly->setChecked(!showInactive);
activeOnly->setText(QTStr("Basic.AdvAudio.ActiveOnly"));
2017-06-29 19:01:10 +00:00
QHBoxLayout *buttonLayout = new QHBoxLayout;
2020-03-25 08:07:22 +00:00
buttonLayout->addWidget(activeOnly);
2017-06-29 19:01:10 +00:00
buttonLayout->addStretch();
buttonLayout->addWidget(closeButton);
2016-02-23 23:16:51 +00:00
vlayout = new QVBoxLayout;
vlayout->setContentsMargins(11, 11, 11, 11);
vlayout->addWidget(scrollArea);
2017-06-29 19:01:10 +00:00
vlayout->addLayout(buttonLayout);
2016-02-23 23:16:51 +00:00
setLayout(vlayout);
2020-03-25 08:07:22 +00:00
connect(activeOnly, SIGNAL(clicked(bool)), this,
SLOT(ActiveOnlyChanged(bool)));
2019-09-22 21:19:10 +00:00
connect(closeButton, &QPushButton::clicked, [this]() { close(); });
2017-06-29 19:01:10 +00:00
2016-02-23 23:16:51 +00:00
installEventFilter(CreateShortcutFilter());
/* enum user scene/sources */
obs_enum_sources(EnumSources, this);
2019-09-22 21:19:10 +00:00
resize(1100, 340);
2016-02-23 23:16:51 +00:00
setWindowTitle(QTStr("Basic.AdvAudio"));
2019-07-27 12:47:10 +00:00
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
2016-02-23 23:16:51 +00:00
setSizeGripEnabled(true);
setWindowModality(Qt::NonModal);
setAttribute(Qt::WA_DeleteOnClose, true);
2020-03-25 08:07:22 +00:00
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this,
SLOT(ShowContextMenu(const QPoint &)));
2016-02-23 23:16:51 +00:00
}
OBSBasicAdvAudio::~OBSBasicAdvAudio()
{
2019-09-22 21:19:10 +00:00
OBSBasic *main = reinterpret_cast<OBSBasic *>(parent());
2016-02-23 23:16:51 +00:00
for (size_t i = 0; i < controls.size(); ++i)
delete controls[i];
main->SaveProject();
}
bool OBSBasicAdvAudio::EnumSources(void *param, obs_source_t *source)
{
2019-09-22 21:19:10 +00:00
OBSBasicAdvAudio *dialog = reinterpret_cast<OBSBasicAdvAudio *>(param);
2016-02-23 23:16:51 +00:00
uint32_t flags = obs_source_get_output_flags(source);
2020-03-25 08:07:22 +00:00
if ((flags & OBS_SOURCE_AUDIO) != 0 &&
(dialog->showInactive || obs_source_active(source)))
2016-02-23 23:16:51 +00:00
dialog->AddAudioSource(source);
return true;
}
void OBSBasicAdvAudio::OBSSourceAdded(void *param, calldata_t *calldata)
{
2019-09-22 21:19:10 +00:00
OBSSource source((obs_source_t *)calldata_ptr(calldata, "source"));
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(reinterpret_cast<OBSBasicAdvAudio *>(param),
"SourceAdded", Q_ARG(OBSSource, source));
2016-02-23 23:16:51 +00:00
}
void OBSBasicAdvAudio::OBSSourceRemoved(void *param, calldata_t *calldata)
{
2019-09-22 21:19:10 +00:00
OBSSource source((obs_source_t *)calldata_ptr(calldata, "source"));
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(reinterpret_cast<OBSBasicAdvAudio *>(param),
"SourceRemoved", Q_ARG(OBSSource, source));
2016-02-23 23:16:51 +00:00
}
inline void OBSBasicAdvAudio::AddAudioSource(obs_source_t *source)
{
2020-03-25 08:07:22 +00:00
for (size_t i = 0; i < controls.size(); i++) {
if (controls[i]->GetSource() == source)
return;
}
2016-02-23 23:16:51 +00:00
OBSAdvAudioCtrl *control = new OBSAdvAudioCtrl(mainLayout, source);
2018-12-16 16:14:58 +00:00
InsertQObjectByName(controls, control);
for (auto control : controls) {
control->ShowAudioControl(mainLayout);
}
2016-02-23 23:16:51 +00:00
}
void OBSBasicAdvAudio::SourceAdded(OBSSource source)
{
uint32_t flags = obs_source_get_output_flags(source);
if ((flags & OBS_SOURCE_AUDIO) == 0)
return;
AddAudioSource(source);
}
void OBSBasicAdvAudio::SourceRemoved(OBSSource source)
{
uint32_t flags = obs_source_get_output_flags(source);
if ((flags & OBS_SOURCE_AUDIO) == 0)
return;
for (size_t i = 0; i < controls.size(); i++) {
if (controls[i]->GetSource() == source) {
delete controls[i];
controls.erase(controls.begin() + i);
break;
}
}
}
2020-03-25 08:07:22 +00:00
void OBSBasicAdvAudio::SetVolumeType()
{
QAction *action = reinterpret_cast<QAction *>(sender());
VolumeType type = (VolumeType)action->property("volumeType").toInt();
for (size_t i = 0; i < controls.size(); i++)
controls[i]->SetVolumeWidget(type);
config_set_int(GetGlobalConfig(), "BasicWindow", "AdvAudioVolumeType",
(int)type);
}
void OBSBasicAdvAudio::ShowContextMenu(const QPoint &pos)
{
VolumeType type = (VolumeType)config_get_int(
GetGlobalConfig(), "BasicWindow", "AdvAudioVolumeType");
QMenu *contextMenu = new QMenu(this);
QAction *percent = new QAction(QTStr("Percent"), this);
QAction *dB = new QAction(QTStr("dB"), this);
percent->setProperty("volumeType", (int)VolumeType::Percent);
dB->setProperty("volumeType", (int)VolumeType::dB);
connect(percent, SIGNAL(triggered()), this, SLOT(SetVolumeType()),
Qt::DirectConnection);
connect(dB, SIGNAL(triggered()), this, SLOT(SetVolumeType()),
Qt::DirectConnection);
percent->setCheckable(true);
dB->setCheckable(true);
if (type == VolumeType::Percent)
percent->setChecked(true);
else if (type == VolumeType::dB)
dB->setChecked(true);
contextMenu->addAction(dB);
contextMenu->addAction(percent);
contextMenu->exec(mapToGlobal(pos));
}
void OBSBasicAdvAudio::ActiveOnlyChanged(bool checked)
{
SetShowInactive(!checked);
}
void OBSBasicAdvAudio::SetShowInactive(bool show)
{
if (showInactive == show)
return;
showInactive = show;
activeOnly->setChecked(!showInactive);
sourceAddedSignal.Disconnect();
sourceRemovedSignal.Disconnect();
if (showInactive) {
sourceAddedSignal.Connect(obs_get_signal_handler(),
"source_create", OBSSourceAdded,
this);
sourceRemovedSignal.Connect(obs_get_signal_handler(),
"source_remove", OBSSourceRemoved,
this);
obs_enum_sources(EnumSources, this);
SetIconsVisible(showVisible);
} else {
sourceAddedSignal.Connect(obs_get_signal_handler(),
"source_activate", OBSSourceAdded,
this);
sourceRemovedSignal.Connect(obs_get_signal_handler(),
"source_deactivate",
OBSSourceRemoved, this);
for (size_t i = 0; i < controls.size(); i++) {
const auto source = controls[i]->GetSource();
if (!obs_source_active(source)) {
delete controls[i];
controls.erase(controls.begin() + i);
i--;
}
}
}
}
void OBSBasicAdvAudio::SetIconsVisible(bool visible)
{
showVisible = visible;
QLayoutItem *item = mainLayout->itemAtPosition(0, 0);
QLabel *headerLabel = qobject_cast<QLabel *>(item->widget());
visible ? headerLabel->show() : headerLabel->hide();
for (size_t i = 0; i < controls.size(); i++) {
controls[i]->SetIconVisible(visible);
}
}