Skip to content

Commit

Permalink
Fix #51515 - using properties objects for load/save layer styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Djedouas committed Apr 11, 2023
1 parent 72bb868 commit 6149d5d
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 304 deletions.
28 changes: 28 additions & 0 deletions python/gui/auto_generated/mesh/qgsmeshlayerproperties.sip.in
Expand Up @@ -40,6 +40,34 @@ Constructor
Adds properties page from a factory

.. versionadded:: 3.16
%End

void loadDefaultStyle();
%Docstring
Loads the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveDefaultStyle();
%Docstring
Saves the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void loadStyle();
%Docstring
Loads a saved style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveStyleAs();
%Docstring
Saves a style when appriate button is pressed

.. versionadded:: 3.30
%End

protected slots:
Expand Down
28 changes: 28 additions & 0 deletions python/gui/auto_generated/raster/qgsrasterlayerproperties.sip.in
Expand Up @@ -49,6 +49,34 @@ Adds a properties page factory to the raster layer properties dialog.
virtual bool eventFilter( QObject *obj, QEvent *ev );


void loadDefaultStyle();
%Docstring
Loads the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveDefaultStyle();
%Docstring
Saves the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void loadStyle();
%Docstring
Loads a saved style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveStyleAs();
%Docstring
Saves a style when appriate button is pressed

.. versionadded:: 3.30
%End

protected slots:

};
Expand Down
28 changes: 28 additions & 0 deletions python/gui/auto_generated/vector/qgsvectorlayerproperties.sip.in
Expand Up @@ -30,6 +30,34 @@ Adds a properties page factory to the vector layer properties dialog.
virtual bool eventFilter( QObject *obj, QEvent *ev );


void loadDefaultStyle();
%Docstring
Loads the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveDefaultStyle();
%Docstring
Saves the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void loadStyle();
%Docstring
Loads a saved style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveStyleAs();
%Docstring
Saves a style when appriate button is pressed

.. versionadded:: 3.30
%End

protected slots:
void optionsStackedWidget_CurrentChanged( int index ) final;

Expand Down
Expand Up @@ -25,6 +25,34 @@ Vectortile layer properties dialog
QgsVectorTileLayerProperties( QgsVectorTileLayer *lyr, QgsMapCanvas *canvas, QgsMessageBar *messageBar, QWidget *parent = 0, Qt::WindowFlags = QgsGuiUtils::ModalDialogFlags );
%Docstring
Constructor
%End

void loadDefaultStyle();
%Docstring
Loads the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveDefaultStyle();
%Docstring
Saves the default style when appropriate button is pressed

.. versionadded:: 3.30
%End

void loadStyle();
%Docstring
Loads a saved style when appropriate button is pressed

.. versionadded:: 3.30
%End

void saveStyleAs();
%Docstring
Saves a style when appriate button is pressed

.. versionadded:: 3.30
%End

protected slots:
Expand Down
32 changes: 28 additions & 4 deletions src/app/pointcloud/qgspointcloudlayerproperties.h
Expand Up @@ -97,14 +97,38 @@ class QgsPointCloudLayerProperties : public QgsOptionsDialogBase, private Ui::Qg

void addPropertiesPageFactory( const QgsMapLayerConfigWidgetFactory *factory );

private slots:
void apply();
void onCancel();

/**
* Loads the default style when appropriate button is pressed
*
* \since QGIS 3.30
*/
void loadDefaultStyle();

/**
* Saves the default style when appropriate button is pressed
*
* \since QGIS 3.30
*/
void saveDefaultStyle();

/**
* Loads a saved style when appropriate button is pressed
*
* \since QGIS 3.30
*/
void loadStyle();

/**
* Saves a style when appriate button is pressed
*
* \since QGIS 3.30
*/
void saveStyleAs();

private slots:
void apply();
void onCancel();

