Skip to content
Permalink
Browse files

Port some symbol buttons across to QgsSymbolButton

- composer shape style button (no other composer ones for now- they're
all getting removed with layouts anyway)
- point cluster/displacement renderer buttons
  • Loading branch information
nyalldawson committed Jul 23, 2017
1 parent 22c4740 commit 46f6f83fb973d4f09b67ad6893c99e845d42881d
@@ -53,6 +53,7 @@ class QgsSymbolButton : QToolButton
:rtype: QgsSymbol
%End


QgsMapCanvas *mapCanvas() const;
%Docstring
Returns the map canvas associated with the widget.
@@ -11,7 +11,7 @@



class QgsPointClusterRendererWidget: QgsRendererWidget
class QgsPointClusterRendererWidget: QgsRendererWidget, QgsExpressionContextGenerator
{
%Docstring
A widget which allows configuration of the properties for a QgsPointClusterRenderer.
@@ -48,6 +48,9 @@ class QgsPointClusterRendererWidget: QgsRendererWidget
virtual void setContext( const QgsSymbolWidgetContext &context );


virtual QgsExpressionContext createExpressionContext() const;


};

/************************************************************************
@@ -10,7 +10,7 @@



class QgsPointDisplacementRendererWidget: QgsRendererWidget
class QgsPointDisplacementRendererWidget: QgsRendererWidget, QgsExpressionContextGenerator
{

%TypeHeaderCode
@@ -29,6 +29,9 @@ class QgsPointDisplacementRendererWidget: QgsRendererWidget
virtual void setContext( const QgsSymbolWidgetContext &context );


virtual QgsExpressionContext createExpressionContext() const;


};

/************************************************************************
@@ -52,7 +52,10 @@ QgsComposerShapeWidget::QgsComposerShapeWidget( QgsComposerShape *composerShape
if ( mComposerShape )
{
connect( mComposerShape, &QgsComposerObject::itemChanged, this, &QgsComposerShapeWidget::setGuiElementValues );
mShapeStyleButton->registerExpressionContextGenerator( mComposerShape );
}
connect( mShapeStyleButton, &QgsSymbolButton::changed, this, &QgsComposerShapeWidget::symbolChanged );
mShapeStyleButton->setLayer( atlasCoverageLayer() );
}

QgsComposerShapeWidget::~QgsComposerShapeWidget()
@@ -76,7 +79,7 @@ void QgsComposerShapeWidget::setGuiElementValues()

blockAllSignals( true );

updateShapeStyle();
mShapeStyleButton->setSymbol( mComposerShape->shapeStyleSymbol()->clone() );

mCornerRadiusSpinBox->setValue( mComposerShape->cornerRadius() );
if ( mComposerShape->shapeType() == QgsComposerShape::Ellipse )
@@ -98,38 +101,16 @@ void QgsComposerShapeWidget::setGuiElementValues()
blockAllSignals( false );
}

void QgsComposerShapeWidget::on_mShapeStyleButton_clicked()
void QgsComposerShapeWidget::symbolChanged()
{
if ( !mComposerShape )
{
return;
}

// use the atlas coverage layer, if any
QgsVectorLayer *coverageLayer = atlasCoverageLayer();

QgsFillSymbol *newSymbol = mComposerShape->shapeStyleSymbol()->clone();
QgsExpressionContext context = mComposerShape->createExpressionContext();

QgsSymbolSelectorWidget *d = new QgsSymbolSelectorWidget( newSymbol, QgsStyle::defaultStyle(), coverageLayer, nullptr );
QgsSymbolWidgetContext symbolContext;
symbolContext.setExpressionContext( &context );
d->setContext( symbolContext );

connect( d, &QgsPanelWidget::widgetChanged, this, &QgsComposerShapeWidget::updateSymbolFromWidget );
connect( d, &QgsPanelWidget::panelAccepted, this, &QgsComposerShapeWidget::cleanUpSymbolSelector );
openPanel( d );
mComposerShape->beginCommand( tr( "Shape style changed" ) );
}

void QgsComposerShapeWidget::updateShapeStyle()
{
if ( mComposerShape )
{
mComposerShape->refreshSymbol();
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( mComposerShape->shapeStyleSymbol(), mShapeStyleButton->iconSize() );
mShapeStyleButton->setIcon( icon );
}
mComposerShape->setShapeStyleSymbol( mShapeStyleButton->clonedSymbol<QgsFillSymbol>() );
mComposerShape->endCommand();
}

void QgsComposerShapeWidget::on_mCornerRadiusSpinBox_valueChanged( double val )
@@ -179,23 +160,3 @@ void QgsComposerShapeWidget::toggleRadiusSpin( const QString &shapeText )
mCornerRadiusSpinBox->setEnabled( false );
}
}

void QgsComposerShapeWidget::updateSymbolFromWidget()
{
if ( QgsSymbolSelectorWidget *w = qobject_cast<QgsSymbolSelectorWidget *>( sender() ) )
mComposerShape->setShapeStyleSymbol( static_cast< QgsFillSymbol * >( w->symbol() ) );
}

void QgsComposerShapeWidget::cleanUpSymbolSelector( QgsPanelWidget *container )
{
QgsSymbolSelectorWidget *w = qobject_cast<QgsSymbolSelectorWidget *>( container );
if ( !w )
return;

delete w->symbol();
updateShapeStyle();
mComposerShape->endCommand();
}



@@ -40,17 +40,13 @@ class QgsComposerShapeWidget: public QgsComposerItemBaseWidget, private Ui::QgsC
private slots:
void on_mShapeComboBox_currentIndexChanged( const QString &text );
void on_mCornerRadiusSpinBox_valueChanged( double val );
void on_mShapeStyleButton_clicked();
void symbolChanged();

//! Sets the GUI elements to the currentValues of mComposerShape
void setGuiElementValues();

void updateShapeStyle();

//! Enables or disables the rounded radius spin box based on shape type
void toggleRadiusSpin( const QString &shapeText );
void updateSymbolFromWidget();
void cleanUpSymbolSelector( QgsPanelWidget *container );
};

#endif // QGSCOMPOSERSHAPEWIDGET_H
@@ -72,6 +72,28 @@ class GUI_EXPORT QgsSymbolButton : public QToolButton
*/
QgsSymbol *symbol();

