Skip to content

Commit 878ee5c

Browse files
committedJul 30, 2017
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
1 parent cf753e9 commit 878ee5c

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
 
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Map Layers",
3+
"type": "group",
4+
"description": "Contains a list of map layers available in the current project."
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Relations",
3+
"type": "group",
4+
"description": "Contains a list of relations available in the current project."
5+
}

‎src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "qgsfeatureiterator.h"
2626
#include "qgsvectorlayer.h"
2727
#include "qgssettings.h"
28+
#include "qgsproject.h"
29+
#include "qgsrelationmanager.h"
30+
#include "qgsrelation.h"
2831

2932
#include <QMenu>
3033
#include <QFile>
@@ -440,6 +443,26 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
440443
}
441444
}
442445

446+
void QgsExpressionBuilderWidget::loadLayers()
447+
{
448+
QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
449+
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
450+
for ( ; layerIt != layers.constEnd(); ++layerIt )
451+
{
452+
registerItemForAllGroups( QStringList() << tr( "Map Layers" ), layerIt.value()->name(), QStringLiteral( "'%1'" ).arg( layerIt.key() ), formatLayerHelp( layerIt.value() ) );
453+
}
454+
}
455+
456+
void QgsExpressionBuilderWidget::loadRelations()
457+
{
458+
QMap<QString, QgsRelation> relations = QgsProject::instance()->relationManager()->relations();
459+
QMap<QString, QgsRelation>::const_iterator relIt = relations.constBegin();
460+
for ( ; relIt != relations.constEnd(); ++relIt )
461+
{
462+
registerItemForAllGroups( QStringList() << tr( "Relations" ), relIt->name(), QStringLiteral( "'%1'" ).arg( relIt->id() ), formatRelationHelp( relIt.value() ) );
463+
}
464+
}
465+
443466
void QgsExpressionBuilderWidget::updateFunctionTree()
444467
{
445468
mModel->clear();
@@ -495,6 +518,12 @@ void QgsExpressionBuilderWidget::updateFunctionTree()
495518
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText() );
496519
}
497520

521+
// load relation names
522+
loadRelations();
523+
524+
// load layer IDs
525+
loadLayers();
526+
498527
loadExpressionContext();
499528
}
500529

@@ -614,6 +643,20 @@ void QgsExpressionBuilderWidget::registerItemForAllGroups( const QStringList &gr
614643
}
615644
}
616645

646+
QString QgsExpressionBuilderWidget::formatRelationHelp( const QgsRelation &relation ) const
647+
{
648+
QString text = QStringLiteral( "<p>%1</p>" ).arg( tr( "Inserts the relation ID for the relation named '%1'." ).arg( relation.name() ) );
649+
text.append( QStringLiteral( "<p>%1</p>" ).arg( tr( "Current value: '%1'" ).arg( relation.id() ) ) );
650+
return text;
651+
}
652+
653+
QString QgsExpressionBuilderWidget::formatLayerHelp( const QgsMapLayer *layer ) const
654+
{
655+
QString text = QStringLiteral( "<p>%1</p>" ).arg( tr( "Inserts the layer ID for the layer named '%1'." ).arg( layer->name() ) );
656+
text.append( QStringLiteral( "<p>%1</p>" ).arg( tr( "Current value: '%1'" ).arg( layer->id() ) ) );
657+
return text;
658+
}
659+
617660
void QgsExpressionBuilderWidget::showEvent( QShowEvent *e )
618661
{
619662
QWidget::showEvent( e );

‎src/gui/qgsexpressionbuilderwidget.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
class QgsFields;
3333
class QgsExpressionHighlighter;
34+
class QgsRelation;
3435

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

287288
void loadExpressionContext();
288289

290+
//! Loads current project relations names/id into the expression help tree
291+
void loadRelations();
292+
293+
//! Loads current project layer names/ids into the expression help tree
294+
void loadLayers();
295+
289296
/** Registers a node item for the expression builder, adding multiple items when the function exists in multiple groups
290297
* \param groups The groups the item will be show in the tree view. If a group doesn't exist it will be created.
291298
* \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
300307
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
301308
bool highlightedItem = false, int sortOrder = 1 );
302309

310+
/**
311+
* Returns a HTML formatted string for use as a \a relation item help.
312+
*/
313+
QString formatRelationHelp( const QgsRelation &relation ) const;
314+
315+
/**
316+
* Returns a HTML formatted string for use as a \a layer item help.
317+
*/
318+
QString formatLayerHelp( const QgsMapLayer *layer ) const;
319+
303320
bool mAutoSave;
304321
QString mFunctionsPath;
305322
QgsVectorLayer *mLayer = nullptr;

0 commit comments

Comments
 (0)
Please sign in to comment.