Skip to content
Permalink
Browse files

use QgsLayerTreeFilterProxyModel for layer dependency tree

  • Loading branch information
3nids committed Jun 5, 2020
1 parent 3322d2b commit 961ffcc94ebdd5d316312dad5c1c9f8e8dce0806
@@ -42,6 +42,7 @@ Adds a properties page factory to the vector layer properties dialog.



public:
};


@@ -266,6 +266,7 @@ QgsSnappingLayerTreeModel::QgsSnappingLayerTreeModel( QgsProject *project, QgsMa
{
connect( project, &QgsProject::snappingConfigChanged, this, &QgsSnappingLayerTreeModel::onSnappingSettingsChanged );
connect( project, &QgsProject::avoidIntersectionsLayersChanged, this, &QgsSnappingLayerTreeModel::onSnappingSettingsChanged );
connect( project, &QgsProject::readProject, this, [ = ] {resetLayerTreeModel();} );
}

int QgsSnappingLayerTreeModel::columnCount( const QModelIndex &parent ) const
@@ -92,7 +92,6 @@ QgsSnappingWidget::QgsSnappingWidget( QgsProject *project, QgsMapCanvas *canvas,
connect( model, &QgsSnappingLayerTreeModel::rowsInserted, this, &QgsSnappingWidget::onSnappingTreeLayersChanged );
connect( model, &QgsSnappingLayerTreeModel::modelReset, this, &QgsSnappingWidget::onSnappingTreeLayersChanged );
connect( model, &QgsSnappingLayerTreeModel::rowsRemoved, this, &QgsSnappingWidget::onSnappingTreeLayersChanged );
connect( mProject, &QgsProject::readProject, this, [ = ] {model->resetLayerTreeModel();} );
connect( mProject, &QObject::destroyed, this, [ = ] {mLayerTreeView->setModel( nullptr );} );
// model->setFlags( 0 );
mLayerTreeView->setModel( model );
@@ -379,28 +379,20 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
title += QStringLiteral( " (%1)" ).arg( mLayer->styleManager()->currentStyle() );
restoreOptionsBaseUi( title );

mLayersDependenciesTreeGroup.reset( QgsProject::instance()->layerTreeRoot()->clone() );
mLayersDependenciesTreeModel.reset( new QgsLayerTreeModel( mLayersDependenciesTreeGroup.get() ) );
// use visibility as selection
mLayersDependenciesTreeModel->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility );

mLayersDependenciesTreeGroup->setItemVisibilityChecked( false );

QSet<QString> dependencySources;
QList<QgsMapLayer *> dependencySources;
const QSet<QgsMapLayerDependency> constDependencies = mLayer->dependencies();
for ( const QgsMapLayerDependency &dep : constDependencies )
{
dependencySources << dep.layerId();
}
const QList<QgsLayerTreeLayer *> constFindLayers = mLayersDependenciesTreeGroup->findLayers();
for ( QgsLayerTreeLayer *layer : constFindLayers )
{
layer->setItemVisibilityChecked( dependencySources.contains( layer->layerId() ) );
QgsMapLayer *layer = QgsProject::instance()->mapLayer( dep.layerId() );
if ( layer )
dependencySources << layer;
}

QgsMapSettings ms = mCanvas->mapSettings();
mLayersDependenciesTreeModel->setLegendMapViewData( mCanvas->mapUnitsPerPixel(), ms.outputDpi(), ms.scale() );
mLayersDependenciesTreeView->setModel( mLayersDependenciesTreeModel.get() );
mLayersDependenciesTreeModel = new DependenciesLayerTreeModel( mLayer, this );
mLayersDependenciesTreeModel->setLayerTreeModel( new QgsLayerTreeModel( QgsProject::instance()->layerTreeRoot(), mLayersDependenciesTreeModel ) );
mLayersDependenciesTreeModel->setCheckedLayers( dependencySources );
connect( QgsProject::instance(), &QObject::destroyed, this, [ = ] {mLayersDependenciesTreeView->setModel( nullptr );} );
mLayersDependenciesTreeView->setModel( mLayersDependenciesTreeModel );

connect( mRefreshLayerCheckBox, &QCheckBox::toggled, mRefreshLayerIntervalSpinBox, &QDoubleSpinBox::setEnabled );

@@ -784,12 +776,9 @@ void QgsVectorLayerProperties::apply()

// save dependencies
QSet<QgsMapLayerDependency> deps;
const auto constFindLayers = mLayersDependenciesTreeGroup->findLayers();
for ( const QgsLayerTreeLayer *layer : constFindLayers )
{
if ( layer->isVisible() )
deps << QgsMapLayerDependency( layer->layerId() );
}
const auto checkedLayers = mLayersDependenciesTreeModel->checkedLayers();
for ( const QgsMapLayer *layer : checkedLayers )
deps << QgsMapLayerDependency( layer->id() );
if ( ! mLayer->setDependencies( deps ) )
{
QMessageBox::warning( nullptr, tr( "Save Dependency" ), tr( "This configuration introduces a cycle in data dependencies and will be ignored." ) );
@@ -28,6 +28,7 @@
#include "qgsvectorlayerserverproperties.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreefilterproxymodel.h"

class QgsMapLayer;

@@ -159,6 +160,19 @@ class GUI_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private

private:

class DependenciesLayerTreeModel : public QgsLayerTreeFilterProxyModel
{
public:
DependenciesLayerTreeModel( QgsVectorLayer *mainLayer, QObject *parent = nullptr )
: QgsLayerTreeFilterProxyModel( parent )
, mMainLayer( mainLayer )
{}

private:
QgsVectorLayer *mMainLayer = nullptr;
bool layerShown( QgsMapLayer *layer ) const override {return layer != mMainLayer;}
};

enum PropertyType
{
Style = 0,
@@ -227,8 +241,7 @@ class GUI_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private

QgsExpressionContext createExpressionContext() const override;

std::unique_ptr<QgsLayerTree> mLayersDependenciesTreeGroup;
std::unique_ptr<QgsLayerTreeModel> mLayersDependenciesTreeModel;
DependenciesLayerTreeModel *mLayersDependenciesTreeModel;

void showHelp();

0 comments on commit 961ffcc

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