Add api to allow validation of QgsOptionsPageWidget before applying

options dialog changes
This commit is contained in:
Nyall Dawson 2022-01-14 12:43:12 +10:00
parent 18979e579c
commit f288ab1223
3 changed files with 38 additions and 3 deletions

View File

@ -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:

View File

@ -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();
}

View File

@ -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: