QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgstiledscenerendererpropertieswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstiledscenerendererpropertieswidget.cpp
3 ---------------------
4 begin : August 2023
5 copyright : (C) 2023 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 ***************************************************************************/
16
17#include "qgis.h"
19#include "qgsapplication.h"
22#include "qgstiledscenelayer.h"
26#include "qgslogger.h"
27#include "qgsproject.h"
28#include "qgsprojectutils.h"
29
30static bool initTiledSceneRenderer( const QString &name, QgsTiledSceneRendererWidgetFunc f, const QString &iconName = QString() )
31{
33 if ( !rendererAbstractMetadata )
34 return false;
35 QgsTiledSceneRendererMetadata *rendererMetadata = dynamic_cast<QgsTiledSceneRendererMetadata *>( rendererAbstractMetadata );
36 if ( !rendererMetadata )
37 return false;
38
39 rendererMetadata->setWidgetFunction( f );
40
41 if ( !iconName.isEmpty() )
42 {
43 rendererMetadata->setIcon( QgsApplication::getThemeIcon( iconName ) );
44 }
45
46 QgsDebugMsgLevel( "Set for " + name, 2 );
47 return true;
48}
49
50void QgsTiledSceneRendererPropertiesWidget::initRendererWidgetFunctions()
51{
52 static bool sInitialized = false;
53 if ( sInitialized )
54 return;
55
56 initTiledSceneRenderer( QStringLiteral( "texture" ), QgsTiledSceneTextureRendererWidget::create, QStringLiteral( "styleicons/tiledscenetexture.svg" ) );
57 initTiledSceneRenderer( QStringLiteral( "wireframe" ), QgsTiledSceneWireframeRendererWidget::create, QStringLiteral( "styleicons/tiledscenewireframe.svg" ) );
58
59 sInitialized = true;
60}
61
63 : QgsMapLayerConfigWidget( layer, nullptr, parent )
64 , mLayer( layer )
65 , mStyle( style )
66{
67 setupUi( this );
68
69 layout()->setContentsMargins( 0, 0, 0, 0 );
70
71 // initialize registry's widget functions
72 initRendererWidgetFunctions();
73
75 const QStringList renderers = reg->renderersList();
76 for ( const QString &name : renderers )
77 {
79 cboRenderers->addItem( m->icon(), m->visibleName(), name );
80 }
81
82 cboRenderers->setCurrentIndex( -1 ); // set no current renderer
83
84 connect( cboRenderers, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsTiledSceneRendererPropertiesWidget::rendererChanged );
85
86 connect( mBlendModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
87 connect( mOpacityWidget, &QgsOpacityWidget::opacityChanged, this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
88
89 mMaxErrorUnitWidget->setUnits( { Qgis::RenderUnit::Millimeters,
95 mMaxErrorSpinBox->setClearValue( 3 );
96
97 connect( mMaxErrorSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
98 connect( mMaxErrorUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
99
100 syncToLayer( layer );
101}
102
104{
105 mMapCanvas = context.mapCanvas();
106 mMessageBar = context.messageBar();
107 if ( mActiveWidget )
108 {
109 mActiveWidget->setContext( context );
110 }
111}
112
114{
115 mLayer = qobject_cast< QgsTiledSceneLayer * >( layer );
116
117 mBlockChangedSignal = true;
118 mOpacityWidget->setOpacity( mLayer->opacity() );
119 mBlendModeComboBox->setShowClippingModes( QgsProjectUtils::layerIsContainedInGroupLayer( QgsProject::instance(), mLayer ) );
120 mBlendModeComboBox->setBlendMode( mLayer->blendMode() );
121
122 if ( mLayer->renderer() )
123 {
124 // set current renderer from layer
125 const QString rendererName = mLayer->renderer()->type();
126
127 const int rendererIdx = cboRenderers->findData( rendererName );
128 cboRenderers->setCurrentIndex( rendererIdx );
129
130 // no renderer found... this mustn't happen
131 Q_ASSERT( rendererIdx != -1 && "there must be a renderer!" );
132
133 mMaxErrorSpinBox->setValue( mLayer->renderer()->maximumScreenError() );
134 mMaxErrorUnitWidget->setUnit( mLayer->renderer()->maximumScreenErrorUnit() );
135 }
136
137 mBlockChangedSignal = false;
138}
139
141{
142 if ( mActiveWidget )
143 mActiveWidget->setDockMode( dockMode );
145}
146
148{
149 mLayer->setOpacity( mOpacityWidget->opacity() );
150 mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
151
152 if ( mActiveWidget )
153 mLayer->setRenderer( mActiveWidget->renderer() );
154 else if ( !cboRenderers->currentData().toString().isEmpty() )
155 {
156 QDomElement elem;
157 mLayer->setRenderer( QgsApplication::tiledSceneRendererRegistry()->rendererMetadata( cboRenderers->currentData().toString() )->createRenderer( elem, QgsReadWriteContext() ) );
158 }
159
160 mLayer->renderer()->setMaximumScreenError( mMaxErrorSpinBox->value() );
161 mLayer->renderer()->setMaximumScreenErrorUnit( mMaxErrorUnitWidget->unit() );
162}
163
164void QgsTiledSceneRendererPropertiesWidget::rendererChanged()
165{
166 if ( cboRenderers->currentIndex() == -1 )
167 {
168 QgsDebugError( QStringLiteral( "No current item -- this should never happen!" ) );
169 return;
170 }
171
172 const QString rendererName = cboRenderers->currentData().toString();
173
174 //Retrieve the previous renderer: from the old active widget if possible, otherwise from the layer
175 std::unique_ptr< QgsTiledSceneRenderer > oldRenderer;
176 std::unique_ptr< QgsTiledSceneRenderer > newRenderer;
177 if ( mActiveWidget )
178 newRenderer.reset( mActiveWidget->renderer() );
179
180 if ( newRenderer )
181 {
182 oldRenderer = std::move( newRenderer );
183 }
184 else
185 {
186 oldRenderer.reset( mLayer->renderer()->clone() );
187 }
188
189 // get rid of old active widget (if any)
190 if ( mActiveWidget )
191 {
192 stackedWidget->removeWidget( mActiveWidget );
193
194 delete mActiveWidget;
195 mActiveWidget = nullptr;
196 }
197
198 QgsTiledSceneRendererWidget *widget = nullptr;
200 if ( rendererMetadata )
201 widget = rendererMetadata->createRendererWidget( mLayer, mStyle, oldRenderer.get() );
202 oldRenderer.reset();
203
204 if ( widget )
205 {
206 // instantiate the widget and set as active
207 mActiveWidget = widget;
208 stackedWidget->addWidget( mActiveWidget );
209 stackedWidget->setCurrentWidget( mActiveWidget );
210
211 if ( mMapCanvas || mMessageBar )
212 {
214 context.setMapCanvas( mMapCanvas );
215 context.setMessageBar( mMessageBar );
216 mActiveWidget->setContext( context );
217 }
218
221 widget->setDockMode( dockMode() );
222 }
223 else
224 {
225 // set default "no edit widget available" page
226 stackedWidget->setCurrentWidget( pageNoWidget );
227 }
228 emitWidgetChanged();
229}
230
231void QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged()
232{
233 if ( !mBlockChangedSignal )
234 emit widgetChanged();
235}
236
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsTiledSceneRendererRegistry * tiledSceneRendererRegistry()
Returns the application's tiled scene renderer registry, used for managing tiled scene layer 2D rende...
A panel widget that can be shown in the map style dock.
Base class for all map layer types.
Definition qgsmaplayer.h:74
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
double opacity
Definition qgsmaplayer.h:83
void opacityChanged(double opacity)
Emitted when the opacity is changed in the widget, where opacity ranges from 0.0 (transparent) to 1....
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.
static bool layerIsContainedInGroupLayer(QgsProject *project, QgsMapLayer *layer)
Returns true if the specified layer is a child layer from any QgsGroupLayer in the given project.
static QgsProject * instance()
Returns the QgsProject singleton instance.
The class is used as a container of context for various read/write operations on other objects.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
Represents a map layer supporting display of tiled scene objects.
void setRenderer(QgsTiledSceneRenderer *renderer)
Sets the 2D renderer for the tiled scene.
QgsTiledSceneRenderer * renderer()
Returns the 2D renderer for the tiled scene.
Stores metadata about one tiled scene renderer class.
void setIcon(const QIcon &icon)
Sets an icon representing the renderer.
virtual QgsTiledSceneRendererWidget * createRendererWidget(QgsTiledSceneLayer *layer, QgsStyle *style, QgsTiledSceneRenderer *oldRenderer)
Returns new instance of settings widget for the renderer.
Convenience metadata class that uses static functions to create tiled scene renderer and its widget.
QgsTiledSceneRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Returns new instance of the renderer given the DOM element.
void setWidgetFunction(QgsTiledSceneRendererWidgetFunc f)
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the widget is shown, e.g., the associated map canvas and expression context...
QgsTiledSceneRendererPropertiesWidget(QgsTiledSceneLayer *layer, QgsStyle *style, QWidget *parent=nullptr)
Constructor for QgsTiledSceneRendererPropertiesWidget, associated with the specified layer and style ...
void setDockMode(bool dockMode) final
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
void syncToLayer(QgsMapLayer *layer) final
Reset to original (vector layer) values.
Registry of 2D renderers for tiled scenes.
QgsTiledSceneRendererAbstractMetadata * rendererMetadata(const QString &rendererName)
Returns the metadata for a specified renderer.
QStringList renderersList() const
Returns a list of available renderers.
Base class for tiled scene 2D renderer settings widgets.
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
virtual QgsTiledSceneRenderer * renderer()=0
Returns a new instance of a renderer as defined by the settings in the widget.
virtual QString type() const =0
Returns the identifier of the renderer type.
void setMaximumScreenErrorUnit(Qgis::RenderUnit unit)
Sets the unit for the maximum screen error allowed when rendering the tiled scene.
Qgis::RenderUnit maximumScreenErrorUnit() const
Returns the unit for the maximum screen error allowed when rendering the tiled scene.
virtual QgsTiledSceneRenderer * clone() const =0
Create a deep copy of this renderer.
double maximumScreenError() const
Returns the maximum screen error allowed when rendering the tiled scene.
void setMaximumScreenError(double error)
Sets the maximum screen error allowed when rendering the tiled scene.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
QgsTiledSceneRendererWidget *(* QgsTiledSceneRendererWidgetFunc)(QgsTiledSceneLayer *, QgsStyle *, QgsTiledSceneRenderer *)