Skip to content

Commit

Permalink
Mesh layer remove datasetgroup (#59382)
Browse files Browse the repository at this point in the history
* add removeDatasetGroup() function for mesh data provider and mesh layer

* make dataset group names unique

* fix typo

* add mesh removing to the widget

* fix icon

* add check that added dataset group path is unique to avoid duplicate data and emit message if it is not

* sipify

* fix mistake

* check if uri is correct - just for case

* add tests

* add tests

* fix docstring

* add docstring

* add missing override

* explicitly convert number to string

* update tests to reflect that you cannot add same file as dataset group twice

* fix docstring

* pass by value

* fix docstrings

* const variables

* rename signal

* drop include

* simplify regex

* make test better

* sipify

* add docstring

* fix styling and typo

* fix tests

* fix precommit issues

* fix issue

* Update src/core/mesh/qgsmeshlayer.h

fix docstring

Co-authored-by: Stefanos Natsis <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* const stuff

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* sipify

---------

Co-authored-by: Stefanos Natsis <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent f593da5 commit 254614b
Show file tree
Hide file tree
Showing 19 changed files with 521 additions and 18 deletions.
11 changes: 11 additions & 0 deletions python/PyQt6/core/auto_generated/mesh/qgsmeshdataprovider.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@ Returns the mesh driver metadata of the provider
Closes the data provider and free every resources used

.. versionadded:: 3.22
%End

virtual bool removeDatasetGroup( int index ) = 0;
%Docstring
Remove dataset group from the mesh

emits dataChanged when successful

:return: ``True`` on success

.. versionadded:: 3.42
%End

signals:
Expand Down
24 changes: 23 additions & 1 deletion python/PyQt6/core/auto_generated/mesh/qgsmeshlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,23 @@ Returns the provider type for this layer
%Docstring
Adds datasets to the mesh from file with ``path``. Use the the time ``defaultReferenceTime`` as reference time is not provided in the file

:param path: the path to the atasets file
:param path: the path to the datasets file
:param defaultReferenceTime: reference time used if not provided in the file

:return: whether the dataset is added

.. versionadded:: 3.14
%End

bool removeDatasets( const QString &name );
%Docstring
Removes datasets from the mesh with given ``name``.

:param name: name of dataset group to remove

:return: whether the dataset is removed

.. versionadded:: 3.42
%End

bool addDatasets( QgsMeshDatasetGroup *datasetGroup /Transfer/ );
Expand Down Expand Up @@ -905,6 +916,17 @@ Sets labeling configuration. Takes ownership of the object.
.. versionadded:: 3.36
%End

bool datasetsPathUnique( const QString &path );
%Docstring
Checks whether that datasets path is already added to this mesh layer. Return ``True`` if the
dataset path is not already added.

:param path: the path to the datasets file

:return: whether the datasets path is unique

.. versionadded:: 3.42
%End

public slots:

Expand Down
11 changes: 11 additions & 0 deletions python/core/auto_generated/mesh/qgsmeshdataprovider.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@ Returns the mesh driver metadata of the provider
Closes the data provider and free every resources used

.. versionadded:: 3.22
%End

virtual bool removeDatasetGroup( int index ) = 0;
%Docstring
Remove dataset group from the mesh

emits dataChanged when successful

:return: ``True`` on success

.. versionadded:: 3.42
%End

signals:
Expand Down
24 changes: 23 additions & 1 deletion python/core/auto_generated/mesh/qgsmeshlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,23 @@ Returns the provider type for this layer
%Docstring
Adds datasets to the mesh from file with ``path``. Use the the time ``defaultReferenceTime`` as reference time is not provided in the file

:param path: the path to the atasets file
:param path: the path to the datasets file
:param defaultReferenceTime: reference time used if not provided in the file

:return: whether the dataset is added

.. versionadded:: 3.14
%End

bool removeDatasets( const QString &name );
%Docstring
Removes datasets from the mesh with given ``name``.

:param name: name of dataset group to remove

:return: whether the dataset is removed

.. versionadded:: 3.42
%End

bool addDatasets( QgsMeshDatasetGroup *datasetGroup /Transfer/ );
Expand Down Expand Up @@ -905,6 +916,17 @@ Sets labeling configuration. Takes ownership of the object.
.. versionadded:: 3.36
%End

bool datasetsPathUnique( const QString &path );
%Docstring
Checks whether that datasets path is already added to this mesh layer. Return ``True`` if the
dataset path is not already added.

:param path: the path to the datasets file

:return: whether the datasets path is unique

.. versionadded:: 3.42
%End

public slots:

Expand Down
11 changes: 11 additions & 0 deletions src/core/mesh/qgsmeshdataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ class CORE_EXPORT QgsMeshDataProvider: public QgsDataProvider, public QgsMeshDat
*/
virtual void close() = 0;

/**
* \brief Remove dataset group from the mesh
*
* emits dataChanged when successful
*
* \return TRUE on success
*
* \since QGIS 3.42
*/
virtual bool removeDatasetGroup( int index ) = 0;

signals:
//! Emitted when some new dataset groups have been added
void datasetGroupsAdded( int count );
Expand Down
44 changes: 41 additions & 3 deletions src/core/mesh/qgsmeshdatasetgroupstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,44 @@ bool QgsMeshDatasetGroupStore::addDatasetGroup( QgsMeshDatasetGroup *group )
return true;
}

