Skip to content

Commit c9f9ea8

Browse files
committed
Added layerTreeView() to QgisInterface, moved context menu provider to new file
1 parent 72b3ef7 commit c9f9ea8

10 files changed

+222
-176
lines changed

python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ INCLUDE_DIRECTORIES(
8686
../src/gui/attributetable
8787
../src/gui/editorwidgets
8888
../src/gui/editorwidgets/core
89+
../src/gui/layertree
8990

9091
${CMAKE_BINARY_DIR} # qgsconfig.h, qgsversion.h
9192
)

python/gui/gui.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,5 @@
146146
%Include editorwidgets/core/qgseditorwidgetfactory.sip
147147
%Include editorwidgets/core/qgseditorwidgetregistry.sip
148148
%Include editorwidgets/core/qgseditorwidgetwrapper.sip
149+
150+
%Include layertree/qgslayertreeview.sip

python/gui/qgisinterface.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class QgisInterface : QObject
3232

3333
virtual QgsPluginManagerInterface* pluginManagerInterface() = 0;
3434

35+
virtual QgsLayerTreeView* layerTreeView() = 0;
36+
3537
public slots: // TODO: do these functions really need to be slots?
3638

3739
/* Exposed functions */

src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ SET(QGIS_APP_SRCS
33
qgisappinterface.cpp
44
qgisappstylesheet.cpp
55
qgsabout.cpp
6+
qgsapplayertreeviewmenuprovider.cpp
67
qgssponsors.cpp
78
qgsaddattrdialog.cpp
89
qgsaddtaborgroup.cpp

src/app/qgisapp.cpp

Lines changed: 4 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
#include "qgis.h"
9999
#include "qgisplugin.h"
100100
#include "qgsabout.h"
101+
#include "qgsapplayertreeviewmenuprovider.h"
101102
#include "qgsapplication.h"
102103
#include "qgsattributeaction.h"
103104
#include "qgsattributetabledialog.h"
@@ -135,8 +136,10 @@
135136
#include "qgsguivectorlayertools.h"
136137
#include "qgslabelinggui.h"
137138
#include "qgslayertreemodel.h"
139+
#include "qgslayertreenode.h"
138140
#include "qgslayertreeutils.h"
139141
#include "qgslayertreeview.h"
142+
#include "qgslayertreeviewdefaultactions.h"
140143
#include "qgslegend.h"
141144
#include "qgslegendgroup.h"
142145
#include "qgslayerorder.h"
@@ -816,6 +819,7 @@ QgisApp::QgisApp( )
816819
mInternalClipboard = new QgsClipboard;
817820
mMapCanvas = new QgsMapCanvas();
818821
mMapCanvas->freeze();
822+
mLayerTreeView = new QgsLayerTreeView( this );
819823
mMapLegend = new QgsLegend( mMapCanvas );
820824
mUndoWidget = new QgsUndoWidget( NULL, mMapCanvas );
821825
mInfoBar = new QgsMessageBar( centralWidget() );
@@ -2208,177 +2212,6 @@ QgsMessageBar* QgisApp::messageBar()
22082212
}
22092213

22102214

