Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve style support #120

Merged
merged 2 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion resource_sharing/resource_handler/symbol_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,20 @@ def install(self):
colorramp_name, colorramp['colorramp'], True):
self._group_or_tag(QgsStyle.ColorrampEntity,
colorramp_name, groupOrTag_id)

for textformat in symbol_xml_extractor.textformats:
textformat_name = '%s (%s)' % (
textformat['name'], self.collection['repository_name'])
if self.style.addTextFormat(
textformat_name, textformat['textformat'], True):
self._group_or_tag(QgsStyle.TextFormatEntity,
textformat_name, groupOrTag_id)
for labelsetting in symbol_xml_extractor.labelsettings:
labelsetting_name = '%s (%s)' % (
labelsetting['name'], self.collection['repository_name'])
if self.style.addLabelSettings(
labelsetting_name, labelsetting['labelsettings'], True):
self._group_or_tag(QgsStyle.LabelSettingsEntity,
labelsetting_name, groupOrTag_id)
if valid >= 0:
self.collection[SYMBOL] = valid

Expand All @@ -195,6 +208,16 @@ def uninstall(self):
QgsStyle.ColorrampEntity, child_group_id)
for colorramp in colorramps:
self.style.removeColorRamp(colorramp)
# Get all the textformats for this tag / child group and remove them
textformats = self._get_symbols_for_group_or_tag(
QgsStyle.TextFormatEntity, child_group_id)
for textformat in textformats:
self.style.removeTextFormat(textformat)
# Get all the labelsettings for this tag / child group and remove them
labelsettings = self._get_symbols_for_group_or_tag(
QgsStyle.LabelSettingsEntity, child_group_id)
for labelsetting in labelsettings:
self.style.removeLabelSettings(labelsetting)

# Remove this tag / child group
self._group_or_tag_remove(child_group_id)
Expand Down
60 changes: 60 additions & 0 deletions resource_sharing/symbol_xml_extractor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# coding=utf-8
import os
import logging

from qgis.PyQt.QtXml import QDomDocument
from qgis.PyQt.QtCore import QFile, QIODevice
from qgis.core import QgsSymbolLayerUtils, QgsReadWriteContext, QgsProject
from qgis.core import QgsPalLayerSettings, QgsTextFormat

LOGGER = logging.getLogger('QGIS Resource Sharing')

class SymbolXMLExtractor(object):
"""Parses the given file and returns the symbols and colorramps"""
Expand Down Expand Up @@ -68,6 +71,37 @@ def parse_xml(self):

ramp_element = ramp_element.nextSiblingElement()

# Get all the TextFormats - textformats - textformat
self._textformats = []
textformats_element = document_element.firstChildElement('textformats')
textformat_element = textformats_element.firstChildElement()
while not textformat_element.isNull():
if textformat_element.tagName() == 'textformat':
textformat = QgsTextFormat()
textformat.readXml(textformat_element, QgsReadWriteContext())
if textformat:
self._textformats.append({
'name': textformat_element.attribute('name'),
'textformat': textformat
})
textformat_element = textformat_element.nextSiblingElement()

# Get all the LabelSettings - labelsettings - labelsetting -
# QgsPalLayerSettings.readXML?
self._labelsettings = []
labels_element = document_element.firstChildElement('labelsettings')
label_element = labels_element.firstChildElement()

while not label_element.isNull():
if label_element.tagName() == 'labelsetting':
labelsettings = QgsPalLayerSettings()
labelsettings.readXml(label_element, QgsReadWriteContext())
if labelsettings:
self._labelsettings.append({
'name': label_element.attribute('name'),
'labelsettings': labelsettings
})
label_element = label_element.nextSiblingElement()
return True

@property
Expand Down Expand Up @@ -97,3 +131,29 @@ def colorramps(self):
]
"""
return self._colorramps

@property
def textformats(self):
"""Return a list of the textformats in the XML file.
The structure of the property:
textformats = [
{
'name': str
'textformat': QgsTextFormat???
}
]
"""
return self._textformats

@property
def labelsettings(self):
"""Return a list of the labelsettings in the XML file.
The structure of the property:
labelsettings = [
{
'name': str
'labelsettings': QgsPalLayerSettings???
}
]
"""
return self._labelsettings