@@ -31,12 +31,13 @@ email : tim@linfiniti.com
3131#include " qgslogger.h"
3232#include " qgsmapcanvas.h"
3333#include " qgsproject.h"
34+ #include " qgsreadwritecontext.h"
3435#include " qgssymbollayerutils.h"
3536
3637#include < QPainter>
3738#include < QMenu>
3839#include < QDate>
39- #include < QTextDocument >
40+ #include < QDomDocument >
4041#include < QMatrix>
4142#include < QFile>
4243
@@ -59,25 +60,43 @@ void QgsDecorationCopyright::projectRead()
5960{
6061 QgsDecorationItem::projectRead ();
6162
62- // there is no font setting in the UI, so just use the Qt/QGIS default font (what mQFont gets when created)
63- // mQFont.setFamily( QgsProject::instance()->readEntry( "CopyrightLabel", "/FontName", "Sans Serif" ) );
64- // mQFont.setPointSize( QgsProject::instance()->readNumEntry( "CopyrightLabel", "/FontSize", 9 ) );
65-
6663 mLabelText = QgsProject::instance ()->readEntry ( mNameConfig , QStringLiteral ( " /Label" ), QString () );
6764 mMarginHorizontal = QgsProject::instance ()->readNumEntry ( mNameConfig , QStringLiteral ( " /MarginH" ), 0 );
6865 mMarginVertical = QgsProject::instance ()->readNumEntry ( mNameConfig , QStringLiteral ( " /MarginV" ), 0 );
69- mColor = QgsSymbolLayerUtils::decodeColor ( QgsProject::instance ()->readEntry ( mNameConfig , QStringLiteral ( " /Color" ), QStringLiteral ( " #000000" ) ) );
66+
67+ QDomDocument doc;
68+ QDomElement elem;
69+ QString textXml = QgsProject::instance ()->readEntry ( mNameConfig , QStringLiteral ( " /Font" ) );
70+ if ( !textXml.isEmpty () )
71+ {
72+ doc.setContent ( textXml );
73+ elem = doc.documentElement ();
74+ QgsReadWriteContext rwContext;
75+ rwContext.setPathResolver ( QgsProject::instance ()->pathResolver () );
76+ mTextFormat .readXml ( elem, rwContext );
77+ }
78+
79+ // Migratation for pre QGIS 3.2 settings
80+ QColor oldColor = QgsSymbolLayerUtils::decodeColor ( QgsProject::instance ()->readEntry ( mNameConfig , QStringLiteral ( " /Color" ) ) );
81+ if ( oldColor.isValid () )
82+ {
83+ mTextFormat .setColor ( oldColor );
84+ }
7085}
7186
7287void QgsDecorationCopyright::saveToProject ()
7388{
7489 QgsDecorationItem::saveToProject ();
75- QgsProject::instance ()->writeEntry ( mNameConfig , QStringLiteral ( " /FontName" ), mQFont .family () );
76- QgsProject::instance ()->writeEntry ( mNameConfig , QStringLiteral ( " /FontSize" ), mQFont .pointSize () );
7790 QgsProject::instance ()->writeEntry ( mNameConfig , QStringLiteral ( " /Label" ), mLabelText );
78- QgsProject::instance ()->writeEntry ( mNameConfig , QStringLiteral ( " /Color" ), QgsSymbolLayerUtils::encodeColor ( mColor ) );
7991 QgsProject::instance ()->writeEntry ( mNameConfig , QStringLiteral ( " /MarginH" ), mMarginHorizontal );
8092 QgsProject::instance ()->writeEntry ( mNameConfig , QStringLiteral ( " /MarginV" ), mMarginVertical );
93+
94+ QDomDocument textDoc;
95+ QgsReadWriteContext rwContext;
96+ rwContext.setPathResolver ( QgsProject::instance ()->pathResolver () );
97+ QDomElement textElem = mTextFormat .writeXml ( textDoc, rwContext );
98+ textDoc.appendChild ( textElem );
99+ QgsProject::instance ()->writeEntry ( mNameConfig , QStringLiteral ( " /Font" ), textDoc.toString () );
81100}
82101
83102// Slot called when the buffer menu item is activated
@@ -91,77 +110,82 @@ void QgsDecorationCopyright::run()
91110void QgsDecorationCopyright::render ( const QgsMapSettings &mapSettings, QgsRenderContext &context )
92111{
93112 Q_UNUSED ( mapSettings );
94- // Large IF statement to enable/disable copyright label
95- if ( enabled () )
96- {
97- QString displayString = QgsExpression::replaceExpressionText ( mLabelText , &context.expressionContext () );
113+ if ( !enabled () )
114+ return ;
98115
99- // need width/height of paint device
100- int myHeight = context.painter ()->device ()->height ();
101- int myWidth = context.painter ()->device ()->width ();
116+ context.painter ()->save ();
117+ context.painter ()->setRenderHint ( QPainter::Antialiasing, true );
102118
103- QTextDocument text;
104- text.setDefaultFont ( mQFont );
105- // To set the text color in a QTextDocument we use a CSS style
119+ QString displayString = QgsExpression::replaceExpressionText ( mLabelText , &context.expressionContext () );
120+ QStringList displayStringList = displayString.split ( " \n " );
106121
107- QString style = " <style type=\" text/css\" > p {color: " +
108- QStringLiteral ( " rgba( %1, %2, %3, %4 )" ).arg ( mColor .red () ).arg ( mColor .green () ).arg ( mColor .blue () ).arg ( QString::number ( mColor .alphaF (), ' f' , 2 ) ) + " }</style>" ;
109- text.setHtml ( style + " <p>" + displayString + " </p>" );
110- QSizeF size = text.size ();
122+ QFontMetricsF fm ( mTextFormat .scaledFont ( context ) );
123+ double textWidth = QgsTextRenderer::textWidth ( context, mTextFormat , displayStringList, &fm );
124+ double textHeight = QgsTextRenderer::textHeight ( context, mTextFormat , displayStringList, QgsTextRenderer::Point, &fm );
111125
112- float myXOffset ( 0 ), myYOffset ( 0 );
126+ int deviceHeight = context.painter ()->device ()->height ();
127+ int deviceWidth = context.painter ()->device ()->width ();
113128
114- // Set margin according to selected units
115- switch ( mMarginUnit )
129+ float xOffset ( 0 ), yOffset ( 0 );
130+
131+ // Set margin according to selected units
132+ switch ( mMarginUnit )
133+ {
134+ case QgsUnitTypes::RenderMillimeters:
135+ {
136+ int pixelsInchX = context.painter ()->device ()->logicalDpiX ();
137+ int pixelsInchY = context.painter ()->device ()->logicalDpiY ();
138+ xOffset = pixelsInchX * INCHES_TO_MM * mMarginHorizontal ;
139+ yOffset = pixelsInchY * INCHES_TO_MM * mMarginVertical ;
140+ break ;
141+ }
142+ case QgsUnitTypes::RenderPixels:
116143 {
117- case QgsUnitTypes::RenderMillimeters:
118- {
119- int myPixelsInchX = context.painter ()->device ()->logicalDpiX ();
120- int myPixelsInchY = context.painter ()->device ()->logicalDpiY ();
121- myXOffset = myPixelsInchX * INCHES_TO_MM * mMarginHorizontal ;
122- myYOffset = myPixelsInchY * INCHES_TO_MM * mMarginVertical ;
123- break ;
124- }
125-
126- case QgsUnitTypes::RenderPixels:
127- myXOffset = mMarginHorizontal ;
128- myYOffset = mMarginVertical ;
129- break ;
130-
131- case QgsUnitTypes::RenderPercentage:
132- myXOffset = ( ( myWidth - size.width () ) / 100 . ) * mMarginHorizontal ;
133- myYOffset = ( ( myHeight - size.height () ) / 100 . ) * mMarginVertical ;
134- break ;
135-
136- default : // Use default of top left
137- break ;
144+ xOffset = mMarginHorizontal ;
145+ yOffset = mMarginVertical ;
146+ break ;
138147 }
139- // Determine placement of label from form combo box
140- switch ( mPlacement )
148+ case QgsUnitTypes::RenderPercentage:
141149 {
142- case BottomLeft: // Bottom Left. myXOffset is set above
143- myYOffset = myHeight - myYOffset - size.height ();
144- break ;
145- case TopLeft: // Top left. Already setup above
146- break ;
147- case TopRight: // Top Right. myYOffset is set above
148- myXOffset = myWidth - myXOffset - size.width ();
149- break ;
150- case BottomRight: // Bottom Right
151- // Define bottom right hand corner start point
152- myYOffset = myHeight - myYOffset - size.height ();
153- myXOffset = myWidth - myXOffset - size.width ();
154- break ;
155- default :
156- QgsDebugMsg ( QString ( " Unknown placement index of %1" ).arg ( static_cast <int >( mPlacement ) ) );
150+ xOffset = ( ( deviceWidth - textWidth ) / 100 . ) * mMarginHorizontal ;
151+ yOffset = ( ( deviceHeight - textHeight ) / 100 . ) * mMarginVertical ;
152+ break ;
157153 }
154+ case QgsUnitTypes::RenderMapUnits:
155+ case QgsUnitTypes::RenderPoints:
156+ case QgsUnitTypes::RenderInches:
157+ case QgsUnitTypes::RenderUnknownUnit:
158+ case QgsUnitTypes::RenderMetersInMapUnits:
159+ break ;
160+ }
158161
159- // Paint label to canvas
160- QMatrix worldMatrix = context.painter ()->worldMatrix ();
161- context.painter ()->translate ( myXOffset, myYOffset );
162- text.drawContents ( context.painter () );
163- // Put things back how they were
164- context.painter ()->setWorldMatrix ( worldMatrix );
162+ // Determine placement of label from form combo box
163+ QgsTextRenderer::HAlignment horizontalAlignment = QgsTextRenderer::AlignLeft;
164+ switch ( mPlacement )
165+ {
166+ case BottomLeft: // Bottom Left, xOffset is set above
167+ yOffset = deviceHeight - yOffset;
168+ break ;
169+ case TopLeft: // Top left, xOffset is set above
170+ yOffset = yOffset + textHeight;
171+ break ;
172+ case TopRight: // Top Right
173+ yOffset = yOffset + textHeight;
174+ xOffset = deviceWidth - xOffset;
175+ horizontalAlignment = QgsTextRenderer::AlignRight;
176+ break ;
177+ case BottomRight: // Bottom Right
178+ yOffset = deviceHeight - yOffset;
179+ xOffset = deviceWidth - xOffset;
180+ horizontalAlignment = QgsTextRenderer::AlignRight;
181+ break ;
182+ default :
183+ QgsDebugMsg ( QString ( " Unknown placement index of %1" ).arg ( static_cast <int >( mPlacement ) ) );
165184 }
185+
186+ // Paint label to canvas
187+ QgsTextRenderer::drawText ( QPointF ( xOffset, yOffset ), 0.0 , horizontalAlignment, displayStringList, context, mTextFormat );
188+
189+ context.painter ()->restore ();
166190}
167191
0 commit comments