Skip to content
Permalink
Browse files

action deleteLayer in the dataitemproviders

implemented for derived functions in:
- `QgsGeoPackageAbstractLayerItem`
- `QgsSLLayerItem`
- `QgsPGLayerItem`
- `QgsMssqlLayerItem`
  • Loading branch information
signedav committed Nov 20, 2018
1 parent f06bb79 commit a1ea0ac540a63c8653e653a28a0a4ff3889fccde
@@ -314,8 +314,10 @@ Sets a custom sorting ``key`` for the item.
Move object and all its descendants to thread
%End

void setSelectedItems( const QList<QgsDataItem *> &selectedItems );
QList<QgsDataItem *> selectedItems() const;
virtual bool deleteLayer();
%Docstring
Delete this layer item
%End

protected:
virtual void populate( const QVector<QgsDataItem *> &children );
@@ -366,6 +366,15 @@ 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, [ = ]
{
deleteLayers( selectedItems );
} );
menu->addAction( deleteAction );

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

void QgsLayerItemGuiProvider::deleteLayers( const QList<QgsDataItem *> &items )
{
for ( QgsDataItem *item : items )
{
if ( !item->deleteLayer() )
QMessageBox::information( QgisApp::instance(), tr( "Delete Layer" ), tr( "Item Layer %1 cannot be deleted." ).arg( item->name() ) );
}
}

void QgsLayerItemGuiProvider::showPropertiesForItem( QgsLayerItem *item )
{
if ( ! item )
@@ -100,6 +100,7 @@ class QgsLayerItemGuiProvider : public QObject, public QgsDataItemGuiProvider

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

};

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

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

QIcon QgsDataItem::icon()
{
if ( state() == Populating && sPopulatingIcon )
@@ -323,8 +323,8 @@ class CORE_EXPORT QgsDataItem : public QObject
//! Move object and all its descendants to thread
void moveToThread( QThread *targetThread );

void setSelectedItems( const QList<QgsDataItem *> &selectedItems ) { mSelectedItems = selectedItems; }
QList<QgsDataItem *> selectedItems() const { return mSelectedItems; }
//! Delete this layer item
virtual bool deleteLayer();

protected:
virtual void populate( const QVector<QgsDataItem *> &children );
@@ -211,7 +211,6 @@ void QgsBrowserDockWidget::showContextMenu( QPoint pt )
QMenu *menu = new QMenu( this );

const QList<QMenu *> menus = item->menus( menu );
item->setSelectedItems( selectedItems );
QList<QAction *> actions = item->actions( menu );

if ( !menus.isEmpty() )
@@ -570,46 +570,24 @@ QList<QAction *> QgsMssqlLayerItem::actions( QWidget *actionParent )
{
QList<QAction *> lst;

// delete
const QString deleteText = selectedItems().count() == 1 ? tr( "Delete Table" )
: tr( "Delete Selected Tables" );
QAction *actionDeleteLayer = new QAction( deleteText, actionParent );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ]
{
QList<QgsDataItem *> items = selectedItems();
for ( QgsDataItem *item : items )
{
if ( QgsMssqlLayerItem *mssqlLayerItem = qobject_cast< QgsMssqlLayerItem *>( item ) )
mssqlLayerItem->deleteLayer();
}
} );
lst.append( actionDeleteLayer );

// truncate
const QString truncateText = selectedItems().count() == 1 ? tr( "Truncate Table" )
: tr( "Truncate Selected Tables" );
QAction *actionTruncateLayer = new QAction( truncateText, actionParent );
QAction *actionTruncateLayer = new QAction( tr( "Truncate Table" ), actionParent );
connect( actionTruncateLayer, &QAction::triggered, this, [ = ]
{
QList<QgsDataItem *> items = selectedItems();
for ( QgsDataItem *item : items )
{
if ( QgsMssqlLayerItem *mssqlLayerItem = qobject_cast< QgsMssqlLayerItem *>( item ) )
mssqlLayerItem->truncateTable();
}
truncateTable();
} );
lst.append( actionTruncateLayer );
return lst;
}

void QgsMssqlLayerItem::deleteLayer()
bool QgsMssqlLayerItem::deleteLayer()
{
QgsMssqlConnectionItem *connItem = qobject_cast<QgsMssqlConnectionItem *>( parent() ? parent()->parent() : nullptr );

if ( QMessageBox::question( nullptr, QObject::tr( "Delete Table" ),
QObject::tr( "Are you sure you want to delete [%1].[%2]?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
return true;

QString errCause;
bool res = QgsMssqlConnection::dropTable( mUri, &errCause );
@@ -623,6 +601,7 @@ void QgsMssqlLayerItem::deleteLayer()
if ( connItem )
connItem->refresh();
}
return true;
}

void QgsMssqlLayerItem::truncateTable()
@@ -142,7 +142,7 @@ class QgsMssqlLayerItem : public QgsLayerItem

public slots:
#ifdef HAVE_GUI
void deleteLayer();
bool deleteLayer() override;
void truncateTable();
#endif

@@ -75,26 +75,6 @@ QVector<QgsDataItem *> QgsGeoPackageRootItem::createChildren()
}

#ifdef HAVE_GUI
QList<QAction *> QgsGeoPackageAbstractLayerItem::actions( QWidget * )
{
QList<QAction *> lst;

const QString deleteText = selectedItems().count() == 1 ? tr( "Delete Layer '%1'…" ).arg( mName )
: tr( "Delete Selected Layers" );
QAction *actionDeleteLayer = new QAction( deleteText, this );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ]
{
QList<QgsDataItem *> items = selectedItems();
for ( QgsDataItem *item : items )
{
if ( QgsGeoPackageAbstractLayerItem *gpkgAbstractItem = qobject_cast< QgsGeoPackageAbstractLayerItem *>( item ) )
gpkgAbstractItem->deleteLayer();
}
} ) ;
lst.append( actionDeleteLayer );
return lst;
}

void QgsGeoPackageRootItem::onConnectionsChanged()
{
refresh();
@@ -498,7 +478,7 @@ void QgsGeoPackageCollectionItem::vacuumGeoPackageDbAction()
}
}

void QgsGeoPackageAbstractLayerItem::deleteLayer()
bool QgsGeoPackageAbstractLayerItem::deleteLayer()
{
// Check if the layer(s) are in the registry
QList<QgsMapLayer *> layersList;
@@ -516,14 +496,14 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Layer" ), QObject::tr( "The layer <b>%1</b> exists in the current project <b>%2</b>,"
" do you want to remove it from the project and delete it?" ).arg( mName, layersList.at( 0 )->name() ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
{
return;
return true;
}
}
else if ( QMessageBox::question( nullptr, QObject::tr( "Delete Layer" ),
QObject::tr( "Are you sure you want to delete layer <b>%1</b> from GeoPackage?" ).arg( mName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
{
return;
return true;
}

if ( layersList.isEmpty() )
@@ -543,7 +523,7 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
if ( mParent )
mParent->refreshConnections();
}

return true;
}
#endif

@@ -36,10 +36,9 @@ class QgsGeoPackageAbstractLayerItem : public QgsLayerItem
* the real deletion implementation
*/
virtual bool executeDeleteLayer( QString &errCause );

#ifdef HAVE_GUI
QList<QAction *> actions( QWidget *menu ) override;
public slots:
virtual void deleteLayer();
bool deleteLayer() override;
#endif
};

@@ -317,20 +317,6 @@ QList<QAction *> QgsPGLayerItem::actions( QWidget *parent )
connect( actionRenameLayer, &QAction::triggered, this, &QgsPGLayerItem::renameLayer );
lst.append( actionRenameLayer );

const QString deleteText = selectedItems().count() == 1 ? tr( "Delete %1" ).arg( typeName )
: tr( "Delete Selected Tables" );
QAction *actionDeleteLayer = new QAction( deleteText, parent );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ]
{
QList<QgsDataItem *> items = selectedItems();
for ( QgsDataItem *item : items )
{
if ( QgsPGLayerItem *pgLayerItem = qobject_cast< QgsPGLayerItem *>( item ) )
pgLayerItem->deleteLayer();
}
} ) ;
lst.append( actionDeleteLayer );

if ( !mLayerProperty.isView )
{
QAction *actionTruncateLayer = new QAction( tr( "Truncate %1" ).arg( typeName ), parent );
@@ -348,12 +334,12 @@ QList<QAction *> QgsPGLayerItem::actions( QWidget *parent )
return lst;
}

void QgsPGLayerItem::deleteLayer()
bool QgsPGLayerItem::deleteLayer()
{
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Table" ),
QObject::tr( "Are you sure you want to delete %1.%2?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
return true;

QString errCause;
bool res = ::deleteLayer( mUri, errCause );
@@ -367,6 +353,7 @@ void QgsPGLayerItem::deleteLayer()
if ( mParent )
mParent->refresh();
}
return true;
}

void QgsPGLayerItem::renameLayer()
@@ -129,7 +129,7 @@ class QgsPGLayerItem : public QgsLayerItem

public slots:
#ifdef HAVE_GUI
void deleteLayer();
bool deleteLayer() override;
void renameLayer();
void truncateTable();
void refreshMaterializedView();
@@ -41,34 +41,12 @@ QgsSLLayerItem::QgsSLLayerItem( QgsDataItem *parent, const QString &name, const
}

#ifdef HAVE_GUI
QList<QAction *> QgsSLLayerItem::actions( QWidget *parent )
{
QList<QAction *> lst;

const QString deleteText = selectedItems().count() == 1 ? tr( "Delete Layer '%1'…" ).arg( mName )
: tr( "Delete Selected Layers" );
QAction *actionDeleteLayer = new QAction( deleteText, parent );

connect( actionDeleteLayer, &QAction::triggered, this, [ = ]
{
QList<QgsDataItem *> items = selectedItems();
for ( QgsDataItem *item : items )
{
if ( QgsSLLayerItem *slLayerItem = qobject_cast< QgsSLLayerItem *>( item ) )
slLayerItem->deleteLayer();
}
} ) ;
lst.append( actionDeleteLayer );

return lst;
}

void QgsSLLayerItem::deleteLayer()
bool QgsSLLayerItem::deleteLayer()
{
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Object" ),
QObject::tr( "Are you sure you want to delete %1?" ).arg( mName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
return true;

QgsDataSourceUri uri( mUri );
QString errCause;
@@ -82,6 +60,7 @@ void QgsSLLayerItem::deleteLayer()
QMessageBox::information( nullptr, tr( "Delete Layer" ), tr( "Layer deleted successfully." ) );
mParent->refresh();
}
return true;
}
#endif

@@ -24,12 +24,7 @@ class QgsSLLayerItem : public QgsLayerItem
QgsSLLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType );

#ifdef HAVE_GUI
QList<QAction *> actions( QWidget *parent ) override;
#endif

public slots:
#ifdef HAVE_GUI
void deleteLayer();
bool deleteLayer() override;
#endif
};

0 comments on commit a1ea0ac

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