Skip to content

Commit

Permalink
Extend temporary layer warning to include layers stored inside
Browse files Browse the repository at this point in the history
a user's temporary folder, e.g. the "/tmp" folder on Linux

This can lead to irretrievable data loss.

Fixes #32582

(cherry picked from commit cc80bb4)
  • Loading branch information
nyalldawson committed Nov 11, 2019
1 parent ab80e6d commit fcf78fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
18 changes: 16 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -11785,7 +11785,8 @@ bool QgisApp::checkMemoryLayers()
if ( !QgsSettings().value( QStringLiteral( "askToSaveMemoryLayers" ), true, QgsSettings::App ).toBool() )
return true;

// check to see if there are any memory layers present (with features)
// check to see if there are any temporary layers present (with features)
bool hasTemporaryLayers = false;
bool hasMemoryLayers = false;
const QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
for ( auto it = layers.begin(); it != layers.end(); ++it )
Expand All @@ -11799,10 +11800,23 @@ bool QgisApp::checkMemoryLayers()
break;
}
}
else if ( it.value() && it.value()->isTemporary() )
hasTemporaryLayers = true;
}

if ( hasMemoryLayers )
if ( hasTemporaryLayers )
{
if ( QMessageBox::warning( this,
tr( "Close Project" ),
tr( "This project includes one or more temporary layers. These layers are not permanently saved and their contents will be lost. Are you sure you want to proceed?" ),
QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) == QMessageBox::Yes )
return true;
else
return false;
}
else if ( hasMemoryLayers )
{
// use the more specific warning for memory layers
if ( QMessageBox::warning( this,
tr( "Close Project" ),
tr( "This project includes one or more temporary scratch layers. These layers are not saved to disk and their contents will be permanently lost. Are you sure you want to proceed?" ),
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -298,7 +298,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()

if ( vlayer )
{
if ( vlayer->providerType() == QLatin1String( "memory" ) )
if ( vlayer->isTemporary() )
{
QAction *actionMakePermanent = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileSave.svg" ) ), tr( "Make Permanent…" ), menu );
connect( actionMakePermanent, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->makeMemoryLayerPermanent( vlayer ); } );
Expand Down
12 changes: 7 additions & 5 deletions src/app/qgslayertreeviewmemoryindicator.cpp
Expand Up @@ -42,10 +42,10 @@ void QgsLayerTreeViewMemoryIndicatorProvider::onIndicatorClicked( const QModelIn

bool QgsLayerTreeViewMemoryIndicatorProvider::acceptLayer( QgsMapLayer *layer )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !( vlayer && vlayer->isValid() ) )
if ( !layer )
return false;
return vlayer->providerType() == QLatin1String( "memory" );

return layer->isTemporary();
}

QString QgsLayerTreeViewMemoryIndicatorProvider::iconName( QgsMapLayer *layer )
Expand All @@ -56,7 +56,9 @@ QString QgsLayerTreeViewMemoryIndicatorProvider::iconName( QgsMapLayer *layer )

QString QgsLayerTreeViewMemoryIndicatorProvider::tooltipText( QgsMapLayer *layer )
{
Q_UNUSED( layer )
return tr( "<b>Temporary scratch layer only!</b><br>Contents will be discarded after closing this project" );
if ( layer->providerType() == QLatin1String( "memory" ) )
return tr( "<b>Temporary scratch layer only!</b><br>Contents will be discarded after closing this project" );

return tr( "<b>Temporary layer only!</b><br>Contents will be discarded after closing QGIS." );
}

0 comments on commit fcf78fb

Please sign in to comment.