QGIS API Documentation  3.27.0-Master (597e8eebd4)
qgsmeshrendereractivedatasetwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshrendereractivedatasetwidget.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 <QDateTime>
19 #include <QIcon>
20 
21 #include "qgis.h"
22 #include "qgsapplication.h"
23 #include "qgsmeshlayer.h"
24 #include "qgsmessagelog.h"
26 
28  : QWidget( parent )
29 {
30  setupUi( this );
31 
32  connect( mDatasetGroupTreeView, &QgsMeshActiveDatasetGroupTreeView::activeScalarGroupChanged,
33  this, &QgsMeshRendererActiveDatasetWidget::onActiveScalarGroupChanged );
34  connect( mDatasetGroupTreeView, &QgsMeshActiveDatasetGroupTreeView::activeVectorGroupChanged,
35  this, &QgsMeshRendererActiveDatasetWidget::onActiveVectorGroupChanged );
36 }
37 
39 
40 
42 {
43  if ( mMeshLayer )
44  {
45  disconnect( mMeshLayer, &QgsMeshLayer::activeScalarDatasetGroupChanged,
47  disconnect( mMeshLayer, &QgsMeshLayer::activeVectorDatasetGroupChanged,
49  }
50 
51  mMeshLayer = layer;
52 
53  mDatasetGroupTreeView->setLayer( layer );
58 }
59 
61 {
62  return mActiveScalarDatasetGroup;
63 }
64 
66 {
67  return mActiveVectorDatasetGroup;
68 }
69 
70 
71 void QgsMeshRendererActiveDatasetWidget::onActiveScalarGroupChanged( int groupIndex )
72 {
73  if ( groupIndex == mActiveScalarDatasetGroup )
74  return;
75 
76  mActiveScalarDatasetGroup = groupIndex;
77  updateMetadata();
78  emit activeScalarGroupChanged( mActiveScalarDatasetGroup );
79  emit widgetChanged();
80 }
81 
82 void QgsMeshRendererActiveDatasetWidget::onActiveVectorGroupChanged( int groupIndex )
83 {
84  if ( groupIndex == mActiveVectorDatasetGroup )
85  return;
86 
87  mActiveVectorDatasetGroup = groupIndex;
88  updateMetadata();
89  emit activeVectorGroupChanged( mActiveVectorDatasetGroup );
90  emit widgetChanged();
91 }
92 
93 void QgsMeshRendererActiveDatasetWidget::updateMetadata()
94 {
95  QString msg;
96 
97  if ( !mMeshLayer ||
98  !mMeshLayer->dataProvider() )
99  {
100  msg += tr( "Invalid mesh layer selected" );
101  }
102  else
103  {
104  if ( mActiveScalarDatasetGroup > -1 )
105  {
106  if ( mActiveVectorDatasetGroup > -1 )
107  {
108  if ( mActiveScalarDatasetGroup == mActiveVectorDatasetGroup )
109  {
110  msg += metadata( mActiveScalarDatasetGroup );
111  }
112  else
113  {
114  msg += QStringLiteral( "<p> <h3> %1 </h3> " ).arg( tr( "Scalar dataset" ) );
115  msg += metadata( mActiveScalarDatasetGroup );
116  msg += QStringLiteral( "</p> <p> <h3> %1 </h3>" ).arg( tr( "Vector dataset" ) );
117  msg += metadata( mActiveVectorDatasetGroup );
118  msg += QLatin1String( "</p>" );
119  }
120  }
121  else
122  {
123  msg += metadata( mActiveScalarDatasetGroup );
124  }
125  }
126  else
127  {
128  if ( mActiveVectorDatasetGroup > -1 )
129  {
130  msg += metadata( mActiveVectorDatasetGroup );
131  }
132  else
133  {
134  msg += tr( "No mesh dataset selected" );
135  }
136  }
137  }
138 
139  mActiveDatasetMetadata->setText( msg );
140 }
141 
142 
143 QString QgsMeshRendererActiveDatasetWidget::metadata( QgsMeshDatasetIndex datasetIndex )
144 {
145 
146  QString msg;
147  msg += QLatin1String( "<table>" );
148 
149  QString definedOnMesh;
150  if ( mMeshLayer->contains( QgsMesh::ElementType::Face ) )
151  {
152  if ( mMeshLayer->contains( QgsMesh::ElementType::Edge ) )
153  {
154  definedOnMesh = tr( "faces and edges" );
155  }
156  else
157  {
158  definedOnMesh = tr( "faces" );
159  }
160  }
161  else if ( mMeshLayer->contains( QgsMesh::ElementType::Edge ) )
162  {
163  definedOnMesh = tr( "edges" );
164  }
165  else
166  {
167  definedOnMesh = tr( "invalid mesh" );
168  }
169  msg += QStringLiteral( "<tr><td>%1</td><td>%2</td></tr>" )
170  .arg( tr( "Mesh type" ) )
171  .arg( definedOnMesh );
172 
173  const QgsMeshDatasetGroupMetadata gmeta = mMeshLayer->datasetGroupMetadata( datasetIndex );
174  QString definedOn;
175  switch ( gmeta.dataType() )
176  {
178  definedOn = tr( "vertices" );
179  break;
181  definedOn = tr( "faces" );
182  break;
184  definedOn = tr( "volumes" );
185  break;
187  definedOn = tr( "edges" );
188  break;
189  }
190  msg += QStringLiteral( "<tr><td>%1</td><td>%2</td></tr>" )
191  .arg( tr( "Data type" ) )
192  .arg( definedOn );
193 
194  msg += QStringLiteral( "<tr><td>%1</td><td>%2</td></tr>" )
195  .arg( tr( "Is vector" ) )
196  .arg( gmeta.isVector() ? tr( "Yes" ) : tr( "No" ) );
197 
198  const auto options = gmeta.extraOptions();
199  for ( auto it = options.constBegin(); it != options.constEnd(); ++it )
200  {
201  if ( it.key() == QLatin1String( "classification" ) )
202  {
203  msg += QStringLiteral( "<tr><td>%1</td></tr>" ).arg( tr( "Classified values" ) );
204  continue;
205  }
206  msg += QStringLiteral( "<tr><td>%1</td><td>%2</td></tr>" ).arg( it.key() ).arg( it.value() );
207  }
208 
209  msg += QLatin1String( "</table>" );
210 
211  return msg;
212 }
213 
215 {
216  setEnabled( mMeshLayer );
217 
218  whileBlocking( mDatasetGroupTreeView )->syncToLayer();
219 
220  mActiveScalarDatasetGroup = mDatasetGroupTreeView->activeScalarGroup();
221  mActiveVectorDatasetGroup = mDatasetGroupTreeView->activeVectorGroup();
222 
223  updateMetadata();
224 }
void setActiveScalarGroup(int group)
Sets active group for contours.
void setActiveVectorGroup(int group)
Sets active vector group.
void activeVectorGroupChanged(int groupIndex)
Selected dataset group for vectors changed. -1 for invalid group.
void activeScalarGroupChanged(int groupIndex)
Selected dataset group for contours changed. -1 for invalid group.
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
QMap< QString, QString > extraOptions() const
Returns extra metadata options, for example description.
bool isVector() const
Returns whether dataset group has vector data.
DataType dataType() const
Returns whether dataset group data is defined on vertices or faces or volumes.
@ DataOnEdges
Data is defined on edges.
@ DataOnFaces
Data is defined on faces.
@ DataOnVertices
Data is defined on vertices.
@ DataOnVolumes
Data is defined on volumes.
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:99
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
void activeScalarDatasetGroupChanged(int index)
Emitted when active scalar group dataset is changed.
void activeVectorDatasetGroupChanged(int index)
Emitted when active vector group dataset is changed.
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
QgsMeshRendererActiveDatasetWidget(QWidget *parent=nullptr)
A widget to hold the renderer scalar settings for a mesh layer.
int activeVectorDatasetGroup() const
Returns index of the active vector dataset group.
void syncToLayer()
Synchronizes widgets state with associated mesh layer.
void widgetChanged()
Emitted when any settings related to rendering changed.
int activeScalarDatasetGroup() const
Returns index of the active scalar dataset group.
void setLayer(QgsMeshLayer *layer)
Associates mesh layer with the widget.
void activeScalarGroupChanged(int groupIndex)
Emitted when the current scalar group gets changed.
void activeVectorGroupChanged(int groupIndex)
Emitted when the current vector group gets changed.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2186