diff --git a/veloroutes_voies_vertes/processing/chargement/load_layers.py b/veloroutes_voies_vertes/processing/chargement/load_layers.py index 9e47be74..39c6b322 100644 --- a/veloroutes_voies_vertes/processing/chargement/load_layers.py +++ b/veloroutes_voies_vertes/processing/chargement/load_layers.py @@ -12,20 +12,24 @@ QgsVectorLayer, QgsRasterLayer ) + from processing.tools.postgis import uri_from_name from ...qgis_plugin_tools.tools.algorithm_processing import BaseProcessingAlgorithm from ...qgis_plugin_tools.tools.i18n import tr +from ...qgis_plugin_tools.tools.resources import resources_path class LoadLayersAlgorithm(BaseProcessingAlgorithm): """ - Chargement des couches adresse depuis la base de données + Chargement des couches depuis la base de données """ DATABASE = "DATABASE" SCHEMA = "SCHEMA" RASTER = "RASTER" + STYLE = "STYLE" + SAVE_STYLE = "SAVE_STYLE" OUTPUT = "OUTPUT" OUTPUT_MSG = "OUTPUT MSG" @@ -83,6 +87,24 @@ def initAlgorithm(self, config): ) ) + self.addParameter( + QgsProcessingParameterBoolean( + self.STYLE, + tr("Ajouter les styles aux couches?"), + defaultValue=False, + optional=False, + ) + ) + + self.addParameter( + QgsProcessingParameterBoolean( + self.SAVE_STYLE, + tr("Enregistrer les styles en base?"), + defaultValue=False, + optional=False, + ) + ) + # OUTPUTS output = QgsProcessingOutputMultipleLayers(self.OUTPUT, tr("Couches de sortie")) output.tooltip_3liz = 'Les différentes couches de l\'extention véloroutes et voies vertes' @@ -168,12 +190,24 @@ def processAlgorithm(self, parameters, context, feedback): output_layers.append(result.id()) # add attribute tables + style = self.parameterAsBool(parameters, self.STYLE, context) + save_style = self.parameterAsBool(parameters, self.SAVE_STYLE, context) for x in tables_name: if not context.project().mapLayersByName(x): result = self.initLayer(context, uri, schema, x, None, "") if not result: feedback.pushInfo("La couche " + x + " ne peut pas être chargée") else: + msg_style = "" output_layers.append(result.id()) + if style and save_style: + msg_style = "Style for " + x + " successfully loaded and saved in database" + result.saveStyleToDatabase(result.name(), 'default description', True, '') + elif style and not save_style: + result.loadNamedStyle(resources_path("qml", x + ".qml")) + msg_style = "Style for " + x + " successfully loaded" + elif not style and save_style: + msg_style = "Style for can't sved in database if not loaded" + feedback.pushInfo(msg_style) return {self.OUTPUT_MSG: msg, self.OUTPUT: output_layers} diff --git a/veloroutes_voies_vertes/processing/chargement/load_styles.py b/veloroutes_voies_vertes/processing/chargement/load_styles.py index b18ac655..561908ae 100644 --- a/veloroutes_voies_vertes/processing/chargement/load_styles.py +++ b/veloroutes_voies_vertes/processing/chargement/load_styles.py @@ -17,7 +17,7 @@ class LoadStylesAlgorithm(BaseProcessingAlgorithm): """ - Chargement des couches adresse depuis la base de données + Chargement des styles depuis le dossier resources """ INPUT = "INPUT" @@ -115,3 +115,15 @@ def processAlgorithm(self, parameters, context, feedback): msg = msg + " // Style for " + x + " successfully loaded" return {self.OUTPUT_MSG: msg} + + def postProcessAlgorithm(self, context, feedback): + + if context.project(): + + for lay in context.project().mapLayers().values(): + + # reload provider's data + lay.dataProvider().reloadData() + lay.triggerRepaint() + + return {} diff --git a/veloroutes_voies_vertes/processing/config/save_style_database.py b/veloroutes_voies_vertes/processing/config/save_style_database.py index 75b6e74a..58a2967e 100644 --- a/veloroutes_voies_vertes/processing/config/save_style_database.py +++ b/veloroutes_voies_vertes/processing/config/save_style_database.py @@ -7,18 +7,18 @@ __revision__ = '$Format:%H$' from qgis.core import ( - QgsProcessingParameterString, + QgsProcessingParameterBoolean, QgsProcessingOutputString, - QgsProcessingOutputNumber, - QgsExpressionContextUtils + QgsProject, ) from ...qgis_plugin_tools.tools.algorithm_processing import BaseProcessingAlgorithm -class ConfigurePlugin(BaseProcessingAlgorithm): +class SaveStyleDatabase(BaseProcessingAlgorithm): CONFIRM_SAVE = 'CONFIRM_SAVE' + OUTPUT_STRING = 'OUTPUT_STRING' def name(self): return 'save_style_database' @@ -42,7 +42,9 @@ def initAlgorithm(self, config): """ # INPUTS - #Boolean to confirm save + # Boolean to confirm save + input_bool = QgsProcessingParameterBoolean(self.CONFIRM_SAVE, 'Confirmez la sauvegarde des styles') + self.addParameter(input_bool) # OUTPUTS @@ -54,4 +56,23 @@ def initAlgorithm(self, config): def processAlgorithm(self, parameters, context, feedback): """ Here is where the processing itself takes place. - """ \ No newline at end of file + """ + comfirm = self.parameterAsBool(parameters, self.CONFIRM_SAVE, context) + + if not comfirm: + return { + self.OUTPUT_STRING: 'les styles n\'ont pas été sauvegardés' + } + + layers = QgsProject.instance().mapLayers() + if len(layers) < 1: + return { + self.OUTPUT_STRING: 'Aucune couche n\'a été trouvés' + } + for lay in layers: + layer = QgsProject.instance().mapLayer(lay) + layer.saveStyleToDatabase(layer.name(), 'default description', True, '') + + return { + self.OUTPUT_STRING: 'les styles ont été sauvegardés' + } diff --git a/veloroutes_voies_vertes/processing/provider.py b/veloroutes_voies_vertes/processing/provider.py index fda5c63e..75c0c570 100644 --- a/veloroutes_voies_vertes/processing/provider.py +++ b/veloroutes_voies_vertes/processing/provider.py @@ -11,6 +11,7 @@ from .chargement.load_styles import LoadStylesAlgorithm from ..qgis_plugin_tools.tools.resources import resources_path from .config.configure import ConfigurePlugin +from .config.save_style_database import SaveStyleDatabase from .covadis.import_covadis import ImportCovadis from .covadis.export_covadis import ExportCovadis from .covadis.export_all import ExportCovadisAll @@ -26,6 +27,7 @@ def loadAlgorithms(self): self.addAlgorithm(ImportCovadis()) self.addAlgorithm(ExportCovadis()) self.addAlgorithm(ExportCovadisAll()) + self.addAlgorithm(SaveStyleDatabase()) def id(self): return "veloroutes_vv" diff --git a/veloroutes_voies_vertes/processing/structure/create_database_structure.py b/veloroutes_voies_vertes/processing/structure/create_database_structure.py index bc2286ea..b3c78650 100644 --- a/veloroutes_voies_vertes/processing/structure/create_database_structure.py +++ b/veloroutes_voies_vertes/processing/structure/create_database_structure.py @@ -34,6 +34,7 @@ class CreateDatabaseStructure(BaseProcessingAlgorithm): CONNECTION_NAME = "CONNECTION_NAME" OVERRIDE = "OVERRIDE" ADD_TEST_DATA = "ADD_TEST_DATA" + STYLE = "STYLE" OUTPUT_STATUS = "OUTPUT_STATUS" OUTPUT_STRING = "OUTPUT_STRING" @@ -94,6 +95,15 @@ def initAlgorithm(self, config): ) ) + self.addParameter( + QgsProcessingParameterBoolean( + self.STYLE, + tr("Ajouter les styles aux couches?"), + defaultValue=False, + optional=False, + ) + ) + # OUTPUTS self.addOutput( QgsProcessingOutputNumber(self.OUTPUT_STATUS, tr("Output status")) diff --git a/veloroutes_voies_vertes/processing/structure/upgrade_database_structure.py b/veloroutes_voies_vertes/processing/structure/upgrade_database_structure.py index f022d163..c7b8f6a6 100755 --- a/veloroutes_voies_vertes/processing/structure/upgrade_database_structure.py +++ b/veloroutes_voies_vertes/processing/structure/upgrade_database_structure.py @@ -36,6 +36,7 @@ class UpgradeDatabaseStructure(BaseProcessingAlgorithm): CONNECTION_NAME = "CONNECTION_NAME" RUN_MIGRATIONS = "RUN_MIGRATIONS" + STYLE = "STYLE" OUTPUT_STATUS = "OUTPUT_STATUS" OUTPUT_STRING = "OUTPUT_STRING" @@ -84,6 +85,16 @@ def initAlgorithm(self, config): optional=False, ) ) + + self.addParameter( + QgsProcessingParameterBoolean( + self.STYLE, + tr("Ajouter les styles aux couches?"), + defaultValue=False, + optional=False, + ) + ) + # OUTPUTS self.addOutput( QgsProcessingOutputNumber(self.OUTPUT_STATUS, tr("Output status"))