-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: python/core/composer/qgscomposition.sip tests/src/python/CMakeLists.txt
- Loading branch information
Showing
8 changed files
with
352 additions
and
1 deletion.
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
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,113 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
test_qgscomposerlabel.py | ||
-------------------------------------- | ||
Date : Oct 2012 | ||
Copyright : (C) 2012 by Dr. Hugo Mercier | ||
email : hugo dot mercier at oslandia 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. * | ||
* * | ||
***************************************************************************/ | ||
''' | ||
import unittest | ||
from utilities import * | ||
from PyQt4.QtCore import * | ||
from PyQt4.QtGui import * | ||
from PyQt4.QtXml import * | ||
from qgis.core import * | ||
|
||
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() | ||
|
||
class TestQgsComposerLabel(unittest.TestCase): | ||
|
||
def testCase(self): | ||
TEST_DATA_DIR = unitTestDataPath() | ||
vectorFileInfo = QFileInfo( TEST_DATA_DIR + QDir().separator().toAscii() + "france_parts.shp") | ||
mVectorLayer = QgsVectorLayer( vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr" ) | ||
|
||
QgsMapLayerRegistry.instance().addMapLayer( mVectorLayer ) | ||
|
||
# create composition with composer map | ||
mMapRenderer = QgsMapRenderer() | ||
layerStringList = QStringList() | ||
layerStringList.append( mVectorLayer.id() ) | ||
mMapRenderer.setLayerSet( layerStringList ) | ||
mMapRenderer.setProjectionsEnabled( False ) | ||
|
||
mComposition = QgsComposition( mMapRenderer ) | ||
mComposition.setPaperSize( 297, 210 ) | ||
|
||
mLabel = QgsComposerLabel( mComposition ) | ||
mComposition.addComposerLabel( mLabel ) | ||
|
||
self.evaluation_test( mComposition, mLabel ) | ||
self.feature_evaluation_test( mComposition, mLabel, mVectorLayer ) | ||
self.page_evaluation_test( mComposition, mLabel, mVectorLayer ) | ||
|
||
def evaluation_test( self, mComposition, mLabel ): | ||
# $CURRENT_DATE evaluation | ||
mLabel.setText( "__$CURRENT_DATE__" ) | ||
assert mLabel.displayText() == ( "__" + QDate.currentDate().toString() + "__" ) | ||
|
||
# $CURRENT_DATE() evaluation | ||
mLabel.setText( "__$CURRENT_DATE(dd)(ok)__" ) | ||
expected = "__" + QDateTime.currentDateTime().toString( "dd" ) + "(ok)__" | ||
assert mLabel.displayText() == expected | ||
|
||
# $CURRENT_DATE() evaluation (inside an expression) | ||
mLabel.setText( "__[%$CURRENT_DATE(dd) + 1%](ok)__" ) | ||
dd = QDate.currentDate().day() | ||
expected = "__" + QString( "%1" ).arg(dd+1) + "(ok)__" | ||
assert mLabel.displayText() == expected | ||
|
||
# expression evaluation (without associated feature) | ||
mLabel.setText( "__[%\"NAME_1\"%][%21*2%]__" ) | ||
assert mLabel.displayText() == "__[NAME_1]42__" | ||
|
||
def feature_evaluation_test( self, mComposition, mLabel, mVectorLayer ): | ||
provider = mVectorLayer.dataProvider() | ||
|
||
provider.select( provider.attributeIndexes() ) | ||
feat = QgsFeature() | ||
|
||
provider.nextFeature( feat ) | ||
mLabel.setExpressionContext( feat, mVectorLayer ) | ||
mLabel.setText( "[%\"NAME_1\"||'_ok'%]") | ||
assert mLabel.displayText() == "Basse-Normandie_ok" | ||
|
||
provider.nextFeature( feat ) | ||
mLabel.setExpressionContext( feat, mVectorLayer ) | ||
assert mLabel.displayText() == "Bretagne_ok" | ||
|
||
# evaluation with local variables | ||
locs = { "$test" : "OK" } | ||
mLabel.setExpressionContext( feat, mVectorLayer, locs ) | ||
mLabel.setText( "[%\"NAME_1\"||$test%]" ) | ||
assert mLabel.displayText() == "BretagneOK" | ||
|
||
def page_evaluation_test( self, mComposition, mLabel, mVectorLayer ): | ||
mComposition.setNumPages( 2 ) | ||
mLabel.setText( "[%$page||'/'||$numpages%]" ) | ||
assert mLabel.displayText() == "1/2" | ||
|
||
# move the the second page and re-evaluate | ||
mLabel.setItemPosition( 0, 320 ) | ||
assert mLabel.displayText() == "2/2" | ||
|
||
# use setSpecialColumn | ||
mLabel.setText( "[%$var1 + 1%]" ) | ||
QgsExpression.setSpecialColumn( "$var1", QVariant(41) ) | ||
assert mLabel.displayText() == "42" | ||
QgsExpression.setSpecialColumn( "$var1", QVariant(99) ) | ||
assert mLabel.displayText() == "100" | ||
QgsExpression.unsetSpecialColumn( "$var1" ) | ||
assert mLabel.displayText() == "[%$var1 + 1%]" | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
Oops, something went wrong.