QGIS API Documentation 3.41.0-Master (3440c17df1d)
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#include "moc_qgstiledscenerendererpropertieswidget.cpp"
17
18#include "qgis.h"
20#include "qgsapplication.h"
23#include "qgstiledscenelayer.h"
27#include "qgslogger.h"
28#include "qgsproject.h"
29#include "qgsprojectutils.h"
30
31static bool initTiledSceneRenderer( const QString &name, QgsTiledSceneRendererWidgetFunc f, const QString &iconName = QString() )
32{
34 if ( !rendererAbstractMetadata )
35 return false;
36 QgsTiledSceneRendererMetadata *rendererMetadata = dynamic_cast<QgsTiledSceneRendererMetadata *>( rendererAbstractMetadata );
37 if ( !rendererMetadata )
38 return false;
39
40 rendererMetadata->setWidgetFunction( f );
41
42 if ( !iconName.isEmpty() )
43 {
44 rendererMetadata->setIcon( QgsApplication::getThemeIcon( iconName ) );
45 }
46
47 QgsDebugMsgLevel( "Set for " + name, 2 );
48 return true;
49}
50
51void QgsTiledSceneRendererPropertiesWidget::initRendererWidgetFunctions()
52{
53 static bool sInitialized = false;
54 if ( sInitialized )
55 return;
56
57 initTiledSceneRenderer( QStringLiteral( "texture" ), QgsTiledSceneTextureRendererWidget::create, QStringLiteral( "styleicons/tiledscenetexture.svg" ) );
58 initTiledSceneRenderer( QStringLiteral( "wireframe" ), QgsTiledSceneWireframeRendererWidget::create, QStringLiteral( "styleicons/tiledscenewireframe.svg" ) );
59
60 sInitialized = true;
61}
62
64 : QgsMapLayerConfigWidget( layer, nullptr, parent )
65 , mLayer( layer )
66 , mStyle( style )
67{
68 setupUi( this );
69
70 layout()->setContentsMargins( 0, 0, 0, 0 );
71
72 // initialize registry's widget functions
73 initRendererWidgetFunctions();
74
76 const QStringList renderers = reg->renderersList();
77 for ( const QString &name : renderers )
78 {
80 cboRenderers->addItem( m->icon(), m->visibleName(), name );
81 }
82
83 cboRenderers->setCurrentIndex( -1 ); // set no current renderer
84
85 connect( cboRenderers, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsTiledSceneRendererPropertiesWidget::rendererChanged );
86
87 connect( mBlendModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
88 connect( mOpacityWidget, &QgsOpacityWidget::opacityChanged, this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
89
90 mMaxErrorUnitWidget->setUnits( { Qgis::RenderUnit::Millimeters,
96 mMaxErrorSpinBox->setClearValue( 3 );
97
98 connect( mMaxErrorSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
99 connect( mMaxErrorUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
100
101 syncToLayer( layer );
102}
103
105{
106 mMapCanvas = context.mapCanvas();
107 mMessageBar = context.messageBar();
108 if ( mActiveWidget )
109 {
110 mActiveWidget->setContext( context );
111 }
112}
113
115{
116 mLayer = qobject_cast< QgsTiledSceneLayer * >( layer );
117
118 mBlockChangedSignal = true;
119 mOpacityWidget->setOpacity( mLayer->opacity() );
120 mBlendModeComboBox->setShowClippingModes( QgsProjectUtils::layerIsContainedInGroupLayer( QgsProject::instance(), mLayer ) );
121 mBlendModeComboBox->setBlendMode( mLayer->blendMode() );
122
123 if ( mLayer->renderer() )
124 {
125 // set current renderer from layer
126 const QString rendererName = mLayer->renderer()->type();
127
128 const int rendererIdx = cboRenderers->findData( rendererName );
129 cboRenderers->setCurrentIndex( rendererIdx );
130
131 // no renderer found... this mustn't happen
132 Q_ASSERT( rendererIdx != -1 && "there must be a renderer!" );
133
134 mMaxErrorSpinBox->setValue( mLayer->renderer()->maximumScreenError() );
135 mMaxErrorUnitWidget->setUnit( mLayer->renderer()->maximumScreenErrorUnit() );
136 }
137
138 mBlockChangedSignal = false;
139}
140
142{
143 if ( mActiveWidget )
144 mActiveWidget->setDockMode( dockMode );
146}
147
149{
150 mLayer->setOpacity( mOpacityWidget->opacity() );
151 mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
152
153 if ( mActiveWidget )
154 mLayer->setRenderer( mActiveWidget->renderer() );
155 else if ( !cboRenderers->currentData().toString().isEmpty() )
156 {
157 QDomElement elem;
158 mLayer->setRenderer( QgsApplication::tiledSceneRendererRegistry()->rendererMetadata( cboRenderers->currentData().toString() )->createRenderer( elem, QgsReadWriteContext() ) );
159 }
160
161 mLayer->renderer()->setMaximumScreenError( mMaxErrorSpinBox->value() );
162 mLayer->renderer()->setMaximumScreenErrorUnit( mMaxErrorUnitWidget->unit() );
163}
164
165void QgsTiledSceneRendererPropertiesWidget::rendererChanged()
166{
167 if ( cboRenderers->currentIndex() == -1 )
168 {
169 QgsDebugError( QStringLiteral( "No current item -- this should never happen!" ) );
170 return;
171 }
172
173 const QString rendererName = cboRenderers->currentData().toString();
174
175 //Retrieve the previous renderer: from the old active widget if possible, otherwise from the layer
176 std::unique_ptr< QgsTiledSceneRenderer > oldRenderer;
177 std::unique_ptr< QgsTiledSceneRenderer > newRenderer;
178 if ( mActiveWidget )
179 newRenderer.reset( mActiveWidget->renderer() );
180
181 if ( newRenderer )
182 {
183 oldRenderer = std::move( newRenderer );
184 }
185 else
186 {
187 oldRenderer.reset( mLayer->renderer()->clone() );
188 }
189
190 // get rid of old active widget (if any)
191 if ( mActiveWidget )
192 {
193 stackedWidget->removeWidget( mActiveWidget );
194
195 delete mActiveWidget;
196 mActiveWidget = nullptr;
197 }
198
199 QgsTiledSceneRendererWidget *widget = nullptr;
201 if ( rendererMetadata )
202 widget = rendererMetadata->createRendererWidget( mLayer, mStyle, oldRenderer.get() );
203 oldRenderer.reset();
204
205 if ( widget )
206 {
207 // instantiate the widget and set as active
208 mActiveWidget = widget;
209 stackedWidget->addWidget( mActiveWidget );
210 stackedWidget->setCurrentWidget( mActiveWidget );
211
212 if ( mMapCanvas || mMessageBar )
213 {
215 context.setMapCanvas( mMapCanvas );
216 context.setMessageBar( mMessageBar );
217 mActiveWidget->setContext( context );
218 }
219
222 widget->setDockMode( dockMode() );
223 }
224 else
225 {
226 // set default "no edit widget available" page
227 stackedWidget->setCurrentWidget( pageNoWidget );
228 }
229 emitWidgetChanged();
230}
231
232void QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged()
233{
234 if ( !mBlockChangedSignal )
235 emit widgetChanged();
236}
237
@ 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:76
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:88
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.
void changed()
Emitted when the selected unit is changed, or the definition of the map unit scale is changed.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
QgsTiledSceneRendererWidget *(* QgsTiledSceneRendererWidgetFunc)(QgsTiledSceneLayer *, QgsStyle *, QgsTiledSceneRenderer *)