Skip to content

Commit

Permalink
API fixes, added missing python wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Sep 1, 2014
1 parent ee3d67b commit 68ed52e
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 55 deletions.
2 changes: 2 additions & 0 deletions python/core/core.sip
Expand Up @@ -47,6 +47,8 @@
%Include qgslabel.sip
%Include qgslabelattributes.sip
%Include qgslabelsearchtree.sip
%Include qgslegendrenderer.sip
%Include qgslegendsettings.sip
%Include qgslogger.sip
%Include qgsmaplayer.sip
%Include qgsmaplayerlegend.sip
Expand Down
106 changes: 102 additions & 4 deletions python/core/layertree/qgslayertreemodellegendnode.sip
Expand Up @@ -15,6 +15,7 @@ class QgsLayerTreeModelLegendNode : QObject
%End

public:
~QgsLayerTreeModelLegendNode();

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

virtual bool isEmbeddedInParent() const;
virtual void setEmbeddedInParent( bool embedded );

virtual QString userLabel() const;
virtual void setUserLabel( const QString& userLabel );

virtual bool isScaleOK( double scale ) const;

struct ItemContext
{
//! Painter
QPainter* painter;
//! Top-left corner of the legend item
QPointF point;
//! offset from the left side where label should start
double labelXOffset;
};

struct ItemMetrics
{
QSizeF symbolSize;
QSizeF labelSize;
};

/** Entry point called from QgsLegendRenderer to do the rendering.
* Default implementation calls drawSymbol() and drawSymbolText() methods.
*
* If ctx is null, this is just first stage when preparing layout - without actual rendering.
*/
virtual ItemMetrics draw( const QgsLegendSettings& settings, ItemContext* ctx );

/**
* Draws symbol on the left side of the item
* @param itemHeight Minimal height of the legend item - used for correct positioning when rendering
* @return Real size of the symbol (may be bigger than "normal" symbol size from settings)
*/
virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;

/**
* Draws label on the right side of the item
* @param symbolSize Real size of the associated symbol - used for correct positioning when rendering
* @return Size of the label (may span multiple lines)
*/
virtual QSizeF drawSymbolText( const QgsLegendSettings& settings, ItemContext* ctx, const QSizeF& symbolSize ) const;

protected:
/** Construct the node with pointer to its parent layer node */
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL );
explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL, QObject* parent /TransferThis/ = 0 );

private:
QgsLayerTreeModelLegendNode(const QgsLayerTreeModelLegendNode &);
};


Expand All @@ -41,19 +90,29 @@ class QgsLayerTreeModelLegendNode : QObject
*
* @note added in 2.6
*/
/*
class QgsSymbolV2LegendNode : QgsLayerTreeModelLegendNode
{
%TypeHeaderCode
#include <qgslayertreemodellegendnode.h>
%End
public:
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
~QgsSymbolV2LegendNode();

virtual Qt::ItemFlags flags() const;
virtual QVariant data( int role ) const;
virtual bool setData( const QVariant& value, int role );
};*/

/** Draws a symbol at the current y position and returns the new x position. Returns real symbol height, because for points,
it is possible that it differs from mSymbolHeight */
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;

virtual void setEmbeddedInParent( bool embedded );

void setUserLabel( const QString& userLabel );

virtual bool isScaleOK( double scale ) const;
};


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

virtual QVariant data( int role ) const;
};


/**
* Implementation of legend node interface for displaying arbitrary raster image
*
* @note added in 2.6
*/
class QgsImageLegendNode : QgsLayerTreeModelLegendNode
{
%TypeHeaderCode
#include <qgslayertreemodellegendnode.h>
%End
public:
QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img, QObject* parent /TransferThis/ = 0 );

virtual QVariant data( int role ) const;

QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;

};

/**
* Implementation of legend node interface for displaying raster legend entries
*
* @note added in 2.6
*/
class QgsRasterSymbolLegendNode : QgsLayerTreeModelLegendNode
{
%TypeHeaderCode
#include <qgslayertreemodellegendnode.h>
%End
public:
QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label, QObject* parent /TransferThis/ = 0 );

virtual QVariant data( int role ) const;

QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;

};

39 changes: 39 additions & 0 deletions python/core/qgslegendrenderer.sip
@@ -0,0 +1,39 @@

/**
* @brief The QgsLegendRenderer class handles automatic layout and rendering of legend.
* The content is given by QgsLegendModel instance. Various layout properties can be configured
* within QgsLegendRenderer.
*
* All spacing and sizes are in millimeters.
*
* @note added in 2.6
*/
class QgsLegendRenderer
{
%TypeHeaderCode
#include <qgslegendrenderer.h>
%End

public:
/** Construct legend renderer. The ownership of legend model does not change */
QgsLegendRenderer( QgsLayerTreeModel* legendModel, const QgsLegendSettings& settings );

/** Run the layout algorithm and determine the size required for legend */
QSizeF minimumSize();

/** Set the preferred resulting legend size. */
void setLegendSize( QSizeF s );

/** Find out preferred legend size set by the client. If null, the legend will be drawn with the minimum size */
QSizeF legendSize() const;

/** Draw the legend with given painter. It will occupy the area reported in legendSize().
* Painter should be scaled beforehand so that units correspond to millimeters.
*/
void drawLegend( QPainter* painter );


static void setNodeLegendStyle( QgsLayerTreeNode* node, QgsComposerLegendStyle::Style style );
static QgsComposerLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode* node, QgsLayerTreeModel* model );

};
113 changes: 113 additions & 0 deletions python/core/qgslegendsettings.sip
@@ -0,0 +1,113 @@

