Skip to content

Commit

Permalink
Give symbol widgets optional access to a map canvas
Browse files Browse the repository at this point in the history
This allows symbol widgets to fetch properties from the main map
canvas, for instance fetching the current scale from the map.
  • Loading branch information
nyalldawson committed Sep 16, 2015
1 parent 60cce74 commit a62c6a9
Show file tree
Hide file tree
Showing 36 changed files with 228 additions and 12 deletions.
7 changes: 7 additions & 0 deletions python/gui/symbology-ng/qgslayerpropertieswidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class QgsLayerPropertiesWidget : QWidget
*/
QgsExpressionContext* expressionContext() const;

/** 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
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

public slots:
void layerTypeChanged();
void emitSignalChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class QgsPointDisplacementRendererWidget: QgsRendererV2Widget
~QgsPointDisplacementRendererWidget();

QgsFeatureRendererV2* renderer();
void setMapCanvas( QgsMapCanvas* canvas );
};
8 changes: 8 additions & 0 deletions python/gui/symbology-ng/qgsrendererv2propertiesdialog.sip
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ class QgsRendererV2PropertiesDialog : QDialog

public:
QgsRendererV2PropertiesDialog( QgsVectorLayer* layer, QgsStyleV2* style, bool embedded = false );
~QgsRendererV2PropertiesDialog();

/** 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
* @note added in QGIS 2.12
*/
void setMapCanvas( QgsMapCanvas* canvas );

public slots:
//! called when user changes renderer type
Expand Down
7 changes: 7 additions & 0 deletions python/gui/symbology-ng/qgsrendererv2widget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class QgsRendererV2Widget : QWidget
//! show a dialog with renderer's symbol level settings
void showSymbolLevelsDialog( QgsFeatureRendererV2* r );

/** 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
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

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&)*/
Expand Down
2 changes: 1 addition & 1 deletion python/gui/symbology-ng/qgsrulebasedrendererv2widget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class QgsRendererRulePropsDialog : QDialog
%End

public:
QgsRendererRulePropsDialog( QgsRuleBasedRendererV2::Rule* rule, QgsVectorLayer* layer, QgsStyleV2* style, QWidget* parent /TransferThis/ = 0 );
QgsRendererRulePropsDialog( QgsRuleBasedRendererV2::Rule* rule, QgsVectorLayer* layer, QgsStyleV2* style, QWidget* parent /TransferThis/ = 0, QgsMapCanvas* mapCanvas = 0 );
~QgsRendererRulePropsDialog();

QgsRuleBasedRendererV2::Rule* rule();
Expand Down
2 changes: 2 additions & 0 deletions python/gui/symbology-ng/qgssinglesymbolrendererv2widget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class QgsSingleSymbolRendererV2Widget : QgsRendererV2Widget

virtual QgsFeatureRendererV2* renderer();

virtual void setMapCanvas( QgsMapCanvas* canvas );

public slots:
void changeSingleSymbol();

Expand Down
7 changes: 7 additions & 0 deletions python/gui/symbology-ng/qgssymbollayerv2widget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class QgsSymbolLayerV2Widget : QWidget
*/
QgsExpressionContext* expressionContext() const;

/** 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
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
Expand Down
7 changes: 7 additions & 0 deletions python/gui/symbology-ng/qgssymbolslistwidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class QgsSymbolsListWidget : QWidget
*/
QgsExpressionContext* expressionContext() const;

/** 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
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
Expand Down
7 changes: 7 additions & 0 deletions python/gui/symbology-ng/qgssymbolv2selectordialog.sip
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class QgsSymbolV2SelectorDialog : QDialog
*/
QgsExpressionContext* expressionContext() const;

/** 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
* @note added in QGIS 2.12
*/
void setMapCanvas( QgsMapCanvas* canvas );

