Renamed stored expressions to user expressions

This commit is contained in:
Alessandro Pasotti 2019-12-17 17:10:43 +01:00
parent 6b232414f2
commit fd6075e80b
10 changed files with 56 additions and 62 deletions

View File

@ -227,25 +227,25 @@ Loads the recent expressions from the given ``collection``.
By default it is loaded from the collection "generic". By default it is loaded from the collection "generic".
%End %End
void loadStored( const QString &collection = QStringLiteral( "generic" ) ); void loadUserExpressions( const QString &collection = QStringLiteral( "generic" ) );
%Docstring %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". By default it is loaded from the collection "generic".
.. versionadded:: 3.12 .. versionadded:: 3.12
%End %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 %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". By default it is saved to the collection "generic".
.. versionadded:: 3.12 .. versionadded:: 3.12
%End %End
void removeFromStored( const QString &name, const QString &collection = QStringLiteral( "generic" ) ); void removeFromUserExpressions( const QString &name, const QString &collection = QStringLiteral( "generic" ) );
%Docstring %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". By default it is removed from the collection "generic".
.. versionadded:: 3.12 .. versionadded:: 3.12
@ -344,17 +344,17 @@ when text changes.
:param enabled: ``True`` to enable auto saving. :param enabled: ``True`` to enable auto saving.
%End %End
void storeCurrentExpression( ); void storeCurrentUserExpression( );
%Docstring %Docstring
Adds the current expressions to the stored expressions. Adds the current expressions to the stored user expressions.
.. versionadded:: 3.12 .. versionadded:: 3.12
%End %End
void removeSelectedExpression( ); void removeSelectedUserExpression( );
%Docstring %Docstring
Removes the selected expression from the stored expressions, Removes the selected expression from the stored user expressions,
the selected expression must be a stored expression. the selected expression must be a user stored expression.
.. versionadded:: 3.12 .. versionadded:: 3.12
%End %End
@ -363,10 +363,6 @@ the selected expression must be a stored expression.
%Docstring %Docstring
Returns the list of expression items matching a ``label``. Returns the list of expression items matching a ``label``.
.. note::
this function is exposed for testing purposes only
.. versionadded:: 3.12 .. versionadded:: 3.12
%End %End

View File

@ -30,9 +30,8 @@ A generic dialog for editing expression text, label and help text.
QWidget *parent = 0 ); QWidget *parent = 0 );
%Docstring %Docstring
Creates a QgsExpressionStoreDialog with given ``label``, ``expression`` and ``helpText``. Creates a QgsExpressionStoreDialog with given ``label``, ``expression`` and ``helpText``.
``existingLabels`` is an optional list of existing labels for unique label validation,
:param existingLabels: list of existing labels for unique label validation ``parent`` is the optional parent widget.
:param parent: optional parent widget
%End %End
QString expression( ); QString expression( );

View File

@ -153,7 +153,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer *vl, QWidget *parent )
mOnlyUpdateSelectedCheckBox->setText( tr( "Only update %1 selected features" ).arg( vl->selectedFeatureCount() ) ); mOnlyUpdateSelectedCheckBox->setText( tr( "Only update %1 selected features" ).arg( vl->selectedFeatureCount() ) );
builder->loadRecent( QStringLiteral( "fieldcalc" ) ); builder->loadRecent( QStringLiteral( "fieldcalc" ) );
builder->loadStored( QStringLiteral( "fieldcalc" ) ); builder->loadUserExpressions( QStringLiteral( "fieldcalc" ) );
mInfoIcon->setPixmap( style()->standardPixmap( QStyle::SP_MessageBoxInformation ) ); mInfoIcon->setPixmap( style()->standardPixmap( QStyle::SP_MessageBoxInformation ) );

View File

