Skip to content

Commit 9baabf9

Browse files
committed
[server] Implement LAYERFONTCOLOR and ITEMFONTCOLOR
cherry-picking commit dca0fb9
1 parent d673c6f commit 9baabf9

File tree

13 files changed

+140
-3
lines changed

13 files changed

+140
-3
lines changed

python/core/auto_generated/qgslegendsettings.sip.in

+24
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ Returns style
7575
QColor fontColor() const;
7676
void setFontColor( const QColor &c );
7777

78+
QColor layerFontColor() const;
79+
%Docstring
80+
Returns layer font color, defaults to fontColor()
81+
82+
.. seealso:: :py:func:`setLayerFontColor`
83+
84+
.. seealso:: :py:func:`fontColor`
85+
86+
.. versionadded:: 3.4.7
87+
%End
88+
89+
void setLayerFontColor( const QColor &fontColor );
90+
%Docstring
91+
Sets layer font color to ``fontColor``
92+
Overrides fontColor()
93+
94+
.. seealso:: :py:func:`layerFontColor`
95+
96+
.. seealso:: :py:func:`fontColor`
97+
98+
.. versionadded:: 3.4.7
99+
%End
100+
101+
78102
QSizeF symbolSize() const;
79103
void setSymbolSize( QSizeF s );
80104

src/core/qgslegendrenderer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ QSizeF QgsLegendRenderer::drawLayerTitle( QgsLayerTreeLayer *nodeLayer, QPainter
526526

527527
double y = point.y();
528528

529-
if ( painter ) painter->setPen( mSettings.fontColor() );
529+
if ( painter ) painter->setPen( mSettings.layerFontColor() );
530530

531531
QFont layerFont = mSettings.style( nodeLegendStyle( nodeLayer ) ).font();
532532

src/core/qgslegendsettings.h

+21
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ class CORE_EXPORT QgsLegendSettings
8787
QColor fontColor() const {return mFontColor;}
8888
void setFontColor( const QColor &c ) {mFontColor = c;}
8989

90+
/**
91+
* Returns layer font color, defaults to fontColor()
92+
* \see setLayerFontColor()
93+
* \see fontColor()
94+
* \since QGIS 3.4.7
95+
*/
96+
QColor layerFontColor() const {return mLayerFontColor.isValid() ? mLayerFontColor : fontColor() ;}
97+
98+
/**
99+
* Sets layer font color to \a fontColor
100+
* Overrides fontColor()
101+
* \see layerFontColor()
102+
* \see fontColor()
103+
* \since QGIS 3.4.7
104+
*/
105+
void setLayerFontColor( const QColor &fontColor ) {mLayerFontColor = fontColor;}
106+
107+
90108
QSizeF symbolSize() const {return mSymbolSize;}
91109
void setSymbolSize( QSizeF s ) {mSymbolSize = s;}
92110

@@ -290,6 +308,9 @@ class CORE_EXPORT QgsLegendSettings
290308

291309
//! DPI to be used when rendering legend
292310
int mDpi = 96;
311+
312+
//! Font color for layers, overrides font color
313+
QColor mLayerFontColor;
293314
};
294315

295316

src/server/services/wms/qgswmsparameters.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,16 @@ namespace QgsWms
10941094
return mWmsParameters[ QgsWmsParameter::ITEMFONTSIZE ].toDouble();
10951095
}
10961096

