QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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#include "moc_qgsmeshdatasetgrouptreewidget.cpp"
18
19#include <QFileDialog>
20#include <QMessageBox>
21
23#include "qgsmeshlayer.h"
25#include "qgsproject.h"
27#include "qgsproviderregistry.h"
28#include "qgssettings.h"
29
30
32 : QWidget( parent )
33{
34 setupUi( this );
35
36 connect( mAddDatasetButton, &QToolButton::clicked, this, &QgsMeshDatasetGroupTreeWidget::addDataset );
37 connect( mRemoveDatasetButton, &QToolButton::clicked, this, &QgsMeshDatasetGroupTreeWidget::removeDataset );
38 connect( mCollapseButton, &QToolButton::clicked, mDatasetGroupTreeView, &QTreeView::collapseAll );
39 connect( mExpandButton, &QToolButton::clicked, mDatasetGroupTreeView, &QTreeView::expandAll );
40 connect( mCheckAllButton, &QToolButton::clicked, mDatasetGroupTreeView, &QgsMeshDatasetGroupTreeView::selectAllGroups );
41 connect( mUnCheckAllButton, &QToolButton::clicked, mDatasetGroupTreeView, &QgsMeshDatasetGroupTreeView::deselectAllGroups );
42 connect( mResetDefaultButton, &QToolButton::clicked, this, [this] {
43 this->mDatasetGroupTreeView->resetDefault( this->mMeshLayer );
44 } );
45
46 connect( mDatasetGroupTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, [this]() {
47 const QModelIndex index = mDatasetGroupTreeView->currentIndex();
48 const QgsMeshDatasetGroupTreeItem *meshGroupItem = mDatasetGroupTreeView->datasetGroupTreeRootItem()->childFromDatasetGroupIndex( index.row() );
49 if ( meshGroupItem )
50 {
51 if ( mMeshLayer->dataProvider()->dataSourceUri().contains( meshGroupItem->description() ) )
52 {
53 mRemoveDatasetButton->setEnabled( false );
54 }
55 else
56 {
57 mRemoveDatasetButton->setEnabled( true );
58 }
59 }
60 } );
61
62 connect( mDatasetGroupTreeView, &QgsMeshDatasetGroupTreeView::apply, this, &QgsMeshDatasetGroupTreeWidget::apply );
63}
64
66{
67 mMeshLayer = meshLayer;
68 mDatasetGroupTreeView->syncToLayer( meshLayer );
69}
70
72{
73 if ( mMeshLayer )
74 mMeshLayer->setDatasetGroupTreeRootItem( mDatasetGroupTreeView->datasetGroupTreeRootItem() );
75}
76
77void QgsMeshDatasetGroupTreeWidget::removeDataset()
78{
79 const QModelIndex index = mDatasetGroupTreeView->currentIndex();
80 const QgsMeshDatasetGroupTreeItem *meshGroupItem = mDatasetGroupTreeView->datasetGroupTreeRootItem()->child( index.row() );
81 const QString datasetGroupName = meshGroupItem->defaultName();
82 if ( mMeshLayer->removeDatasets( datasetGroupName ) )
83 {
84 QMessageBox::warning( this, tr( "Remove mesh datasets" ), tr( "Dataset Group removed from mesh." ) );
86 }
87 else
88 {
89 QMessageBox::warning( this, tr( "Remove mesh datasets" ), tr( "Could not remove mesh dataset group." ) );
90 }
91
92 mDatasetGroupTreeView->resetDefault( mMeshLayer );
93}
94
95void QgsMeshDatasetGroupTreeWidget::addDataset()
96{
97 if ( !mMeshLayer->dataProvider() )
98 return;
99
100 QgsSettings settings;
101 const QString openFileDir = settings.value( QStringLiteral( "lastMeshDatasetDir" ), QDir::homePath(), QgsSettings::App ).toString();
102 const QString openFileString = QFileDialog::getOpenFileName( nullptr, tr( "Load mesh datasets" ), openFileDir, QgsProviderRegistry::instance()->fileMeshDatasetFilters() );
103
104 if ( openFileString.isEmpty() )
105 {
106 return; // canceled by the user
107 }
108
109 if ( !mMeshLayer->datasetsPathUnique( openFileString ) )
110 {
111 QMessageBox::warning( this, tr( "Load mesh datasets" ), tr( "Could not add dataset from path that is already added to the mesh." ) );
112 return;
113 }
114
115 const QFileInfo openFileInfo( openFileString );
116 settings.setValue( QStringLiteral( "lastMeshDatasetDir" ), openFileInfo.absolutePath(), QgsSettings::App );
117 const QFile datasetFile( openFileString );
118
119 if ( mMeshLayer->addDatasets( openFileString, QgsProject::instance()->timeSettings()->temporalRange().begin() ) )
120 {
121 QMessageBox::information( this, tr( "Load mesh datasets" ), tr( "Datasets successfully added to the mesh layer" ) );
123 }
124 else
125 {
126 QMessageBox::warning( this, tr( "Load mesh datasets" ), tr( "Could not read mesh dataset." ) );
127 }
128}
Tree item for display of the mesh dataset groups.
QgsMeshDatasetGroupTreeItem * child(int row) const
Returns a child.
void datasetGroupsChanged()
Emitted when dataset groups changed (addition or removal)
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.
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.
bool datasetsPathUnique(const QString &path)
Checks whether that datasets path is already added to this mesh layer.
bool removeDatasets(const QString &name)
Removes datasets from the mesh with given name.
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.
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:64
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.
T begin() const
Returns the beginning of the range.
Definition qgsrange.h:444