Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #33462 from elpaso/stored-expressions-no-groups
Browse files Browse the repository at this point in the history
[feature] User expressions followup
  • Loading branch information
elpaso committed Dec 18, 2019
2 parents 971552a + b894cb7 commit ecff2e6
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 65 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgssettings.sip.in
Expand Up @@ -57,6 +57,7 @@ QgsSettings provides some shortcuts to get/set namespaced settings from/to a spe
Auth,
App,
Providers,
Expressions,
Misc
};

Expand Down
25 changes: 11 additions & 14 deletions python/gui/auto_generated/qgsexpressionbuilderwidget.sip.in
Expand Up @@ -230,53 +230,50 @@ Loads the recent expressions from the given ``collection``.
By default it is loaded from the collection "generic".
%End

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

.. versionadded:: 3.12
%End

void saveToUserExpressions( 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 );
%Docstring
Stores the user ``expression`` in the given ``collection`` with given ``label`` and ``helpText``.
By default it is saved to the collection "generic".
Stores the user ``expression`` with given ``label`` and ``helpText``.

.. versionadded:: 3.12
%End

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

.. versionadded:: 3.12
%End

void newFunctionFile( const QString &fileName = "scratch" );
%Docstring
Create a new file in the function editor
Creates a new file in the function editor
%End

void saveFunctionFile( QString fileName );
%Docstring
Save the current function editor text to the given file.
Saves the current function editor text to the given file.
%End

void loadCodeFromFile( QString path );
%Docstring
Load code from the given file into the function editor
Loads code from the given file into the function editor
%End

void loadFunctionCode( const QString &code );
%Docstring
Load code into the function editor
Loads code into the function editor
%End

void updateFunctionFileList( const QString &path );
%Docstring
Update the list of function files found at the given path
Updates the list of function files found at the given path
%End

QStandardItemModel *model();
Expand Down
5 changes: 5 additions & 0 deletions resources/function_help/json/UserGroup
@@ -0,0 +1,5 @@
{
"name": "UserGroup",
"type": "group",
"description": "This group contains expressions stored in the user profile."
}
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->loadUserExpressions( QStringLiteral( "fieldcalc" ) );
builder->loadUserExpressions( );

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

Expand Down
1 change: 1 addition & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -875,6 +875,7 @@ QString QgsExpression::group( const QString &name )
sGroups()->insert( QStringLiteral( "String" ), tr( "String" ) );
sGroups()->insert( QStringLiteral( "Variables" ), tr( "Variables" ) );
sGroups()->insert( QStringLiteral( "Recent (%1)" ), tr( "Recent (%1)" ) );
sGroups()->insert( QStringLiteral( "UserGroup" ), tr( "User expressions" ) );
}

//return the translated name for this group. If group does not
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgssettings.cpp
Expand Up @@ -237,8 +237,10 @@ QString QgsSettings::prefixedKey( const QString &key, const Section section ) co
case Section::Providers :
prefix = QStringLiteral( "providers" );
break;
case Section::Expressions :
prefix = QStringLiteral( "expressions" );
break;
case Section::NoSection:
default:
return sanitizeKey( key );
}
return prefix + "/" + sanitizeKey( key );
Expand Down
1 change: 1 addition & 0 deletions src/core/qgssettings.h
Expand Up @@ -71,6 +71,7 @@ class CORE_EXPORT QgsSettings : public QObject
Auth,
App,
Providers,
Expressions,
Misc
};

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->loadUserExpressions( QStringLiteral( "generic" ) );
expressionBuilder->loadUserExpressions( );
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->loadUserExpressions( mRecentKey );
builder->loadUserExpressions( );

connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
}
Expand Down
47 changes: 22 additions & 25 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -65,7 +65,6 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
connect( btnClearEditor, &QPushButton::pressed, txtExpressionString, &QgsCodeEditorExpression::clear );

txtHelpText->setOpenExternalLinks( true );

mValueGroupBox->hide();
// highlighter = new QgsExpressionHighlighter( txtExpressionString->document() );

Expand All @@ -79,6 +78,9 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )

expressionTree->setSelectionMode( QAbstractItemView::SelectionMode::SingleSelection );

// Note: must be in sync with the json help file for UserGroup
mUserExpressionsGroupName = tr( "User" );

// Set icons for tool buttons
btnSaveExpression->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileSave.svg" ) ) );
btnRemoveExpression->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionDeleteSelected.svg" ) ) );
Expand Down Expand Up @@ -153,7 +155,6 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
txtExpressionString->setWrapMode( QsciScintilla::WrapWord );
lblAutoSave->clear();


// Note: If you add a indicator here you should add it to clearErrors method if you need to clear it on text parse.
txtExpressionString->indicatorDefine( QgsCodeEditor::SquiggleIndicator, QgsExpression::ParserError::FunctionUnknown );
txtExpressionString->indicatorDefine( QgsCodeEditor::SquiggleIndicator, QgsExpression::ParserError::FunctionWrongArgs );
Expand Down Expand Up @@ -286,7 +287,7 @@ void QgsExpressionBuilderWidget::runPythonCode( const QString &code )
updateFunctionTree();
loadFieldNames();
loadRecent( mRecentKey );
loadUserExpressions( mRecentKey );
loadUserExpressions( );
}

