Skip to content
Permalink
Browse files

[25d] Improve convertability to other layers

 * Move height and angle expressions for 2.5D renderer to layer
 * Apply color based on main symbol color

This makes the transition to other renderers easy.

Fixes #14132
  • Loading branch information
m-kuhn committed Jan 21, 2016
1 parent aca4999 commit 8d72f13a5750f9f5d499bc182c9531730e06a515
@@ -391,6 +391,8 @@ class QgsExpressionContext
static const QString EXPR_FIELDS;
static const QString EXPR_FEATURE;
static const QString EXPR_ORIGINAL_VALUE;
static const QString EXPR_SYMBOL_COLOR;
static const QString EXPR_SYMBOL_ANGLE;
};

/** \ingroup core
@@ -488,6 +490,16 @@ class QgsExpressionContextUtils
*/
static QgsExpressionContextScope* mapSettingsScope( const QgsMapSettings &mapSettings ) /Factory/;

/**
* Updates a symbol scope related to a QgsSymbolV2 to an expression context. If there is no existing scope
* provided, a new one will be generated and returned.
* @param symbol symbol to extract properties from
* @param symbolScope optional pointer to an existing scope to update
* @note added in QGIS 2.14
*/
static QgsExpressionContextScope* updateSymbolScope( const QgsSymbolV2* symbol, QgsExpressionContextScope* symbolScope = nullptr ) /Factory/;


/** Creates a new scope which contains variables and functions relating to a QgsComposition.
* For instance, number of pages and page sizes.
* @param composition source composition
@@ -39,24 +39,6 @@ class Qgs25DRenderer : QgsFeatureRendererV2
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );
virtual QgsSymbolV2List symbols( QgsRenderContext& context );

/**
* Get the field or expression used to determine the height of extrusion
*/
QgsDataDefined height() const;
/**
* Set the field or expression used to determine the height of extrusion
*/
void setHeight( const QgsDataDefined& height );

/**
* Get the angle for the extrusion effect
*/
int angle() const;
/**
* Set the angle for the extrusion effect
*/
void setAngle( int angle );

/**
* Get the roof color
*/
@@ -77,6 +59,16 @@ class Qgs25DRenderer : QgsFeatureRendererV2
*/
void setWallColor( const QColor& wallColor );

/**
* Set wall shading enabled
*/
void setWallShadingEnabled( bool enabled );

/**
* Get wall shading enabled
*/
bool wallShadingEnabled();

/**
* Get the shadow's color
*/
@@ -35,4 +35,8 @@ class Qgs25DRendererWidget : QgsRendererV2Widget
Qgs25DRendererWidget( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer );

QgsFeatureRendererV2* renderer();

private:
void apply();

};
@@ -15,6 +15,14 @@ class QgsRendererV2PropertiesDialog : QDialog
*/
void setMapCanvas( QgsMapCanvas* canvas );

signals:
/**
* Emitted when expression context variables on the associated
* vector layers have been changed. Will request the parent dialog
* to re-synchronize with the variables.
*/
void layerVariablesChanged();

public slots:
//! called when user changes renderer type
void rendererChanged();
@@ -34,6 +34,19 @@ class QgsRendererV2Widget : QWidget
*/
const QgsVectorLayer* vectorLayer() const;

/**
* This method should be called whenever the renderer is actually set on the layer.
*/
void applyChanges();

signals:
/**
* Emitted when expression context variables on the associated
* vector layers have been changed. Will request the parent dialog
* to re-synchronize with the variables.
*/
void layerVariablesChanged();

protected:
/** Subclasses may provide the capability of changing multiple symbols at once by implementing the following two methods
and by connecting the slot contextMenuViewCategories(const QPoint&)*/
@@ -58,6 +71,13 @@ class QgsRendererV2Widget : QWidget
virtual void copy();
virtual void paste();

private:
/**
* This will be called whenever the renderer is set on a layer.
* This can be overwritten in subclasses.
*/
virtual void apply();

};


@@ -50,9 +50,11 @@ static QgsExpressionContext _getExpressionContext( const void* context )
if ( layer )
expContext << QgsExpressionContextUtils::layerScope( layer );

