diff --git a/veloroutes_voies_vertes/processing/chargement/load_layers.py b/veloroutes_voies_vertes/processing/chargement/load_layers.py index 9e47be74..8dd17a1d 100644 --- a/veloroutes_voies_vertes/processing/chargement/load_layers.py +++ b/veloroutes_voies_vertes/processing/chargement/load_layers.py @@ -12,20 +12,25 @@ 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 """ + all_layer = None DATABASE = "DATABASE" SCHEMA = "SCHEMA" RASTER = "RASTER" + STYLE = "STYLE" + SAVE_STYLE = "SAVE_STYLE" OUTPUT = "OUTPUT" OUTPUT_MSG = "OUTPUT MSG" @@ -77,7 +82,25 @@ def initAlgorithm(self, config): self.addParameter( QgsProcessingParameterBoolean( self.RASTER, - tr("Ajouter un fond raster OpenStreetMap?"), + tr("Ajouter un fond raster OpenStreetMap ?"), + defaultValue=False, + optional=False, + ) + ) + + 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, ) @@ -122,6 +145,9 @@ def processAlgorithm(self, parameters, context, feedback): layers_name = ["repere", "poi_tourisme", "poi_service", "liaison", "segment"] layers_v_name = ["v_portion", "v_itineraire"] tables_name = ["element", "etape", "portion", "itineraire"] + + self.all_layer = layers_name + layers_v_name + tables_name + connection = self.parameterAsString(parameters, self.DATABASE, context) feedback.pushInfo("## CONNEXION A LA BASE DE DONNEES ##") @@ -167,6 +193,9 @@ def processAlgorithm(self, parameters, context, feedback): result = self.XYZ(context, url_with_params, 'OpenStreetMap') output_layers.append(result.id()) + self.style = self.parameterAsBool(parameters, self.STYLE, context) + self.save_style = self.parameterAsBool(parameters, self.SAVE_STYLE, context) + # add attribute tables for x in tables_name: if not context.project().mapLayersByName(x): @@ -177,3 +206,24 @@ def processAlgorithm(self, parameters, context, feedback): output_layers.append(result.id()) return {self.OUTPUT_MSG: msg, self.OUTPUT: output_layers} + + def postProcessAlgorithm(self, context, feedback): + + if not self.style and self.save_style: + feedback.reportError( + "Le style des couches ne peut pas être chargé en base car aucun " + " style n'est importé" + ) + else: + for x in self.all_layer: + layers = context.project().mapLayersByName(x) + if len(layers) >= 1: + layer = layers[0] + if self.style: + layer.loadNamedStyle(resources_path("qml", x + ".qml")) + feedback.pushInfo("Le style de la couche " + x + " a été chargé") + if self.save_style: + layer.saveStyleToDatabase(layer.name(), 'Extension Véloroutes - ' + x, True, '') + feedback.pushInfo("Le style de la couche " + x + " a été enregistré en base") + + return {} diff --git a/veloroutes_voies_vertes/processing/chargement/load_styles.py b/veloroutes_voies_vertes/processing/chargement/load_styles.py index b18ac655..6416ee1f 100644 --- a/veloroutes_voies_vertes/processing/chargement/load_styles.py +++ b/veloroutes_voies_vertes/processing/chargement/load_styles.py @@ -17,8 +17,13 @@ class LoadStylesAlgorithm(BaseProcessingAlgorithm): """ - Chargement des couches adresse depuis la base de données + Chargement des styles depuis le dossier resources """ + layers_name = [ + "repere", "poi_tourisme", "poi_service", "OpenStreetMap", + "portion", "itineraire", "liaison", "segment", "v_portion", + "v_itineraire", "etape", "element" + ] INPUT = "INPUT" OUTPUT_MSG = "OUTPUT MSG" @@ -99,13 +104,7 @@ def processAlgorithm(self, parameters, context, feedback): manager.addRelation(rel3) manager.addRelation(rel4) - layers_name = [ - "repere", "poi_tourisme", "poi_service", "OpenStreetMap", - "portion", "itineraire", "liaison", "segment", "v_portion", - "v_itineraire", "etape", "element" - ] - - for x in layers_name: + for x in self.layers_name: layers = context.project().mapLayersByName(x) if layers: for layer in layers: @@ -115,3 +114,17 @@ 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 x in self.layers_name: + layers = context.project().mapLayersByName(x) + if len(layers) >= 1: + lay = layers[0] + # 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..47484e99 100644 --- a/veloroutes_voies_vertes/processing/config/save_style_database.py +++ b/veloroutes_voies_vertes/processing/config/save_style_database.py @@ -7,24 +7,23 @@ __revision__ = '$Format:%H$' from qgis.core import ( - QgsProcessingParameterString, + QgsProcessingParameterBoolean, QgsProcessingOutputString, - QgsProcessingOutputNumber, - QgsExpressionContextUtils ) 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' def displayName(self): - return 'Sauvegerder les styles en base de données' + return 'Sauvegarder les styles en base de données' def group(self): return 'Configuration' @@ -42,7 +41,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 +55,30 @@ def initAlgorithm(self, config): def processAlgorithm(self, parameters, context, feedback): """ Here is where the processing itself takes place. - """ \ No newline at end of file + """ + confirm = self.parameterAsBool(parameters, self.CONFIRM_SAVE, context) + + if not confirm: + return { + self.OUTPUT_STRING: 'les styles n\'ont pas été sauvegardés' + } + + layers_required = [ + "repere", "poi_tourisme", "poi_service", "OpenStreetMap", + "portion", "itineraire", "liaison", "segment", "v_portion", + "v_itineraire", "etape", "element" + ] + + layers = context.project().mapLayers() + if len(layers) < 1: + return { + self.OUTPUT_STRING: 'Aucune couche n\'a été trouvée' + } + for lay in layers: + if lay in layers_required: + layer = layers[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/covadis/export_covadis.py b/veloroutes_voies_vertes/processing/covadis/export_covadis.py index 6b5904c8..baeae6cb 100644 --- a/veloroutes_voies_vertes/processing/covadis/export_covadis.py +++ b/veloroutes_voies_vertes/processing/covadis/export_covadis.py @@ -115,7 +115,6 @@ def initAlgorithm(self, config): self.PROJECTS_FOLDER, tr("Chemin de destination"), '', - False, False ) outparam.tooltip_3liz = 'Chemin de destination pour enregistrer les exports Shapefile' 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/upgrade_database_structure.py b/veloroutes_voies_vertes/processing/structure/upgrade_database_structure.py index f022d163..da792728 100755 --- a/veloroutes_voies_vertes/processing/structure/upgrade_database_structure.py +++ b/veloroutes_voies_vertes/processing/structure/upgrade_database_structure.py @@ -84,6 +84,7 @@ def initAlgorithm(self, config): optional=False, ) ) + # OUTPUTS self.addOutput( QgsProcessingOutputNumber(self.OUTPUT_STATUS, tr("Output status"))