Skip to content

Commit 68ed52e

Browse files
committed
API fixes, added missing python wrappers
1 parent ee3d67b commit 68ed52e

File tree

10 files changed

+299
-55
lines changed

10 files changed

+299
-55
lines changed

python/core/core.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
%Include qgslabel.sip
4848
%Include qgslabelattributes.sip
4949
%Include qgslabelsearchtree.sip
50+
%Include qgslegendrenderer.sip
51+
%Include qgslegendsettings.sip
5052
%Include qgslogger.sip
5153
%Include qgsmaplayer.sip
5254
%Include qgsmaplayerlegend.sip

python/core/layertree/qgslayertreemodellegendnode.sip

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class QgsLayerTreeModelLegendNode : QObject
1515
%End
1616

1717
public:
18+
~QgsLayerTreeModelLegendNode();
1819

1920
/** Return pointer to the parent layer node */
2021
QgsLayerTreeLayer* parent() const;
@@ -28,9 +29,57 @@ class QgsLayerTreeModelLegendNode : QObject
2829
/** Set some data associated with the item. Default implementation does nothing and returns false. */
2930
virtual bool setData( const QVariant& value, int role );
3031

32+
virtual bool isEmbeddedInParent() const;
33+
virtual void setEmbeddedInParent( bool embedded );
34+
35+
virtual QString userLabel() const;
36+
virtual void setUserLabel( const QString& userLabel );
37+
38+
virtual bool isScaleOK( double scale ) const;
39+
40+
struct ItemContext
41+
{
42+
//! Painter
43+
QPainter* painter;
44+
//! Top-left corner of the legend item
45+
QPointF point;
46+
//! offset from the left side where label should start
47+
double labelXOffset;
48+
};
49+
50+
struct ItemMetrics
51+
{
52+
QSizeF symbolSize;
53+
QSizeF labelSize;
54+
};
55+
56+
/** Entry point called from QgsLegendRenderer to do the rendering.
57+
* Default implementation calls drawSymbol() and drawSymbolText() methods.
58+
*
59+
* If ctx is null, this is just first stage when preparing layout - without actual rendering.
60+
*/
61+
virtual ItemMetrics draw( const QgsLegendSettings& settings, ItemContext* ctx );
62+
63+
/**
64+
* Draws symbol on the left side of the item
65+
* @param itemHeight Minimal height of the legend item - used for correct positioning when rendering
66+
* @return Real size of the symbol (may be bigger than "normal" symbol size from settings)
67+
*/
68+
virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
69+
70+
/**
71+
* Draws label on the right side of the item
72+
* @param symbolSize Real size of the associated symbol - used for correct positioning when rendering
73+
* @return Size of the label (may span multiple lines)
74+
*/
75+
virtual QSizeF drawSymbolText( const QgsLegendSettings& settings, ItemContext* ctx, const QSizeF& symbolSize ) const;
76+
3177
protected:
3278
/** Construct the node with pointer to its parent layer node */
33-
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL );
79+
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL, QObject* parent /TransferThis/ = 0 );
80+
81+
private:
82+
QgsLayerTreeModelLegendNode(const QgsLayerTreeModelLegendNode &);
3483
};
3584

3685

@@ -41,19 +90,29 @@ class QgsLayerTreeModelLegendNode : QObject
4190
*
4291
* @note added in 2.6
4392
*/
44-
/*
4593
class QgsSymbolV2LegendNode : QgsLayerTreeModelLegendNode
4694
{
4795
%TypeHeaderCode
4896
#include <qgslayertreemodellegendnode.h>
4997
%End
5098
public:
5199
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
100+
~QgsSymbolV2LegendNode();
52101

53102
virtual Qt::ItemFlags flags() const;
54103
virtual QVariant data( int role ) const;
55104
virtual bool setData( const QVariant& value, int role );
56-
};*/
105+
106+
/** Draws a symbol at the current y position and returns the new x position. Returns real symbol height, because for points,
107+
it is possible that it differs from mSymbolHeight */
108+
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
109+
110+
virtual void setEmbeddedInParent( bool embedded );
111+
112+
void setUserLabel( const QString& userLabel );
113+
114+
virtual bool isScaleOK( double scale ) const;
115+
};
57116

58117

