Skip to content

Commit

Permalink
[FEATURE][composer] Data defined control over map layers
Browse files Browse the repository at this point in the history
Adds new data defined control for the map layers to show in a
composer map. Expression result should be a | delimited list of
layer names which will be shown in the map.

Sponsored by City of Uster
  • Loading branch information
nyalldawson committed Aug 17, 2015
1 parent a05a970 commit 7527638
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 65 deletions.
3 changes: 2 additions & 1 deletion python/core/composer/qgscomposerobject.sip
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class QgsComposerObject : QObject
MapXMax, /*!< map extent x maximum */
MapYMax, /*!< map extent y maximum */
MapAtlasMargin, /*!< map atlas margin*/
MapLayers, /*!< map layer set*/
//composer picture
PictureSource, /*!< picture source url */
//html item
Expand Down Expand Up @@ -156,7 +157,7 @@ class QgsComposerObject : QObject
* @param expressionValue QVariant for storing the evaluated value
* @note this method was added in version 2.5
*/
bool dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue );
bool dataDefinedEvaluate( const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue ) const;

signals:
/** Emitted when the item changes. Signifies that the item widgets must update the
Expand Down
12 changes: 11 additions & 1 deletion src/app/composer/qgscomposermapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap )
connect( mAtlasMarginDDBtn, SIGNAL( dataDefinedChanged( const QString& ) ), this, SLOT( updateDataDefinedProperty() ) );
connect( mAtlasMarginDDBtn, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( updateDataDefinedProperty() ) );

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

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

//initialise buttons to use atlas coverage layer
mScaleDDBtn->init( vl, mComposerMap->dataDefinedProperty( QgsComposerObject::MapScale ),
Expand All @@ -210,6 +214,8 @@ void QgsComposerMapWidget::populateDataDefinedButtons()
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::doubleDesc() );
mAtlasMarginDDBtn->init( vl, mComposerMap->dataDefinedProperty( QgsComposerObject::MapAtlasMargin ),
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::doubleDesc() );
mLayersDDBtn->init( vl, mComposerMap->dataDefinedProperty( QgsComposerObject::MapLayers ),
QgsDataDefinedButton::String, tr( "list of map layer names separated by | characters" ) );

//unblock signals from data defined buttons
mScaleDDBtn->blockSignals( false );
Expand All @@ -219,6 +225,7 @@ void QgsComposerMapWidget::populateDataDefinedButtons()
mXMaxDDBtn->blockSignals( false );
mYMaxDDBtn->blockSignals( false );
mAtlasMarginDDBtn->blockSignals( false );
mLayersDDBtn->blockSignals( false );
}

QgsComposerObject::DataDefinedProperty QgsComposerMapWidget::ddPropertyForWidget( QgsDataDefinedButton* widget )
Expand Down Expand Up @@ -251,7 +258,10 @@ QgsComposerObject::DataDefinedProperty QgsComposerMapWidget::ddPropertyForWidget
{
return QgsComposerObject::MapAtlasMargin;
}

else if ( widget == mLayersDDBtn )
{
return QgsComposerObject::MapLayers;
}
return QgsComposerObject::NoProperty;
}

Expand Down
18 changes: 18 additions & 0 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void QgsComposerMap::init()
mDataDefinedNames.insert( QgsComposerObject::MapXMax, QString( "dataDefinedMapXMax" ) );
mDataDefinedNames.insert( QgsComposerObject::MapYMax, QString( "dataDefinedMapYMax" ) );
mDataDefinedNames.insert( QgsComposerObject::MapAtlasMargin, QString( "dataDefinedMapAtlasMargin" ) );
mDataDefinedNames.insert( QgsComposerObject::MapLayers, QString( "dataDefinedMapLayers" ) );
}

void QgsComposerMap::updateToolTip()
Expand Down Expand Up @@ -532,6 +533,23 @@ QStringList QgsComposerMap::layersToRender() const
renderLayerSet = mComposition->mapSettings().layers();
}

QVariant exprVal;
if ( dataDefinedEvaluate( QgsComposerObject::MapLayers, exprVal ) )
{
renderLayerSet.clear();

QStringList layerNames = exprVal.toString().split( "|" );
//need to convert layer names to layer ids
Q_FOREACH ( QString name, layerNames )
{
QList< QgsMapLayer* > matchingLayers = QgsMapLayerRegistry::instance()->mapLayersByName( name );
Q_FOREACH ( QgsMapLayer* layer, matchingLayers )
{
renderLayerSet << layer->id();
}
}
}

//remove atlas coverage layer if required
//TODO - move setting for hiding coverage layer to map item properties
if ( mComposition->atlasMode() != QgsComposition::AtlasOff )
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void refreshMapExtents();

friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
friend class TestQgsComposerMap;
};
Q_NOWARN_DEPRECATED_POP

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void QgsComposerObject::refreshDataDefinedProperty( const DataDefinedProperty pr
//nothing to do in base class for now
}

bool QgsComposerObject::dataDefinedEvaluate( const DataDefinedProperty property, QVariant &expressionValue )
bool QgsComposerObject::dataDefinedEvaluate( const DataDefinedProperty property, QVariant &expressionValue ) const
{
if ( !mComposition )
{
Expand Down
6 changes: 4 additions & 2 deletions src/core/composer/qgscomposerobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class CORE_EXPORT QgsComposerObject: public QObject
MapXMax, /*!< map extent x maximum */
MapYMax, /*!< map extent y maximum */
MapAtlasMargin, /*!< map atlas margin*/
MapLayers, /*!< map layer set*/
//composer picture
PictureSource, /*!< picture source url */
//html item
Expand Down Expand Up @@ -189,7 +190,7 @@ class CORE_EXPORT QgsComposerObject: public QObject
* @param expressionValue QVariant for storing the evaluated value
* @note this method was added in version 2.5
*/
bool dataDefinedEvaluate( const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue );
bool dataDefinedEvaluate( const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue ) const;

