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