Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsdatadefinedsizelegend.h 3 : : -------------------------------------- 4 : : Date : June 2017 5 : : Copyright : (C) 2017 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 QGSDATADEFINEDSIZELEGEND_H 17 : : #define QGSDATADEFINEDSIZELEGEND_H 18 : : 19 : : #include "qgslegendsymbolitem.h" 20 : : 21 : : #include <QColor> 22 : : #include <QFont> 23 : : 24 : : class QDomElement; 25 : : class QgsMarkerSymbol; 26 : : class QgsProperty; 27 : : class QgsReadWriteContext; 28 : : class QgsRenderContext; 29 : : class QgsSizeScaleTransformer; 30 : : class QgsLineSymbol; 31 : : 32 : : 33 : : /** 34 : : * \ingroup core 35 : : * \brief Object that keeps configuration of appearance of marker symbol's data-defined size in legend. 36 : : * 37 : : * For example: the list of classes (size values), whether the classes should appear in separate 38 : : * legend nodes or whether to collapse them into one legend node. 39 : : * 40 : : * \since QGIS 3.0 41 : : */ 42 : : class CORE_EXPORT QgsDataDefinedSizeLegend 43 : : { 44 : : public: 45 : : 46 : : /** 47 : : * Constructor for QgsDataDefinedSizeLegend. 48 : : */ 49 : : QgsDataDefinedSizeLegend(); 50 : : 51 : : ~QgsDataDefinedSizeLegend(); 52 : : 53 : : //! Copy constructor 54 : : QgsDataDefinedSizeLegend( const QgsDataDefinedSizeLegend &other ); 55 : : QgsDataDefinedSizeLegend &operator=( const QgsDataDefinedSizeLegend &other ); 56 : : 57 : : //! Determines how to display data-defined size legend 58 : : enum LegendType 59 : : { 60 : : LegendSeparated, //!< Each class (size value) has a separate legend node 61 : : LegendCollapsed, //!< All classes are rendered within one legend node 62 : : }; 63 : : 64 : : //! How to vertically align symbols when all classes go into one node 65 : : enum VerticalAlignment 66 : : { 67 : : AlignCenter, //!< Symbols are aligned to the center 68 : : AlignBottom, //!< Symbols are aligned to the bottom 69 : : }; 70 : : 71 : : //! Definition of one class for the legend 72 : 0 : struct SizeClass 73 : : { 74 : 0 : SizeClass( double size, const QString &label ): size( size ), label( label ) {} 75 : : 76 : : double size; //!< Marker size in units used by the symbol (usually millimeters). May be further scaled before rendering if size scale transformer is enabled. 77 : : QString label; //!< Label to be shown with the particular symbol size 78 : : }; 79 : : 80 : : //! Sets how the legend should be rendered 81 : 0 : void setLegendType( LegendType type ) { mType = type; } 82 : : //! Returns how the legend should be rendered 83 : : LegendType legendType() const { return mType; } 84 : : 85 : : //! Sets marker symbol that will be used to draw markers in legend 86 : : void setSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER ); 87 : : //! Returns marker symbol that will be used to draw markers in legend 88 : : QgsMarkerSymbol *symbol() const; 89 : : 90 : : /** 91 : : * Sets the line \a symbol that will be used to draw callout lines in legend. 92 : : * 93 : : * Ownership of \a symbol is transferred. 94 : : * 95 : : * \see lineSymbol() 96 : : * \since QGIS 3.14 97 : : */ 98 : : void setLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER ); 99 : : 100 : : /** 101 : : * Returns the line symbol that will be used to draw callout lines in legend. 102 : : * \see setLineSymbol() 103 : : * \since QGIS 3.14 104 : : */ 105 : : QgsLineSymbol *lineSymbol() const; 106 : : 107 : : //! Sets transformer for scaling of symbol sizes. Takes ownership of the object. Accepts NULLPTR to set no transformer. 108 : : void setSizeScaleTransformer( QgsSizeScaleTransformer *transformer SIP_TRANSFER ); 109 : : //! Returns transformer for scaling of symbol sizes. Returns NULLPTR if no transformer is defined. 110 : : QgsSizeScaleTransformer *sizeScaleTransformer() const; 111 : : 112 : : //! Sets list of classes: each class is a pair of symbol size (in units used by the symbol) and label 113 : 0 : void setClasses( const QList< QgsDataDefinedSizeLegend::SizeClass > &classes ) { mSizeClasses = classes; } 114 : : //! Returns list of classes: each class is a pair of symbol size (in units used by the symbol) and label 115 : 0 : QList< QgsDataDefinedSizeLegend::SizeClass > classes() const { return mSizeClasses; } 116 : : 117 : : //! Sets title label for data-defined size legend 118 : 0 : void setTitle( const QString &title ) { mTitleLabel = title; } 119 : : //! Returns title label for data-defined size legend 120 : : QString title() const { return mTitleLabel; } 121 : : 122 : : //! Sets vertical alignment of symbols - only valid for collapsed legend 123 : 0 : void setVerticalAlignment( VerticalAlignment vAlign ) { mVAlign = vAlign; } 124 : : //! Returns vertical alignment of symbols - only valid for collapsed legend 125 : : VerticalAlignment verticalAlignment() const { return mVAlign; } 126 : : 127 : : //! Sets font used for rendering of labels - only valid for collapsed legend 128 : 0 : void setFont( const QFont &font ) { mFont = font; } 129 : : //! Returns font used for rendering of labels - only valid for collapsed legend 130 : : QFont font() const { return mFont; } 131 : : 132 : : //! Sets text color for rendering of labels - only valid for collapsed legend 133 : 0 : void setTextColor( const QColor &color ) { mTextColor = color; } 134 : : //! Returns text color for rendering of labels - only valid for collapsed legend 135 : : QColor textColor() const { return mTextColor; } 136 : : 137 : : //! Sets horizontal text alignment for rendering of labels - only valid for collapsed legend 138 : 0 : void setTextAlignment( Qt::AlignmentFlag flag ) { mTextAlignment = flag; } 139 : : //! Returns horizontal text alignment for rendering of labels - only valid for collapsed legend 140 : : Qt::AlignmentFlag textAlignment() const { return mTextAlignment; } 141 : : 142 : : // 143 : : 144 : : //! Updates the list of classes, source symbol and title label from given symbol and property 145 : : void updateFromSymbolAndProperty( const QgsMarkerSymbol *symbol, const QgsProperty &ddSize ); 146 : : 147 : : //! Generates legend symbol items according to the configuration 148 : : QgsLegendSymbolList legendSymbolList() const; 149 : : 150 : : /** 151 : : * Draw the legend if using LegendOneNodeForAll and optionally output size of the legend and x offset of labels (in painter units). 152 : : * If the painter in context is NULLPTR, it only does size calculation without actual rendering. 153 : : * Does nothing if legend is not configured as collapsed. 154 : : */ 155 : : void drawCollapsedLegend( QgsRenderContext &context, QSizeF *outputSize SIP_OUT = nullptr, double *labelXOffset SIP_OUT = nullptr ) const; 156 : : 157 : : //! Returns output image that would be shown in the legend. Returns invalid image if legend is not configured as collapsed. 158 : : QImage collapsedLegendImage( QgsRenderContext &context, const QColor &backgroundColor = Qt::transparent, double paddingMM = 1 ) const; 159 : : 160 : : //! Creates instance from given element and returns it (caller takes ownership). Returns NULLPTR on error. 161 : : static QgsDataDefinedSizeLegend *readXml( const QDomElement &elem, const QgsReadWriteContext &context ) SIP_FACTORY; 162 : : 163 : : //! Writes configuration to the given XML element. 164 : : void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const; 165 : : 166 : : private: 167 : : LegendType mType = LegendSeparated; 168 : : QString mTitleLabel; //!< Title label for the following size-based item(s) 169 : : QList< SizeClass > mSizeClasses; //!< List of classes: symbol size (in whatever units symbol uses) + label 170 : : std::unique_ptr<QgsMarkerSymbol> mSymbol; 171 : : std::unique_ptr<QgsLineSymbol> mLineSymbol; 172 : : std::unique_ptr<QgsSizeScaleTransformer> mSizeScaleTransformer; //!< Optional transformer for classes 173 : : VerticalAlignment mVAlign = AlignBottom; 174 : : QFont mFont; 175 : : QColor mTextColor = Qt::black; 176 : : Qt::AlignmentFlag mTextAlignment = Qt::AlignLeft; 177 : : }; 178 : : 179 : : #endif // QGSDATADEFINEDSIZELEGEND_H