Skip to content

Commit

Permalink
Renamed stored expressions to user expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Dec 17, 2019
1 parent 6b23241 commit fd6075e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 62 deletions.
26 changes: 11 additions & 15 deletions python/gui/auto_generated/qgsexpressionbuilderwidget.sip.in
Expand Up @@ -227,25 +227,25 @@ Loads the recent expressions from the given ``collection``.
By default it is loaded from the collection "generic".
%End

void loadStored( const QString &collection = QStringLiteral( "generic" ) );
void loadUserExpressions( const QString &collection = QStringLiteral( "generic" ) );
%Docstring
Loads the stored expressions from the given ``collection``.
Loads the user expressions from the given ``collection``.
By default it is loaded from the collection "generic".

.. versionadded:: 3.12
%End

void saveToStored( const QString &label, const QString expression, const QString &helpText, const QString &collection = QStringLiteral( "generic" ) );
void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText, const QString &collection = QStringLiteral( "generic" ) );
%Docstring
Stores the ``expression`` in the given ``collection`` with given ``label`` and ``helpText``.
Stores the user ``expression`` in the given ``collection`` with given ``label`` and ``helpText``.
By default it is saved to the collection "generic".

.. versionadded:: 3.12
%End

void removeFromStored( const QString &name, const QString &collection = QStringLiteral( "generic" ) );
void removeFromUserExpressions( const QString &name, const QString &collection = QStringLiteral( "generic" ) );
%Docstring
Removes the expression ``name`` from the stored expressions in the given ``collection``.
Removes the expression ``name`` from the user stored expressions in the given ``collection``.
By default it is removed from the collection "generic".

.. versionadded:: 3.12
Expand Down Expand Up @@ -344,17 +344,17 @@ when text changes.
:param enabled: ``True`` to enable auto saving.
%End

void storeCurrentExpression( );
void storeCurrentUserExpression( );
%Docstring
Adds the current expressions to the stored expressions.
Adds the current expressions to the stored user expressions.

.. versionadded:: 3.12
%End

void removeSelectedExpression( );
void removeSelectedUserExpression( );
%Docstring
Removes the selected expression from the stored expressions,
the selected expression must be a stored expression.
Removes the selected expression from the stored user expressions,
the selected expression must be a user stored expression.

.. versionadded:: 3.12
%End
Expand All @@ -363,10 +363,6 @@ the selected expression must be a stored expression.
%Docstring
Returns the list of expression items matching a ``label``.

.. note::

this function is exposed for testing purposes only

.. versionadded:: 3.12
%End

Expand Down
5 changes: 2 additions & 3 deletions python/gui/auto_generated/qgsexpressionstoredialog.sip.in
Expand Up @@ -30,9 +30,8 @@ A generic dialog for editing expression text, label and help text.
QWidget *parent = 0 );
%Docstring
Creates a QgsExpressionStoreDialog with given ``label``, ``expression`` and ``helpText``.

:param existingLabels: list of existing labels for unique label validation
:param parent: optional parent widget
``existingLabels`` is an optional list of existing labels for unique label validation,
``parent`` is the optional parent widget.
%End

QString expression( );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldcalculator.cpp
Expand Up @@ -153,7 +153,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer *vl, QWidget *parent )
mOnlyUpdateSelectedCheckBox->setText( tr( "Only update %1 selected features" ).arg( vl->selectedFeatureCount() ) );

builder->loadRecent( QStringLiteral( "fieldcalc" ) );
builder->loadStored( QStringLiteral( "fieldcalc" ) );
builder->loadUserExpressions( QStringLiteral( "fieldcalc" ) );

mInfoIcon->setPixmap( style()->standardPixmap( QStyle::SP_MessageBoxInformation ) );

Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -861,7 +861,7 @@ void QgsDualView::modifySort()
expressionBuilder->setLayer( mLayer );
expressionBuilder->loadFieldNames();
expressionBuilder->loadRecent( QStringLiteral( "generic" ) );
expressionBuilder->loadStored( QStringLiteral( "generic" ) );
expressionBuilder->loadUserExpressions( QStringLiteral( "generic" ) );
expressionBuilder->setExpressionText( sortExpression().isEmpty() ? mLayer->displayExpression() : sortExpression() );

