Skip to content

Commit

Permalink
Simplify whitespaces in saved expression labels
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and nyalldawson committed Aug 12, 2021
1 parent 8eca166 commit 5f7a067
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions python/gui/auto_generated/qgsexpressionstoredialog.sip.in
Expand Up @@ -47,6 +47,11 @@ Returns the label text
QString helpText() const;
%Docstring
Returns the help text
%End

bool isLabelModified();
%Docstring
Returns whether the label text was modified
%End

};
Expand Down
8 changes: 4 additions & 4 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -912,7 +912,7 @@ void QgsExpressionBuilderWidget::storeCurrentUserExpression()
QgsExpressionStoreDialog dlg { expression, expression, QString( ), mExpressionTreeView->userExpressionLabels() };
if ( dlg.exec() == QDialog::DialogCode::Accepted )
{
mExpressionTreeView->saveToUserExpressions( dlg.label(), dlg.expression(), dlg.helpText() );
mExpressionTreeView->saveToUserExpressions( dlg.label().simplified(), dlg.expression(), dlg.helpText() );
}
}

Expand All @@ -931,17 +931,17 @@ void QgsExpressionBuilderWidget::editSelectedUserExpression()

QgsSettings settings;
QString helpText = settings.value( QStringLiteral( "user/%1/helpText" ).arg( item->text() ), "", QgsSettings::Section::Expressions ).toString();
QgsExpressionStoreDialog dlg { item->text(), item->getExpressionText(), helpText };
QgsExpressionStoreDialog dlg { item->text(), item->getExpressionText(), helpText, mExpressionTreeView->userExpressionLabels() };

if ( dlg.exec() == QDialog::DialogCode::Accepted )
{
// label has changed removed the old one before adding the new one
if ( dlg.label() != item->text() )
if ( dlg.isLabelModified() )
{
mExpressionTreeView->removeFromUserExpressions( item->text() );
}

mExpressionTreeView->saveToUserExpressions( dlg.label(), dlg.expression(), dlg.helpText() );
mExpressionTreeView->saveToUserExpressions( dlg.label().simplified(), dlg.expression(), dlg.helpText() );
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/gui/qgsexpressionstoredialog.cpp
Expand Up @@ -20,6 +20,7 @@
QgsExpressionStoreDialog::QgsExpressionStoreDialog( const QString &label, const QString &expression, const QString &helpText, const QStringList &existingLabels, QWidget *parent )
: QDialog( parent )
, mExistingLabels( existingLabels )
, mOriginalLabel( label )
{
setupUi( this );
mExpression->setText( expression );
Expand All @@ -33,7 +34,8 @@ QgsExpressionStoreDialog::QgsExpressionStoreDialog( const QString &label, const
connect( mLabel, &QLineEdit::textChanged, this, [ = ]( const QString & text )
{
QString errorMessage;
if ( mExistingLabels.contains( text ) )
if ( mOriginalLabel.simplified() != text.simplified() &&
mExistingLabels.contains( text.simplified() ) )
{
errorMessage = tr( "A stored expression with this name already exists" );
}
Expand All @@ -56,7 +58,7 @@ QgsExpressionStoreDialog::QgsExpressionStoreDialog( const QString &label, const
// No slashes in labels!
QString labelFixed { label };
labelFixed.remove( '/' ).remove( '\\' );
mLabel->setText( labelFixed );
mLabel->setText( labelFixed.simplified() );
}

QString QgsExpressionStoreDialog::helpText() const
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsexpressionstoredialog.h
Expand Up @@ -57,9 +57,15 @@ class GUI_EXPORT QgsExpressionStoreDialog : public QDialog, private Ui::QgsExpre
*/
QString helpText() const;

/**
* Returns whether the label text was modified
*/
bool isLabelModified() { return mLabel->text() != mOriginalLabel; }

private:

QStringList mExistingLabels;
QString mOriginalLabel;

};

Expand Down

0 comments on commit 5f7a067

Please sign in to comment.