Skip to content
Permalink
Browse files
Layer tree view interface for context menu configuration
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 6a5b752 commit 69da2b3db0cef0c653d4eb77b7d65844fc9957a4
@@ -2173,6 +2173,64 @@ QgsMessageBar* QgisApp::messageBar()
return mInfoBar;
}




// ===========
// TODO: move to a separate file
#include "qgslayertreenode.h"
#include "qgslayertreeviewdefaultactions.h"

class QgsAppLayerTreeViewMenuProvider : public QgsLayerTreeViewMenuProvider
{
public:
QgsAppLayerTreeViewMenuProvider(QgsLayerTreeView* view, QgsMapCanvas* canvas) : mView(view), mCanvas(canvas) {}

QMenu* createContextMenu();

protected:
QgsLayerTreeView* mView;
QgsMapCanvas* mCanvas;
};

QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
{
QMenu* menu = new QMenu;

QgsLayerTreeViewDefaultActions* actions = mView->defaultActions();

QModelIndex idx = mView->currentIndex();
if (!idx.isValid())
{
// global menu
menu->addAction( actions->actionAddGroup(menu) );
}
else if (QgsLayerTreeNode* node = mView->layerTreeModel()->index2node(idx))
{
// layer or group selected
if (node->nodeType() == QgsLayerTreeNode::NodeGroup)
{
menu->addAction( actions->actionZoomToGroup(mCanvas, menu) );
menu->addAction( actions->actionAddGroup(menu) );
}
else if (node->nodeType() == QgsLayerTreeNode::NodeLayer)
{
menu->addAction( actions->actionZoomToLayer(mCanvas, menu) );
menu->addAction( actions->actionShowInOverview(menu) );
}

menu->addAction( actions->actionRemoveGroupOrLayer(menu) );
menu->addAction( actions->actionRenameGroupOrLayer(menu) );
}
else
{
// symbology item?
}

return menu;
}


void QgisApp::initLayerTreeView()
{
mLayerTreeDock = new QDockWidget( tr( "Layers NEW" ), this );
@@ -2184,11 +2242,13 @@ void QgisApp::initLayerTreeView()

mLayerTreeView = new QgsLayerTreeView( this );
mLayerTreeView->setModel( model );
mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider(mLayerTreeView, mMapCanvas) );

mLayerTreeDock->setWidget( mLayerTreeView );
addDockWidget( Qt::LeftDockWidgetArea, mLayerTreeDock );
}


void QgisApp::initLegend()
{
mMapLegend->setWhatsThis( tr( "Map legend that displays all the layers currently on the map canvas. Click on the check box to turn a layer on or off. Double click on a layer in the legend to customize its appearance and set other properties." ) );
@@ -11,6 +11,7 @@ QgsLayerTreeView::QgsLayerTreeView(QWidget *parent)
: QTreeView(parent)
, mCurrentLayer(0)
, mDefaultActions(0)
, mMenuProvider(0)
{
setHeaderHidden(true);

@@ -24,6 +25,11 @@ QgsLayerTreeView::QgsLayerTreeView(QWidget *parent)
connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(updateExpandedStateToNode(QModelIndex)));
}

QgsLayerTreeView::~QgsLayerTreeView()
{
delete mMenuProvider;
}

void QgsLayerTreeView::setModel(QAbstractItemModel* model)
{
if (!qobject_cast<QgsLayerTreeModel*>(model))
@@ -50,6 +56,12 @@ QgsLayerTreeViewDefaultActions* QgsLayerTreeView::defaultActions()
return mDefaultActions;
}

void QgsLayerTreeView::setMenuProvider(QgsLayerTreeViewMenuProvider* menuProvider)
{
delete mMenuProvider;
mMenuProvider = menuProvider;
}

QgsMapLayer* QgsLayerTreeView::currentLayer() const
{
return mCurrentLayer;
@@ -67,34 +79,17 @@ void QgsLayerTreeView::setCurrentLayer(QgsMapLayer* layer)

void QgsLayerTreeView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu;
if (!mMenuProvider)
return;

QModelIndex idx = indexAt(event->pos());
if (!idx.isValid())
{
setCurrentIndex(QModelIndex());

menu.addAction( defaultActions()->actionAddGroup(&menu) );
}
else
{
QgsLayerTreeNode* node = layerTreeModel()->index2node(idx);
if (!node)
return; // probably a symbology item

if (node->nodeType() == QgsLayerTreeNode::NodeGroup)
menu.addAction( defaultActions()->actionAddGroup(&menu) );
else if (node->nodeType() == QgsLayerTreeNode::NodeLayer)
{
// TODO menu.addAction( defaultActions()->actionZoomToLayer(canvas, &menu) );
menu.addAction( defaultActions()->actionShowInOverview(&menu) ); // TODO: should be custom action
}

menu.addAction( defaultActions()->actionRemoveGroupOrLayer(&menu) );
menu.addAction( defaultActions()->actionRenameGroupOrLayer(&menu) );
}

menu.exec(mapToGlobal(event->pos()));
QMenu* menu = mMenuProvider->createContextMenu();
if (menu)
menu->exec(mapToGlobal(event->pos()));
delete menu;
}


@@ -7,20 +7,25 @@ class QgsLayerTreeGroup;
class QgsLayerTreeModel;
class QgsLayerTreeNode;
class QgsLayerTreeViewDefaultActions;
class QgsLayerTreeViewMenuProvider;
class QgsMapLayer;

