29#include "moc_qgslayertreeviewdefaultactions.cpp"
64 QAction *a =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionInOverview.svg" ) ), tr(
"Show in &Overview" ), parent );
66 a->setCheckable(
true );
67 a->setChecked( node->
customProperty( QStringLiteral(
"overview" ), 0 ).toInt() );
79 text = tr(
"Re&name Group" );
81 text = tr(
"Re&name Layer" );
83 QAction *a =
new QAction( text, parent );
94 QAction *a =
new QAction( tr(
"Show Feature &Count" ), parent );
96 a->setCheckable(
true );
97 a->setChecked( node->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt() );
103 QAction *a =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionZoomToLayer.svg" ) ), tr(
"&Zoom to Layer" ), parent );
104 a->setData( QVariant::fromValue(
reinterpret_cast<void *
>( canvas ) ) );
113 QAction *a =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionZoomToLayer.svg" ) ), tr(
"&Zoom to Layer(s)" ), parent );
114 a->setData( QVariant::fromValue( canvas ) );
121 QAction *a =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionZoomToSelected.svg" ) ), tr(
"Zoom to &Selection" ), parent );
122 a->setData( QVariant::fromValue(
reinterpret_cast<void *
>( canvas ) ) );
129 QAction *a =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionZoomToLayer.svg" ) ), tr(
"&Zoom to Group" ), parent );
130 a->setData( QVariant::fromValue(
reinterpret_cast<void *
>( canvas ) ) );
137 QAction *a =
new QAction( tr(
"&Move to Top-level" ), parent );
146 QAction *a =
new QAction( tr(
"Move O&ut of Group" ), parent );
153 QAction *a =
new QAction( tr(
"Move to &Top" ), parent );
160 QAction *a =
new QAction( tr(
"Move to &Bottom" ), parent );
167 QAction *a =
new QAction( tr(
"&Group Selected" ), parent );
178 QAction *a =
new QAction( tr(
"&Mutually Exclusive Group" ), parent );
179 a->setCheckable(
true );
191 QAction *a =
new QAction( tr(
"Check and All its Children (⌘-click)" ), parent );
193 QAction *a =
new QAction( tr(
"Check and All its Children (Ctrl-click)" ), parent );
195 connect( a, &QAction::triggered,
this, &QgsLayerTreeViewDefaultActions::checkAndAllChildren );
205 QAction *a =
new QAction( tr(
"Uncheck and All its Children (⌘-click)" ), parent );
207 QAction *a =
new QAction( tr(
"Uncheck and All its Children (Ctrl-click)" ), parent );
209 connect( a, &QAction::triggered,
this, &QgsLayerTreeViewDefaultActions::uncheckAndAllChildren );
218 QAction *a =
new QAction( tr(
"Chec&k and All its Parents" ), parent );
219 connect( a, &QAction::triggered,
this, &QgsLayerTreeViewDefaultActions::checkAndAllParents );
223void QgsLayerTreeViewDefaultActions::checkAndAllChildren()
231void QgsLayerTreeViewDefaultActions::uncheckAndAllChildren()
233 QgsLayerTreeNode *node =
mView->currentNode();
239void QgsLayerTreeViewDefaultActions::checkAndAllParents()
241 QgsLayerTreeNode *node =
mView->currentNode();
249 int nodeCount =
mView->selectedNodes(
true ).count();
250 if ( nodeCount > 1 || ( nodeCount == 1 &&
mView->currentLayer() ) )
257 group =
mView->layerTreeModel()->rootGroup();
265 const auto constSelectedNodes =
mView->selectedNodes(
true );
269 qobject_cast<QgsLayerTreeGroup *>( node->
parent() )->removeChildNode( node );
278 if ( !group || group ==
mView->layerTreeModel()->rootGroup() )
282 if ( !newParentGroup )
285 const int existingNodePosition = newParentGroup->
children().indexOf( group );
287 const QList< QgsLayerTreeNode * > childrenToPromote = group->
children();
288 if ( !childrenToPromote.isEmpty() )
290 for (
auto it = childrenToPromote.rbegin(); it != childrenToPromote.rend(); ++it )
292 newParentGroup->
insertChildNode( existingNodePosition, ( *it )->clone() );
310 int newValue = node->
customProperty( QStringLiteral(
"overview" ), 0 ).toInt();
311 const auto constSelectedLayerNodes =
mView->selectedLayerNodes();
313 l->setCustomProperty( QStringLiteral(
"overview" ), newValue ? 0 : 1 );
322 int newValue = node->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt();
323 const auto constSelectedLayerNodes =
mView->selectedLayerNodes();
325 l->setCustomProperty( QStringLiteral(
"showFeatureCount" ), newValue ? 0 : 1 );
334 const QList<QgsMapLayer *> layers { layer };
340 const QList<QgsMapLayer *> layers =
mView->selectedLayers();
349 const QList<QgsMapLayer *> layers =
mView->selectedLayers();
351 if ( layers.size() > 1 )
353 else if ( layers.size() <= 1 && layer )
363 QList<QgsMapLayer *> layers;
364 const QStringList findLayerIds = groupNode->
findLayerIds();
365 for (
const QString &layerId : findLayerIds )
374 QAction *s = qobject_cast<QAction *>( sender() );
383 QAction *s = qobject_cast<QAction *>( sender() );
390 QAction *s = qobject_cast<QAction *>( sender() );
397 QAction *s = qobject_cast<QAction *>( sender() );
409 if ( !layers.empty() )
411 for (
int i = 0; i < layers.size(); ++i )
425 layerExtent = vLayer->
extent();
429 if ( layerExtent.
isNull() )
440 else if (
mView->currentLayer() )
451 extent.
scale( 1.05 );
461 QString prefix = parentGroup ==
mView->layerTreeModel()->rootGroup() ?
"group" :
"sub-group";
462 QString newName = prefix +
'1';
463 for (
int i = 2; parentGroup->
findGroup( newName ); ++i )
464 newName = prefix + QString::number( i );
471 const auto constSelectedLayerNodes =
mView->selectedLayerNodes();
475 QgsLayerTreeGroup *parentGroup = qobject_cast<QgsLayerTreeGroup *>( l->parent() );
476 if ( !parentGroup || parentGroup == rootGroup )
487 const QList<QgsLayerTreeLayer *> selectedLayerNodes =
mView->selectedLayerNodes();
491 QgsLayerTreeGroup *parentGroup = qobject_cast<QgsLayerTreeGroup *>( l->parent() );
492 if ( !parentGroup || parentGroup == rootGroup )
495 while ( tempGroup->
parent() != rootGroup )
497 tempGroup = qobject_cast<QgsLayerTreeGroup *>( tempGroup->
parent() );
500 int insertIdx = rootGroup->
children().indexOf( tempGroup );
509 QList<QgsLayerTreeNode *> selectedNodes =
mView->selectedNodes();
510 std::reverse( selectedNodes.begin(), selectedNodes.end() );
513 return a->depth() > b->depth();
517 QgsLayerTreeGroup *parentGroup = qobject_cast<QgsLayerTreeGroup *>( n->parent() );
527 QList<QgsLayerTreeNode *> selectedNodes =
mView->selectedNodes();
530 return a->depth() > b->depth();
534 QgsLayerTreeGroup *parentGroup = qobject_cast<QgsLayerTreeGroup *>( n->parent() );
544 const QList<QgsLayerTreeNode *> nodes =
mView->selectedNodes(
true );
549 int insertIdx = parentGroup->
children().indexOf( nodes[0] );
564 mView->setCurrentIndex(
mView->node2index( newGroup ) );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Layer tree group node serves as a container for layers and further groups.
void insertChildNode(int index, QgsLayerTreeNode *node)
Insert existing node at specified position.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
void removeChildNode(QgsLayerTreeNode *node)
Remove a child node from this group.
QStringList findLayerIds() const
Find layer IDs used in all layer nodes.
void addChildNode(QgsLayerTreeNode *node)
Append an existing node.
void setIsMutuallyExclusive(bool enabled, int initialChildIndex=-1)
Set whether the group is mutually exclusive (only one child can be checked at a time).
QgsLayerTreeGroup * addGroup(const QString &name)
Append a new group node with given name.
Layer tree node points to a map layer.
QgsLayerTreeLayer * clone() const override
Create a copy of the node. Returns new instance.
Base class for nodes in a layer tree.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well).
bool isItemVisibilityUncheckedRecursive() const
Returns whether this node is unchecked and all its children.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
virtual void setItemVisibilityCheckedRecursive(bool checked)
Check or uncheck a node and all its children (taking into account exclusion rules).
virtual QgsLayerTreeNode * clone() const =0
Create a copy of the node. Returns new instance.
bool isItemVisibilityCheckedRecursive() const
Returns whether this node is checked and all its children.
void setItemVisibilityCheckedParentRecursive(bool checked)
Check or uncheck a node and all its parents.
Base class for QTreeView widgets which display a layer tree.
QgsLayerTreeNode * currentNode() const
Returns the current node.
QAction * actionRemoveGroupOrLayer(QObject *parent=nullptr)
Returns a new action which removes either a group or layer, depending on the selected node.
void removeGroupOrLayer()
void zoomToLayers()
Zooms to the combined extent of all the selected layer(s) in the layer tree.
void zoomToGroup(QgsMapCanvas *canvas)
Q_DECL_DEPRECATED void makeTopLevel()
void removeGroupPromoteLayers()
Removes the selected group node, promoting all child nodes up into the removed group's parent node.
void renameGroupOrLayer()
void zoomToLayers(QgsMapCanvas *canvas)
Zooms a map canvas to all the selected layer(s) in the layer tree.
QAction * actionZoomToLayers(QgsMapCanvas *canvas, QObject *parent=nullptr)
Action to zoom to all the selected layer(s) in the layer tree.
void moveToTop()
Moves selected layer(s) and/or group(s) to the top of the layer panel or the top of the group if the ...
QAction * actionMoveToTop(QObject *parent=nullptr)
QAction * actionShowInOverview(QObject *parent=nullptr)
Returns a new action for toggling whether a layer is shown in the map overview.
QAction * actionRenameGroupOrLayer(QObject *parent=nullptr)
Returns a new action for renaming a group or layer, depending on the selected node.
void zoomToSelection(QgsMapCanvas *canvas)
QAction * actionCheckAndAllChildren(QObject *parent=nullptr)
Action to check a group and all its children.
QAction * actionCheckAndAllParents(QObject *parent=nullptr)
Action to check a group and all its parents.
QgsLayerTreeViewBase * mView
void moveToBottom()
Moves selected layer(s) and/or group(s) to the bottom of the layer panel or the bottom of the group i...
QAction * actionRemoveGroupPromoteLayers(QObject *parent=nullptr)
Returns a new action which removes a group.
Q_DECL_DEPRECATED QAction * actionZoomToLayer(QgsMapCanvas *canvas, QObject *parent=nullptr)
Action to zoom to the active layer from the layer tree.
QAction * actionAddGroup(QObject *parent=nullptr)
Returns a new action which adds a group.
QAction * actionGroupSelected(QObject *parent=nullptr)
QgsLayerTreeViewDefaultActions(QgsLayerTreeViewBase *view)
Constructor for QgsLayerTreeViewDefaultActions, creating actions for a view.
QAction * actionMoveToBottom(QObject *parent=nullptr)
QAction * actionShowFeatureCount(QObject *parent=nullptr)
Returns a new action for toggling whether the feature count is shown for a layer.
Q_DECL_DEPRECATED void zoomToLayer(QgsMapCanvas *canvas)
Zooms a map canvas to the extent of the active layer in the layer tree.
QAction * actionMutuallyExclusiveGroup(QObject *parent=nullptr)
Action to enable/disable mutually exclusive flag of a group (only one child node may be checked).
Q_DECL_DEPRECATED QAction * actionMakeTopLevel(QObject *parent=nullptr)
QString uniqueGroupName(QgsLayerTreeGroup *parentGroup)
QAction * actionUncheckAndAllChildren(QObject *parent=nullptr)
Action to uncheck a group and all its children.
void mutuallyExclusiveGroup()
Slot to enable/disable mutually exclusive group flag.
void zoomToSelection()
Zooms to the bounding box of all selected features of a vector layer.
void moveOutOfGroup()
Moves selected layer(s) out of the group(s) and places this/these above the group(s).
QAction * actionZoomToSelection(QgsMapCanvas *canvas, QObject *parent=nullptr)
Action to zoom to selected features of a vector layer.
QAction * actionMoveOutOfGroup(QObject *parent=nullptr)
QAction * actionZoomToGroup(QgsMapCanvas *canvas, QObject *parent=nullptr)
Q_DECL_DEPRECATED void zoomToLayer()
Zooms to the extent of the active layer in the layer tree.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Map canvas is a class for displaying all GIS data types on a canvas.
void zoomToSelected(QgsMapLayer *layer=nullptr)
Zoom to the extent of the selected features of provided map layer.
void setExtent(const QgsRectangle &r, bool magnified=false)
Sets the extent of the map canvas to the specified rectangle.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
void refresh()
Repaints the canvas map.
Base class for all map layer types.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
A rectangle specified with double values.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void setNull()
Mark a rectangle as being null (holding no spatial information).
Temporarily sets a cursor override for the QApplication for the lifetime of the object.
Represents a vector layer which manages a vector based dataset.
QgsRectangle extent() const final
Returns the extent of the layer.
virtual void updateExtents(bool force=false)
Update the extents for the layer.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH