Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Expose renderer variables to users
  • Loading branch information
nyalldawson committed Sep 29, 2016
1 parent 1725182 commit 962a4e9
Show file tree
Hide file tree
Showing 26 changed files with 223 additions and 5 deletions.
4 changes: 4 additions & 0 deletions python/core/qgsexpressioncontext.sip
Expand Up @@ -457,6 +457,10 @@ class QgsExpressionContext
static const QString EXPR_GEOMETRY_PART_COUNT;
//! Inbuilt variable name for geometry part number variable
static const QString EXPR_GEOMETRY_PART_NUM;
//! Inbuilt variable name for cluster size variable
static const QString EXPR_CLUSTER_SIZE;
//! Inbuilt variable name for cluster color variable
static const QString EXPR_CLUSTER_COLOR;
};

/** \ingroup core
Expand Down
8 changes: 8 additions & 0 deletions python/gui/symbology-ng/qgslayerpropertieswidget.sip
Expand Up @@ -15,6 +15,13 @@ class QgsLayerPropertiesWidget : QgsPanelWidget
*/
QgsExpressionContext* expressionContext() const;

/** Sets a list of additional expression context scopes to show as available within the layer.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
* @see setExpressionContext()
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
Expand All @@ -33,6 +40,7 @@ class QgsLayerPropertiesWidget : QgsPanelWidget
* be kept alive for the lifetime of the properties widget.
* @note added in QGIS 2.12
* @see expressionContext()
* @see setAdditionalExpressionContextScopes()
*/
void setExpressionContext( QgsExpressionContext* context );

Expand Down
12 changes: 12 additions & 0 deletions python/gui/symbology-ng/qgsrendererwidget.sip
Expand Up @@ -29,6 +29,12 @@ class QgsRendererWidget : QgsPanelWidget
*/
const QgsMapCanvas* mapCanvas() const;

/** Sets a list of additional expression context scopes to show as available within the renderer.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
Expand Down Expand Up @@ -118,6 +124,12 @@ class QgsDataDefinedValueDialog : QDialog
*/
const QgsVectorLayer* vectorLayer() const;

/** Sets a list of additional expression context scopes to show as available for the symbol.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

public slots:
void dataDefinedChanged();

Expand Down
8 changes: 8 additions & 0 deletions python/gui/symbology-ng/qgssymbollayerwidget.sip
Expand Up @@ -19,6 +19,13 @@ class QgsSymbolLayerWidget : QWidget
*/
QgsExpressionContext* expressionContext() const;

/** Sets a list of additional expression context scopes to show as available within the layer.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
* @see setExpressionContext()
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
Expand Down Expand Up @@ -47,6 +54,7 @@ class QgsSymbolLayerWidget : QWidget
* be kept alive for the lifetime of the layer widget.
* @note added in QGIS 2.12
* @see expressionContext()
* @see setAdditionalExpressionContextScopes()
*/
void setExpressionContext( QgsExpressionContext* context );

Expand Down
16 changes: 16 additions & 0 deletions python/gui/symbology-ng/qgssymbolselectordialog.sip
Expand Up @@ -16,6 +16,7 @@ class QgsSymbolSelectorWidget : QgsPanelWidget
* @param context expression context pointer. Ownership is transferred to the dialog.
* @note added in QGIS 2.12
* @see expressionContext()
* @see setAdditionalExpressionContextScopes()
*/
void setExpressionContext( QgsExpressionContext* context /Transfer/ );

Expand All @@ -27,6 +28,13 @@ class QgsSymbolSelectorWidget : QgsPanelWidget
*/
QgsExpressionContext* expressionContext() const;

/** Sets a list of additional expression context scopes to show as available for the symbol.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
* @see setExpressionContext()
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

/** Sets the map canvas associated with the dialog. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
Expand Down Expand Up @@ -106,6 +114,7 @@ class QgsSymbolSelectorDialog : QDialog
* @param context expression context pointer. Ownership is transferred to the dialog.
* @note added in QGIS 2.12
* @see expressionContext()
* @see setAdditionalExpressionContextScopes()
*/
void setExpressionContext( QgsExpressionContext* context /Transfer/ );

Expand All @@ -117,6 +126,13 @@ class QgsSymbolSelectorDialog : QDialog
*/
QgsExpressionContext* expressionContext() const;

/** Sets a list of additional expression context scopes to show as available within the symbol.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
* @see setExpressionContext()
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

/** Sets the map canvas associated with the dialog. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
Expand Down
6 changes: 6 additions & 0 deletions python/gui/symbology-ng/qgssymbolslistwidget.sip
Expand Up @@ -14,6 +14,12 @@ class QgsSymbolsListWidget : QWidget
*/
QgsExpressionContext* expressionContext() const;

/** Sets a list of additional expression context scopes to show as available within the symbol.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -5094,6 +5094,10 @@ void QgsExpression::initVariableHelp()

gVariableHelpTexts.insert( "symbol_color", QCoreApplication::translate( "symbol_color", "Color of symbol used to render the feature." ) );
gVariableHelpTexts.insert( "symbol_angle", QCoreApplication::translate( "symbol_angle", "Angle of symbol used to render the feature (valid for marker symbols only)." ) );

//cluster variables
gVariableHelpTexts.insert( "cluster_color", QCoreApplication::translate( "cluster_color", "Color of symbols within a cluster, or NULL if symbols have mixed colors." ) );
gVariableHelpTexts.insert( "cluster_size", QCoreApplication::translate( "cluster_size", "Number of symbols contained within a cluster." ) );
}

QString QgsExpression::variableHelpText( const QString &variableName, bool showValue, const QVariant &value )
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -40,6 +40,8 @@ const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( "geometry_part_cou
const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( "geometry_part_num" );
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( "geometry_point_count" );
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( "geometry_point_num" );
const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( "cluster_size" );
const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( "cluster_color" );

//
// QgsExpressionContextScope
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsexpressioncontext.h
Expand Up @@ -503,6 +503,10 @@ class CORE_EXPORT QgsExpressionContext
static const QString EXPR_GEOMETRY_POINT_COUNT;
//! Inbuilt variable name for point number variable
static const QString EXPR_GEOMETRY_POINT_NUM;
//! Inbuilt variable name for cluster size variable
static const QString EXPR_CLUSTER_SIZE;
//! Inbuilt variable name for cluster color variable
static const QString EXPR_CLUSTER_COLOR;

private:

Expand Down
6 changes: 3 additions & 3 deletions src/core/symbology-ng/qgspointdistancerenderer.cpp
Expand Up @@ -438,15 +438,15 @@ QgsExpressionContextScope* QgsPointDistanceRenderer::createGroupScope( const Clu

if ( groupColor.isValid() )
{
clusterScope->setVariable( "cluster_color", QgsSymbolLayerUtils::encodeColor( groupColor ) );
clusterScope->setVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, QgsSymbolLayerUtils::encodeColor( groupColor ) );
}
else
{
//mixed colors
clusterScope->setVariable( "cluster_color", "" );
clusterScope->setVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "" );
}

clusterScope->setVariable( "cluster_size", group.size() );
clusterScope->setVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, group.size() );
}
return clusterScope;
}
Expand Down
3 changes: 3 additions & 0 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererwidget.cpp
Expand Up @@ -528,6 +528,7 @@ void QgsCategorizedSymbolRendererWidget::changeSelectedSymbols()
QgsSymbol* newSymbol = mCategorizedSymbol->clone();
QgsSymbolSelectorDialog dlg( newSymbol, mStyle, mLayer, this );
dlg.setMapCanvas( mMapCanvas );
dlg.setAdditionalExpressionContextScopes( mAdditionalScopes );
if ( !dlg.exec() )
{
delete newSymbol;
Expand All @@ -550,6 +551,7 @@ void QgsCategorizedSymbolRendererWidget::changeCategorizedSymbol()
QgsSymbol* newSymbol = mCategorizedSymbol->clone();
QgsSymbolSelectorWidget* dlg = new QgsSymbolSelectorWidget( newSymbol, mStyle, mLayer, nullptr );
dlg->setMapCanvas( mMapCanvas );
dlg->setAdditionalExpressionContextScopes( mAdditionalScopes );

connect( dlg, SIGNAL( widgetChanged() ), this, SLOT( updateSymbolsFromWidget() ) );
connect( dlg, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( cleanUpSymbolSelector( QgsPanelWidget* ) ) );
Expand Down Expand Up @@ -594,6 +596,7 @@ void QgsCategorizedSymbolRendererWidget::changeCategorySymbol()

QgsSymbolSelectorWidget* dlg = new QgsSymbolSelectorWidget( symbol, mStyle, mLayer, nullptr );
dlg->setMapCanvas( mMapCanvas );
dlg->setAdditionalExpressionContextScopes( mAdditionalScopes );
connect( dlg, SIGNAL( widgetChanged() ), this, SLOT( updateSymbolsFromWidget() ) );
connect( dlg, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( cleanUpSymbolSelector( QgsPanelWidget* ) ) );
openPanel( dlg );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/symbology-ng/qgsgraduatedsymbolrendererwidget.cpp
Expand Up @@ -830,6 +830,7 @@ void QgsGraduatedSymbolRendererWidget::changeGraduatedSymbol()
QgsSymbol* newSymbol = mGraduatedSymbol->clone();
QgsSymbolSelectorWidget* dlg = new QgsSymbolSelectorWidget( newSymbol, mStyle, mLayer, nullptr );
dlg->setMapCanvas( mMapCanvas );
dlg->setAdditionalExpressionContextScopes( mAdditionalScopes );

connect( dlg, SIGNAL( widgetChanged() ), this, SLOT( updateSymbolsFromWidget() ) );
connect( dlg, SIGNAL( accepted( QgsPanelWidget* ) ), this, SLOT( cleanUpSymbolSelector( QgsPanelWidget* ) ) );
Expand Down Expand Up @@ -908,6 +909,7 @@ void QgsGraduatedSymbolRendererWidget::changeRangeSymbol( int rangeIdx )
QgsSymbol* newSymbol = mRenderer->ranges()[rangeIdx].symbol()->clone();
QgsSymbolSelectorWidget* dlg = new QgsSymbolSelectorWidget( newSymbol, mStyle, mLayer, nullptr );
dlg->setMapCanvas( mMapCanvas );
dlg->setAdditionalExpressionContextScopes( mAdditionalScopes );

connect( dlg, SIGNAL( widgetChanged() ), this, SLOT( updateSymbolsFromWidget() ) );
connect( dlg, SIGNAL( accepted( QgsPanelWidget* ) ), this, SLOT( cleanUpSymbolSelector( QgsPanelWidget* ) ) );
Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology-ng/qgsinvertedpolygonrendererwidget.cpp
Expand Up @@ -126,6 +126,7 @@ void QgsInvertedPolygonRendererWidget::on_mRendererComboBox_currentIndexChanged(
mEmbeddedRendererWidget.reset( m->createRendererWidget( mLayer, mStyle, const_cast<QgsFeatureRenderer*>( mRenderer->embeddedRenderer() )->clone() ) );
connect( mEmbeddedRendererWidget.data(), SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );
mEmbeddedRendererWidget->setMapCanvas( mMapCanvas );
mEmbeddedRendererWidget->setAdditionalExpressionContextScopes( mAdditionalScopes );

if ( layout()->count() > 2 )
{
Expand Down
10 changes: 10 additions & 0 deletions src/gui/symbology-ng/qgslayerpropertieswidget.cpp
Expand Up @@ -126,6 +126,15 @@ QgsLayerPropertiesWidget::QgsLayerPropertiesWidget( QgsSymbolLayer* layer, const
mEffectWidget->setPaintEffect( mLayer->paintEffect() );
}

void QgsLayerPropertiesWidget::setAdditionalExpressionContextScopes( const QList<QgsExpressionContextScope>& scopes )
{
mAdditionalScopes = scopes;

QgsSymbolLayerWidget* w = dynamic_cast< QgsSymbolLayerWidget* >( stackedWidget->currentWidget() );
if ( w )
w->setAdditionalExpressionContextScopes( mAdditionalScopes );
}

void QgsLayerPropertiesWidget::setMapCanvas( QgsMapCanvas *canvas )
{
mMapCanvas = canvas;
Expand Down Expand Up @@ -191,6 +200,7 @@ void QgsLayerPropertiesWidget::updateSymbolLayerWidget( QgsSymbolLayer* layer )
if ( w )
{
w->setExpressionContext( mPresetExpressionContext );
w->setAdditionalExpressionContextScopes( mAdditionalScopes );
if ( mMapCanvas )
w->setMapCanvas( mMapCanvas );
w->setSymbolLayer( layer );
Expand Down
11 changes: 10 additions & 1 deletion src/gui/symbology-ng/qgslayerpropertieswidget.h
Expand Up @@ -17,14 +17,14 @@
#define QGSLAYERPROPERTIESWIDGET_H

#include "ui_widget_layerproperties.h"
#include "qgsexpressioncontext.h"

class QgsSymbol;
class QgsSymbolLayer;
class QgsSymbolLayerWidget;
class QgsVectorLayer;
class QgsMapCanvas;
class QgsPanelWidget;
class QgsExpressionContext;

class SymbolLayerItem;

Expand All @@ -49,6 +49,13 @@ class GUI_EXPORT QgsLayerPropertiesWidget : public QgsPanelWidget, private Ui::L
*/
QgsExpressionContext* expressionContext() const { return mPresetExpressionContext; }

