Skip to content

Commit

Permalink
[ui] Ask user before closing expression dialog if expression was edited
Browse files Browse the repository at this point in the history
  • Loading branch information
kannes authored Aug 30, 2022
1 parent cdf668a commit e9b90b5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/gui/auto_generated/qgsexpressionbuilderdialog.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ Used to save geometry

virtual void accept();

virtual void reject();


};

Expand Down
38 changes: 38 additions & 0 deletions src/gui/qgsexpressionbuilderdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#include "qgssettings.h"
#include "qgsguiutils.h"
#include "qgsgui.h"
#include <QMessageBox>

QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent, const QString &key, const QgsExpressionContext &context )
: QDialog( parent )
, mInitialText( startText )
, mRecentKey( key )
{
setupUi( this );
Expand All @@ -34,6 +36,7 @@ QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, c
builder->expressionTree()->loadRecent( mRecentKey );
builder->expressionTree()->loadUserExpressions( );


connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
}

Expand Down Expand Up @@ -83,6 +86,41 @@ void QgsExpressionBuilderDialog::accept()
QDialog::accept();
}

void QgsExpressionBuilderDialog::reject()
{

if ( builder->expressionText() != mInitialText )
{

QgsSettings settings;
const bool askToDiscardEditedExpression = settings.value( QStringLiteral( "askToDiscardEditedExpression" ), true, QgsSettings::Gui ).toBool();

if ( askToDiscardEditedExpression )
{
QMessageBox confirmMessage( QMessageBox::Question,
tr( "Expression was Edited" ),
tr( "The changes to the expression will be discarded. Would you like to continue?" ),
QMessageBox::Yes | QMessageBox::No,
this );
confirmMessage.setCheckBox( new QCheckBox( tr( "Don't show this message again" ) ) );
confirmMessage.checkBox()->setChecked( false );
confirmMessage.button( QMessageBox::Yes )->setText( tr( "Discard changes" ) );

int res = confirmMessage.exec();

if ( confirmMessage.checkBox()->isChecked() )
{
settings.setValue( QStringLiteral( "askToDiscardEditedExpression" ), false, QgsSettings::Gui );
}

if ( res != QMessageBox::Yes )
return;
}
}

QDialog::reject();
}

void QgsExpressionBuilderDialog::setGeomCalculator( const QgsDistanceArea &da )
{
// Store in child widget only.
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsexpressionbuilderdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ class GUI_EXPORT QgsExpressionBuilderDialog : public QDialog, private Ui::QgsExp
void done( int r ) override;

void accept() override;
void reject() override;

private:
const QString mInitialText;
QString mRecentKey;
bool mAllowEvalErrors = false;

Expand Down

0 comments on commit e9b90b5

Please sign in to comment.