Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid duplicate code for evaluating label obstacle data defined values
- Loading branch information
1 parent
0443299
commit 7f38c41
Showing
11 changed files
with
138 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*************************************************************************** | ||
qgslabelobstaclesettings.cpp | ||
---------------------------- | ||
Date : December 2019 | ||
Copyright : (C) 2019 by Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgslabelobstaclesettings.h" | ||
#include "qgspropertycollection.h" | ||
#include "qgsexpressioncontext.h" | ||
#include "qgspallabeling.h" | ||
|
||
void QgsLabelObstacleSettings::updateDataDefinedProperties( const QgsPropertyCollection &properties, QgsExpressionContext &context ) | ||
{ | ||
if ( properties.isActive( QgsPalLayerSettings::ObstacleFactor ) ) | ||
{ | ||
context.setOriginalValueVariable( mObstacleFactor ); | ||
QVariant exprVal = properties.value( QgsPalLayerSettings::ObstacleFactor, context ); | ||
if ( exprVal.isValid() ) | ||
{ | ||
bool ok; | ||
double factorD = exprVal.toDouble( &ok ); | ||
if ( ok ) | ||
{ | ||
factorD = qBound( 0.0, factorD, 10.0 ); | ||
factorD = factorD / 5.0 + 0.0001; // convert 0 -> 10 to 0.0001 -> 2.0 | ||
mObstacleFactor = factorD; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsLabelObstacleSettings | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'Nyall Dawson' | ||
__date__ = '2019-12-07' | ||
__copyright__ = 'Copyright 2019, The QGIS Project' | ||
|
||
import qgis # NOQA | ||
|
||
from qgis.core import (QgsProperty, | ||
QgsPropertyCollection, | ||
QgsPalLayerSettings, | ||
QgsLabelObstacleSettings, | ||
QgsExpressionContext, | ||
QgsExpressionContextScope) | ||
|
||
from qgis.testing import unittest | ||
|
||
|
||
class TestQgsLabelObstacleSettings(unittest.TestCase): | ||
|
||
def test_obstacle_settings(self): | ||
""" | ||
Test obstacle settings | ||
""" | ||
settings = QgsLabelObstacleSettings() | ||
settings.setIsObstacle(True) | ||
self.assertTrue(settings.isObstacle()) | ||
settings.setIsObstacle(False) | ||
self.assertFalse(settings.isObstacle()) | ||
|
||
settings.setFactor(0.1) | ||
self.assertEqual(settings.factor(), 0.1) | ||
|
||
settings.setType(QgsLabelObstacleSettings.PolygonWhole) | ||
self.assertEqual(settings.type(), QgsLabelObstacleSettings.PolygonWhole) | ||
|
||
# check that compatibility code works | ||
pal_settings = QgsPalLayerSettings() | ||
pal_settings.obstacle = True | ||
self.assertTrue(pal_settings.obstacle) | ||
self.assertTrue(pal_settings.obstacleSettings().isObstacle()) | ||
pal_settings.obstacle = False | ||
self.assertFalse(pal_settings.obstacle) | ||
self.assertFalse(pal_settings.obstacleSettings().isObstacle()) | ||
|
||
pal_settings.obstacleFactor = 0.2 | ||
self.assertEqual(pal_settings.obstacleFactor, 0.2) | ||
self.assertEqual(pal_settings.obstacleSettings().factor(), 0.2) | ||
|
||
pal_settings.obstacleType = QgsPalLayerSettings.PolygonWhole | ||
self.assertEqual(pal_settings.obstacleType, QgsPalLayerSettings.PolygonWhole) | ||
self.assertEqual(pal_settings.obstacleSettings().type(), QgsLabelObstacleSettings.PolygonWhole) | ||
|
||
def testUpdateDataDefinedProps(self): | ||
settings = QgsLabelObstacleSettings() | ||
settings.setFactor(0.1) | ||
self.assertEqual(settings.factor(), 0.1) | ||
|
||
props = QgsPropertyCollection() | ||
props.setProperty(QgsPalLayerSettings.ObstacleFactor, QgsProperty.fromExpression('@factor')) | ||
context = QgsExpressionContext() | ||
scope = QgsExpressionContextScope() | ||
scope.setVariable('factor', 9) | ||
props.updateDataDefinedProperties(props, context) | ||
self.assertEqual(settings.factor(), 0.1) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters