QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
QgsLegendRenderer Class Reference

The QgsLegendRenderer class handles automatic layout and rendering of legend. More...

#include <qgslegendrenderer.h>

Collaboration diagram for QgsLegendRenderer:
Collaboration graph
[legend]

Classes

class  Atom
 Atom is indivisible set (indivisible into more columns). More...
 
class  Nucleon
 Nucleon is either group title, layer title or layer child item. More...
 

Public Member Functions

 QgsLegendRenderer (QgsLegendModel *legendModel, const QgsLegendSettings &settings)
 Construct legend renderer. More...
 
QSizeF minimumSize ()
 Run the layout algorithm and determine the size required for legend. More...
 
void setLegendSize (QSizeF s)
 Set the preferred resulting legend size. More...
 
QSizeF legendSize () const
 Find out preferred legend size set by the client. More...
 
void drawLegend (QPainter *painter)
 Draw the legend with given painter. More...
 

Private Member Functions

QSizeF paintAndDetermineSize (QPainter *painter)
 
QList< AtomcreateAtomList (QStandardItem *rootItem, bool splitLayer)
 Create list of atoms according to current layer splitting mode. More...
 
void setColumns (QList< Atom > &atomList)
 Divide atoms to columns and set columns on atoms. More...
 
QSizeF drawTitle (QPainter *painter=0, QPointF point=QPointF(), Qt::AlignmentFlag halignment=Qt::AlignLeft, double legendWidth=0)
 Draws title in the legend using the title font and the specified alignment If no painter is specified, function returns the required width/height to draw the title. More...
 
double spaceAboveAtom (Atom atom)
 
QSizeF drawAtom (Atom atom, QPainter *painter=0, QPointF point=QPointF())
 Draw atom and return its actual size, the atom is drawn with the space above it so that first atoms in column are all aligned to the same line regardles their style top space. More...
 
QStringList splitStringForWrapping (QString stringToSplt)
 Splits a string using the wrap char taking into account handling empty wrap char which means no wrapping. More...
 
void drawText (QPainter *p, double x, double y, const QString &text, const QFont &font) const
 Draws Text. More...
 
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
 Like the above, but with a rectangle for multiline text. More...
 
QFont scaledFontPixelSize (const QFont &font) const
 Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE. More...
 
double pixelFontSize (double pointSize) const
 Calculates font to from point size to pixel size. More...
 
double textWidthMillimeters (const QFont &font, const QString &text) const
 Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE. More...
 
double fontHeightCharacterMM (const QFont &font, const QChar &c) const
 Returns the font height of a character in millimeters. More...
 
double fontAscentMillimeters (const QFont &font) const
 Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE. More...
 
double fontDescentMillimeters (const QFont &font) const
 Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE. More...
 
QSizeF drawGroupItemTitle (QgsComposerGroupItem *groupItem, QPainter *painter=0, QPointF point=QPointF())
 Draws a group item and all subitems Returns list of sizes of layers and groups including this group. More...
 
QSizeF drawLayerItemTitle (QgsComposerLayerItem *layerItem, QPainter *painter=0, QPointF point=QPointF())
 Draws a layer item and all subitems. More...
 
Nucleon drawSymbolItem (QgsComposerLegendItem *symbolItem, QPainter *painter=0, QPointF point=QPointF(), double labelXOffset=0.)
 
void drawSymbolV2 (QPainter *p, QgsSymbolV2 *s, double currentYCoord, double &currentXPosition, double &symbolHeight, int opacity=255) const
 Draws a symbol at the current y position and returns the new x position. More...
 

Private Attributes

QgsLegendModelmLegendModel
 
QgsLegendSettings mSettings
 
QSizeF mLegendSize
 

Detailed Description

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

Definition at line 26 of file qgslegendrenderer.h.

Constructor & Destructor Documentation

QgsLegendRenderer::QgsLegendRenderer ( QgsLegendModel legendModel,
const QgsLegendSettings settings 
)

Construct legend renderer.

The ownership of legend model does not change

Definition at line 14 of file qgslegendrenderer.cpp.

Member Function Documentation

