From c31364f15a3a20fec87f44e0b4ec0c408da2b7d4 Mon Sep 17 00:00:00 2001 From: Jacky Volpes Date: Fri, 29 Apr 2022 15:58:47 +0200 Subject: [PATCH] Enable category selection when saving style to database --- python/core/auto_generated/qgsmaplayer.sip.in | 18 ++- .../vector/qgsvectorlayer.sip.in | 5 +- .../qgsannotationlayerproperties.cpp | 4 + .../qgspointcloudlayerproperties.cpp | 4 + src/app/qgisapp.cpp | 11 +- .../qgsvectortilelayerproperties.cpp | 4 + src/core/qgsmaplayer.cpp | 7 +- src/core/qgsmaplayer.h | 15 +- src/core/vector/qgsvectorlayer.cpp | 4 +- src/core/vector/qgsvectorlayer.h | 4 +- src/gui/mesh/qgsmeshlayerproperties.cpp | 4 + src/gui/qgsmaplayerstylemanagerwidget.cpp | 4 + src/gui/raster/qgsrasterlayerproperties.cpp | 4 + src/gui/vector/qgsvectorlayerproperties.cpp | 31 ++-- .../vector/qgsvectorlayersavestyledialog.cpp | 5 +- src/ui/qgsvectorlayersavestyledialog.ui | 153 ++++++++---------- 16 files changed, 158 insertions(+), 119 deletions(-) diff --git a/python/core/auto_generated/qgsmaplayer.sip.in b/python/core/auto_generated/qgsmaplayer.sip.in index 83bd6e25cf8..f4d9a94f833 100644 --- a/python/core/auto_generated/qgsmaplayer.sip.in +++ b/python/core/auto_generated/qgsmaplayer.sip.in @@ -981,7 +981,21 @@ Export the properties of this layer as SLD style in a QDomDocument during the execution of writeSymbology %End - virtual QString saveDefaultStyle( bool &resultFlag /Out/ ); + virtual QString saveDefaultStyle( bool &resultFlag /Out/, StyleCategories categories ); +%Docstring +Save the properties of this layer as the default style +(either as a .qml file on disk or as a +record in the users style table in their personal qgis.db) + +:param categories: the style categories to be saved (since QGIS 3.26) + +:return: - a QString with any status messages + - resultFlag: a reference to a flag that will be set to ``False`` if we did not manage to save the default style. + +.. seealso:: :py:func:`loadNamedStyle` +%End + + virtual QString saveDefaultStyle( bool &resultFlag /Out/ ) /Deprecated/; %Docstring Save the properties of this layer as the default style (either as a .qml file on disk or as a @@ -992,6 +1006,8 @@ record in the users style table in their personal qgis.db) - resultFlag: a reference to a flag that will be set to ``False`` if we did not manage to save the default style. .. seealso:: :py:func:`loadNamedStyle` + +.. deprecated:: QGIS 3.26 %End virtual QString saveNamedStyle( const QString &uri, bool &resultFlag /Out/, StyleCategories categories = AllStyleCategories ); diff --git a/python/core/auto_generated/vector/qgsvectorlayer.sip.in b/python/core/auto_generated/vector/qgsvectorlayer.sip.in index 8fccc0fa5bf..180d1e9dcc1 100644 --- a/python/core/auto_generated/vector/qgsvectorlayer.sip.in +++ b/python/core/auto_generated/vector/qgsvectorlayer.sip.in @@ -932,7 +932,8 @@ Resolves references to other layers (kept as layer IDs after reading XML) into l virtual void saveStyleToDatabase( const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, - QString &msgError /Out/ ); + QString &msgError /Out/, + QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ); %Docstring Saves named and sld style of the layer to the style table in the db. @@ -940,6 +941,8 @@ Saves named and sld style of the layer to the style table in the db. :param description: A description of the style :param useAsDefault: Set to ``True`` if style should be used as the default style for the layer :param uiFileContent: +:param msgError: will be set to a descriptive error message if any occurs +:param categories: the style categories to be saved. .. note:: diff --git a/src/app/annotations/qgsannotationlayerproperties.cpp b/src/app/annotations/qgsannotationlayerproperties.cpp index b58d0b974cf..74fd614ad94 100644 --- a/src/app/annotations/qgsannotationlayerproperties.cpp +++ b/src/app/annotations/qgsannotationlayerproperties.cpp @@ -213,9 +213,13 @@ void QgsAnnotationLayerProperties::saveDefaultStyle() // a flag passed by reference bool defaultSavedFlag = false; + // One the deprecated `saveDefaultStyle()` method is gone, just + // remove the NOWARN_DEPRECATED tags + Q_NOWARN_DEPRECATED_PUSH // after calling this the above flag will be set true for success // or false if the save operation failed const QString myMessage = mLayer->saveDefaultStyle( defaultSavedFlag ); + Q_NOWARN_DEPRECATED_POP if ( !defaultSavedFlag ) { // let the user know what went wrong diff --git a/src/app/pointcloud/qgspointcloudlayerproperties.cpp b/src/app/pointcloud/qgspointcloudlayerproperties.cpp index 678373d6c7d..cf0dde618f3 100644 --- a/src/app/pointcloud/qgspointcloudlayerproperties.cpp +++ b/src/app/pointcloud/qgspointcloudlayerproperties.cpp @@ -243,9 +243,13 @@ void QgsPointCloudLayerProperties::saveDefaultStyle() // a flag passed by reference bool defaultSavedFlag = false; + // One the deprecated `saveDefaultStyle()` method is gone, just + // remove the NOWARN_DEPRECATED tags + Q_NOWARN_DEPRECATED_PUSH // after calling this the above flag will be set true for success // or false if the save operation failed const QString myMessage = mLayer->saveDefaultStyle( defaultSavedFlag ); + Q_NOWARN_DEPRECATED_POP if ( !defaultSavedFlag ) { // let the user know what went wrong diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index b13dfa7f44c..b74b38f0819 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -9093,6 +9093,7 @@ void QgisApp::saveStyleFile( QgsMapLayer *layer ) if ( dlg.exec() ) { bool resultFlag = false; + QString errorMessage; QgsVectorLayerProperties::StyleType type = dlg.currentStyleType(); switch ( type ) @@ -9100,12 +9101,11 @@ void QgisApp::saveStyleFile( QgsMapLayer *layer ) case QgsVectorLayerProperties::QML: case QgsVectorLayerProperties::SLD: { - QString message; QString filePath = dlg.outputFilePath(); if ( type == QgsVectorLayerProperties::QML ) - message = vlayer->saveNamedStyle( filePath, resultFlag, dlg.styleCategories() ); + errorMessage = vlayer->saveNamedStyle( filePath, resultFlag, dlg.styleCategories() ); else - message = vlayer->saveSldStyle( filePath, resultFlag ); + errorMessage = vlayer->saveSldStyle( filePath, resultFlag ); if ( resultFlag ) { @@ -9113,7 +9113,7 @@ void QgisApp::saveStyleFile( QgsMapLayer *layer ) } else { - mInfoBar->pushMessage( tr( "Save Style" ), message, Qgis::MessageLevel::Warning ); + mInfoBar->pushMessage( tr( "Save Style" ), errorMessage, Qgis::MessageLevel::Warning ); } break; @@ -9125,7 +9125,6 @@ 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, tr( "Save style in database" ), @@ -9141,7 +9140,7 @@ void QgisApp::saveStyleFile( QgsMapLayer *layer ) return; } - vlayer->saveStyleToDatabase( dbSettings.name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError ); + vlayer->saveStyleToDatabase( dbSettings.name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError, dlg.styleCategories() ); if ( !msgError.isNull() ) { diff --git a/src/app/vectortile/qgsvectortilelayerproperties.cpp b/src/app/vectortile/qgsvectortilelayerproperties.cpp index 3adaef6e40e..093e5c04226 100644 --- a/src/app/vectortile/qgsvectortilelayerproperties.cpp +++ b/src/app/vectortile/qgsvectortilelayerproperties.cpp @@ -193,9 +193,13 @@ void QgsVectorTileLayerProperties::saveDefaultStyle() // a flag passed by reference bool defaultSavedFlag = false; + // One the deprecated `saveDefaultStyle()` method is gone, just + // remove the NOWARN_DEPRECATED tags + Q_NOWARN_DEPRECATED_PUSH // after calling this the above flag will be set true for success // or false if the save operation failed const QString myMessage = mLayer->saveDefaultStyle( defaultSavedFlag ); + Q_NOWARN_DEPRECATED_POP if ( !defaultSavedFlag ) { // let the user know what went wrong diff --git a/src/core/qgsmaplayer.cpp b/src/core/qgsmaplayer.cpp index 6672dfb31be..2e125ef0d1a 100644 --- a/src/core/qgsmaplayer.cpp +++ b/src/core/qgsmaplayer.cpp @@ -1322,7 +1322,12 @@ void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, const QString QgsMapLayer::saveDefaultStyle( bool &resultFlag ) { - return saveNamedStyle( styleURI(), resultFlag ); + return saveDefaultStyle( resultFlag, AllStyleCategories ); +} + +QString QgsMapLayer::saveDefaultStyle( bool &resultFlag, StyleCategories categories ) +{ + return saveNamedStyle( styleURI(), resultFlag, categories ); } QString QgsMapLayer::saveNamedMetadata( const QString &uri, bool &resultFlag ) diff --git a/src/core/qgsmaplayer.h b/src/core/qgsmaplayer.h index cb1c91b6b75..66e13abda31 100644 --- a/src/core/qgsmaplayer.h +++ b/src/core/qgsmaplayer.h @@ -1102,10 +1102,23 @@ class CORE_EXPORT QgsMapLayer : public QObject * record in the users style table in their personal qgis.db) * \param resultFlag a reference to a flag that will be set to FALSE if * we did not manage to save the default style. + * \param categories the style categories to be saved (since QGIS 3.26) * \returns a QString with any status messages * \see loadNamedStyle() and \see saveNamedStyle() */ - virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT ); + virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT, StyleCategories categories ); + + /** + * Save the properties of this layer as the default style + * (either as a .qml file on disk or as a + * record in the users style table in their personal qgis.db) + * \param resultFlag a reference to a flag that will be set to FALSE if + * we did not manage to save the default style. + * \returns a QString with any status messages + * \see loadNamedStyle() and \see saveNamedStyle() + * \deprecated since QGIS 3.26 + */ + Q_DECL_DEPRECATED virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT ) SIP_DEPRECATED; /** * Save the properties of this layer as a named style diff --git a/src/core/vector/qgsvectorlayer.cpp b/src/core/vector/qgsvectorlayer.cpp index 2f72c7bd07b..a69139fac6c 100644 --- a/src/core/vector/qgsvectorlayer.cpp +++ b/src/core/vector/qgsvectorlayer.cpp @@ -5442,13 +5442,13 @@ bool QgsVectorLayer::deleteStyleFromDatabase( const QString &styleId, QString &m void QgsVectorLayer::saveStyleToDatabase( const QString &name, const QString &description, - bool useAsDefault, const QString &uiFileContent, QString &msgError ) + bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories ) { QString sldStyle, qmlStyle; QDomDocument qmlDocument, sldDocument; QgsReadWriteContext context; - exportNamedStyle( qmlDocument, msgError, context ); + exportNamedStyle( qmlDocument, msgError, context, categories ); if ( !msgError.isNull() ) { return; diff --git a/src/core/vector/qgsvectorlayer.h b/src/core/vector/qgsvectorlayer.h index f614b5510df..4bcd51b0aca 100644 --- a/src/core/vector/qgsvectorlayer.h +++ b/src/core/vector/qgsvectorlayer.h @@ -987,6 +987,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte * \param useAsDefault Set to TRUE if style should be used as the default style for the layer * \param uiFileContent * \param msgError will be set to a descriptive error message if any occurs + * \param categories the style categories to be saved. * * * \note Prior to QGIS 3.24, this method would show a message box warning when a @@ -997,7 +998,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte */ virtual void saveStyleToDatabase( const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, - QString &msgError SIP_OUT ); + QString &msgError SIP_OUT, + QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ); /** * Lists all the style in db split into related to the layer and not related to diff --git a/src/gui/mesh/qgsmeshlayerproperties.cpp b/src/gui/mesh/qgsmeshlayerproperties.cpp index 505ad20b8cc..74d4dd232b9 100644 --- a/src/gui/mesh/qgsmeshlayerproperties.cpp +++ b/src/gui/mesh/qgsmeshlayerproperties.cpp @@ -268,9 +268,13 @@ void QgsMeshLayerProperties::saveDefaultStyle() // a flag passed by reference bool defaultSavedFlag = false; + // One the deprecated `saveDefaultStyle()` method is gone, just + // remove the NOWARN_DEPRECATED tags + Q_NOWARN_DEPRECATED_PUSH // after calling this the above flag will be set true for success // or false if the save operation failed QString myMessage = mMeshLayer->saveDefaultStyle( defaultSavedFlag ); + Q_NOWARN_DEPRECATED_POP if ( !defaultSavedFlag ) { // let the user know what went wrong diff --git a/src/gui/qgsmaplayerstylemanagerwidget.cpp b/src/gui/qgsmaplayerstylemanagerwidget.cpp index 97c9726b1da..9e17b2fae33 100644 --- a/src/gui/qgsmaplayerstylemanagerwidget.cpp +++ b/src/gui/qgsmaplayerstylemanagerwidget.cpp @@ -243,7 +243,11 @@ void QgsMapLayerStyleManagerWidget::saveAsDefault() } bool defaultSavedFlag = false; + // One the deprecated `saveDefaultStyle()` method is gone, just + // remove the NOWARN_DEPRECATED tags + Q_NOWARN_DEPRECATED_PUSH errorMsg = mLayer->saveDefaultStyle( defaultSavedFlag ); + Q_NOWARN_DEPRECATED_POP if ( !defaultSavedFlag ) { QMessageBox::warning( this, tr( "Default Style" ), errorMsg ); diff --git a/src/gui/raster/qgsrasterlayerproperties.cpp b/src/gui/raster/qgsrasterlayerproperties.cpp index 183258f438c..dd07e342938 100644 --- a/src/gui/raster/qgsrasterlayerproperties.cpp +++ b/src/gui/raster/qgsrasterlayerproperties.cpp @@ -1553,9 +1553,13 @@ void QgsRasterLayerProperties::saveDefaultStyle_clicked() // a flag passed by reference bool defaultSavedFlag = false; + // One the deprecated `saveDefaultStyle()` method is gone, just + // remove the NOWARN_DEPRECATED tags + Q_NOWARN_DEPRECATED_PUSH // after calling this the above flag will be set true for success // or false if the save operation failed QString myMessage = mRasterLayer->saveDefaultStyle( defaultSavedFlag ); + Q_NOWARN_DEPRECATED_POP if ( !defaultSavedFlag ) { //let the user know what went wrong diff --git a/src/gui/vector/qgsvectorlayerproperties.cpp b/src/gui/vector/qgsvectorlayerproperties.cpp index fa788faa8bf..cd7cc8f4d80 100644 --- a/src/gui/vector/qgsvectorlayerproperties.cpp +++ b/src/gui/vector/qgsvectorlayerproperties.cpp @@ -1091,7 +1091,11 @@ void QgsVectorLayerProperties::saveDefaultStyle_clicked() } bool defaultSavedFlag = false; + // One the deprecated `saveDefaultStyle()` method is gone, just + // remove the NOWARN_DEPRECATED tags + Q_NOWARN_DEPRECATED_PUSH errorMsg = mLayer->saveDefaultStyle( defaultSavedFlag ); + Q_NOWARN_DEPRECATED_POP if ( !defaultSavedFlag ) { QMessageBox::warning( this, tr( "Default Style" ), errorMsg ); @@ -1213,6 +1217,7 @@ void QgsVectorLayerProperties::saveStyleAs() apply(); bool defaultLoadedFlag = false; + QString errorMessage; StyleType type = dlg.currentStyleType(); switch ( type ) @@ -1220,12 +1225,11 @@ void QgsVectorLayerProperties::saveStyleAs() case QML: case SLD: { - QString message; QString filePath = dlg.outputFilePath(); if ( type == QML ) - message = mLayer->saveNamedStyle( filePath, defaultLoadedFlag, dlg.styleCategories() ); + errorMessage = mLayer->saveNamedStyle( filePath, defaultLoadedFlag, dlg.styleCategories() ); else - message = mLayer->saveSldStyle( filePath, defaultLoadedFlag ); + errorMessage = mLayer->saveSldStyle( filePath, defaultLoadedFlag ); //reset if the default style was loaded OK only if ( defaultLoadedFlag ) @@ -1235,7 +1239,7 @@ void QgsVectorLayerProperties::saveStyleAs() else { //let the user know what went wrong - QMessageBox::information( this, tr( "Save Style" ), message ); + QMessageBox::information( this, tr( "Save Style" ), errorMessage ); } break; @@ -1243,11 +1247,9 @@ void QgsVectorLayerProperties::saveStyleAs() case DB: { QString infoWindowTitle = QObject::tr( "Save style to DB (%1)" ).arg( mLayer->providerType() ); - QString msgError; 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" ), @@ -1263,11 +1265,11 @@ void QgsVectorLayerProperties::saveStyleAs() return; } - mLayer->saveStyleToDatabase( dbSettings.name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError ); + mLayer->saveStyleToDatabase( dbSettings.name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, errorMessage, dlg.styleCategories() ); - if ( !msgError.isNull() ) + if ( !errorMessage.isNull() ) { - mMessageBar->pushMessage( infoWindowTitle, msgError, Qgis::MessageLevel::Warning ); + mMessageBar->pushMessage( infoWindowTitle, errorMessage, Qgis::MessageLevel::Warning ); } else { @@ -1393,7 +1395,7 @@ void QgsVectorLayerProperties::saveMultipleStylesAs() return; } - mLayer->saveStyleToDatabase( name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError ); + mLayer->saveStyleToDatabase( name, dbSettings.description, dbSettings.isDefault, dbSettings.uiFileContent, msgError, dlg.styleCategories() ); if ( !msgError.isNull() ) { @@ -1461,21 +1463,20 @@ void QgsVectorLayerProperties::loadStyle() mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() ); QgsMapLayer::StyleCategories categories = dlg.styleCategories(); StyleType type = dlg.currentStyleType(); + bool defaultLoadedFlag = false; switch ( type ) { case QML: case SLD: { - QString message; - bool defaultLoadedFlag = false; QString filePath = dlg.filePath(); if ( type == SLD ) { - message = mLayer->loadSldStyle( filePath, defaultLoadedFlag ); + errorMsg = mLayer->loadSldStyle( filePath, defaultLoadedFlag ); } else { - message = mLayer->loadNamedStyle( filePath, defaultLoadedFlag, true, categories ); + errorMsg = mLayer->loadNamedStyle( filePath, defaultLoadedFlag, true, categories ); } //reset if the default style was loaded OK only if ( defaultLoadedFlag ) @@ -1485,7 +1486,7 @@ void QgsVectorLayerProperties::loadStyle() else { //let the user know what went wrong - QMessageBox::warning( this, tr( "Load Style" ), message ); + QMessageBox::warning( this, tr( "Load Style" ), errorMsg ); } break; } diff --git a/src/gui/vector/qgsvectorlayersavestyledialog.cpp b/src/gui/vector/qgsvectorlayersavestyledialog.cpp index 51333a0d748..2b5ccc5de5d 100644 --- a/src/gui/vector/qgsvectorlayersavestyledialog.cpp +++ b/src/gui/vector/qgsvectorlayersavestyledialog.cpp @@ -46,9 +46,10 @@ QgsVectorLayerSaveStyleDialog::QgsVectorLayerSaveStyleDialog( QgsVectorLayer *la connect( mStyleTypeComboBox, qOverload( &QComboBox::currentIndexChanged ), this, [ = ]( int ) { const QgsVectorLayerProperties::StyleType type = currentStyleType(); - mSaveToFileWidget->setVisible( type != QgsVectorLayerProperties::DB ); + mFileLabel->setVisible( type != QgsVectorLayerProperties::DB ); + mFileWidget->setVisible( type != QgsVectorLayerProperties::DB ); mSaveToDbWidget->setVisible( type == QgsVectorLayerProperties::DB ); - mStyleCategoriesListView->setEnabled( type == QgsVectorLayerProperties::QML ); + mStyleCategoriesListView->setEnabled( type != QgsVectorLayerProperties::SLD ); mFileWidget->setFilter( type == QgsVectorLayerProperties::QML ? tr( "QGIS Layer Style File (*.qml)" ) : tr( "SLD File (*.sld)" ) ); updateSaveButtonState(); } ); diff --git a/src/ui/qgsvectorlayersavestyledialog.ui b/src/ui/qgsvectorlayersavestyledialog.ui index 743c87ec57a..e3bc4b467fa 100644 --- a/src/ui/qgsvectorlayersavestyledialog.ui +++ b/src/ui/qgsvectorlayersavestyledialog.ui @@ -7,7 +7,7 @@ 0 0 491 - 535 + 614 @@ -21,6 +21,36 @@ + + + + + 0 + 0 + + + + + + + + Styles + + + + + + + + + + File + + + + + + @@ -36,6 +66,20 @@ 0 + + + + Optionally pick an input form for attribute editing (QT Designer UI format), it will be stored in the database + + + + + + + Use as default style for this layer + + + @@ -46,9 +90,6 @@ - - - @@ -69,85 +110,30 @@ - - - - Use as default style for this layer - - - - - - - Optionally pick an input form for attribute editing (QT Designer UI format), it will be stored in the database - - - - - - - - - - - 0 - 0 - - - - - - - - - 0 - 0 - - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Categories - - - - - - - File - - - - - - - - 0 - 0 - - - - - + - + + + + Categories + + + + + + + + 0 + 0 + + + + + Qt::Horizontal @@ -157,16 +143,6 @@ - - - - - - - Styles - - - @@ -180,7 +156,6 @@ mStyleTypeComboBox mStylesWidget - mStyleCategoriesListView mDbStyleNameEdit mDbStyleDescriptionEdit mDbStyleUseAsDefault