expContext << QgsExpressionContextUtils::updateSymbolScope( nullptr );

//TODO - show actual value
expContext.setOriginalValueVariable( QVariant() );
expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE );
expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << QgsExpressionContext::EXPR_SYMBOL_COLOR );

return expContext;
}
@@ -1263,10 +1263,12 @@ void QgsVectorLayerProperties::updateSymbologyPage()
mRendererDialog = new QgsRendererV2PropertiesDialog( layer, QgsStyleV2::defaultStyle(), true );
mRendererDialog->setMapCanvas( QgisApp::instance()->mapCanvas() );

connect( mRendererDialog, SIGNAL( layerVariablesChanged() ), this, SLOT( updateVariableEditor() ) );

// display the menu to choose the output format (fix #5136)
mActionSaveStyleAs->setText( tr( "Save Style" ) );
mActionSaveStyleAs->setMenu( mSaveAsMenu );
QObject::disconnect( mActionSaveStyleAs, SIGNAL( triggered() ), this, SLOT( saveStyleAs_clicked() ) );
disconnect( mActionSaveStyleAs, SIGNAL( triggered() ), this, SLOT( saveStyleAs_clicked() ) );
}
else
{
@@ -4546,6 +4546,9 @@ void QgsExpression::initVariableHelp()
//symbol variables
gVariableHelpTexts.insert( "geometry_part_count", QCoreApplication::translate( "variable_help", "Number of parts in rendered feature's geometry." ) );
gVariableHelpTexts.insert( "geometry_part_num", QCoreApplication::translate( "variable_help", "Current geometry part number for feature being rendered." ) );

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

QString QgsExpression::variableHelpText( const QString &variableName, bool showValue, const QVariant &value )
@@ -33,6 +33,8 @@
const QString QgsExpressionContext::EXPR_FIELDS( "_fields_" );
const QString QgsExpressionContext::EXPR_FEATURE( "_feature_" );
const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( "value" );
const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( "symbol_color" );
const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( "symbol_angle" );

//
// QgsExpressionContextScope
@@ -716,6 +718,24 @@ QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope( const Qg
return scope;
}

QgsExpressionContextScope* QgsExpressionContextUtils::updateSymbolScope( const QgsSymbolV2* symbol, QgsExpressionContextScope* symbolScope )
{
if ( !symbolScope )
symbolScope = new QgsExpressionContextScope( QObject::tr( "Symbol Scope" ) );

symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbol ? symbol->color() : QColor(), true ) );

double angle = 0.0;
const QgsMarkerSymbolV2* markerSymbol = dynamic_cast< const QgsMarkerSymbolV2* >( symbol );
if ( markerSymbol )
{
angle = markerSymbol->angle();
}
symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_ANGLE, angle, true ) );

return symbolScope;
}

QgsExpressionContextScope *QgsExpressionContextUtils::compositionScope( const QgsComposition *composition )
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composition" ) );
@@ -28,6 +28,7 @@ class QgsComposition;
class QgsComposerItem;
class QgsAtlasComposition;
class QgsMapSettings;
class QgsSymbolV2;

/** \ingroup core
* \class QgsScopedExpressionFunction
@@ -425,6 +426,8 @@ class CORE_EXPORT QgsExpressionContext
static const QString EXPR_FIELDS;
static const QString EXPR_FEATURE;
static const QString EXPR_ORIGINAL_VALUE;
static const QString EXPR_SYMBOL_COLOR;
static const QString EXPR_SYMBOL_ANGLE;

private:

@@ -519,6 +522,16 @@ class CORE_EXPORT QgsExpressionContextUtils
*/
static QgsExpressionContextScope* mapSettingsScope( const QgsMapSettings &mapSettings );

/**
* Updates a symbol scope related to a QgsSymbolV2 to an expression context. If there is no existing scope
* provided, a new one will be generated and returned.
* @param symbol symbol to extract properties from
* @param symbolScope optional pointer to an existing scope to update
* @note added in QGIS 2.14
*/
static QgsExpressionContextScope* updateSymbolScope( const QgsSymbolV2* symbol, QgsExpressionContextScope* symbolScope = nullptr );


