openlp/openlp/core/ui/formattingtagform.py

222 lines
10 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2011-12-27 10:33:55 +00:00
# Copyright (c) 2008-2012 Raoul Snyman #
# Portions copyright (c) 2008-2012 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-11-11 21:16:14 +00:00
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
2012-10-21 13:16:22 +00:00
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
2012-11-07 21:37:01 +00:00
# Frode Woldsund, Martin Zibricky #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2011-02-07 15:55:02 +00:00
"""
2011-07-30 07:40:34 +00:00
The :mod:`formattingtagform` provides an Tag Edit facility. The Base set are
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.
2011-02-07 15:55:02 +00:00
"""
from PyQt4 import QtCore, QtGui
from openlp.core.lib import translate, FormattingTags
2011-02-21 22:10:08 +00:00
from openlp.core.lib.ui import critical_error_message_box
from openlp.core.ui.formattingtagdialog import Ui_FormattingTagDialog
class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
2011-02-07 15:55:02 +00:00
"""
The :class:`FormattingTagForm` manages the settings tab .
2011-02-07 15:55:02 +00:00
"""
2011-02-20 17:06:58 +00:00
def __init__(self, parent):
2011-02-07 15:55:02 +00:00
"""
2011-02-20 17:06:58 +00:00
Constructor
2011-02-07 15:55:02 +00:00
"""
2011-02-20 17:06:58 +00:00
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
QtCore.QObject.connect(self.tagTableWidget,
QtCore.SIGNAL(u'itemSelectionChanged()'),self.onRowSelected)
2011-02-20 17:06:58 +00:00
QtCore.QObject.connect(self.newPushButton,
QtCore.SIGNAL(u'clicked()'), self.onNewClicked)
2011-05-07 17:43:24 +00:00
QtCore.QObject.connect(self.savePushButton,
QtCore.SIGNAL(u'clicked()'), self.onSavedClicked)
2011-02-20 17:06:58 +00:00
QtCore.QObject.connect(self.deletePushButton,
QtCore.SIGNAL(u'clicked()'), self.onDeleteClicked)
2011-05-08 05:42:48 +00:00
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()
2011-02-20 17:06:58 +00:00
def exec_(self):
"""
2011-02-20 17:06:58 +00:00
Load Display and set field state.
"""
# Create initial copy from master
self._reloadTable()
2011-02-21 20:06:55 +00:00
self.selected = -1
return QtGui.QDialog.exec_(self)
def onRowSelected(self):
"""
Table Row selected so display items and set field state.
"""
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'])
self.endTagLineEdit.setText(html[u'end html'])
if html[u'protected']:
self.descriptionLineEdit.setEnabled(False)
self.tagLineEdit.setEnabled(False)
self.startTagLineEdit.setEnabled(False)
self.endTagLineEdit.setEnabled(False)
self.deletePushButton.setEnabled(False)
else:
self.descriptionLineEdit.setEnabled(True)
self.tagLineEdit.setEnabled(True)
self.startTagLineEdit.setEnabled(True)
self.endTagLineEdit.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.get_html_tags():
if self._strip(html[u'start tag']) == u'n':
critical_error_message_box(
2011-07-30 07:43:19 +00:00
translate('OpenLP.FormattingTagForm', 'Update Error'),
translate('OpenLP.FormattingTagForm',
2011-01-15 19:24:50 +00:00
'Tag "n" already defined.'))
return
# Add new tag to list
tag = {
2011-07-30 07:43:19 +00:00
u'desc': translate('OpenLP.FormattingTagForm', 'New Tag'),
u'start tag': u'{n}',
2011-07-30 07:43:19 +00:00
u'start html': translate('OpenLP.FormattingTagForm', '<HTML here>'),
u'end tag': u'{/n}',
2011-07-30 07:43:19 +00:00
u'end html': translate('OpenLP.FormattingTagForm', '</and here>'),
u'protected': False,
u'temporary': False
}
FormattingTags.add_html_tags([tag])
FormattingTags.save_html_tags()
self._reloadTable()
2011-01-08 09:44:44 +00:00
# Highlight new row
self.tagTableWidget.selectRow(self.tagTableWidget.rowCount() - 1)
2011-02-20 20:23:33 +00:00
self.onRowSelected()
self.tagTableWidget.scrollToBottom()
#self.savePushButton.setEnabled(False)
def onDeleteClicked(self):
"""
Delete selected custom tag.
"""
if self.selected != -1:
FormattingTags.remove_html_tag(self.selected)
# As the first items are protected we should not have to take care
# of negative indexes causing tracebacks.
self.tagTableWidget.selectRow(self.selected - 1)
self.selected = -1
FormattingTags.save_html_tags()
self._reloadTable()
def onSavedClicked(self):
"""
2011-05-08 05:42:48 +00:00
Update Custom Tag details if not duplicate and save the data.
"""
html_expands = FormattingTags.get_html_tags()
if self.selected != -1:
html = html_expands[self.selected]
tag = unicode(self.tagLineEdit.text())
for linenumber, html1 in enumerate(html_expands):
if self._strip(html1[u'start tag']) == tag and \
linenumber != self.selected:
critical_error_message_box(
2011-07-30 07:43:19 +00:00
translate('OpenLP.FormattingTagForm', 'Update Error'),
unicode(translate('OpenLP.FormattingTagForm',
2011-01-15 19:24:50 +00:00
'Tag %s already defined.')) % tag)
return
html[u'desc'] = unicode(self.descriptionLineEdit.text())
html[u'start html'] = unicode(self.startTagLineEdit.text())
html[u'end html'] = unicode(self.endTagLineEdit.text())
html[u'start tag'] = u'{%s}' % tag
html[u'end tag'] = u'{/%s}' % tag
# Keep temporary tags when the user changes one.
html[u'temporary'] = False
self.selected = -1
2011-08-27 14:00:24 +00:00
FormattingTags.save_html_tags()
self._reloadTable()
def _reloadTable(self):
"""
Reset List for loading.
"""
self.tagTableWidget.clearContents()
self.tagTableWidget.setRowCount(0)
2011-02-21 20:06:55 +00:00
self.newPushButton.setEnabled(True)
2011-05-07 17:43:24 +00:00
self.savePushButton.setEnabled(False)
2011-02-21 20:06:55 +00:00
self.deletePushButton.setEnabled(False)
2012-05-02 18:25:37 +00:00
for linenumber, html in enumerate(FormattingTags.get_html_tags()):
self.tagTableWidget.setRowCount(self.tagTableWidget.rowCount() + 1)
2011-02-21 20:06:55 +00:00
self.tagTableWidget.setItem(linenumber, 0,
QtGui.QTableWidgetItem(html[u'desc']))
self.tagTableWidget.setItem(linenumber, 1,
QtGui.QTableWidgetItem(self._strip(html[u'start tag'])))
self.tagTableWidget.setItem(linenumber, 2,
QtGui.QTableWidgetItem(html[u'start html']))
self.tagTableWidget.setItem(linenumber, 3,
QtGui.QTableWidgetItem(html[u'end html']))
# Permanent (persistent) tags do not have this key.
if u'temporary' not in html:
html[u'temporary'] = False
2011-02-21 20:06:55 +00:00
self.tagTableWidget.resizeRowsToContents()
self.descriptionLineEdit.setText(u'')
self.tagLineEdit.setText(u'')
self.startTagLineEdit.setText(u'')
self.endTagLineEdit.setText(u'')
self.descriptionLineEdit.setEnabled(False)
self.tagLineEdit.setEnabled(False)
self.startTagLineEdit.setEnabled(False)
self.endTagLineEdit.setEnabled(False)
def _strip(self, tag):
"""
Remove tag wrappers for editing.
"""
tag = tag.replace(u'{', u'')
tag = tag.replace(u'}', u'')
2010-12-31 08:34:53 +00:00
return tag