Skip to content

Commit

Permalink
Fixed problem unloading / uninstalling plugin due to loading extra fonts
Browse files Browse the repository at this point in the history
Fixed problem sorting and accessing search results
Updated delimitation files http address
  • Loading branch information
icgcaadell committed May 30, 2023
1 parent 4fef6db commit 31006f9
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 54 deletions.
9 changes: 7 additions & 2 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ [email protected]

qgisMinimumVersion=2.99
qgisMaximumVersion=3.99
version=1.1.14
changelog=v1.1.14 (2023-05-23)
version=1.1.15
changelog=v1.1.15 (2023-05-30)
- Fixed problem unloading / uninstalling plugin due to loading extra fonts
- Fixed problem sorting and accessing search results
- Updated delimitation files http address

v1.1.14 (2023-05-23)
- Fixed problems with download types "all available" and "catalonia"

v1.1.13 (2023-04-18)
Expand Down
48 changes: 36 additions & 12 deletions openicgc.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@
from .qlib3.downloaddialog.downloaddialog import DownloadDialog
# Import wms resources access functions
from .resources3.wms import get_historic_ortho, get_lastest_ortoxpres, get_superexpedita_ortho, get_full_ortho
#from .resources3.wfs import get_delimitations as get_wfs_delimitations
from .resources3.fme import get_clip_data_url, get_services, get_historic_ortho_code, get_historic_ortho_ref
from .resources3.fme import get_regex_styles as get_fme_regex_styles, FME_DOWNLOAD_EPSG, FME_MAX_POLYGON_POINTS
from .resources3.http import get_dtms, get_sheets, get_delimitations, get_ndvis, get_topographic_5k, get_regex_styles as get_http_regex_styles
from .resources3.http import get_dtms, get_sheets, get_delimitations, get_ndvis, get_topographic_5k
from .resources3 import http as http_resources, wms as wms_resources, fme as fme_resources
else:
# Import basic plugin functionalities
Expand Down Expand Up @@ -92,13 +93,16 @@
import resources3.wms
reload(resources3.wms)
from resources3.wms import get_historic_ortho, get_lastest_ortoxpres, get_superexpedita_ortho, get_full_ortho
#import resources3.wfs
#reload(resources3.wfs)
#from resources3.wfs import get_delimitations as get_wfs_delimitations
import resources3.fme
reload(resources3.fme)
from resources3.fme import get_clip_data_url, get_services, get_historic_ortho_code, get_historic_ortho_ref
from resources3.fme import get_regex_styles as get_fme_regex_styles, FME_DOWNLOAD_EPSG, FME_MAX_POLYGON_POINTS
import resources3.http
reload(resources3.http)
from resources3.http import get_dtms, get_sheets, get_delimitations, get_ndvis, get_topographic_5k, get_regex_styles as get_http_regex_styles
from resources3.http import get_dtms, get_sheets, get_delimitations, get_ndvis, get_topographic_5k
from resources3 import http as http_resources, wms as wms_resources, fme as fme_resources

# Global function to set HTML tags to apply fontsize to QInputDialog text
Expand Down Expand Up @@ -348,7 +352,10 @@ def __init__(self, iface, debug_mode=False):
fme_resources.log = self.log

# Load extra fonts (Fira Sans)
self.load_fonts()
fonts_status, self.font_id_list = self.load_fonts(copy_to_temporal_folder=True)
self.log.info("Load fonts folder %s: %s" % (self.get_fonts_temporal_folder(), fonts_status))
if not fonts_status:
self.log.warning("Error loading extra fonts")