/** Sets a list of additional expression context scopes to show as available within the layer.
* @param scopes list of additional scopes which will be added in order to the end of the default expression context
* @note added in QGIS 3.0
* @see setExpressionContext()
*/
void setAdditionalExpressionContextScopes( const QList< QgsExpressionContextScope >& scopes );

/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
Expand All @@ -74,6 +81,7 @@ class GUI_EXPORT QgsLayerPropertiesWidget : public QgsPanelWidget, private Ui::L
* be kept alive for the lifetime of the properties widget.
* @note added in QGIS 2.12
* @see expressionContext()
* @see setAdditionalExpressionContextScopes()
*/
void setExpressionContext( QgsExpressionContext* context );

Expand All @@ -96,6 +104,7 @@ class GUI_EXPORT QgsLayerPropertiesWidget : public QgsPanelWidget, private Ui::L

private:
QgsExpressionContext* mPresetExpressionContext;
QList< QgsExpressionContextScope > mAdditionalScopes;
QgsMapCanvas* mMapCanvas;

};
Expand Down
12 changes: 12 additions & 0 deletions src/gui/symbology-ng/qgspointclusterrendererwidget.cpp
Expand Up @@ -135,6 +135,12 @@ void QgsPointClusterRendererWidget::on_mRendererSettingsButton_clicked()
{
QgsRendererWidget* w = m->createRendererWidget( mLayer, mStyle, mRenderer->embeddedRenderer()->clone() );
w->setMapCanvas( mMapCanvas );
QgsExpressionContextScope scope;
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "" );
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, 0 );
QList< QgsExpressionContextScope > scopes = mAdditionalScopes;
scopes << scope;
w->setAdditionalExpressionContextScopes( scopes );
connect( w, SIGNAL( widgetChanged() ), this, SLOT( updateRendererFromWidget() ) );
w->setDockMode( this->dockMode() );
openPanel( w );
Expand Down Expand Up @@ -178,6 +184,12 @@ void QgsPointClusterRendererWidget::on_mCenterSymbolPushButton_clicked()
QgsSymbolSelectorWidget* dlg = new QgsSymbolSelectorWidget( markerSymbol, QgsStyle::defaultStyle(), mLayer, this );
dlg->setDockMode( this->dockMode() );
dlg->setMapCanvas( mMapCanvas );
QgsExpressionContextScope scope;
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "" );
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, 0 );
QList< QgsExpressionContextScope > scopes = mAdditionalScopes;
scopes << scope;
dlg->setAdditionalExpressionContextScopes( scopes );

