Skip to content

Commit

Permalink
Port some symbol buttons across to QgsSymbolButton
Browse files Browse the repository at this point in the history
- 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 24, 2017
1 parent 22c4740 commit 46f6f83
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 197 deletions.
1 change: 1 addition & 0 deletions python/gui/qgssymbolbutton.sip
Expand Up @@ -53,6 +53,7 @@ class QgsSymbolButton : QToolButton
:rtype: QgsSymbol :rtype: QgsSymbol
%End %End



QgsMapCanvas *mapCanvas() const; QgsMapCanvas *mapCanvas() const;
%Docstring %Docstring
Returns the map canvas associated with the widget. Returns the map canvas associated with the widget.
Expand Down
5 changes: 4 additions & 1 deletion python/gui/symbology-ng/qgspointclusterrendererwidget.sip
Expand Up @@ -11,7 +11,7 @@






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




virtual QgsExpressionContext createExpressionContext() const;


}; };


/************************************************************************ /************************************************************************
Expand Down
Expand Up @@ -10,7 +10,7 @@






class QgsPointDisplacementRendererWidget: QgsRendererWidget class QgsPointDisplacementRendererWidget: QgsRendererWidget, QgsExpressionContextGenerator
{ {


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




virtual QgsExpressionContext createExpressionContext() const;


}; };


/************************************************************************ /************************************************************************
Expand Down
53 changes: 7 additions & 46 deletions src/app/composer/qgscomposershapewidget.cpp
Expand Up @@ -52,7 +52,10 @@ QgsComposerShapeWidget::QgsComposerShapeWidget( QgsComposerShape *composerShape
if ( mComposerShape ) if ( mComposerShape )
{ {
connect( mComposerShape, &QgsComposerObject::itemChanged, this, &QgsComposerShapeWidget::setGuiElementValues ); connect( mComposerShape, &QgsComposerObject::itemChanged, this, &QgsComposerShapeWidget::setGuiElementValues );
mShapeStyleButton->registerExpressionContextGenerator( mComposerShape );
} }
connect( mShapeStyleButton, &QgsSymbolButton::changed, this, &QgsComposerShapeWidget::symbolChanged );
mShapeStyleButton->setLayer( atlasCoverageLayer() );
} }


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


blockAllSignals( true ); blockAllSignals( true );


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


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


void QgsComposerShapeWidget::on_mShapeStyleButton_clicked() void QgsComposerShapeWidget::symbolChanged()
{ {
if ( !mComposerShape ) if ( !mComposerShape )
{ {
return; 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" ) ); mComposerShape->beginCommand( tr( "Shape style changed" ) );
} mComposerShape->setShapeStyleSymbol( mShapeStyleButton->clonedSymbol<QgsFillSymbol>() );

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


void QgsComposerShapeWidget::on_mCornerRadiusSpinBox_valueChanged( double val ) void QgsComposerShapeWidget::on_mCornerRadiusSpinBox_valueChanged( double val )
Expand Down Expand Up @@ -179,23 +160,3 @@ void QgsComposerShapeWidget::toggleRadiusSpin( const QString &shapeText )
mCornerRadiusSpinBox->setEnabled( false ); 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();
}



6 changes: 1 addition & 5 deletions src/app/composer/qgscomposershapewidget.h
Expand Up @@ -40,17 +40,13 @@ class QgsComposerShapeWidget: public QgsComposerItemBaseWidget, private Ui::QgsC
private slots: private slots:
void on_mShapeComboBox_currentIndexChanged( const QString &text ); void on_mShapeComboBox_currentIndexChanged( const QString &text );
void on_mCornerRadiusSpinBox_valueChanged( double val ); void on_mCornerRadiusSpinBox_valueChanged( double val );
void on_mShapeStyleButton_clicked(); void symbolChanged();


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


void updateShapeStyle();

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


#endif // QGSCOMPOSERSHAPEWIDGET_H #endif // QGSCOMPOSERSHAPEWIDGET_H
22 changes: 22 additions & 0 deletions src/gui/qgssymbolbutton.h
Expand Up @@ -72,6 +72,28 @@ class GUI_EXPORT QgsSymbolButton : public QToolButton
*/ */
QgsSymbol *symbol(); 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. * Returns the map canvas associated with the widget.
* \see setMapCanvas() * \see setMapCanvas()
Expand Down
68 changes: 22 additions & 46 deletions src/gui/symbology-ng/qgspointclusterrendererwidget.cpp
Expand Up @@ -79,6 +79,7 @@ QgsPointClusterRendererWidget::QgsPointClusterRendererWidget( QgsVectorLayer *la
mDistanceSpinBox->setValue( mRenderer->tolerance() ); mDistanceSpinBox->setValue( mRenderer->tolerance() );
mDistanceUnitWidget->setUnit( mRenderer->toleranceUnit() ); mDistanceUnitWidget->setUnit( mRenderer->toleranceUnit() );
mDistanceUnitWidget->setMapUnitScale( mRenderer->toleranceMapUnitScale() ); mDistanceUnitWidget->setMapUnitScale( mRenderer->toleranceMapUnitScale() );
mCenterSymbolToolButton->setSymbol( mRenderer->clusterSymbol()->clone() );


blockAllSignals( false ); blockAllSignals( false );


Expand All @@ -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() QgsPointClusterRendererWidget::~QgsPointClusterRendererWidget()
Expand All @@ -112,6 +116,8 @@ void QgsPointClusterRendererWidget::setContext( const QgsSymbolWidgetContext &co
QgsRendererWidget::setContext( context ); QgsRendererWidget::setContext( context );
if ( mDistanceUnitWidget ) if ( mDistanceUnitWidget )
mDistanceUnitWidget->setMapCanvas( context.mapCanvas() ); mDistanceUnitWidget->setMapCanvas( context.mapCanvas() );
if ( mCenterSymbolToolButton )
mCenterSymbolToolButton->setMapCanvas( context.mapCanvas() );
} }


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


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

