Skip to content

Commit cf4393e

Browse files
committedJun 3, 2014
[layertree] Add Python bindings - part two (gui)
1 parent 73358c3 commit cf4393e

File tree

6 files changed

+296
-2
lines changed

6 files changed

+296
-2
lines changed
 

‎python/gui/gui.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
%Include attributetable/qgsfeatureselectionmodel.sip
109109
%Include attributetable/qgsifeatureselectionmanager.sip
110110

111+
%Include layertree/qgscustomlayerorderwidget.sip
112+
%Include layertree/qgslayertreemapcanvasbridge.sip
113+
%Include layertree/qgslayertreemodel.sip
114+
%Include layertree/qgslayertreeview.sip
115+
%Include layertree/qgslayertreeviewdefaultactions.sip
116+
111117
%Include raster/qgsmultibandcolorrendererwidget.sip
112118
%Include raster/qgspalettedrendererwidget.sip
113119
%Include raster/qgsrasterhistogramwidget.sip
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* The QgsCustomLayerOrderWidget class provides a list box where the user can define
3+
* custom order for drawing of layers. It also features a checkbox for enabling
4+
* or disabling the custom order. Any changes made by the user are automatically
5+
* propagated to the assigned QgsLayerTreeMapCanvasBridge. Also, any updates
6+
* to the layer tree cause refresh of the list.
7+
*
8+
* @see QgsLayerTreeMapCanvasBridge
9+
* @note added in 2.4
10+
*/
11+
class QgsCustomLayerOrderWidget : QWidget
12+
{
13+
%TypeHeaderCode
14+
#include <qgscustomlayerorderwidget.h>
15+
%End
16+
17+
public:
18+
explicit QgsCustomLayerOrderWidget( QgsLayerTreeMapCanvasBridge* bridge, QWidget *parent /TransferThis/ = 0 );
19+
20+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* The QgsLayerTreeMapCanvasBridge class takes care of updates of layer set
3+
* for QgsMapCanvas from a layer tree. The class listens to the updates in the layer tree
4+
* and updates the list of layers for rendering whenever some layers are added, removed,
5+
* or their visibility changes.
6+
*
7+
* The update of layers is not done immediately - it is postponed, so a series of updates
8+
* to the layer tree will trigger just one update of canvas layers.
9+
*
10+
* Also allows the client to override the default order of layers. This is useful
11+
* in advanced cases where the grouping in layer tree should be independent from the actual
12+
* order in the canvas.
13+
*
14+
* @added in 2.4
15+
*/
16+
class QgsLayerTreeMapCanvasBridge : QObject
17+
{
18+
%TypeHeaderCode
19+
#include <qgslayertreemapcanvasbridge.h>
20+
%End
21+
22+
public:
23+
//! Constructor: does not take ownership of the layer tree nor canvas
24+
QgsLayerTreeMapCanvasBridge( QgsLayerTreeGroup* root, QgsMapCanvas* canvas, QObject* parent /TransferThis/ = 0 );
25+
26+
void clear();
27+
28+
QgsLayerTreeGroup* rootGroup() const;
29+
QgsMapCanvas* mapCanvas() const;
30+
31+
bool hasCustomLayerOrder() const;
32+
QStringList customLayerOrder() const;
33+
34+
QStringList defaultLayerOrder() const;
35+
36+
//! if enabled, will automatically set full canvas extent and destination CRS + map units
37+
//! when first layer(s) are added
38+
void setAutoSetupOnFirstLayer( bool enabled );
39+
bool autoSetupOnFirstLayer() const;
40+
41+
//! if enabled, will automatically turn on on-the-fly reprojection of layers if a layer
42+
//! with different source CRS is added
43+
void setAutoEnableCrsTransform( bool enabled );
44+
bool autoEnableCrsTransform() const;
45+
46+
public slots:
47+
void setHasCustomLayerOrder( bool override );
48+
void setCustomLayerOrder( const QStringList& order );
49+
50+
//! force update of canvas layers from the layer tree. Normally this should not be needed to be called.
51+
void setCanvasLayers();
52+
53+
void readProject( const QDomDocument& doc );
54+
void writeProject( QDomDocument& doc );
55+
56+
signals:
57+
void hasCustomLayerOrderChanged( bool );
58+
void customLayerOrderChanged( const QStringList& order );
59+
60+
};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* The QgsLayerTreeModel class is model implementation for Qt item views framework.
3+
* The model can be used in any QTreeView, it is however recommended to use it
4+
* with QgsLayerTreeView which brings additional functionality specific to layer tree handling.
5+
*
6+
* The model listens to the changes in the layer tree and signals the changes as appropriate,
7+
* so that any view that uses the model is updated accordingly.
8+
*
9+
* Behavior of the model can be customized with flags. For example, whether to show symbology or
10+
* whether to allow changes to the layer tree.
11+
*
12+
* @see QgsLayerTreeView
13+
* @note added in 2.4
14+
*/
15+
class QgsLayerTreeModel : QAbstractItemModel
16+
{
17+
%TypeHeaderCode
18+
#include <qgslayertreemodel.h>
19+
%End
20+
21+
public:
22+
//! Construct a new tree model with given layer tree (root node must not be null pointer).
23+
//! The root node is not transferred by the model.
24+
explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent /TransferThis/ = 0 );
25+
~QgsLayerTreeModel();
26+
27+
// Implementation of virtual functions from QAbstractItemModel
28+
29+
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
30+
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
31+
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
32+
QModelIndex parent( const QModelIndex &child ) const;
33+
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
34+
Qt::ItemFlags flags( const QModelIndex &index ) const;
35+
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
36+
Qt::DropActions supportedDropActions() const;
37+
QStringList mimeTypes() const;
38+
QMimeData* mimeData( const QModelIndexList& indexes ) const;
39+
bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent );
40+
bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() );
41+
42+
// New stuff
43+
44+
enum Flag
45+
{
46+
// display flags
47+
ShowSymbology, //!< Add symbology items for layer nodes
48+
49+
// behavioral flags
50+
AllowNodeReorder, //!< Allow reordering with drag'n'drop
51+
AllowNodeRename, //!< Allow renaming of groups and layers
52+
AllowNodeChangeVisibility, //!< Allow user to set node visibility with a check box
53+
};
54+
typedef QFlags<QgsLayerTreeModel::Flag> Flags;
55+
56+
//! Set OR-ed combination of model flags
57+
void setFlags( Flags f );
58+
//! Enable or disable a model flag
59+
void setFlag( Flag f, bool on = true );
60+
//! Return OR-ed combination of model flags
61+
Flags flags() const;
62+
//! Check whether a flag is enabled
63+
bool testFlag( Flag f ) const;
64+
65+
//! Return layer tree node for given index. Returns root node for invalid index.
66+
//! Returns null pointer if index does not refer to a layer tree node (e.g. it is a symbology item)
67+
QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
68+
//! Return index for a given node. If the node does not belong to the layer tree, the result is undefined
69+
QModelIndex node2index( QgsLayerTreeNode* node ) const;
70+
//! Convert a list of indexes to a list of layer tree nodes.
71+
//! Indices that do not represent layer tree nodes are skipped.
72+
//! @arg skipInternal If true, a node is included in the output list only if no parent node is in the list
73+
QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
74+
//! Return true if index represents a symbology node (instead of layer node)
75+
bool isIndexSymbologyNode( const QModelIndex& index ) const;
76+
//! Return layer node to which a symbology node belongs to. Returns null pointer if index is not a symbology node.
77+
QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
78+
79+
//! Return pointer to the root node of the layer tree. Always a non-null pointer.
80+
QgsLayerTreeGroup* rootGroup();
81+
82+
//! Force a refresh of symbology of layer node.
83+
//! Not necessary to call when layer's renderer is changed as the model listens to these events.
84+
void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer );
85+
86+
//! Get index of the item marked as current. Item marked as current is underlined.
87+
QModelIndex currentIndex() const;
88+
//! Set index of the current item. May be used by view. Item marked as current is underlined.
89+
void setCurrentIndex( const QModelIndex& currentIndex );
90+
91+
signals:
92+
93+
};
Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,88 @@
1-
1+
/**
2+
* The QgsLayerTreeView class extends QTreeView and provides some additional functionality
3+
* when working with a layer tree.
4+
*
5+
* The view updates expanded state of layer tree nodes and also listens to changes
6+
* to expanded states in the layer tree.
7+
*
8+
* The view keeps track of the current layer and emits a signal when the current layer has changed.
9+
*
10+
* Allows the client to specify a context menu provider with custom actions. Also it comes
11+
* with a set of default actions that can be used when building context menu.
12+
*
13+
* @see QgsLayerTreeModel
14+
* @note added in 2.4
15+
*/
216
class QgsLayerTreeView : QTreeView
317
{
418
%TypeHeaderCode
519
#include <qgslayertreeview.h>
620
%End
721

8-
// this is just a stub
22+
public:
23+
explicit QgsLayerTreeView( QWidget *parent /TransferThis/ = 0 );
24+
~QgsLayerTreeView();
25+
26+
//! Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.
27+
virtual void setModel( QAbstractItemModel* model );
28+
29+
//! Get access to the model casted to QgsLayerTreeModel
30+
QgsLayerTreeModel* layerTreeModel() const;
31+
32+
//! Get access to the default actions that may be used with the tree view
33+
QgsLayerTreeViewDefaultActions* defaultActions();
34+
35+
//! Set provider for context menu. Takes ownership of the instance
36+
void setMenuProvider( QgsLayerTreeViewMenuProvider* menuProvider /Transfer/ );
37+
//! Return pointer to the context menu provider. May be null
38+
QgsLayerTreeViewMenuProvider* menuProvider() const;
39+
40+
//! Get currently selected layer. May be null
41+
QgsMapLayer* currentLayer() const;
42+
//! Set currently selected layer. Null pointer will deselect any layer.
43+
void setCurrentLayer( QgsMapLayer* layer );
44+
45+
//! Get current node. May be null
46+
QgsLayerTreeNode* currentNode() const;
47+
//! Get current group node. If a layer is current node, the function will return parent group. May be null.
48+
QgsLayerTreeGroup* currentGroupNode() const;
49+
50+
//! Return list of selected nodes
51+
//! @arg skipInternal If true, will ignore nodes which have an ancestor in the selection
52+
QList<QgsLayerTreeNode*> selectedNodes( bool skipInternal = false ) const;
53+
//! Return list of selected nodes filtered to just layer nodes
54+
QList<QgsLayerTreeLayer*> selectedLayerNodes() const;
55+
56+
//! Get list of selected layers
57+
QList<QgsMapLayer*> selectedLayers() const;
58+
59+
public slots:
60+
//! Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model
61+
void refreshLayerSymbology( const QString& layerId );
62+
63+
signals:
64+
//! Emitted when a current layer is changed
65+
void currentLayerChanged( QgsMapLayer* layer );
66+
67+
};
68+
69+
70+
/**
71+
* Implementation of this interface can be implemented to allow QgsLayerTreeView
72+
* instance to provide custom context menus (opened upon right-click).
73+
*
74+
* @see QgsLayerTreeView
75+
* @note added in 2.4
76+
*/
77+
class QgsLayerTreeViewMenuProvider
78+
{
79+
%TypeHeaderCode
80+
#include <qgslayertreeview.h>
81+
%End
82+
83+
public:
84+
virtual ~QgsLayerTreeViewMenuProvider();
85+
86+
//! Return a newly created menu instance (or null pointer on error)
87+
virtual QMenu* createContextMenu() = 0 /Factory/;
988
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* The QgsLayerTreeViewDefaultActions class serves as a factory of actions
3+
* that can be used together with a layer tree view.
4+
*
5+
* @see QgsLayerTreeView
6+
* @note added in 2.4
7+
*/
8+
class QgsLayerTreeViewDefaultActions : QObject
9+
{
10+
%TypeHeaderCode
11+
#include <qgslayertreeviewdefaultactions.h>
12+
%End
13+
14+
public:
15+
QgsLayerTreeViewDefaultActions( QgsLayerTreeView* view );
16+
17+
QAction* actionAddGroup( QObject* parent = 0 ) /Factory/;
18+
QAction* actionRemoveGroupOrLayer( QObject* parent = 0 ) /Factory/;
19+
QAction* actionShowInOverview( QObject* parent = 0 ) /Factory/;
20+
QAction* actionRenameGroupOrLayer( QObject* parent = 0 ) /Factory/;
21+
QAction* actionShowFeatureCount( QObject* parent = 0 ) /Factory/;
22+
23+
QAction* actionZoomToLayer( QgsMapCanvas* canvas, QObject* parent = 0 ) /Factory/;
24+
QAction* actionZoomToGroup( QgsMapCanvas* canvas, QObject* parent = 0 ) /Factory/;
25+
// TODO: zoom to selected
26+
27+
QAction* actionMakeTopLevel( QObject* parent = 0 ) /Factory/;
28+
QAction* actionGroupSelected( QObject* parent = 0 ) /Factory/;
29+
30+
void zoomToLayer( QgsMapCanvas* canvas );
31+
void zoomToGroup( QgsMapCanvas* canvas );
32+
33+
public slots:
34+
void showInOverview();
35+
36+
};

0 commit comments

Comments
 (0)
Please sign in to comment.