void QgsMeshDatasetGroupStore::removeDatasetGroup( int index )
{
const QgsMeshDatasetGroupStore::DatasetGroup group = datasetGroup( index );
if ( group.first == mPersistentProvider )
mPersistentProvider->removeDatasetGroup( group.second );
else if ( group.first == &mExtraDatasets )
eraseExtraDataset( group.second );

reindexDatasetGroups();
}

void QgsMeshDatasetGroupStore::reindexDatasetGroups()
{
mRegistry.clear();
mPersistentExtraDatasetGroupIndexes.clear();
mGroupNameToGlobalIndex.clear();

int globalIndex = 0;

for ( int i = 0; i < mPersistentProvider->datasetGroupCount(); i++ )
{
const QString name = mPersistentProvider->datasetGroupMetadata( i ).name();
mRegistry[globalIndex] = DatasetGroup{mPersistentProvider, i};
mPersistentExtraDatasetGroupIndexes.append( globalIndex );
mGroupNameToGlobalIndex.insert( name, globalIndex );
globalIndex++;
}

for ( int i = 0; i < mExtraDatasets.datasetGroupCount(); i++ )
{
QgsMeshDatasetSourceInterface *source = &mExtraDatasets;
const QString name = source->datasetGroupMetadata( i ).name();
mRegistry[globalIndex] = DatasetGroup{source, i};
mGroupNameToGlobalIndex.insert( name, globalIndex );
globalIndex++;
}
}

void QgsMeshDatasetGroupStore::resetDatasetGroupTreeItem()
{
mDatasetGroupTreeRootItem.reset( new QgsMeshDatasetGroupTreeItem );
Expand Down Expand Up @@ -695,10 +733,10 @@ int QgsMeshExtraDatasetStore::addDatasetGroup( QgsMeshDatasetGroup *datasetGroup
return mGroups.size() - 1;
}

void QgsMeshExtraDatasetStore::removeDatasetGroup( int index )
void QgsMeshExtraDatasetStore::removeDatasetGroup( int groupIndex )
{
if ( index < datasetGroupCount() )
mGroups.erase( mGroups.begin() + index );
if ( groupIndex < datasetGroupCount() )
mGroups.erase( mGroups.begin() + groupIndex );


updateTemporalCapabilities();
Expand Down
10 changes: 10 additions & 0 deletions src/core/mesh/qgsmeshdatasetgroupstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ class QgsMeshDatasetGroupStore: public QObject
*/
QString groupName( int groupIndex ) const;

/**
* Removes dataset group with global index \a groupIndex
*
* \since QGIS 3.42
*/
void removeDatasetGroup( int groupIndex );

signals:
//! Emitted after dataset groups are added
void datasetGroupsAdded( QList<int> indexes );
Expand All @@ -259,6 +266,9 @@ class QgsMeshDatasetGroupStore: public QObject

void removePersistentProvider();

//! reindex dataset group stores variables from provider and extra datasets, to keep data in sync after removal of dataset group
void reindexDatasetGroups();

DatasetGroup datasetGroup( int index ) const;

//! Returns a index that is not already used
Expand Down
38 changes: 38 additions & 0 deletions src/core/mesh/qgsmeshlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,30 @@ QString QgsMeshLayer::loadDefaultStyle( bool &resultFlag )
return QgsMapLayer::loadDefaultStyle( resultFlag );
}

bool QgsMeshLayer::removeDatasets( const QString &name )
{
const int index = mDatasetGroupStore->indexFromGroupName( name );

if ( index == -1 )
{
return false;
}

const QgsMeshDatasetGroupMetadata groupMetadata = datasetGroupMetadata( index );

mDatasetGroupStore->removeDatasetGroup( index );

if ( mExtraDatasetUri.contains( groupMetadata.uri() ) )
{
mExtraDatasetUri.removeOne( groupMetadata.uri() );
}

resetDatasetGroupTreeItem();

emit dataSourceChanged();
return true;
}

bool QgsMeshLayer::addDatasets( const QString &path, const QDateTime &defaultReferenceTime )
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Expand Down Expand Up @@ -2210,3 +2234,17 @@ void QgsMeshLayer::setLabeling( QgsAbstractMeshLayerLabeling *labeling )
mLabeling = labeling;
triggerRepaint();
}

