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