protected:
//! Reimplements dialog keyPress event so we can ignore it
void keyPressEvent( QKeyEvent * event );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsvectorlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ void QgsVectorLayerProperties::updateSymbologyPage()
if ( layer->rendererV2() )
{
mRendererDialog = new QgsRendererV2PropertiesDialog( layer, QgsStyleV2::defaultStyle(), true );
mRendererDialog->setMapCanvas( QgisApp::instance()->mapCanvas() );

// display the menu to choose the output format (fix #5136)
mActionSaveStyleAs->setText( tr( "Save Style" ) );
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsvectorlayerproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class QgsVectorLayer;
class QgsLabelingGui;
class QgsDiagramProperties;
class QgsFieldsProperties;
class QgsRendererV2PropertiesDialog;

class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private Ui::QgsVectorLayerPropertiesBase
{
Expand Down Expand Up @@ -164,7 +165,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
QAction* mActionSaveStyleAs;

/** Renderer dialog which is shown*/
QDialog* mRendererDialog;
QgsRendererV2PropertiesDialog* mRendererDialog;
/** Labeling dialog. If apply is pressed, options are applied to vector's QgsLabel */
QgsLabelingGui* labelingDialog;
/** Label dialog. If apply is pressed, options are applied to vector's QgsLabel */
Expand Down
3 changes: 3 additions & 0 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ void QgsCategorizedSymbolRendererV2Widget::changeSelectedSymbols()
{
QgsSymbolV2* newSymbol = mCategorizedSymbol->clone();
QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, mLayer, this );
dlg.setMapCanvas( mMapCanvas );
if ( !dlg.exec() )
{
delete newSymbol;
Expand Down Expand Up @@ -555,6 +556,7 @@ void QgsCategorizedSymbolRendererV2Widget::changeCategorizedSymbol()
QgsSymbolV2* newSymbol = mCategorizedSymbol->clone();

QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, mLayer, this );
dlg.setMapCanvas( mMapCanvas );
if ( !dlg.exec() )
{
delete newSymbol;
Expand Down Expand Up @@ -605,6 +607,7 @@ void QgsCategorizedSymbolRendererV2Widget::changeCategorySymbol()
}

QgsSymbolV2SelectorDialog dlg( symbol, mStyle, mLayer, this );
dlg.setMapCanvas( mMapCanvas );
if ( !dlg.exec() )
{
delete symbol;
Expand Down
3 changes: 3 additions & 0 deletions src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ void QgsGraduatedSymbolRendererV2Widget::changeGraduatedSymbol()
QgsSymbolV2* newSymbol = mGraduatedSymbol->clone();

QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, mLayer, this );
dlg.setMapCanvas( mMapCanvas );
if ( !dlg.exec() )
{
delete newSymbol;
Expand Down Expand Up @@ -860,6 +861,7 @@ void QgsGraduatedSymbolRendererV2Widget::changeSelectedSymbols()
{
QgsSymbolV2* newSymbol = mGraduatedSymbol->clone();
QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, mLayer, this );
dlg.setMapCanvas( mMapCanvas );
if ( !dlg.exec() )
{
delete newSymbol;
Expand All @@ -885,6 +887,7 @@ void QgsGraduatedSymbolRendererV2Widget::changeRangeSymbol( int rangeIdx )
QgsSymbolV2* newSymbol = mRenderer->ranges()[rangeIdx].symbol()->clone();

QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, mLayer, this );
dlg.setMapCanvas( mMapCanvas );
if ( !dlg.exec() )
{
delete newSymbol;
Expand Down
6 changes: 6 additions & 0 deletions src/gui/symbology-ng/qgsheatmaprendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ QgsFeatureRendererV2* QgsHeatmapRendererWidget::renderer()
return mRenderer;
}

void QgsHeatmapRendererWidget::setMapCanvas( QgsMapCanvas* canvas )
{
QgsRendererV2Widget::setMapCanvas( canvas );
mRadiusUnitWidget->setMapCanvas( canvas );
}

void QgsHeatmapRendererWidget::applyColorRamp()
{
if ( !mRenderer )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/symbology-ng/qgsheatmaprendererwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class GUI_EXPORT QgsHeatmapRendererWidget : public QgsRendererV2Widget, private
/** @returns the current feature renderer */
virtual QgsFeatureRendererV2* renderer() override;

void setMapCanvas( QgsMapCanvas* canvas ) override;

protected:
QgsHeatmapRenderer* mRenderer;

Expand Down
7 changes: 7 additions & 0 deletions src/gui/symbology-ng/qgsinvertedpolygonrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,20 @@ QgsFeatureRendererV2* QgsInvertedPolygonRendererWidget::renderer()
return mRenderer.data();
}

void QgsInvertedPolygonRendererWidget::setMapCanvas( QgsMapCanvas* canvas )
{
QgsRendererV2Widget::setMapCanvas( canvas );
mEmbeddedRendererWidget->setMapCanvas( canvas );
}

void QgsInvertedPolygonRendererWidget::on_mRendererComboBox_currentIndexChanged( int index )
{
QString rendererId = mRendererComboBox->itemData( index ).toString();
QgsRendererV2AbstractMetadata* m = QgsRendererV2Registry::instance()->rendererMetadata( rendererId );
if ( m )
{
mEmbeddedRendererWidget.reset( m->createRendererWidget( mLayer, mStyle, const_cast<QgsFeatureRendererV2*>( mRenderer->embeddedRenderer() )->clone() ) );
mEmbeddedRendererWidget->setMapCanvas( mMapCanvas );

if ( mLayout->count() > 2 )
{
Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology-ng/qgsinvertedpolygonrendererwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class GUI_EXPORT QgsInvertedPolygonRendererWidget : public QgsRendererV2Widget,

/** @returns the current feature renderer */
virtual QgsFeatureRendererV2* renderer() override;
void setMapCanvas( QgsMapCanvas* canvas ) override;

protected:
/** The mask renderer */
Expand Down
11 changes: 11 additions & 0 deletions src/gui/symbology-ng/qgslayerpropertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static void _initWidgetFunctions()
QgsLayerPropertiesWidget::QgsLayerPropertiesWidget( QgsSymbolLayerV2* layer, const QgsSymbolV2* symbol, const QgsVectorLayer* vl, QWidget* parent )
: QWidget( parent )
, mPresetExpressionContext( 0 )
, mMapCanvas( 0 )
{

mLayer = layer;
Expand Down Expand Up @@ -116,6 +117,14 @@ QgsLayerPropertiesWidget::QgsLayerPropertiesWidget( QgsSymbolLayerV2* layer, con
mEffectWidget->setPaintEffect( mLayer->paintEffect() );
}

void QgsLayerPropertiesWidget::setMapCanvas( QgsMapCanvas *canvas )
{
mMapCanvas = canvas;
QgsSymbolLayerV2Widget* w = dynamic_cast< QgsSymbolLayerV2Widget* >( stackedWidget->currentWidget() );
if ( w )
w->setMapCanvas( mMapCanvas );
}

void QgsLayerPropertiesWidget::setExpressionContext( QgsExpressionContext *context )
{
mPresetExpressionContext = context;
Expand Down Expand Up @@ -165,6 +174,8 @@ void QgsLayerPropertiesWidget::updateSymbolLayerWidget( QgsSymbolLayerV2* layer
{
w->setSymbolLayer( layer );
w->setExpressionContext( mPresetExpressionContext );
if ( mMapCanvas )
w->setMapCanvas( mMapCanvas );
stackedWidget->addWidget( w );
stackedWidget->setCurrentWidget( w );
// start receiving updates from widget
Expand Down
10 changes: 10 additions & 0 deletions src/gui/symbology-ng/qgslayerpropertieswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class QgsSymbolV2;
class QgsSymbolLayerV2;
class QgsSymbolLayerV2Widget;
class QgsVectorLayer;
class QgsMapCanvas;

class SymbolLayerItem;

Expand All @@ -44,6 +45,13 @@ class GUI_EXPORT QgsLayerPropertiesWidget : public QWidget, private Ui::LayerPro
*/
QgsExpressionContext* expressionContext() const { return mPresetExpressionContext; }

/** 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
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

public slots:
void layerTypeChanged();
void emitSignalChanged();
Expand Down Expand Up @@ -74,6 +82,8 @@ class GUI_EXPORT QgsLayerPropertiesWidget : public QWidget, private Ui::LayerPro

private:
QgsExpressionContext* mPresetExpressionContext;
QgsMapCanvas* mMapCanvas;

};

#endif //QGSLAYERPROPERTIESWIDGET_H
9 changes: 9 additions & 0 deletions src/gui/symbology-ng/qgspointdisplacementrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ QgsFeatureRendererV2* QgsPointDisplacementRendererWidget::renderer()
return mRenderer;
}

void QgsPointDisplacementRendererWidget::setMapCanvas( QgsMapCanvas* canvas )
{
QgsRendererV2Widget::setMapCanvas( canvas );
mDistanceUnitWidget->setMapCanvas( canvas );
mEmbeddedRendererWidget->setMapCanvas( canvas );
}

void QgsPointDisplacementRendererWidget::on_mLabelFieldComboBox_currentIndexChanged( const QString& text )
{
if ( mRenderer )
Expand All @@ -183,6 +190,7 @@ void QgsPointDisplacementRendererWidget::on_mRendererComboBox_currentIndexChange
{
delete mEmbeddedRendererWidget;
mEmbeddedRendererWidget = m->createRendererWidget( mLayer, mStyle, mRenderer->embeddedRenderer()->clone() );
mEmbeddedRendererWidget->setMapCanvas( mMapCanvas );
}
}

Expand Down Expand Up @@ -332,6 +340,7 @@ void QgsPointDisplacementRendererWidget::on_mCenterSymbolPushButton_clicked()
}
QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( mRenderer->centerSymbol()->clone() );
QgsSymbolV2SelectorDialog dlg( markerSymbol, QgsStyleV2::defaultStyle(), mLayer, this );
dlg.setMapCanvas( mMapCanvas );
if ( dlg.exec() == QDialog::Rejected )
{
delete markerSymbol;
Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology-ng/qgspointdisplacementrendererwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class GUI_EXPORT QgsPointDisplacementRendererWidget: public QgsRendererV2Widget,
~QgsPointDisplacementRendererWidget();

QgsFeatureRendererV2* renderer() override;
void setMapCanvas( QgsMapCanvas* canvas ) override;

private:
QgsPointDisplacementRenderer* mRenderer;
Expand Down
10 changes: 10 additions & 0 deletions src/gui/symbology-ng/qgsrendererv2propertiesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ QgsRendererV2PropertiesDialog::QgsRendererV2PropertiesDialog( QgsVectorLayer* la
, mStyle( style )
, mActiveWidget( NULL )
, mPaintEffect( 0 )
, mMapCanvas( 0 )
{
setupUi( this );

Expand Down Expand Up @@ -149,6 +150,13 @@ QgsRendererV2PropertiesDialog::~QgsRendererV2PropertiesDialog()
delete mPaintEffect;
}

void QgsRendererV2PropertiesDialog::setMapCanvas( QgsMapCanvas* canvas )
{
mMapCanvas = canvas;
if ( mActiveWidget )
mActiveWidget->setMapCanvas( mMapCanvas );
}


void QgsRendererV2PropertiesDialog::rendererChanged()
{
Expand Down Expand Up @@ -193,6 +201,8 @@ void QgsRendererV2PropertiesDialog::rendererChanged()
mActiveWidget = w;
stackedWidget->addWidget( mActiveWidget );
stackedWidget->setCurrentWidget( mActiveWidget );
if ( mMapCanvas )
mActiveWidget->setMapCanvas( mMapCanvas );
}
else
{
Expand Down
10 changes: 10 additions & 0 deletions src/gui/symbology-ng/qgsrendererv2propertiesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class QgsStyleV2;
class QgsSymbolV2;
class QgsPaintEffect;
class QgsRendererV2Widget;
class QgsMapCanvas;

class GUI_EXPORT QgsRendererV2PropertiesDialog : public QDialog, private Ui::QgsRendererV2PropsDialogBase
{
Expand All @@ -37,6 +38,13 @@ class GUI_EXPORT QgsRendererV2PropertiesDialog : public QDialog, private Ui::Qgs
QgsRendererV2PropertiesDialog( QgsVectorLayer* layer, QgsStyleV2* style, bool embedded = false );
~QgsRendererV2PropertiesDialog();

/** 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
* @note added in QGIS 2.12
*/
void setMapCanvas( QgsMapCanvas* canvas );

public slots:
//! called when user changes renderer type
void rendererChanged();
Expand All @@ -56,6 +64,8 @@ class GUI_EXPORT QgsRendererV2PropertiesDialog : public QDialog, private Ui::Qgs
QgsRendererV2Widget* mActiveWidget;

QgsPaintEffect* mPaintEffect;

QgsMapCanvas* mMapCanvas;
};


Expand Down
Loading

0 comments on commit a62c6a9

Please sign in to comment.