Skip to content

Commit

Permalink
Add checks for existing styles and confirmation question to app classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 24, 2022
1 parent 1872e4b commit 6a350b4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -9193,6 +9193,22 @@ void QgisApp::saveStyleFile( QgsMapLayer *layer )

QgsVectorLayerSaveStyleDialog::SaveToDbSettings dbSettings = dlg.saveToDbSettings();

QString errorMessage;
if ( QgsProviderRegistry::instance()->styleExists( vlayer->providerType(), vlayer->source(), dbSettings.name, errorMessage ) )
{
if ( QMessageBox::question( nullptr, QObject::tr( "Save style in database" ),
QObject::tr( "A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
return;
}
}
else if ( !errorMessage.isEmpty() )
{
mInfoBar->pushMessage( infoWindowTitle, errorMessage, Qgis::MessageLevel::Warning );
return;
}

vlayer->saveStyleToDatabase( dbSettings.name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError );

if ( !msgError.isNull() )
Expand Down
20 changes: 20 additions & 0 deletions src/gui/qgsmaplayerstylemanagerwidget.cpp
Expand Up @@ -31,6 +31,7 @@
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsapplication.h"
#include "qgsproviderregistry.h"

QgsMapLayerStyleManagerWidget::QgsMapLayerStyleManagerWidget( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent )
: QgsMapLayerConfigWidget( layer, canvas, parent )
Expand Down Expand Up @@ -210,12 +211,31 @@ void QgsMapLayerStyleManagerWidget::saveAsDefault()
case 0:
return;
case 2:
{
QString errorMessage;
if ( QgsProviderRegistry::instance()->styleExists( layer->providerType(), layer->source(), QString(), errorMessage ) )
{
if ( QMessageBox::question( nullptr, QObject::tr( "Save style in database" ),
QObject::tr( "A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
return;
}
}
else if ( !errorMessage.isEmpty() )
{
QMessageBox::warning( nullptr, QObject::tr( "Save style in database" ),
errorMessage );
return;
}

layer->saveStyleToDatabase( QString(), QString(), true, QString(), errorMsg );
if ( errorMsg.isNull() )
{
return;
}
break;
}
default:
break;
}
Expand Down
53 changes: 53 additions & 0 deletions src/gui/vector/qgsvectorlayerproperties.cpp
Expand Up @@ -72,6 +72,7 @@
#include "qgsvectorlayertemporalpropertieswidget.h"
#include "qgsprovidersourcewidgetproviderregistry.h"
#include "qgsprovidersourcewidget.h"
#include "qgsproviderregistry.h"

#include "layertree/qgslayertreelayer.h"
#include "qgslayertree.h"
Expand Down Expand Up @@ -1069,12 +1070,31 @@ void QgsVectorLayerProperties::saveDefaultStyle_clicked()
case 0:
return;
case 2:
{
QString errorMessage;
if ( QgsProviderRegistry::instance()->styleExists( mLayer->providerType(), mLayer->source(), QString(), errorMessage ) )
{
if ( QMessageBox::question( nullptr, QObject::tr( "Save style in database" ),
QObject::tr( "A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
return;
}
}
else if ( !errorMessage.isEmpty() )
{
QMessageBox::warning( nullptr, QObject::tr( "Save style in database" ),
errorMessage );
return;
}

mLayer->saveStyleToDatabase( QString(), QString(), true, QString(), errorMsg );
if ( errorMsg.isNull() )
{
return;
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -1237,6 +1257,22 @@ void QgsVectorLayerProperties::saveStyleAs()

QgsVectorLayerSaveStyleDialog::SaveToDbSettings dbSettings = dlg.saveToDbSettings();

QString errorMessage;
if ( QgsProviderRegistry::instance()->styleExists( mLayer->providerType(), mLayer->source(), dbSettings.name, errorMessage ) )
{
if ( QMessageBox::question( nullptr, QObject::tr( "Save style in database" ),
QObject::tr( "A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
return;
}
}
else if ( !errorMessage.isEmpty() )
{
mMessageBar->pushMessage( infoWindowTitle, errorMessage, Qgis::MessageLevel::Warning );
return;
}

mLayer->saveStyleToDatabase( dbSettings.name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError );

if ( !msgError.isNull() )
Expand Down Expand Up @@ -1350,6 +1386,23 @@ void QgsVectorLayerProperties::saveMultipleStylesAs()
i++;
}
}

QString errorMessage;
if ( QgsProviderRegistry::instance()->styleExists( mLayer->providerType(), mLayer->source(), dbSettings.name, errorMessage ) )
{
if ( QMessageBox::question( nullptr, QObject::tr( "Save style in database" ),
QObject::tr( "A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
return;
}
}
else if ( !errorMessage.isEmpty() )
{
mMessageBar->pushMessage( infoWindowTitle, errorMessage, Qgis::MessageLevel::Warning );
return;
}

mLayer->saveStyleToDatabase( name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError );

if ( !msgError.isNull() )
Expand Down

0 comments on commit 6a350b4

Please sign in to comment.