yolobs-studio/UI/window-basic-properties.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

522 lines
14 KiB
C++
Raw Normal View History

2016-02-23 23:16:51 +00:00
/******************************************************************************
Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
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, either version 2 of the License, or
(at your option) any later version.
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 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 "obs-app.hpp"
#include "window-basic-properties.hpp"
#include "window-basic-main.hpp"
#include "qt-wrappers.hpp"
#include "display-helpers.hpp"
#include "properties-view.hpp"
#include <QCloseEvent>
#include <QScreen>
#include <QWindow>
#include <QMessageBox>
using namespace std;
2019-09-22 21:19:10 +00:00
static void CreateTransitionScene(OBSSource scene, const char *text,
uint32_t color);
2019-07-27 12:47:10 +00:00
2016-02-23 23:16:51 +00:00
OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
2019-09-22 21:19:10 +00:00
: QDialog(parent),
preview(new OBSQTDisplay(this)),
main(qobject_cast<OBSBasic *>(parent)),
acceptClicked(false),
source(source_),
removedSignal(obs_source_get_signal_handler(source), "remove",
OBSBasicProperties::SourceRemoved, this),
renamedSignal(obs_source_get_signal_handler(source), "rename",
OBSBasicProperties::SourceRenamed, this),
oldSettings(obs_data_create()),
buttonBox(new QDialogButtonBox(this))
2016-02-23 23:16:51 +00:00
{
int cx = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
2019-09-22 21:19:10 +00:00
"cx");
2016-02-23 23:16:51 +00:00
int cy = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
2019-09-22 21:19:10 +00:00
"cy");
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
enum obs_source_type type = obs_source_get_type(source);
2016-02-23 23:16:51 +00:00
buttonBox->setObjectName(QStringLiteral("buttonBox"));
2018-02-19 19:54:37 +00:00
buttonBox->setStandardButtons(QDialogButtonBox::Ok |
2019-09-22 21:19:10 +00:00
QDialogButtonBox::Cancel |
QDialogButtonBox::RestoreDefaults);
2018-02-19 19:54:37 +00:00
buttonBox->button(QDialogButtonBox::Ok)->setText(QTStr("OK"));
buttonBox->button(QDialogButtonBox::Cancel)->setText(QTStr("Cancel"));
2019-09-22 21:19:10 +00:00
buttonBox->button(QDialogButtonBox::RestoreDefaults)
->setText(QTStr("Defaults"));
2016-02-23 23:16:51 +00:00
if (cx > 400 && cy > 400)
resize(cx, cy);
else
resize(720, 580);
2019-07-27 12:47:10 +00:00
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
2016-02-23 23:16:51 +00:00
QMetaObject::connectSlotsByName(this);
/* The OBSData constructor increments the reference once */
obs_data_release(oldSettings);
OBSData settings = obs_source_get_settings(source);
obs_data_apply(oldSettings, settings);
obs_data_release(settings);
2019-09-22 21:19:10 +00:00
view = new OBSPropertiesView(
settings, source,
(PropertiesReloadCallback)obs_source_properties,
(PropertiesUpdateCallback)obs_source_update);
2017-04-19 19:54:15 +00:00
view->setMinimumHeight(150);
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
preview->setMinimumSize(20, 150);
2019-09-22 21:19:10 +00:00
preview->setSizePolicy(
QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
// Create a QSplitter to keep a unified workflow here.
windowSplitter = new QSplitter(Qt::Orientation::Vertical, this);
windowSplitter->addWidget(preview);
windowSplitter->addWidget(view);
windowSplitter->setChildrenCollapsible(false);
//windowSplitter->setSizes(QList<int>({ 16777216, 150 }));
windowSplitter->setStretchFactor(0, 3);
windowSplitter->setStretchFactor(1, 1);
2016-02-23 23:16:51 +00:00
setLayout(new QVBoxLayout(this));
2017-04-19 19:54:15 +00:00
layout()->addWidget(windowSplitter);
2019-07-27 12:47:10 +00:00
if (type == OBS_SOURCE_TYPE_TRANSITION) {
AddPreviewButton();
2019-09-22 21:19:10 +00:00
connect(view, SIGNAL(PropertiesRefreshed()), this,
SLOT(AddPreviewButton()));
2019-07-27 12:47:10 +00:00
}
2016-02-23 23:16:51 +00:00
layout()->addWidget(buttonBox);
2018-02-19 19:54:37 +00:00
layout()->setAlignment(buttonBox, Qt::AlignBottom);
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
view->show();
2016-02-23 23:16:51 +00:00
installEventFilter(CreateShortcutFilter());
const char *name = obs_source_get_name(source);
setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
obs_source_inc_showing(source);
updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
2019-09-22 21:19:10 +00:00
"update_properties",
OBSBasicProperties::UpdateProperties,
this);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
auto addDrawCallback = [this]() {
2016-02-23 23:16:51 +00:00
obs_display_add_draw_callback(preview->GetDisplay(),
2019-09-22 21:19:10 +00:00
OBSBasicProperties::DrawPreview,
this);
2016-02-23 23:16:51 +00:00
};
2019-09-22 21:19:10 +00:00
auto addTransitionDrawCallback = [this]() {
obs_display_add_draw_callback(
preview->GetDisplay(),
2019-07-27 12:47:10 +00:00
OBSBasicProperties::DrawTransitionPreview, this);
};
2016-05-24 19:53:01 +00:00
uint32_t caps = obs_source_get_output_flags(source);
bool drawable_type = type == OBS_SOURCE_TYPE_INPUT ||
2019-09-22 21:19:10 +00:00
type == OBS_SOURCE_TYPE_SCENE;
2018-12-16 16:14:58 +00:00
bool drawable_preview = (caps & OBS_SOURCE_VIDEO) != 0;
2016-05-24 19:53:01 +00:00
2018-12-16 16:14:58 +00:00
if (drawable_preview && drawable_type) {
preview->show();
2016-05-24 19:53:01 +00:00
connect(preview.data(), &OBSQTDisplay::DisplayCreated,
2019-09-22 21:19:10 +00:00
addDrawCallback);
2019-07-27 12:47:10 +00:00
} else if (type == OBS_SOURCE_TYPE_TRANSITION) {
2019-09-22 21:19:10 +00:00
sourceA =
obs_source_create_private("scene", "sourceA", nullptr);
sourceB =
obs_source_create_private("scene", "sourceB", nullptr);
2019-07-27 12:47:10 +00:00
obs_source_release(sourceA);
obs_source_release(sourceB);
uint32_t colorA = 0xFFB26F52;
uint32_t colorB = 0xFF6FB252;
CreateTransitionScene(sourceA, "A", colorA);
CreateTransitionScene(sourceB, "B", colorB);
/**
* The cloned source is made from scratch, rather than using
* obs_source_duplicate, as the stinger transition would not
* play correctly otherwise.
*/
obs_data_t *settings = obs_source_get_settings(source);
sourceClone = obs_source_create_private(
2019-09-22 21:19:10 +00:00
obs_source_get_id(source), "clone", settings);
2019-07-27 12:47:10 +00:00
obs_source_release(sourceClone);
obs_source_inc_active(sourceClone);
obs_transition_set(sourceClone, sourceA);
obs_data_release(settings);
2019-09-22 21:19:10 +00:00
auto updateCallback = [=]() {
2019-07-27 12:47:10 +00:00
obs_data_t *settings = obs_source_get_settings(source);
obs_source_update(sourceClone, settings);
obs_transition_clear(sourceClone);
obs_transition_set(sourceClone, sourceA);
obs_transition_force_stop(sourceClone);
obs_data_release(settings);
direction = true;
};
connect(view, &OBSPropertiesView::Changed, updateCallback);
preview->show();
connect(preview.data(), &OBSQTDisplay::DisplayCreated,
addTransitionDrawCallback);
2018-12-16 16:14:58 +00:00
} else {
preview->hide();
}
2016-02-23 23:16:51 +00:00
}
OBSBasicProperties::~OBSBasicProperties()
{
2019-07-27 12:47:10 +00:00
if (sourceClone) {
obs_source_dec_active(sourceClone);
}
2016-02-23 23:16:51 +00:00
obs_source_dec_showing(source);
main->SaveProject();
}
2019-07-27 12:47:10 +00:00
void OBSBasicProperties::AddPreviewButton()
{
2019-09-22 21:19:10 +00:00
QPushButton *playButton =
new QPushButton(QTStr("PreviewTransition"), this);
2019-07-27 12:47:10 +00:00
VScrollArea *area = view;
area->widget()->layout()->addWidget(playButton);
playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
2019-09-22 21:19:10 +00:00
auto play = [=]() {
2019-07-27 12:47:10 +00:00
OBSSource start;
OBSSource end;
if (direction) {
start = sourceA;
end = sourceB;
} else {
start = sourceB;
end = sourceA;
}
obs_transition_set(sourceClone, start);
2019-09-22 21:19:10 +00:00
obs_transition_start(sourceClone, OBS_TRANSITION_MODE_AUTO,
main->GetTransitionDuration(), end);
2019-07-27 12:47:10 +00:00
direction = !direction;
start = nullptr;
end = nullptr;
};
connect(playButton, &QPushButton::clicked, play);
}
static obs_source_t *CreateLabel(const char *name, size_t h)
{
obs_data_t *settings = obs_data_create();
obs_data_t *font = obs_data_create();
std::string text;
text += " ";
text += name;
text += " ";
#if defined(_WIN32)
obs_data_set_string(font, "face", "Arial");
#elif defined(__APPLE__)
obs_data_set_string(font, "face", "Helvetica");
#else
obs_data_set_string(font, "face", "Monospace");
#endif
obs_data_set_int(font, "flags", 1); // Bold text
obs_data_set_int(font, "size", min(int(h), 300));
obs_data_set_obj(settings, "font", font);
obs_data_set_string(settings, "text", text.c_str());
obs_data_set_bool(settings, "outline", false);
#ifdef _WIN32
const char *text_source_id = "text_gdiplus";
#else
const char *text_source_id = "text_ft2_source";
#endif
2019-09-22 21:19:10 +00:00
obs_source_t *txtSource =
obs_source_create_private(text_source_id, name, settings);
2019-07-27 12:47:10 +00:00
obs_data_release(font);
obs_data_release(settings);
return txtSource;
}
2019-09-22 21:19:10 +00:00
static void CreateTransitionScene(OBSSource scene, const char *text,
uint32_t color)
2019-07-27 12:47:10 +00:00
{
obs_data_t *settings = obs_data_create();
obs_data_set_int(settings, "width", obs_source_get_width(scene));
obs_data_set_int(settings, "height", obs_source_get_height(scene));
obs_data_set_int(settings, "color", color);
2019-09-22 21:19:10 +00:00
obs_source_t *colorBG = obs_source_create_private(
"color_source", "background", settings);
2019-07-27 12:47:10 +00:00
obs_scene_add(obs_scene_from_source(scene), colorBG);
obs_source_t *label = CreateLabel(text, obs_source_get_height(scene));
2019-09-22 21:19:10 +00:00
obs_sceneitem_t *item =
obs_scene_add(obs_scene_from_source(scene), label);
2019-07-27 12:47:10 +00:00
vec2 size;
vec2_set(&size, obs_source_get_width(scene),
#ifdef _WIN32
2019-09-22 21:19:10 +00:00
obs_source_get_height(scene));
2019-07-27 12:47:10 +00:00
#else
2019-09-22 21:19:10 +00:00
obs_source_get_height(scene) * 0.8);
2019-07-27 12:47:10 +00:00
#endif
obs_sceneitem_set_bounds(item, &size);
obs_sceneitem_set_bounds_type(item, OBS_BOUNDS_SCALE_INNER);
obs_data_release(settings);
obs_source_release(colorBG);
obs_source_release(label);
}
2016-02-23 23:16:51 +00:00
void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data),
"close");
2016-02-23 23:16:51 +00:00
UNUSED_PARAMETER(params);
}
void OBSBasicProperties::SourceRenamed(void *data, calldata_t *params)
{
const char *name = calldata_string(params, "new_name");
QString title = QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name));
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data),
"setWindowTitle", Q_ARG(QString, title));
2016-02-23 23:16:51 +00:00
}
void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
{
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data)->view,
"ReloadProperties");
2016-02-23 23:16:51 +00:00
}
void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
{
QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);
if (val == QDialogButtonBox::AcceptRole) {
acceptClicked = true;
close();
if (view->DeferUpdate())
view->UpdateSettings();
2018-02-19 19:54:37 +00:00
} else if (val == QDialogButtonBox::RejectRole) {
2016-02-23 23:16:51 +00:00
obs_data_t *settings = obs_source_get_settings(source);
obs_data_clear(settings);
obs_data_release(settings);
if (view->DeferUpdate())
obs_data_apply(settings, oldSettings);
else
obs_source_update(source, oldSettings);
close();
2018-02-19 19:54:37 +00:00
} else if (val == QDialogButtonBox::ResetRole) {
obs_data_t *settings = obs_source_get_settings(source);
obs_data_clear(settings);
obs_data_release(settings);
if (!view->DeferUpdate())
obs_source_update(source, nullptr);
2019-07-27 12:47:10 +00:00
view->ReloadProperties();
2016-02-23 23:16:51 +00:00
}
}
void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
{
2019-09-22 21:19:10 +00:00
OBSBasicProperties *window = static_cast<OBSBasicProperties *>(data);
2016-02-23 23:16:51 +00:00
if (!window->source)
return;
uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
2019-09-22 21:19:10 +00:00
int x, y;
int newCX, newCY;
2016-02-23 23:16:51 +00:00
float scale;
GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
newCX = int(scale * float(sourceCX));
newCY = int(scale * float(sourceCY));
gs_viewport_push();
gs_projection_push();
2019-09-22 21:19:10 +00:00
gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
2016-02-23 23:16:51 +00:00
gs_set_viewport(x, y, newCX, newCY);
obs_source_video_render(window->source);
gs_projection_pop();
gs_viewport_pop();
}
2019-07-27 12:47:10 +00:00
void OBSBasicProperties::DrawTransitionPreview(void *data, uint32_t cx,
2019-09-22 21:19:10 +00:00
uint32_t cy)
2019-07-27 12:47:10 +00:00
{
2019-09-22 21:19:10 +00:00
OBSBasicProperties *window = static_cast<OBSBasicProperties *>(data);
2019-07-27 12:47:10 +00:00
if (!window->source)
return;
uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
2019-09-22 21:19:10 +00:00
int x, y;
int newCX, newCY;
2019-07-27 12:47:10 +00:00
float scale;
GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
newCX = int(scale * float(sourceCX));
newCY = int(scale * float(sourceCY));
gs_viewport_push();
gs_projection_push();
2019-09-22 21:19:10 +00:00
gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
2019-07-27 12:47:10 +00:00
gs_set_viewport(x, y, newCX, newCY);
obs_source_video_render(window->sourceClone);
gs_projection_pop();
gs_viewport_pop();
}
2016-02-23 23:16:51 +00:00
void OBSBasicProperties::Cleanup()
{
config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
2019-09-22 21:19:10 +00:00
width());
2016-02-23 23:16:51 +00:00
config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
2019-09-22 21:19:10 +00:00
height());
2016-05-24 19:53:01 +00:00
obs_display_remove_draw_callback(preview->GetDisplay(),
2019-09-22 21:19:10 +00:00
OBSBasicProperties::DrawPreview, this);
obs_display_remove_draw_callback(
preview->GetDisplay(),
2019-07-27 12:47:10 +00:00
OBSBasicProperties::DrawTransitionPreview, this);
2016-02-23 23:16:51 +00:00
}
void OBSBasicProperties::reject()
{
if (!acceptClicked && (CheckSettings() != 0)) {
if (!ConfirmQuit()) {
return;
}
}
Cleanup();
done(0);
}
void OBSBasicProperties::closeEvent(QCloseEvent *event)
{
if (!acceptClicked && (CheckSettings() != 0)) {
if (!ConfirmQuit()) {
event->ignore();
return;
}
}
QDialog::closeEvent(event);
if (!event->isAccepted())
return;
Cleanup();
}
void OBSBasicProperties::Init()
{
show();
}
int OBSBasicProperties::CheckSettings()
{
OBSData currentSettings = obs_source_get_settings(source);
const char *oldSettingsJson = obs_data_get_json(oldSettings);
const char *currentSettingsJson = obs_data_get_json(currentSettings);
int ret = strcmp(currentSettingsJson, oldSettingsJson);
obs_data_release(currentSettings);
return ret;
}
bool OBSBasicProperties::ConfirmQuit()
{
QMessageBox::StandardButton button;
2019-09-22 21:19:10 +00:00
button = OBSMessageBox::question(
this, QTStr("Basic.PropertiesWindow.ConfirmTitle"),
QTStr("Basic.PropertiesWindow.Confirm"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
2016-02-23 23:16:51 +00:00
switch (button) {
case QMessageBox::Save:
2018-02-19 19:54:37 +00:00
acceptClicked = true;
2016-02-23 23:16:51 +00:00
if (view->DeferUpdate())
view->UpdateSettings();
// Do nothing because the settings are already updated
break;
case QMessageBox::Discard:
obs_source_update(source, oldSettings);
break;
case QMessageBox::Cancel:
return false;
break;
default:
/* If somehow the dialog fails to show, just default to
* saving the settings. */
break;
}
return true;
}