/** Creates a new scope which contains variables and functions relating to a QgsComposition.
* For instance, number of pages and page sizes.
* @param composition source composition
@@ -281,19 +281,33 @@ QList<QgsLabelFeature*> QgsVectorLayerLabelProvider::labelFeatures( QgsRenderCon
request.setSubsetOfAttributes( attrNames, mFields );
QgsFeatureIterator fit = mSource->getFeatures( request );

QgsExpressionContextScope* symbolScope = nullptr;
QgsFeature fet;
while ( fit.nextFeature( fet ) )
{
QScopedPointer<QgsGeometry> obstacleGeometry;
if ( fet.constGeometry()->type() == QGis::Point )
if ( mRenderer )
{
//point feature, use symbol bounds as obstacle
obstacleGeometry.reset( getPointObstacleGeometry( fet, ctx, mRenderer ) );
QgsSymbolV2List symbols = mRenderer->originalSymbolsForFeature( fet, ctx );
if ( !symbols.isEmpty() && fet.constGeometry()->type() == QGis::Point )
{
//point feature, use symbol bounds as obstacle
obstacleGeometry.reset( QgsVectorLayerLabelProvider::getPointObstacleGeometry( fet, ctx, symbols ) );
}
if ( !symbols.isEmpty() )
{
symbolScope = QgsExpressionContextUtils::updateSymbolScope( symbols.at( 0 ), symbolScope );
if ( !ctx.expressionContext().scopes().contains( symbolScope ) )
ctx.expressionContext().appendScope( symbolScope );
}
}
ctx.expressionContext().setFeature( fet );
registerFeature( fet, ctx, obstacleGeometry.data() );
}

if ( ctx.expressionContext().lastScope() == symbolScope )
ctx.expressionContext().popScope();

if ( mRenderer )
mRenderer->stopRender( ctx );

@@ -308,14 +322,11 @@ void QgsVectorLayerLabelProvider::registerFeature( QgsFeature& feature, QgsRende
mLabels << label;
}

QgsGeometry* QgsVectorLayerLabelProvider::getPointObstacleGeometry( QgsFeature& fet, QgsRenderContext& context, QgsFeatureRendererV2* renderer )
QgsGeometry* QgsVectorLayerLabelProvider::getPointObstacleGeometry( QgsFeature& fet, QgsRenderContext& context, const QgsSymbolV2List& symbols )
{
if ( !fet.constGeometry() || fet.constGeometry()->isEmpty() || fet.constGeometry()->type() != QGis::Point || !renderer )
if ( !fet.constGeometry() || fet.constGeometry()->isEmpty() || fet.constGeometry()->type() != QGis::Point )
return nullptr;

//calculate bounds for symbols for feature
QgsSymbolV2List symbols = renderer->originalSymbolsForFeature( fet, context );

bool isMultiPoint = fet.constGeometry()->geometry()->nCoordinates() > 1;
QgsAbstractGeometryV2* obstacleGeom = nullptr;
if ( isMultiPoint )
@@ -17,9 +17,11 @@
#define QGSVECTORLAYERLABELPROVIDER_H

#include "qgslabelingenginev2.h"
#include "qgsrendererv2.h"

class QgsAbstractFeatureSource;
class QgsFeatureRendererV2;
class QgsSymbolV2;

/**
* @brief The QgsVectorLayerLabelProvider class implements a label provider
@@ -80,10 +82,10 @@ class CORE_EXPORT QgsVectorLayerLabelProvider : public QgsAbstractLabelProvider
* point, and ensures that labels will not overlap large or offset points.
* @param fet point feature
* @param context render context
* @param renderer renderer used for layer, required to determine symbols rendered for point feature
* @param symbols symbols rendered for point feature
* @note added in QGIS 2.14
*/
static QgsGeometry* getPointObstacleGeometry( QgsFeature& fet, QgsRenderContext& context, QgsFeatureRendererV2* renderer );
static QgsGeometry* getPointObstacleGeometry( QgsFeature& fet, QgsRenderContext& context, const QgsSymbolV2List& symbols );

