Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete selected tables as DataItem action #8507

Merged
merged 14 commits into from Jan 8, 2019
Merged
5 changes: 5 additions & 0 deletions python/core/auto_generated/qgsdataitem.sip.in
Expand Up @@ -312,6 +312,11 @@ 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
18 changes: 18 additions & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -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, [ = ]
{
Expand Down Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.h
Expand Up @@ -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 );

};

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

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

QIcon QgsDataItem::icon()
{
if ( state() == Populating && sPopulatingIcon )
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -323,6 +323,9 @@ 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this belongs in the QgsLayerItem subclass


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

Expand Down
85 changes: 44 additions & 41 deletions src/providers/mssql/qgsmssqldataitems.cpp
Expand Up @@ -568,57 +568,60 @@ QgsMssqlLayerItem::QgsMssqlLayerItem( QgsDataItem *parent, const QString &name,
#ifdef HAVE_GUI
QList<QAction *> QgsMssqlLayerItem::actions( QWidget *actionParent )
{
QgsMssqlConnectionItem *connItem = qobject_cast<QgsMssqlConnectionItem *>( parent() ? parent()->parent() : nullptr );

QList<QAction *> lst;

// delete
QAction *actionDeleteLayer = new QAction( tr( "Delete Table" ), actionParent );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ]
{
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;

QString errCause;
bool res = QgsMssqlConnection::dropTable( mUri, &errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
if ( connItem )
connItem->refresh();
}
} );
lst.append( actionDeleteLayer );

// truncate
QAction *actionTruncateLayer = new QAction( tr( "Truncate Table" ), actionParent );
connect( actionTruncateLayer, &QAction::triggered, this, [ = ]
{
if ( QMessageBox::question( nullptr, QObject::tr( "Truncate Table" ),
QObject::tr( "Are you sure you want to truncate [%1].[%2]?\n\nThis will delete all data within the table." ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QString errCause;
bool res = QgsMssqlConnection::truncateTable( mUri, &errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Truncate Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
}
truncateTable();
} );
lst.append( actionTruncateLayer );
return lst;
}

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 true;

QString errCause;
bool res = QgsMssqlConnection::dropTable( mUri, &errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
if ( connItem )
connItem->refresh();
}
return true;
}

void QgsMssqlLayerItem::truncateTable()
{
if ( QMessageBox::question( nullptr, QObject::tr( "Truncate Table" ),
QObject::tr( "Are you sure you want to truncate [%1].[%2]?\n\nThis will delete all data within the table." ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QString errCause;
bool res = QgsMssqlConnection::truncateTable( mUri, &errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Truncate Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
}
}
#endif

QgsMssqlLayerItem *QgsMssqlLayerItem::createClone()
Expand Down
6 changes: 6 additions & 0 deletions src/providers/mssql/qgsmssqldataitems.h
Expand Up @@ -140,6 +140,12 @@ class QgsMssqlLayerItem : public QgsLayerItem

bool disableInvalidGeometryHandling() const;

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

private:
QgsMssqlLayerProperty mLayerProperty;
bool mDisableInvalidGeometryHandling = false;
Expand Down
17 changes: 4 additions & 13 deletions src/providers/ogr/qgsgeopackagedataitems.cpp
Expand Up @@ -75,15 +75,6 @@ QVector<QgsDataItem *> QgsGeoPackageRootItem::createChildren()
}

#ifdef HAVE_GUI
QList<QAction *> QgsGeoPackageAbstractLayerItem::actions( QWidget * )
{
QList<QAction *> lst;
QAction *actionDeleteLayer = new QAction( tr( "Delete Layer '%1'…" ).arg( mName ), this );
connect( actionDeleteLayer, &QAction::triggered, this, &QgsGeoPackageAbstractLayerItem::deleteLayer );
lst.append( actionDeleteLayer );
return lst;
}

void QgsGeoPackageRootItem::onConnectionsChanged()
{
refresh();
Expand Down Expand Up @@ -487,7 +478,7 @@ void QgsGeoPackageCollectionItem::vacuumGeoPackageDbAction()
}
}

void QgsGeoPackageAbstractLayerItem::deleteLayer()
bool QgsGeoPackageAbstractLayerItem::deleteLayer()
{
// Check if the layer(s) are in the registry
QList<QgsMapLayer *> layersList;
Expand All @@ -505,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() )
Expand All @@ -532,7 +523,7 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
if ( mParent )
mParent->refreshConnections();
}

return true;
}
#endif

Expand Down
5 changes: 2 additions & 3 deletions src/providers/ogr/qgsgeopackagedataitems.h
Expand Up @@ -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
};

Expand Down
9 changes: 3 additions & 6 deletions src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -317,10 +317,6 @@ QList<QAction *> QgsPGLayerItem::actions( QWidget *parent )
connect( actionRenameLayer, &QAction::triggered, this, &QgsPGLayerItem::renameLayer );
lst.append( actionRenameLayer );

QAction *actionDeleteLayer = new QAction( tr( "Delete %1" ).arg( typeName ), parent );
connect( actionDeleteLayer, &QAction::triggered, this, &QgsPGLayerItem::deleteLayer );
lst.append( actionDeleteLayer );

if ( !mLayerProperty.isView )
{
QAction *actionTruncateLayer = new QAction( tr( "Truncate %1" ).arg( typeName ), parent );
Expand All @@ -338,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 );
Expand All @@ -357,6 +353,7 @@ void QgsPGLayerItem::deleteLayer()
if ( mParent )
mParent->refresh();
}
return true;
}

void QgsPGLayerItem::renameLayer()
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresdataitems.h
Expand Up @@ -129,7 +129,7 @@ class QgsPGLayerItem : public QgsLayerItem

public slots:
#ifdef HAVE_GUI
void deleteLayer();
bool deleteLayer() override;
void renameLayer();
void truncateTable();
void refreshMaterializedView();
Expand Down
16 changes: 3 additions & 13 deletions src/providers/spatialite/qgsspatialitedataitems.cpp
Expand Up @@ -41,23 +41,12 @@ QgsSLLayerItem::QgsSLLayerItem( QgsDataItem *parent, const QString &name, const
}

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

QAction *actionDeleteLayer = new QAction( tr( "Delete Layer" ), parent );
connect( actionDeleteLayer, &QAction::triggered, this, &QgsSLLayerItem::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;
Expand All @@ -71,6 +60,7 @@ void QgsSLLayerItem::deleteLayer()
QMessageBox::information( nullptr, tr( "Delete Layer" ), tr( "Layer deleted successfully." ) );
mParent->refresh();
}
return true;
}
#endif

Expand Down
7 changes: 1 addition & 6 deletions src/providers/spatialite/qgsspatialitedataitems.h
Expand Up @@ -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
};

Expand Down