Skip to content

Commit

Permalink
[FEATURE][composer] Allow data defined control of atlas margin for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 25, 2014
1 parent a9c4136 commit 89a7573
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 33 deletions.
5 changes: 4 additions & 1 deletion python/core/composer/qgscomposermap.sip
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,15 @@ class QgsComposerMap : QgsComposerItem
void setAtlasScalingMode( AtlasScalingMode mode );

/**Returns the margin size (percentage) used when the map is in atlas mode.
* @param valueType controls whether the returned value is the user specified atlas margin,
* or the current evaluated atlas margin (which may be affected by data driven atlas margin
* settings).
* @returns margin size in percentage to leave around the atlas feature's extent
* @note this is only used if atlasScalingMode() is Auto.
* @see atlasScalingMode
* @see setAtlasMargin
*/
double atlasMargin() const;
double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );

/**Sets the margin size (percentage) used when the map is in atlas mode.
* @param margin size in percentage to leave around the atlas feature's extent
Expand Down
1 change: 1 addition & 0 deletions python/core/composer/qgscomposerobject.sip
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class QgsComposerObject : QObject
MapYMin, /*< map extent y minimum */
MapXMax, /*< map extent x maximum */
MapYMax, /*< map extent y maximum */
MapAtlasMargin, /*< map atlas margin*/
//composer picture
PictureSource /*< picture source url */
};
Expand Down
13 changes: 12 additions & 1 deletion src/app/composer/qgscomposermapwidget.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QgsCo
connect( mYMaxDDBtn, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mYMaxDDBtn, SIGNAL( dataDefinedActivated( bool ) ), mYMaxLineEdit, SLOT( setDisabled( bool ) ) );

connect( mAtlasMarginDDBtn, SIGNAL( dataDefinedChanged( const QString& ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mAtlasMarginDDBtn, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( updateDataDefinedProperty( ) ) );

updateGuiElements();
loadGridEntries();
loadOverviewEntries();
Expand All @@ -183,6 +186,7 @@ void QgsComposerMapWidget::populateDataDefinedButtons()
mYMinDDBtn->blockSignals( true );
mXMaxDDBtn->blockSignals( true );
mYMaxDDBtn->blockSignals( true );
mAtlasMarginDDBtn->blockSignals( true );

//initialise buttons to use atlas coverage layer
mScaleDDBtn->init( vl, mComposerMap->dataDefinedProperty( QgsComposerObject::MapScale ),
Expand All @@ -197,6 +201,8 @@ void QgsComposerMapWidget::populateDataDefinedButtons()
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::doubleDesc() );
mYMaxDDBtn->init( vl, mComposerMap->dataDefinedProperty( QgsComposerObject::MapYMax ),
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::doubleDesc() );
mAtlasMarginDDBtn->init( vl, mComposerMap->dataDefinedProperty( QgsComposerObject::MapAtlasMargin ),
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::doubleDesc() );

//initial state of controls - disable related controls when dd buttons are active
mScaleLineEdit->setEnabled( !mScaleDDBtn->isActive() );
Expand All @@ -213,6 +219,7 @@ void QgsComposerMapWidget::populateDataDefinedButtons()
mYMinDDBtn->blockSignals( false );
mXMaxDDBtn->blockSignals( false );
mYMaxDDBtn->blockSignals( false );
mAtlasMarginDDBtn->blockSignals( false );
}

QgsComposerObject::DataDefinedProperty QgsComposerMapWidget::ddPropertyForWidget( QgsDataDefinedButton* widget )
Expand Down Expand Up @@ -241,6 +248,10 @@ QgsComposerObject::DataDefinedProperty QgsComposerMapWidget::ddPropertyForWidget
{
return QgsComposerObject::MapYMax;
}
else if ( widget == mAtlasMarginDDBtn )
{
return QgsComposerObject::MapAtlasMargin;
}

return QgsComposerObject::NoProperty;
}
Expand Down Expand Up @@ -640,7 +651,7 @@ void QgsComposerMapWidget::updateGuiElements()

//atlas controls
mAtlasCheckBox->setChecked( mComposerMap->atlasDriven() );
mAtlasMarginSpinBox->setValue( static_cast<int>( mComposerMap->atlasMargin() * 100 ) );
mAtlasMarginSpinBox->setValue( static_cast<int>( mComposerMap->atlasMargin( QgsComposerObject::OriginalValue ) * 100 ) );

