QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "moc_qgssinglesymbolrendererwidget.cpp"
17
21#include "qgssymbol.h"
22
23#include "qgslogger.h"
24#include "qgsvectorlayer.h"
25
27#include "qgsmarkersymbol.h"
28
29#include <QMenu>
30
31
36
38 : QgsRendererWidget( layer, style )
39{
40 // try to recognize the previous renderer
41 // (null renderer means "no previous renderer")
42
43 if ( renderer )
44 {
46 }
47 if ( !mRenderer )
48 {
50
51 if ( symbol )
52 mRenderer = std::make_unique< QgsSingleSymbolRenderer >( symbol );
53
54 if ( renderer )
55 renderer->copyRendererData( mRenderer.get() );
56 }
57
58 // load symbol from it
59 if ( mRenderer )
60 mSingleSymbol.reset( mRenderer->symbol()->clone() );
61
62 // setup ui
63 mSelector = new QgsSymbolSelectorWidget( mSingleSymbol.get(), mStyle, mLayer, nullptr );
64 connect( mSelector, &QgsSymbolSelectorWidget::symbolModified, this, &QgsSingleSymbolRendererWidget::changeSingleSymbol );
65 connect( mSelector, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
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 mActionLevels = advMenu->addAction( tr( "Symbol Levels…" ) );
75 connect( mActionLevels, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::showSymbolLevels );
76 if ( mSingleSymbol && mSingleSymbol->type() == Qgis::SymbolType::Marker )
77 {
78 QAction *actionDdsLegend = advMenu->addAction( tr( "Data-defined Size Legend…" ) );
79 connect( actionDdsLegend, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::dataDefinedSizeLegend );
80 }
81}
82
84{
85 mSingleSymbol.reset();
86 mRenderer.reset();
87
88 delete mSelector;
89}
90
92{
93 return mRenderer.get();
94}
95
97{
99 if ( mSelector )
100 mSelector->setContext( context );
101}
102
104{
106 if ( mSelector )
107 mSelector->setDockMode( dockMode );
108}
109
111{
112 delete mActionLevels;
113 mActionLevels = nullptr;
114}
115
116void QgsSingleSymbolRendererWidget::setSymbolLevels( const QList<QgsLegendSymbolItem> &levels, bool enabled )
117{
118 mSingleSymbol.reset( levels.at( 0 ).symbol()->clone() );
119 if ( !enabled )
120 {
121 // remove the renderer symbol levels flag (if present), as we don't symbol levels automatically re-enabling when other changes
122 // are made to the symbol
123 mSingleSymbol->setFlags( mSingleSymbol->flags() & ~Qgis::SymbolFlags( Qgis::SymbolFlag::RendererShouldUseSymbolLevels ) );
124 }
125 mRenderer->setSymbol( mSingleSymbol->clone() );
126 mRenderer->setUsingSymbolLevels( enabled );
127 mSelector->loadSymbol( mSingleSymbol.get() );
128 emit widgetChanged();
129}
130
131void QgsSingleSymbolRendererWidget::changeSingleSymbol()
132{
133 // update symbol from the GUI
134 mRenderer->setSymbol( mSingleSymbol->clone() );
135
136 if ( mSingleSymbol->flags() & Qgis::SymbolFlag::RendererShouldUseSymbolLevels )
137 mRenderer->setUsingSymbolLevels( true );
138
139 emit widgetChanged();
140}
141
142void QgsSingleSymbolRendererWidget::showSymbolLevels()
143{
144 showSymbolLevelsDialog( mRenderer.get() );
145}
146
147void QgsSingleSymbolRendererWidget::dataDefinedSizeLegend()
148{
149 QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mSingleSymbol.get() ); // this should be only enabled for marker symbols
150 QgsDataDefinedSizeLegendWidget *panel = createDataDefinedSizeLegendWidget( s, mRenderer->dataDefinedSizeLegend() );
151 if ( panel )
152 {
153 connect( panel, &QgsPanelWidget::widgetChanged, this, [ = ]
154 {
155 mRenderer->setDataDefinedSizeLegend( panel->dataDefinedSizeLegend() );
156 emit widgetChanged();
157 } );
158 openPanel( panel ); // takes ownership of the panel
159 }
160}
QFlags< SymbolFlag > SymbolFlags
Symbol flags.
Definition qgis.h:787
@ 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.
Abstract base class for all 2D vector feature renderers.
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
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:231
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.