fixed regression, reworked staticmethods, changed button behaviour to reflect the internal actions

This commit is contained in:
Andreas Preikschat 2012-05-05 18:03:40 +02:00
parent 1f1bcc954f
commit 5959ee5158
3 changed files with 58 additions and 60 deletions

View File

@ -46,13 +46,36 @@ class FormattingTags(object):
"""
Provide access to the html_expands list.
"""
# Load user defined tags otherwise user defined tags are not present.
return FormattingTags.html_expands
@staticmethod
def reset_html_tags():
def save_html_tags():
"""
Resets the html_expands list.
Saves all formatting tags except protected ones.
"""
tags = []
for tag in FormattingTags.html_expands:
if not tag[u'protected'] and not tag.get(u'temporary'):
# Using dict ensures that copy is made and encoding of values
# a little later does not affect tags in the original list
tags.append(dict(tag))
tag = tags[-1]
# Remove key 'temporary' from tags.
# It is not needed to be saved.
if u'temporary' in tag:
del tag[u'temporary']
for element in tag:
if isinstance(tag[element], unicode):
tag[element] = tag[element].encode('utf8')
# 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.
"""
temporary_tags = [tag for tag in FormattingTags.html_expands
if tag.get(u'temporary')]
@ -140,38 +163,6 @@ class FormattingTags(object):
FormattingTags.add_html_tags(base_tags)
FormattingTags.add_html_tags(temporary_tags)
@staticmethod
def save_html_tags():
"""
Saves all formatting tags except protected ones.
"""
tags = []
for tag in FormattingTags.html_expands:
if not tag[u'protected'] and not tag.get(u'temporary'):
# Using dict ensures that copy is made and encoding of values
# a little later does not affect tags in the original list
tags.append(dict(tag))
tag = tags[-1]
# Remove key 'temporary' from tags.
# It is not needed to be saved.
if u'temporary' in tag:
del tag[u'temporary']
for element in tag:
if isinstance(tag[element], unicode):
tag[element] = tag[element].encode('utf8')
# 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()
@ -187,17 +178,13 @@ class FormattingTags(object):
FormattingTags.add_html_tags(user_tags)
@staticmethod
def add_html_tags(tags, save=False):
def add_html_tags(tags):
"""
Add a list of tags to the list.
``tags``
The list with tags to add.
``save``
Defaults to ``False``. If set to ``True`` the given ``tags`` are
saved to the config.
Each **tag** has to be a ``dict`` and should have the following keys:
* desc
@ -225,8 +212,6 @@ class FormattingTags(object):
displaying text containing the tag. It has to be a ``boolean``.
"""
FormattingTags.html_expands.extend(tags)
if save:
FormattingTags.save_html_tags()
@staticmethod
def remove_html_tag(tag_id):

View File

@ -57,6 +57,14 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
QtCore.SIGNAL(u'clicked()'), self.onDeleteClicked)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(u'rejected()'),
self.close)
QtCore.QObject.connect(self.descriptionLineEdit,
QtCore.SIGNAL(u'textEdited(QString)'), self.onTextEdited)
QtCore.QObject.connect(self.tagLineEdit,
QtCore.SIGNAL(u'textEdited(QString)'), self.onTextEdited)
QtCore.QObject.connect(self.startTagLineEdit,
QtCore.SIGNAL(u'textEdited(QString)'), self.onTextEdited)
QtCore.QObject.connect(self.endTagLineEdit,
QtCore.SIGNAL(u'textEdited(QString)'), self.onTextEdited)
# Forces reloading of tags from openlp configuration.
FormattingTags.load_tags()
@ -65,7 +73,7 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
Load Display and set field state.
"""
# Create initial copy from master
self._resetTable()
self._reloadTable()
self.selected = -1
return QtGui.QDialog.exec_(self)
@ -73,9 +81,9 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
"""
Table Row selected so display items and set field state.
"""
row = self.tagTableWidget.currentRow()
html = FormattingTags.html_expands[row]
self.selected = row
self.savePushButton.setEnabled(False)
self.selected = self.tagTableWidget.currentRow()
html = FormattingTags.get_html_tags()[self.selected]
self.descriptionLineEdit.setText(html[u'desc'])
self.tagLineEdit.setText(self._strip(html[u'start tag']))
self.startTagLineEdit.setText(html[u'start html'])
@ -85,21 +93,26 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
self.tagLineEdit.setEnabled(False)
self.startTagLineEdit.setEnabled(False)
self.endTagLineEdit.setEnabled(False)
self.savePushButton.setEnabled(False)
self.deletePushButton.setEnabled(False)
else:
self.descriptionLineEdit.setEnabled(True)
self.tagLineEdit.setEnabled(True)
self.startTagLineEdit.setEnabled(True)
self.endTagLineEdit.setEnabled(True)
self.savePushButton.setEnabled(True)
self.deletePushButton.setEnabled(True)
def onTextEdited(self, text):
"""
Enable the ``savePushButton`` when any of the selected tag's properties
has been changed.
"""
self.savePushButton.setEnabled(True)
def onNewClicked(self):
"""
Add a new tag to list only if it is not a duplicate.
"""
for html in FormattingTags.html_expands:
for html in FormattingTags.get_html_tags():
if self._strip(html[u'start tag']) == u'n':
critical_error_message_box(
translate('OpenLP.FormattingTagForm', 'Update Error'),
@ -117,11 +130,13 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
u'temporary': False
}
FormattingTags.add_html_tags([tag])
self._resetTable()
FormattingTags.save_html_tags()
self._reloadTable()
# Highlight new row
self.tagTableWidget.selectRow(self.tagTableWidget.rowCount() - 1)
self.onRowSelected()
self.tagTableWidget.scrollToBottom()
#self.savePushButton.setEnabled(False)
def onDeleteClicked(self):
"""
@ -130,14 +145,14 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
if self.selected != -1:
FormattingTags.remove_html_tag(self.selected)
self.selected = -1
self._resetTable()
FormattingTags.save_html_tags()
FormattingTags.save_html_tags()
self._reloadTable()
def onSavedClicked(self):
"""
Update Custom Tag details if not duplicate and save the data.
"""
html_expands = FormattingTags.html_expands
html_expands = FormattingTags.get_html_tags()
if self.selected != -1:
html = html_expands[self.selected]
tag = unicode(self.tagLineEdit.text())
@ -157,14 +172,13 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
# Keep temporary tags when the user changes one.
html[u'temporary'] = False
self.selected = -1
self._resetTable()
FormattingTags.save_html_tags()
self._reloadTable()
def _resetTable(self):
def _reloadTable(self):
"""
Reset List for loading.
"""
FormattingTags.load_tags()
self.tagTableWidget.clearContents()
self.tagTableWidget.setRowCount(0)
self.newPushButton.setEnabled(True)

View File

@ -317,9 +317,7 @@ class OpenLyrics(object):
tags_element = None
match = re.search(u'\{/?\w+\}', song.lyrics, re.UNICODE)
if match:
# Reset available tags.
FormattingTags.reset_html_tags()
# Named 'formatting' - 'format' is built-in fuction in Python.
# Named 'format_' - 'format' is built-in fuction in Python.
format_ = etree.SubElement(song_xml, u'format')
tags_element = etree.SubElement(format_, u'tags')
tags_element.set(u'application', u'OpenLP')
@ -572,7 +570,8 @@ class OpenLyrics(object):
for tag in FormattingTags.get_html_tags()]
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)
FormattingTags.add_html_tags(new_tags)
FormattingTags.save_html_tags()
def _process_lines_mixed_content(self, element, newlines=True):
"""