Skip to content

Commit 917263a

Browse files
committedJul 30, 2017
Allow setting project for QgsExpressionBuilderWidget
(still defaults to QgsProject::instance())
1 parent e79bf3f commit 917263a

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed
 

‎python/gui/qgsexpressionbuilderwidget.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ Sets the expression string for the widget
244244
:rtype: QStandardItemModel
245245
%End
246246

247+
QgsProject *project();
248+
%Docstring
249+
Returns the project currently associated with the widget.
250+
.. seealso:: setProject()
251+
.. versionadded:: 3.0
252+
:rtype: QgsProject
253+
%End
254+
255+
void setProject( QgsProject *project );
256+
%Docstring
257+
Sets the ``project`` currently associated with the widget. This
258+
controls which layers and relations and other project-specific items are shown in the widget.
259+
.. seealso:: project()
260+
.. versionadded:: 3.0
261+
%End
262+
247263
public slots:
248264

249265
void loadSampleValues();

‎src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
4545
, mLayer( nullptr )
4646
, highlighter( nullptr )
4747
, mExpressionValid( false )
48+
, mProject( QgsProject::instance() )
4849
{
4950
setupUi( this );
5051

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

446447
void QgsExpressionBuilderWidget::loadLayers()
447448
{
448-
QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
449+
if ( !mProject )
450+
return;
451+
452+
QMap<QString, QgsMapLayer *> layers = mProject->mapLayers();
449453
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
450454
for ( ; layerIt != layers.constEnd(); ++layerIt )
451455
{
@@ -455,7 +459,10 @@ void QgsExpressionBuilderWidget::loadLayers()
455459

456460
void QgsExpressionBuilderWidget::loadRelations()
457461
{
458-
QMap<QString, QgsRelation> relations = QgsProject::instance()->relationManager()->relations();
462+
if ( !mProject )
463+
return;
464+
465+
QMap<QString, QgsRelation> relations = mProject->relationManager()->relations();
459466
QMap<QString, QgsRelation>::const_iterator relIt = relations.constBegin();
460467
for ( ; relIt != relations.constEnd(); ++relIt )
461468
{
@@ -662,6 +669,17 @@ QStandardItemModel *QgsExpressionBuilderWidget::model()
662669
return mModel;
663670
}
664671

672+
QgsProject *QgsExpressionBuilderWidget::project()
673+
{
674+
return mProject;
675+
}
676+
677+
void QgsExpressionBuilderWidget::setProject( QgsProject *project )
678+
{
679+
mProject = project;
680+
updateFunctionTree();
681+
}
682+
665683
void QgsExpressionBuilderWidget::showEvent( QShowEvent *e )
666684
{
667685
QWidget::showEvent( e );

‎src/gui/qgsexpressionbuilderwidget.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
233233
*/
234234
QStandardItemModel *model();
235235

236+
/**
237+
* Returns the project currently associated with the widget.
238+
* \see setProject()
239+
* \since QGIS 3.0
240+
*/
241+
QgsProject *project();
242+
243+
/**
244+
* Sets the \a project currently associated with the widget. This
245+
* controls which layers and relations and other project-specific items are shown in the widget.
246+
* \see project()
247+
* \since QGIS 3.0
248+
*/
249+
void setProject( QgsProject *project );
250+
236251
public slots:
237252

238253
/**
@@ -338,6 +353,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
338353
QString mRecentKey;
339354
QMap<QString, QStringList> mFieldValues;
340355
QgsExpressionContext mExpressionContext;
356+
QPointer< QgsProject > mProject;
341357
};
342358

343359
#endif // QGSEXPRESSIONBUILDER_H

‎tests/src/python/test_qgsexpressionbuilderwidget.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ def testLayers(self):
122122
items = m.findItems('layer2', Qt.MatchRecursive)
123123
self.assertEqual(len(items), 1)
124124

125+
# change project
126+
p2 = QgsProject()
127+
layer3 = QgsVectorLayer("Point", "layer3", "memory")
128+
p2.addMapLayers([layer3])
129+
w.setProject(p2)
130+
m = w.model()
131+
items = m.findItems('layer1', Qt.MatchRecursive)
132+
self.assertEqual(len(items), 0)
133+
items = m.findItems('layer2', Qt.MatchRecursive)
134+
self.assertEqual(len(items), 0)
135+
items = m.findItems('layer3', Qt.MatchRecursive)
136+
self.assertEqual(len(items), 1)
137+
125138
def testRelations(self):
126139
""" check that layers are shown in widget model"""
127140
p = QgsProject.instance()

0 commit comments

Comments
 (0)
Please sign in to comment.