Skip to content
Permalink
Browse files
addresses #46673 (QgsProcessingMapLayerComboBox shows layers out of
QgsProcessingParameterWidgetContext)

added a `setProject(QgsProject *project)` to QgsMapLayerModel,
QgsMapLayerProxyModel and QgsMapLayerComboBox

QgsProcessingMapLayerComboBox::setWidgetContext(...) now uses the
context project
  • Loading branch information
jakimowb authored and nyalldawson committed Jan 7, 2022
1 parent 28b7fd9 commit 9b3e437ae3d9e91f0f504ed4ee072b1a4065b54c
@@ -54,6 +54,13 @@ populate the model.
void setItemsCheckable( bool checkable );
%Docstring
setItemsCheckable defines if layers should be selectable in the widget
%End

void setProject( QgsProject *project );
%Docstring
Sets tje :py:class:`QgsProject` from which map layers are shown

.. versionadded:: 3.24
%End

void setItemsCanBeReordered( bool allow );
@@ -69,6 +69,13 @@ Returns the filter flags which affect how layers are filtered within the model.
.. seealso:: :py:func:`setFilters`

.. versionadded:: 2.3
%End

void setProject( QgsProject *project );
%Docstring
Sets the :py:class:`QgsProject` from which map layers are shown

.. versionadded:: 3.24
%End

static bool layerMatchesFilters( const QgsMapLayer *layer, const Filters &filters );
@@ -68,6 +68,14 @@ Returns the list of data providers which are excluded from the combobox.
.. versionadded:: 3.0
%End

void setProject( QgsProject *project );
%Docstring
Sets the :py:class:`QgsProject` from which map layers are shown

.. versionadded:: 3.24
%End


void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
%Docstring
Sets whether an optional empty layer ("not set") option is shown in the combo box.
@@ -40,6 +40,25 @@ QgsMapLayerModel::QgsMapLayerModel( QObject *parent, QgsProject *project )
addLayers( mProject->mapLayers().values() );
}

void QgsMapLayerModel::setProject( QgsProject *project )
{

// remove layers from previous project
if ( mProject )
{
removeLayers( mProject->mapLayers().keys() );
disconnect( mProject, &QgsProject::layersAdded, this, &QgsMapLayerModel::addLayers );
disconnect( mProject, static_cast < void ( QgsProject::* )( const QStringList & ) >( &QgsProject::layersWillBeRemoved ), this, &QgsMapLayerModel::removeLayers );
}

mProject = project;

connect( mProject, &QgsProject::layersAdded, this, &QgsMapLayerModel::addLayers );
connect( mProject, static_cast < void ( QgsProject::* )( const QStringList & ) >( &QgsProject::layersWillBeRemoved ), this, &QgsMapLayerModel::removeLayers );
addLayers( mProject->mapLayers().values() );
}


void QgsMapLayerModel::setItemsCheckable( bool checkable )
{
mItemCheckable = checkable;
@@ -76,6 +76,13 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
*/
void setItemsCheckable( bool checkable );

/**
* Sets tje QgsProject from which map layers are shown
*
* \since QGIS 3.24
*/
void setProject( QgsProject *project );

/**
* Sets whether items in the model can be reordered via drag and drop.
*
@@ -107,6 +107,11 @@ void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &ex
invalidateFilter();
}

void QgsMapLayerProxyModel::setProject( QgsProject *project )
{
mModel->setProject( project );
}

void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
{
mExceptList.clear();
@@ -24,6 +24,7 @@

class QgsMapLayerModel;
class QgsMapLayer;
class QgsProject;

/**
* \ingroup core
@@ -88,6 +89,13 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
*/
const Filters &filters() const { return mFilters; }

/**
* Sets the QgsProject from which map layers are shown
*
* \since QGIS 3.24
*/
void setProject( QgsProject *project );

/**
* Returns if the \a layer matches the given \a filters
* \since QGIS 3.14
@@ -373,6 +373,7 @@ QVariant QgsProcessingMapLayerComboBox::value() const
void QgsProcessingMapLayerComboBox::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
{
mBrowserModel = context.browserModel();
mCombo->setProject( context.project() );
}

void QgsProcessingMapLayerComboBox::setEditable( bool editable )
@@ -39,6 +39,12 @@ void QgsMapLayerComboBox::setExcludedProviders( const QStringList &providers )
mProxyModel->setExcludedProviders( providers );
}

void QgsMapLayerComboBox::setProject( QgsProject *project )
{
mProxyModel->setProject( project );
}


QStringList QgsMapLayerComboBox::excludedProviders() const
{
return mProxyModel->excludedProviders();
@@ -73,6 +73,14 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
*/
QStringList excludedProviders() const;

/**
* Sets the QgsProject from which map layers are shown
*
* \since QGIS 3.24
*/
void setProject( QgsProject *project );


/**
* Sets whether an optional empty layer ("not set") option is shown in the combo box.
*
@@ -264,6 +264,22 @@ def testAdditionalLayers(self):
self.assertEqual(m.itemText(3), 'a')
self.assertEqual(m.itemText(4), 'b')

def testProject(self):
QgsProject.instance().clear()
lA = create_layer('lA')
lB = create_layer('lB')
QgsProject.instance().addMapLayer(lA)
projectB = QgsProject()
projectB.addMapLayer(lB)
cb = QgsMapLayerComboBox()
self.assertEqual(cb.currentLayer(), lA)

m.setProject(projectB)
self.assertEqual(cb.currentLayer(), lB)

m.setProject(projectA)
self.assertEqual(cb.currentLayer(), lA)


if __name__ == '__main__':
unittest.main()
@@ -258,6 +258,26 @@ def testIndexFromLayer(self):

QgsProject.instance().removeMapLayers([l1.id(), l2.id()])

def testProject(self):
lA = create_layer('lA')
lB = create_layer('lB')
projectA = QgsProject.instance()
projectB = QgsProject()

projectA.addMapLayer(lA)
projectB.addMapLayer(lB)

m = QgsMapLayerModel()
self.assertEqual(m.data(m.index(0, 0), Qt.DisplayRole), lA.name())

m.setProject(projectB)
self.assertEqual(m.data(m.index(0, 0), Qt.DisplayRole), lB.name())

m.setProject(projectA)
self.assertEqual(m.data(m.index(0, 0), Qt.DisplayRole), lA.name())

QgsProject.instance().removeAllMapLayers()

def testDisplayRole(self):
l1 = create_layer('l1')
l2 = create_layer('l2')

0 comments on commit 9b3e437

Please sign in to comment.