sortingGroupBox->layout()->addWidget( expressionBuilder );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsexpressionbuilderdialog.cpp
Expand Up @@ -33,7 +33,7 @@ QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, c
builder->setExpressionText( startText );
builder->loadFieldNames();
builder->loadRecent( mRecentKey );
builder->loadStored( mRecentKey );
builder->loadUserExpressions( mRecentKey );

connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
}
Expand Down
42 changes: 21 additions & 21 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -60,8 +60,8 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
connect( txtSearchEdit, &QgsFilterLineEdit::textChanged, this, &QgsExpressionBuilderWidget::txtSearchEdit_textChanged );
connect( lblPreview, &QLabel::linkActivated, this, &QgsExpressionBuilderWidget::lblPreview_linkActivated );
connect( mValuesListView, &QListView::doubleClicked, this, &QgsExpressionBuilderWidget::mValuesListView_doubleClicked );
connect( btnSaveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::storeCurrentExpression );
connect( btnRemoveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::removeSelectedExpression );
connect( btnSaveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::storeCurrentUserExpression );
connect( btnRemoveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::removeSelectedUserExpression );
connect( btnClearEditor, &QPushButton::pressed, txtExpressionString, &QgsCodeEditorExpression::clear );

txtHelpText->setOpenExternalLinks( true );
Expand Down Expand Up @@ -262,7 +262,7 @@ void QgsExpressionBuilderWidget::currentChanged( const QModelIndex &index, const
txtHelpText->setText( help );

btnRemoveExpression->setEnabled( item->parent() &&
item->parent()->text() == mStoredGroupName );
item->parent()->text() == mUserExpressionsGroupName );

}

Expand All @@ -286,7 +286,7 @@ void QgsExpressionBuilderWidget::runPythonCode( const QString &code )
updateFunctionTree();
loadFieldNames();
loadRecent( mRecentKey );
loadStored( mRecentKey );
loadUserExpressions( mRecentKey );
}

void QgsExpressionBuilderWidget::saveFunctionFile( QString fileName )
Expand Down Expand Up @@ -606,15 +606,15 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
}
}

void QgsExpressionBuilderWidget::loadStored( const QString &collection )
void QgsExpressionBuilderWidget::loadUserExpressions( const QString &collection )
{
mRecentKey = collection;
mStoredGroupName = tr( "Stored (%1)" ).arg( collection );
mUserExpressionsGroupName = tr( "User expressions (%1)" ).arg( collection );

// Cleanup
if ( mExpressionGroups.contains( mStoredGroupName ) )
if ( mExpressionGroups.contains( mUserExpressionsGroupName ) )
{
QgsExpressionItem *node = mExpressionGroups.value( mStoredGroupName );
QgsExpressionItem *node = mExpressionGroups.value( mUserExpressionsGroupName );
node->removeRows( 0, node->rowCount() );
}

Expand All @@ -625,26 +625,26 @@ void QgsExpressionBuilderWidget::loadStored( const QString &collection )
QString helpText;
QString expression;
int i = 0;
mStoredLabels = settings.childGroups();
for ( const auto &label : qgis::as_const( mStoredLabels ) )
mUserExpressionLabels = settings.childGroups();
for ( const auto &label : qgis::as_const( mUserExpressionLabels ) )
{
settings.beginGroup( label );
expression = settings.value( QStringLiteral( "expression" ) ).toString();
helpText = settings.value( QStringLiteral( "helpText" ) ).toString();
this->registerItem( mStoredGroupName, label, expression, helpText, QgsExpressionItem::ExpressionNode, false, i++ );
this->registerItem( mUserExpressionsGroupName, label, expression, helpText, QgsExpressionItem::ExpressionNode, false, i++ );
settings.endGroup();
}
}

