Skip to content
Permalink
Browse files

Allow setting project for QgsExpressionBuilderWidget

(still defaults to QgsProject::instance())
  • Loading branch information
nyalldawson committed Jul 30, 2017
1 parent e79bf3f commit 917263a9a3b48369ccb33f72ee66f0ff17d0be2f
@@ -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();
@@ -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<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 )
{
@@ -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 )
{
@@ -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 );
@@ -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<QString, QStringList> mFieldValues;
QgsExpressionContext mExpressionContext;
QPointer< QgsProject > mProject;
};

#endif // QGSEXPRESSIONBUILDER_H
@@ -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()

0 comments on commit 917263a

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