bool QgsMeshLayer::datasetsPathUnique( const QString &path )
{
if ( ! mDataProvider )
{
QgsDebugMsgLevel( QStringLiteral( "Unable to get mesh data provider" ), 2 );
return false;
}

if ( mDataProvider->dataSourceUri().contains( path ) )
return false;

return !mExtraDatasetUri.contains( path );
}
23 changes: 21 additions & 2 deletions src/core/mesh/qgsmeshlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,24 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer, public QgsAbstractProfileSo
/**
* Adds datasets to the mesh from file with \a path. Use the the time \a defaultReferenceTime as reference time is not provided in the file
*
* \param path the path to the atasets file
* \param path the path to the datasets file
* \param defaultReferenceTime reference time used if not provided in the file
* \return whether the dataset is added
*
* \since QGIS 3.14
*/
bool addDatasets( const QString &path, const QDateTime &defaultReferenceTime = QDateTime() );

/**
* Removes datasets from the mesh with given \a name.
*
* \param name name of dataset group to remove
* \return whether the dataset is removed
*
* \since QGIS 3.42
*/
bool removeDatasets( const QString &name );

/**
* Adds extra datasets to the mesh. Take ownership.
*
Expand Down Expand Up @@ -932,6 +942,16 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer, public QgsAbstractProfileSo
*/
void setLabeling( QgsAbstractMeshLayerLabeling *labeling SIP_TRANSFER );

/**
* Checks whether that datasets path is already added to this mesh layer. Return TRUE if the
* dataset path is not already added.
*
* \param path the path to the datasets file
* \return whether the datasets path is unique
*
* \since QGIS 3.42
*/
bool datasetsPathUnique( const QString &path );

public slots:

Expand Down Expand Up @@ -986,7 +1006,6 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer, public QgsAbstractProfileSo
* \param flags provider flags since QGIS 3.16
*/
bool setDataProvider( QString const &provider, const QgsDataProvider::ProviderOptions &options, Qgis::DataProviderReadFlags flags = Qgis::DataProviderReadFlags() );

#ifdef SIP_RUN
QgsMeshLayer( const QgsMeshLayer &rhs );
#endif
Expand Down
20 changes: 20 additions & 0 deletions src/core/providers/meshmemory/qgsmeshmemorydataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,26 @@ bool QgsMeshMemoryDataProvider::addDataset( const QString &uri )
return valid;
}

bool QgsMeshMemoryDataProvider::removeDatasetGroup( int index )
{
if ( index < 0 && index > datasetGroupCount() - 1 )
{
return false;
}
else
{
const QgsMeshDatasetGroupMetadata datasetGroupMeta = datasetGroupMetadata( index );

mDatasetGroups.removeAt( index );

if ( !mExtraDatasetUris.contains( datasetGroupMeta.uri() ) )
mExtraDatasetUris.removeAll( datasetGroupMeta.uri() );

emit dataChanged();
return true;
}
}

QStringList QgsMeshMemoryDataProvider::extraDatasets() const
{
return mExtraDatasetUris;
Expand Down
3 changes: 3 additions & 0 deletions src/core/providers/meshmemory/qgsmeshmemorydataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class CORE_EXPORT QgsMeshMemoryDataProvider final: public QgsMeshDataProvider
* \endcode
*/
bool addDataset( const QString &uri ) override;

bool removeDatasetGroup( int index ) override;

QStringList extraDatasets() const override;
int datasetGroupCount() const override;
int datasetCount( int groupIndex ) const override;
Expand Down
Loading

0 comments on commit 254614b

Please sign in to comment.