QList< QgsLegendRenderer::Atom > QgsLegendRenderer::createAtomList ( QStandardItem *  rootItem,
bool  splitLayer 
)
private
QSizeF QgsLegendRenderer::drawAtom ( Atom  atom,
QPainter *  painter = 0,
QPointF  point = QPointF() 
)
private
QSizeF QgsLegendRenderer::drawGroupItemTitle ( QgsComposerGroupItem groupItem,
QPainter *  painter = 0,
QPointF  point = QPointF() 
)
private

Draws a group item and all subitems Returns list of sizes of layers and groups including this group.

Definition at line 579 of file qgslegendrenderer.cpp.

References drawText(), fontAscentMillimeters(), QgsLegendSettings::fontColor(), QgsLegendSettings::lineSpacing(), mSettings, size, splitStringForWrapping(), QgsLegendSettings::style(), QgsComposerLegendItem::style(), and textWidthMillimeters().

Referenced by createAtomList(), and drawAtom().

QSizeF QgsLegendRenderer::drawLayerItemTitle ( QgsComposerLayerItem layerItem,
QPainter *  painter = 0,
QPointF  point = QPointF() 
)
private
void QgsLegendRenderer::drawLegend ( QPainter *  painter)

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.

Definition at line 25 of file qgslegendrenderer.cpp.

References paintAndDetermineSize().

Referenced by QgsComposerLegend::paint(), and QgsComposerLegend::paintAndDetermineSize().

QgsLegendRenderer::Nucleon QgsLegendRenderer::drawSymbolItem ( QgsComposerLegendItem symbolItem,
QPainter *  painter = 0,
QPointF  point = QPointF(),
double  labelXOffset = 0. 
)
private
void QgsLegendRenderer::drawSymbolV2 ( QPainter *  p,
QgsSymbolV2 s,
double  currentYCoord,
double &  currentXPosition,
double &  symbolHeight,
int  opacity = 255 
) const
private

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

Definition at line 789 of file qgslegendrenderer.cpp.

References QgsSymbolV2::drawPreviewIcon(), QgsSymbolV2::MapUnit, QgsSymbolV2::MM, QgsLegendSettings::mmPerMapUnit(), mSettings, QgsSymbolV2::outputUnit(), QgsSymbolV2::setOutputUnit(), QgsMarkerSymbolV2::setSize(), size, QgsMarkerSymbolV2::size(), QgsLegendSettings::symbolSize(), and QgsLegendSettings::useAdvancedEffects().

Referenced by drawSymbolItem().

void QgsLegendRenderer::drawText ( QPainter *  p,
double  x,
double  y,
const QString &  text,
const QFont &  font 
) const
private

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)

Definition at line 508 of file qgslegendrenderer.cpp.

References FONT_WORKAROUND_SCALE, and scaledFontPixelSize().

Referenced by drawGroupItemTitle(), drawLayerItemTitle(), drawSymbolItem(), and drawTitle().

void QgsLegendRenderer::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
private

Like the above, but with a rectangle for multiline text.

Parameters
ppainter to use
rectrectangle to draw into
texttext to draw
fontfont to use
halignmentoptional horizontal alignment
valignmentoptional vertical alignment
flagsallows for passing Qt::TextFlags to control appearance of rendered text

Definition at line 521 of file qgslegendrenderer.cpp.

References FONT_WORKAROUND_SCALE, and scaledFontPixelSize().

QSizeF QgsLegendRenderer::drawTitle ( QPainter *  painter = 0,
QPointF  point = QPointF(),
Qt::AlignmentFlag  halignment = Qt::AlignLeft,
double  legendWidth = 0 
)
private

Draws title in the legend using the title font and the specified alignment If no painter is specified, function returns the required width/height to draw the title.

Definition at line 342 of file qgslegendrenderer.cpp.

References QgsLegendSettings::boxSpace(), drawText(), QgsComposerLegendStyle::font(), fontAscentMillimeters(), QgsLegendSettings::fontColor(), fontDescentMillimeters(), QgsLegendSettings::lineSpacing(), mSettings, size, splitStringForWrapping(), QgsLegendSettings::style(), textWidthMillimeters(), QgsLegendSettings::title(), and QgsComposerLegendStyle::Title.

