QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmeshstaticdatasetwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshstaticdatasetwidget.cpp
3  -------------------------------------
4  begin : March 2020
5  copyright : (C) 2020 by Vincent Cloarec
6  email : vcloarec 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 "qgsmeshlayer.h"
19 
20 QgsMeshStaticDatasetWidget::QgsMeshStaticDatasetWidget( QWidget *parent ): QWidget( parent )
21 {
22  setupUi( this );
23 
24  this->layout()->setContentsMargins( 0, 0, 0, 0 );
25  mDatasetScalarModel = new QgsMeshDatasetListModel( this );
26  mScalarDatasetComboBox->setModel( mDatasetScalarModel );
27  mDatasetVectorModel = new QgsMeshDatasetListModel( this );
28  mVectorDatasetComboBox->setModel( mDatasetVectorModel );
29 }
30 
32 {
33  mLayer = layer;
34 }
35 
37 {
38  if ( !mLayer )
39  return;
40 
41  mDatasetScalarModel->setMeshLayer( mLayer );
42  mDatasetVectorModel->setMeshLayer( mLayer );
43  setScalarDatasetGroup( mLayer->rendererSettings().activeScalarDatasetGroup() );
44  setVectorDatasetGroup( mLayer->rendererSettings().activeVectorDatasetGroup() );
45 }
46 
48 {
49  if ( !mLayer )
50  return;
51 
52  int scalarIndex;
53  // if only one item, there is no active dataset group.
54  // Set to 0 instead of -1 to avoid none dataset (item 0) when the group is reactivate
55  if ( mScalarDatasetComboBox->count() == 1 )
56  scalarIndex = 0;
57  else
58  scalarIndex = mScalarDatasetComboBox->currentIndex() - 1;
59  int vectorIndex;
60  // Same as scalar
61  if ( mVectorDatasetComboBox->count() == 1 )
62  vectorIndex = 0;
63  else
64  vectorIndex = mVectorDatasetComboBox->currentIndex() - 1;
65 
66  mLayer->setStaticScalarDatasetIndex( QgsMeshDatasetIndex( mScalarDatasetGroup, scalarIndex ) );
67  mLayer->setStaticVectorDatasetIndex( QgsMeshDatasetIndex( mVectorDatasetGroup, vectorIndex ) );
68 }
69 
71 {
72  mScalarDatasetGroup = index;
73  mDatasetScalarModel->setDatasetGroup( index );
74  mScalarDatasetComboBox->setEnabled( mScalarDatasetGroup >= 0 );
75  if ( mLayer )
76  {
77  mScalarName->setText( mLayer->datasetGroupMetadata( index ).name() );
78  setScalarDatasetIndex( mLayer->staticScalarDatasetIndex().dataset() );
79  }
80 }
81 
83 {
84  mVectorDatasetGroup = index;
85  mDatasetVectorModel->setDatasetGroup( index );
86  mVectorDatasetComboBox->setEnabled( mVectorDatasetGroup >= 0 );
87  if ( mLayer )
88  {
89  mVectorName->setText( mLayer->datasetGroupMetadata( index ).name() );
90  setVectorDatasetIndex( mLayer->staticVectorDatasetIndex().dataset() );
91  }
92 }
93 
94 void QgsMeshStaticDatasetWidget::setScalarDatasetIndex( int index )
95 {
96  if ( index < mLayer->datasetCount( mScalarDatasetGroup ) )
97  mScalarDatasetComboBox->setCurrentIndex( index + 1 );
98  else
99  mScalarDatasetComboBox->setCurrentIndex( 0 );
100 }
101 
102 void QgsMeshStaticDatasetWidget::setVectorDatasetIndex( int index )
103 {
104  if ( index < mLayer->datasetCount( mVectorDatasetGroup ) )
105  mVectorDatasetComboBox->setCurrentIndex( index + 1 );
106  else
107  mVectorDatasetComboBox->setCurrentIndex( 0 );
108 }
109 
110 QgsMeshDatasetListModel::QgsMeshDatasetListModel( QObject *parent ): QAbstractListModel( parent )
111 {}
112 
114 {
115  beginResetModel();
116  mLayer = layer;
117  endResetModel();
118 }
119 
121 {
122  beginResetModel();
123  mDatasetGroup = group;
124  endResetModel();
125 }
126 
127 int QgsMeshDatasetListModel::rowCount( const QModelIndex &parent ) const
128 {
129  Q_UNUSED( parent )
130 
131  if ( mLayer )
132  return mLayer->datasetCount( mDatasetGroup ) + 1;
133  else
134  return 0;
135 }
136 
137 QVariant QgsMeshDatasetListModel::data( const QModelIndex &index, int role ) const
138 {
139  if ( !index.isValid() )
140  return QVariant();
141 
142  if ( role == Qt::DisplayRole )
143  {
144  if ( !mLayer || mDatasetGroup < 0 || index.row() == 0 )
145  return tr( "none" );
146 
147  else if ( index.row() == 1 && mLayer->datasetCount( mDatasetGroup ) == 1 )
148  {
149  return tr( "Display dataset" );
150  }
151  else
152  {
153  const QgsInterval time = mLayer->datasetRelativeTime( QgsMeshDatasetIndex( mDatasetGroup, index.row() - 1 ) );
154  return mLayer->formatTime( time.hours() );
155  }
156  }
157 
158  return QVariant();
159 }
QgsMeshDatasetListModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: qgsmeshstaticdatasetwidget.cpp:137
QgsMeshStaticDatasetWidget::syncToLayer
void syncToLayer()
Synchronizes widgets state with associated mesh layer.
Definition: qgsmeshstaticdatasetwidget.cpp:36
QgsMeshStaticDatasetWidget::setLayer
void setLayer(QgsMeshLayer *layer)
Sets the layer.
Definition: qgsmeshstaticdatasetwidget.cpp:31
QgsMeshDatasetListModel::rowCount
int rowCount(const QModelIndex &parent) const override
Definition: qgsmeshstaticdatasetwidget.cpp:127
QgsMeshDatasetListModel::setMeshLayer
void setMeshLayer(QgsMeshLayer *layer)
Sets the layer.
Definition: qgsmeshstaticdatasetwidget.cpp:113
QgsMeshLayer::formatTime
QString formatTime(double hours)
Returns (date) time in hours formatted to human readable form.
Definition: qgsmeshlayer.cpp:376
QgsMeshLayer::datasetRelativeTime
QgsInterval datasetRelativeTime(const QgsMeshDatasetIndex &index)
Returns the relative time of the dataset from the reference time of its group.
Definition: qgsmeshlayer.cpp:918
QgsMeshDatasetIndex
QgsMeshDatasetIndex is index that identifies the dataset group (e.g. wind speed) and a dataset in thi...
Definition: qgsmeshdataset.h:48
QgsMeshLayer
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:98
QgsMeshStaticDatasetWidget::setScalarDatasetGroup
void setScalarDatasetGroup(int index)
Sets the scalar dataset group.
Definition: qgsmeshstaticdatasetwidget.cpp:70
QgsMeshLayer::datasetCount
int datasetCount(const QgsMeshDatasetIndex &index) const
Returns the dataset count in the dataset groups.
Definition: qgsmeshlayer.cpp:409
QgsMeshDatasetListModel
List model for dataset contained in dataset group, used to display by time dataset in widget.
Definition: qgsmeshstaticdatasetwidget.h:36
QgsMeshStaticDatasetWidget::apply
void apply()
Applies the settings made in the widget.
Definition: qgsmeshstaticdatasetwidget.cpp:47
qgsmeshlayer.h
qgsmeshstaticdatasetwidget.h
QgsInterval
A representation of the interval between two datetime values.
Definition: qgsinterval.h:41
QgsInterval::hours
double hours() const
Returns the interval duration in hours.
Definition: qgsinterval.cpp:170
QgsMeshDatasetListModel::QgsMeshDatasetListModel
QgsMeshDatasetListModel(QObject *parent)
Constructor.
Definition: qgsmeshstaticdatasetwidget.cpp:110
QgsMeshStaticDatasetWidget::QgsMeshStaticDatasetWidget
QgsMeshStaticDatasetWidget(QWidget *parent=nullptr)
Constructor.
Definition: qgsmeshstaticdatasetwidget.cpp:20
QgsMeshDatasetListModel::setDatasetGroup
void setDatasetGroup(int group)
Sets the dataset group.
Definition: qgsmeshstaticdatasetwidget.cpp:120
QgsMeshStaticDatasetWidget::setVectorDatasetGroup
void setVectorDatasetGroup(int index)
Sets the vector dataset group.
Definition: qgsmeshstaticdatasetwidget.cpp:82