QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmergedfeaturerendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmergedfeaturerendererwidget.cpp
3  ---------------------
4  begin : December 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson 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  ***************************************************************************/
17 #include "qgsrendererregistry.h"
18 
19 #include "qgssymbol.h"
20 
21 #include "qgslogger.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsapplication.h"
24 
26 {
27  return new QgsMergedFeatureRendererWidget( layer, style, renderer );
28 }
29 
31  : QgsRendererWidget( layer, style )
32 {
33  if ( !layer )
34  {
35  return;
36  }
37 
39 
40  // the renderer only applies to line or polygon vector layers
42  {
43  //setup blank dialog
44  mRenderer.reset( nullptr );
45  QGridLayout *layout = new QGridLayout( this );
46  QLabel *label = new QLabel( tr( "The merged feature renderer only applies to line and polygon layers. \n"
47  "'%1' is not a line or polygon layer and then cannot be displayed" )
48  .arg( layer->name() ), this );
49  this->setLayout( layout );
50  layout->addWidget( label );
51  return;
52  }
53  setupUi( this );
54  connect( mRendererComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsMergedFeatureRendererWidget::mRendererComboBox_currentIndexChanged );
55 
56  // try to recognize the previous renderer
57  // (null renderer means "no previous renderer")
58  if ( renderer )
59  {
61  }
62  if ( ! mRenderer )
63  {
64  // use default embedded renderer
65  mRenderer.reset( new QgsMergedFeatureRenderer( QgsFeatureRenderer::defaultRenderer( type ) ) );
66  if ( renderer )
67  renderer->copyRendererData( mRenderer.get() );
68  }
69 
70  int currentEmbeddedIdx = 0;
71  //insert possible renderer types
73  QStringList::const_iterator it = rendererList.constBegin();
74  int idx = 0;
75  mRendererComboBox->blockSignals( true );
76  for ( ; it != rendererList.constEnd(); ++it, ++idx )
77  {
78  if ( *it != QLatin1String( "mergedFeatureRenderer" )
79  && *it != QLatin1String( "invertedPolygonRenderer" ) ) //< an merged renderer cannot contain another merged or inverted renderer
80  {
82  mRendererComboBox->addItem( m->icon(), m->visibleName(), /* data */ *it );
83  const QgsFeatureRenderer *embeddedRenderer = mRenderer->embeddedRenderer();
84  if ( embeddedRenderer && embeddedRenderer->type() == m->name() )
85  {
86  // store the combo box index of the current renderer
87  currentEmbeddedIdx = idx;
88  }
89  }
90  }
91  mRendererComboBox->blockSignals( false );
92 
93  const int oldIdx = mRendererComboBox->currentIndex();
94  mRendererComboBox->setCurrentIndex( currentEmbeddedIdx );
95  if ( oldIdx == currentEmbeddedIdx )
96  {
97  // force update
98  mRendererComboBox_currentIndexChanged( currentEmbeddedIdx );
99  }
100 }
101 
103 
105 {
106  if ( mRenderer && mEmbeddedRendererWidget )
107  {
108  QgsFeatureRenderer *embeddedRenderer = mEmbeddedRendererWidget->renderer();
109  if ( embeddedRenderer )
110  {
111  mRenderer->setEmbeddedRenderer( embeddedRenderer->clone() );
112  }
113  }
114  return mRenderer.get();
115 }
116 
118 {
120  if ( mEmbeddedRendererWidget )
121  mEmbeddedRendererWidget->setContext( context );
122 }
123 
125 {
127  if ( mEmbeddedRendererWidget )
128  mEmbeddedRendererWidget->setDockMode( dockMode );
129 }
130 
131 void QgsMergedFeatureRendererWidget::mRendererComboBox_currentIndexChanged( int index )
132 {
133  const QString rendererId = mRendererComboBox->itemData( index ).toString();
135  if ( m )
136  {
137  const std::unique_ptr< QgsFeatureRenderer > oldRenderer( mRenderer->embeddedRenderer()->clone() );
138  mEmbeddedRendererWidget.reset( m->createRendererWidget( mLayer, mStyle, oldRenderer.get() ) );
139  connect( mEmbeddedRendererWidget.get(), &QgsRendererWidget::widgetChanged, this, &QgsMergedFeatureRendererWidget::widgetChanged );
140  mEmbeddedRendererWidget->setContext( mContext );
141  mEmbeddedRendererWidget->disableSymbolLevels();
142  mEmbeddedRendererWidget->setDockMode( this->dockMode() );
143  connect( mEmbeddedRendererWidget.get(), &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
144 
145  if ( layout()->count() > 2 )
146  {
147  // remove the current renderer widget
148  layout()->takeAt( 2 );
149  }
150  layout()->addWidget( mEmbeddedRendererWidget.get() );
151  }
152 }
QgsMergedFeatureRendererWidget::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: qgsmergedfeaturerendererwidget.cpp:124
QgsFeatureRenderer::copyRendererData
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
QgsMergedFeatureRendererWidget::renderer
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Definition: qgsmergedfeaturerendererwidget.cpp:104
QgsRendererWidget::mStyle
QgsStyle * mStyle
Definition: qgsrendererwidget.h:113
QgsApplication::rendererRegistry
static QgsRendererRegistry * rendererRegistry()
Returns the application's renderer registry, used for managing vector layer renderers.
Definition: qgsapplication.cpp:2320
QgsFeatureRenderer::defaultRenderer
static QgsFeatureRenderer * defaultRenderer(QgsWkbTypes::GeometryType geomType)
Returns a new renderer - used by default in vector layers.
Definition: qgsrenderer.cpp:79
QgsMergedFeatureRendererWidget::create
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Static creation method.
Definition: qgsmergedfeaturerendererwidget.cpp:25
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:725
QgsMergedFeatureRendererWidget::~QgsMergedFeatureRendererWidget
~QgsMergedFeatureRendererWidget() override
QgsRendererWidget::mLayer
QgsVectorLayer * mLayer
Definition: qgsrendererwidget.h:112
qgsmergedfeaturerenderer.h
QgsRendererAbstractMetadata::visibleName
QString visibleName() const
Returns a friendly display name of the renderer.
Definition: qgsrendererregistry.h:82
QgsRendererAbstractMetadata::name
QString name() const
Returns the unique name of the renderer.
Definition: qgsrendererregistry.h:76
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
QgsFeatureRenderer::type
QString type() const
Definition: qgsrenderer.h:142
QgsRendererAbstractMetadata::LineLayer
@ LineLayer
Compatible with line layers.
Definition: qgsrendererregistry.h:52
QgsPanelWidget::dockMode
bool dockMode()
Returns the dock mode state.
Definition: qgspanelwidget.h:93
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsPanelWidget::showPanel
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
qgsapplication.h
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
qgsrendererregistry.h
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.
QgsMergedFeatureRendererWidget::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: qgsmergedfeaturerendererwidget.cpp:117
QgsRendererRegistry::renderersList
QStringList renderersList(QgsRendererAbstractMetadata::LayerTypes layerTypes=QgsRendererAbstractMetadata::All) const
Returns a list of available renderers.
Definition: qgsrendererregistry.cpp:137
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
QgsMergedFeatureRendererWidget::QgsMergedFeatureRendererWidget
QgsMergedFeatureRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor.
Definition: qgsmergedfeaturerendererwidget.cpp:30
qgsvectorlayer.h
QgsMergedFeatureRenderer::convertFromRenderer
static QgsMergedFeatureRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a QgsMergedFeatureRenderer by a conversion from an existing renderer.
Definition: qgsmergedfeaturerenderer.cpp:554
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsStyle
Definition: qgsstyle.h:159
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsRendererWidget::mContext
QgsSymbolWidgetContext mContext
Context in which widget is shown.
Definition: qgsrendererwidget.h:131
qgsmergedfeaturerendererwidget.h
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:76
QgsWkbTypes::geometryType
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
QgsRendererAbstractMetadata::icon
QIcon icon() const
Returns an icon representing the renderer.
Definition: qgsrendererregistry.h:88
QgsMergedFeatureRenderer
QgsMergedFeatureRenderer is a polygon or line-only feature renderer used to renderer a set of feature...
Definition: qgsmergedfeaturerenderer.h:40
qgslogger.h
QgsRendererAbstractMetadata::createRendererWidget
virtual QgsRendererWidget * createRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *oldRenderer)
Returns new instance of settings widget for the renderer.
Definition: qgsrendererregistry.h:115
QgsRendererRegistry::rendererMetadata
QgsRendererAbstractMetadata * rendererMetadata(const QString &rendererName)
Returns the metadata for a specified renderer.
Definition: qgsrendererregistry.cpp:132
QgsRendererAbstractMetadata
Stores metadata about one renderer class.
Definition: qgsrendererregistry.h:41
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
QgsRendererAbstractMetadata::PolygonLayer
@ PolygonLayer
Compatible with polygon layers.
Definition: qgsrendererregistry.h:53