Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add api to allow validation of QgsOptionsPageWidget before applying
options dialog changes
  • Loading branch information
nyalldawson committed Jan 14, 2022
1 parent 18979e5 commit f288ab1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
9 changes: 9 additions & 0 deletions python/gui/auto_generated/qgsoptionswidgetfactory.sip.in
Expand Up @@ -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:

Expand Down
21 changes: 19 additions & 2 deletions src/app/options/qgsoptions.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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();
}
Expand Down
11 changes: 10 additions & 1 deletion src/gui/qgsoptionswidgetfactory.h
Expand Up @@ -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:

Expand Down

0 comments on commit f288ab1

Please sign in to comment.