Skip to content
Permalink
Browse files
Handle active (current) layer in layer tree view
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 1906de1 commit a6158d55ffda0ff1ef2c82b94587a597164b0f70
Showing with 40 additions and 2 deletions.
  1. +31 −2 src/gui/layertree/qgslayertreeview.cpp
  2. +9 −0 src/gui/layertree/qgslayertreeview.h
@@ -6,8 +6,9 @@
#include <QMenu>
#include <QContextMenuEvent>

QgsLayerTreeView::QgsLayerTreeView(QWidget *parent) :
QTreeView(parent)
QgsLayerTreeView::QgsLayerTreeView(QWidget *parent)
: QTreeView(parent)
, mCurrentLayer(0)
{
setHeaderHidden(true);

@@ -19,6 +20,7 @@ QgsLayerTreeView::QgsLayerTreeView(QWidget *parent) :

connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(updateExpandedStateToNode(QModelIndex)));
connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(updateExpandedStateToNode(QModelIndex)));
connect(this, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(onCurrentChanged(QModelIndex)));
}

void QgsLayerTreeView::setModel(QAbstractItemModel* model)
@@ -38,6 +40,20 @@ QgsLayerTreeModel *QgsLayerTreeView::layerTreeModel()
return qobject_cast<QgsLayerTreeModel*>(model());
}

QgsMapLayer* QgsLayerTreeView::currentLayer() const
{
return mCurrentLayer;
}

void QgsLayerTreeView::setCurrentLayer(QgsMapLayer* layer)
{
if (mCurrentLayer == layer)
return;

mCurrentLayer = layer;
emit currentLayerChanged(mCurrentLayer);
}


void QgsLayerTreeView::contextMenuEvent(QContextMenuEvent *event)
{
@@ -144,6 +160,19 @@ void QgsLayerTreeView::updateExpandedStateToNode(QModelIndex index)
node->setExpanded(isExpanded(index));
}

void QgsLayerTreeView::onCurrentChanged(QModelIndex current)
{
QgsLayerTreeNode* node = layerTreeModel()->index2node(current);
if (!node)
return; // TODO: maybe also support symbology nodes

QgsMapLayer* layer = 0;
if (node->nodeType() == QgsLayerTreeNode::NodeLayer)
layer = static_cast<QgsLayerTreeLayer*>(node)->layer();

setCurrentLayer(layer);
}

void QgsLayerTreeView::updateExpandedStateFromNode(QgsLayerTreeNode* node)
{
QModelIndex idx = layerTreeModel()->node2index(node);
@@ -5,6 +5,7 @@

class QgsLayerTreeModel;
class QgsLayerTreeNode;
class QgsMapLayer;

class GUI_EXPORT QgsLayerTreeView : public QTreeView
{
@@ -16,6 +17,9 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView

QgsLayerTreeModel* layerTreeModel();

QgsMapLayer* currentLayer() const;
void setCurrentLayer(QgsMapLayer* layer);

protected:
void contextMenuEvent(QContextMenuEvent* event);

@@ -26,6 +30,7 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
void updateExpandedStateFromNode(QgsLayerTreeNode* node);

signals:
void currentLayerChanged(QgsMapLayer* layer);

public slots:

@@ -38,6 +43,10 @@ protected slots:

void updateExpandedStateToNode(QModelIndex index);

void onCurrentChanged(QModelIndex current);

protected:
QgsMapLayer* mCurrentLayer;
};

#endif // QGSLAYERTREEVIEW_H

0 comments on commit a6158d5

Please sign in to comment.