Referenced by paintAndDetermineSize().

double QgsLegendRenderer::fontAscentMillimeters ( const QFont &  font) const
private

Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.

Definition at line 564 of file qgslegendrenderer.cpp.

References FONT_WORKAROUND_SCALE, and scaledFontPixelSize().

Referenced by drawGroupItemTitle(), drawLayerItemTitle(), and drawTitle().

double QgsLegendRenderer::fontDescentMillimeters ( const QFont &  font) const
private

Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.

Definition at line 571 of file qgslegendrenderer.cpp.

References FONT_WORKAROUND_SCALE, and scaledFontPixelSize().

Referenced by drawTitle().

double QgsLegendRenderer::fontHeightCharacterMM ( const QFont &  font,
const QChar &  c 
) const
private

Returns the font height of a character in millimeters.

Definition at line 557 of file qgslegendrenderer.cpp.

References FONT_WORKAROUND_SCALE, and scaledFontPixelSize().

Referenced by drawSymbolItem().

QSizeF QgsLegendRenderer::legendSize ( ) const
inline

Find out preferred legend size set by the client.

If null, the legend will be drawn with the minimum size

Definition at line 39 of file qgslegendrenderer.h.

QSizeF QgsLegendRenderer::minimumSize ( )

Run the layout algorithm and determine the size required for legend.

Definition at line 20 of file qgslegendrenderer.cpp.

References paintAndDetermineSize().

Referenced by QgsComposerLegend::adjustBoxSize(), and QgsComposerLegend::paintAndDetermineSize().

QSizeF QgsLegendRenderer::paintAndDetermineSize ( QPainter *  painter)
private
double QgsLegendRenderer::pixelFontSize ( double  pointSize) const
private

Calculates font to from point size to pixel size.

Definition at line 545 of file qgslegendrenderer.cpp.

Referenced by scaledFontPixelSize().

QFont QgsLegendRenderer::scaledFontPixelSize ( const QFont &  font) const
private

Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE.

Definition at line 537 of file qgslegendrenderer.cpp.

References FONT_WORKAROUND_SCALE, and pixelFontSize().

Referenced by drawText(), fontAscentMillimeters(), fontDescentMillimeters(), fontHeightCharacterMM(), and textWidthMillimeters().

void QgsLegendRenderer::setColumns ( QList< Atom > &  atomList)
private
void QgsLegendRenderer::setLegendSize ( QSizeF  s)
inline

Set the preferred resulting legend size.

Definition at line 36 of file qgslegendrenderer.h.

Referenced by QgsComposerLegend::paint().

double QgsLegendRenderer::spaceAboveAtom ( Atom  atom)
private
QStringList QgsLegendRenderer::splitStringForWrapping ( QString  stringToSplt)
private

Splits a string using the wrap char taking into account handling empty wrap char which means no wrapping.

Definition at line 495 of file qgslegendrenderer.cpp.

References mSettings, and QgsLegendSettings::wrapChar().

Referenced by drawGroupItemTitle(), drawLayerItemTitle(), drawSymbolItem(), and drawTitle().

double QgsLegendRenderer::textWidthMillimeters ( const QFont &  font,
const QString &  text 
) const
private

Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.

Definition at line 550 of file qgslegendrenderer.cpp.

References FONT_WORKAROUND_SCALE, and scaledFontPixelSize().

Referenced by drawGroupItemTitle(), drawLayerItemTitle(), drawSymbolItem(), and drawTitle().

Member Data Documentation

QgsLegendModel* QgsLegendRenderer::mLegendModel
private

Definition at line 159 of file qgslegendrenderer.h.

Referenced by paintAndDetermineSize().

QSizeF QgsLegendRenderer::mLegendSize
private

Definition at line 163 of file qgslegendrenderer.h.

Referenced by paintAndDetermineSize().

QgsLegendSettings QgsLegendRenderer::mSettings
private

The documentation for this class was generated from the following files: