Skip to content
Permalink
Browse files

Add items for project map layers and relations to expression builder

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 authored and m-kuhn committed Sep 28, 2017
1 parent 4312504 commit ae806ad11c8197ca337d92fc5c303ef4c4124f38
@@ -0,0 +1,5 @@
{
"name": "Map Layers",
"type": "group",
"description": "Contains a list of map layers available in the current project."
}
@@ -0,0 +1,5 @@
{
"name": "Relations",
"type": "group",
"description": "Contains a list of relations available in the current project."
}
@@ -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>
@@ -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();
@@ -495,6 +518,12 @@ void QgsExpressionBuilderWidget::updateFunctionTree()
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText() );
}

// load relation names
loadRelations();

// load layer IDs
loadLayers();

loadExpressionContext();
}

@@ -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 );
@@ -31,6 +31,7 @@

class QgsFields;
class QgsExpressionHighlighter;
class QgsRelation;

/** \ingroup gui
* An expression item that can be used in the QgsExpressionBuilderWidget tree.
@@ -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.
@@ -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;

0 comments on commit ae806ad

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