signals:
/** Emitted when the item changes. Signifies that the item widgets must update the
Expand All @@ -206,7 +207,8 @@ class CORE_EXPORT QgsComposerObject: public QObject
private:

/** Map of current data defined properties*/
QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* > mDataDefinedProperties;
//mutable since expressions in data defineds need to be preparable
mutable QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* > mDataDefinedProperties;

friend class TestQgsComposerObject;
};
Expand Down
109 changes: 65 additions & 44 deletions src/ui/qgscomposermapwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
Expand Down Expand Up @@ -55,8 +64,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>447</width>
<height>2546</height>
<width>446</width>
<height>2490</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -81,7 +90,7 @@
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0" colspan="2">
<item row="0" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QComboBox" name="mPreviewModeComboBox">
Expand All @@ -102,6 +111,13 @@
</item>
</layout>
</item>
<item row="4" column="0" colspan="3">
<widget class="QCheckBox" name="mDrawCanvasItemsCheckBox">
<property name="text">
<string>Draw map canvas items</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
Expand All @@ -112,7 +128,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="1" column="2">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLineEdit" name="mScaleLineEdit">
Expand All @@ -130,42 +146,7 @@
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="mMapRotationLabel">
<property name="text">
<string>Map rotation</string>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QgsDoubleSpinBox" name="mMapRotationSpinBox">
<property name="suffix">
<string> °</string>
</property>
<property name="maximum">
<double>360.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QgsDataDefinedButton" name="mMapRotationDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="mDrawCanvasItemsCheckBox">
<property name="text">
<string>Draw map canvas items</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<item row="5" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<widget class="QCheckBox" name="mKeepLayerListCheckBox">
Expand Down Expand Up @@ -203,15 +184,54 @@
</property>
</widget>
</item>
<item>
<widget class="QgsDataDefinedButton" name="mLayersDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="0" colspan="2">
<widget class="QCheckBox" name="mKeepLayerStylesCheckBox">
<item row="2" column="0">
<widget class="QLabel" name="mMapRotationLabel">
<property name="text">
<string>Lock layer styles for map item</string>
<string>Map rotation</string>
</property>
</widget>
</item>
<item row="2" column="2">
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QgsDoubleSpinBox" name="mMapRotationSpinBox">
<property name="suffix">
<string> °</string>
</property>
<property name="maximum">
<double>360.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QgsDataDefinedButton" name="mMapRotationDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="8" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<widget class="QCheckBox" name="mKeepLayerStylesCheckBox">
<property name="text">
<string>Lock layer styles for map item</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -1416,6 +1436,7 @@
<tabstop>mDrawCanvasItemsCheckBox</tabstop>
<tabstop>mKeepLayerListCheckBox</tabstop>
<tabstop>mLayerListFromPresetButton</tabstop>
<tabstop>mLayersDDBtn</tabstop>
<tabstop>mKeepLayerStylesCheckBox</tabstop>
<tabstop>mExtentsCheckBox</tabstop>
<tabstop>mXMinLineEdit</tabstop>
Expand Down
Loading

0 comments on commit 7527638

Please sign in to comment.