|
18 | 18 | #include "qgslogger.h"
|
19 | 19 | #include "qgsmessagelog.h"
|
20 | 20 | #include "qgssettings.h"
|
| 21 | +#include "qgsproject.h" |
21 | 22 |
|
22 | 23 | #include <QFileInfo>
|
23 | 24 | #include <QTextStream>
|
@@ -131,31 +132,48 @@ void QgsOgrLayerItem::deleteLayer()
|
131 | 132 | {
|
132 | 133 | // Messages are different for files and tables
|
133 | 134 | QString title = mIsSubLayer ? QObject::tr( "Delete Layer" ) : QObject::tr( "Delete File" );
|
134 |
| - QString confirmMessage; |
135 |
| - if ( mIsSubLayer ) |
| 135 | + // Check if the layer is in the registry |
| 136 | + const QgsMapLayer *projectLayer = nullptr; |
| 137 | + Q_FOREACH ( const QgsMapLayer *layer, QgsProject::instance()->mapLayers() ) |
136 | 138 | {
|
137 |
| - confirmMessage = QObject::tr( "Are you sure you want to delete layer '%1' from datasource?" ).arg( mName ); |
138 |
| - } |
139 |
| - else |
140 |
| - { |
141 |
| - confirmMessage = QObject::tr( "Are you sure you want to delete file '%1'?" ).arg( mUri ); |
| 139 | + if ( layer->publicSource() == mUri ) |
| 140 | + { |
| 141 | + projectLayer = layer; |
| 142 | + } |
142 | 143 | }
|
143 |
| - if ( QMessageBox::question( nullptr, title, |
144 |
| - confirmMessage, |
145 |
| - QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes ) |
146 |
| - return; |
147 |
| - |
148 |
| - QString errCause; |
149 |
| - bool res = ::deleteLayer( mUri, errCause ); |
150 |
| - if ( !res ) |
| 144 | + if ( ! projectLayer ) |
151 | 145 | {
|
152 |
| - QMessageBox::warning( nullptr, title, errCause ); |
| 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 | + } |
153 | 172 | }
|
154 | 173 | else
|
155 | 174 | {
|
156 |
| - QMessageBox::information( nullptr, title, mIsSubLayer ? tr( "Layer deleted successfully." ) : tr( "File deleted successfully." ) ); |
157 |
| - if ( mParent ) |
158 |
| - mParent->refresh(); |
| 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() ) ); |
159 | 177 | }
|
160 | 178 | }
|
161 | 179 | #endif
|
|
0 commit comments