void QgsExpressionBuilderWidget::saveToStored( const QString &label, const QString expression, const QString &helpText, const QString &collection )
void QgsExpressionBuilderWidget::saveToUserExpressions( const QString &label, const QString expression, const QString &helpText, const QString &collection )
{
QgsSettings settings;
const QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
settings.beginGroup( label );
settings.setValue( QStringLiteral( "expression" ), expression );
settings.setValue( QStringLiteral( "helpText" ), helpText );
loadStored( collection );
loadUserExpressions( collection );
// Scroll
const QModelIndexList idxs { expressionTree->model()->match( expressionTree->model()->index( 0, 0 ),
Qt::DisplayRole, label, 1,
Expand All @@ -655,13 +655,13 @@ void QgsExpressionBuilderWidget::saveToStored( const QString &label, const QStri
}
}

void QgsExpressionBuilderWidget::removeFromStored( const QString &name, const QString &collection )
void QgsExpressionBuilderWidget::removeFromUserExpressions( const QString &name, const QString &collection )
{
QgsSettings settings;
QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
settings.remove( name );
this->loadStored( collection );
this->loadUserExpressions( collection );
}

void QgsExpressionBuilderWidget::loadLayers()
Expand Down Expand Up @@ -1226,17 +1226,17 @@ void QgsExpressionBuilderWidget::autosave()
anim->start( QAbstractAnimation::DeleteWhenStopped );
}

void QgsExpressionBuilderWidget::storeCurrentExpression()
void QgsExpressionBuilderWidget::storeCurrentUserExpression()
{
const QString expression { this->expressionText() };
QgsExpressionStoreDialog dlg { expression, expression, QString( ), mStoredLabels };
QgsExpressionStoreDialog dlg { expression, expression, QString( ), mUserExpressionLabels };
if ( dlg.exec() == QDialog::DialogCode::Accepted )
{
saveToStored( dlg.label(), dlg.expression(), dlg.helpText(), mRecentKey );
saveToUserExpressions( dlg.label(), dlg.expression(), dlg.helpText(), mRecentKey );
}
}

void QgsExpressionBuilderWidget::removeSelectedExpression()
void QgsExpressionBuilderWidget::removeSelectedUserExpression()
{

// Get the item
Expand All @@ -1248,14 +1248,14 @@ void QgsExpressionBuilderWidget::removeSelectedExpression()
// Don't handle remove if we are on a header node or the parent
// is not the stored group
if ( item->getItemType() == QgsExpressionItem::Header ||
( item->parent() && item->parent()->text() != mStoredGroupName ) )
( item->parent() && item->parent()->text() != mUserExpressionsGroupName ) )
return;

if ( QMessageBox::Yes == QMessageBox::question( this, tr( "Remove Stored Expression" ),
tr( "Do you really want to remove stored expressions '%1'?" ).arg( item->text() ),
QMessageBox::Yes | QMessageBox::No ) )
{
removeFromStored( item->text(), mRecentKey );
removeFromUserExpressions( item->text(), mRecentKey );
}

}
Expand Down
27 changes: 13 additions & 14 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -237,25 +237,25 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
void loadRecent( const QString &collection = QStringLiteral( "generic" ) );

/**
* Loads the stored expressions from the given \a collection.
* Loads the user expressions from the given \a collection.
* By default it is loaded from the collection "generic".
* \since QGIS 3.12
*/
void loadStored( const QString &collection = QStringLiteral( "generic" ) );
void loadUserExpressions( const QString &collection = QStringLiteral( "generic" ) );

/**
* Stores the \a expression in the given \a collection with given \a label and \a helpText.
* Stores the user \a expression in the given \a collection with given \a label and \a helpText.
* By default it is saved to the collection "generic".
* \since QGIS 3.12
*/
void saveToStored( const QString &label, const QString expression, const QString &helpText, const QString &collection = QStringLiteral( "generic" ) );
void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText, const QString &collection = QStringLiteral( "generic" ) );