QgsSymbolWidgetContext context = mContext;
QgsExpressionContextScope scope; QgsExpressionContextScope scope;
scope.addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "", true ) ); scope.addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_CLUSTER_COLOR, "", true ) );
scope.addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, 0, true ) ); scope.addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_CLUSTER_SIZE, 0, true ) );
QList< QgsExpressionContextScope > scopes = context.additionalExpressionContextScopes(); QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
scopes << scope; scopes << scope;
context.setAdditionalExpressionContextScopes( scopes ); Q_FOREACH ( const QgsExpressionContextScope &s, scopes )

{
dlg->setContext( context ); context << new QgsExpressionContextScope( s );

}
connect( dlg, &QgsPanelWidget::widgetChanged, this, &QgsPointClusterRendererWidget::updateCenterSymbolFromWidget ); return context;
connect( dlg, &QgsPanelWidget::panelAccepted, this, &QgsPointClusterRendererWidget::cleanUpSymbolSelector );
openPanel( dlg );
} }


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


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

void QgsPointClusterRendererWidget::updateRendererFromWidget() void QgsPointClusterRendererWidget::updateRendererFromWidget()
{ {
QgsRendererWidget *w = qobject_cast<QgsRendererWidget *>( sender() ); QgsRendererWidget *w = qobject_cast<QgsRendererWidget *>( sender() );
Expand All @@ -234,17 +221,6 @@ void QgsPointClusterRendererWidget::updateRendererFromWidget()
emit widgetChanged(); 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 ) void QgsPointClusterRendererWidget::setupBlankUi( const QString &layerName )
{ {
QGridLayout *layout = new QGridLayout( this ); QGridLayout *layout = new QGridLayout( this );
Expand Down
11 changes: 6 additions & 5 deletions src/gui/symbology-ng/qgspointclusterrendererwidget.h
Expand Up @@ -21,6 +21,7 @@
#include "ui_qgspointclusterrendererwidgetbase.h" #include "ui_qgspointclusterrendererwidgetbase.h"
#include "qgis.h" #include "qgis.h"
#include "qgsrendererwidget.h" #include "qgsrendererwidget.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgis_gui.h" #include "qgis_gui.h"


class QgsPointClusterRenderer; class QgsPointClusterRenderer;
Expand All @@ -31,9 +32,10 @@ class QgsPointClusterRenderer;
* \since QGIS 3.0 * \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 Q_OBJECT

public: public:


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


QgsExpressionContext createExpressionContext() const override;

private: private:
QgsPointClusterRenderer *mRenderer = nullptr; QgsPointClusterRenderer *mRenderer = nullptr;


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


private slots: private slots:


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


Expand Down

0 comments on commit 46f6f83

Please sign in to comment.