void aboutToShowStyleMenu();
void loadMetadata();
void saveMetadataAs();
Expand Down
124 changes: 19 additions & 105 deletions src/app/qgisapp.cpp
Expand Up @@ -91,7 +91,6 @@
#include "qgsfixattributedialog.h"
#include "qgsprojecttimesettings.h"
#include "qgsgeometrycollection.h"
#include "qgsvectorlayersavestyledialog.h"
#include "maptools/qgsappmaptools.h"
#include "qgsexpressioncontextutils.h"
#include "qgsauxiliarystorage.h"
Expand Down Expand Up @@ -8231,122 +8230,37 @@ void QgisApp::saveStyleFile( QgsMapLayer *layer )
{

case Qgis::LayerType::Vector:
{
QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( layer );
QgsVectorLayerSaveStyleDialog dlg( vlayer, this );

if ( dlg.exec() )
{
bool resultFlag = false;
QString errorMessage;

QgsVectorLayerProperties::StyleType type = dlg.currentStyleType();
switch ( type )
{
case QgsVectorLayerProperties::QML:
case QgsVectorLayerProperties::SLD:
{
QString filePath = dlg.outputFilePath();
if ( type == QgsVectorLayerProperties::QML )
errorMessage = vlayer->saveNamedStyle( filePath, resultFlag, dlg.styleCategories() );
else
errorMessage = vlayer->saveSldStyle( filePath, resultFlag );

if ( resultFlag )
{
mInfoBar->pushMessage( tr( "Style saved" ), tr( "Successfully exported style to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( filePath ).toString(), QDir::toNativeSeparators( filePath ) ), Qgis::MessageLevel::Success, 0 );
}
else
{
mInfoBar->pushMessage( tr( "Save Style" ), errorMessage, Qgis::MessageLevel::Warning );
}

break;
}
case QgsVectorLayerProperties::DB:
{
QString infoWindowTitle = tr( "Save style to DB (%1)" ).arg( vlayer->providerType() );
QString msgError;

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

if ( QgsProviderRegistry::instance()->styleExists( vlayer->providerType(), vlayer->source(), dbSettings.name, errorMessage ) )
{
if ( QMessageBox::question( nullptr, tr( "Save style in database" ),
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, dlg.styleCategories() );

if ( !msgError.isNull() )
{
mInfoBar->pushMessage( infoWindowTitle, msgError, Qgis::MessageLevel::Warning );
}
else
{
mInfoBar->pushMessage( infoWindowTitle, tr( "Style saved" ), Qgis::MessageLevel::Success );
}
break;
}
case QgsVectorLayerProperties::Local:
{
const QString infoWindowTitle = tr( "Save default style to local database" );
errorMessage = vlayer->saveDefaultStyle( resultFlag, dlg.styleCategories() );
if ( !resultFlag )
{
mInfoBar->pushMessage( infoWindowTitle, errorMessage, Qgis::MessageLevel::Warning );
}
else
{
mInfoBar->pushMessage( infoWindowTitle, tr( "Style saved" ), Qgis::MessageLevel::Success );
}
break;
}
}
}
QgsVectorLayerProperties( mMapCanvas,
visibleMessageBar(),
qobject_cast<QgsVectorLayer *>( layer ) ).saveStyleAs();
break;
}

case Qgis::LayerType::Raster:
case Qgis::LayerType::Mesh:
case Qgis::LayerType::PointCloud:
case Qgis::LayerType::VectorTile:
{
QgsSettings settings;
QString lastUsedDir = settings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();
QString filename = QFileDialog::getSaveFileName( this,
tr( "Save as QGIS Layer Style File" ),
lastUsedDir,
tr( "QGIS Layer Style File" ) + " (*.qml)" );
if ( filename.isEmpty() )
return;
QgsRasterLayerProperties( layer, mMapCanvas ).saveStyleAs();
break;

if ( ! filename.endsWith( QLatin1String( ".qml" ) ) )
{
filename += QLatin1String( ".qml" );
}
case Qgis::LayerType::Mesh:
QgsMeshLayerProperties( layer, mMapCanvas ).saveStyleAs();
break;

bool defaultLoadedFlag;
layer->saveNamedStyle( filename, defaultLoadedFlag );
case Qgis::LayerType::VectorTile:
QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( layer ),
mMapCanvas,
visibleMessageBar() ).saveStyleAs();
break;

settings.setValue( QStringLiteral( "style/lastStyleDir" ), filename );
case Qgis::LayerType::PointCloud:
QgsPointCloudLayerProperties( qobject_cast<QgsPointCloudLayer *>( layer ),
mMapCanvas,
visibleMessageBar() ).saveStyleAs();
break;
}

// Not available for these
case Qgis::LayerType::Annotation:
case Qgis::LayerType::Plugin:
case Qgis::LayerType::Group:
default:
break;

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgslayerstylingwidget.cpp
Expand Up @@ -901,9 +901,9 @@ bool QgsLayerStyleManagerWidgetFactory::supportsLayer( QgsMapLayer *layer ) cons
case Qgis::LayerType::Vector:
case Qgis::LayerType::Raster:
case Qgis::LayerType::Mesh:
case Qgis::LayerType::VectorTile:
return true;

case Qgis::LayerType::VectorTile:
case Qgis::LayerType::PointCloud:
case Qgis::LayerType::Plugin:
case Qgis::LayerType::Annotation:
Expand Down
36 changes: 28 additions & 8 deletions src/gui/mesh/qgsmeshlayerproperties.h
Expand Up @@ -65,6 +65,34 @@ class GUI_EXPORT QgsMeshLayerProperties : public QgsOptionsDialogBase, private U
*/
void addPropertiesPageFactory( const QgsMapLayerConfigWidgetFactory *factory );

/**
* Loads the default style when appropriate button is pressed
*
* \since QGIS 3.30
*/
void loadDefaultStyle();

/**
* Saves the default style when appropriate button is pressed
*
* \since QGIS 3.30
*/
void saveDefaultStyle();

/**
* Loads a saved style when appropriate button is pressed
*
* \since QGIS 3.30
*/
void loadStyle();

/**
* Saves a style when appriate button is pressed
*
* \since QGIS 3.30
*/
void saveStyleAs();

protected slots:
void optionsStackedWidget_CurrentChanged( int index ) override SIP_SKIP ;

Expand All @@ -78,14 +106,6 @@ class GUI_EXPORT QgsMeshLayerProperties : public QgsOptionsDialogBase, private U
void syncAndRepaint();
//! Changes layer coordinate reference system
void changeCrs( const QgsCoordinateReferenceSystem &crs );
//! Loads the default style when appropriate button is pressed
void loadDefaultStyle();
//! Saves the default style when appropriate button is pressed
void saveDefaultStyle();
//! Loads a saved style when appropriate button is pressed
void loadStyle();
//! Saves a style when appriate button is pressed
void saveStyleAs();
//! Prepares style menu
void aboutToShowStyleMenu();
//! Reloads temporal properties from the provider
Expand Down

0 comments on commit 6149d5d

Please sign in to comment.