Skip to content

Commit

Permalink
delete layer action only on items that have capability "delete"
Browse files Browse the repository at this point in the history
check if the item with the menu has this capability and only handle the selected items with this capabiltiy
  • Loading branch information
signedav committed Nov 21, 2018
1 parent 4296c77 commit 1136aab
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 24 deletions.
11 changes: 6 additions & 5 deletions python/core/auto_generated/qgsdataitem.sip.in
Expand Up @@ -201,6 +201,7 @@ Items that return valid URI will be returned in mime data when dragging a select
Fast,
Collapse,
Rename,
Delete,
};
typedef QFlags<QgsDataItem::Capability> Capabilities;

Expand Down Expand Up @@ -312,11 +313,6 @@ Sets a custom sorting ``key`` for the item.
void moveToThread( QThread *targetThread );
%Docstring
Move object and all its descendants to thread
%End

virtual bool deleteLayer();
%Docstring
Delete this layer item
%End

protected:
Expand Down Expand Up @@ -490,6 +486,11 @@ Returns the string representation of the given ``layerType``
Returns the icon name of the given ``layerType``

.. versionadded:: 3
%End

virtual bool deleteLayer();
%Docstring
Delete this layer item
%End

protected:
Expand Down
28 changes: 19 additions & 9 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -366,14 +366,24 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
} );
menu->addAction( addAction );

const QString deleteText = selectedItems.count() == 1 ? tr( "Delete Layer" )
: tr( "Delete Selected Layers" );
QAction *deleteAction = new QAction( deleteText, menu );
connect( deleteAction, &QAction::triggered, this, [ = ]
if ( item->capabilities2() & QgsDataItem::Delete )
{
deleteLayers( selectedItems );
} );
menu->addAction( deleteAction );
QList<QgsLayerItem *> selectedDeletableItems;
for ( QgsDataItem *selectedItem : selectedItems )
{
if ( qobject_cast<QgsLayerItem *>( selectedItem ) && ( selectedItem->capabilities2() & QgsDataItem::Delete ) )
selectedDeletableItems.append( qobject_cast<QgsLayerItem *>( selectedItem ) );
}

const QString deleteText = selectedDeletableItems.count() == 1 ? tr( "Delete Layer" )
: tr( "Delete Selected Layers" );
QAction *deleteAction = new QAction( deleteText, menu );
connect( deleteAction, &QAction::triggered, this, [ = ]
{
deleteLayers( selectedDeletableItems );
} );
menu->addAction( deleteAction );
}

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

void QgsLayerItemGuiProvider::deleteLayers( const QList<QgsDataItem *> &items )
void QgsLayerItemGuiProvider::deleteLayers( const QList<QgsLayerItem *> &items )
{
for ( QgsDataItem *item : items )
for ( QgsLayerItem *item : items )
{
if ( !item->deleteLayer() )
QMessageBox::information( QgisApp::instance(), tr( "Delete Layer" ), tr( "Item Layer %1 cannot be deleted." ).arg( item->name() ) );
Expand Down
2 changes: 1 addition & 1 deletion src/app/browser/qgsinbuiltdataitemproviders.h
Expand Up @@ -100,7 +100,7 @@ class QgsLayerItemGuiProvider : public QObject, public QgsDataItemGuiProvider

void addLayersFromItems( const QList<QgsDataItem *> &items );
void showPropertiesForItem( QgsLayerItem *item );
void deleteLayers( const QList<QgsDataItem *> &items );
void deleteLayers( const QList<QgsLayerItem *> &items );

};

Expand Down
10 changes: 5 additions & 5 deletions src/core/qgsdataitem.cpp
Expand Up @@ -219,11 +219,6 @@ void QgsDataItem::moveToThread( QThread *targetThread )
QObject::moveToThread( targetThread );
}

bool QgsDataItem::deleteLayer()
{
return false;
}

QIcon QgsDataItem::icon()
{
if ( state() == Populating && sPopulatingIcon )
Expand Down Expand Up @@ -706,6 +701,11 @@ QString QgsLayerItem::iconName( QgsLayerItem::LayerType layerType )
}
}

bool QgsLayerItem::deleteLayer()
{
return false;
}

bool QgsLayerItem::equal( const QgsDataItem *other )
{
//QgsDebugMsg ( mPath + " x " + other->mPath );
Expand Down
9 changes: 5 additions & 4 deletions src/core/qgsdataitem.h
Expand Up @@ -210,8 +210,9 @@ class CORE_EXPORT QgsDataItem : public QObject
SetCrs = 1 << 0, //!< Can set CRS on layer or group of layers
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.
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
Collapse = 1 << 3, //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
Collapse = 1 << 3, //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
Rename = 1 << 4, //!< Item can be renamed
Delete = 1 << 5, //!< Item can be deleted
};
Q_DECLARE_FLAGS( Capabilities, Capability )

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

//! Delete this layer item
virtual bool deleteLayer();

protected:
virtual void populate( const QVector<QgsDataItem *> &children );

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

//! Delete this layer item
virtual bool deleteLayer();

protected:

//! The provider key
Expand Down
1 change: 1 addition & 0 deletions src/providers/mssql/qgsmssqldataitems.cpp
Expand Up @@ -561,6 +561,7 @@ QgsMssqlLayerItem::QgsMssqlLayerItem( QgsDataItem *parent, const QString &name,
: QgsLayerItem( parent, name, path, QString(), layerType, QStringLiteral( "mssql" ) )
, mLayerProperty( layerProperty )
{
mCapabilities |= Delete;
mUri = createUri();
setState( Populated );
}
Expand Down
1 change: 1 addition & 0 deletions src/providers/ogr/qgsgeopackagedataitems.cpp
Expand Up @@ -699,6 +699,7 @@ bool QgsGeoPackageConnectionItem::equal( const QgsDataItem *other )
QgsGeoPackageAbstractLayerItem::QgsGeoPackageAbstractLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, QgsLayerItem::LayerType layerType, const QString &providerKey )
: QgsLayerItem( parent, name, path, uri, layerType, providerKey )
{
mCapabilities |= Delete;
mToolTip = uri;
setState( Populated ); // no children are expected
}
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -296,6 +296,7 @@ QgsPGLayerItem::QgsPGLayerItem( QgsDataItem *parent, const QString &name, const
: QgsLayerItem( parent, name, path, QString(), layerType, QStringLiteral( "postgres" ) )
, mLayerProperty( layerProperty )
{
mCapabilities |= Delete;
mUri = createUri();
setState( Populated );
Q_ASSERT( mLayerProperty.size() == 1 );
Expand Down
1 change: 1 addition & 0 deletions src/providers/spatialite/qgsspatialitedataitems.cpp
Expand Up @@ -37,6 +37,7 @@ QGISEXTERN bool deleteLayer( const QString &dbPath, const QString &tableName, QS
QgsSLLayerItem::QgsSLLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType )
: QgsLayerItem( parent, name, path, uri, layerType, QStringLiteral( "spatialite" ) )
{
mCapabilities |= Delete;
setState( Populated ); // no children are expected
}

Expand Down

0 comments on commit 1136aab

Please sign in to comment.