QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsmeshdatasetgrouptreewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshdatasetgrouptreewidget.cpp
3  -------------------------------
4  begin : May 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 <QFileDialog>
19 #include <QMessageBox>
20 
22 #include "qgsmeshlayer.h"
24 #include "qgsproject.h"
25 #include "qgsprojecttimesettings.h"
26 #include "qgsproviderregistry.h"
27 #include "qgssettings.h"
28 
29 
31  QWidget( parent )
32 {
33  setupUi( this );
34 
35  connect( mAddDatasetButton, &QToolButton::clicked, this, &QgsMeshDatasetGroupTreeWidget::addDataset );
36  connect( mCollapseButton, &QToolButton::clicked, mDatasetGroupTreeView, &QTreeView::collapseAll );
37  connect( mExpandButton, &QToolButton::clicked, mDatasetGroupTreeView, &QTreeView::expandAll );
38  connect( mCheckAllButton, &QToolButton::clicked, mDatasetGroupTreeView, &QgsMeshDatasetGroupTreeView::selectAllGroups );
39  connect( mUnCheckAllButton, &QToolButton::clicked, mDatasetGroupTreeView, &QgsMeshDatasetGroupTreeView::deselectAllGroups );
40  connect( mResetDefaultButton, &QToolButton::clicked, this, [this]
41  {
42  this->mDatasetGroupTreeView->resetDefault( this->mMeshLayer );
43  } );
44 
45  connect( mDatasetGroupTreeView, &QgsMeshDatasetGroupTreeView::apply, this, &QgsMeshDatasetGroupTreeWidget::apply );
46 }
47 
49 {
50  mMeshLayer = meshLayer;
51  mDatasetGroupTreeView->syncToLayer( meshLayer );
52 }
53 
55 {
56  if ( mMeshLayer )
57  mMeshLayer->setDatasetGroupTreeRootItem( mDatasetGroupTreeView->datasetGroupTreeRootItem() );
58 }
59 
60 void QgsMeshDatasetGroupTreeWidget::addDataset()
61 {
62  if ( !mMeshLayer->dataProvider() )
63  return;
64 
65  QgsSettings settings;
66  const QString openFileDir = settings.value( QStringLiteral( "lastMeshDatasetDir" ), QDir::homePath(), QgsSettings::App ).toString();
67  const QString openFileString = QFileDialog::getOpenFileName( nullptr,
68  tr( "Load mesh datasets" ),
69  openFileDir,
70  QgsProviderRegistry::instance()->fileMeshDatasetFilters() );
71 
72  if ( openFileString.isEmpty() )
73  {
74  return; // canceled by the user
75  }
76 
77  const QFileInfo openFileInfo( openFileString );
78  settings.setValue( QStringLiteral( "lastMeshDatasetDir" ), openFileInfo.absolutePath(), QgsSettings::App );
79  const QFile datasetFile( openFileString );
80 
81  if ( mMeshLayer->addDatasets( openFileString, QgsProject::instance()->timeSettings()->temporalRange().begin() ) )
82  {
83  QMessageBox::information( this, tr( "Load mesh datasets" ), tr( "Datasets successfully added to the mesh layer" ) );
84  emit datasetGroupAdded();
85  }
86  else
87  {
88  QMessageBox::warning( this, tr( "Load mesh datasets" ), tr( "Could not read mesh dataset." ) );
89  }
90 }
void apply()
Apply the dataset group tree item to the layer.
QgsMeshDatasetGroupTreeWidget(QWidget *parent=nullptr)
Constructor.
void syncToLayer(QgsMeshLayer *meshLayer)
Synchronizes widgets state with associated mesh layer.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:97
bool addDatasets(const QString &path, const QDateTime &defaultReferenceTime=QDateTime())
Adds datasets to the mesh from file with path.
void setDatasetGroupTreeRootItem(QgsMeshDatasetGroupTreeItem *rootItem)
Sets the root items of the dataset group tree item.
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
QgsDateTimeRange temporalRange() const
Returns the project's temporal range, which indicates the earliest and latest datetime ranges associa...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:470
const QgsProjectTimeSettings * timeSettings() const
Returns the project's time settings, which contains the project's temporal range and other time based...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.