Skip to content

Commit

Permalink
Allow setting project for QgsExpressionBuilderWidget
Browse files Browse the repository at this point in the history
(still defaults to QgsProject::instance())
  • Loading branch information
nyalldawson committed Jul 30, 2017
1 parent e79bf3f commit 917263a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
16 changes: 16 additions & 0 deletions python/gui/qgsexpressionbuilderwidget.sip
Expand Up @@ -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();
Expand Down
22 changes: 20 additions & 2 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -45,6 +45,7 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
, mLayer( nullptr )
, highlighter( nullptr )
, mExpressionValid( false )
, mProject( QgsProject::instance() )
{
setupUi( this );

Expand Down Expand Up @@ -445,7 +446,10 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )

void QgsExpressionBuilderWidget::loadLayers()
{
QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
if ( !mProject )
return;

QMap<QString, QgsMapLayer *> layers = mProject->mapLayers();
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
for ( ; layerIt != layers.constEnd(); ++layerIt )
{
Expand All @@ -455,7 +459,10 @@ void QgsExpressionBuilderWidget::loadLayers()

void QgsExpressionBuilderWidget::loadRelations()
{
QMap<QString, QgsRelation> relations = QgsProject::instance()->relationManager()->relations();
if ( !mProject )
return;

QMap<QString, QgsRelation> relations = mProject->relationManager()->relations();
QMap<QString, QgsRelation>::const_iterator relIt = relations.constBegin();
for ( ; relIt != relations.constEnd(); ++relIt )
{
Expand Down Expand Up @@ -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 );
Expand Down
16 changes: 16 additions & 0 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -338,6 +353,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QString mRecentKey;
QMap<QString, QStringList> mFieldValues;
QgsExpressionContext mExpressionContext;
QPointer< QgsProject > mProject;
};

#endif // QGSEXPRESSIONBUILDER_H
13 changes: 13 additions & 0 deletions tests/src/python/test_qgsexpressionbuilderwidget.py
Expand Up @@ -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()
Expand Down

0 comments on commit 917263a

Please sign in to comment.