Skip to content

Commit

Permalink
Add items for project map layers and relations to expression builder
Browse files Browse the repository at this point in the history
Allows easy insertion of map layer IDs and relation IDs into
expressions. Numerous expression functions now utilise these,
so it makes sense to allow them to be easily inserted.

Fix #11680, #16879

Sponsored by Andreas Neumann
  • Loading branch information
nyalldawson committed Jul 30, 2017
1 parent cf753e9 commit 878ee5c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions resources/function_help/json/Map Layers
@@ -0,0 +1,5 @@
{
"name": "Map Layers",
"type": "group",
"description": "Contains a list of map layers available in the current project."
}
5 changes: 5 additions & 0 deletions resources/function_help/json/Relations
@@ -0,0 +1,5 @@
{
"name": "Relations",
"type": "group",
"description": "Contains a list of relations available in the current project."
}
43 changes: 43 additions & 0 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -25,6 +25,9 @@
#include "qgsfeatureiterator.h"
#include "qgsvectorlayer.h"
#include "qgssettings.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgsrelation.h"

#include <QMenu>
#include <QFile>
Expand Down Expand Up @@ -440,6 +443,26 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
}
}

void QgsExpressionBuilderWidget::loadLayers()
{
QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
for ( ; layerIt != layers.constEnd(); ++layerIt )
{
registerItemForAllGroups( QStringList() << tr( "Map Layers" ), layerIt.value()->name(), QStringLiteral( "'%1'" ).arg( layerIt.key() ), formatLayerHelp( layerIt.value() ) );
}
}

void QgsExpressionBuilderWidget::loadRelations()
{
QMap<QString, QgsRelation> relations = QgsProject::instance()->relationManager()->relations();
QMap<QString, QgsRelation>::const_iterator relIt = relations.constBegin();
for ( ; relIt != relations.constEnd(); ++relIt )
{
registerItemForAllGroups( QStringList() << tr( "Relations" ), relIt->name(), QStringLiteral( "'%1'" ).arg( relIt->id() ), formatRelationHelp( relIt.value() ) );
}
}

void QgsExpressionBuilderWidget::updateFunctionTree()
{
mModel->clear();
Expand Down Expand Up @@ -495,6 +518,12 @@ void QgsExpressionBuilderWidget::updateFunctionTree()
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText() );
}

// load relation names
loadRelations();

// load layer IDs
loadLayers();

loadExpressionContext();
}

Expand Down Expand Up @@ -614,6 +643,20 @@ void QgsExpressionBuilderWidget::registerItemForAllGroups( const QStringList &gr
}
}

QString QgsExpressionBuilderWidget::formatRelationHelp( const QgsRelation &relation ) const
{
QString text = QStringLiteral( "<p>%1</p>" ).arg( tr( "Inserts the relation ID for the relation named '%1'." ).arg( relation.name() ) );
text.append( QStringLiteral( "<p>%1</p>" ).arg( tr( "Current value: '%1'" ).arg( relation.id() ) ) );
return text;
}

QString QgsExpressionBuilderWidget::formatLayerHelp( const QgsMapLayer *layer ) const
{
QString text = QStringLiteral( "<p>%1</p>" ).arg( tr( "Inserts the layer ID for the layer named '%1'." ).arg( layer->name() ) );
text.append( QStringLiteral( "<p>%1</p>" ).arg( tr( "Current value: '%1'" ).arg( layer->id() ) ) );
return text;
}

void QgsExpressionBuilderWidget::showEvent( QShowEvent *e )
{
QWidget::showEvent( e );
Expand Down
17 changes: 17 additions & 0 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -31,6 +31,7 @@

class QgsFields;
class QgsExpressionHighlighter;
class QgsRelation;

/** \ingroup gui
* An expression item that can be used in the QgsExpressionBuilderWidget tree.
Expand Down Expand Up @@ -286,6 +287,12 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp

void loadExpressionContext();

//! Loads current project relations names/id into the expression help tree
void loadRelations();

//! Loads current project layer names/ids into the expression help tree
void loadLayers();

/** Registers a node item for the expression builder, adding multiple items when the function exists in multiple groups
* \param groups The groups the item will be show in the tree view. If a group doesn't exist it will be created.
* \param label The label that is show to the user for the item in the tree.
Expand All @@ -300,6 +307,16 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false, int sortOrder = 1 );

/**
* Returns a HTML formatted string for use as a \a relation item help.
*/
QString formatRelationHelp( const QgsRelation &relation ) const;

/**
* Returns a HTML formatted string for use as a \a layer item help.
*/
QString formatLayerHelp( const QgsMapLayer *layer ) const;

bool mAutoSave;
QString mFunctionsPath;
QgsVectorLayer *mLayer = nullptr;
Expand Down

0 comments on commit 878ee5c

Please sign in to comment.