Skip to content

Commit 64b5c2e

Browse files
committed
[browser] Make delete file action also show for raster layers,
not just vectors
1 parent 2c16b14 commit 64b5c2e

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/core/qgsdataitem.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class CORE_EXPORT QgsDataItem : public QObject
322322

323323
Type mType;
324324
Capabilities mCapabilities;
325-
QgsDataItem *mParent = nullptr;
325+
QPointer< QgsDataItem > mParent;
326326
QVector<QgsDataItem *> mChildren; // easier to have it always
327327
State mState;
328328
QString mName;

src/providers/gdal/qgsgdaldataitems.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
#include "qgslogger.h"
1919
#include "qgssettings.h"
2020
#include "qgsogrutils.h"
21+
#include "qgsproject.h"
22+
#include "qgsgdalutils.h"
2123

2224
#include <QFileInfo>
25+
#include <QAction>
2326
#include <mutex>
27+
#include <QMessageBox>
2428

2529
// defined in qgsgdalprovider.cpp
2630
void buildSupportedRasterFileFilterAndExtensions( QString &fileFiltersString, QStringList &extensions, QStringList &wildcards );
@@ -119,7 +123,59 @@ QString QgsGdalLayerItem::layerName() const
119123
else
120124
return info.completeBaseName();
121125
}
126+
#ifdef HAVE_GUI
127+
QList<QAction *> QgsGdalLayerItem::actions( QWidget *parent )
128+
{
129+
QList<QAction *> lst;
130+
// Messages are different for files and tables
131+
const QString message = QObject::tr( "Delete File “%1”…" ).arg( mName );
132+
QAction *actionDeleteLayer = new QAction( message, parent );
133+
connect( actionDeleteLayer, &QAction::triggered, this, &QgsGdalLayerItem::deleteLayer );
134+
lst.append( actionDeleteLayer );
135+
return lst;
136+
}
137+
138+
void QgsGdalLayerItem::deleteLayer()
139+
{
140+
const QString title = QObject::tr( "Delete File" );
141+
// Check if the layer is in the project
142+
const QgsMapLayer *projectLayer = nullptr;
143+
const auto mapLayers = QgsProject::instance()->mapLayers();
144+
for ( auto it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
145+
{
146+
if ( it.value()->publicSource() == mUri )
147+
{
148+
projectLayer = it.value();
149+
}
150+
}
151+
if ( ! projectLayer )
152+
{
153+
const QString confirmMessage = QObject::tr( "Are you sure you want to delete file '%1'?" ).arg( mPath );
154+
155+
if ( QMessageBox::question( nullptr, title,
156+
confirmMessage,
157+
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
158+
return;
159+
122160

161+
if ( !QFile::remove( mPath ) )
162+
{
163+
QMessageBox::warning( nullptr, title, tr( "Could not delete file." ) );
164+
}
165+
else
166+
{
167+
QMessageBox::information( nullptr, title, tr( "File deleted successfully." ) );
168+
if ( mParent )
169+
mParent->refresh();
170+
}
171+
}
172+
else
173+
{
174+
QMessageBox::warning( nullptr, title, QObject::tr( "The layer '%1' cannot be deleted because it is in the current project as '%2',"
175+
" remove it from the project and retry." ).arg( mName, projectLayer->name() ) );
176+
}
177+
}
178+
#endif
123179
// ---------------------------------------------------------------------------
124180

125181
static QString sFilterString;

src/providers/gdal/qgsgdaldataitems.h

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class QgsGdalLayerItem : public QgsLayerItem
3737
QVector<QgsDataItem *> createChildren() override;
3838

3939
QString layerName() const override;
40+
41+
#ifdef HAVE_GUI
42+
QList<QAction *> actions( QWidget *parent ) override;
43+
public slots:
44+
void deleteLayer();
45+
#endif
4046
};
4147

4248
//! Provider for GDAL root data item

0 commit comments

Comments
 (0)