QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 );
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 }
A representation of the interval between two datetime values.
Definition: qgsinterval.h:42
double hours() const
Returns the interval duration in hours.
QString name() const
Returns name of the dataset group.
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
int dataset() const
Returns a dataset index within group()
List model for dataset contained in dataset group, used to display by time dataset in widget.
void setDatasetGroup(int group)
Sets the dataset group.
void setMeshLayer(QgsMeshLayer *layer)
Sets the layer.
int rowCount(const QModelIndex &parent) const override
QVariant data(const QModelIndex &index, int role) const override
QgsMeshDatasetListModel(QObject *parent)
Constructor.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:97
int datasetCount(const QgsMeshDatasetIndex &index) const
Returns the dataset count in the dataset groups.
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.
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.
QgsInterval datasetRelativeTime(const QgsMeshDatasetIndex &index)
Returns the relative time of the dataset from the reference time of its group.
QString formatTime(double hours)
Returns (date) time in hours formatted to human readable form.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
QgsMeshStaticDatasetWidget(QWidget *parent=nullptr)
Constructor.
void apply()
Applies the settings made in the widget.
void syncToLayer()
Synchronizes widgets state with associated mesh layer.
void setScalarDatasetGroup(int index)
Sets the scalar dataset group.
void setLayer(QgsMeshLayer *layer)
Sets the layer.
void setVectorDatasetGroup(int index)
Sets the vector dataset group.