Skip to content
Permalink
Browse files

[FEATURE][composer] Data defined control over map layers

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 7527638d436ac964159b3e67a87933ba53eb8f7a
@@ -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
@@ -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
@@ -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();
@@ -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 ),
@@ -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 );
@@ -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 )
@@ -251,7 +258,10 @@ QgsComposerObject::DataDefinedProperty QgsComposerMapWidget::ddPropertyForWidget
{
return QgsComposerObject::MapAtlasMargin;
}

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

@@ -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()
@@ -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 )
@@ -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

@@ -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 )
{
@@ -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
@@ -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
@@ -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;
};
@@ -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>
@@ -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">
@@ -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">
@@ -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">
@@ -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">
@@ -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">
@@ -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>
@@ -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>

0 comments on commit 7527638

Please sign in to comment.
You can’t perform that action at this time.