Skip to content

Commit

Permalink
Handle active (current) layer in layer tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 1906de1 commit a6158d5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/gui/layertree/qgslayertreeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include <QMenu>
#include <QContextMenuEvent>

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

Expand All @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/gui/layertree/qgslayertreeview.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class QgsLayerTreeModel;
class QgsLayerTreeNode;
class QgsMapLayer;

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

QgsLayerTreeModel* layerTreeModel();

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

protected:
void contextMenuEvent(QContextMenuEvent* event);

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

signals:
void currentLayerChanged(QgsMapLayer* layer);

public slots:

Expand All @@ -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.