Allow setting project for QgsExpressionBuilderWidget

(still defaults to QgsProject::instance())
This commit is contained in:
Nyall Dawson 2017-07-31 08:18:09 +10:00
parent e79bf3fb94
commit 917263a9a3
4 changed files with 65 additions and 2 deletions

View File

@ -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();

View File

@ -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 );

View File

@ -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

View File

@ -122,6 +122,19 @@ class TestQgsExpressionBuilderWidget(unittest.TestCase):
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()