Skip to content

Commit 1136aab

Browse files
committed
delete layer action only on items that have capability "delete"
check if the item with the menu has this capability and only handle the selected items with this capabiltiy
1 parent 4296c77 commit 1136aab

File tree

9 files changed

+40
-24
lines changed

9 files changed

+40
-24
lines changed

python/core/auto_generated/qgsdataitem.sip.in

+6-5
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Items that return valid URI will be returned in mime data when dragging a select
201201
Fast,
202202
Collapse,
203203
Rename,
204+
Delete,
204205
};
205206
typedef QFlags<QgsDataItem::Capability> Capabilities;
206207

@@ -312,11 +313,6 @@ Sets a custom sorting ``key`` for the item.
312313
void moveToThread( QThread *targetThread );
313314
%Docstring
314315
Move object and all its descendants to thread
315-
%End
316-
317-
virtual bool deleteLayer();
318-
%Docstring
319-
Delete this layer item
320316
%End
321317

322318
protected:
@@ -490,6 +486,11 @@ Returns the string representation of the given ``layerType``
490486
Returns the icon name of the given ``layerType``
491487

492488
.. versionadded:: 3
489+
%End
490+
491+
virtual bool deleteLayer();
492+
%Docstring
493+
Delete this layer item
493494
%End
494495

495496
protected:

src/app/browser/qgsinbuiltdataitemproviders.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,24 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
366366
} );
367367
menu->addAction( addAction );
368368

369-
const QString deleteText = selectedItems.count() == 1 ? tr( "Delete Layer" )
370-
: tr( "Delete Selected Layers" );
371-
QAction *deleteAction = new QAction( deleteText, menu );
372-
connect( deleteAction, &QAction::triggered, this, [ = ]
369+
if ( item->capabilities2() & QgsDataItem::Delete )
373370
{
374-
deleteLayers( selectedItems );
375-
} );
376-
menu->addAction( deleteAction );
371+
QList<QgsLayerItem *> selectedDeletableItems;
372+
for ( QgsDataItem *selectedItem : selectedItems )
373+
{
374+
if ( qobject_cast<QgsLayerItem *>( selectedItem ) && ( selectedItem->capabilities2() & QgsDataItem::Delete ) )
375+
selectedDeletableItems.append( qobject_cast<QgsLayerItem *>( selectedItem ) );
376+
}
377+
378+
const QString deleteText = selectedDeletableItems.count() == 1 ? tr( "Delete Layer" )
379+
: tr( "Delete Selected Layers" );
380+
QAction *deleteAction = new QAction( deleteText, menu );
381+
connect( deleteAction, &QAction::triggered, this, [ = ]
382+
{
383+
deleteLayers( selectedDeletableItems );
384+
} );
385+
menu->addAction( deleteAction );
386+
}
377387

378388
QAction *propertiesAction = new QAction( tr( "Layer Properties…" ), menu );
379389
connect( propertiesAction, &QAction::triggered, this, [ = ]
@@ -442,9 +452,9 @@ void QgsLayerItemGuiProvider::addLayersFromItems( const QList<QgsDataItem *> &it
442452
QgisApp::instance()->handleDropUriList( layerUriList );
443453
}
444454

445-
void QgsLayerItemGuiProvider::deleteLayers( const QList<QgsDataItem *> &items )
455+
void QgsLayerItemGuiProvider::deleteLayers( const QList<QgsLayerItem *> &items )
446456
{
447-
for ( QgsDataItem *item : items )
457+
for ( QgsLayerItem *item : items )
448458
{
449459
if ( !item->deleteLayer() )
450460
QMessageBox::information( QgisApp::instance(), tr( "Delete Layer" ), tr( "Item Layer %1 cannot be deleted." ).arg( item->name() ) );

src/app/browser/qgsinbuiltdataitemproviders.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class QgsLayerItemGuiProvider : public QObject, public QgsDataItemGuiProvider
100100

101101
void addLayersFromItems( const QList<QgsDataItem *> &items );
102102
void showPropertiesForItem( QgsLayerItem *item );
103-
void deleteLayers( const QList<QgsDataItem *> &items );
103+
void deleteLayers( const QList<QgsLayerItem *> &items );
104104

105105
};
106106

src/core/qgsdataitem.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,6 @@ void QgsDataItem::moveToThread( QThread *targetThread )
219219
QObject::moveToThread( targetThread );
220220
}
221221

222-
bool QgsDataItem::deleteLayer()
223-
{
224-
return false;
225-
}
226-
227222
QIcon QgsDataItem::icon()
228223
{
229224
if ( state() == Populating && sPopulatingIcon )
@@ -706,6 +701,11 @@ QString QgsLayerItem::iconName( QgsLayerItem::LayerType layerType )
706701
}
707702
}
708703

