QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  if ( renderer )
54  renderer->copyRendererData( mRenderer.get() );
55  }
56 
57  // load symbol from it
58  if ( mRenderer )
59  mSingleSymbol.reset( mRenderer->symbol()->clone() );
60 
61  // setup ui
62  mSelector = new QgsSymbolSelectorWidget( mSingleSymbol.get(), mStyle, mLayer, nullptr );
63  connect( mSelector, &QgsSymbolSelectorWidget::symbolModified, this, &QgsSingleSymbolRendererWidget::changeSingleSymbol );
64  connect( mSelector, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
65 
66  QVBoxLayout *layout = new QVBoxLayout( this );
67  layout->setContentsMargins( 0, 0, 0, 0 );
68  layout->addWidget( mSelector );
69 
70  // advanced actions - data defined rendering
71  QMenu *advMenu = mSelector->advancedMenu();
72 
73  mActionLevels = advMenu->addAction( tr( "Symbol Levels…" ) );
74  connect( mActionLevels, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::showSymbolLevels );
75  if ( mSingleSymbol && mSingleSymbol->type() == Qgis::SymbolType::Marker )
76  {
77  QAction *actionDdsLegend = advMenu->addAction( tr( "Data-defined Size Legend…" ) );
78  connect( actionDdsLegend, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::dataDefinedSizeLegend );
79  }
80 }
81 
83 {
84  mSingleSymbol.reset();
85  mRenderer.reset();
86 
87  delete mSelector;
88 }
89 
91 {
92  return mRenderer.get();
93 }
94 
96 {
98  if ( mSelector )
99  mSelector->setContext( context );
100 }
101 
103 {
105  if ( mSelector )
106  mSelector->setDockMode( dockMode );
107 }
108 
110 {
111  delete mActionLevels;
112  mActionLevels = nullptr;
113 }
114 
115 void QgsSingleSymbolRendererWidget::setSymbolLevels( const QList<QgsLegendSymbolItem> &levels, bool enabled )
116 {
117  mSingleSymbol.reset( levels.at( 0 ).symbol()->clone() );
118  if ( !enabled )
119  {
120  // remove the renderer symbol levels flag (if present), as we don't symbol levels automatically re-enabling when other changes
121  // are made to the symbol
122  mSingleSymbol->setFlags( mSingleSymbol->flags() & ~Qgis::SymbolFlags( Qgis::SymbolFlag::RendererShouldUseSymbolLevels ) );
123  }
124  mRenderer->setSymbol( mSingleSymbol->clone() );
125  mRenderer->setUsingSymbolLevels( enabled );
126  mSelector->loadSymbol( mSingleSymbol.get() );
127  emit widgetChanged();
128 }
129 
130 void QgsSingleSymbolRendererWidget::changeSingleSymbol()
131 {
132  // update symbol from the GUI
133  mRenderer->setSymbol( mSingleSymbol->clone() );
134 
135  if ( mSingleSymbol->flags() & Qgis::SymbolFlag::RendererShouldUseSymbolLevels )
136  mRenderer->setUsingSymbolLevels( true );
137 
138  emit widgetChanged();
139 }
140 
141 void QgsSingleSymbolRendererWidget::showSymbolLevels()
142 {
143  showSymbolLevelsDialog( mRenderer.get() );
144 }
145 
146 void QgsSingleSymbolRendererWidget::dataDefinedSizeLegend()
147 {
148  QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mSingleSymbol.get() ); // this should be only enabled for marker symbols
149  QgsDataDefinedSizeLegendWidget *panel = createDataDefinedSizeLegendWidget( s, mRenderer->dataDefinedSizeLegend() );
150  if ( panel )
151  {
152  connect( panel, &QgsPanelWidget::widgetChanged, this, [ = ]
153  {
154  mRenderer->setDataDefinedSizeLegend( panel->dataDefinedSizeLegend() );
155  emit widgetChanged();
156  } );
157  openPanel( panel ); // takes ownership of the panel
158  }
159 }
QgsFeatureRenderer::copyRendererData
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
QgsRendererWidget::mStyle
QgsStyle * mStyle
Definition: qgsrendererwidget.h:113
QgsSingleSymbolRenderer::convertFromRenderer
static QgsSingleSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a new single symbol renderer from an existing renderer.
Definition: qgssinglesymbolrenderer.cpp:350
QgsSymbol::defaultSymbol
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:673
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:112
Qgis::SymbolFlag::RendererShouldUseSymbolLevels
@ RendererShouldUseSymbolLevels
If present, indicates that a QgsFeatureRenderer using the symbol should use symbol levels for best re...
QgsSingleSymbolRendererWidget::~QgsSingleSymbolRendererWidget
~QgsSingleSymbolRendererWidget() override
Definition: qgssinglesymbolrendererwidget.cpp:82
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:346
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:35
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:84
qgsdatadefinedsizelegendwidget.h
QgsSymbolSelectorWidget
Symbol selector widget that can be used to select and build a symbol.
Definition: qgssymbolselectordialog.h:87
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
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:93
QgsPanelWidget::showPanel
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
qgssinglesymbolrendererwidget.h
QgsSingleSymbolRendererWidget::setSymbolLevels
void setSymbolLevels(const QList< QgsLegendSymbolItem > &levels, bool enabled) override
Sets the symbol levels for the renderer defined in the widget.
Definition: qgssinglesymbolrendererwidget.cpp:115
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgsmarkersymbol.h:30
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:356
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:102
qgssymbolselectordialog.h
QgsSingleSymbolRendererWidget::create
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Definition: qgssinglesymbolrendererwidget.cpp:31
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
qgsvectorlayer.h
QgsRendererWidget::createDataDefinedSizeLegendWidget
QgsDataDefinedSizeLegendWidget * createDataDefinedSizeLegendWidget(const QgsMarkerSymbol *symbol, const QgsDataDefinedSizeLegend *ddsLegend)
Creates widget to setup data-defined size legend.
Definition: qgsrendererwidget.cpp:373
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:101
QgsStyle
Definition: qgsstyle.h:159
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
qgsmarkersymbol.h
QgsSingleSymbolRendererWidget::renderer
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Definition: qgssinglesymbolrendererwidget.cpp:90
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:95
qgsdatadefinedsizelegend.h
qgslogger.h
QgsSingleSymbolRendererWidget::QgsSingleSymbolRendererWidget
QgsSingleSymbolRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Definition: qgssinglesymbolrendererwidget.cpp:36
QgsVectorLayer::geometryType
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
Definition: qgsvectorlayer.cpp:720
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:341
qgssymbol.h
QgsSingleSymbolRendererWidget::disableSymbolLevels
void disableSymbolLevels() override
Disables symbol level modification on the widget.
Definition: qgssinglesymbolrendererwidget.cpp:109
Qgis::SymbolType::Marker
@ Marker
Marker symbol.
QgsRendererWidget::showSymbolLevelsDialog
void showSymbolLevelsDialog(QgsFeatureRenderer *r)
Show a dialog with renderer's symbol level settings.
Definition: qgsrendererwidget.cpp:318
QgsSymbolSelectorWidget::loadSymbol
void loadSymbol(QgsSymbol *symbol, SymbolLayerItem *parent=nullptr)
Loads the given symbol into the widget.
Definition: qgssymbolselectordialog.cpp:383