Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[settings] support direct connection from widget value change to upda… #58611

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3nids marked this conversation as resolved.
Show resolved Hide resolved
*/
void configureAutomaticUpdate( QDialog *dialog = nullptr );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I think this would be a bit better in my humble opinion :)

(also, you'll need a sip update, so good reason to do a final name tweak ;) )

Suggested change
void configureAutomaticUpdate( QDialog *dialog = nullptr );
void configureUpdateBehavior( QDialog *dialog = nullptr );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer configureAutomaticUpdate because it actually enables the auto update (not direct if dialog, direct if no dialog)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@3nids , I guess yeah automatic update in that it'll automatically take care of updates (live update vs dialog close update). OK.



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
Loading