Skip to content
Permalink
Browse files
Add api to allow validation of QgsOptionsPageWidget before applying
options dialog changes
  • Loading branch information
nyalldawson committed Jan 14, 2022
1 parent 18979e5 commit f288ab12238e0d26c7350416397f76ae11dbcd6c
Showing with 38 additions and 3 deletions.
  1. +9 −0 python/gui/auto_generated/qgsoptionswidgetfactory.sip.in
  2. +19 −2 src/app/options/qgsoptions.cpp
  3. +10 −1 src/gui/qgsoptionswidgetfactory.h
@@ -39,7 +39,16 @@ help will be retrieved.
%End


virtual bool isValid();
%Docstring
Validates the current state of the widget.

Subclasses should return ``True`` if the widget state is currently valid and acceptable to :py:func:`~QgsOptionsPageWidget.apply`.

The default implementation returns ``True``.

.. versionadded:: 3.24
%End

public slots:

@@ -166,6 +166,18 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
// switching vertical tabs between icon/text to icon-only modes (splitter collapsed to left),
// and connecting QDialogButtonBox's accepted/rejected signals to dialog's accept/reject slots
initOptionsBase( false );
// disconnect default connection setup by initOptionsBase for accepting dialog, and insert logic
// to validate widgets before allowing dialog to be closed
disconnect( mOptButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
connect( mOptButtonBox, &QDialogButtonBox::accepted, this, [ = ]
{
for ( QgsOptionsPageWidget *widget : std::as_const( mAdditionalOptionWidgets ) )
{
if ( !widget->isValid() )
return;
}
accept();
} );

// stylesheet setup
mStyleSheetBuilder = QgisApp::instance()->styleSheetBuilder();
@@ -1531,6 +1543,12 @@ void QgsOptions::selectProjectOnLaunch()

void QgsOptions::saveOptions()
{
for ( QgsOptionsPageWidget *widget : std::as_const( mAdditionalOptionWidgets ) )
{
if ( !widget->isValid() )
return;
}

QgsSettings settings;

mSettings->setValue( QStringLiteral( "UI/UITheme" ), cmbUITheme->currentText() );
@@ -1999,8 +2017,7 @@ void QgsOptions::saveOptions()

mLocatorOptionsWidget->commitChanges();

const auto constMAdditionalOptionWidgets = mAdditionalOptionWidgets;
for ( QgsOptionsPageWidget *widget : constMAdditionalOptionWidgets )
for ( QgsOptionsPageWidget *widget : std::as_const( mAdditionalOptionWidgets ) )
{
widget->apply();
}
@@ -52,13 +52,22 @@ class GUI_EXPORT QgsOptionsPageWidget : public QWidget
*/
virtual QString helpKey() const { return QString(); }


/**
* Returns the registered highlight widgets used to search and highlight text in
* options dialogs.
*/
QHash<QWidget *, QgsOptionsDialogHighlightWidget *> registeredHighlightWidgets() {return mHighlightWidgets;} SIP_SKIP

/**
* Validates the current state of the widget.
*
* Subclasses should return TRUE if the widget state is currently valid and acceptable to apply().
*
* The default implementation returns TRUE.
*
* \since QGIS 3.24
*/
virtual bool isValid() { return true; }

public slots:

0 comments on commit f288ab1

Please sign in to comment.