class GUI_EXPORT QgsLayerTreeView : public QTreeView
{
Q_OBJECT
public:
explicit QgsLayerTreeView(QWidget *parent = 0);
~QgsLayerTreeView();

virtual void setModel(QAbstractItemModel* model);

QgsLayerTreeModel* layerTreeModel() const;

QgsLayerTreeViewDefaultActions* defaultActions();

void setMenuProvider(QgsLayerTreeViewMenuProvider* menuProvider); // takes ownership
QgsLayerTreeViewMenuProvider* menuProvider() const { return mMenuProvider; }

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

@@ -51,6 +56,16 @@ protected slots:
QgsMapLayer* mCurrentLayer;

QgsLayerTreeViewDefaultActions* mDefaultActions;
QgsLayerTreeViewMenuProvider* mMenuProvider;
};

/** interface to allow custom menus */
class GUI_EXPORT QgsLayerTreeViewMenuProvider
{
public:
virtual ~QgsLayerTreeViewMenuProvider() {}

virtual QMenu* createContextMenu() = 0;
};


@@ -5,6 +5,7 @@
#include "qgslayertreenode.h"
#include "qgslayertreeview.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"

#include <QAction>
@@ -24,7 +25,7 @@ QAction* QgsLayerTreeViewDefaultActions::actionAddGroup(QObject* parent)

QAction* QgsLayerTreeViewDefaultActions::actionRemoveGroupOrLayer(QObject* parent)
{
QAction* a = new QAction(tr("Remove"), parent);
QAction* a = new QAction(QgsApplication::getThemeIcon( "/mActionRemoveLayer.svg" ), tr("&Remove"), parent);
connect(a, SIGNAL(triggered()), this, SLOT(removeGroupOrLayer()));
return a;
}
@@ -35,7 +36,7 @@ QAction* QgsLayerTreeViewDefaultActions::actionShowInOverview(QObject* parent)
if (!node)
return 0;

QAction* a = new QAction(tr("Show in overview"), parent);
QAction* a = new QAction(tr("&Show in overview"), parent);
connect(a, SIGNAL(triggered()), this, SLOT(showInOverview()));
a->setCheckable(true);
a->setChecked(node->customProperty("overview", 0).toInt());
@@ -52,12 +53,21 @@ QAction* QgsLayerTreeViewDefaultActions::actionRenameGroupOrLayer(QObject* paren
QAction* QgsLayerTreeViewDefaultActions::actionZoomToLayer(QgsMapCanvas* canvas, QObject* parent)
{
QAction* a = new QAction(QgsApplication::getThemeIcon( "/mActionZoomToLayer.svg" ),
tr("&Zoom to Layer Extent"), parent);
tr("&Zoom to Layer"), parent);
a->setData(QVariant::fromValue(reinterpret_cast<void*>(canvas)));
connect(a, SIGNAL(triggered()), this, SLOT(zoomToLayer()));
return a;
}

QAction* QgsLayerTreeViewDefaultActions::actionZoomToGroup(QgsMapCanvas* canvas, QObject* parent)
{
QAction* a = new QAction(QgsApplication::getThemeIcon( "/mActionZoomToLayer.svg" ),
tr("&Zoom to Group"), parent);
a->setData(QVariant::fromValue(reinterpret_cast<void*>(canvas)));
connect(a, SIGNAL(triggered()), this, SLOT(zoomToGroup()));
return a;
}

void QgsLayerTreeViewDefaultActions::addGroup()
{
QgsLayerTreeGroup* group = mView->currentGroupNode();
@@ -82,10 +92,6 @@ void QgsLayerTreeViewDefaultActions::removeGroupOrLayer()

void QgsLayerTreeViewDefaultActions::renameGroupOrLayer()
{
/*QgsLayerTreeNode* node = mView->currentNode();
if (!node)
return;*/

mView->edit(mView->currentIndex());
}

@@ -112,6 +118,18 @@ void QgsLayerTreeViewDefaultActions::zoomToLayer()
zoomToLayers(canvas, layers);
}

void QgsLayerTreeViewDefaultActions::zoomToGroup()
{
QAction* s = qobject_cast<QAction*>(sender());
QgsMapCanvas* canvas = reinterpret_cast<QgsMapCanvas*>(s->data().value<void*>());

QList<QgsMapLayer*> layers;
foreach (QString layerId, mView->currentGroupNode()->childLayerIds())
layers << QgsMapLayerRegistry::instance()->mapLayer(layerId);

zoomToLayers(canvas, layers);
}


void QgsLayerTreeViewDefaultActions::zoomToLayers(QgsMapCanvas* canvas, const QList<QgsMapLayer*>& layers)
{
@@ -148,4 +166,5 @@ void QgsLayerTreeViewDefaultActions::zoomToLayers(QgsMapCanvas* canvas, const QL

//zoom to bounding box
canvas->setExtent( extent );
canvas->refresh();
}
@@ -21,13 +21,16 @@ class GUI_EXPORT QgsLayerTreeViewDefaultActions : public QObject
QAction* actionRenameGroupOrLayer(QObject* parent = 0);

QAction* actionZoomToLayer(QgsMapCanvas* canvas, QObject* parent = 0);
QAction* actionZoomToGroup(QgsMapCanvas* canvas, QObject* parent = 0);
// TODO: zoom to selected

protected slots:
void addGroup();
void removeGroupOrLayer();
void renameGroupOrLayer();
void showInOverview();
void zoomToLayer();
void zoomToGroup();

protected:
void zoomToLayers(QgsMapCanvas* canvas, const QList<QgsMapLayer*>& layers);

0 comments on commit 69da2b3

Please sign in to comment.