2211-
2212-
2213-
// ===========
2214-
// TODO: move to a separate file
2215-
#include "qgslayertreenode.h"
2216-
#include "qgslayertreeviewdefaultactions.h"
2217-
2218-
class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewMenuProvider
2219-
{
2220-
public:
2221-
QgsAppLayerTreeViewMenuProvider(QgsLayerTreeView* view, QgsMapCanvas* canvas) : mView(view), mCanvas(canvas) {}
2222-
2223-
QMenu* createContextMenu();
2224-
2225-
protected:
2226-
QgsLayerTreeView* mView;
2227-
QgsMapCanvas* mCanvas;
2228-
};
2229-
2230-
QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
2231-
{
2232-
QMenu* menu = new QMenu;
2233-
2234-
QgsLayerTreeViewDefaultActions* actions = mView->defaultActions();
2235-
2236-
QModelIndex idx = mView->currentIndex();
2237-
if (!idx.isValid())
2238-
{
2239-
// global menu
2240-
menu->addAction( actions->actionAddGroup(menu) );
2241-
2242-
// TODO: expand all, collapse all
2243-
// TODO: update drawing order
2244-
}
2245-
else if (QgsLayerTreeNode* node = mView->layerTreeModel()->index2node(idx))
2246-
{
2247-
// layer or group selected
2248-
if (node->nodeType() == QgsLayerTreeNode::NodeGroup)
2249-
{
2250-
menu->addAction( actions->actionZoomToGroup(mCanvas, menu) );
2251-
menu->addAction( actions->actionRemoveGroupOrLayer(menu) );
2252-
2253-
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ),
2254-
tr( "&Set Group CRS" ), QgisApp::instance(), SLOT( legendGroupSetCRS() ) );
2255-
2256-
menu->addAction( actions->actionRenameGroupOrLayer(menu) );
2257-
2258-
if (mView->selectedNodes(true).count() >= 2)
2259-
menu->addAction( actions->actionGroupSelected(menu) );
2260-
2261-
menu->addAction( actions->actionAddGroup(menu) );
2262-
}
2263-
else if (node->nodeType() == QgsLayerTreeNode::NodeLayer)
2264-
{
2265-
QgsMapLayer* layer = static_cast<QgsLayerTreeLayer*>(node)->layer();
2266-
2267-
menu->addAction( actions->actionZoomToLayer(mCanvas, menu) );
2268-
menu->addAction( actions->actionShowInOverview(menu) );
2269-
2270-
if ( layer->type() == QgsMapLayer::RasterLayer )
2271-
{
2272-
menu->addAction( tr( "&Zoom to Best Scale (100%)" ), QgisApp::instance(), SLOT( legendLayerZoomNative() ) );
2273-
2274-
QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( layer );
2275-
if ( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette )
2276-
menu->addAction( tr( "&Stretch Using Current Extent" ), QgisApp::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
2277-
}
2278-
2279-
menu->addAction( actions->actionRemoveGroupOrLayer(menu) );
2280-
2281-
// duplicate layer
2282-
QAction* duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( "/mActionDuplicateLayer.svg" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );
2283-
2284-
// set layer crs
2285-
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) );
2286-
2287-
// assign layer crs to project
2288-
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetProjectCRS.png" ), tr( "Set &Project CRS from Layer" ), QgisApp::instance(), SLOT( setProjectCRSFromLayer() ) );
2289-
2290-
menu->addSeparator();
2291-
2292-
if ( layer->type() == QgsMapLayer::VectorLayer )
2293-
{
2294-
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( layer );
2295-
2296-
QAction *toggleEditingAction = QgisApp::instance()->actionToggleEditing();
2297-
QAction *saveLayerEditsAction = QgisApp::instance()->actionSaveActiveLayerEdits();
2298-
QAction *allEditsAction = QgisApp::instance()->actionAllEdits();
2299-
2300-
// attribute table
2301-
menu->addAction( QgsApplication::getThemeIcon( "/mActionOpenTable.png" ), tr( "&Open Attribute Table" ),
2302-
QgisApp::instance(), SLOT( attributeTable() ) );
2303-
2304-
// allow editing
2305-
int cap = vlayer->dataProvider()->capabilities();
2306-
if ( cap & QgsVectorDataProvider::EditingCapabilities )
2307-
{
2308-
if ( toggleEditingAction )
2309-
{
2310-
menu->addAction( toggleEditingAction );
2311-
toggleEditingAction->setChecked( vlayer->isEditable() );
2312-
}
2313-
if ( saveLayerEditsAction && vlayer->isModified() )
2314-
{
2315-
menu->addAction( saveLayerEditsAction );
2316-
}
2317-
}
2318-
2319-
if ( allEditsAction->isEnabled() )
2320-
menu->addAction( allEditsAction );
2321-
2322-
// disable duplication of memory layers
2323-
if ( vlayer->storageType() == "Memory storage" && mView->selectedLayerNodes().count() == 1 )
2324-
duplicateLayersAction->setEnabled( false );
2325-
2326-
// save as vector file
2327-
menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsFile() ) );
2328-
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
2329-
2330-
if ( !vlayer->isEditable() && vlayer->dataProvider()->supportsSubsetString() && vlayer->vectorJoins().isEmpty() )
2331-
menu->addAction( tr( "&Filter..." ), QgisApp::instance(), SLOT( layerSubsetString() ) );
2332-
2333-
menu->addAction( actions->actionShowFeatureCount(menu) );
2334-
2335-
menu->addSeparator();
2336-
}
2337-
else if ( layer->type() == QgsMapLayer::RasterLayer )
2338-
{
2339-
menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsRasterFile() ) );
2340-
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
2341-
}
2342-
else if ( layer->type() == QgsMapLayer::PluginLayer && mView->selectedLayerNodes().count() == 1 )
2343-
{
2344-
// disable duplication of plugin layers
2345-
duplicateLayersAction->setEnabled( false );
2346-
}
2347-
2348-
// TODO: custom actions
2349-
2350-
if (layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
2351-
menu->addAction( tr( "&Properties" ), QgisApp::instance(), SLOT( layerProperties() ) );
2352-
2353-
if (node->parent() != mView->layerTreeModel()->rootGroup())
2354-
menu->addAction( actions->actionMakeTopLevel(menu) );
2355-
2356-
menu->addAction( actions->actionRenameGroupOrLayer(menu) );
2357-
2358-
if (mView->selectedNodes(true).count() >= 2)
2359-
menu->addAction( actions->actionGroupSelected(menu) );
2360-
2361-
if ( mView->selectedLayerNodes().count() == 1 )
2362-
{
2363-
QgisApp* app = QgisApp::instance();
2364-
menu->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
2365-
if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
2366-
{
2367-
menu->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
2368-
}
2369-
}
2370-
}
2371-
2372-
}
2373-
else
2374-
{
2375-
// symbology item?
2376-
}
2377-
2378-
return menu;
2379-
}
2380-
2381-
23822215
void QgisApp::initLayerTreeView()
23832216
{
23842217
mLayerTreeDock = new QDockWidget( tr( "Layers NEW" ), this );

src/app/qgisappinterface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ QgsPluginManagerInterface* QgisAppInterface::pluginManagerInterface()
8181
return &pluginManagerIface;
8282
}
8383

84+
QgsLayerTreeView*QgisAppInterface::layerTreeView()
85+
{
86+
return qgis->layerTreeView();
87+
}
88+
8489
void QgisAppInterface::zoomFull()
8590
{
8691
qgis->zoomFull();

src/app/qgisappinterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class APP_EXPORT QgisAppInterface : public QgisInterface
4848

4949
QgsPluginManagerInterface* pluginManagerInterface();
5050

51+
QgsLayerTreeView* layerTreeView();
52+
5153
/* Exposed functions */
5254

5355
//! Zoom map to full extent

0 commit comments

Comments
 (0)