QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  {
45  }
46  if ( !mRenderer )
47  {
49 
50  if ( symbol )
51  mRenderer = new QgsSingleSymbolRenderer( symbol );
52  }
53 
54  // load symbol from it
55  if ( mRenderer )
56  mSingleSymbol = mRenderer->symbol()->clone();
57 
58  // setup ui
59  mSelector = new QgsSymbolSelectorWidget( mSingleSymbol, mStyle, mLayer, nullptr );
60  connect( mSelector, &QgsSymbolSelectorWidget::symbolModified, this, &QgsSingleSymbolRendererWidget::changeSingleSymbol );
61  connect( mSelector, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
62  connect( this, &QgsRendererWidget::symbolLevelsChanged, [ = ]()
63  {
64  delete mSingleSymbol;
65  mSingleSymbol = mRenderer->symbol()->clone();
66  mSelector->loadSymbol( mSingleSymbol );
67  } );
68 
69  QVBoxLayout *layout = new QVBoxLayout( this );
70  layout->setContentsMargins( 0, 0, 0, 0 );
71  layout->addWidget( mSelector );
72 
73  // advanced actions - data defined rendering
74  QMenu *advMenu = mSelector->advancedMenu();
75 
76  QAction *actionLevels = advMenu->addAction( tr( "Symbol Levels…" ) );
77  connect( actionLevels, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::showSymbolLevels );
78  if ( mSingleSymbol && mSingleSymbol->type() == QgsSymbol::Marker )
79  {
80  QAction *actionDdsLegend = advMenu->addAction( tr( "Data-defined Size Legend…" ) );
81  // only from Qt 5.6 there is convenience addAction() with new style connection
82  connect( actionDdsLegend, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::dataDefinedSizeLegend );
83  }
84 }
85 
87 {
88  delete mSingleSymbol;
89 
90  delete mRenderer;
91 
92  delete mSelector;
93 }
94 
95 
97 {
98  return mRenderer;
99 }
100 
102 {
104  if ( mSelector )
105  mSelector->setContext( context );
106 }
107 
109 {
111  if ( mSelector )
112  mSelector->setDockMode( dockMode );
113 }
114 
115 void QgsSingleSymbolRendererWidget::changeSingleSymbol()
116 {
117  // update symbol from the GUI
118  mRenderer->setSymbol( mSingleSymbol->clone() );
119  emit widgetChanged();
120 }
121 
122 void QgsSingleSymbolRendererWidget::showSymbolLevels()
123 {
124  showSymbolLevelsDialog( mRenderer );
125 }
126 
127 void QgsSingleSymbolRendererWidget::dataDefinedSizeLegend()
128 {
129  QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mSingleSymbol ); // this should be only enabled for marker symbols
130  QgsDataDefinedSizeLegendWidget *panel = createDataDefinedSizeLegendWidget( s, mRenderer->dataDefinedSizeLegend() );
131  if ( panel )
132  {
133  connect( panel, &QgsPanelWidget::widgetChanged, this, [ = ]
134  {
135  mRenderer->setDataDefinedSizeLegend( panel->dataDefinedSizeLegend() );
136  emit widgetChanged();
137  } );
138  openPanel( panel ); // takes ownership of the panel
139  }
140 }
QgsSingleSymbolRenderer::dataDefinedSizeLegend
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration of appearance of legend when using data-defined size for marker symbols.
Definition: qgssinglesymbolrenderer.cpp:389
QgsRendererWidget::mStyle
QgsStyle * mStyle
Definition: qgsrendererwidget.h:100
QgsSingleSymbolRenderer::convertFromRenderer
static QgsSingleSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a new single symbol renderer from an existing renderer.
Definition: qgssinglesymbolrenderer.cpp:345
QgsSymbol::defaultSymbol
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:320
QgsPanelWidget::setDockMode
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Definition: qgspanelwidget.cpp:44
qgssinglesymbolrenderer.h
QgsRendererWidget::mLayer
QgsVectorLayer * mLayer
Definition: qgsrendererwidget.h:99
QgsSingleSymbolRendererWidget::~QgsSingleSymbolRendererWidget
~QgsSingleSymbolRendererWidget() override
Definition: qgssinglesymbolrendererwidget.cpp:86
QgsRendererWidget::context
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
Definition: qgsrendererwidget.cpp:342
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:36
QgsPanelWidget::openPanel
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Definition: qgspanelwidget.cpp:79
QgsSingleSymbolRenderer
Definition: qgssinglesymbolrenderer.h:30
QgsSingleSymbolRenderer::setDataDefinedSizeLegend
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
Definition: qgssinglesymbolrenderer.cpp:384
qgsdatadefinedsizelegendwidget.h
QgsRendererWidget::symbolLevelsChanged
void symbolLevelsChanged()
Emitted when the symbol levels settings have been changed.
QgsSymbolSelectorWidget
Symbol selector widget that can be used to select and build a symbol.
Definition: qgssymbolselectordialog.h:88
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsSymbolSelectorWidget::setContext
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
Definition: qgssymbolselectordialog.cpp:355
QgsSymbolSelectorWidget::symbolModified
void symbolModified()
Emitted when a symbol is modified in the widget.
QgsPanelWidget::dockMode
bool dockMode()
Returns the dock mode state.
Definition: qgspanelwidget.h:83
QgsPanelWidget::showPanel
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
qgssinglesymbolrendererwidget.h
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:931
QgsRendererWidget::setDockMode
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Definition: qgsrendererwidget.cpp:352
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
QgsSingleSymbolRendererWidget::setDockMode
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Definition: qgssinglesymbolrendererwidget.cpp:108
qgssymbolselectordialog.h
QgsSingleSymbolRendererWidget::create
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Definition: qgssinglesymbolrendererwidget.cpp:30
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
QgsSingleSymbolRenderer::setSymbol
void setSymbol(QgsSymbol *s)
Sets the symbol which will be rendered for every feature.
Definition: qgssinglesymbolrenderer.cpp:101
qgsvectorlayer.h
QgsSymbol::clone
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
QgsRendererWidget::createDataDefinedSizeLegendWidget
QgsDataDefinedSizeLegendWidget * createDataDefinedSizeLegendWidget(const QgsMarkerSymbol *symbol, const QgsDataDefinedSizeLegend *ddsLegend)
Creates widget to setup data-defined size legend.
Definition: qgsrendererwidget.cpp:365
QgsSymbolSelectorWidget::advancedMenu
QMenu * advancedMenu()
Returns menu for "advanced" button - create it if doesn't exist and show the advanced button.
Definition: qgssymbolselectordialog.cpp:344
QgsFeatureRenderer
Definition: qgsrenderer.h:103
QgsStyle
Definition: qgsstyle.h:160
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsSingleSymbolRendererWidget::renderer
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Definition: qgssinglesymbolrendererwidget.cpp:96
QgsSingleSymbolRendererWidget::setContext
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
Definition: qgssinglesymbolrendererwidget.cpp:101
QgsSymbol::type
SymbolType type() const
Returns the symbol's type.
Definition: qgssymbol.h:122
QgsSymbol::Marker
@ Marker
Marker symbol.
Definition: qgssymbol.h:87
qgsdatadefinedsizelegend.h
qgslogger.h
QgsSingleSymbolRenderer::symbol
QgsSymbol * symbol() const
Returns the symbol which will be rendered for every feature.
Definition: qgssinglesymbolrenderer.cpp:96
QgsSingleSymbolRendererWidget::QgsSingleSymbolRendererWidget
QgsSingleSymbolRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Definition: qgssinglesymbolrendererwidget.cpp:35
QgsVectorLayer::geometryType
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
Definition: qgsvectorlayer.cpp:659
QgsRendererWidget::setContext
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
Definition: qgsrendererwidget.cpp:337
qgssymbol.h
QgsRendererWidget::showSymbolLevelsDialog
void showSymbolLevelsDialog(QgsFeatureRenderer *r)
show a dialog with renderer's symbol level settings
Definition: qgsrendererwidget.cpp:316
QgsSymbolSelectorWidget::loadSymbol
void loadSymbol(QgsSymbol *symbol, SymbolLayerItem *parent=nullptr)
Loads the given symbol into the widget.
Definition: qgssymbolselectordialog.cpp:386