Skip to content
Permalink
Browse files

[browser] Make delete file action also show for raster layers,

not just vectors
  • Loading branch information
nyalldawson committed Oct 11, 2018
1 parent 2c16b14 commit 64b5c2ee40b1c82fa34ff262e8bc45b0dcf321fc
Showing with 63 additions and 1 deletion.
  1. +1 −1 src/core/qgsdataitem.h
  2. +56 −0 src/providers/gdal/qgsgdaldataitems.cpp
  3. +6 −0 src/providers/gdal/qgsgdaldataitems.h
@@ -322,7 +322,7 @@ class CORE_EXPORT QgsDataItem : public QObject

Type mType;
Capabilities mCapabilities;
QgsDataItem *mParent = nullptr;
QPointer< QgsDataItem > mParent;
QVector<QgsDataItem *> mChildren; // easier to have it always
State mState;
QString mName;
@@ -18,9 +18,13 @@
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgsogrutils.h"
#include "qgsproject.h"
#include "qgsgdalutils.h"

#include <QFileInfo>
#include <QAction>
#include <mutex>
#include <QMessageBox>

// defined in qgsgdalprovider.cpp
void buildSupportedRasterFileFilterAndExtensions( QString &fileFiltersString, QStringList &extensions, QStringList &wildcards );
@@ -119,7 +123,59 @@ QString QgsGdalLayerItem::layerName() const
else
return info.completeBaseName();
}
#ifdef HAVE_GUI
QList<QAction *> QgsGdalLayerItem::actions( QWidget *parent )
{
QList<QAction *> lst;
// Messages are different for files and tables
const QString message = QObject::tr( "Delete File “%1”…" ).arg( mName );
QAction *actionDeleteLayer = new QAction( message, parent );
connect( actionDeleteLayer, &QAction::triggered, this, &QgsGdalLayerItem::deleteLayer );
lst.append( actionDeleteLayer );
return lst;
}

void QgsGdalLayerItem::deleteLayer()
{
const QString title = QObject::tr( "Delete File" );
// Check if the layer is in the project
const QgsMapLayer *projectLayer = nullptr;
const auto mapLayers = QgsProject::instance()->mapLayers();
for ( auto it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
{
if ( it.value()->publicSource() == mUri )
{
projectLayer = it.value();
}
}
if ( ! projectLayer )
{
const QString confirmMessage = QObject::tr( "Are you sure you want to delete file '%1'?" ).arg( mPath );

if ( QMessageBox::question( nullptr, title,
confirmMessage,
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;


if ( !QFile::remove( mPath ) )
{
QMessageBox::warning( nullptr, title, tr( "Could not delete file." ) );
}
else
{
QMessageBox::information( nullptr, title, tr( "File deleted successfully." ) );
if ( mParent )
mParent->refresh();
}
}
else
{
QMessageBox::warning( nullptr, title, QObject::tr( "The layer '%1' cannot be deleted because it is in the current project as '%2',"
" remove it from the project and retry." ).arg( mName, projectLayer->name() ) );
}
}
#endif
// ---------------------------------------------------------------------------

static QString sFilterString;
@@ -37,6 +37,12 @@ class QgsGdalLayerItem : public QgsLayerItem
QVector<QgsDataItem *> createChildren() override;

QString layerName() const override;

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

//! Provider for GDAL root data item

0 comments on commit 64b5c2e

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