Skip to content

Commit

Permalink
[server] Implement LAYERFONTCOLOR and ITEMFONTCOLOR
Browse files Browse the repository at this point in the history
cherry-picking commit dca0fb9
  • Loading branch information
elpaso committed Apr 17, 2019
1 parent d673c6f commit 9baabf9
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 3 deletions.
24 changes: 24 additions & 0 deletions python/core/auto_generated/qgslegendsettings.sip.in
Expand Up @@ -75,6 +75,30 @@ Returns style
QColor fontColor() const;
void setFontColor( const QColor &c );

QColor layerFontColor() const;
%Docstring
Returns layer font color, defaults to fontColor()

.. seealso:: :py:func:`setLayerFontColor`

.. seealso:: :py:func:`fontColor`

.. versionadded:: 3.4.7
%End

void setLayerFontColor( const QColor &fontColor );
%Docstring
Sets layer font color to ``fontColor``
Overrides fontColor()

.. seealso:: :py:func:`layerFontColor`

.. seealso:: :py:func:`fontColor`

.. versionadded:: 3.4.7
%End


QSizeF symbolSize() const;
void setSymbolSize( QSizeF s );

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgslegendrenderer.cpp
Expand Up @@ -526,7 +526,7 @@ QSizeF QgsLegendRenderer::drawLayerTitle( QgsLayerTreeLayer *nodeLayer, QPainter

double y = point.y();

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

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

Expand Down
21 changes: 21 additions & 0 deletions src/core/qgslegendsettings.h
Expand Up @@ -87,6 +87,24 @@ class CORE_EXPORT QgsLegendSettings
QColor fontColor() const {return mFontColor;}
void setFontColor( const QColor &c ) {mFontColor = c;}

/**
* Returns layer font color, defaults to fontColor()
* \see setLayerFontColor()
* \see fontColor()
* \since QGIS 3.4.7
*/
QColor layerFontColor() const {return mLayerFontColor.isValid() ? mLayerFontColor : fontColor() ;}

/**
* Sets layer font color to \a fontColor
* Overrides fontColor()
* \see layerFontColor()
* \see fontColor()
* \since QGIS 3.4.7
*/
void setLayerFontColor( const QColor &fontColor ) {mLayerFontColor = fontColor;}


QSizeF symbolSize() const {return mSymbolSize;}
void setSymbolSize( QSizeF s ) {mSymbolSize = s;}

Expand Down Expand Up @@ -290,6 +308,9 @@ class CORE_EXPORT QgsLegendSettings

//! DPI to be used when rendering legend
int mDpi = 96;

//! Font color for layers, overrides font color
QColor mLayerFontColor;
};


Expand Down
22 changes: 22 additions & 0 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -1094,6 +1094,16 @@ namespace QgsWms
return mWmsParameters[ QgsWmsParameter::ITEMFONTSIZE ].toDouble();
}

QString QgsWmsParameters::itemFontColor() const
{
return mWmsParameters[ QgsWmsParameter::ITEMFONTCOLOR ].toString();
}

QColor QgsWmsParameters::itemFontColorAsColor() const
{
return mWmsParameters[ QgsWmsParameter::ITEMFONTCOLOR ].toColor();
}

QFont QgsWmsParameters::layerFont() const
{
QFont font;
Expand Down Expand Up @@ -1147,6 +1157,18 @@ namespace QgsWms
settings.rstyle( QgsLegendStyle::Subgroup ).setMargin( QgsLegendStyle::Top, layerSpaceAsDouble() );
settings.rstyle( QgsLegendStyle::Subgroup ).setFont( layerFont() );

if ( !itemFontColor().isEmpty() )
{
settings.setFontColor( itemFontColorAsColor() );
}

// Ok, this is tricky: because QgsLegendSettings's layerFontColor was added to the API after
// fontColor, to fix regressions #21871 and #21870 and the previous behavior was to use fontColor
// for the whole legend we need to preserve that behavior.
// But, the 2.18 server parameters ITEMFONTCOLOR did not have effect on the layer titles too, so
// we set explicitly layerFontColor to black if it's not overridden by LAYERFONTCOLOR argument.
settings.setLayerFontColor( layerFontColor().isEmpty() ? QColor( Qt::black ) : layerFontColorAsColor() );

settings.rstyle( QgsLegendStyle::SymbolLabel ).setFont( itemFont() );
settings.rstyle( QgsLegendStyle::Symbol ).setMargin( QgsLegendStyle::Top, symbolSpaceAsDouble() );
settings.rstyle( QgsLegendStyle::SymbolLabel ).setMargin( QgsLegendStyle::Left, iconLabelSpaceAsDouble() );
Expand Down
4 changes: 2 additions & 2 deletions src/server/services/wms/qgswmsparameters.h
Expand Up @@ -187,7 +187,7 @@ namespace QgsWms
/**
* Default destructor for QgsWmsParameter.
*/
virtual ~QgsWmsParameter() = default;
virtual ~QgsWmsParameter() override = default;

/**
* Returns true if the parameter is valid, false otherwise.
Expand Down Expand Up @@ -335,7 +335,7 @@ namespace QgsWms
*/
QgsWmsParameters();

virtual ~QgsWmsParameters() = default;
virtual ~QgsWmsParameters() override = default;

/**
* Dumps parameters.
Expand Down
70 changes: 70 additions & 0 deletions tests/src/python/test_qgsserver_wms_getlegendgraphic.py
Expand Up @@ -665,6 +665,76 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Max", max_size_diff=QSize(15, 15))

def test_wms_GetLegendGraphic_LAYERFONTCOLOR(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetLegendGraphic",
"LAYER": "Country,Hello",
"FORMAT": "image/png",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857",
"LAYERFONTCOLOR": "red"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_LAYERFONTCOLOR", max_size_diff=QSize(10, 2))

def test_wms_GetLegendGraphic_ITEMFONTCOLOR(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetLegendGraphic",
"LAYER": "Country,Hello",
"FORMAT": "image/png",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857",
"ITEMFONTCOLOR": "red",
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ITEMFONTCOLOR", max_size_diff=QSize(10, 2))

def test_wms_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetLegendGraphic",
"LAYER": "Country,Hello",
"FORMAT": "image/png",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857",
"ITEMFONTCOLOR": "red",
"LAYERFONTCOLOR": "blue"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR", max_size_diff=QSize(10, 2))

def test_wms_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR_hex(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetLegendGraphic",
"LAYER": "Country,Hello",
"FORMAT": "image/png",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857",
"ITEMFONTCOLOR": r"%23FF0000",
"LAYERFONTCOLOR": r"%230000FF"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ITEMFONTCOLOR_and_LAYERFONTCOLOR", max_size_diff=QSize(10, 2))


if __name__ == '__main__':
unittest.main()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testdata/qgis_server_accesscontrol/geo.gpkg
Binary file not shown.

0 comments on commit 9baabf9

Please sign in to comment.