Skip to content

Commit

Permalink
Merge branch 'atlas' into atlas2
Browse files Browse the repository at this point in the history
Conflicts:
	python/core/composer/qgscomposition.sip
	tests/src/python/CMakeLists.txt
  • Loading branch information
Hugo Mercier committed Oct 4, 2012
2 parents b5f736a + 371efa0 commit e4364f3
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 1 deletion.
3 changes: 3 additions & 0 deletions python/core/composer/qgscomposerlabel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class QgsComposerLabel : QgsComposerItem
@note this function was added in version 1.2*/
QString displayText() const;

/** Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions */
void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, QMap<QString, QVariant> substitutions = QMap<QString, QVariant>() );

QFont font() const;
void setFont( const QFont& f );
/** Accessor for the vertical alignment of the label
Expand Down
20 changes: 20 additions & 0 deletions python/core/composer/qgscomposermap.sip
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,29 @@ class QgsComposerMap : QgsComposerItem
Usually, this function is called before adding the composer map to the composition*/
void assignFreeId();

bool atlasHideCoverage() const;
void setAtlasHideCoverage( bool hide );

bool atlasFixedScale() const;
void setAtlasFixedScale( bool fixed );

float atlasMargin() const;
void setAtlasMargin( float margin );

QString atlasFilenamePattern() const;
void setAtlasFilenamePattern( const QString& pattern );

QgsVectorLayer* atlasCoverageLayer() const;
void setAtlasCoverageLayer( QgsVectorLayer* lmap );

bool atlasSingleFile() const;
void setAtlasSingleFile( bool single );

signals:
void extentChanged();

void atlasCoverageLayerChanged( QgsVectorLayer* );

public slots:

/**Called if map canvas has changed*/
Expand Down
38 changes: 38 additions & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,14 @@ void QgsComposerMapWidget::on_mIsAtlasCheckBox_stateChanged( int state )
}
}

// Connect to addition / removal of layers
QgsMapLayerRegistry* layerRegistry = QgsMapLayerRegistry::instance();
if ( layerRegistry )
{
connect( layerRegistry, SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( onLayerRemoved( QString ) ) );
connect( layerRegistry, SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( onLayerAdded( QgsMapLayer* ) ) );
}

mAtlasFrame->setEnabled( true );
updateGuiElements();
}
Expand All @@ -976,10 +984,40 @@ void QgsComposerMapWidget::on_mIsAtlasCheckBox_stateChanged( int state )
if ( composition->atlasMap() == mComposerMap )
{
composition->setAtlasMap( 0 );

QgsMapLayerRegistry* layerRegistry = QgsMapLayerRegistry::instance();
if ( layerRegistry )
{
disconnect( layerRegistry, SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( onLayerRemoved( QString ) ) );
disconnect( layerRegistry, SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( onLayerAdded( QgsMapLayer* ) ) );
}
}
}
}

void QgsComposerMapWidget::onLayerRemoved( QString layerName )
{
// update the atlas coverage layer combo box
for ( int i = 0; i < mAtlasCoverageLayerComboBox->count(); ++i )
{
if ( mAtlasCoverageLayerComboBox->itemText( i ) == layerName )
{
mAtlasCoverageLayerComboBox->removeItem( i );
break;
}
}
}

void QgsComposerMapWidget::onLayerAdded( QgsMapLayer* map )
{
// update the atlas coverage layer combo box
QgsVectorLayer* vectorLayer = dynamic_cast<QgsVectorLayer*>( map );
if ( vectorLayer )
{
mAtlasCoverageLayerComboBox->addItem( map->id(), qVariantFromValue( (void*)map ) );
}
}

void QgsComposerMapWidget::on_mAtlasCoverageLayerComboBox_currentIndexChanged( int index )
{
if ( !mComposerMap )
Expand Down
5 changes: 5 additions & 0 deletions src/app/composer/qgscomposermapwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "ui_qgscomposermapwidgetbase.h"
#include "qgscomposermap.h"
class QgsMapLayer;

/** \ingroup MapComposer
* Input widget for the configuration of QgsComposerMap
Expand Down Expand Up @@ -104,6 +105,10 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
/**Sets the GUI elements to the values of mPicture*/
void setGuiElementValues();

/** Updates atlas' coverage layer combobox on layer addition / removal */
void onLayerRemoved( QString );
void onLayerAdded( QgsMapLayer* );

private:
QgsComposerMap* mComposerMap;

Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void TestQgsComposerLabel::evaluation()
QDate now = QDate::currentDate();
int dd = now.day();

QString expected = "__" + QString("%1").arg(dd+1, 2, 10, QChar('0')) + "(ok)__";
QString expected = "__" + QString("%1").arg(dd+1) + "(ok)__";
mComposerLabel->setText( "__[%$CURRENT_DATE(dd) + 1%](ok)__" );
QString evaluated = mComposerLabel->displayText();
QCOMPARE( evaluated, expected );
Expand Down
2 changes: 2 additions & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
#ADD_PYTHON_TEST(PyQgsComposerMap test_qgscomposermap.py)
ADD_PYTHON_TEST(PyQgsSymbolLayerV2 test_qgssymbollayerv2.py)
ADD_PYTHON_TEST(PyQgsComposerMapAtlas test_qgscomposermapatlas.py)
ADD_PYTHON_TEST(PyQgsComposerLabel test_qgscomposerlabel.py)
113 changes: 113 additions & 0 deletions tests/src/python/test_qgscomposerlabel.py
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()

Loading

0 comments on commit e4364f3

Please sign in to comment.