QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
28  : QgsMapLayerConfigWidget( layer, canvas, parent )
29  , mMeshLayer( layer )
30 {
31  if ( !mMeshLayer )
32  return;
33 
34  setupUi( this );
35 
36  connect( mMeshLayer,
38  this,
39  &QgsRendererMeshPropertiesWidget::syncToLayerPrivate );
40 
41  mMeshRendererActiveDatasetWidget->setLayer( mMeshLayer );
42  mMeshRendererScalarSettingsWidget->setLayer( mMeshLayer );
43  mNativeMeshSettingsWidget->setLayer( mMeshLayer, QgsMeshRendererMeshSettingsWidget::MeshType::Native );
44  mTriangularMeshSettingsWidget->setLayer( mMeshLayer, QgsMeshRendererMeshSettingsWidget::MeshType::Triangular );
45  mEdgeMeshSettingsWidget->setLayer( mMeshLayer, QgsMeshRendererMeshSettingsWidget::MeshType::Edge );
46  mMeshRendererVectorSettingsWidget->setLayer( mMeshLayer );
47  m3dAveragingSettingsWidget->setLayer( mMeshLayer );
48  syncToLayer( mMeshLayer );
49 
50  //blend mode
51  mBlendModeComboBox->setBlendMode( mMeshLayer->blendMode() );
52  connect( mBlendModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
53 
54  connect( mMeshRendererActiveDatasetWidget, &QgsMeshRendererActiveDatasetWidget::activeScalarGroupChanged,
55  this, &QgsRendererMeshPropertiesWidget::onActiveScalarGroupChanged );
56  connect( mMeshRendererActiveDatasetWidget, &QgsMeshRendererActiveDatasetWidget::activeVectorGroupChanged,
57  this, &QgsRendererMeshPropertiesWidget::onActiveVectorGroupChanged );
58 
59  connect( mNativeMeshGroup, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
60  connect( mEdgeMeshGroup, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
61  connect( mTriangularMeshGroup, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
62  connect( mContoursGroupBox, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
63  connect( mVectorsGroupBox, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
64  connect( mMeshRendererActiveDatasetWidget, &QgsMeshRendererActiveDatasetWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
65  connect( mMeshRendererScalarSettingsWidget, &QgsMeshRendererScalarSettingsWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
66  connect( mMeshRendererVectorSettingsWidget, &QgsMeshRendererVectorSettingsWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
67  connect( mNativeMeshSettingsWidget, &QgsMeshRendererMeshSettingsWidget::widgetChanged,
69  connect( mTriangularMeshSettingsWidget, &QgsMeshRendererMeshSettingsWidget::widgetChanged,
71  connect( mEdgeMeshSettingsWidget, &QgsMeshRendererMeshSettingsWidget::widgetChanged,
73  connect( m3dAveragingSettingsWidget, &QgsMeshRenderer3dAveragingWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
74 }
75 
77 {
78  if ( !mMeshLayer )
79  return;
80 
81  // 1D EDGE MESH
82  const bool edgeMeshRenderingIsEnabled = mEdgeMeshGroup->isChecked();
83  QgsMeshRendererMeshSettings edgeMeshSettings = mEdgeMeshSettingsWidget->settings();
84  edgeMeshSettings.setEnabled( edgeMeshRenderingIsEnabled );
85 
86  // 2D NATIVE MESH
87  const bool nativeMeshRenderingIsEnabled = mNativeMeshGroup->isChecked();
88  QgsMeshRendererMeshSettings nativeMeshSettings = mNativeMeshSettingsWidget->settings();
89  nativeMeshSettings.setEnabled( nativeMeshRenderingIsEnabled );
90 
91  // 2D TRIANGULAR MESH
92  const bool triangularMeshRenderingIsEnabled = mTriangularMeshGroup->isChecked();
93  QgsMeshRendererMeshSettings triangularMeshSettings = mTriangularMeshSettingsWidget->settings();
94  triangularMeshSettings.setEnabled( triangularMeshRenderingIsEnabled );
95 
96  // SCALAR
97  int activeScalarDatasetGroupIndex = mMeshRendererActiveDatasetWidget->activeScalarDatasetGroup();
98  if ( !mContoursGroupBox->isChecked() )
99  activeScalarDatasetGroupIndex = -1;
100 
101  // VECTOR
102  int activeVectorDatasetGroupIndex = mMeshRendererActiveDatasetWidget->activeVectorDatasetGroup();
103  if ( !mVectorsGroupBox->isChecked() )
104  activeVectorDatasetGroupIndex = -1;
105 
106  QgsMeshRendererSettings settings = mMeshLayer->rendererSettings();
107  settings.setEdgeMeshSettings( edgeMeshSettings );
108  settings.setNativeMeshSettings( nativeMeshSettings );
109  settings.setTriangularMeshSettings( triangularMeshSettings );
110 
111  settings.setActiveScalarDatasetGroup( activeScalarDatasetGroupIndex );
112  if ( activeScalarDatasetGroupIndex > -1 )
113  settings.setScalarSettings( activeScalarDatasetGroupIndex, mMeshRendererScalarSettingsWidget->settings() );
114 
115  settings.setActiveVectorDatasetGroup( activeVectorDatasetGroupIndex );
116  if ( activeVectorDatasetGroupIndex > -1 )
117  settings.setVectorSettings( activeVectorDatasetGroupIndex, mMeshRendererVectorSettingsWidget->settings() );
118 
119  const QgsMeshDatasetIndex staticScalarDatasetIndex( activeScalarDatasetGroupIndex, mMeshLayer->staticScalarDatasetIndex().dataset() );
120  const QgsMeshDatasetIndex staticVectorDatasetIndex( activeVectorDatasetGroupIndex, mMeshLayer->staticVectorDatasetIndex().dataset() );
121  mMeshLayer->setStaticScalarDatasetIndex( staticScalarDatasetIndex );
122  mMeshLayer->setStaticVectorDatasetIndex( staticVectorDatasetIndex );
123 
124  //set the blend mode for the layer
125  mMeshLayer->setBlendMode( mBlendModeComboBox->blendMode() );
126  //set the averaging method for the layer
127  const std::unique_ptr<QgsMesh3dAveragingMethod> averagingMethod( m3dAveragingSettingsWidget->averagingMethod() );
128  settings.setAveragingMethod( averagingMethod.get() );
129  mMeshLayer->setRendererSettings( settings );
130  mMeshLayer->triggerRepaint();
131 
132  QgsSettings windowsSettings;
133  windowsSettings.setValue( QStringLiteral( "/Windows/RendererMeshProperties/tab" ), mStyleOptionsTab->currentIndex() );
134 }
135 
137 {
138  QgsMeshLayer *ml = qobject_cast<QgsMeshLayer *>( mapLayer );
139  if ( ml )
140  {
141  mLayer = ml;
142  mMeshRendererActiveDatasetWidget->setLayer( ml );
143  mNativeMeshSettingsWidget->setLayer( ml, QgsMeshRendererMeshSettingsWidget::Native );
144  mTriangularMeshSettingsWidget->setLayer( ml, QgsMeshRendererMeshSettingsWidget::Triangular );
145  mEdgeMeshSettingsWidget->setLayer( ml, QgsMeshRendererMeshSettingsWidget::Edge );
146  m3dAveragingSettingsWidget->setLayer( ml );
147  }
148  else
149  return;
150 
151  syncToLayerPrivate();
152 }
153 
154 void QgsRendererMeshPropertiesWidget::syncToLayerPrivate()
155 {
156  mMeshRendererActiveDatasetWidget->syncToLayer();
157  mNativeMeshSettingsWidget->syncToLayer();
158  mTriangularMeshSettingsWidget->syncToLayer();
159  mEdgeMeshSettingsWidget->syncToLayer();
160  m3dAveragingSettingsWidget->syncToLayer();
161 
162  mNativeMeshGroup->setChecked( mMeshLayer ? mMeshLayer->rendererSettings().nativeMeshSettings().isEnabled() : false );
163  mTriangularMeshGroup->setChecked( mMeshLayer ? mMeshLayer->rendererSettings().triangularMeshSettings().isEnabled() : false );
164  mEdgeMeshGroup->setChecked( mMeshLayer ? mMeshLayer->rendererSettings().edgeMeshSettings().isEnabled() : false );
165 
166  onActiveScalarGroupChanged( mMeshLayer->rendererSettings().activeScalarDatasetGroup() );
167  onActiveVectorGroupChanged( mMeshLayer->rendererSettings().activeVectorDatasetGroup() );
168 
169  const bool hasFaces = ( mMeshLayer->contains( QgsMesh::ElementType::Face ) );
170  mFaceMeshGroupBox->setVisible( hasFaces );
171 
172  const bool hasEdges = ( mMeshLayer->contains( QgsMesh::ElementType::Edge ) );
173  mEdgeMeshGroupBox->setVisible( hasEdges );
174 
175  QgsSettings settings;
176  if ( !settings.contains( QStringLiteral( "/Windows/RendererMeshProperties/tab" ) ) )
177  settings.setValue( QStringLiteral( "/Windows/RendererMeshProperties/tab" ), 0 );
178  else
179  mStyleOptionsTab->setCurrentIndex( settings.value( QStringLiteral( "/Windows/RendererMeshProperties/tab" ) ).toInt() );
180 }
181 
182 void QgsRendererMeshPropertiesWidget::onActiveScalarGroupChanged( int groupIndex )
183 {
184  mMeshRendererScalarSettingsWidget->setActiveDatasetGroup( groupIndex );
185  mMeshRendererScalarSettingsWidget->syncToLayer();
186  mContoursGroupBox->setChecked( groupIndex >= 0 );
187  mContoursGroupBox->setEnabled( groupIndex >= 0 );
188 }
189 
190 void QgsRendererMeshPropertiesWidget::onActiveVectorGroupChanged( int groupIndex )
191 {
192  if ( groupIndex >= 0 && !mMeshLayer->datasetGroupMetadata( groupIndex ).isVector() )
193  groupIndex = -1;
194  mMeshRendererVectorSettingsWidget->setActiveDatasetGroup( groupIndex );
195  mMeshRendererVectorSettingsWidget->syncToLayer();
196  mVectorsGroupBox->setChecked( groupIndex >= 0 );
197  mVectorsGroupBox->setEnabled( groupIndex >= 0 );
198 }
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
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.
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.