@ -861,7 +861,7 @@ void QgsDualView::modifySort()
expressionBuilder->setLayer( mLayer ); expressionBuilder->setLayer( mLayer );
expressionBuilder->loadFieldNames(); expressionBuilder->loadFieldNames();
expressionBuilder->loadRecent( QStringLiteral( "generic" ) ); expressionBuilder->loadRecent( QStringLiteral( "generic" ) );
expressionBuilder->loadStored( QStringLiteral( "generic" ) ); expressionBuilder->loadUserExpressions( QStringLiteral( "generic" ) );
expressionBuilder->setExpressionText( sortExpression().isEmpty() ? mLayer->displayExpression() : sortExpression() ); expressionBuilder->setExpressionText( sortExpression().isEmpty() ? mLayer->displayExpression() : sortExpression() );
sortingGroupBox->layout()->addWidget( expressionBuilder ); sortingGroupBox->layout()->addWidget( expressionBuilder );

View File

@ -33,7 +33,7 @@ QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, c
builder->setExpressionText( startText ); builder->setExpressionText( startText );
builder->loadFieldNames(); builder->loadFieldNames();
builder->loadRecent( mRecentKey ); builder->loadRecent( mRecentKey );
builder->loadStored( mRecentKey ); builder->loadUserExpressions( mRecentKey );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp ); connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
} }

View File

