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:
Weikardzaena
2015-01-15 21:49:03 -08:00
committed by jp9000
parent d54b3cc8c0
commit 2152b40e68
4 changed files with 95 additions and 3 deletions
+76 -2
View File
@@ -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;
}