704+
bool QgsLayerItem::deleteLayer()
705+
{
706+
return false;
707+
}
708+
709709
bool QgsLayerItem::equal( const QgsDataItem *other )
710710
{
711711
//QgsDebugMsg ( mPath + " x " + other->mPath );

src/core/qgsdataitem.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ class CORE_EXPORT QgsDataItem : public QObject
210210
SetCrs = 1 << 0, //!< Can set CRS on layer or group of layers
211211
Fertile = 1 << 1, //!< Can create children. Even items without this capability may have children, but cannot create them, it means that children are created by item ancestors.
212212
Fast = 1 << 2, //!< CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,wfs,wcs,postgres...) are considered fast because they are reading data only from QgsSettings
213-
Collapse = 1 << 3, //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
213+
Collapse = 1 << 3, //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
214214
Rename = 1 << 4, //!< Item can be renamed
215+
Delete = 1 << 5, //!< Item can be deleted
215216
};
216217
Q_DECLARE_FLAGS( Capabilities, Capability )
217218

@@ -323,9 +324,6 @@ class CORE_EXPORT QgsDataItem : public QObject
323324
//! Move object and all its descendants to thread
324325
void moveToThread( QThread *targetThread );
325326

326-
//! Delete this layer item
327-
virtual bool deleteLayer();
328-
329327
protected:
330328
virtual void populate( const QVector<QgsDataItem *> &children );
331329

@@ -508,6 +506,9 @@ class CORE_EXPORT QgsLayerItem : public QgsDataItem
508506
*/
509507
static QString iconName( LayerType layerType );
510508

509+
//! Delete this layer item
510+
virtual bool deleteLayer();
511+
511512
protected:
512513

513514
//! The provider key

src/providers/mssql/qgsmssqldataitems.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ QgsMssqlLayerItem::QgsMssqlLayerItem( QgsDataItem *parent, const QString &name,
561561
: QgsLayerItem( parent, name, path, QString(), layerType, QStringLiteral( "mssql" ) )
562562
, mLayerProperty( layerProperty )
563563
{
564+
mCapabilities |= Delete;
564565
mUri = createUri();
565566
setState( Populated );
566567
}

src/providers/ogr/qgsgeopackagedataitems.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ bool QgsGeoPackageConnectionItem::equal( const QgsDataItem *other )
699699
QgsGeoPackageAbstractLayerItem::QgsGeoPackageAbstractLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, QgsLayerItem::LayerType layerType, const QString &providerKey )
700700
: QgsLayerItem( parent, name, path, uri, layerType, providerKey )
701701
{
702+
mCapabilities |= Delete;
702703
mToolTip = uri;
703704
setState( Populated ); // no children are expected
704705
}

src/providers/postgres/qgspostgresdataitems.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ QgsPGLayerItem::QgsPGLayerItem( QgsDataItem *parent, const QString &name, const
296296
: QgsLayerItem( parent, name, path, QString(), layerType, QStringLiteral( "postgres" ) )
297297
, mLayerProperty( layerProperty )
298298
{
299+
mCapabilities |= Delete;
299300
mUri = createUri();
300301
setState( Populated );
301302
Q_ASSERT( mLayerProperty.size() == 1 );

src/providers/spatialite/qgsspatialitedataitems.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ QGISEXTERN bool deleteLayer( const QString &dbPath, const QString &tableName, QS
3737
QgsSLLayerItem::QgsSLLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType )
3838
: QgsLayerItem( parent, name, path, uri, layerType, QStringLiteral( "spatialite" ) )
3939
{
40+
mCapabilities |= Delete;
4041
setState( Populated ); // no children are expected
4142
}
4243

0 commit comments

Comments
 (0)