59118
/**
@@ -67,8 +126,47 @@ class QgsSimpleLegendNode : QgsLayerTreeModelLegendNode
67126
#include <qgslayertreemodellegendnode.h>
68127
%End
69128
public:
70-
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QString& id, const QIcon& icon = QIcon() );
129+
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon(), QObject* parent /TransferThis/ = 0 );
71130

72131
virtual QVariant data( int role ) const;
73132
};
74133

134+
135+
/**
136+
* Implementation of legend node interface for displaying arbitrary raster image
137+
*
138+
* @note added in 2.6
139+
*/
140+
class QgsImageLegendNode : QgsLayerTreeModelLegendNode
141+
{
142+
%TypeHeaderCode
143+
#include <qgslayertreemodellegendnode.h>
144+
%End
145+
public:
146+
QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img, QObject* parent /TransferThis/ = 0 );
147+
148+
virtual QVariant data( int role ) const;
149+
150+
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
151+
152+
};
153+
154+
/**
155+
* Implementation of legend node interface for displaying raster legend entries
156+
*
157+
* @note added in 2.6
158+
*/
159+
class QgsRasterSymbolLegendNode : QgsLayerTreeModelLegendNode
160+
{
161+
%TypeHeaderCode
162+
#include <qgslayertreemodellegendnode.h>
163+
%End
164+
public:
165+
QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label, QObject* parent /TransferThis/ = 0 );
166+
167+
virtual QVariant data( int role ) const;
168+
169+
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
170+
171+
};
172+

python/core/qgslegendrenderer.sip

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
/**
3+
* @brief The QgsLegendRenderer class handles automatic layout and rendering of legend.
4+
* The content is given by QgsLegendModel instance. Various layout properties can be configured
5+
* within QgsLegendRenderer.
6+
*
7+
* All spacing and sizes are in millimeters.
8+
*
9+
* @note added in 2.6
10+
*/
11+
class QgsLegendRenderer
12+
{
13+
%TypeHeaderCode
14+
#include <qgslegendrenderer.h>
15+
%End
16+
17+
public:
18+
/** Construct legend renderer. The ownership of legend model does not change */
19+
QgsLegendRenderer( QgsLayerTreeModel* legendModel, const QgsLegendSettings& settings );
20+
21+
/** Run the layout algorithm and determine the size required for legend */
22+
QSizeF minimumSize();
23+
24+
/** Set the preferred resulting legend size. */
25+
void setLegendSize( QSizeF s );
26+
27+
/** Find out preferred legend size set by the client. If null, the legend will be drawn with the minimum size */
28+
QSizeF legendSize() const;
29+
30+
/** Draw the legend with given painter. It will occupy the area reported in legendSize().
31+
* Painter should be scaled beforehand so that units correspond to millimeters.
32+
*/
33+
void drawLegend( QPainter* painter );
34+
35+
36+
static void setNodeLegendStyle( QgsLayerTreeNode* node, QgsComposerLegendStyle::Style style );
37+
static QgsComposerLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode* node, QgsLayerTreeModel* model );
38+
39+
};