/**
* @brief The QgsLegendSettings class stores the appearance and layout settings
* for legend drawing with QgsLegendRenderer. The content of the legend is given
* in QgsLegendModel class.
*
* @note added in 2.6
*/
class QgsLegendSettings
{
%TypeHeaderCode
#include <qgslegendsettings.h>
%End

public:
QgsLegendSettings();

void setTitle( const QString& t );
QString title() const;

/** Returns the alignment of the legend title
* @returns Qt::AlignmentFlag for the legend title
* @see setTitleAlignment
*/
Qt::AlignmentFlag titleAlignment() const;
/** Sets the alignment of the legend title
* @param alignment Text alignment for drawing the legend title
* @see titleAlignment
*/
void setTitleAlignment( Qt::AlignmentFlag alignment );

/** Returns reference to modifiable style */
QgsComposerLegendStyle & rstyle( QgsComposerLegendStyle::Style s );
/** Returns style */
QgsComposerLegendStyle style( QgsComposerLegendStyle::Style s ) const;
void setStyle( QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style );

double boxSpace() const;
void setBoxSpace( double s );

void setWrapChar( const QString& t );
QString wrapChar() const;

double columnSpace() const;
void setColumnSpace( double s );

int columnCount() const;
void setColumnCount( int c );

int splitLayer() const;
void setSplitLayer( bool s );

int equalColumnWidth() const;
void setEqualColumnWidth( bool s );

QColor fontColor() const;
void setFontColor( const QColor& c );

QSizeF symbolSize() const;
void setSymbolSize( QSizeF s );

QSizeF wmsLegendSize() const;
void setWmsLegendSize( QSizeF s );

double lineSpacing() const;
void setLineSpacing( double s );

double mmPerMapUnit() const;
void setMmPerMapUnit( double mmPerMapUnit );

bool useAdvancedEffects() const;
void setUseAdvancedEffects( bool use );

// utility functions

/** Splits a string using the wrap char taking into account handling empty
wrap char which means no wrapping */
QStringList splitStringForWrapping( QString stringToSplt ) const;

/** Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
to work around the Qt font bug)*/
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;

/** Like the above, but with a rectangle for multiline text
* @param p painter to use
* @param rect rectangle to draw into
* @param text text to draw
* @param font font to use
* @param halignment optional horizontal alignment
* @param valignment optional vertical alignment
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
*/
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;

/** Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE */
QFont scaledFontPixelSize( const QFont& font ) const;

/** Calculates font to from point size to pixel size */
double pixelFontSize( double pointSize ) const;

/** Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE */
double textWidthMillimeters( const QFont& font, const QString& text ) const;

/** Returns the font height of a character in millimeters */
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;

/** Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE */
double fontAscentMillimeters( const QFont& font ) const;

/** Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE */
double fontDescentMillimeters( const QFont& font ) const;

};
8 changes: 4 additions & 4 deletions python/core/qgsmaplayerlegend.sip
Expand Up @@ -20,7 +20,7 @@ class QgsMapLayerLegend : QObject
* Return list of legend nodes to be used for a particular layer tree layer node.
* Ownership is transferred to the caller.
*/
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer, bool applyLayerNodeProperties = true ) = 0 /Factory/;
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer ) = 0 /Factory/;

// TODO: support for layer tree view delegates

Expand Down Expand Up @@ -75,7 +75,7 @@ class QgsDefaultVectorLayerLegend : QgsMapLayerLegend
public:
explicit QgsDefaultVectorLayerLegend( QgsVectorLayer* vl );

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

};

Expand All @@ -91,7 +91,7 @@ class QgsDefaultRasterLayerLegend : QgsMapLayerLegend
public:
explicit QgsDefaultRasterLayerLegend( QgsRasterLayer* rl );

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

};

Expand All @@ -107,7 +107,7 @@ class QgsDefaultPluginLayerLegend : QgsMapLayerLegend
public:
explicit QgsDefaultPluginLayerLegend( QgsPluginLayer* pl );

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

};

5 changes: 4 additions & 1 deletion src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -694,12 +694,15 @@ void QgsLayerTreeModel::addSymbologyToLayer( QgsLayerTreeLayer* nodeL )

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

// apply filtering defined in layer node's custom properties (reordering, filtering, custom labels)
QgsMapLayerLegendUtils::applyLayerNodeProperties( nodeL, lstNew );

QList<QgsLayerTreeModelLegendNode*> filteredLstNew = filterLegendNodes( lstNew );

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

foreach ( QgsLayerTreeModelLegendNode* n, lstNew )
n->setParent( nodeL );
n->setParent( this );

mOriginalSymbologyNodes[nodeL] = lstNew;
mSymbologyNodes[nodeL] = filteredLstNew;
Expand Down

0 comments on commit 68ed52e

Please sign in to comment.