/**
* Returns a clone of the current symbol (as the specified template type) defined by the button.
* \see setSymbol()
* \see changed()
* \note Not available in Python bindings.
*/
template <class SymbolType> SymbolType *clonedSymbol() SIP_SKIP
{
QgsSymbol *tmpSymbol = mSymbol.get();
SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );

if ( symbolCastToType )
{
return symbolCastToType->clone();
}
else
{
//could not cast
return nullptr;
}
}

/**
* Returns the map canvas associated with the widget.
* \see setMapCanvas()
@@ -79,6 +79,7 @@ QgsPointClusterRendererWidget::QgsPointClusterRendererWidget( QgsVectorLayer *la
mDistanceSpinBox->setValue( mRenderer->tolerance() );
mDistanceUnitWidget->setUnit( mRenderer->toleranceUnit() );
mDistanceUnitWidget->setMapUnitScale( mRenderer->toleranceMapUnitScale() );
mCenterSymbolToolButton->setSymbol( mRenderer->clusterSymbol()->clone() );

blockAllSignals( false );

@@ -94,7 +95,10 @@ QgsPointClusterRendererWidget::QgsPointClusterRendererWidget( QgsVectorLayer *la
}
}

updateCenterIcon();
connect( mCenterSymbolToolButton, &QgsSymbolButton::changed, this, &QgsPointClusterRendererWidget::centerSymbolChanged );
mCenterSymbolToolButton->setDialogTitle( tr( "Cluster symbol" ) );
mCenterSymbolToolButton->setLayer( mLayer );
mCenterSymbolToolButton->registerExpressionContextGenerator( this );
}

QgsPointClusterRendererWidget::~QgsPointClusterRendererWidget()
@@ -112,6 +116,8 @@ void QgsPointClusterRendererWidget::setContext( const QgsSymbolWidgetContext &co
QgsRendererWidget::setContext( context );
if ( mDistanceUnitWidget )
mDistanceUnitWidget->setMapCanvas( context.mapCanvas() );
if ( mCenterSymbolToolButton )
mCenterSymbolToolButton->setMapCanvas( context.mapCanvas() );
}

void QgsPointClusterRendererWidget::on_mRendererComboBox_currentIndexChanged( int index )
@@ -175,55 +181,36 @@ void QgsPointClusterRendererWidget::on_mDistanceUnitWidget_changed()
void QgsPointClusterRendererWidget::blockAllSignals( bool block )
{
mRendererComboBox->blockSignals( block );
mCenterSymbolPushButton->blockSignals( block );
mCenterSymbolToolButton->blockSignals( block );
mDistanceSpinBox->blockSignals( block );
mDistanceUnitWidget->blockSignals( block );
}

void QgsPointClusterRendererWidget::on_mCenterSymbolPushButton_clicked()
QgsExpressionContext QgsPointClusterRendererWidget::createExpressionContext() const
{
if ( !mRenderer || !mRenderer->clusterSymbol() )
{
return;
}
QgsMarkerSymbol *markerSymbol = mRenderer->clusterSymbol()->clone();
QgsSymbolSelectorWidget *dlg = new QgsSymbolSelectorWidget( markerSymbol, QgsStyle::defaultStyle(), mLayer, this );
dlg->setPanelTitle( tr( "Cluster symbol" ) );
dlg->setDockMode( this->dockMode() );

QgsSymbolWidgetContext context = mContext;
QgsExpressionContext context;
if ( mContext.expressionContext() )
context = *mContext.expressionContext();
else
context.appendScopes( mContext.globalProjectAtlasMapLayerScopes( mLayer ) );
QgsExpressionContextScope scope;
scope.addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "", true ) );
scope.addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, 0, true ) );
QList< QgsExpressionContextScope > scopes = context.additionalExpressionContextScopes();
QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
scopes << scope;
context.setAdditionalExpressionContextScopes( scopes );

dlg->setContext( context );

connect( dlg, &QgsPanelWidget::widgetChanged, this, &QgsPointClusterRendererWidget::updateCenterSymbolFromWidget );
connect( dlg, &QgsPanelWidget::panelAccepted, this, &QgsPointClusterRendererWidget::cleanUpSymbolSelector );
openPanel( dlg );
Q_FOREACH ( const QgsExpressionContextScope &s, scopes )
{
context << new QgsExpressionContextScope( s );
}
return context;
}

void QgsPointClusterRendererWidget::updateCenterSymbolFromWidget()
void QgsPointClusterRendererWidget::centerSymbolChanged()
{
QgsSymbolSelectorWidget *dlg = qobject_cast<QgsSymbolSelectorWidget *>( sender() );
QgsSymbol *symbol = dlg->symbol()->clone();
mRenderer->setClusterSymbol( static_cast< QgsMarkerSymbol * >( symbol ) );
updateCenterIcon();
mRenderer->setClusterSymbol( mCenterSymbolToolButton->clonedSymbol< QgsMarkerSymbol >() );
emit widgetChanged();
}

void QgsPointClusterRendererWidget::cleanUpSymbolSelector( QgsPanelWidget *container )
{
if ( container )
{
QgsSymbolSelectorWidget *dlg = qobject_cast<QgsSymbolSelectorWidget *>( container );
delete dlg->symbol();
}
}

void QgsPointClusterRendererWidget::updateRendererFromWidget()
{
QgsRendererWidget *w = qobject_cast<QgsRendererWidget *>( sender() );
@@ -234,17 +221,6 @@ void QgsPointClusterRendererWidget::updateRendererFromWidget()
emit widgetChanged();
}

void QgsPointClusterRendererWidget::updateCenterIcon()
{
QgsMarkerSymbol *symbol = mRenderer->clusterSymbol();
if ( !symbol )
{
return;
}
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( symbol, mCenterSymbolPushButton->iconSize() );
mCenterSymbolPushButton->setIcon( icon );
}

void QgsPointClusterRendererWidget::setupBlankUi( const QString &layerName )
{
QGridLayout *layout = new QGridLayout( this );
@@ -21,6 +21,7 @@
#include "ui_qgspointclusterrendererwidgetbase.h"
#include "qgis.h"
#include "qgsrendererwidget.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgis_gui.h"

class QgsPointClusterRenderer;
@@ -31,9 +32,10 @@ class QgsPointClusterRenderer;
* \since QGIS 3.0
*/

