Skip to content

Commit

Permalink
pass items to delete layer activity
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Nov 15, 2018
1 parent 3e14761 commit fa06246
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions python/core/auto_generated/qgsdataitem.sip.in
Expand Up @@ -314,6 +314,9 @@ 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;

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

Expand Down
4 changes: 4 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 );

void setSelectedItems( const QList<QgsDataItem *> &selectedItems ) { mSelectedItems = selectedItems; }
QList<QgsDataItem *> selectedItems() const { return mSelectedItems; }

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

Expand Down Expand Up @@ -418,6 +421,7 @@ class CORE_EXPORT QgsDataItem : public QObject
QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher;
// number of items currently in loading (populating) state
static QgsAnimatedIcon *sPopulatingIcon;
QList<QgsDataItem *> mSelectedItems;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsbrowserdockwidget.cpp
Expand Up @@ -211,6 +211,7 @@ 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() )
Expand Down
27 changes: 20 additions & 7 deletions src/providers/ogr/qgsgeopackagedataitems.cpp
Expand Up @@ -77,9 +77,21 @@ QVector<QgsDataItem *> QgsGeoPackageRootItem::createChildren()
#ifdef HAVE_GUI
QList<QAction *> QgsGeoPackageAbstractLayerItem::actions( QWidget * )
{
//dave: decide if more than one layer is selected. if more then "Delete selected Layers"
QList<QAction *> lst;
QAction *actionDeleteLayer = new QAction( tr( "Delete Layer '%1'…" ).arg( mName ), this );
connect( actionDeleteLayer, &QAction::triggered, this, &QgsGeoPackageAbstractLayerItem::deleteLayer );

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 ( const QgsDataItem *item : items )
{
if ( const QgsGeoPackageAbstractLayerItem *gpkgAbstractItem = qobject_cast<const QgsGeoPackageAbstractLayerItem *>( item ) )
deleteLayer( gpkgAbstractItem );
}
} ) ;
lst.append( actionDeleteLayer );
return lst;
}
Expand Down Expand Up @@ -487,14 +499,15 @@ void QgsGeoPackageCollectionItem::vacuumGeoPackageDbAction()
}
}

void QgsGeoPackageAbstractLayerItem::deleteLayer()
void QgsGeoPackageAbstractLayerItem::deleteLayer( const QgsGeoPackageAbstractLayerItem *item )
{
//dave: here we are - we need to find out what other layers in the gpkg are selected and delete em
// Check if the layer(s) are in the registry
QList<QgsMapLayer *> layersList;
const auto mapLayers( QgsProject::instance()->mapLayers() );
for ( QgsMapLayer *layer : mapLayers )
{
if ( layer->publicSource() == mUri )
if ( layer->publicSource() == item->uri() )
{
layersList << layer;
}
Expand All @@ -503,13 +516,13 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
if ( ! layersList.isEmpty( ) )
{
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 )
" do you want to remove it from the project and delete it?" ).arg( item->name(), layersList.at( 0 )->name() ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
{
return;
}
}
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 ),
QObject::tr( "Are you sure you want to delete layer <b>%1</b> from GeoPackage?" ).arg( item->name() ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
{
return;
Expand All @@ -528,7 +541,7 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
}
else
{
QMessageBox::information( nullptr, tr( "Delete Layer" ), tr( "Layer <b>%1</b> deleted successfully." ).arg( mName ) );
QMessageBox::information( nullptr, tr( "Delete Layer" ), tr( "Layer <b>%1</b> deleted successfully." ).arg( item->name() ) );
if ( mParent )
mParent->refreshConnections();
}
Expand Down
5 changes: 4 additions & 1 deletion src/providers/ogr/qgsgeopackagedataitems.h
Expand Up @@ -28,6 +28,9 @@ class QgsGeoPackageAbstractLayerItem : public QgsLayerItem
{
Q_OBJECT

//! Returns layer uri or empty string if layer cannot be created
QString uri() const { return mUri; }

protected:
QgsGeoPackageAbstractLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, const QString &providerKey );

Expand All @@ -39,7 +42,7 @@ class QgsGeoPackageAbstractLayerItem : public QgsLayerItem
#ifdef HAVE_GUI
QList<QAction *> actions( QWidget *menu ) override;
public slots:
virtual void deleteLayer();
virtual void deleteLayer( const QgsGeoPackageAbstractLayerItem *item );
#endif
};

Expand Down

0 comments on commit fa06246

Please sign in to comment.