python/core/qgslegendsettings.sip

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
/**
3+
* @brief The QgsLegendSettings class stores the appearance and layout settings
4+
* for legend drawing with QgsLegendRenderer. The content of the legend is given
5+
* in QgsLegendModel class.
6+
*
7+
* @note added in 2.6
8+
*/
9+
class QgsLegendSettings
10+
{
11+
%TypeHeaderCode
12+
#include <qgslegendsettings.h>
13+
%End
14+
15+
public:
16+
QgsLegendSettings();
17+
18+
void setTitle( const QString& t );
19+
QString title() const;
20+
21+
/** Returns the alignment of the legend title
22+
* @returns Qt::AlignmentFlag for the legend title
23+
* @see setTitleAlignment
24+
*/
25+
Qt::AlignmentFlag titleAlignment() const;
26+
/** Sets the alignment of the legend title
27+
* @param alignment Text alignment for drawing the legend title
28+
* @see titleAlignment
29+
*/
30+
void setTitleAlignment( Qt::AlignmentFlag alignment );
31+
32+
/** Returns reference to modifiable style */
33+
QgsComposerLegendStyle & rstyle( QgsComposerLegendStyle::Style s );
34+
/** Returns style */
35+
QgsComposerLegendStyle style( QgsComposerLegendStyle::Style s ) const;
36+
void setStyle( QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style );
37+
38+
double boxSpace() const;
39+
void setBoxSpace( double s );
40+
41+
void setWrapChar( const QString& t );
42+
QString wrapChar() const;
43+
44+
double columnSpace() const;
45+
void setColumnSpace( double s );
46+
47+
int columnCount() const;
48+
void setColumnCount( int c );
49+
50+
int splitLayer() const;
51+
void setSplitLayer( bool s );
52+
53+
int equalColumnWidth() const;
54+
void setEqualColumnWidth( bool s );
55+
56+
QColor fontColor() const;
57+
void setFontColor( const QColor& c );
58+
59+
QSizeF symbolSize() const;
60+
void setSymbolSize( QSizeF s );
61+
62+
QSizeF wmsLegendSize() const;
63+
void setWmsLegendSize( QSizeF s );
64+
65+
double lineSpacing() const;
66+
void setLineSpacing( double s );
67+
68+
double mmPerMapUnit() const;
69+
void setMmPerMapUnit( double mmPerMapUnit );
70+
71+
bool useAdvancedEffects() const;
72+
void setUseAdvancedEffects( bool use );
73+
74+
// utility functions
75+
76+
/** Splits a string using the wrap char taking into account handling empty
77+
wrap char which means no wrapping */
78+
QStringList splitStringForWrapping( QString stringToSplt ) const;
79+
80+
/** Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
81+
to work around the Qt font bug)*/
82+
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
83+
84+
/** Like the above, but with a rectangle for multiline text
85+
* @param p painter to use
86+
* @param rect rectangle to draw into
87+
* @param text text to draw
88+
* @param font font to use
89+
* @param halignment optional horizontal alignment
90+
* @param valignment optional vertical alignment
91+
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
92+
*/
93+
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const;
94+
95+
/** Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE */
96+
QFont scaledFontPixelSize( const QFont& font ) const;
97+
98+
/** Calculates font to from point size to pixel size */
99+
double pixelFontSize( double pointSize ) const;
100+
101+
/** Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE */
102+
double textWidthMillimeters( const QFont& font, const QString& text ) const;
103+
104+
/** Returns the font height of a character in millimeters */
105+
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
106+
107+
/** Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE */
108+
double fontAscentMillimeters( const QFont& font ) const;
109+
110+
/** Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE */
111+
double fontDescentMillimeters( const QFont& font ) const;
112+
113+
};

python/core/qgsmaplayerlegend.sip

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class QgsMapLayerLegend : QObject
2020
* Return list of legend nodes to be used for a particular layer tree layer node.
2121
* Ownership is transferred to the caller.
2222
*/
23-
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer, bool applyLayerNodeProperties = true ) = 0 /Factory/;
23+
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer ) = 0 /Factory/;
2424

2525
// TODO: support for layer tree view delegates
2626

@@ -75,7 +75,7 @@ class QgsDefaultVectorLayerLegend : QgsMapLayerLegend
7575
public:
7676
explicit QgsDefaultVectorLayerLegend( QgsVectorLayer* vl );
7777

78-
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer, bool applyLayerNodeProperties = true ) /Factory/;
78+
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer ) /Factory/;
7979

8080
};
8181

@@ -91,7 +91,7 @@ class QgsDefaultRasterLayerLegend : QgsMapLayerLegend
9191
public:
9292
explicit QgsDefaultRasterLayerLegend( QgsRasterLayer* rl );
9393

94-
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer, bool applyLayerNodeProperties = true ) /Factory/;
94+
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer ) /Factory/;
9595

9696
};
9797

@@ -107,7 +107,7 @@ class QgsDefaultPluginLayerLegend : QgsMapLayerLegend
107107
public:
108108
explicit QgsDefaultPluginLayerLegend( QgsPluginLayer* pl );
109109

110-
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer, bool applyLayerNodeProperties = true ) /Factory/;
110+
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer ) /Factory/;
111111

112112
};
113113

src/core/layertree/qgslayertreemodel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,15 @@ void QgsLayerTreeModel::addSymbologyToLayer( QgsLayerTreeLayer* nodeL )
694694

695695
QList<QgsLayerTreeModelLegendNode*> lstNew = layerLegend->createLayerTreeModelLegendNodes( nodeL );
696696

697+
// apply filtering defined in layer node's custom properties (reordering, filtering, custom labels)
698+
QgsMapLayerLegendUtils::applyLayerNodeProperties( nodeL, lstNew );
699+
697700
QList<QgsLayerTreeModelLegendNode*> filteredLstNew = filterLegendNodes( lstNew );
698701

699702
beginInsertRows( node2index( nodeL ), 0, filteredLstNew.count() - 1 );
700703

701704
foreach ( QgsLayerTreeModelLegendNode* n, lstNew )
702-
n->setParent( nodeL );
705+
n->setParent( this );
703706

704707
mOriginalSymbologyNodes[nodeL] = lstNew;
705708
mSymbologyNodes[nodeL] = filteredLstNew;

0 commit comments

Comments
 (0)