@ -60,8 +60,8 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
connect( txtSearchEdit, &QgsFilterLineEdit::textChanged, this, &QgsExpressionBuilderWidget::txtSearchEdit_textChanged ); connect( txtSearchEdit, &QgsFilterLineEdit::textChanged, this, &QgsExpressionBuilderWidget::txtSearchEdit_textChanged );
connect( lblPreview, &QLabel::linkActivated, this, &QgsExpressionBuilderWidget::lblPreview_linkActivated ); connect( lblPreview, &QLabel::linkActivated, this, &QgsExpressionBuilderWidget::lblPreview_linkActivated );
connect( mValuesListView, &QListView::doubleClicked, this, &QgsExpressionBuilderWidget::mValuesListView_doubleClicked ); connect( mValuesListView, &QListView::doubleClicked, this, &QgsExpressionBuilderWidget::mValuesListView_doubleClicked );
connect( btnSaveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::storeCurrentExpression ); connect( btnSaveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::storeCurrentUserExpression );
connect( btnRemoveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::removeSelectedExpression ); connect( btnRemoveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::removeSelectedUserExpression );
connect( btnClearEditor, &QPushButton::pressed, txtExpressionString, &QgsCodeEditorExpression::clear ); connect( btnClearEditor, &QPushButton::pressed, txtExpressionString, &QgsCodeEditorExpression::clear );
txtHelpText->setOpenExternalLinks( true ); txtHelpText->setOpenExternalLinks( true );
@ -262,7 +262,7 @@ void QgsExpressionBuilderWidget::currentChanged( const QModelIndex &index, const
txtHelpText->setText( help ); txtHelpText->setText( help );
btnRemoveExpression->setEnabled( item->parent() && btnRemoveExpression->setEnabled( item->parent() &&
item->parent()->text() == mStoredGroupName ); item->parent()->text() == mUserExpressionsGroupName );
} }
@ -286,7 +286,7 @@ void QgsExpressionBuilderWidget::runPythonCode( const QString &code )
updateFunctionTree(); updateFunctionTree();
loadFieldNames(); loadFieldNames();
loadRecent( mRecentKey ); loadRecent( mRecentKey );
loadStored( mRecentKey ); loadUserExpressions( mRecentKey );
} }
void QgsExpressionBuilderWidget::saveFunctionFile( QString fileName ) void QgsExpressionBuilderWidget::saveFunctionFile( QString fileName )
@ -606,15 +606,15 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
} }
} }
void QgsExpressionBuilderWidget::loadStored( const QString &collection ) void QgsExpressionBuilderWidget::loadUserExpressions( const QString &collection )
{ {
mRecentKey = collection; mRecentKey = collection;
mStoredGroupName = tr( "Stored (%1)" ).arg( collection ); mUserExpressionsGroupName = tr( "User expressions (%1)" ).arg( collection );
// Cleanup // Cleanup
if ( mExpressionGroups.contains( mStoredGroupName ) ) if ( mExpressionGroups.contains( mUserExpressionsGroupName ) )
{ {
QgsExpressionItem *node = mExpressionGroups.value( mStoredGroupName ); QgsExpressionItem *node = mExpressionGroups.value( mUserExpressionsGroupName );
node->removeRows( 0, node->rowCount() ); node->removeRows( 0, node->rowCount() );
} }
@ -625,18 +625,18 @@ void QgsExpressionBuilderWidget::loadStored( const QString &collection )
QString helpText; QString helpText;
QString expression; QString expression;
int i = 0; int i = 0;
mStoredLabels = settings.childGroups(); mUserExpressionLabels = settings.childGroups();
for ( const auto &label : qgis::as_const( mStoredLabels ) ) for ( const auto &label : qgis::as_const( mUserExpressionLabels ) )
{ {
settings.beginGroup( label ); settings.beginGroup( label );
expression = settings.value( QStringLiteral( "expression" ) ).toString(); expression = settings.value( QStringLiteral( "expression" ) ).toString();
helpText = settings.value( QStringLiteral( "helpText" ) ).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(); 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; QgsSettings settings;
const QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection ); const QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
@ -644,7 +644,7 @@ void QgsExpressionBuilderWidget::saveToStored( const QString &label, const QStri
settings.beginGroup( label ); settings.beginGroup( label );
settings.setValue( QStringLiteral( "expression" ), expression ); settings.setValue( QStringLiteral( "expression" ), expression );
settings.setValue( QStringLiteral( "helpText" ), helpText ); settings.setValue( QStringLiteral( "helpText" ), helpText );
loadStored( collection ); loadUserExpressions( collection );
// Scroll // Scroll
const QModelIndexList idxs { expressionTree->model()->match( expressionTree->model()->index( 0, 0 ), const QModelIndexList idxs { expressionTree->model()->match( expressionTree->model()->index( 0, 0 ),
Qt::DisplayRole, label, 1, Qt::DisplayRole, label, 1,
@ -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; QgsSettings settings;
QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection ); QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui ); settings.beginGroup( location, QgsSettings::Section::Gui );
settings.remove( name ); settings.remove( name );
this->loadStored( collection ); this->loadUserExpressions( collection );
} }
void QgsExpressionBuilderWidget::loadLayers() void QgsExpressionBuilderWidget::loadLayers()
@ -1226,17 +1226,17 @@ void QgsExpressionBuilderWidget::autosave()
anim->start( QAbstractAnimation::DeleteWhenStopped ); anim->start( QAbstractAnimation::DeleteWhenStopped );
} }
void QgsExpressionBuilderWidget::storeCurrentExpression() void QgsExpressionBuilderWidget::storeCurrentUserExpression()
{ {
const QString expression { this->expressionText() }; const QString expression { this->expressionText() };
QgsExpressionStoreDialog dlg { expression, expression, QString( ), mStoredLabels }; QgsExpressionStoreDialog dlg { expression, expression, QString( ), mUserExpressionLabels };
if ( dlg.exec() == QDialog::DialogCode::Accepted ) 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 // Get the item
@ -1248,14 +1248,14 @@ void QgsExpressionBuilderWidget::removeSelectedExpression()
// Don't handle remove if we are on a header node or the parent // Don't handle remove if we are on a header node or the parent
// is not the stored group // is not the stored group
if ( item->getItemType() == QgsExpressionItem::Header || if ( item->getItemType() == QgsExpressionItem::Header ||
( item->parent() && item->parent()->text() != mStoredGroupName ) ) ( item->parent() && item->parent()->text() != mUserExpressionsGroupName ) )
return; return;
if ( QMessageBox::Yes == QMessageBox::question( this, tr( "Remove Stored Expression" ), if ( QMessageBox::Yes == QMessageBox::question( this, tr( "Remove Stored Expression" ),
tr( "Do you really want to remove stored expressions '%1'?" ).arg( item->text() ), tr( "Do you really want to remove stored expressions '%1'?" ).arg( item->text() ),
QMessageBox::Yes | QMessageBox::No ) ) QMessageBox::Yes | QMessageBox::No ) )
{ {
removeFromStored( item->text(), mRecentKey ); removeFromUserExpressions( item->text(), mRecentKey );
} }
} }

View File

@ -237,25 +237,25 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
void loadRecent( const QString &collection = QStringLiteral( "generic" ) ); 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". * By default it is loaded from the collection "generic".
* \since QGIS 3.12 * \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". * By default it is saved to the collection "generic".
* \since QGIS 3.12 * \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". * By default it is removed from the collection "generic".
* \since QGIS 3.12 * \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 * Create a new file in the function editor
@ -345,21 +345,20 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
void setAutoSave( bool enabled ) { mAutoSave = enabled; } 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 * \since QGIS 3.12
*/ */
void storeCurrentExpression( ); void storeCurrentUserExpression( );
/** /**
* Removes the selected expression from the stored expressions, * Removes the selected expression from the stored user expressions,
* the selected expression must be a stored expression. * the selected expression must be a user stored expression.
* \since QGIS 3.12 * \since QGIS 3.12
*/ */
void removeSelectedExpression( ); void removeSelectedUserExpression( );
/** /**
* Returns the list of expression items matching a \a label. * Returns the list of expression items matching a \a label.
* \note this function is exposed for testing purposes only
* \since QGIS 3.12 * \since QGIS 3.12
*/ */
const QList<QgsExpressionItem *> findExpressions( const QString &label ); const QList<QgsExpressionItem *> findExpressions( const QString &label );
@ -497,8 +496,8 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QPointer< QgsProject > mProject; QPointer< QgsProject > mProject;
bool mEvalError = true; bool mEvalError = true;
bool mParserError = true; bool mParserError = true;
QString mStoredGroupName; QString mUserExpressionsGroupName;
QStringList mStoredLabels; QStringList mUserExpressionLabels;
}; };
// clazy:excludeall=qstring-allocations // clazy:excludeall=qstring-allocations