/**
* Removes the expression \a name from the stored expressions in the given \a collection.
* Removes the expression \a name from the user stored expressions in the given \a collection.
* By default it is removed from the collection "generic".
* \since QGIS 3.12
*/
void removeFromStored( const QString &name, const QString &collection = QStringLiteral( "generic" ) );
void removeFromUserExpressions( const QString &name, const QString &collection = QStringLiteral( "generic" ) );

/**
* Create a new file in the function editor
Expand Down Expand Up @@ -345,21 +345,20 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
void setAutoSave( bool enabled ) { mAutoSave = enabled; }

/**
* Adds the current expressions to the stored expressions.
* Adds the current expressions to the stored user expressions.
* \since QGIS 3.12
*/
void storeCurrentExpression( );
void storeCurrentUserExpression( );

/**
* Removes the selected expression from the stored expressions,
* the selected expression must be a stored expression.
* Removes the selected expression from the stored user expressions,
* the selected expression must be a user stored expression.
* \since QGIS 3.12
*/
void removeSelectedExpression( );
void removeSelectedUserExpression( );

/**
* Returns the list of expression items matching a \a label.
* \note this function is exposed for testing purposes only
* \since QGIS 3.12
*/
const QList<QgsExpressionItem *> findExpressions( const QString &label );
Expand Down Expand Up @@ -497,8 +496,8 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QPointer< QgsProject > mProject;
bool mEvalError = true;
bool mParserError = true;
QString mStoredGroupName;
QStringList mStoredLabels;
QString mUserExpressionsGroupName;
QStringList mUserExpressionLabels;
};

// clazy:excludeall=qstring-allocations
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsexpressionselectiondialog.cpp
Expand Up @@ -59,7 +59,7 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *laye
mExpressionBuilder->setExpressionText( startText );
mExpressionBuilder->loadFieldNames();
mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) );
mExpressionBuilder->loadStored( QStringLiteral( "Selection" ) );
mExpressionBuilder->loadUserExpressions( QStringLiteral( "Selection" ) );

QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
mExpressionBuilder->setExpressionContext( context );
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsexpressionstoredialog.h
Expand Up @@ -33,8 +33,8 @@ class GUI_EXPORT QgsExpressionStoreDialog : public QDialog, private Ui::QgsExpre

/**
* Creates a QgsExpressionStoreDialog with given \a label, \a expression and \a helpText.
* \param existingLabels list of existing labels for unique label validation
* \param parent optional parent widget
* \a existingLabels is an optional list of existing labels for unique label validation,
* \a parent is the optional parent widget.
*/
QgsExpressionStoreDialog( const QString &label,
const QString &expression,
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_qgsexpressionbuilderwidget.py
Expand Up @@ -169,14 +169,14 @@ def testStoredExpressions(self):

w = QgsExpressionBuilderWidget()

w.saveToStored('Stored Expression Number One', '"field_one" = 123', "An humble expression", "my_custom_collection")
w.saveToUserExpressions('Stored Expression Number One', '"field_one" = 123', "An humble expression", "my_custom_collection")
items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 1)
exp = items[0]
self.assertEqual(exp.getExpressionText(), '"field_one" = 123')

# Add another one with the same name (overwrite)
w.saveToStored('Stored Expression Number One', '"field_two" = 456', "An even more humble expression", "my_custom_collection")
w.saveToUserExpressions('Stored Expression Number One', '"field_two" = 456', "An even more humble expression", "my_custom_collection")
items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 1)
exp = items[0]
Expand All @@ -187,7 +187,7 @@ def testStoredExpressions(self):
self.assertEqual(exp.getExpressionText(), '"field_two" = 456')

# Test removal
w.removeFromStored('Stored Expression Number One', "my_custom_collection")
w.removeFromUserExpressions('Stored Expression Number One', "my_custom_collection")
items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 0)

Expand Down

0 comments on commit fd6075e

Please sign in to comment.