|
18 | 18 | #include "qgslogger.h"
|
19 | 19 | #include "qgssettings.h"
|
20 | 20 | #include "qgsogrutils.h"
|
| 21 | +#include "qgsproject.h" |
| 22 | +#include "qgsgdalutils.h" |
21 | 23 |
|
22 | 24 | #include <QFileInfo>
|
| 25 | +#include <QAction> |
23 | 26 | #include <mutex>
|
| 27 | +#include <QMessageBox> |
24 | 28 |
|
25 | 29 | // defined in qgsgdalprovider.cpp
|
26 | 30 | void buildSupportedRasterFileFilterAndExtensions( QString &fileFiltersString, QStringList &extensions, QStringList &wildcards );
|
@@ -119,7 +123,59 @@ QString QgsGdalLayerItem::layerName() const
|
119 | 123 | else
|
120 | 124 | return info.completeBaseName();
|
121 | 125 | }
|
| 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 | + |
122 | 160 |
|
| 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 |
123 | 179 | // ---------------------------------------------------------------------------
|
124 | 180 |
|
125 | 181 | static QString sFilterString;
|
|
0 commit comments