Skip to content
Permalink
Browse files

User expressions followup

- renamed the group to "User"
- added help text for the group
- added QgsSettings Expressions namespace
- reworded tooltips for store/remove actions
  • Loading branch information
elpaso committed Dec 18, 2019
1 parent e64a6ac commit a8c0e4cbf61e710c73f0bcc80881a6a35967fce3
@@ -57,6 +57,7 @@ QgsSettings provides some shortcuts to get/set namespaced settings from/to a spe
Auth,
App,
Providers,
Expressions,
Misc
};

@@ -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();
@@ -0,0 +1,5 @@
{
"name": "UserGroup",
"type": "group",
"description": "This group contains custom functions and snippets stored in the user profile."
}
@@ -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 ) );

@@ -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" ) );
}

//return the translated name for this group. If group does not
@@ -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 );
@@ -71,6 +71,7 @@ class CORE_EXPORT QgsSettings : public QObject
Auth,
App,
Providers,
Expressions,
Misc
};

@@ -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 );
@@ -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 );
}
@@ -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() );

@@ -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" ) ) );
@@ -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 );
@@ -286,7 +287,7 @@ void QgsExpressionBuilderWidget::runPythonCode( const QString &code )
updateFunctionTree();
loadFieldNames();
loadRecent( mRecentKey );
loadUserExpressions( mRecentKey );
loadUserExpressions( );
}

void QgsExpressionBuilderWidget::saveFunctionFile( QString fileName )
@@ -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;
@@ -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,
@@ -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()
@@ -1233,7 +1229,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() );
}
}

@@ -1247,7 +1243,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;
@@ -1256,7 +1252,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() );
}

}
@@ -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 );

@@ -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;
};
@@ -59,7 +59,7 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *laye
mExpressionBuilder->setExpressionText( startText );
mExpressionBuilder->loadFieldNames();
mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) );
mExpressionBuilder->loadUserExpressions( QStringLiteral( "Selection" ) );
mExpressionBuilder->loadUserExpressions( );

QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
mExpressionBuilder->setExpressionContext( context );
@@ -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>
@@ -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>

0 comments on commit a8c0e4c

Please sign in to comment.
You can’t perform that action at this time.