class GUI_EXPORT QgsPointClusterRendererWidget: public QgsRendererWidget, private Ui::QgsPointClusterRendererWidgetBase
class GUI_EXPORT QgsPointClusterRendererWidget: public QgsRendererWidget, public QgsExpressionContextGenerator, private Ui::QgsPointClusterRendererWidgetBase
{
Q_OBJECT

public:

/** Returns a new QgsPointClusterRendererWidget.
@@ -56,22 +58,21 @@ class GUI_EXPORT QgsPointClusterRendererWidget: public QgsRendererWidget, privat
QgsFeatureRenderer *renderer() override;
void setContext( const QgsSymbolWidgetContext &context ) override;

QgsExpressionContext createExpressionContext() const override;

private:
QgsPointClusterRenderer *mRenderer = nullptr;

void blockAllSignals( bool block );
void updateCenterIcon();
void setupBlankUi( const QString &layerName );

private slots:

void on_mRendererComboBox_currentIndexChanged( int index );
void on_mDistanceSpinBox_valueChanged( double d );
void on_mDistanceUnitWidget_changed();
void on_mCenterSymbolPushButton_clicked();
void on_mRendererSettingsButton_clicked();
void updateCenterSymbolFromWidget();
void cleanUpSymbolSelector( QgsPanelWidget *container );
void centerSymbolChanged();
void updateRendererFromWidget();
};

0 comments on commit 46f6f83

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