openlp/openlp/core/ui/displaytagform.py

238 lines
9.8 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 #
# --------------------------------------------------------------------------- #
2010-12-28 11:07:59 +00:00
# Copyright (c) 2008-2011 Raoul Snyman #
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Meinert Jordan, Armin Köhler, Andreas Preikschat, #
# Christian Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon #
# Tibble, Carsten Tinggaard, Frode Woldsund #
# --------------------------------------------------------------------------- #
# 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
"""
The :mod:`DisplayTagTab` 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
"""
import cPickle
2011-02-07 15:55:02 +00:00
from PyQt4 import QtCore, QtGui
2011-02-20 15:35:52 +00:00
from openlp.core.lib import translate, DisplayTags
2011-02-21 22:10:08 +00:00
from openlp.core.lib.ui import critical_error_message_box
2011-02-20 15:35:52 +00:00
from openlp.core.ui.displaytagdialog import Ui_DisplayTagDialog
2011-02-20 15:35:52 +00:00
class DisplayTagForm(QtGui.QDialog, Ui_DisplayTagDialog):
2011-02-07 15:55:02 +00:00
"""
The :class:`DisplayTagTab` 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)
2011-02-21 20:06:55 +00:00
self.preLoad()
2011-02-20 17:06:58 +00:00
QtCore.QObject.connect(self.tagTableWidget,
QtCore.SIGNAL(u'clicked(QModelIndex)'), self.onRowSelected)
QtCore.QObject.connect(self.defaultPushButton,
QtCore.SIGNAL(u'pressed()'), self.onDefaultPushed)
QtCore.QObject.connect(self.newPushButton,
QtCore.SIGNAL(u'pressed()'), self.onNewPushed)
QtCore.QObject.connect(self.updatePushButton,
QtCore.SIGNAL(u'pressed()'), self.onUpdatePushed)
QtCore.QObject.connect(self.deletePushButton,
QtCore.SIGNAL(u'pressed()'), self.onDeletePushed)
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
2011-02-21 20:06:55 +00:00
self.preLoad()
2011-02-20 20:23:33 +00:00
self._resetTable()
2011-02-21 20:06:55 +00:00
self.selected = -1
return QtGui.QDialog.exec_(self)
def preLoad(self):
"""
Load the Tags from store so can be used in the system or used to
2011-02-25 17:05:01 +00:00
update the display. If Cancel was selected this is needed to reset the
2011-02-21 20:06:55 +00:00
dsiplay to the correct version.
"""
# Initial Load of the Tags
2010-12-31 08:34:53 +00:00
DisplayTags.reset_html_tags()
user_expands = QtCore.QSettings().value(u'displayTags/html_tags',
QtCore.QVariant(u'')).toString()
# cPickle only accepts str not unicode strings
2011-01-09 17:17:42 +00:00
user_expands_string = str(unicode(user_expands).encode(u'utf8'))
if user_expands_string:
2011-01-10 19:07:09 +00:00
user_tags = cPickle.loads(user_expands_string)
2011-01-09 17:17:42 +00:00
# If we have some user ones added them as well
for t in user_tags:
DisplayTags.add_html_tag(t)
2011-02-20 20:23:33 +00:00
def accept(self):
"""
Save Custom tags in a pickle .
"""
temp = []
2010-12-31 08:34:53 +00:00
for tag in DisplayTags.get_html_tags():
if not tag[u'protected']:
temp.append(tag)
if temp:
ctemp = cPickle.dumps(temp)
QtCore.QSettings().setValue(u'displayTags/html_tags',
QtCore.QVariant(ctemp))
else:
QtCore.QSettings().setValue(u'displayTags/html_tags',
QtCore.QVariant(u''))
2011-02-20 20:23:33 +00:00
return QtGui.QDialog.accept(self)
2011-02-20 20:23:33 +00:00
def reject(self):
"""
Reset Custom tags from Settings.
"""
self._resetTable()
2011-02-20 20:23:33 +00:00
return QtGui.QDialog.reject(self)
def onRowSelected(self):
"""
Table Row selected so display items and set field state.
"""
row = self.tagTableWidget.currentRow()
2010-12-31 08:34:53 +00:00
html = DisplayTags.get_html_tags()[row]
self.selected = row
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.updatePushButton.setEnabled(False)
self.deletePushButton.setEnabled(False)
else:
self.descriptionLineEdit.setEnabled(True)
self.tagLineEdit.setEnabled(True)
self.startTagLineEdit.setEnabled(True)
self.endTagLineEdit.setEnabled(True)
self.updatePushButton.setEnabled(True)
self.deletePushButton.setEnabled(True)
def onNewPushed(self):
"""
Add a new tag to list only if it is not a duplicate.
"""
2010-12-31 08:34:53 +00:00
for html in DisplayTags.get_html_tags():
if self._strip(html[u'start tag']) == u'n':
critical_error_message_box(
translate('OpenLP.DisplayTagTab', 'Update Error'),
translate('OpenLP.DisplayTagTab',
2011-01-15 19:24:50 +00:00
'Tag "n" already defined.'))
return
# Add new tag to list
2010-12-31 08:34:53 +00:00
tag = {u'desc': u'New Item', u'start tag': u'{n}',
u'start html': u'<Html_here>', u'end tag': u'{/n}',
2010-12-31 08:34:53 +00:00
u'end html': u'</and here>', u'protected': False}
DisplayTags.add_html_tag(tag)
self._resetTable()
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()
def onDefaultPushed(self):
"""
Remove all Custom Tags and reset to base set only.
"""
2010-12-31 08:34:53 +00:00
DisplayTags.reset_html_tags()
self._resetTable()
def onDeletePushed(self):
"""
Delete selected custom tag.
"""
if self.selected != -1:
2010-12-31 08:34:53 +00:00
DisplayTags.remove_html_tag(self.selected)
self.selected = -1
self._resetTable()
def onUpdatePushed(self):
"""
Update Custom Tag details if not duplicate.
"""
2010-12-31 08:34:53 +00:00
html_expands = DisplayTags.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(
translate('OpenLP.DisplayTagTab', 'Update Error'),
unicode(translate('OpenLP.DisplayTagTab',
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
self.selected = -1
self._resetTable()
def _resetTable(self):
"""
Reset List for loading.
"""
self.tagTableWidget.clearContents()
self.tagTableWidget.setRowCount(0)
2011-02-21 20:06:55 +00:00
self.newPushButton.setEnabled(True)
self.updatePushButton.setEnabled(False)
self.deletePushButton.setEnabled(False)
for linenumber, html in enumerate(DisplayTags.get_html_tags()):
self.tagTableWidget.setRowCount(
self.tagTableWidget.rowCount() + 1)
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']))
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