View File

@ -59,7 +59,7 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *laye
mExpressionBuilder->setExpressionText( startText ); mExpressionBuilder->setExpressionText( startText );
mExpressionBuilder->loadFieldNames(); mExpressionBuilder->loadFieldNames();
mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) ); mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) );
mExpressionBuilder->loadStored( QStringLiteral( "Selection" ) ); mExpressionBuilder->loadUserExpressions( QStringLiteral( "Selection" ) );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) ); QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
mExpressionBuilder->setExpressionContext( context ); mExpressionBuilder->setExpressionContext( context );

View File

@ -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. * Creates a QgsExpressionStoreDialog with given \a label, \a expression and \a helpText.
* \param existingLabels list of existing labels for unique label validation * \a existingLabels is an optional list of existing labels for unique label validation,
* \param parent optional parent widget * \a parent is the optional parent widget.
*/ */
QgsExpressionStoreDialog( const QString &label, QgsExpressionStoreDialog( const QString &label,
const QString &expression, const QString &expression,

View File

@ -169,14 +169,14 @@ class TestQgsExpressionBuilderWidget(unittest.TestCase):
w = QgsExpressionBuilderWidget() 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') items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 1) self.assertEqual(len(items), 1)
exp = items[0] exp = items[0]
self.assertEqual(exp.getExpressionText(), '"field_one" = 123') self.assertEqual(exp.getExpressionText(), '"field_one" = 123')
# Add another one with the same name (overwrite) # 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') items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 1) self.assertEqual(len(items), 1)
exp = items[0] exp = items[0]
@ -187,7 +187,7 @@ class TestQgsExpressionBuilderWidget(unittest.TestCase):
self.assertEqual(exp.getExpressionText(), '"field_two" = 456') self.assertEqual(exp.getExpressionText(), '"field_two" = 456')
# Test removal # 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') items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 0) self.assertEqual(len(items), 0)