Skip to content

Commit

Permalink
support automatic setting update on dialog accept
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 11, 2024
1 parent 2e573e3 commit e939c09
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@




class QgsSettingsEditorWidgetWrapper : QObject
{
%Docstring(signature="appended")
Expand Down Expand Up @@ -82,13 +83,17 @@ Sets the ``value`` of the widget
The wrapper must be configured before calling this medthod
%End

void enableAutomaticUpdate();
void enableAutomaticUpdate( QDialog *dialog = 0 );
%Docstring
Enables automatic update
so that the setting gets update
on value change in the widget and
sets the widget to the current settings value.
This must called after createEditor or configureEditor.
Enables automatic update, which causes the setting to be updated immediately when the widget
value is changed.

If a ``dialog`` is provided, the setting will be updated when the dialog is accpeted.
If not, the setting will be updated directly at each widget value change.

.. note::

This must called after :py:func:`~QgsSettingsEditorWidgetWrapper.createEditor` or :py:func:`~QgsSettingsEditorWidgetWrapper.configureEditor`.

.. versionadded:: 3.40
%End
Expand All @@ -107,9 +112,8 @@ Configures an existing ``editor`` widget

virtual void enableAutomaticUpdatePrivate() = 0;
%Docstring
Enables automatic update
so that the setting gets update
on value change in the widget.
Enables automatic update, which causes the setting to be updated immediately when the widget
value is changed.

.. versionadded:: 3.40
%End
Expand Down
17 changes: 17 additions & 0 deletions src/gui/settings/qgssettingseditorwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgslogger.h"
#include "qgssettingsentry.h"

#include <QDialog>
#include <QWidget>


Expand Down Expand Up @@ -59,3 +60,19 @@ bool QgsSettingsEditorWidgetWrapper::configureEditor( QWidget *editor, const Qgs

return ok;
}

void QgsSettingsEditorWidgetWrapper::enableAutomaticUpdate( QDialog *dialog )
{
setWidgetFromSetting();
if ( dialog )
{
QObject::connect( dialog, &QDialog::accepted, this, [ = ]()
{
setSettingFromWidget();
} );
}
else
{
enableAutomaticUpdatePrivate();
}
}
12 changes: 5 additions & 7 deletions src/gui/settings/qgssettingseditorwidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

class QgsSettingsEntryBase;

class QDialog;

/**
* \ingroup gui
* \brief Base class for settings editor wrappers
Expand Down Expand Up @@ -84,18 +86,14 @@ class GUI_EXPORT QgsSettingsEditorWidgetWrapper : public QObject
* Enables automatic update, which causes the setting to be updated immediately when the widget
* value is changed.
*
* Calling this method will set the widget's current value to match the current settings value.
* If a \a dialog is provided, the setting will be updated when the dialog is accpeted.
* If not, the setting will be updated directly at each widget value change.
*
* \note This must called after createEditor() or configureEditor().
* \warning Do NOT call this method from places where a widget is embedded in a dialog with a cancel button, as the auto-update logic will immediately overwrite the setting value and prevent rollback if the user cancels the dialog.
*
* \since QGIS 3.40
*/
void enableAutomaticUpdate()
{
setWidgetFromSetting();
enableAutomaticUpdatePrivate();
}
void enableAutomaticUpdate( QDialog *dialog = nullptr );


protected:
Expand Down

0 comments on commit e939c09

Please sign in to comment.