# Translated long tooltip text
self.TOOLTIP_HELP = self.tr("""Find:
Expand Down Expand Up @@ -391,15 +398,15 @@ def __init__(self, iface, debug_mode=False):

# Initialize references names (with translation)
self.HTTP_NAMES_DICT = {
"caps-municipi": self.tr("Municipal capitals"),
"caps-municipi": self.tr("Municipal capitals"), # Available HTTP
"capmunicipi": self.tr("Municipal capitals"), # Available WFS
"capcomarca": self.tr("County capitals"), # # Available WFS
"municipis": self.tr("Municipalities"),
"comarques": self.tr("Counties"),
"vegueries": self.tr("Vegueries"),
"vegueries": self.tr("Vegueries"), # Available HTTP
"provincies": self.tr("Provinces"),
"catalunya": self.tr("Catalonia"),
"catalunya": self.tr("Catalonia"), #Available HTTP
}
# Get download services regex styles
self.http_regex_styles_list = get_http_regex_styles()

# Initialize download names (with translation)
self.FME_NAMES_DICT = {
Expand Down Expand Up @@ -522,6 +529,7 @@ def unload(self):
photo_search_layer.selectionChanged.disconnect(self.on_change_photo_selection)
if self.photo_search_dialog:
photo_search_layer.willBeDeleted.disconnect(self.photo_search_dialog.reset)
self.log.debug("Disconnected signals")
# Remove photo dialog
if self.photo_search_dialog:
self.photo_search_dialog.visibilityChanged.disconnect()
Expand All @@ -531,10 +539,14 @@ def unload(self):
# Remove photo search groups
self.legend.remove_group_by_name(self.photos_group_name)
self.legend.remove_group_by_name(self.download_group_name)
self.log.debug("Removed groups")
# Remove GeoFinder dialog
self.geofinder_dialog = None
# Remove Download dialog
self.download_dialog = None
self.log.debug("Removed dialogs")
# Unload fonts
self.log.debug("Removed fonts: %s" % self.unload_fonts(self.font_id_list))
# Log plugin unloaded
self.log.info("Unload %s%s", self.metadata.get_name(), " Lite" if self.lite else "")
# Parent PluginBase class release all GUI resources created with their functions
Expand Down Expand Up @@ -591,6 +603,7 @@ def initGui(self, check_qgis_updates=True, check_icgc_updates=False):

# Get Available delimitations
delimitations_list = get_delimitations()
#wfs_delimitations_url, wfs_delimitations_list = get_wfs_delimitations()

# Gets available Sheets
sheets_list = get_sheets()
Expand Down Expand Up @@ -698,17 +711,28 @@ def initGui(self, check_qgis_updates=True, check_icgc_updates=False):
self.manage_metadata_button("Administrative divisions"), True),
"---",
] + [
(self.HTTP_NAMES_DICT.get(name, name),
(lambda _checked, name=name, scale_list=scale_list:self.layers.add_vector_layer(self.HTTP_NAMES_DICT.get(name, name), scale_list[0][1], group_name=self.backgroup_map_group_name, only_one_map_on_group=False, set_current=True, regex_styles_list=self.http_regex_styles_list) if len(scale_list) == 1 else None),
(self.HTTP_NAMES_DICT.get(name, name),
(lambda _checked, name=name, scale_list=scale_list, style_file=style_file:self.layers.add_vector_layer(self.HTTP_NAMES_DICT.get(name, name), scale_list[0][1], group_name=self.backgroup_map_group_name, only_one_map_on_group=False, set_current=True, style_file=style_file) if len(scale_list) == 1 else None),
QIcon(":/lib/qlib3/base/images/cat_vector.png"), ([
("%s 1:%s" % (self.HTTP_NAMES_DICT.get(name, name), self.format_scale(scale)),
lambda _checked, name=name, scale=scale, url=url:self.layers.add_vector_layer("%s 1:%s" % (self.HTTP_NAMES_DICT.get(name, name), self.format_scale(scale)), url, group_name=self.backgroup_map_group_name, only_one_map_on_group=False, set_current=True, regex_styles_list=self.http_regex_styles_list),
lambda _checked, name=name, scale=scale, url=url, style_file=style_file:self.layers.add_vector_layer("%s 1:%s" % (self.HTTP_NAMES_DICT.get(name, name), self.format_scale(scale)), url, group_name=self.backgroup_map_group_name, only_one_map_on_group=False, set_current=True, style_file=style_file),
QIcon(":/lib/qlib3/base/images/cat_vector.png"),
self.manage_metadata_button("Administrative divisions"), True)
for scale, url in scale_list] if len(scale_list) > 1 \
else self.manage_metadata_button("Administrative divisions")),
len(scale_list) == 1)
for name, scale_list in delimitations_list
for name, scale_list, style_file in delimitations_list
#(self.HTTP_NAMES_DICT.get(name, name),
#(lambda _checked, name=name, scale_list=scale_list, style_file=style_file:self.layers.add_wfs_layer(self.HTTP_NAMES_DICT.get(name, name), wfs_delimitations_url, [scale_list[0][1]], epsg=25831, group_name=self.backgroup_map_group_name, only_one_map_on_group=False, set_current=True, style_file=style_file) if len(scale_list) == 1 else None),
#QIcon(":/lib/qlib3/base/images/cat_vector.png"), ([
# ("%s 1:%s" % (self.HTTP_NAMES_DICT.get(name, name), self.format_scale(scale)),
# lambda _checked, name=name, scale=scale, style_file=style_file, layer_id=layer_id:self.layers.add_wfs_layer("%s 1:%s" % (self.HTTP_NAMES_DICT.get(name, name), self.format_scale(scale)), wfs_delimitations_url, [layer_id], epsg=25831, group_name=self.backgroup_map_group_name, only_one_map_on_group=False, set_current=True, style_file=style_file),
# QIcon(":/lib/qlib3/base/images/cat_vector.png"),
# self.manage_metadata_button("Administrative divisions"), True)
# for scale, layer_id in scale_list] if len(scale_list) > 1 \
# else self.manage_metadata_button("Administrative divisions")),
# len(scale_list) == 1)
#for name, scale_list, style_file in wfs_delimitations_list
]),
(self.tr("Cartographic series"), None, QIcon(":/lib/qlib3/base/images/sheets.png"), enable_http_files, [
(self.tr("%s serie") % sheet_name,
Expand Down
79 changes: 72 additions & 7 deletions qlib3/base/pluginbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import base64
import zipfile
import io
import tempfile
import shutil
from xml.etree import ElementTree
from importlib import reload
try:
Expand Down Expand Up @@ -6436,25 +6438,56 @@ def set_setting_value(self, key, value, group_name=None):
# self.settings.endArray()
# self.settings.endGroup();

def load_fonts(self, fonts_path=None, file_filter_list=["*.ttf", "*.otf"]):
def get_fonts_temporal_folder(self):
""" Retorna la carpeta temporal on copiar les fonts
---
Returns temporal fonts folder where we copy fonts
"""
tmp_path = os.path.join(tempfile.gettempdir(), "%sFonts" % self.plugin_id)
return tmp_path

def load_fonts(self, fonts_path=None, file_filter_list=["*.ttf", "*.otf"], copy_to_temporal_folder=True):
""" Carrega totes les fonts trobades en una carpeta. Per defecte en <plugin_path>\fonts
Retorna: (bool, [<font_id>,...]) True si ha anat tot bé i la llista d'ids de font
---
Load all found fonts in a folder. Default <plugin_path>\fonts
Returns: (bool, [<font_id>,...]) True if ok and a font id's list
"""
if not fonts_path:
# Si no ens passen un path, utilitzem el path per defecte: <plugin_path>\fonts
if not fonts_path:
fonts_path = os.path.join(self.plugin_path, "fonts")
if not os.path.exists(fonts_path):
return False
return False, []

# Creem una carpeta carpeta temporal si cal:
if copy_to_temporal_folder:
tmp_path = self.get_fonts_temporal_folder()
if not os.path.exists(tmp_path):
os.mkdir(tmp_path)

# Cerquem fonts dins la carpeta indicada
status = True
font_id_list = []
for file_filter in file_filter_list:
for font_pathname in glob.glob(os.path.join(fonts_path, file_filter)):
status &= self.load_font(font_pathname)
return status
# Copiem la font a una carpeta temporal si cal
if copy_to_temporal_folder:
temporal_font_pathname = os.path.join(tmp_path, os.path.basename(font_pathname))
if not os.path.exists(temporal_font_pathname):
shutil.copyfile(font_pathname, temporal_font_pathname)
font_pathname = temporal_font_pathname
# Carreguem la font
font_id = self.load_font(font_pathname)
font_id_list.append(font_id)
status &= (font_id >=0)
return status, font_id_list

def load_font(self, font_pathname_or_filename, file_types_list=["ttf", "otf"]):
""" Carrega un fitxer de font (si no és un path absolut utlitza <plugin_path>/fonts com a path base)
Retorna l'identificador de font o -1 si error
---
Loads a font file (if not it is a absolute path use <plugin_path>/fonts as source path)
Returns font id or -1 if error
"""
# Afegim el path si cal
font_pathname = font_pathname_or_filename if os.path.isabs(font_pathname_or_filename) \
Expand All @@ -6464,6 +6497,38 @@ def load_font(self, font_pathname_or_filename, file_types_list=["ttf", "otf"]):
for file_type in file_types_list:
if os.path.exists(font_pathname + "." + file_type):
font_pathname += ("." + file_type)
break
break
# Carreguem la font
return QFontDatabase.addApplicationFont(font_pathname) == 0
return QFontDatabase.addApplicationFont(font_pathname)

def unload_fonts(self, font_id_list, remove_temporal_fonts_folder=False):
""" Descarrega una llista de fonts d'aplicació
Retona: booleà, Cert si ok si no fals
---
Unload a list of application fonts
Returns: bool, True if ok else False
"""
# Descarrega totes les fonts
global_status = True
for font_id in font_id_list:
if font_id >= 0:
status = self.unload_font(font_id)
global_status &= status

# ATENCIÓ!!! ELS ARXIUS DE FONTS ES QUEDEN BLOQUEJATS I DÓNA ERROR A L'ESBORRAR-LOS
# DE MOMENT DEIXO LA OPCIÓ DESACTIVADA PER DEFECTE
# Esborrar la carpeta de fonts temporal si cal
if remove_temporal_fonts_folder:
tmp_path = self.get_fonts_temporal_folder()
if os.path.exists(tmp_path):
shutil.rmtree(tmp_path)
return global_status

def unload_font(self, font_id):
""" Descarrega una font d'aplicació
Retona: booleà, Cert si ok si no fals
---
Unload a application font
Returns: bool, True if ok else False
"""
return QFontDatabase.removeApplicationFont(font_id)
9 changes: 7 additions & 2 deletions qlib3/geofinderdialog/geofinderdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def setupUi(self, title, columns_list, keep_scale_text, default_scale):
self.tableWidget.setSelectionMode(QAbstractItemView.SingleSelection)
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
self.tableWidget.setSortingEnabled(True)

# Setup column names
if columns_list:
Expand All @@ -96,6 +95,7 @@ def setupUi(self, title, columns_list, keep_scale_text, default_scale):
self.comboBox_scale.setCurrentIndex(pos)

def set_data(self, topodata_list):
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setRowCount(len(topodata_list))

# topodata_list example:
Expand All @@ -105,9 +105,12 @@ def set_data(self, topodata_list):
self.tableWidget.setItem(i, 1, QTableWidgetItem(topodata['nomTipus'])) ## + " (" + topodata['idTipus'] + ")"))
self.tableWidget.setItem(i, 2, QTableWidgetItem(topodata['nomMunicipi']))
self.tableWidget.setItem(i, 3, QTableWidgetItem(topodata['nomComarca']))
# Ens guardem la posició original de l'item per si després s'ordena la llista i canvia l'index
self.tableWidget.item(i, 0).setData(Qt.UserRole, i);

self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
self.tableWidget.setSortingEnabled(True)

if len(topodata_list) > 0:
self.tableWidget.selectRow(0)
Expand All @@ -120,7 +123,9 @@ def do_modal(self):

def get_selection_index(self):
""" Return number of selected dialog row """
return self.tableWidget.currentRow() if self.status else -1
if not self.status or self.tableWidget.currentRow() < 0:
return -1
return self.tableWidget.item(self.tableWidget.currentRow(), 0).data(Qt.UserRole)

def find(self, text, default_epsg):
# Find text
Expand Down
Loading

0 comments on commit 31006f9

Please sign in to comment.