Skip to content
Permalink
Browse files
Added layerTreeView() to QgisInterface, moved context menu provider t…
…o new file
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 72b3ef7 commit c9f9ea89858089689c1ea8d710550b6cc5f42854
@@ -86,6 +86,7 @@ INCLUDE_DIRECTORIES(
../src/gui/attributetable
../src/gui/editorwidgets
../src/gui/editorwidgets/core
../src/gui/layertree

${CMAKE_BINARY_DIR} # qgsconfig.h, qgsversion.h
)
@@ -146,3 +146,5 @@
%Include editorwidgets/core/qgseditorwidgetfactory.sip
%Include editorwidgets/core/qgseditorwidgetregistry.sip
%Include editorwidgets/core/qgseditorwidgetwrapper.sip

%Include layertree/qgslayertreeview.sip
@@ -32,6 +32,8 @@ class QgisInterface : QObject

virtual QgsPluginManagerInterface* pluginManagerInterface() = 0;

virtual QgsLayerTreeView* layerTreeView() = 0;

public slots: // TODO: do these functions really need to be slots?

/* Exposed functions */
@@ -3,6 +3,7 @@ SET(QGIS_APP_SRCS
qgisappinterface.cpp
qgisappstylesheet.cpp
qgsabout.cpp
qgsapplayertreeviewmenuprovider.cpp
qgssponsors.cpp
qgsaddattrdialog.cpp
qgsaddtaborgroup.cpp
@@ -98,6 +98,7 @@
#include "qgis.h"
#include "qgisplugin.h"
#include "qgsabout.h"
#include "qgsapplayertreeviewmenuprovider.h"
#include "qgsapplication.h"
#include "qgsattributeaction.h"
#include "qgsattributetabledialog.h"
@@ -135,8 +136,10 @@
#include "qgsguivectorlayertools.h"
#include "qgslabelinggui.h"
#include "qgslayertreemodel.h"
#include "qgslayertreenode.h"
#include "qgslayertreeutils.h"
#include "qgslayertreeview.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgslegend.h"
#include "qgslegendgroup.h"
#include "qgslayerorder.h"
@@ -816,6 +819,7 @@ QgisApp::QgisApp( )
mInternalClipboard = new QgsClipboard;
mMapCanvas = new QgsMapCanvas();
mMapCanvas->freeze();
mLayerTreeView = new QgsLayerTreeView( this );
mMapLegend = new QgsLegend( mMapCanvas );
mUndoWidget = new QgsUndoWidget( NULL, mMapCanvas );
mInfoBar = new QgsMessageBar( centralWidget() );
@@ -2208,177 +2212,6 @@ QgsMessageBar* QgisApp::messageBar()
}




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

class QgsAppLayerTreeViewMenuProvider : public QObject, 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) );

// TODO: expand all, collapse all
// TODO: update drawing order
}
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->actionRemoveGroupOrLayer(menu) );

menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ),
tr( "&Set Group CRS" ), QgisApp::instance(), SLOT( legendGroupSetCRS() ) );

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

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

menu->addAction( actions->actionAddGroup(menu) );
}
else if (node->nodeType() == QgsLayerTreeNode::NodeLayer)
{
QgsMapLayer* layer = static_cast<QgsLayerTreeLayer*>(node)->layer();

menu->addAction( actions->actionZoomToLayer(mCanvas, menu) );
menu->addAction( actions->actionShowInOverview(menu) );

if ( layer->type() == QgsMapLayer::RasterLayer )
{
menu->addAction( tr( "&Zoom to Best Scale (100%)" ), QgisApp::instance(), SLOT( legendLayerZoomNative() ) );

QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( layer );
if ( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette )
menu->addAction( tr( "&Stretch Using Current Extent" ), QgisApp::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
}

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

// duplicate layer
QAction* duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( "/mActionDuplicateLayer.svg" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );

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

// assign layer crs to project
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetProjectCRS.png" ), tr( "Set &Project CRS from Layer" ), QgisApp::instance(), SLOT( setProjectCRSFromLayer() ) );

menu->addSeparator();

if ( layer->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( layer );

QAction *toggleEditingAction = QgisApp::instance()->actionToggleEditing();
QAction *saveLayerEditsAction = QgisApp::instance()->actionSaveActiveLayerEdits();
QAction *allEditsAction = QgisApp::instance()->actionAllEdits();

// attribute table
menu->addAction( QgsApplication::getThemeIcon( "/mActionOpenTable.png" ), tr( "&Open Attribute Table" ),
QgisApp::instance(), SLOT( attributeTable() ) );

// allow editing
int cap = vlayer->dataProvider()->capabilities();
if ( cap & QgsVectorDataProvider::EditingCapabilities )
{
if ( toggleEditingAction )
{
menu->addAction( toggleEditingAction );
toggleEditingAction->setChecked( vlayer->isEditable() );
}
if ( saveLayerEditsAction && vlayer->isModified() )
{
menu->addAction( saveLayerEditsAction );
}
}

if ( allEditsAction->isEnabled() )
menu->addAction( allEditsAction );

// disable duplication of memory layers
if ( vlayer->storageType() == "Memory storage" && mView->selectedLayerNodes().count() == 1 )
duplicateLayersAction->setEnabled( false );

// save as vector file
menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsFile() ) );
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );

if ( !vlayer->isEditable() && vlayer->dataProvider()->supportsSubsetString() && vlayer->vectorJoins().isEmpty() )
menu->addAction( tr( "&Filter..." ), QgisApp::instance(), SLOT( layerSubsetString() ) );

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

menu->addSeparator();
}
else if ( layer->type() == QgsMapLayer::RasterLayer )
{
menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsRasterFile() ) );
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
}
else if ( layer->type() == QgsMapLayer::PluginLayer && mView->selectedLayerNodes().count() == 1 )
{
// disable duplication of plugin layers
duplicateLayersAction->setEnabled( false );
}

// TODO: custom actions

if (layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
menu->addAction( tr( "&Properties" ), QgisApp::instance(), SLOT( layerProperties() ) );

if (node->parent() != mView->layerTreeModel()->rootGroup())
menu->addAction( actions->actionMakeTopLevel(menu) );

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

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

if ( mView->selectedLayerNodes().count() == 1 )
{
QgisApp* app = QgisApp::instance();
menu->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menu->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
}
}
}

}
else
{
// symbology item?
}

return menu;
}


void QgisApp::initLayerTreeView()
{
mLayerTreeDock = new QDockWidget( tr( "Layers NEW" ), this );
@@ -81,6 +81,11 @@ QgsPluginManagerInterface* QgisAppInterface::pluginManagerInterface()
return &pluginManagerIface;
}

QgsLayerTreeView*QgisAppInterface::layerTreeView()
{
return qgis->layerTreeView();
}

void QgisAppInterface::zoomFull()
{
qgis->zoomFull();
@@ -48,6 +48,8 @@ class APP_EXPORT QgisAppInterface : public QgisInterface

QgsPluginManagerInterface* pluginManagerInterface();

QgsLayerTreeView* layerTreeView();

/* Exposed functions */

//! Zoom map to full extent

0 comments on commit c9f9ea8

Please sign in to comment.