QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgssinglesymbolrendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssinglesymbolrendererwidget.cpp
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
16 
20 #include "qgssymbol.h"
21 
22 #include "qgslogger.h"
23 #include "qgsvectorlayer.h"
24 
26 
27 #include <QMenu>
28 
29 
31 {
32  return new QgsSingleSymbolRendererWidget( layer, style, renderer );
33 }
34 
36  : QgsRendererWidget( layer, style )
37 
38 {
39  // try to recognize the previous renderer
40  // (null renderer means "no previous renderer")
41 
42  if ( renderer )
43  {
44  mRenderer = QgsSingleSymbolRenderer::convertFromRenderer( renderer );
45  }
46  if ( !mRenderer )
47  {
49 
50  mRenderer = new QgsSingleSymbolRenderer( symbol );
51  }
52 
53  // load symbol from it
54  mSingleSymbol = mRenderer->symbol()->clone();
55 
56  // setup ui
57  mSelector = new QgsSymbolSelectorWidget( mSingleSymbol, mStyle, mLayer, nullptr );
58  connect( mSelector, &QgsSymbolSelectorWidget::symbolModified, this, &QgsSingleSymbolRendererWidget::changeSingleSymbol );
59  connect( mSelector, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
60  connect( this, &QgsRendererWidget::symbolLevelsChanged, [ = ]()
61  {
62  delete mSingleSymbol;
63  mSingleSymbol = mRenderer->symbol()->clone();
64  mSelector->loadSymbol( mSingleSymbol );
65  } );
66 
67  QVBoxLayout *layout = new QVBoxLayout( this );
68  layout->setContentsMargins( 0, 0, 0, 0 );
69  layout->addWidget( mSelector );
70 
71  // advanced actions - data defined rendering
72  QMenu *advMenu = mSelector->advancedMenu();
73 
74  QAction *actionLevels = advMenu->addAction( tr( "Symbol Levels…" ) );
75  connect( actionLevels, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::showSymbolLevels );
76  if ( mSingleSymbol->type() == QgsSymbol::Marker )
77  {
78  QAction *actionDdsLegend = advMenu->addAction( tr( "Data-defined Size Legend…" ) );
79  // only from Qt 5.6 there is convenience addAction() with new style connection
80  connect( actionDdsLegend, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::dataDefinedSizeLegend );
81  }
82 }
83 
85 {
86  delete mSingleSymbol;
87 
88  delete mRenderer;
89 
90  delete mSelector;
91 }
92 
93 
95 {
96  return mRenderer;
97 }
98 
100 {
102  if ( mSelector )
103  mSelector->setContext( context );
104 }
105 
107 {
108  QgsRendererWidget::setDockMode( dockMode );
109  if ( mSelector )
110  mSelector->setDockMode( dockMode );
111 }
112 
113 void QgsSingleSymbolRendererWidget::changeSingleSymbol()
114 {
115  // update symbol from the GUI
116  mRenderer->setSymbol( mSingleSymbol->clone() );
117  emit widgetChanged();
118 }
119 
120 void QgsSingleSymbolRendererWidget::showSymbolLevels()
121 {
122  showSymbolLevelsDialog( mRenderer );
123 }
124 
125 void QgsSingleSymbolRendererWidget::dataDefinedSizeLegend()
126 {
127  QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mSingleSymbol ); // this should be only enabled for marker symbols
128  QgsDataDefinedSizeLegendWidget *panel = createDataDefinedSizeLegendWidget( s, mRenderer->dataDefinedSizeLegend() );
129  if ( panel )
130  {
131  connect( panel, &QgsPanelWidget::widgetChanged, this, [ = ]
132  {
133  mRenderer->setDataDefinedSizeLegend( panel->dataDefinedSizeLegend() );
134  emit widgetChanged();
135  } );
136  openPanel( panel ); // takes ownership of the panel
137  }
138 }
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
bool dockMode()
Returns the dock mode state.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
void showSymbolLevelsDialog(QgsFeatureRenderer *r)
show a dialog with renderer&#39;s symbol level settings
Base class for renderer settings widgets.
QgsVectorLayer * mLayer
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
static QgsSingleSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a new single symbol renderer from an existing renderer.
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
void symbolModified()
Emiited when a symbol is modified in the widget.
void symbolLevelsChanged()
Emitted when the symbol levels settings have been changed.
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:860
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs...
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:297
Contains settings which reflect the context in which a symbol (or renderer) widget is shown...
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs...
QgsDataDefinedSizeLegendWidget * createDataDefinedSizeLegendWidget(const QgsMarkerSymbol *symbol, const QgsDataDefinedSizeLegend *ddsLegend)
Creates widget to setup data-defined size legend.
Symbol selector widget that can be used to select and build a symbol.
QMenu * advancedMenu()
Returns menu for "advanced" button - create it if doesn&#39;t exist and show the advanced button...
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
void widgetChanged()
Emitted when the widget state changes.
void setSymbol(QgsSymbol *s)
Sets the symbol which will be rendered for every feature.
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Marker symbol.
Definition: qgssymbol.h:85
SymbolType type() const
Returns the symbol&#39;s type.
Definition: qgssymbol.h:120
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsSingleSymbolRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration of appearance of legend when using data-defined size for marker symbols...
Represents a vector layer which manages a vector based data sets.
void loadSymbol(QgsSymbol *symbol, SymbolLayerItem *parent=nullptr)
Loads the given symbol into the widget.
QgsSymbol * symbol() const
Returns the symbol which will be rendered for every feature.