Fix regressions with users tags in preview and import/export in openlyrics

This commit is contained in:
Martin Zibricky 2011-09-22 16:23:51 +02:00
parent c47f936fd5
commit 8b6887036b
3 changed files with 33 additions and 44 deletions

View File

@ -33,6 +33,7 @@ from PyQt4 import QtCore
from openlp.core.lib import translate
class FormattingTags(object):
"""
Static Class to HTML Tags to be access around the code the list is managed
@ -45,6 +46,8 @@ class FormattingTags(object):
"""
Provide access to the html_expands list.
"""
# Load user defined tags otherwise user defined tags are not present.
FormattingTags.load_tags()
return FormattingTags.html_expands
@staticmethod
@ -63,7 +66,7 @@ class FormattingTags(object):
u'start html': u'<span style="-webkit-text-fill-color:red">',
u'end tag': u'{/r}', u'end html': u'</span>', u'protected': True,
u'temporary': False})
base_tags.append({u'desc': translate('OpenLP.FormattingTags', 'Black'),
base_tags.append({u'desc': translate('OpenLP.FormattingTags', 'Black'),
u'start tag': u'{b}',
u'start html': u'<span style="-webkit-text-fill-color:black">',
u'end tag': u'{/b}', u'end html': u'</span>', u'protected': True,
@ -144,13 +147,32 @@ class FormattingTags(object):
Saves all formatting tags except protected ones.
"""
tags = []
for tag in FormattingTags.get_html_tags():
for tag in FormattingTags.html_expands:
if not tag[u'protected'] and not tag[u'temporary']:
tags.append(tag)
# Formatting Tags were also known as display tags.
QtCore.QSettings().setValue(u'displayTags/html_tags',
QtCore.QVariant(cPickle.dumps(tags) if tags else u''))
@staticmethod
def load_tags():
"""
Load the Tags from store so can be used in the system or used to
update the display. If Cancel was selected this is needed to reset the
dsiplay to the correct version.
"""
# Initial Load of the Tags
FormattingTags.reset_html_tags()
# Formatting Tags were also known as display tags.
user_expands = QtCore.QSettings().value(u'displayTags/html_tags',
QtCore.QVariant(u'')).toString()
# cPickle only accepts str not unicode strings
user_expands_string = str(unicode(user_expands).encode(u'utf8'))
if user_expands_string:
user_tags = cPickle.loads(user_expands_string)
# If we have some user ones added them as well
FormattingTags.add_html_tags(user_tags)
@staticmethod
def add_html_tags(tags, save=False):
"""

View File

@ -30,14 +30,13 @@ protected and included each time loaded. Custom tags can be defined and saved.
The Custom Tag arrays are saved in a pickle so QSettings works on them. Base
Tags cannot be changed.
"""
import cPickle
from PyQt4 import QtCore, QtGui
from openlp.core.lib import translate, FormattingTags
from openlp.core.lib.ui import critical_error_message_box
from openlp.core.ui.formattingtagdialog import Ui_FormattingTagDialog
class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
"""
The :class:`FormattingTagForm` manages the settings tab .
@ -48,7 +47,6 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
"""
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
self._loadFormattingTags()
QtCore.QObject.connect(self.tagTableWidget,
QtCore.SIGNAL(u'clicked(QModelIndex)'), self.onRowSelected)
QtCore.QObject.connect(self.newPushButton,
@ -65,29 +63,10 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
Load Display and set field state.
"""
# Create initial copy from master
self._loadFormattingTags()
self._resetTable()
self.selected = -1
return QtGui.QDialog.exec_(self)
def _loadFormattingTags(self):
"""
Load the Tags from store so can be used in the system or used to
update the display. If Cancel was selected this is needed to reset the
dsiplay to the correct version.
"""
# Initial Load of the Tags
FormattingTags.reset_html_tags()
# Formatting Tags were also known as display tags.
user_expands = QtCore.QSettings().value(u'displayTags/html_tags',
QtCore.QVariant(u'')).toString()
# cPickle only accepts str not unicode strings
user_expands_string = str(unicode(user_expands).encode(u'utf8'))
if user_expands_string:
user_tags = cPickle.loads(user_expands_string)
# If we have some user ones added them as well
FormattingTags.add_html_tags(user_tags)
def onRowSelected(self):
"""
Table Row selected so display items and set field state.
@ -199,7 +178,7 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
self.tagTableWidget.setItem(linenumber, 3,
QtGui.QTableWidgetItem(html[u'end html']))
# Tags saved prior to 1.9.7 do not have this key.
if not html.has_key(u'temporary'):
if u'temporary' not in html:
html[u'temporary'] = False
self.tagTableWidget.resizeRowsToContents()
self.descriptionLineEdit.setText(u'')

View File

@ -319,8 +319,8 @@ class OpenLyrics(object):
# Reset available tags.
FormattingTags.reset_html_tags()
# Named 'formatting' - 'format' is built-in fuction in Python.
formatting = etree.SubElement(song_xml, u'format')
tags_element = etree.SubElement(formatting, u'tags')
format_ = etree.SubElement(song_xml, u'format')
tags_element = etree.SubElement(format_, u'tags')
tags_element.set(u'application', u'OpenLP')
# Process the song's lyrics.
lyrics = etree.SubElement(song_xml, u'lyrics')
@ -337,7 +337,7 @@ class OpenLyrics(object):
virtual_verses = verse[1].split(u'[---]')
for index, virtual_verse in enumerate(virtual_verses):
# Add formatting tags to text
lines_element =self._add_text_with_tags_to_lines(verse_element,
lines_element = self._add_text_with_tags_to_lines(verse_element,
virtual_verse, tags_element)
# Do not add the break attribute to the last lines element.
if index < len(virtual_verses) - 1:
@ -428,7 +428,6 @@ class OpenLyrics(object):
"""
start_tags = self.start_tags_regex.findall(text)
end_tags = self.end_tags_regex.findall(text)
# Replace start tags with xml syntax.
for tag in start_tags:
# Tags already converted to xml structure.
@ -442,16 +441,12 @@ class OpenLyrics(object):
# Add tag to <format> element if tag not present.
if tag not in xml_tags:
self._add_tag_to_formatting(tag, tags_element)
# Replace end tags.
for t in end_tags:
print repr(t)
text = text.replace(u'{/%s}' % t, u'</tag>')
# Replace \n with <br/>.
text = text.replace(u'\n', u'<br/>')
text = u'<lines>' + text + u'</lines>'
print repr(text)
element = etree.XML(text)
verse_element.append(element)
return element
@ -558,7 +553,7 @@ class OpenLyrics(object):
continue
start_tag = u'{%s}' % name[:5]
# Some tags have only start tag e.g. {br}
end_tag = u'{' + name[:5] + u'}' if hasattr(tag, 'close') else u''
end_tag = u'{/' + name[:5] + u'}' if hasattr(tag, 'close') else u''
openlp_tag = {
u'desc': name,
u'start tag': start_tag,
@ -572,8 +567,9 @@ class OpenLyrics(object):
found_tags.append(openlp_tag)
existing_tag_ids = [tag[u'start tag']
for tag in FormattingTags.get_html_tags()]
FormattingTags.add_html_tags([tag for tag in found_tags
if tag[u'start tag'] not in existing_tag_ids], True)
new_tags = [tag for tag in found_tags
if tag[u'start tag'] not in existing_tag_ids]
FormattingTags.add_html_tags(new_tags, True)
def _process_lines_mixed_content(self, element, newlines=True):
"""
@ -589,7 +585,6 @@ class OpenLyrics(object):
"""
text = u''
use_endtag = True
# Skip <comment> elements - not yet supported.
if element.tag == NSMAP % u'comment' and element.tail:
# Append tail text at chord element.
@ -606,7 +601,6 @@ class OpenLyrics(object):
if element.tail:
text += element.tail
return text
# Start formatting tag.
if element.tag == NSMAP % u'tag':
text += u'{%s}' % element.get(u'name')
@ -614,24 +608,19 @@ class OpenLyrics(object):
# Handle this case if element has no children and contains no text.
if len(element) == 0 and not element.text:
use_endtag = False
# Append text from element.
if element.text:
text += element.text
# Process nested formatting tags.
for child in element:
# Use recursion since nested formatting tags are allowed.
text += self._process_lines_mixed_content(child, newlines)
# Append text from tail and add formatting end tag.
if element.tag == NSMAP % 'tag' and use_endtag:
text += u'{/%s}' % element.get(u'name')
# Append text from tail.
if element.tail:
text += element.tail
return text
def _process_verse_lines(self, lines):
@ -657,7 +646,6 @@ class OpenLyrics(object):
# Append text from "lines" element to verse text.
else:
text = self._process_lines_mixed_content(element)
return text
def _process_lyrics(self, properties, song_xml, song_obj):