1097+
QString QgsWmsParameters::itemFontColor() const
1098+
{
1099+
return mWmsParameters[ QgsWmsParameter::ITEMFONTCOLOR ].toString();
1100+
}
1101+
1102+
QColor QgsWmsParameters::itemFontColorAsColor() const
1103+
{
1104+
return mWmsParameters[ QgsWmsParameter::ITEMFONTCOLOR ].toColor();
1105+
}
1106+
10971107
QFont QgsWmsParameters::layerFont() const
10981108
{
10991109
QFont font;
@@ -1147,6 +1157,18 @@ namespace QgsWms
11471157
settings.rstyle( QgsLegendStyle::Subgroup ).setMargin( QgsLegendStyle::Top, layerSpaceAsDouble() );
11481158
settings.rstyle( QgsLegendStyle::Subgroup ).setFont( layerFont() );
11491159

1160+
if ( !itemFontColor().isEmpty() )
1161+
{
1162+
settings.setFontColor( itemFontColorAsColor() );
1163+
}
1164+
1165+
// Ok, this is tricky: because QgsLegendSettings's layerFontColor was added to the API after
1166+
// fontColor, to fix regressions #21871 and #21870 and the previous behavior was to use fontColor
1167+
// for the whole legend we need to preserve that behavior.
1168+
// But, the 2.18 server parameters ITEMFONTCOLOR did not have effect on the layer titles too, so
1169+
// we set explicitly layerFontColor to black if it's not overridden by LAYERFONTCOLOR argument.
1170+
settings.setLayerFontColor( layerFontColor().isEmpty() ? QColor( Qt::black ) : layerFontColorAsColor() );
1171+
11501172
settings.rstyle( QgsLegendStyle::SymbolLabel ).setFont( itemFont() );
11511173
settings.rstyle( QgsLegendStyle::Symbol ).setMargin( QgsLegendStyle::Top, symbolSpaceAsDouble() );
11521174
settings.rstyle( QgsLegendStyle::SymbolLabel ).setMargin( QgsLegendStyle::Left, iconLabelSpaceAsDouble() );

src/server/services/wms/qgswmsparameters.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace QgsWms
187187
/**
188188
* Default destructor for QgsWmsParameter.
189189
*/
190-
virtual ~QgsWmsParameter() = default;
190+
virtual ~QgsWmsParameter() override = default;
191191

192192
/**
193193
* Returns true if the parameter is valid, false otherwise.
@@ -335,7 +335,7 @@ namespace QgsWms
335335
*/
336336
QgsWmsParameters();
337337

338-
virtual ~QgsWmsParameters() = default;
338+
virtual ~QgsWmsParameters() override = default;
339339

340340
/**
341341
* Dumps parameters.

tests/src/python/test_qgsserver_wms_getlegendgraphic.py

+70
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,76 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
665665
r, h = self._result(self._execute_request(qs))
666666
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Max", max_size_diff=QSize(15, 15))
667667

668+
def test_wms_GetLegendGraphic_LAYERFONTCOLOR(self):
669+
qs = "?" + "&".join(["%s=%s" % i for i in list({
670+
"MAP": urllib.parse.quote(self.projectPath),
671+
"SERVICE": "WMS",
672+
"VERSION": "1.1.1",
673+
"REQUEST": "GetLegendGraphic",
674+
"LAYER": "Country,Hello",
675+
"FORMAT": "image/png",
676+
"HEIGHT": "500",
677+
"WIDTH": "500",
678+
"CRS": "EPSG:3857",
679+
"LAYERFONTCOLOR": "red"
680+
}.items())])
681+
682+
r, h = self._result(self._execute_request(qs))
683+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_LAYERFONTCOLOR", max_size_diff=QSize(10, 2))
684+
685+
def test_wms_GetLegendGraphic_ITEMFONTCOLOR(self):
686+
qs = "?" + "&".join(["%s=%s" % i for i in list({
687+
"MAP": urllib.parse.quote(self.projectPath),
688+
"SERVICE": "WMS",
689+
"VERSION": "1.1.1",
690+
"REQUEST": "GetLegendGraphic",
691+
"LAYER": "Country,Hello",
692+
"FORMAT": "image/png",
693+
"HEIGHT": "500",
694+
"WIDTH": "500",
695+
"CRS": "EPSG:3857",
696+
"ITEMFONTCOLOR": "red",
697+
}.items())])
698+
699+
r, h = self._result(self._execute_request(qs))
700+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ITEMFONTCOLOR", max_size_diff=QSize(10, 2))
701+
702+
def test_wms_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR(self):
703+
qs = "?" + "&".join(["%s=%s" % i for i in list({
704+
"MAP": urllib.parse.quote(self.projectPath),
705+
"SERVICE": "WMS",
706+
"VERSION": "1.1.1",
707+
"REQUEST": "GetLegendGraphic",
708+
"LAYER": "Country,Hello",
709+
"FORMAT": "image/png",
710+
"HEIGHT": "500",
711+
"WIDTH": "500",
712+
"CRS": "EPSG:3857",
713+
"ITEMFONTCOLOR": "red",
714+
"LAYERFONTCOLOR": "blue"
715+
}.items())])
716+
717+
r, h = self._result(self._execute_request(qs))
718+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR", max_size_diff=QSize(10, 2))
719+
720+
def test_wms_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR_hex(self):
721+
qs = "?" + "&".join(["%s=%s" % i for i in list({
722+
"MAP": urllib.parse.quote(self.projectPath),
723+
"SERVICE": "WMS",
724+
"VERSION": "1.1.1",
725+
"REQUEST": "GetLegendGraphic",
726+
"LAYER": "Country,Hello",
727+
"FORMAT": "image/png",
728+
"HEIGHT": "500",
729+
"WIDTH": "500",
730+
"CRS": "EPSG:3857",
731+
"ITEMFONTCOLOR": r"%23FF0000",
732+
"LAYERFONTCOLOR": r"%230000FF"
733+
}.items())])
734+
735+
r, h = self._result(self._execute_request(qs))
736+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR", max_size_diff=QSize(10, 2))
737+
668738

669739
if __name__ == '__main__':
670740
unittest.main()
Binary file not shown.

0 commit comments

Comments
 (0)