QGIS API Documentation 3.41.0-Master (cea29feecf2)
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
91 mMaxErrorSpinBox->setClearValue( 3 );
92
93 connect( mMaxErrorSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
94 connect( mMaxErrorUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged );
95
96 syncToLayer( layer );
97}
98
100{
101 mMapCanvas = context.mapCanvas();
102 mMessageBar = context.messageBar();
103 if ( mActiveWidget )
104 {
105 mActiveWidget->setContext( context );
106 }
107}
108
110{
111 mLayer = qobject_cast<QgsTiledSceneLayer *>( layer );
112
113 mBlockChangedSignal = true;
114 mOpacityWidget->setOpacity( mLayer->opacity() );
115 mBlendModeComboBox->setShowClippingModes( QgsProjectUtils::layerIsContainedInGroupLayer( QgsProject::instance(), mLayer ) );
116 mBlendModeComboBox->setBlendMode( mLayer->blendMode() );
117
118 if ( mLayer->renderer() )
119 {
120 // set current renderer from layer
121 const QString rendererName = mLayer->renderer()->type();
122
123 const int rendererIdx = cboRenderers->findData( rendererName );
124 cboRenderers->setCurrentIndex( rendererIdx );
125
126 // no renderer found... this mustn't happen
127 Q_ASSERT( rendererIdx != -1 && "there must be a renderer!" );
128
129 mMaxErrorSpinBox->setValue( mLayer->renderer()->maximumScreenError() );
130 mMaxErrorUnitWidget->setUnit( mLayer->renderer()->maximumScreenErrorUnit() );
131 }
132
133 mBlockChangedSignal = false;
134}
135
137{
138 if ( mActiveWidget )
139 mActiveWidget->setDockMode( dockMode );
141}
142
144{
145 mLayer->setOpacity( mOpacityWidget->opacity() );
146 mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
147
148 if ( mActiveWidget )
149 mLayer->setRenderer( mActiveWidget->renderer() );
150 else if ( !cboRenderers->currentData().toString().isEmpty() )
151 {
152 QDomElement elem;
153 mLayer->setRenderer( QgsApplication::tiledSceneRendererRegistry()->rendererMetadata( cboRenderers->currentData().toString() )->createRenderer( elem, QgsReadWriteContext() ) );
154 }
155
156 mLayer->renderer()->setMaximumScreenError( mMaxErrorSpinBox->value() );
157 mLayer->renderer()->setMaximumScreenErrorUnit( mMaxErrorUnitWidget->unit() );
158}
159
160void QgsTiledSceneRendererPropertiesWidget::rendererChanged()
161{
162 if ( cboRenderers->currentIndex() == -1 )
163 {
164 QgsDebugError( QStringLiteral( "No current item -- this should never happen!" ) );
165 return;
166 }
167
168 const QString rendererName = cboRenderers->currentData().toString();
169
170 //Retrieve the previous renderer: from the old active widget if possible, otherwise from the layer
171 std::unique_ptr<QgsTiledSceneRenderer> oldRenderer;
172 std::unique_ptr<QgsTiledSceneRenderer> newRenderer;
173 if ( mActiveWidget )
174 newRenderer.reset( mActiveWidget->renderer() );
175
176 if ( newRenderer )
177 {
178 oldRenderer = std::move( newRenderer );
179 }
180 else
181 {
182 oldRenderer.reset( mLayer->renderer()->clone() );
183 }
184
185 // get rid of old active widget (if any)
186 if ( mActiveWidget )
187 {
188 stackedWidget->removeWidget( mActiveWidget );
189
190 delete mActiveWidget;
191 mActiveWidget = nullptr;
192 }
193
194 QgsTiledSceneRendererWidget *widget = nullptr;
196 if ( rendererMetadata )
197 widget = rendererMetadata->createRendererWidget( mLayer, mStyle, oldRenderer.get() );
198 oldRenderer.reset();
199
200 if ( widget )
201 {
202 // instantiate the widget and set as active
203 mActiveWidget = widget;
204 stackedWidget->addWidget( mActiveWidget );
205 stackedWidget->setCurrentWidget( mActiveWidget );
206
207 if ( mMapCanvas || mMessageBar )
208 {
210 context.setMapCanvas( mMapCanvas );
211 context.setMessageBar( mMessageBar );
212 mActiveWidget->setContext( context );
213 }
214
217 widget->setDockMode( dockMode() );
218 }
219 else
220 {
221 // set default "no edit widget available" page
222 stackedWidget->setCurrentWidget( pageNoWidget );
223 }
224 emitWidgetChanged();
225}
226
227void QgsTiledSceneRendererPropertiesWidget::emitWidgetChanged()
228{
229 if ( !mBlockChangedSignal )
230 emit widgetChanged();
231}
@ 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 *)