33 : mLegendModel( legendModel )
34 , mSettings( settings )
40 return paintAndDetermineSize(
nullptr );
45 paintAndDetermineSize( painter );
49 QSizeF QgsLegendRenderer::paintAndDetermineSize(
QPainter* painter )
53 if ( !rootGroup )
return size;
57 setColumns( atomList );
59 qreal maxColumnWidth = 0;
62 Q_FOREACH (
const Atom& atom, atomList )
64 maxColumnWidth = qMax( atom.size.width(), maxColumnWidth );
69 QSizeF titleSize = drawTitle();
71 titleSize.rwidth() += mSettings.
boxSpace() * 2.0;
75 bool firstInColumn =
true;
76 double columnMaxHeight = 0;
77 qreal columnWidth = 0;
79 Q_FOREACH (
const Atom& atom, atomList )
81 if ( atom.column > column )
90 point.rx() += mSettings.
columnSpace() + columnWidth;
92 point.ry() = columnTop;
99 point.ry() += spaceAboveAtom( atom );
102 QSizeF atomSize = drawAtom( atom, painter, point );
103 columnWidth = qMax( atomSize.
width(), columnWidth );
105 point.ry() += atom.size.height();
106 columnMaxHeight = qMax( point.y() - columnTop, columnMaxHeight );
108 firstInColumn =
false;
110 point.rx() += columnWidth + mSettings.
boxSpace();
113 size.
rwidth() = point.x();
116 size.
rwidth() = qMax( titleSize.width(), size.
width() );
122 qreal w = qMax( size.
width(), mLegendSize.
width() );
136 point.rx() = size.
width() / 2;
154 if ( !parentGroup )
return atoms;
163 QList<Atom> groupAtoms = createAtomList( nodeGroup, splitLayer );
164 bool hasSubItems = groupAtoms.
size() > 0;
170 nucleon.size = drawGroupTitle( nodeGroup );
175 groupAtoms[0].
size.rheight() += spaceAboveAtom( groupAtoms[0] );
177 groupAtoms[0].nucleons.
prepend( nucleon );
178 groupAtoms[0].
size.rheight() += nucleon.size.height();
179 groupAtoms[0].
size.rwidth() = qMax( nucleon.size.width(), groupAtoms[0].
size.width() );
185 atom.nucleons.append( nucleon );
186 atom.size.rwidth() += nucleon.size.width();
187 atom.size.rheight() += nucleon.size.height();
188 atom.size.rwidth() = qMax( nucleon.size.width(), atom.size.width() );
189 groupAtoms.
append( atom );
195 atoms.
append( groupAtoms );
209 nucleon.size = drawLayerTitle( nodeLayer );
210 atom.nucleons.append( nucleon );
211 atom.size.rwidth() = nucleon.size.width();
212 atom.size.rheight() = nucleon.size.height();
225 for (
int j = 0; j < legendNodes.
count(); j++ )
229 Nucleon symbolNucleon = drawSymbolItem( legendNode );
235 atom.size.rwidth() = qMax( symbolNucleon.size.width(), atom.size.width() );
237 if ( !atom.nucleons.isEmpty() )
242 atom.size.rheight() += symbolNucleon.size.height();
243 atom.nucleons.append( symbolNucleon );
248 symbolAtom.nucleons.append( symbolNucleon );
249 symbolAtom.size.rwidth() = symbolNucleon.size.width();
250 symbolAtom.size.rheight() = symbolNucleon.size.height();
251 layerAtoms.
append( symbolAtom );
255 atoms.
append( layerAtoms );
263 void QgsLegendRenderer::setColumns(
QList<Atom>& atomList )
268 double totalHeight = 0;
270 qreal maxAtomHeight = 0;
271 Q_FOREACH (
const Atom& atom, atomList )
275 totalHeight += spaceAboveAtom( atom );
277 totalHeight += atom.size.height();
278 maxAtomHeight = qMax( atom.size.height(), maxAtomHeight );
288 double avgColumnHeight = totalHeight / mSettings.
columnCount();
289 int currentColumn = 0;
290 int currentColumnAtomCount = 0;
291 double currentColumnHeight = 0;
292 double maxColumnHeight = 0;
293 double closedColumnsHeight = 0;
295 for (
int i = 0; i < atomList.
size(); i++ )
297 Atom atom = atomList[i];
298 double currentHeight = currentColumnHeight;
301 currentHeight += spaceAboveAtom( atom );
303 currentHeight += atom.size.height();
306 avgColumnHeight = ( totalHeight - closedColumnsHeight ) / ( mSettings.
columnCount() - currentColumn );
307 if (( currentHeight - avgColumnHeight ) > atom.size.height() / 2
308 && currentColumnAtomCount > 0
309 && currentHeight > maxAtomHeight
310 && currentHeight > maxColumnHeight
315 currentColumnAtomCount = 0;
316 closedColumnsHeight += currentColumnHeight;
317 currentColumnHeight = atom.size.height();
321 currentColumnHeight = currentHeight;
323 atomList[i].column = currentColumn;
324 currentColumnAtomCount++;
325 maxColumnHeight = qMax( currentColumnHeight, maxColumnHeight );
332 for (
int i = 0; i < atomList.
size(); i++ )
334 Atom& atom = atomList[i];
335 for (
int j = 0; j < atom.nucleons.size(); j++ )
339 QString key =
QString(
"%1-%2" ).
arg( reinterpret_cast< qulonglong >( legendNode->layerNode() ) ).
arg( atom.column );
340 maxSymbolWidth[key] = qMax( atom.nucleons.at( j ).symbolSize.width(), maxSymbolWidth[key] );
344 for (
int i = 0; i < atomList.
size(); i++ )
346 Atom& atom = atomList[i];
347 for (
int j = 0; j < atom.nucleons.size(); j++ )
351 QString key =
QString(
"%1-%2" ).
arg( reinterpret_cast< qulonglong >( legendNode->layerNode() ) ).
arg( atom.column );
354 atom.nucleons[j].labelXOffset = maxSymbolWidth[key] + space;
355 atom.nucleons[j].
size.rwidth() = maxSymbolWidth[key] + space + atom.nucleons.at( j ).labelSize.width();
362 QSizeF QgsLegendRenderer::drawTitle(
QPainter* painter,
QPointF point, Qt::AlignmentFlag halignment,
double legendWidth )
371 double y = point.
y();
381 switch ( halignment )
383 case Qt::AlignHCenter:
384 textBoxWidth = ( qMin( static_cast< double >( point.
x() ), legendWidth - point.
x() ) - mSettings.
boxSpace() ) * 2.0;
385 textBoxLeft = point.
x() - textBoxWidth / 2.;
389 textBoxWidth = point.
x() - mSettings.
boxSpace();
393 textBoxLeft = point.
x();
394 textBoxWidth = legendWidth - point.
x() - mSettings.
boxSpace();
407 QRectF r( textBoxLeft, y, textBoxWidth, height );
411 mSettings.
drawText( painter, r, *titlePart, titleFont, halignment, Qt::AlignVCenter, Qt::TextDontClip );
418 if ( titlePart != lines.
end() )
429 double QgsLegendRenderer::spaceAboveAtom(
const Atom& atom )
431 if ( atom.nucleons.isEmpty() )
return 0;
433 Nucleon nucleon = atom.nucleons.first();
435 if (
QgsLayerTreeGroup* nodeGroup = qobject_cast<QgsLayerTreeGroup*>( nucleon.item ) )
439 else if (
QgsLayerTreeLayer* nodeLayer = qobject_cast<QgsLayerTreeLayer*>( nucleon.item ) )
443 else if ( qobject_cast<QgsLayerTreeModelLegendNode*>( nucleon.item ) )
458 Q_FOREACH (
const Nucleon& nucleon, atom.nucleons )
460 if (
QgsLayerTreeGroup* groupItem = qobject_cast<QgsLayerTreeGroup*>( nucleon.item ) )
469 drawGroupTitle( groupItem, painter, point );
472 else if (
QgsLayerTreeLayer* layerItem = qobject_cast<QgsLayerTreeLayer*>( nucleon.item ) )
481 drawLayerTitle( layerItem, painter, point );
491 Nucleon symbolNucleon = drawSymbolItem( legendNode, painter, point, nucleon.labelXOffset );
493 size.
rwidth() = qMax( symbolNucleon.size.width(), size.
width() );
495 point.
ry() += nucleon.size.height();
512 nucleon.item = symbolItem;
518 nucleon.size =
QSizeF( width, height );
531 double y = point.
y();
541 if ( painter ) mSettings.
drawText( painter, point.
x(), y, *layerItemPart, layerFont );
544 if ( layerItemPart != lines.
end() )
560 double y = point.
y();
570 if ( painter ) mSettings.
drawText( painter, point.
x(), y, *groupPart, groupFont );
573 if ( groupPart != lines.
end() )
587 if ( style ==
"hidden" )
589 else if ( style ==
"group" )
591 else if ( style ==
"subgroup" )
600 if ( legendNodes.
count() == 1 && legendNodes[0]->isEmbeddedInParent() )
Layer tree group node serves as a container for layers and further groups.
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer)
Return filtered list of active legend nodes attached to a particular layer node.
double lineSpacing() const
QgsLayerTreeGroup * rootGroup() const
Return pointer to the root node of the layer tree. Always a non-null pointer.
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
void drawLegend(QPainter *painter)
Draw the legend with given painter.
const T & at(int i) const
double columnSpace() const
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
bool equalColumnWidth() const
QPainter * painter
Painter.
QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is ...
QgsLegendRenderer(QgsLayerTreeModel *legendModel, const QgsLegendSettings &settings)
Construct legend renderer.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
static void setNodeLegendStyle(QgsLayerTreeNode *node, QgsComposerLegendStyle::Style style)
int count(const T &value) const
void append(const T &value)
void setPen(const QColor &color)
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
QStringList splitStringForWrapping(const QString &stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
This class is a base class for nodes in a layer tree.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
QList< QgsLayerTreeNode * > children()
Get list of children of the node. Children are owned by the parent.
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
QSizeF minimumSize()
Run the layout algorithm and determine the size required for legend.
virtual ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx)
Entry point called from QgsLegendRenderer to do the rendering.
QModelIndex node2index(QgsLayerTreeNode *node) const
Return index for a given node. If the node does not belong to the layer tree, the result is undefined...
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or null if none is enabled) ...
QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is ...
QPointF point
Top-left corner of the legend item.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
static QgsComposerLegendStyle::Style nodeLegendStyle(QgsLayerTreeNode *node, QgsLayerTreeModel *model)
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
void prepend(const T &value)
double labelXOffset
offset from the left side where label should start
bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the node.
Layer tree node points to a map layer.
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...