QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include "qgsmarkersymbol.h"
27 
28 #include <QMenu>
29 
30 
32 {
33  return new QgsSingleSymbolRendererWidget( layer, style, renderer );
34 }
35 
37  : QgsRendererWidget( layer, style )
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 = std::make_unique< QgsSingleSymbolRenderer >( symbol );
52  }
53 
54  // load symbol from it
55  if ( mRenderer )
56  mSingleSymbol.reset( mRenderer->symbol()->clone() );
57 
58  // setup ui
59  mSelector = new QgsSymbolSelectorWidget( mSingleSymbol.get(), mStyle, mLayer, nullptr );
60  connect( mSelector, &QgsSymbolSelectorWidget::symbolModified, this, &QgsSingleSymbolRendererWidget::changeSingleSymbol );
61  connect( mSelector, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
62 
63  QVBoxLayout *layout = new QVBoxLayout( this );
64  layout->setContentsMargins( 0, 0, 0, 0 );
65  layout->addWidget( mSelector );
66 
67  // advanced actions - data defined rendering
68  QMenu *advMenu = mSelector->advancedMenu();
69 
70  mActionLevels = advMenu->addAction( tr( "Symbol Levels…" ) );
71  connect( mActionLevels, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::showSymbolLevels );
72  if ( mSingleSymbol && mSingleSymbol->type() == Qgis::SymbolType::Marker )
73  {
74  QAction *actionDdsLegend = advMenu->addAction( tr( "Data-defined Size Legend…" ) );
75  connect( actionDdsLegend, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::dataDefinedSizeLegend );
76  }
77 }
78 
80 {
81  mSingleSymbol.reset();
82  mRenderer.reset();
83 
84  delete mSelector;
85 }
86 
88 {
89  return mRenderer.get();
90 }
91 
93 {
95  if ( mSelector )
96  mSelector->setContext( context );
97 }
98 
100 {
102  if ( mSelector )
103  mSelector->setDockMode( dockMode );
104 }
105 
107 {
108  delete mActionLevels;
109  mActionLevels = nullptr;
110 }
111 
112 void QgsSingleSymbolRendererWidget::setSymbolLevels( const QList<QgsLegendSymbolItem> &levels, bool enabled )
113 {
114  mSingleSymbol.reset( levels.at( 0 ).symbol()->clone() );
115  if ( !enabled )
116  {
117  // remove the renderer symbol levels flag (if present), as we don't symbol levels automatically re-enabling when other changes
118  // are made to the symbol
119  mSingleSymbol->setFlags( mSingleSymbol->flags() & ~Qgis::SymbolFlags( Qgis::SymbolFlag::RendererShouldUseSymbolLevels ) );
120  }
121  mRenderer->setSymbol( mSingleSymbol->clone() );
122  mRenderer->setUsingSymbolLevels( enabled );
123  mSelector->loadSymbol( mSingleSymbol.get() );
124  emit widgetChanged();
125 }
126 
127 void QgsSingleSymbolRendererWidget::changeSingleSymbol()
128 {
129  // update symbol from the GUI
130  mRenderer->setSymbol( mSingleSymbol->clone() );
131 
132  if ( mSingleSymbol->flags() & Qgis::SymbolFlag::RendererShouldUseSymbolLevels )
133  mRenderer->setUsingSymbolLevels( true );
134 
135  emit widgetChanged();
136 }
137 
138 void QgsSingleSymbolRendererWidget::showSymbolLevels()
139 {
140  showSymbolLevelsDialog( mRenderer.get() );
141 }
142 
143 void QgsSingleSymbolRendererWidget::dataDefinedSizeLegend()
144 {
145  QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mSingleSymbol.get() ); // this should be only enabled for marker symbols
146  QgsDataDefinedSizeLegendWidget *panel = createDataDefinedSizeLegendWidget( s, mRenderer->dataDefinedSizeLegend() );
147  if ( panel )
148  {
149  connect( panel, &QgsPanelWidget::widgetChanged, this, [ = ]
150  {
151  mRenderer->setDataDefinedSizeLegend( panel->dataDefinedSizeLegend() );
152  emit widgetChanged();
153  } );
154  openPanel( panel ); // takes ownership of the panel
155  }
156 }
@ Marker
Marker symbol.
@ RendererShouldUseSymbolLevels
If present, indicates that a QgsFeatureRenderer using the symbol should use symbol levels for best re...
Widget for configuration of appearance of legend for marker symbols with data-defined size.
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration as set up in the dialog (may be nullptr). Ownership is passed to the caller.
A marker symbol type, for rendering Point and MultiPoint geometries.
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void widgetChanged()
Emitted when the widget state changes.
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
bool dockMode()
Returns the dock mode state.
Base class for renderer settings widgets.
void showSymbolLevelsDialog(QgsFeatureRenderer *r)
Show a dialog with renderer's symbol level settings.
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsDataDefinedSizeLegendWidget * createDataDefinedSizeLegendWidget(const QgsMarkerSymbol *symbol, const QgsDataDefinedSizeLegend *ddsLegend)
Creates widget to setup data-defined size legend.
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
QgsVectorLayer * mLayer
void disableSymbolLevels() override
Disables symbol level modification on the widget.
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
QgsSingleSymbolRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
void setSymbolLevels(const QList< QgsLegendSymbolItem > &levels, bool enabled) override
Sets the symbol levels for the renderer defined in the widget.
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
static QgsSingleSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a new single symbol renderer from an existing renderer.
Symbol selector widget that can be used to select and build a symbol.
void loadSymbol(QgsSymbol *symbol, SymbolLayerItem *parent=nullptr)
Loads the given symbol into the widget.
QMenu * advancedMenu()
Returns menu for "advanced" button - create it if doesn't exist and show the advanced button.
void symbolModified()
Emitted when a symbol is modified in the widget.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:355
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.