mirror of
https://github.com/obsproject/obs-studio.git
synced 2026-09-24 23:31:36 +08:00
UI: Add Okay/Cancel Buttons to Properties Dialog
Use QDialogButtonBox to add "Okay" and "Cancel" buttons to the properties dialog. The core functionality of the dialog is not changed; I.E. the settings are still applied to the source as the user changes them. If the user clicks "Okay", the dialog simply exits. If the user clicks "Cancel", the original settings are reapplied to the source then the dialog exits. If the window is closed by any other means (I.E. by the main obs window closing) then the properties dialog prompts the user if they changed anything and asks if they wish to save their settings. In order to implement this last feature, a method of checking for open dialogs and sending each a quit message is added to the closeEvent() method for OBSBasic.
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#include <QCloseEvent>
|
||||
#include <QScreen>
|
||||
#include <QWindow>
|
||||
#include <QMessageBox>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -31,6 +32,7 @@ OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
|
||||
: QDialog (parent),
|
||||
main (qobject_cast<OBSBasic*>(parent)),
|
||||
resizeTimer (0),
|
||||
acceptClicked (false),
|
||||
ui (new Ui::OBSBasicProperties),
|
||||
source (source_),
|
||||
removedSignal (obs_source_get_signal_handler(source),
|
||||
@@ -39,20 +41,26 @@ OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
|
||||
updatePropertiesSignal (obs_source_get_signal_handler(source),
|
||||
"update_properties",
|
||||
OBSBasicProperties::UpdateProperties,
|
||||
this)
|
||||
|
||||
this),
|
||||
buttonBox (new QDialogButtonBox(this)),
|
||||
oldSettings (obs_data_create())
|
||||
{
|
||||
int cx = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
|
||||
"cx");
|
||||
int cy = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
|
||||
"cy");
|
||||
|
||||
buttonBox->setStandardButtons(QDialogButtonBox::Ok |
|
||||
QDialogButtonBox::Cancel);
|
||||
buttonBox->setObjectName(QStringLiteral("buttonBox"));
|
||||
|
||||
ui->setupUi(this);
|
||||
|
||||
if (cx > 400 && cy > 400)
|
||||
resize(cx, cy);
|
||||
|
||||
OBSData settings = obs_source_get_settings(source);
|
||||
obs_data_apply(oldSettings, settings);
|
||||
obs_data_release(settings);
|
||||
|
||||
view = new OBSPropertiesView(settings, source,
|
||||
@@ -60,6 +68,8 @@ OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
|
||||
(PropertiesUpdateCallback)obs_source_update);
|
||||
|
||||
layout()->addWidget(view);
|
||||
layout()->addWidget(buttonBox);
|
||||
layout()->setAlignment(buttonBox, Qt::AlignRight | Qt::AlignBottom);
|
||||
layout()->setAlignment(view, Qt::AlignBottom);
|
||||
view->setMaximumHeight(250);
|
||||
view->setMinimumHeight(150);
|
||||
@@ -92,6 +102,21 @@ void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
|
||||
"ReloadProperties");
|
||||
}
|
||||
|
||||
void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
|
||||
{
|
||||
QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);
|
||||
|
||||
if (val == QDialogButtonBox::AcceptRole) {
|
||||
acceptClicked = true;
|
||||
close();
|
||||
}
|
||||
|
||||
if (val == QDialogButtonBox::RejectRole) {
|
||||
obs_source_update(source, oldSettings);
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
|
||||
{
|
||||
OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);
|
||||
@@ -154,10 +179,19 @@ void OBSBasicProperties::timerEvent(QTimerEvent *event)
|
||||
|
||||
void OBSBasicProperties::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
if (!acceptClicked && (CheckSettings() != 0)) {
|
||||
if (!ConfirmQuit()) {
|
||||
event->ignore();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QDialog::closeEvent(event);
|
||||
if (!event->isAccepted())
|
||||
return;
|
||||
|
||||
obs_data_release(oldSettings);
|
||||
|
||||
// remove draw callback and release display in case our drawable
|
||||
// surfaces go away before the destructor gets called
|
||||
obs_display_remove_draw_callback(display,
|
||||
@@ -188,3 +222,43 @@ void OBSBasicProperties::Init()
|
||||
obs_display_add_draw_callback(display,
|
||||
OBSBasicProperties::DrawPreview, this);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
button = QMessageBox::question(this,
|
||||
QTStr("Basic.PropertiesWindow.ConfirmTitle"),
|
||||
QTStr("Basic.PropertiesWindow.Confirm"),
|
||||
QMessageBox::Save | QMessageBox::Discard |
|
||||
QMessageBox::Cancel);
|
||||
|
||||
switch (button) {
|
||||
case QMessageBox::Save:
|
||||
// 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user