From 93337c01d0c1463954805194755b8feec0cbe5cf Mon Sep 17 00:00:00 2001 From: DamnBack Date: Fri, 17 Jan 2020 11:36:41 +0100 Subject: [PATCH] Fixed strings replacement in the layout labels within templates. (#17) * Fixed strings replacement in the layout labels within templates. --- .../Planning Application/A3L.qpt | 2 +- .../Planning Application/A4L.qpt | 6 +-- QGIS Plugin/metadata.txt | 6 ++- QGIS Plugin/templateselectordialog.py | 46 +++++++++++++------ README.md | 4 +- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Example Files/QGIS Template Files/Planning Application/A3L.qpt b/Example Files/QGIS Template Files/Planning Application/A3L.qpt index 02da981..3b095bb 100644 --- a/Example Files/QGIS Template Files/Planning Application/A3L.qpt +++ b/Example Files/QGIS Template Files/Planning Application/A3L.qpt @@ -95,7 +95,7 @@ - + diff --git a/Example Files/QGIS Template Files/Planning Application/A4L.qpt b/Example Files/QGIS Template Files/Planning Application/A4L.qpt index 8ad5353..4e22f41 100644 --- a/Example Files/QGIS Template Files/Planning Application/A4L.qpt +++ b/Example Files/QGIS Template Files/Planning Application/A4L.qpt @@ -93,7 +93,7 @@ - + @@ -109,7 +109,7 @@ - + @@ -163,7 +163,7 @@ - + diff --git a/QGIS Plugin/metadata.txt b/QGIS Plugin/metadata.txt index 8a2acd6..6c87aa8 100644 --- a/QGIS Plugin/metadata.txt +++ b/QGIS Plugin/metadata.txt @@ -13,7 +13,7 @@ name=Project and Template Selector qgisMinimumVersion=3.4 qgisMaximumVersion=3.99 description=Tool for selecting pre-defined QGIS projects. -version=1.3.2 +version=1.3.3 author=Dartmoor National Park Authority email=gi@dartmoor.gov.uk about=Tools for simplifying and automating common tasks for national parks and other protected areas. @@ -23,7 +23,9 @@ about=Tools for simplifying and automating common tasks for national parks and o # Optional items: # Uncomment the following line and add your changelog entries: -changelog=1.3.2 - Bug fixes: +changelog=1.3.3 - Bug fixes: + - Fixed strings replacement within templates +

1.3.2 - Bug fixes: - Added zooming to full extent after opening print layout

1.3.1 - Bug fixes: - Fixed issue #616 diff --git a/QGIS Plugin/templateselectordialog.py b/QGIS Plugin/templateselectordialog.py index d7ff0fb..ab05c58 100644 --- a/QGIS Plugin/templateselectordialog.py +++ b/QGIS Plugin/templateselectordialog.py @@ -23,6 +23,7 @@ import os import traceback import locale +from sys import platform from qgis.PyQt import QtGui, uic from qgis.PyQt.QtXml import QDomDocument from qgis.PyQt.QtWidgets import ( @@ -44,6 +45,7 @@ QgsLayerTreeLayer, QgsLayoutItemLegend, QgsLayoutItemMap, + QgsLayoutItemLabel, QgsReadWriteContext, QgsFeature, QgsPointXY, @@ -90,9 +92,13 @@ def __init__(self, iface): # Replacement map self.ui.suitableForComboBox.addItem('') - self.user = os.environ.get('username', '[user]') + if platform == 'win32': + self.username = os.environ.get('username', '') + else: + self.username = os.environ.get('USER', '') self.replaceMap = { - 'author': "Compiled by {} on [%concat(day($now ),'/',month($now),'/',year($now))%]".format(self.user) + 'username': self.username, + 'author': f"Compiled by {self.username} on [%concat(day($now ),'/',month($now),'/',year($now))%]" } self.ui.autofit_btn.clicked.connect(self.autofit_map) self.ui.suitableForComboBox.currentIndexChanged.connect(self.specify_dpi) @@ -253,7 +259,7 @@ def populateScaleChoices(self): locale.setlocale(locale.LC_ALL, '') currentMapCanvasScale = self.iface.mapCanvas().scale() scaleString = locale.format('%d', currentMapCanvasScale, grouping=True) - comboBox.addItem('%s (Current map canvas)' % scaleString) + comboBox.addItem(f'{scaleString} (Current map canvas)') for scale in self.presetScales: comboBox.addItem(str(scale)) self.ui.scalesGridLayout.addWidget(comboBox, i, 3) @@ -329,7 +335,7 @@ def getCopyrightText(self): self.ui.templateTypeComboBox.currentText(), 'Copyrights', self.ui.copyrightComboBox.currentText() + '.txt') try: - with open(copyrightFilePath, 'r') as copyrightFile: + with open(copyrightFilePath, 'r', errors='ignore') as copyrightFile: copyrightText = copyrightFile.read().strip() except IOError: return '' @@ -340,15 +346,15 @@ def openTemplate(self): # Load replaceable text self.replaceMap['copyright'] = self.getCopyrightText() self.replaceMap['title'] = self.ui.titleLineEdit.text() - # not in examples, is it still supported? self.replaceMap['subtitle'] = self.ui.subtitleLineEdit.text() - self.replaceMap['gridref'] = self.getPoiText() - - for k, v in self.replaceMap.items(): - item = print_layout.itemById(k) - if item: - item.setText(v) - + for item in self.get_text_items(print_layout): + item_text = item.currentText() + for k, v in self.replaceMap.items(): + item_text = item_text.replace(f'[{k}]', v) if v else item_text + item.setText(item_text) + gridref_item = print_layout.itemById('gridref') + if gridref_item: + gridref_item.setText(self.getPoiText()) # Update images of all maps elements in layout if self.identifiable_only: try: @@ -419,13 +425,13 @@ def getPoiText(self): poiLayer = layer break if poiLayer == None: - return 'Failed to find POI layer %s' % self.ui.poiLayerComboBox.currentText() + return 'Failed to find POI layer {}'.format(self.ui.poiLayerComboBox.currentText()) poiString = 'Grid References\n\n' f = QgsFeature() fit = poiLayer.getFeatures() while fit.nextFeature(f): gridRef = xy_to_osgb(f.geometry().centroid().asPoint()[0], f.geometry().centroid().asPoint()[1], 10) - coordText = '%s\t%s\n' % (f.attribute(self.ui.poiFieldComboBox.currentText()), gridRef) + coordText = '{}\t{}\n'.format(f.attribute(self.ui.poiFieldComboBox.currentText()), gridRef) poiString += coordText return poiString @@ -476,3 +482,15 @@ def get_print_layout(self): return return print_layout + + @staticmethod + def get_text_items(print_layout): + print_layout_item_model = print_layout.itemsModel() + row_count = print_layout_item_model.rowCount() + column_count = print_layout_item_model.columnCount() + for r in range(row_count): + for c in range(column_count): + index = print_layout_item_model.index(r, c) + item = print_layout_item_model.itemFromIndex(index) + if isinstance(item, QgsLayoutItemLabel): + yield item diff --git a/README.md b/README.md index cfe5506..aa3a8f9 100644 --- a/README.md +++ b/README.md @@ -81,14 +81,14 @@ This section describes how to create effective layout templates. ### String Replacement -_Template Selector_ supports automatic replacement of strings in addition to those already supported by QGIS. The following strings will automatically be replaced within layout templates: +_Template Selector_ supports automatic replacement of strings in addition to those already supported by QGIS. The following strings will automatically be replaced within layout templates labels: - [username] : the user's username (e.g. %USERNAME%), specified as environment variable +- [author]: the user's username + compilation date info - [title] : The _Title_ specified by the user in the above dialog - [subtitle] : The _Sub-title_ specified by the user in the above dialog - [copyright] : The content of the selected copyright file -NOTE: If you update from QGIS 2 print templates, in QGIS 3 it is necessary to set up 'id' fields of the items above as 'author', 'title', 'subtitle', 'copyright', respectively. ### Multiple Composer Maps Templates with multiple layout maps are supported. Composer maps are identified by their _Item ID_ property wherever present and their scales can be set independently.