Skip to content

Commit

Permalink
[settings] support direct connection from widget value change to upda… (
Browse files Browse the repository at this point in the history
qgis#58611)

* [settings] support direct connection from widget value change to update setting value

* support automatic setting update on dialog accept

* add settings entry

* better name + fix typo

* Update src/gui/settings/qgssettingseditorwidgetwrapper.h

Co-authored-by: Mathieu Pellerin <nirvn.asia@gmail.com>

* sipify

---------

Co-authored-by: Mathieu Pellerin <nirvn.asia@gmail.com>
  • Loading branch information
2 people authored and velle committed Sep 17, 2024
1 parent a73034e commit 028c168
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 1 deletion.
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,6 +83,21 @@ Sets the ``value`` of the widget
The wrapper must be configured before calling this medthod
%End

void configureAutomaticUpdate( QDialog *dialog = 0 );
%Docstring
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 accepted.
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


protected:
virtual QWidget *createEditorPrivate( QWidget *parent = 0 ) const = 0;
Expand All @@ -92,6 +108,14 @@ Creates the widgets
virtual bool configureEditorPrivate( QWidget *editor, const QgsSettingsEntryBase *setting ) = 0;
%Docstring
Configures an existing ``editor`` widget
%End

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

.. versionadded:: 3.40
%End

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Constructor

virtual bool setWidgetValue( const QString &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -145,6 +148,9 @@ Constructor

virtual bool setWidgetValue( const bool &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -181,6 +187,9 @@ Constructor

virtual bool setWidgetValue( const int &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -218,6 +227,9 @@ Constructor

virtual bool setWidgetValue( const double &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -258,6 +270,9 @@ Constructor

virtual void configureEditorPrivateImplementation();


virtual void enableAutomaticUpdatePrivate();

};


Expand Down
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,6 +83,21 @@ Sets the ``value`` of the widget
The wrapper must be configured before calling this medthod
%End

void configureAutomaticUpdate( QDialog *dialog = 0 );
%Docstring
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 accepted.
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


protected:
virtual QWidget *createEditorPrivate( QWidget *parent = 0 ) const = 0;
Expand All @@ -92,6 +108,14 @@ Creates the widgets
virtual bool configureEditorPrivate( QWidget *editor, const QgsSettingsEntryBase *setting ) = 0;
%Docstring
Configures an existing ``editor`` widget
%End

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

.. versionadded:: 3.40
%End

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Constructor

virtual bool setWidgetValue( const QString &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -145,6 +148,9 @@ Constructor

virtual bool setWidgetValue( const bool &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -181,6 +187,9 @@ Constructor

virtual bool setWidgetValue( const int &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -218,6 +227,9 @@ Constructor

virtual bool setWidgetValue( const double &value ) const;


virtual void enableAutomaticUpdatePrivate();

};


Expand Down Expand Up @@ -258,6 +270,9 @@ Constructor

virtual void configureEditorPrivateImplementation();


virtual void enableAutomaticUpdatePrivate();

};


Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define QGSGUI_H

#include "qgis_gui.h"
#include "qgssettingstree.h"
#include "qgis_sip.h"
#include <QWidget>
#include <memory>
Expand Down Expand Up @@ -64,6 +65,8 @@ class GUI_EXPORT QgsGui : public QObject

public:

static inline QgsSettingsTreeNode *sTtreeWidgetLastUsedValues = QgsSettingsTree::sTreeApp->createChildNode( QStringLiteral( "widget-last-used-values" ) ) SIP_SKIP;

/**
* Defines the behavior to use when setting the CRS for a newly created project.
*/
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::configureAutomaticUpdate( QDialog *dialog )
{
setWidgetFromSetting();
if ( dialog )
{
QObject::connect( dialog, &QDialog::accepted, this, [ = ]()
{
setSettingFromWidget();
} );
}
else
{
enableAutomaticUpdatePrivate();
}
}
21 changes: 21 additions & 0 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 @@ -80,6 +82,18 @@ class GUI_EXPORT QgsSettingsEditorWidgetWrapper : public QObject
*/
virtual void setWidgetFromVariant( const QVariant &value ) const = 0;

/**
* Configure the settings update behavior when a widget value is changed.
*
* If a \a dialog is provided, the setting will be updated when the dialog is accepted.
* If not, the setting will be updated directly at each widget value change.
*
* \note This must called after createEditor() or configureEditor().
*
* \since QGIS 3.40
*/
void configureAutomaticUpdate( QDialog *dialog = nullptr );


protected:
//! Creates the widgets
Expand All @@ -88,6 +102,13 @@ class GUI_EXPORT QgsSettingsEditorWidgetWrapper : public QObject
//! Configures an existing \a editor widget
virtual bool configureEditorPrivate( QWidget *editor, const QgsSettingsEntryBase *setting ) = 0;

/**
* Enables automatic update, which causes the setting to be updated immediately when the widget
* value is changed.
* \since QGIS 3.40
*/
virtual void enableAutomaticUpdatePrivate() = 0;

QStringList mDynamicKeyPartList;
};

Expand Down
40 changes: 40 additions & 0 deletions src/gui/settings/qgssettingseditorwidgetwrapperimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ bool QgsSettingsStringEditorWidgetWrapper::setWidgetValue( const QString &value
return false;
}

void QgsSettingsStringEditorWidgetWrapper::enableAutomaticUpdatePrivate()
{
QObject::connect( this->mEditor, &QLineEdit::textChanged, this, [ = ]( const QString & text )
{
this->mSetting->setValue( text, this->mDynamicKeyPartList );
} );
}

bool QgsSettingsStringEditorWidgetWrapper::setSettingFromWidget() const
{
if ( mEditor )
Expand Down Expand Up @@ -96,6 +104,14 @@ bool QgsSettingsBoolEditorWidgetWrapper::setWidgetValue( const bool &value ) con
return false;
}

void QgsSettingsBoolEditorWidgetWrapper::enableAutomaticUpdatePrivate()
{
QObject::connect( this->mEditor, &QCheckBox::clicked, this, [ = ]( bool checked )
{
this->mSetting->setValue( checked, this->mDynamicKeyPartList );
} );
}

bool QgsSettingsBoolEditorWidgetWrapper::setSettingFromWidget() const
{
if ( mEditor )
Expand Down Expand Up @@ -148,6 +164,14 @@ bool QgsSettingsIntegerEditorWidgetWrapper::setWidgetValue( const int &value ) c
return false;
}

void QgsSettingsIntegerEditorWidgetWrapper::enableAutomaticUpdatePrivate()
{
QObject::connect( this->mEditor, qOverload<int>( &QSpinBox::valueChanged ), this, [ = ]( int value )
{
this->mSetting->setValue( value, this->mDynamicKeyPartList );
} );
}

bool QgsSettingsIntegerEditorWidgetWrapper::setSettingFromWidget() const
{
if ( mEditor )
Expand Down Expand Up @@ -200,6 +224,14 @@ bool QgsSettingsDoubleEditorWidgetWrapper::setWidgetValue( const double &value )
return false;
}

void QgsSettingsDoubleEditorWidgetWrapper::enableAutomaticUpdatePrivate()
{
QObject::connect( this->mEditor, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [ = ]( double value )
{
this->mSetting->setValue( value, this->mDynamicKeyPartList );
} );
}

bool QgsSettingsDoubleEditorWidgetWrapper::setSettingFromWidget() const
{
if ( mEditor )
Expand Down Expand Up @@ -262,6 +294,14 @@ void QgsSettingsColorEditorWidgetWrapper::configureEditorPrivateImplementation()
}
}

void QgsSettingsColorEditorWidgetWrapper::enableAutomaticUpdatePrivate()
{
QObject::connect( this->mEditor, &QgsColorButton::colorChanged, this, [ = ]( const QColor & color )
{
this->mSetting->setValue( color, this->mDynamicKeyPartList );
} );
}

bool QgsSettingsColorEditorWidgetWrapper::setSettingFromWidget() const
{
if ( mEditor )
Expand Down
10 changes: 10 additions & 0 deletions src/gui/settings/qgssettingseditorwidgetwrapperimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class GUI_EXPORT QgsSettingsStringEditorWidgetWrapper : public QgsSettingsEditor
QString valueFromWidget() const override;

bool setWidgetValue( const QString &value ) const override;

void enableAutomaticUpdatePrivate() override;
};

/**
Expand All @@ -164,6 +166,8 @@ class GUI_EXPORT QgsSettingsBoolEditorWidgetWrapper : public QgsSettingsEditorWi
bool valueFromWidget() const override;

bool setWidgetValue( const bool &value ) const override;

void enableAutomaticUpdatePrivate() override;
};

/**
Expand All @@ -189,6 +193,8 @@ class GUI_EXPORT QgsSettingsIntegerEditorWidgetWrapper : public QgsSettingsEdito
int valueFromWidget() const override;

bool setWidgetValue( const int &value ) const override;

void enableAutomaticUpdatePrivate() override;
};


Expand All @@ -215,6 +221,8 @@ class GUI_EXPORT QgsSettingsDoubleEditorWidgetWrapper : public QgsSettingsEditor
double valueFromWidget() const override;

bool setWidgetValue( const double &value ) const override;

void enableAutomaticUpdatePrivate() override;
};


Expand Down Expand Up @@ -243,6 +251,8 @@ class GUI_EXPORT QgsSettingsColorEditorWidgetWrapper : public QgsSettingsEditorW
bool setWidgetValue( const QColor &value ) const override;

void configureEditorPrivateImplementation() override;

void enableAutomaticUpdatePrivate() override;
};


Expand Down
Loading

0 comments on commit 028c168

Please sign in to comment.