1818#include " qgslogger.h"
1919#include " qgsmessagelog.h"
2020#include " qgssettings.h"
21+ #include " qgsproject.h"
2122
2223#include < QFileInfo>
2324#include < QTextStream>
2425#include < QAction>
26+ #include < QMessageBox>
2527
2628#include < ogr_srs_api.h>
2729#include < cpl_error.h>
3133QGISEXTERN QStringList fileExtensions ();
3234QGISEXTERN QStringList wildcards ();
3335
36+ QGISEXTERN bool deleteLayer ( const QString &uri, const QString &errCause );
37+
3438
3539QgsOgrLayerItem::QgsOgrLayerItem ( QgsDataItem *parent,
36- QString name, QString path, QString uri, LayerType layerType )
40+ QString name, QString path, QString uri, LayerType layerType, bool isSubLayer )
3741 : QgsLayerItem( parent, name, path, uri, layerType, QStringLiteral( " ogr" ) )
3842{
43+ mIsSubLayer = isSubLayer;
3944 mToolTip = uri;
4045 setState ( Populated ); // children are not expected
4146
@@ -56,6 +61,7 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem *parent,
5661 }
5762}
5863
64+
5965bool QgsOgrLayerItem::setCrs ( const QgsCoordinateReferenceSystem &crs )
6066{
6167 if ( !( mCapabilities & SetCrs ) )
@@ -110,9 +116,71 @@ QString QgsOgrLayerItem::layerName() const
110116 return info.completeBaseName ();
111117}
112118
119+ #ifdef HAVE_GUI
120+ QList<QAction *> QgsOgrLayerItem::actions ()
121+ {
122+ QList<QAction *> lst;
123+ // Messages are different for files and tables
124+ QString message = mIsSubLayer ? QObject::tr ( " Delete layer '%1'..." ).arg ( mName ) : QObject::tr ( " Delete file '%1'..." ).arg ( mUri );
125+ QAction *actionDeleteLayer = new QAction ( message, this );
126+ connect ( actionDeleteLayer, &QAction::triggered, this , &QgsOgrLayerItem::deleteLayer );
127+ lst.append ( actionDeleteLayer );
128+ return lst;
129+ }
130+
131+ void QgsOgrLayerItem::deleteLayer ()
132+ {
133+ // Messages are different for files and tables
134+ QString title = mIsSubLayer ? QObject::tr ( " Delete Layer" ) : QObject::tr ( " Delete File" );
135+ // Check if the layer is in the registry
136+ const QgsMapLayer *projectLayer = nullptr ;
137+ Q_FOREACH ( const QgsMapLayer *layer, QgsProject::instance ()->mapLayers () )
138+ {
139+ if ( layer->publicSource () == mUri )
140+ {
141+ projectLayer = layer;
142+ }
143+ }
144+ if ( ! projectLayer )
145+ {
146+ QString confirmMessage;
147+ if ( mIsSubLayer )
148+ {
149+ confirmMessage = QObject::tr ( " Are you sure you want to delete layer '%1' from datasource?" ).arg ( mName );
150+ }
151+ else
152+ {
153+ confirmMessage = QObject::tr ( " Are you sure you want to delete file '%1'?" ).arg ( mUri );
154+ }
155+ if ( QMessageBox::question ( nullptr , title,
156+ confirmMessage,
157+ QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
158+ return ;
159+
160+ QString errCause;
161+ bool res = ::deleteLayer ( mUri , errCause );
162+ if ( !res )
163+ {
164+ QMessageBox::warning ( nullptr , title, errCause );
165+ }
166+ else
167+ {
168+ QMessageBox::information ( nullptr , title, mIsSubLayer ? tr ( " Layer deleted successfully." ) : tr ( " File deleted successfully." ) );
169+ if ( mParent )
170+ mParent ->refresh ();
171+ }
172+ }
173+ else
174+ {
175+ QMessageBox::warning ( nullptr , title, QObject::tr ( " The layer '%1' cannot be deleted because it is in the current project as '%2',"
176+ " remove it from the project and retry." ).arg ( mName , projectLayer->name () ) );
177+ }
178+ }
179+ #endif
180+
113181// -------
114182
115- static QgsOgrLayerItem *dataItemForLayer ( QgsDataItem *parentItem, QString name, QString path, OGRDataSourceH hDataSource, int layerId )
183+ static QgsOgrLayerItem *dataItemForLayer ( QgsDataItem *parentItem, QString name, QString path, OGRDataSourceH hDataSource, int layerId, bool isSubLayer = false )
116184{
117185 OGRLayerH hLayer = OGR_DS_GetLayer ( hDataSource, layerId );
118186 OGRFeatureDefnH hDef = OGR_L_GetLayerDefn ( hLayer );
@@ -166,7 +234,7 @@ static QgsOgrLayerItem *dataItemForLayer( QgsDataItem *parentItem, QString name,
166234
167235 QgsDebugMsgLevel ( " OGR layer uri : " + layerUri, 2 );
168236
169- return new QgsOgrLayerItem ( parentItem, name, path, layerUri, layerType );
237+ return new QgsOgrLayerItem ( parentItem, name, path, layerUri, layerType, isSubLayer );
170238}
171239
172240// ----
@@ -189,7 +257,7 @@ QVector<QgsDataItem *> QgsOgrDataCollectionItem::createChildren()
189257 children.reserve ( numLayers );
190258 for ( int i = 0 ; i < numLayers; ++i )
191259 {
192- QgsOgrLayerItem *item = dataItemForLayer ( this , QString (), mPath , hDataSource, i );
260+ QgsOgrLayerItem *item = dataItemForLayer ( this , QString (), mPath , hDataSource, i, true );
193261 children.append ( item );
194262 }
195263
0 commit comments