QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsrenderermeshpropertieswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrenderermeshpropertieswidget.cpp
3  -----------------------------------
4  begin : June 2018
5  copyright : (C) 2018 by Peter Petrik
6  email : zilolv 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  ***************************************************************************/
15 
17 
18 #include "qgis.h"
19 #include "qgsmapcanvas.h"
20 #include "qgsmeshlayer.h"
21 #include "qgsmessagelog.h"
25 #include "qgsmeshlayerutils.h"
26 #include "qgsproject.h"
27 #include "qgsprojectutils.h"
28 
30  : QgsMapLayerConfigWidget( layer, canvas, parent )
31  , mMeshLayer( layer )
32 {
33  if ( !mMeshLayer )
34  return;
35 
36  setupUi( this );
37 
38  connect( mMeshLayer,
40  this,
41  &QgsRendererMeshPropertiesWidget::syncToLayerPrivate );
42 
43  mMeshRendererActiveDatasetWidget->setLayer( mMeshLayer );
44  mMeshRendererScalarSettingsWidget->setLayer( mMeshLayer );
45  mNativeMeshSettingsWidget->setLayer( mMeshLayer, QgsMeshRendererMeshSettingsWidget::MeshType::Native );
46  mTriangularMeshSettingsWidget->setLayer( mMeshLayer, QgsMeshRendererMeshSettingsWidget::MeshType::Triangular );
47  mEdgeMeshSettingsWidget->setLayer( mMeshLayer, QgsMeshRendererMeshSettingsWidget::MeshType::Edge );
48  mMeshRendererVectorSettingsWidget->setLayer( mMeshLayer );
49  m3dAveragingSettingsWidget->setLayer( mMeshLayer );
50  syncToLayer( mMeshLayer );
51 
52  //blend mode
53  mBlendModeComboBox->setShowClippingModes( QgsProjectUtils::layerIsContainedInGroupLayer( QgsProject::instance(), mMeshLayer ) );
54  mBlendModeComboBox->setBlendMode( mMeshLayer->blendMode() );
55  connect( mBlendModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
56 
57  connect( mMeshRendererActiveDatasetWidget, &QgsMeshRendererActiveDatasetWidget::activeScalarGroupChanged,
58  this, &QgsRendererMeshPropertiesWidget::onActiveScalarGroupChanged );
59  connect( mMeshRendererActiveDatasetWidget, &QgsMeshRendererActiveDatasetWidget::activeVectorGroupChanged,
60  this, &QgsRendererMeshPropertiesWidget::onActiveVectorGroupChanged );
61 
62  connect( mNativeMeshGroup, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
63  connect( mEdgeMeshGroup, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
64  connect( mTriangularMeshGroup, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
65  connect( mContoursGroupBox, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
66  connect( mVectorsGroupBox, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
67  connect( mMeshRendererActiveDatasetWidget, &QgsMeshRendererActiveDatasetWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
68  connect( mMeshRendererScalarSettingsWidget, &QgsMeshRendererScalarSettingsWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
69  connect( mMeshRendererVectorSettingsWidget, &QgsMeshRendererVectorSettingsWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
70  connect( mNativeMeshSettingsWidget, &QgsMeshRendererMeshSettingsWidget::widgetChanged,
72  connect( mTriangularMeshSettingsWidget, &QgsMeshRendererMeshSettingsWidget::widgetChanged,
74  connect( mEdgeMeshSettingsWidget, &QgsMeshRendererMeshSettingsWidget::widgetChanged,
76  connect( m3dAveragingSettingsWidget, &QgsMeshRenderer3dAveragingWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
77 }
78 
80 {
81  if ( !mMeshLayer )
82  return;
83 
84  // 1D EDGE MESH
85  const bool edgeMeshRenderingIsEnabled = mEdgeMeshGroup->isChecked();
86  QgsMeshRendererMeshSettings edgeMeshSettings = mEdgeMeshSettingsWidget->settings();
87  edgeMeshSettings.setEnabled( edgeMeshRenderingIsEnabled );
88 
89  // 2D NATIVE MESH
90  const bool nativeMeshRenderingIsEnabled = mNativeMeshGroup->isChecked();
91  QgsMeshRendererMeshSettings nativeMeshSettings = mNativeMeshSettingsWidget->settings();
92  nativeMeshSettings.setEnabled( nativeMeshRenderingIsEnabled );
93 
94  // 2D TRIANGULAR MESH
95  const bool triangularMeshRenderingIsEnabled = mTriangularMeshGroup->isChecked();
96  QgsMeshRendererMeshSettings triangularMeshSettings = mTriangularMeshSettingsWidget->settings();
97  triangularMeshSettings.setEnabled( triangularMeshRenderingIsEnabled );
98 
99  // SCALAR
100  int activeScalarDatasetGroupIndex = mMeshRendererActiveDatasetWidget->activeScalarDatasetGroup();
101  if ( !mContoursGroupBox->isChecked() )
102  activeScalarDatasetGroupIndex = -1;
103 
104  // VECTOR
105  int activeVectorDatasetGroupIndex = mMeshRendererActiveDatasetWidget->activeVectorDatasetGroup();
106  if ( !mVectorsGroupBox->isChecked() )
107  activeVectorDatasetGroupIndex = -1;
108 
109  QgsMeshRendererSettings settings = mMeshLayer->rendererSettings();
110  settings.setEdgeMeshSettings( edgeMeshSettings );
111  settings.setNativeMeshSettings( nativeMeshSettings );
112  settings.setTriangularMeshSettings( triangularMeshSettings );
113 
114  settings.setActiveScalarDatasetGroup( activeScalarDatasetGroupIndex );
115  if ( activeScalarDatasetGroupIndex > -1 )
116  settings.setScalarSettings( activeScalarDatasetGroupIndex, mMeshRendererScalarSettingsWidget->settings() );
117 
118  settings.setActiveVectorDatasetGroup( activeVectorDatasetGroupIndex );
119  if ( activeVectorDatasetGroupIndex > -1 )
120  settings.setVectorSettings( activeVectorDatasetGroupIndex, mMeshRendererVectorSettingsWidget->settings() );
121 
122  const QgsMeshDatasetIndex staticScalarDatasetIndex( activeScalarDatasetGroupIndex, mMeshLayer->staticScalarDatasetIndex().dataset() );
123  const QgsMeshDatasetIndex staticVectorDatasetIndex( activeVectorDatasetGroupIndex, mMeshLayer->staticVectorDatasetIndex().dataset() );
124  mMeshLayer->setStaticScalarDatasetIndex( staticScalarDatasetIndex );
125  mMeshLayer->setStaticVectorDatasetIndex( staticVectorDatasetIndex );
126 
127  //set the blend mode for the layer
128  mMeshLayer->setBlendMode( mBlendModeComboBox->blendMode() );
129  //set the averaging method for the layer
130  const std::unique_ptr<QgsMesh3dAveragingMethod> averagingMethod( m3dAveragingSettingsWidget->averagingMethod() );
131  settings.setAveragingMethod( averagingMethod.get() );
132  mMeshLayer->setRendererSettings( settings );
133  mMeshLayer->triggerRepaint();
134 
135  QgsSettings windowsSettings;
136  windowsSettings.setValue( QStringLiteral( "/Windows/RendererMeshProperties/tab" ), mStyleOptionsTab->currentIndex() );
137 }
138 
140 {
141  QgsMeshLayer *ml = qobject_cast<QgsMeshLayer *>( mapLayer );
142  if ( ml )
143  {
144  mLayer = ml;
145  mMeshRendererActiveDatasetWidget->setLayer( ml );
146  mNativeMeshSettingsWidget->setLayer( ml, QgsMeshRendererMeshSettingsWidget::Native );
147  mTriangularMeshSettingsWidget->setLayer( ml, QgsMeshRendererMeshSettingsWidget::Triangular );
148  mEdgeMeshSettingsWidget->setLayer( ml, QgsMeshRendererMeshSettingsWidget::Edge );
149  m3dAveragingSettingsWidget->setLayer( ml );
150  }
151  else
152  return;
153 
154  syncToLayerPrivate();
155 }
156 
157 void QgsRendererMeshPropertiesWidget::syncToLayerPrivate()
158 {
159  mMeshRendererActiveDatasetWidget->syncToLayer();
160  mNativeMeshSettingsWidget->syncToLayer();
161  mTriangularMeshSettingsWidget->syncToLayer();
162  mEdgeMeshSettingsWidget->syncToLayer();
163  m3dAveragingSettingsWidget->syncToLayer();
164 
165  mNativeMeshGroup->setChecked( mMeshLayer ? mMeshLayer->rendererSettings().nativeMeshSettings().isEnabled() : false );
166  mTriangularMeshGroup->setChecked( mMeshLayer ? mMeshLayer->rendererSettings().triangularMeshSettings().isEnabled() : false );
167  mEdgeMeshGroup->setChecked( mMeshLayer ? mMeshLayer->rendererSettings().edgeMeshSettings().isEnabled() : false );
168 
169  onActiveScalarGroupChanged( mMeshLayer->rendererSettings().activeScalarDatasetGroup() );
170  onActiveVectorGroupChanged( mMeshLayer->rendererSettings().activeVectorDatasetGroup() );
171 
172  const bool hasFaces = ( mMeshLayer->contains( QgsMesh::ElementType::Face ) );
173  mFaceMeshGroupBox->setVisible( hasFaces );
174 
175  const bool hasEdges = ( mMeshLayer->contains( QgsMesh::ElementType::Edge ) );
176  mEdgeMeshGroupBox->setVisible( hasEdges );
177 
178  QgsSettings settings;
179  if ( !settings.contains( QStringLiteral( "/Windows/RendererMeshProperties/tab" ) ) )
180  settings.setValue( QStringLiteral( "/Windows/RendererMeshProperties/tab" ), 0 );
181  else
182  mStyleOptionsTab->setCurrentIndex( settings.value( QStringLiteral( "/Windows/RendererMeshProperties/tab" ) ).toInt() );
183 }
184 
185 void QgsRendererMeshPropertiesWidget::onActiveScalarGroupChanged( int groupIndex )
186 {
187  mMeshRendererScalarSettingsWidget->setActiveDatasetGroup( groupIndex );
188  mMeshRendererScalarSettingsWidget->syncToLayer();
189  mContoursGroupBox->setChecked( groupIndex >= 0 );
190  mContoursGroupBox->setEnabled( groupIndex >= 0 );
191 }
192 
193 void QgsRendererMeshPropertiesWidget::onActiveVectorGroupChanged( int groupIndex )
194 {
195  if ( groupIndex >= 0 && !mMeshLayer->datasetGroupMetadata( groupIndex ).isVector() )
196  groupIndex = -1;
197  mMeshRendererVectorSettingsWidget->setActiveDatasetGroup( groupIndex );
198  mMeshRendererVectorSettingsWidget->syncToLayer();
199  mVectorsGroupBox->setChecked( groupIndex >= 0 );
200  mVectorsGroupBox->setEnabled( groupIndex >= 0 );
201 }
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
A panel widget that can be shown in the map style dock.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
void dataChanged()
Data of layer changed.
bool isVector() const
Returns whether dataset group has vector data.
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
int dataset() const
Returns a dataset index within group()
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:97
void setStaticVectorDatasetIndex(const QgsMeshDatasetIndex &staticVectorDatasetIndex)
Sets the static vector dataset index that is rendered if the temporal properties is not active.
void setStaticScalarDatasetIndex(const QgsMeshDatasetIndex &staticScalarDatasetIndex)
Sets the static scalar dataset index that is rendered if the temporal properties is not active.
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
QgsMeshDatasetIndex staticVectorDatasetIndex() const
Returns the static vector dataset index that is rendered if the temporal properties is not active.
QgsMeshDatasetIndex staticScalarDatasetIndex() const
Returns the static scalar dataset index that is rendered if the temporal properties is not active.
void setRendererSettings(const QgsMeshRendererSettings &settings)
Sets new renderer settings.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
void widgetChanged()
Mesh rendering settings changed.
void widgetChanged()
Emitted when any settings related to rendering changed.
void activeScalarGroupChanged(int groupIndex)
Emitted when the current scalar group gets changed.
void activeVectorGroupChanged(int groupIndex)
Emitted when the current vector group gets changed.
void widgetChanged()
Mesh rendering settings changed.
Represents a mesh renderer settings for mesh object.
void setEnabled(bool enabled)
Sets whether mesh structure rendering is enabled.
bool isEnabled() const
Returns whether mesh structure rendering is enabled.
void widgetChanged()
Mesh rendering settings changed.
Represents all mesh renderer settings.
void setActiveVectorDatasetGroup(int activeVectorDatasetGroup)
Sets the active vector dataset group.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
void setEdgeMeshSettings(const QgsMeshRendererMeshSettings &settings)
Sets new edge mesh renderer settings.
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
void setActiveScalarDatasetGroup(int activeScalarDatasetGroup)
Sets the active scalar dataset group.
void setVectorSettings(int groupIndex, const QgsMeshRendererVectorSettings &settings)
Sets new renderer settings.
void setTriangularMeshSettings(const QgsMeshRendererMeshSettings &settings)
Sets new triangular mesh renderer settings.
QgsMeshRendererMeshSettings edgeMeshSettings() const
Returns edge mesh renderer settings.
void setAveragingMethod(QgsMesh3dAveragingMethod *method)
Sets averaging method for conversion of 3d stacked mesh data to 2d data.
QgsMeshRendererMeshSettings nativeMeshSettings() const
Returns native mesh renderer settings.
void setScalarSettings(int groupIndex, const QgsMeshRendererScalarSettings &settings)
Sets new renderer settings.
QgsMeshRendererMeshSettings triangularMeshSettings() const
Returns triangular mesh renderer settings.
void setNativeMeshSettings(const QgsMeshRendererMeshSettings &settings)
Sets new native mesh renderer settings, triggers repaint.
void widgetChanged()
Mesh rendering settings changed.
void widgetChanged()
Emitted when the widget state changes.
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.
Definition: qgsproject.cpp:470
void apply() override
Applies the settings made in the dialog.
QgsRendererMeshPropertiesWidget(QgsMeshLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr)
A widget to hold the renderer properties for a mesh layer.
void syncToLayer(QgsMapLayer *mapLayer) override
Synchronize widgets state with associated map layer.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.