protected:
//! initialization method - called from constructors
@@ -286,6 +286,9 @@ void QgsVectorLayerRenderer::setGeometryCachePointer( QgsGeometryCache* cache )

void QgsVectorLayerRenderer::drawRendererV2( QgsFeatureIterator& fit )
{
QgsExpressionContextScope* symbolScope = QgsExpressionContextUtils::updateSymbolScope( nullptr );
mContext.expressionContext().appendScope( symbolScope );

QgsFeature fet;
while ( fit.nextFeature( fet ) )
{
@@ -332,10 +335,18 @@ void QgsVectorLayerRenderer::drawRendererV2( QgsFeatureIterator& fit )
if ( mContext.labelingEngineV2() )
{
QScopedPointer<QgsGeometry> obstacleGeometry;
if ( fet.constGeometry()->type() == QGis::Point )
QgsSymbolV2List symbols = mRendererV2->originalSymbolsForFeature( fet, mContext );

if ( !symbols.isEmpty() && fet.constGeometry()->type() == QGis::Point )
{
obstacleGeometry.reset( QgsVectorLayerLabelProvider::getPointObstacleGeometry( fet, mContext, mRendererV2 ) );
obstacleGeometry.reset( QgsVectorLayerLabelProvider::getPointObstacleGeometry( fet, mContext, symbols ) );
}

if ( !symbols.isEmpty() )
{
QgsExpressionContextUtils::updateSymbolScope( symbols.at( 0 ), symbolScope );
}

if ( mLabelProvider )
{
mLabelProvider->registerFeature( fet, mContext, obstacleGeometry.data() );
@@ -355,6 +366,8 @@ void QgsVectorLayerRenderer::drawRendererV2( QgsFeatureIterator& fit )
}
}

mContext.expressionContext().popScope();

stopRendererV2( nullptr );
}

@@ -371,6 +384,9 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )
selRenderer->startRender( mContext, mFields );
}

QgsExpressionContextScope* symbolScope = QgsExpressionContextUtils::updateSymbolScope( nullptr );
mContext.expressionContext().appendScope( symbolScope );

// 1. fetch features
QgsFeature fet;
while ( fit.nextFeature( fet ) )
@@ -382,6 +398,7 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )
{
qDebug( "rendering stop!" );
stopRendererV2( selRenderer );
mContext.expressionContext().popScope();
return;
}

@@ -420,10 +437,18 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )
if ( mContext.labelingEngineV2() )
{
QScopedPointer<QgsGeometry> obstacleGeometry;
if ( fet.constGeometry()->type() == QGis::Point )
QgsSymbolV2List symbols = mRendererV2->originalSymbolsForFeature( fet, mContext );

if ( !symbols.isEmpty() && fet.constGeometry()->type() == QGis::Point )
{
obstacleGeometry.reset( QgsVectorLayerLabelProvider::getPointObstacleGeometry( fet, mContext, mRendererV2 ) );
obstacleGeometry.reset( QgsVectorLayerLabelProvider::getPointObstacleGeometry( fet, mContext, symbols ) );
}

if ( !symbols.isEmpty() )
{
QgsExpressionContextUtils::updateSymbolScope( symbols.at( 0 ), symbolScope );
}

if ( mLabelProvider )
{
mLabelProvider->registerFeature( fet, mContext, obstacleGeometry.data() );
@@ -435,6 +460,8 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )
}
}

mContext.expressionContext().popScope();

// find out the order
QgsSymbolV2LevelOrder levels;
QgsSymbolV2List symbols = mRendererV2->symbols( mContext );

2 comments on commit 8d72f13

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn replied Jan 22, 2016

@m-kuhn working great.

Applying a gradient fill of type radial with coord mode set to viewport is real nice :)
radial-gradient png

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Jan 22, 2016

@nirvn hint: use the right hand rule to digitize (or find a toll that converts the data for you)

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