QGIS API Documentation 4.1.0-Master (60fea48833c)
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] { this->mDatasetGroupTreeView->resetDefault( this->mMeshLayer ); } );
46
47 connect( mDatasetGroupTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, [this]() {
48 const QModelIndex index = mDatasetGroupTreeView->currentIndex();
49 const QgsMeshDatasetGroupTreeItem *meshGroupItem = mDatasetGroupTreeView->datasetGroupTreeRootItem()->childFromDatasetGroupIndex( index.row() );
50 if ( meshGroupItem )
51 {
52 if ( mMeshLayer->dataProvider()->dataSourceUri().contains( meshGroupItem->description() ) )
53 {
54 mRemoveDatasetButton->setEnabled( false );
55 }
56 else
57 {
58 mRemoveDatasetButton->setEnabled( true );
59 }
60 }
61 } );
62
63 connect( mDatasetGroupTreeView, &QgsMeshDatasetGroupTreeView::apply, this, &QgsMeshDatasetGroupTreeWidget::apply );
64}
65
67{
68 mMeshLayer = meshLayer;
69 mDatasetGroupTreeView->syncToLayer( meshLayer );
70}
71
73{
74 if ( mMeshLayer )
75 mMeshLayer->setDatasetGroupTreeRootItem( mDatasetGroupTreeView->datasetGroupTreeRootItem() );
76}
77
78void QgsMeshDatasetGroupTreeWidget::removeDataset()
79{
80 const QModelIndex index = mDatasetGroupTreeView->currentIndex();
81 const QgsMeshDatasetGroupTreeItem *meshGroupItem = mDatasetGroupTreeView->datasetGroupTreeRootItem()->child( index.row() );
82 const QString datasetGroupName = meshGroupItem->defaultName();
83 if ( mMeshLayer->removeDatasets( datasetGroupName ) )
84 {
85 QMessageBox::warning( this, tr( "Remove mesh datasets" ), tr( "Dataset Group removed from mesh." ) );
87 }
88 else
89 {
90 QMessageBox::warning( this, tr( "Remove mesh datasets" ), tr( "Could not remove mesh dataset group." ) );
91 }
92
93 mDatasetGroupTreeView->resetDefault( mMeshLayer );
94}
95
96void QgsMeshDatasetGroupTreeWidget::addDataset()
97{
98 if ( !mMeshLayer->dataProvider() )
99 return;
100
101 QgsSettings settings;
102 const QString openFileDir = settings.value( u"lastMeshDatasetDir"_s, QDir::homePath(), QgsSettings::App ).toString();
103 const QString openFileString = QFileDialog::getOpenFileName( nullptr, tr( "Load mesh datasets" ), openFileDir, QgsProviderRegistry::instance()->fileMeshDatasetFilters() );
104
105 if ( openFileString.isEmpty() )
106 {
107 return; // canceled by the user
108 }
109
110 if ( !mMeshLayer->datasetsPathUnique( openFileString ) )
111 {
112 QMessageBox::warning( this, tr( "Load mesh datasets" ), tr( "Could not add dataset from path that is already added to the mesh." ) );
113 return;
114 }
115
116 const QFileInfo openFileInfo( openFileString );
117 settings.setValue( u"lastMeshDatasetDir"_s, openFileInfo.absolutePath(), QgsSettings::App );
118 const QFile datasetFile( openFileString );
119
120 if ( mMeshLayer->addDatasets( openFileString, QgsProject::instance()->timeSettings()->temporalRange().begin() ) )
121 {
122 QMessageBox::information( this, tr( "Load mesh datasets" ), tr( "Datasets successfully added to the mesh layer" ) );
124 }
125 else
126 {
127 QMessageBox::warning( this, tr( "Load mesh datasets" ), tr( "Could not read mesh dataset." ) );
128 }
129}
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:408