Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Change of ergonomy of the visibility of layers inside groups
See qgis/QGIS-Enhancement-Proposals#86

- Checking/unchecking a group doesn't change the check state of its children.
A node is visible if and only if it is checked and all its parents too.
- There is no more a semi-checked state for a group
- Ctrl-clic on a unchecked group will check the group and all its descendants.
- Ctrl-clic on a unchecked layer will check the lager and all its parents.
- Ctrl-clic on a checked group will uncheck the group and all its descendants.
- Ctrl-clic on a checked layer will uncheck the layer and all its parents.
- Those actions are available in contextual menu items in the tree view.
- Invisible layers because they or their parent(s) is unchecked are greyed out.
  • Loading branch information
rouault committed Jan 2, 2017
1 parent 3ba2f8e commit 60a1701
Show file tree
Hide file tree
Showing 32 changed files with 423 additions and 339 deletions.
15 changes: 13 additions & 2 deletions doc/api_break.dox
Expand Up @@ -687,6 +687,13 @@ QgsCptCitySelectionItem {#qgis_api_break_3_0_QgsCptCitySelectionItem}

- parseXML() has been renamed to parseXml()


QgsCustomLayerOrderWidget {#qgis_api_break_3_0_QgsCustomLayerOrderWidget}
-------------------------

- the signature of the visibilityChanged() signal is changed to visibilityChanged( QgsLayerTreeNode *node )


QgsCRSCache {#qgis_api_break_3_0_QgsCRSCache}
-----------

Expand Down Expand Up @@ -1059,12 +1066,16 @@ QgsLayerTreeGroup {#qgis_api_break_3_0_QgsLayerTreeGroup}
-----------------

- readChildrenFromXML() has been renamed to readChildrenFromXml()

- isVisible() is moved to QgsLayerTreeNode
- setVisible() is replaced by QgsLayerTreeNode::setItemVisibilityChecked()
- protected methods updateVisibilityFromChildren() and updateChildVisibility() removed

QgsLayerTreeLayer {#qgis_api_break_3_0_QgsLayerTreeLayer}
-----------------

- setLayerName(), layerName() were renamed to setName(), name()
- isVisible() is moved to QgsLayerTreeNode
- setVisible() is replaced by QgsLayerTreeNode::setItemVisibilityChecked()


QgsLayerTreeModel {#qgis_api_break_3_0_QgsLayerTreeMode}
Expand All @@ -1090,7 +1101,7 @@ QgsLayerTreeNode {#qgis_api_break_3_0_QgsLayerTreeNode}

- readCommonXML() has been renamed to readCommonXml()
- writeCommonXML() has been renamed to writeCommonXml()

- the signature of the visibilityChanged() signal is changed to visibilityChanged( QgsLayerTreeNode *node )

QgsLimitedRandomColorRampDialog {#qgis_api_break_3_0_QgsLimitedRandomRampDialog}
-------------------------------
Expand Down
11 changes: 1 addition & 10 deletions python/core/layertree/qgslayertreegroup.sip
Expand Up @@ -71,11 +71,6 @@ class QgsLayerTreeGroup : QgsLayerTreeNode
//! Return a clone of the group. The children are cloned too.
virtual QgsLayerTreeGroup* clone() const /Factory/;

//! Return the check state of the group node
Qt::CheckState isVisible() const;
//! Set check state of the group node - will also update children
void setVisible( Qt::CheckState state );

//! Return whether the group is mutually exclusive (only one child can be checked at a time)
//! @note added in 2.12
bool isMutuallyExclusive() const;
Expand All @@ -86,14 +81,10 @@ class QgsLayerTreeGroup : QgsLayerTreeNode
void setIsMutuallyExclusive( bool enabled, int initialChildIndex = -1 );

protected slots:
void layerDestroyed();
void nodeVisibilityChanged( QgsLayerTreeNode* node );

protected:
//! Set check state of this group from its children
void updateVisibilityFromChildren();
//! Set check state of children (when this group's check state changes) - if not mutually exclusive
void updateChildVisibility();

//! Set check state of children - if mutually exclusive
void updateChildVisibilityMutuallyExclusive();

Expand Down
3 changes: 0 additions & 3 deletions python/core/layertree/qgslayertreelayer.sip
Expand Up @@ -38,9 +38,6 @@ class QgsLayerTreeLayer : QgsLayerTreeNode
//! @note added in 3.0
void setName( const QString& n );

Qt::CheckState isVisible() const;
void setVisible( Qt::CheckState visible );

static QgsLayerTreeLayer* readXml( QDomElement& element ) /Factory/;
virtual void writeXml( QDomElement& parentElement );

Expand Down
1 change: 1 addition & 0 deletions python/core/layertree/qgslayertreemodel.sip
Expand Up @@ -61,6 +61,7 @@ class QgsLayerTreeModel : QAbstractItemModel
AllowNodeRename, //!< Allow renaming of groups and layers
AllowNodeChangeVisibility, //!< Allow user to set node visibility with a check box
AllowLegendChangeState, //!< Allow check boxes for legend nodes (if supported by layer's legend)
ActionHierarchical, //!< Check/uncheck action has consequences on children (or parents for leaf node)
};
typedef QFlags<QgsLayerTreeModel::Flag> Flags;

Expand Down
30 changes: 29 additions & 1 deletion python/core/layertree/qgslayertreenode.sip
Expand Up @@ -94,6 +94,34 @@ class QgsLayerTreeNode : QObject
//! Create a copy of the node. Returns new instance
virtual QgsLayerTreeNode *clone() const = 0 /Factory/;

//! Returns whether a node is really visible (ie checked and all its ancestors checked as well)
//! @note added in 3.0
bool isVisible() const;

//! Returns whether a node is checked (independantly of its ancestors or children)
//! @note added in 3.0
bool itemVisibilityChecked() const;

//! Check or uncheck a node (independantly of its ancestors or children)
//! @note added in 3.0
void setItemVisibilityChecked( bool checked );

//! Check or uncheck a node and all its children (taking into account exclusion rules)
//! @note added in 3.0
virtual void setItemVisibilityCheckedRecursive( bool checked );

//! Check or uncheck a node and all its parents
//! @note added in 3.0
void setItemVisibilityCheckedParentRecursive( bool checked );

//! Return whether this node is checked and all its children.
//! @note added in 3.0
bool isItemVisibilityCheckedRecursive() const;

//! Return whether this node is unchecked and all its children.
//! @note added in 3.0
bool isItemVisibilityUncheckedRecursive() const;

//! Return whether the node should be shown as expanded or collapsed in GUI
bool isExpanded() const;
//! Set whether the node should be shown as expanded or collapsed in GUI
Expand Down Expand Up @@ -121,7 +149,7 @@ class QgsLayerTreeNode : QObject
//! Emitted when one or more nodes has been removed from a node within the tree
void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
//! Emitted when check state of a node within the tree has been changed
void visibilityChanged( QgsLayerTreeNode *node, Qt::CheckState state );
void visibilityChanged( QgsLayerTreeNode *node );
//! Emitted when a custom property of a node within the tree has been changed or removed
void customPropertyChanged( QgsLayerTreeNode *node, const QString& key );
//! Emitted when the collapsed/expanded state of a node within the tree has been changed
Expand Down
3 changes: 2 additions & 1 deletion python/gui/layertree/qgscustomlayerorderwidget.sip
Expand Up @@ -20,7 +20,8 @@ class QgsCustomLayerOrderWidget : QWidget
protected slots:
void bridgeHasCustomLayerOrderChanged( bool state );
void bridgeCustomLayerOrderChanged( const QStringList& order );
void nodeVisibilityChanged( QgsLayerTreeNode* node, Qt::CheckState state );
//! Slot triggered when the ivsibility of a node changes
void nodeVisibilityChanged( QgsLayerTreeNode* node );

void modelUpdated();
};
9 changes: 9 additions & 0 deletions python/gui/layertree/qgslayertreeviewdefaultactions.sip
Expand Up @@ -20,6 +20,15 @@ class QgsLayerTreeViewDefaultActions : QObject
QAction* actionRenameGroupOrLayer( QObject* parent = 0 ) /Factory/;
QAction* actionShowFeatureCount( QObject* parent = 0 ) /Factory/;

//! Action to check a group and all its children
QAction* actionCheckAndAllChildren( QObject* parent = nullptr );

//! Action to uncheck a group and all its children
QAction* actionUncheckAndAllChildren( QObject* parent = nullptr );

//! Action to check a group and all its parents
QAction* actionCheckAndAllParents( QObject* parent = nullptr );

QAction* actionZoomToLayer( QgsMapCanvas* canvas, QObject* parent = 0 ) /Factory/;
QAction* actionZoomToGroup( QgsMapCanvas* canvas, QObject* parent = 0 ) /Factory/;
// TODO: zoom to selected
Expand Down
2 changes: 1 addition & 1 deletion src/app/dwg/qgsdwgimportdialog.cpp
Expand Up @@ -422,7 +422,7 @@ void QgsDwgImportDialog::createGroup( QgsLayerTreeGroup *group, QString name, QS
if ( !layerGroup->children().isEmpty() )
{
layerGroup->setExpanded( false );
layerGroup->setVisible( visible ? Qt::Checked : Qt::Unchecked );
layerGroup->setItemVisibilityChecked( visible );
}
else
{
Expand Down
30 changes: 13 additions & 17 deletions src/app/qgisapp.cpp
Expand Up @@ -2808,7 +2808,7 @@ void QgisApp::setupConnections()
this, SLOT( markDirty() ) );
connect( mLayerTreeView->layerTreeModel()->rootGroup(), SIGNAL( removedChildren( QgsLayerTreeNode*, int, int ) ),
this, SLOT( updateNewLayerInsertionPoint() ) );
connect( mLayerTreeView->layerTreeModel()->rootGroup(), SIGNAL( visibilityChanged( QgsLayerTreeNode*, Qt::CheckState ) ),
connect( mLayerTreeView->layerTreeModel()->rootGroup(), SIGNAL( visibilityChanged( QgsLayerTreeNode* ) ),
this, SLOT( markDirty() ) );
connect( mLayerTreeView->layerTreeModel()->rootGroup(), SIGNAL( customPropertyChanged( QgsLayerTreeNode*, QString ) ),
this, SLOT( markDirty() ) );
Expand Down Expand Up @@ -5759,19 +5759,16 @@ void QgisApp::stopRendering()
void QgisApp::hideAllLayers()
{
QgsDebugMsg( "hiding all layers!" );
mLayerTreeView->layerTreeModel()->rootGroup()->setItemVisibilityCheckedRecursive( false );

Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, mLayerTreeView->layerTreeModel()->rootGroup()->findLayers() )
nodeLayer->setVisible( Qt::Unchecked );
}


// reimplements method from base (gui) class
void QgisApp::showAllLayers()
{
QgsDebugMsg( "Showing all layers!" );

Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, mLayerTreeView->layerTreeModel()->rootGroup()->findLayers() )
nodeLayer->setVisible( Qt::Checked );
mLayerTreeView->layerTreeModel()->rootGroup()->setItemVisibilityCheckedRecursive( true );
}

//reimplements method from base (gui) class
Expand All @@ -5781,10 +5778,7 @@ void QgisApp::hideSelectedLayers()

Q_FOREACH ( QgsLayerTreeNode* node, mLayerTreeView->selectedNodes() )
{
if ( QgsLayerTree::isGroup( node ) )
QgsLayerTree::toGroup( node )->setVisible( Qt::Unchecked );
else if ( QgsLayerTree::isLayer( node ) )
QgsLayerTree::toLayer( node )->setVisible( Qt::Unchecked );
node->setItemVisibilityChecked( false );
}
}

Expand All @@ -5796,7 +5790,7 @@ void QgisApp::hideDeselectedLayers()
{
if ( selectedLayerNodes.contains( nodeLayer ) )
continue;
nodeLayer->setVisible( Qt::Unchecked );
nodeLayer->setItemVisibilityChecked( false );
}
}

Expand All @@ -5807,10 +5801,12 @@ void QgisApp::showSelectedLayers()

Q_FOREACH ( QgsLayerTreeNode* node, mLayerTreeView->selectedNodes() )
{
if ( QgsLayerTree::isGroup( node ) )
QgsLayerTree::toGroup( node )->setVisible( Qt::Checked );
else if ( QgsLayerTree::isLayer( node ) )
QgsLayerTree::toLayer( node )->setVisible( Qt::Checked );
QgsLayerTreeNode* nodeIter = node;
while ( nodeIter )
{
nodeIter->setItemVisibilityChecked( true );
nodeIter = nodeIter->parent();
}
}
}

Expand Down Expand Up @@ -8373,7 +8369,7 @@ void QgisApp::layerSubsetString()
// hide the old layer
QgsLayerTreeLayer* vLayerTreeLayer = QgsProject::instance()->layerTreeRoot()->findLayer( vlayer->id() );
if ( vLayerTreeLayer )
vLayerTreeLayer->setVisible( Qt::Unchecked );
vLayerTreeLayer->setItemVisibilityChecked( false );
vlayer = newLayer;
}
else
Expand Down Expand Up @@ -8663,7 +8659,7 @@ void QgisApp::duplicateLayers( const QList<QgsMapLayer *>& lyrList )
QgsLayerTreeLayer* nodeDupLayer = parentGroup->insertLayer( parentGroup->children().indexOf( nodeSelectedLyr ) + 1, dupLayer );

// always set duplicated layers to not visible so layer can be configured before being turned on
nodeDupLayer->setVisible( Qt::Unchecked );
nodeDupLayer->setItemVisibilityChecked( false );

// duplicate the layer style
QString errMsg;
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -80,6 +80,10 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()

menu->addAction( actions->actionMutuallyExclusiveGroup( menu ) );

menu->addAction( actions->actionCheckAndAllChildren( menu ) );

menu->addAction( actions->actionUncheckAndAllChildren( menu ) );

if ( mView->selectedNodes( true ).count() >= 2 )
menu->addAction( actions->actionGroupSelected( menu ) );

Expand Down Expand Up @@ -125,6 +129,8 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
if ( !layer->isInScaleRange( mCanvas->scale() ) )
menu->addAction( tr( "Zoom to &Visible Scale" ), QgisApp::instance(), SLOT( zoomToLayerScale() ) );

menu->addAction( actions->actionCheckAndAllParents( menu ) );

// set layer crs
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSetCRS.png" ) ), tr( "Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCrs() ) );

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -309,7 +309,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
// use visibility as selection
mLayersDependenciesTreeModel->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility );

mLayersDependenciesTreeGroup->setVisible( Qt::Unchecked );
mLayersDependenciesTreeGroup->setItemVisibilityChecked( false );

QSet<QString> dependencySources;
Q_FOREACH ( const QgsMapLayerDependency& dep, mLayer->dependencies() )
Expand All @@ -318,7 +318,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
}
Q_FOREACH ( QgsLayerTreeLayer* layer, mLayersDependenciesTreeGroup->findLayers() )
{
layer->setVisible( dependencySources.contains( layer->layerId() ) ? Qt::Checked : Qt::Unchecked );
layer->setItemVisibilityChecked( dependencySources.contains( layer->layerId() ) );
}

mLayersDependenciesTreeView->setModel( mLayersDependenciesTreeModel.data() );
Expand Down

0 comments on commit 60a1701

Please sign in to comment.