Skip to content
Permalink
Browse files

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
  • Loading branch information
signedav committed Nov 21, 2018
1 parent 4296c77 commit 1136aabf5b0f86b1d0804bc5e3eaf65ff5b5ff35
@@ -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;

@@ -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:
@@ -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:
@@ -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, [ = ]
@@ -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() ) );
@@ -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 );

};

@@ -219,11 +219,6 @@ void QgsDataItem::moveToThread( QThread *targetThread )
QObject::moveToThread( targetThread );
}

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

QIcon QgsDataItem::icon()
{
if ( state() == Populating && sPopulatingIcon )
@@ -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 );
@@ -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 )

@@ -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 );

@@ -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
@@ -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 );
}
@@ -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
}
@@ -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 );
@@ -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
}

0 comments on commit 1136aab

Please sign in to comment.
You can’t perform that action at this time.