QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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#include "moc_qgsmeshstaticdatasetwidget.cpp"
18
19#include "qgsmeshlayer.h"
20
21QgsMeshStaticDatasetWidget::QgsMeshStaticDatasetWidget( QWidget *parent ): QWidget( parent )
22{
23 setupUi( this );
24
25 this->layout()->setContentsMargins( 0, 0, 0, 0 );
26 mDatasetScalarModel = new QgsMeshDatasetListModel( this );
27 mScalarDatasetComboBox->setModel( mDatasetScalarModel );
28 mDatasetVectorModel = new QgsMeshDatasetListModel( this );
29 mVectorDatasetComboBox->setModel( mDatasetVectorModel );
30}
31
33{
34 mLayer = layer;
35}
36
38{
39 if ( !mLayer )
40 return;
41
42 mDatasetScalarModel->setMeshLayer( mLayer );
43 mDatasetVectorModel->setMeshLayer( mLayer );
46}
47
49{
50 if ( !mLayer )
51 return;
52
53 int scalarIndex;
54 // if only one item, there is no active dataset group.
55 // Set to 0 instead of -1 to avoid none dataset (item 0) when the group is reactivate
56 if ( mScalarDatasetComboBox->count() == 1 )
57 scalarIndex = 0;
58 else
59 scalarIndex = mScalarDatasetComboBox->currentIndex() - 1;
60 int vectorIndex;
61 // Same as scalar
62 if ( mVectorDatasetComboBox->count() == 1 )
63 vectorIndex = 0;
64 else
65 vectorIndex = mVectorDatasetComboBox->currentIndex() - 1;
66
67 mLayer->setStaticScalarDatasetIndex( QgsMeshDatasetIndex( mScalarDatasetGroup, scalarIndex ) );
68 mLayer->setStaticVectorDatasetIndex( QgsMeshDatasetIndex( mVectorDatasetGroup, vectorIndex ) );
69}
70
72{
73 mScalarDatasetGroup = index;
74 mDatasetScalarModel->setDatasetGroup( index );
75 mScalarDatasetComboBox->setEnabled( mScalarDatasetGroup >= 0 );
76 if ( mLayer )
77 {
78 mScalarName->setText( mLayer->datasetGroupMetadata( index ).name() );
79 setScalarDatasetIndex( mLayer->staticScalarDatasetIndex().dataset() );
80 }
81}
82
84{
85 mVectorDatasetGroup = index;
86 mDatasetVectorModel->setDatasetGroup( index );
87 mVectorDatasetComboBox->setEnabled( mVectorDatasetGroup >= 0 );
88 if ( mLayer )
89 {
90 mVectorName->setText( mLayer->datasetGroupMetadata( index ).name() );
91 setVectorDatasetIndex( mLayer->staticVectorDatasetIndex().dataset() );
92 }
93}
94
95void QgsMeshStaticDatasetWidget::setScalarDatasetIndex( int index )
96{
97 if ( index < mLayer->datasetCount( mScalarDatasetGroup ) )
98 mScalarDatasetComboBox->setCurrentIndex( index + 1 );
99 else
100 mScalarDatasetComboBox->setCurrentIndex( 0 );
101}
102
103void QgsMeshStaticDatasetWidget::setVectorDatasetIndex( int index )
104{
105 if ( index < mLayer->datasetCount( mVectorDatasetGroup ) )
106 mVectorDatasetComboBox->setCurrentIndex( index + 1 );
107 else
108 mVectorDatasetComboBox->setCurrentIndex( 0 );
109}
110
111QgsMeshDatasetListModel::QgsMeshDatasetListModel( QObject *parent ): QAbstractListModel( parent )
112{}
113
115{
116 beginResetModel();
117 mLayer = layer;
118 endResetModel();
119}
120
122{
123 beginResetModel();
124 mDatasetGroup = group;
125 endResetModel();
126}
127
128int QgsMeshDatasetListModel::rowCount( const QModelIndex &parent ) const
129{
130 Q_UNUSED( parent )
131
132 if ( mLayer )
133 return mLayer->datasetCount( mDatasetGroup ) + 1;
134 else
135 return 0;
136}
137
138QVariant QgsMeshDatasetListModel::data( const QModelIndex &index, int role ) const
139{
140 if ( !index.isValid() )
141 return QVariant();
142
143 if ( role == Qt::DisplayRole )
144 {
145 if ( !mLayer || mDatasetGroup < 0 || index.row() == 0 )
146 return tr( "none" );
147
148 else if ( index.row() == 1 && mLayer->datasetCount( mDatasetGroup ) == 1 )
149 {
150 return tr( "Display dataset" );
151 }
152 else
153 {
154 const QgsInterval time = mLayer->datasetRelativeTime( QgsMeshDatasetIndex( mDatasetGroup, index.row() - 1 ) );
155 return mLayer->formatTime( time.hours() );
156 }
157 }
158
159 return QVariant();
160}
A representation of the interval between two datetime values.
Definition qgsinterval.h:46
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.
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(int group=-1) const
Returns the static vector 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.
QgsMeshDatasetIndex staticScalarDatasetIndex(int group=-1) const
Returns the static scalar dataset index that is rendered if the temporal properties is not active.
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.