Skip to content

Commit

Permalink
Add the ability to edit user expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed Mar 14, 2020
1 parent 465f8a5 commit e2725a9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgsexpressionbuilderwidget.sip.in
Expand Up @@ -379,6 +379,14 @@ Removes the selected expression from the stored user expressions,
the selected expression must be a user stored expression.

.. versionadded:: 3.12
%End

void editSelectedUserExpression();
%Docstring
Edits the selected expression from the stored user expressions,
the selected expression must be a user stored expression.

.. versionadded:: 3.14
%End

const QList<QgsExpressionItem *> findExpressions( const QString &label );
Expand Down
30 changes: 28 additions & 2 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -61,6 +61,7 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
connect( lblPreview, &QLabel::linkActivated, this, &QgsExpressionBuilderWidget::lblPreview_linkActivated );
connect( mValuesListView, &QListView::doubleClicked, this, &QgsExpressionBuilderWidget::mValuesListView_doubleClicked );
connect( btnSaveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::storeCurrentUserExpression );
connect( btnEditExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::editSelectedUserExpression );
connect( btnRemoveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::removeSelectedUserExpression );
connect( btnClearEditor, &QPushButton::pressed, txtExpressionString, &QgsCodeEditorExpression::clear );

Expand All @@ -83,6 +84,7 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )

// Set icons for tool buttons
btnSaveExpression->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileSave.svg" ) ) );
btnEditExpression->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionToggleEditing.svg" ) ) );
btnRemoveExpression->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionDeleteSelected.svg" ) ) );
btnClearEditor->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileNew.svg" ) ) );

Expand Down Expand Up @@ -267,8 +269,10 @@ void QgsExpressionBuilderWidget::currentChanged( const QModelIndex &index, const
QString help = loadFunctionHelp( item );
txtHelpText->setText( help );

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

btnRemoveExpression->setEnabled( isUserExpression );
btnEditExpression->setEnabled( isUserExpression );

}

Expand Down Expand Up @@ -1349,6 +1353,28 @@ void QgsExpressionBuilderWidget::storeCurrentUserExpression()
}
}

void QgsExpressionBuilderWidget::editSelectedUserExpression()
{
// Get the item
QModelIndex idx = mProxyModel->mapToSource( expressionTree->currentIndex() );
QgsExpressionItem *item = dynamic_cast<QgsExpressionItem *>( mModel->itemFromIndex( idx ) );
if ( !item )
return;

// Don't handle remove if we are on a header node or the parent
// is not the user group
if ( item->getItemType() == QgsExpressionItem::Header ||
( item->parent() && item->parent()->text() != mUserExpressionsGroupName ) )
return;

QgsExpressionStoreDialog dlg { item->text(), item->getExpressionText(), item->getHelpText(), QStringList() };

if ( dlg.exec() == QDialog::DialogCode::Accepted )
{
saveToUserExpressions( dlg.label(), dlg.expression(), dlg.helpText() );
}
}

void QgsExpressionBuilderWidget::removeSelectedUserExpression()
{

Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -380,6 +380,13 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
*/
void removeSelectedUserExpression( );

/**
* Edits the selected expression from the stored user expressions,
* the selected expression must be a user stored expression.
* \since QGIS 3.14
*/
void editSelectedUserExpression();

/**
* Returns the list of expression items matching a \a label.
* \since QGIS 3.12
Expand Down
13 changes: 13 additions & 0 deletions src/ui/qgsexpressionbuilder.ui
Expand Up @@ -356,6 +356,19 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnEditExpression">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>Edit selected expression from user expressions</string>
</property>
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnRemoveExpression">
<property name="enabled">
Expand Down

0 comments on commit e2725a9

Please sign in to comment.