void QgsExpressionBuilderWidget::saveFunctionFile( QString fileName )
Expand Down Expand Up @@ -602,26 +603,23 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
int i = 0;
for ( const QString &expression : expressions )
{
this->registerItem( name, expression, expression, expression, QgsExpressionItem::ExpressionNode, false, i );
registerItem( name, expression, expression, expression, QgsExpressionItem::ExpressionNode, false, i );
i++;
}
}

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

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

QgsSettings settings;
const QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
const QString location = QStringLiteral( "user" );
settings.beginGroup( location, QgsSettings::Section::Expressions );
QString label;
QString helpText;
QString expression;
Expand All @@ -632,20 +630,20 @@ void QgsExpressionBuilderWidget::loadUserExpressions( const QString &collection
settings.beginGroup( label );
expression = settings.value( QStringLiteral( "expression" ) ).toString();
helpText = settings.value( QStringLiteral( "helpText" ) ).toString();
this->registerItem( mUserExpressionsGroupName, label, expression, helpText, QgsExpressionItem::ExpressionNode, false, i++ );
registerItem( QStringLiteral( "UserGroup" ), label, expression, helpText, QgsExpressionItem::ExpressionNode, false, i++ );
settings.endGroup();
}
}

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

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

void QgsExpressionBuilderWidget::loadLayers()
Expand Down Expand Up @@ -789,6 +785,7 @@ void QgsExpressionBuilderWidget::setExpressionContext( const QgsExpressionContex
updateFunctionTree();
loadFieldNames();
loadRecent( mRecentKey );
loadUserExpressions( );
}

void QgsExpressionBuilderWidget::txtExpressionString_textChanged()
Expand Down Expand Up @@ -1233,7 +1230,7 @@ void QgsExpressionBuilderWidget::storeCurrentUserExpression()
QgsExpressionStoreDialog dlg { expression, expression, QString( ), mUserExpressionLabels };
if ( dlg.exec() == QDialog::DialogCode::Accepted )
{
saveToUserExpressions( dlg.label(), dlg.expression(), dlg.helpText(), mRecentKey );
saveToUserExpressions( dlg.label(), dlg.expression(), dlg.helpText() );
}
}

Expand All @@ -1247,7 +1244,7 @@ void QgsExpressionBuilderWidget::removeSelectedUserExpression()
return;

// Don't handle remove if we are on a header node or the parent
// is not the stored group
// is not the user group
if ( item->getItemType() == QgsExpressionItem::Header ||
( item->parent() && item->parent()->text() != mUserExpressionsGroupName ) )
return;
Expand All @@ -1256,7 +1253,7 @@ void QgsExpressionBuilderWidget::removeSelectedUserExpression()
tr( "Do you really want to remove stored expressions '%1'?" ).arg( item->text() ),
QMessageBox::Yes | QMessageBox::No ) )
{
removeFromUserExpressions( item->text(), mRecentKey );
removeFromUserExpressions( item->text() );
}

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

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

/**
* 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".
* Stores the user \a expression with given \a label and \a helpText.
* \since QGIS 3.12
*/
void saveToUserExpressions( 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 );

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

/**
* Create a new file in the function editor
* Creates a new file in the function editor
*/
void newFunctionFile( const QString &fileName = "scratch" );

/**
* Save the current function editor text to the given file.
* Saves the current function editor text to the given file.
*/
void saveFunctionFile( QString fileName );

/**
* Load code from the given file into the function editor
* Loads code from the given file into the function editor
*/
void loadCodeFromFile( QString path );

/**
* Load code into the function editor
* Loads code into the function editor
*/
void loadFunctionCode( const QString &code );

/**
* Update the list of function files found at the given path
* Updates the list of function files found at the given path
*/
void updateFunctionFileList( const QString &path );

Expand Down Expand Up @@ -501,6 +498,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QPointer< QgsProject > mProject;
bool mEvalError = true;
bool mParserError = true;
// Translated name of the user expressions group
QString mUserExpressionsGroupName;
QStringList mUserExpressionLabels;
};
Expand Down
3 changes: 0 additions & 3 deletions src/gui/qgsexpressionselectiondialog.cpp
Expand Up @@ -57,9 +57,6 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *laye

mExpressionBuilder->setLayer( layer );
mExpressionBuilder->setExpressionText( startText );
mExpressionBuilder->loadFieldNames();
mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) );
mExpressionBuilder->loadUserExpressions( QStringLiteral( "Selection" ) );

QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
mExpressionBuilder->setExpressionContext( context );
Expand Down
4 changes: 2 additions & 2 deletions src/ui/qgsexpressionbuilder.ui
Expand Up @@ -343,7 +343,7 @@
<bool>false</bool>
</property>
<property name="toolTip">
<string>Add current expression to stored expressions</string>
<string>Add current expression to user expressions</string>
</property>
<property name="text">
<string>Save</string>
Expand All @@ -356,7 +356,7 @@
<bool>false</bool>
</property>
<property name="toolTip">
<string>Removes selected expression from the stored expressions</string>
<string>Remove selected expression from user expressions</string>
</property>
<property name="text">
<string>Remove</string>
Expand Down

0 comments on commit ecff2e6

Please sign in to comment.