diff --git a/python/gui/qgsexpressionbuilderwidget.sip b/python/gui/qgsexpressionbuilderwidget.sip index 83ec875eb220..7a0d0dc8b35e 100644 --- a/python/gui/qgsexpressionbuilderwidget.sip +++ b/python/gui/qgsexpressionbuilderwidget.sip @@ -244,6 +244,22 @@ Sets the expression string for the widget :rtype: QStandardItemModel %End + QgsProject *project(); +%Docstring + Returns the project currently associated with the widget. +.. seealso:: setProject() +.. versionadded:: 3.0 + :rtype: QgsProject +%End + + void setProject( QgsProject *project ); +%Docstring + Sets the ``project`` currently associated with the widget. This + controls which layers and relations and other project-specific items are shown in the widget. +.. seealso:: project() +.. versionadded:: 3.0 +%End + public slots: void loadSampleValues(); diff --git a/src/gui/qgsexpressionbuilderwidget.cpp b/src/gui/qgsexpressionbuilderwidget.cpp index 1adb54f80648..f5b73fec3de4 100644 --- a/src/gui/qgsexpressionbuilderwidget.cpp +++ b/src/gui/qgsexpressionbuilderwidget.cpp @@ -45,6 +45,7 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent ) , mLayer( nullptr ) , highlighter( nullptr ) , mExpressionValid( false ) + , mProject( QgsProject::instance() ) { setupUi( this ); @@ -445,7 +446,10 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection ) void QgsExpressionBuilderWidget::loadLayers() { - QMap layers = QgsProject::instance()->mapLayers(); + if ( !mProject ) + return; + + QMap layers = mProject->mapLayers(); QMap::const_iterator layerIt = layers.constBegin(); for ( ; layerIt != layers.constEnd(); ++layerIt ) { @@ -455,7 +459,10 @@ void QgsExpressionBuilderWidget::loadLayers() void QgsExpressionBuilderWidget::loadRelations() { - QMap relations = QgsProject::instance()->relationManager()->relations(); + if ( !mProject ) + return; + + QMap relations = mProject->relationManager()->relations(); QMap::const_iterator relIt = relations.constBegin(); for ( ; relIt != relations.constEnd(); ++relIt ) { @@ -662,6 +669,17 @@ QStandardItemModel *QgsExpressionBuilderWidget::model() return mModel; } +QgsProject *QgsExpressionBuilderWidget::project() +{ + return mProject; +} + +void QgsExpressionBuilderWidget::setProject( QgsProject *project ) +{ + mProject = project; + updateFunctionTree(); +} + void QgsExpressionBuilderWidget::showEvent( QShowEvent *e ) { QWidget::showEvent( e ); diff --git a/src/gui/qgsexpressionbuilderwidget.h b/src/gui/qgsexpressionbuilderwidget.h index 4ffdd4c6baa4..3c27aaf53c02 100644 --- a/src/gui/qgsexpressionbuilderwidget.h +++ b/src/gui/qgsexpressionbuilderwidget.h @@ -233,6 +233,21 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp */ QStandardItemModel *model(); + /** + * Returns the project currently associated with the widget. + * \see setProject() + * \since QGIS 3.0 + */ + QgsProject *project(); + + /** + * Sets the \a project currently associated with the widget. This + * controls which layers and relations and other project-specific items are shown in the widget. + * \see project() + * \since QGIS 3.0 + */ + void setProject( QgsProject *project ); + public slots: /** @@ -338,6 +353,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp QString mRecentKey; QMap mFieldValues; QgsExpressionContext mExpressionContext; + QPointer< QgsProject > mProject; }; #endif // QGSEXPRESSIONBUILDER_H diff --git a/tests/src/python/test_qgsexpressionbuilderwidget.py b/tests/src/python/test_qgsexpressionbuilderwidget.py index 0041bb6fe08e..090102e20dc2 100644 --- a/tests/src/python/test_qgsexpressionbuilderwidget.py +++ b/tests/src/python/test_qgsexpressionbuilderwidget.py @@ -122,6 +122,19 @@ def testLayers(self): items = m.findItems('layer2', Qt.MatchRecursive) self.assertEqual(len(items), 1) + # change project + p2 = QgsProject() + layer3 = QgsVectorLayer("Point", "layer3", "memory") + p2.addMapLayers([layer3]) + w.setProject(p2) + m = w.model() + items = m.findItems('layer1', Qt.MatchRecursive) + self.assertEqual(len(items), 0) + items = m.findItems('layer2', Qt.MatchRecursive) + self.assertEqual(len(items), 0) + items = m.findItems('layer3', Qt.MatchRecursive) + self.assertEqual(len(items), 1) + def testRelations(self): """ check that layers are shown in widget model""" p = QgsProject.instance()