Skip to content

Commit 89941f6

Browse files
committedSep 1, 2014
Moved QgsLayerTreeModelLegendNode (+derived classes) to a new file
1 parent 6891aae commit 89941f6

12 files changed

+701
-650
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
%Include layertree/qgslayertreegroup.sip
169169
%Include layertree/qgslayertreelayer.sip
170170
%Include layertree/qgslayertreemodel.sip
171+
%Include layertree/qgslayertreemodellegendnode.sip
171172
%Include layertree/qgslayertreenode.sip
172173
%Include layertree/qgslayertreeregistrybridge.sip
173174
%Include layertree/qgslayertreeutils.sip
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
/**
3+
* The QgsLegendRendererItem class is abstract interface for legend items
4+
* returned from QgsMapLayerLegend implementation.
5+
*
6+
* The objects are used in QgsLayerTreeModel. Custom implementations may offer additional interactivity
7+
* and customized look.
8+
*
9+
* @note added in 2.6
10+
*/
11+
class QgsLayerTreeModelLegendNode : QObject
12+
{
13+
%TypeHeaderCode
14+
#include <qgslayertreemodellegendnode.h>
15+
%End
16+
17+
public:
18+
19+
/** Return pointer to the parent layer node */
20+
QgsLayerTreeLayer* parent() const;
21+
22+
/** Return item flags associated with the item. Default implementation returns Qt::ItemIsEnabled. */
23+
virtual Qt::ItemFlags flags() const;
24+
25+
/** Return data associated with the item. Must be implemented in derived class. */
26+
virtual QVariant data( int role ) const = 0;
27+
28+
/** Set some data associated with the item. Default implementation does nothing and returns false. */
29+
virtual bool setData( const QVariant& value, int role );
30+
31+
protected:
32+
/** Construct the node with pointer to its parent layer node */
33+
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL );
34+
};
35+
36+
37+
38+
/**
39+
* Implementation of legend node interface for displaying preview of vector symbols and their labels
40+
* and allowing interaction with the symbol / renderer.
41+
*
42+
* @note added in 2.6
43+
*/
44+
/*
45+
class QgsSymbolV2LegendNode : QgsLayerTreeModelLegendNode
46+
{
47+
%TypeHeaderCode
48+
#include <qgslayertreemodellegendnode.h>
49+
%End
50+
public:
51+
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
52+
53+
virtual Qt::ItemFlags flags() const;
54+
virtual QVariant data( int role ) const;
55+
virtual bool setData( const QVariant& value, int role );
56+
};*/
57+
58+
59+
/**
60+
* Implementation of legend node interface for displaying arbitrary label with icon.
61+
*
62+
* @note added in 2.6
63+
*/
64+
class QgsSimpleLegendNode : QgsLayerTreeModelLegendNode
65+
{
66+
%TypeHeaderCode
67+
#include <qgslayertreemodellegendnode.h>
68+
%End
69+
public:
70+
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon() );
71+
72+
virtual QVariant data( int role ) const;
73+
};
74+

‎python/core/qgsmaplayerlegend.sip

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,4 @@
11

2-
/**
3-
* The QgsLegendRendererItem class is abstract interface for legend items
4-
* returned from QgsMapLayerLegend implementation.
5-
*
6-
* The objects are used in QgsLayerTreeModel. Custom implementations may offer additional interactivity
7-
* and customized look.
8-
*
9-
* @note added in 2.6
10-
*/
11-
class QgsLayerTreeModelLegendNode : QObject
12-
{
13-
%TypeHeaderCode
14-
#include <qgsmaplayerlegend.h>
15-
%End
16-
17-
public:
18-
19-
/** Return pointer to the parent layer node */
20-
QgsLayerTreeLayer* parent() const;
21-
22-
/** Return item flags associated with the item. Default implementation returns Qt::ItemIsEnabled. */
23-
virtual Qt::ItemFlags flags() const;
24-
25-
/** Return data associated with the item. Must be implemented in derived class. */
26-
virtual QVariant data( int role ) const = 0;
27-
28-
/** Set some data associated with the item. Default implementation does nothing and returns false. */
29-
virtual bool setData( const QVariant& value, int role );
30-
31-
protected:
32-
/** Construct the node with pointer to its parent layer node */
33-
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL );
34-
};
35-
36-
37-
38-
/**
39-
* Implementation of legend node interface for displaying preview of vector symbols and their labels
40-
* and allowing interaction with the symbol / renderer.
41-
*
42-
* @note added in 2.6
43-
*/
44-
/*
45-
class QgsSymbolV2LegendNode : QgsLayerTreeModelLegendNode
46-
{
47-
%TypeHeaderCode
48-
#include <qgsmaplayerlegend.h>
49-
%End
50-
public:
51-
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
52-
53-
virtual Qt::ItemFlags flags() const;
54-
virtual QVariant data( int role ) const;
55-
virtual bool setData( const QVariant& value, int role );
56-
};*/
57-
58-
59-
/**
60-
* Implementation of legend node interface for displaying arbitrary label with icon.
61-
*
62-
* @note added in 2.6
63-
*/
64-
class QgsSimpleLegendNode : QgsLayerTreeModelLegendNode
65-
{
66-
%TypeHeaderCode
67-
#include <qgsmaplayerlegend.h>
68-
%End
69-
public:
70-
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon() );
71-
72-
virtual QVariant data( int role ) const;
73-
};
74-
75-
762
/**
773
* The QgsMapLayerLegend class is abstract interface for implementations
784
* of legends for one map layer.
@@ -98,9 +24,6 @@ class QgsMapLayerLegend : QObject
9824

9925
// TODO: support for layer tree view delegates
10026

101-
// TODO: support for legend renderer
102-
103-
10427
//! Create new legend implementation for vector layer
10528
static QgsMapLayerLegend* defaultVectorLegend( QgsVectorLayer* vl ) /Factory/;
10629

‎src/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ SET(QGIS_CORE_SRCS
4949
layertree/qgslayertreegroup.cpp
5050
layertree/qgslayertreelayer.cpp
5151
layertree/qgslayertreemodel.cpp
52+
layertree/qgslayertreemodellegendnode.cpp
5253
layertree/qgslayertreenode.cpp
5354
layertree/qgslayertreeregistrybridge.cpp
5455
layertree/qgslayertreeutils.cpp
@@ -409,6 +410,7 @@ SET(QGIS_CORE_MOC_HDRS
409410
layertree/qgslayertreegroup.h
410411
layertree/qgslayertreelayer.h
411412
layertree/qgslayertreemodel.h
413+
layertree/qgslayertreemodellegendnode.h
412414
layertree/qgslayertreenode.h
413415
layertree/qgslayertreeregistrybridge.h
414416
)
@@ -639,6 +641,7 @@ SET(QGIS_CORE_HDRS
639641
layertree/qgslayertreegroup.h
640642
layertree/qgslayertreelayer.h
641643
layertree/qgslayertreemodel.h
644+
layertree/qgslayertreemodellegendnode.h
642645
layertree/qgslayertreenode.h
643646
layertree/qgslayertreeregistrybridge.h
644647
layertree/qgslayertreeutils.h

‎src/core/layertree/qgslayertreemodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "qgslayertreemodel.h"
1717

1818
#include "qgslayertree.h"
19+
#include "qgslayertreemodellegendnode.h"
1920

2021
#include <QMimeData>
2122
#include <QTextStream>

‎src/core/layertree/qgslayertreemodellegendnode.cpp

Lines changed: 424 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/***************************************************************************
2+
qgslayertreemodellegendnode.h
3+
--------------------------------------
4+
Date : August 2014
5+
Copyright : (C) 2014 by Martin Dobias
6+
Email : wonder dot sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSLAYERTREEMODELLEGENDNODE_H
17+
#define QGSLAYERTREEMODELLEGENDNODE_H
18+
19+
#include <QIcon>
20+
#include <QObject>
21+
22+
class QgsLayerTreeLayer;
23+
class QgsLegendSettings;
24+
class QgsSymbolV2;
25+
26+
27+
/**
28+
* The QgsLegendRendererItem class is abstract interface for legend items
29+
* returned from QgsMapLayerLegend implementation.
30+
*
31+
* The objects are used in QgsLayerTreeModel. Custom implementations may offer additional interactivity
32+
* and customized look.
33+
*
34+
* @note added in 2.6
35+
*/
36+
class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
37+
{
38+
Q_OBJECT
39+
public:
40+
41+
/** Return pointer to the parent layer node */
42+
QgsLayerTreeLayer* parent() const { return mParent; }
43+
44+
/** Return item flags associated with the item. Default implementation returns Qt::ItemIsEnabled. */
45+
virtual Qt::ItemFlags flags() const;
46+
47+
/** Return data associated with the item. Must be implemented in derived class. */
48+
virtual QVariant data( int role ) const = 0;
49+
50+
/** Set some data associated with the item. Default implementation does nothing and returns false. */
51+
virtual bool setData( const QVariant& value, int role );
52+
53+
virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
54+
virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
55+
56+
struct ItemContext
57+
{
58+
//! Painter
59+
QPainter* painter;
60+
//! Top-left corner of the legend item
61+
QPointF point;
62+
//! offset from the left side where label should start
63+
double labelXOffset;
64+
};
65+
66+
struct ItemMetrics
67+
{
68+
QSizeF symbolSize;
69+
QSizeF labelSize;
70+
};
71+
72+
/** Entry point called from QgsLegendRenderer to do the rendering.
73+
* Default implementation calls drawSymbol() and drawSymbolText() methods.
74+
*
75+
* If ctx is null, this is just first stage when preparing layout - without actual rendering.
76+
*/
77+
virtual ItemMetrics draw( const QgsLegendSettings& settings, ItemContext* ctx );
78+
79+
/**
80+
* Draws symbol on the left side of the item
81+
* @param itemHeight Minimal height of the legend item - used for correct positioning when rendering
82+
* @return Real size of the symbol (may be bigger than "normal" symbol size from settings)
83+
*/
84+
virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
85+
86+
/**
87+
* Draws label on the right side of the item
88+
* @param symbolSize Real size of the associated symbol - used for correct positioning when rendering
89+
* @return Size of the label (may span multiple lines)
90+
*/
91+
virtual QSizeF drawSymbolText( const QgsLegendSettings& settings, ItemContext* ctx, const QSizeF& symbolSize ) const;
92+
93+
protected:
94+
/** Construct the node with pointer to its parent layer node */
95+
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL );
96+
97+
protected:
98+
QgsLayerTreeLayer* mParent;
99+
bool mEmbeddedInParent;
100+
};
101+
102+
#include "qgslegendsymbolitemv2.h"
103+
104+
/**
105+
* Implementation of legend node interface for displaying preview of vector symbols and their labels
106+
* and allowing interaction with the symbol / renderer.
107+
*
108+
* @note added in 2.6
109+
*/
110+
class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode
111+
{
112+
public:
113+
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
114+
~QgsSymbolV2LegendNode();
115+
116+
virtual Qt::ItemFlags flags() const;
117+
virtual QVariant data( int role ) const;
118+
virtual bool setData( const QVariant& value, int role );
119+
120+
/** Draws a symbol at the current y position and returns the new x position. Returns real symbol height, because for points,
121+
it is possible that it differs from mSymbolHeight */
122+
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
123+
124+
virtual void setEmbeddedInParent( bool embedded );
125+
126+
private:
127+
void updateLabel();
128+
129+
private:
130+
QgsLegendSymbolItemV2 mItem;
131+
mutable QIcon mIcon; // cached symbol preview
132+
QString mLabel;
133+
};
134+
135+
136+
/**
137+
* Implementation of legend node interface for displaying arbitrary label with icon.
138+
*
139+
* @note added in 2.6
140+
*/
141+
class CORE_EXPORT QgsSimpleLegendNode : public QgsLayerTreeModelLegendNode
142+
{
143+
public:
144+
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon() );
145+
146+
virtual QVariant data( int role ) const;
147+
148+
private:
149+
QString mLabel;
150+
QIcon mIcon;
151+
};
152+
153+
154+
/**
155+
* Implementation of legend node interface for displaying arbitrary raster image
156+
*
157+
* @note added in 2.6
158+
*/
159+
class CORE_EXPORT QgsImageLegendNode : public QgsLayerTreeModelLegendNode
160+
{
161+
public:
162+
QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img );
163+
164+
virtual QVariant data( int role ) const;
165+
166+
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
167+
168+
private:
169+
QImage mImage;
170+
};
171+
172+
/**
173+
* Implementation of legend node interface for displaying raster legend entries
174+
*
175+
* @note added in 2.6
176+
*/
177+
class CORE_EXPORT QgsRasterSymbolLegendNode : public QgsLayerTreeModelLegendNode
178+
{
179+
public:
180+
QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label );
181+
182+
virtual QVariant data( int role ) const;
183+
184+
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
185+
186+
private:
187+
QColor mColor;
188+
QString mLabel;
189+
};
190+
191+
#endif // QGSLAYERTREEMODELLEGENDNODE_H

‎src/core/qgslegendrenderer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "qgscomposerlegenditem.h"
44
#include "qgslayertree.h"
55
#include "qgslayertreemodel.h"
6+
#include "qgslayertreemodellegendnode.h"
67
#include "qgslegendmodel.h"
78
#include "qgsmaplayerlegend.h"
89
#include "qgsmaplayerregistry.h"

‎src/core/qgsmaplayerlegend.cpp

Lines changed: 3 additions & 406 deletions
Large diffs are not rendered by default.

‎src/core/qgsmaplayerlegend.h

Lines changed: 1 addition & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -16,181 +16,15 @@
1616
#ifndef QGSMAPLAYERLEGEND_H
1717
#define QGSMAPLAYERLEGEND_H
1818

19-
#include <QIcon>
2019
#include <QObject>
2120

2221
class QgsLayerTreeLayer;
23-
class QgsLegendSettings;
22+
class QgsLayerTreeModelLegendNode;
2423
class QgsPluginLayer;
2524
class QgsRasterLayer;
26-
class QgsSymbolV2;
2725
class QgsVectorLayer;
2826

2927

30-
/**
31-
* The QgsLegendRendererItem class is abstract interface for legend items
32-
* returned from QgsMapLayerLegend implementation.
33-
*
34-
* The objects are used in QgsLayerTreeModel. Custom implementations may offer additional interactivity
35-
* and customized look.
36-
*
37-
* @note added in 2.6
38-
*/
39-
class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
40-
{
41-
Q_OBJECT
42-
public:
43-
44-
/** Return pointer to the parent layer node */
45-
QgsLayerTreeLayer* parent() const { return mParent; }
46-
47-
/** Return item flags associated with the item. Default implementation returns Qt::ItemIsEnabled. */
48-
virtual Qt::ItemFlags flags() const;
49-
50-
/** Return data associated with the item. Must be implemented in derived class. */
51-
virtual QVariant data( int role ) const = 0;
52-
53-
/** Set some data associated with the item. Default implementation does nothing and returns false. */
54-
virtual bool setData( const QVariant& value, int role );
55-
56-
virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
57-
virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
58-
59-
struct ItemContext
60-
{
61-
//! Painter
62-
QPainter* painter;
63-
//! Top-left corner of the legend item
64-
QPointF point;
65-
//! offset from the left side where label should start
66-
double labelXOffset;
67-
};
68-
69-
struct ItemMetrics
70-
{
71-
QSizeF symbolSize;
72-
QSizeF labelSize;
73-
};
74-
75-
/** Entry point called from QgsLegendRenderer to do the rendering.
76-
* Default implementation calls drawSymbol() and drawSymbolText() methods.
77-
*
78-
* If ctx is null, this is just first stage when preparing layout - without actual rendering.
79-
*/
80-
virtual ItemMetrics draw( const QgsLegendSettings& settings, ItemContext* ctx );
81-
82-
/**
83-
* Draws symbol on the left side of the item
84-
* @param itemHeight Minimal height of the legend item - used for correct positioning when rendering
85-
* @return Real size of the symbol (may be bigger than "normal" symbol size from settings)
86-
*/
87-
virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
88-
89-
/**
90-
* Draws label on the right side of the item
91-
* @param symbolSize Real size of the associated symbol - used for correct positioning when rendering
92-
* @return Size of the label (may span multiple lines)
93-
*/
94-
virtual QSizeF drawSymbolText( const QgsLegendSettings& settings, ItemContext* ctx, const QSizeF& symbolSize ) const;
95-
96-
protected:
97-
/** Construct the node with pointer to its parent layer node */
98-
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL );
99-
100-
protected:
101-
QgsLayerTreeLayer* mParent;
102-
bool mEmbeddedInParent;
103-
};
104-
105-
#include "qgslegendsymbolitemv2.h"
106-
107-
/**
108-
* Implementation of legend node interface for displaying preview of vector symbols and their labels
109-
* and allowing interaction with the symbol / renderer.
110-
*
111-
* @note added in 2.6
112-
*/
113-
class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode
114-
{
115-
public:
116-
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
117-
~QgsSymbolV2LegendNode();
118-
119-
virtual Qt::ItemFlags flags() const;
120-
virtual QVariant data( int role ) const;
121-
virtual bool setData( const QVariant& value, int role );
122-
123-
/** Draws a symbol at the current y position and returns the new x position. Returns real symbol height, because for points,
124-
it is possible that it differs from mSymbolHeight */
125-
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
126-
127-
virtual void setEmbeddedInParent( bool embedded );
128-
129-
private:
130-
void updateLabel();
131-
132-
private:
133-
QgsLegendSymbolItemV2 mItem;
134-
mutable QIcon mIcon; // cached symbol preview
135-
QString mLabel;
136-
};
137-
138-
139-
/**
140-
* Implementation of legend node interface for displaying arbitrary label with icon.
141-
*
142-
* @note added in 2.6
143-
*/
144-
class CORE_EXPORT QgsSimpleLegendNode : public QgsLayerTreeModelLegendNode
145-
{
146-
public:
147-
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon() );
148-
149-
virtual QVariant data( int role ) const;
150-
151-
private:
152-
QString mLabel;
153-
QIcon mIcon;
154-
};
155-
156-
157-
/**
158-
* Implementation of legend node interface for displaying arbitrary raster image
159-
*
160-
* @note added in 2.6
161-
*/
162-
class CORE_EXPORT QgsImageLegendNode : public QgsLayerTreeModelLegendNode
163-
{
164-
public:
165-
QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img );
166-
167-
virtual QVariant data( int role ) const;
168-
169-
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
170-
171-
private:
172-
QImage mImage;
173-
};
174-
175-
/**
176-
* Implementation of legend node interface for displaying raster legend entries
177-
*
178-
* @note added in 2.6
179-
*/
180-
class CORE_EXPORT QgsRasterSymbolLegendNode : public QgsLayerTreeModelLegendNode
181-
{
182-
public:
183-
QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label );
184-
185-
virtual QVariant data( int role ) const;
186-
187-
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
188-
189-
private:
190-
QColor mColor;
191-
QString mLabel;
192-
};
193-
19428
/**
19529
* The QgsMapLayerLegend class is abstract interface for implementations
19630
* of legends for one map layer.

‎src/mapserver/qgswmsserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsgeometry.h"
2323
#include "qgslayertree.h"
2424
#include "qgslayertreemodel.h"
25+
#include "qgslayertreemodellegendnode.h"
2526
#include "qgslegendrenderer.h"
2627
#include "qgsmaplayer.h"
2728
#include "qgsmaplayerlegend.h"

‎tests/src/core/testqgslegendrenderer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "qgsfontutils.h"
99
#include "qgslayertree.h"
1010
#include "qgslayertreemodel.h"
11+
#include "qgslayertreemodellegendnode.h"
1112
#include "qgslegendmodel.h"
1213
#include "qgsmaplayerlegend.h"
1314
#include "qgsmaplayerregistry.h"

0 commit comments

Comments
 (0)
Please sign in to comment.