connect( dlg, SIGNAL( widgetChanged() ), this, SLOT( updateCenterSymbolFromWidget() ) );
connect( dlg, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( cleanUpSymbolSelector( QgsPanelWidget* ) ) );
Expand Down
13 changes: 13 additions & 0 deletions src/gui/symbology-ng/qgspointdisplacementrendererwidget.cpp
Expand Up @@ -214,6 +214,12 @@ void QgsPointDisplacementRendererWidget::on_mRendererSettingsButton_clicked()
{
QgsRendererWidget* w = m->createRendererWidget( mLayer, mStyle, mRenderer->embeddedRenderer()->clone() );
w->setMapCanvas( mMapCanvas );
QgsExpressionContextScope scope;
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "" );
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, 0 );
QList< QgsExpressionContextScope > scopes = mAdditionalScopes;
scopes << scope;
w->setAdditionalExpressionContextScopes( scopes );
connect( w, SIGNAL( widgetChanged() ), this, SLOT( updateRendererFromWidget() ) );
openPanel( w );
}
Expand Down Expand Up @@ -351,6 +357,13 @@ void QgsPointDisplacementRendererWidget::on_mCenterSymbolPushButton_clicked()
QgsMarkerSymbol* markerSymbol = mRenderer->centerSymbol()->clone();
QgsSymbolSelectorWidget* dlg = new QgsSymbolSelectorWidget( markerSymbol, QgsStyle::defaultStyle(), mLayer, this );
dlg->setMapCanvas( mMapCanvas );

QgsExpressionContextScope scope;
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "" );
scope.setVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, 0 );
QList< QgsExpressionContextScope > scopes = mAdditionalScopes;
scopes << scope;
dlg->setAdditionalExpressionContextScopes( scopes );
connect( dlg, SIGNAL( widgetChanged() ), this, SLOT( updateCenterSymbolFromWidget() ) );
connect( dlg, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( cleanUpSymbolSelector( QgsPanelWidget* ) ) );
openPanel( dlg );
Expand Down

0 comments on commit 962a4e9

Please sign in to comment.