mAtlasFixedScaleRadio->setEnabled( mComposerMap->atlasDriven() );
mAtlasFixedScaleRadio->setChecked( mComposerMap->atlasScalingMode() == QgsComposerMap::Fixed );
Expand Down
30 changes: 30 additions & 0 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void QgsComposerMap::init()
mDataDefinedNames.insert( QgsComposerObject::MapYMin, QString( "dataDefinedMapYMin" ) );
mDataDefinedNames.insert( QgsComposerObject::MapXMax, QString( "dataDefinedMapXMax" ) );
mDataDefinedNames.insert( QgsComposerObject::MapYMax, QString( "dataDefinedMapYMax" ) );
mDataDefinedNames.insert( QgsComposerObject::MapAtlasMargin, QString( "dataDefinedMapAtlasMargin" ) );
}

void QgsComposerMap::adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const
Expand Down Expand Up @@ -2017,6 +2018,7 @@ void QgsComposerMap::refreshDataDefinedProperty( const QgsComposerObject::DataDe
if ( property == QgsComposerObject::MapRotation || property == QgsComposerObject::MapScale ||
property == QgsComposerObject::MapXMin || property == QgsComposerObject::MapYMin ||
property == QgsComposerObject::MapXMax || property == QgsComposerObject::MapYMax ||
property == QgsComposerObject::MapAtlasMargin ||
property == QgsComposerObject::AllProperties )
{
refreshMapExtents();
Expand Down Expand Up @@ -2303,3 +2305,31 @@ void QgsComposerMap::setAtlasFixedScale( bool fixed )
mAtlasScalingMode = fixed ? Fixed : Auto;
}

double QgsComposerMap::atlasMargin( const QgsComposerObject::PropertyValueType valueType )
{
if ( valueType == QgsComposerObject::EvaluatedValue )
{
//evaluate data defined atlas margin

//start with user specified margin
double margin = mAtlasMargin;
QVariant exprVal;
if ( dataDefinedEvaluate( QgsComposerObject::MapAtlasMargin, exprVal ) )
{
bool ok;
double ddMargin = exprVal.toDouble( &ok );
QgsDebugMsg( QString( "exprVal Map Atlas Margin:%1" ).arg( ddMargin ) );
if ( ok )
{
//divide by 100 to convert to 0 -> 1.0 range
margin = ddMargin / 100;
}
}
return margin;
}
else
{
return mAtlasMargin;
}
}

5 changes: 4 additions & 1 deletion src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,15 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }

/**Returns the margin size (percentage) used when the map is in atlas mode.
* @param valueType controls whether the returned value is the user specified atlas margin,
* or the current evaluated atlas margin (which may be affected by data driven atlas margin
* settings).
* @returns margin size in percentage to leave around the atlas feature's extent
* @note this is only used if atlasScalingMode() is Auto.
* @see atlasScalingMode
* @see setAtlasMargin
*/
double atlasMargin() const { return mAtlasMargin; }
double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );

/**Sets the margin size (percentage) used when the map is in atlas mode.
* @param margin size in percentage to leave around the atlas feature's extent
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CORE_EXPORT QgsComposerObject: public QObject
MapYMin, /*< map extent y minimum */
MapXMax, /*< map extent x maximum */
MapYMax, /*< map extent y maximum */
MapAtlasMargin, /*< map atlas margin*/
//composer picture
PictureSource, /*< picture source url */
//html item
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2975,7 +2975,7 @@ bool QgsComposition::dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty
return false;
}

QVariant QgsComposition::dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields *fields, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties )
QVariant QgsComposition::dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields *fields, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const
{
if ( property == QgsComposerObject::AllProperties || property == QgsComposerObject::NoProperty )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
* @note this method was added in version 2.5
*/
QVariant dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields *fields,
QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties );
QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const;


/**Prepares the expression for a data defined property, using the current atlas layer if set.
Expand Down
67 changes: 39 additions & 28 deletions src/ui/qgscomposermapwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>447</width>
<height>2274</height>
<width>444</width>
<height>2238</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -363,28 +363,49 @@
<string notr="true">composeritem</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<widget class="QSpinBox" name="mAtlasMarginSpinBox">
<item row="0" column="0">
<widget class="QRadioButton" name="mAtlasMarginRadio">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="suffix">
<string>%</string>
</property>
<property name="maximum">
<number>9999</number>
</property>
<property name="value">
<number>10</number>
<property name="text">
<string>Margin around feature</string>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_11">
<item>
<widget class="QSpinBox" name="mAtlasMarginSpinBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="suffix">
<string>%</string>
</property>
<property name="maximum">
<number>9999</number>
</property>
<property name="value">
<number>10</number>
</property>
</widget>
</item>
<item>
<widget class="QgsDataDefinedButton" name="mAtlasMarginDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QRadioButton" name="mAtlasPredefinedScaleRadio">
<property name="toolTip">
Expand All @@ -405,16 +426,6 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="mAtlasMarginRadio">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Margin around feature</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 89a7573

Please sign in to comment.