openlp/openlp/core/ui/printserviceform.py

427 lines
16 KiB
Python
Raw Normal View History

2011-02-04 18:00:59 +00:00
# -*- coding: utf-8 -*-
2012-12-29 13:35:16 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2011-02-04 18:00:59 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2012-12-29 20:56:56 +00:00
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 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, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
2011-02-04 18:00:59 +00:00
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
2013-02-01 20:52:42 +00:00
"""
The actual print service dialog
"""
import cgi
2011-02-04 18:00:59 +00:00
import datetime
import os
2011-02-04 18:00:59 +00:00
from PyQt4 import QtCore, QtGui
2011-04-06 19:14:02 +00:00
from lxml import html
2011-02-04 18:00:59 +00:00
2013-02-07 08:42:17 +00:00
from openlp.core.lib import Settings, UiStrings, Registry, translate, get_text_file_string
2011-02-19 17:33:24 +00:00
from openlp.core.ui.printservicedialog import Ui_PrintServiceDialog, ZoomSize
from openlp.core.utils import AppLocation
DEFAULT_CSS = """/*
Edit this file to customize the service order print. Note, that not all CSS
properties are supported. See:
http://doc.trolltech.com/4.7/richtext-html-subset.html#css-properties
*/
.serviceTitle {
2011-05-30 05:03:46 +00:00
font-weight: 600;
font-size: x-large;
color: black;
}
2011-04-25 10:13:13 +00:00
.item {
2011-05-30 05:03:46 +00:00
color: black;
2011-04-25 10:13:13 +00:00
}
.itemTitle {
2011-05-30 05:03:46 +00:00
font-weight: 600;
font-size: large;
}
.itemText {
2011-05-30 05:03:46 +00:00
margin-top: 10px;
}
.itemFooter {
2011-05-30 05:03:46 +00:00
font-size: 8px;
}
2011-04-25 10:13:13 +00:00
.itemNotes {}
2011-04-24 19:50:28 +00:00
.itemNotesTitle {
2011-05-30 05:03:46 +00:00
font-weight: bold;
font-size: 12px;
}
.itemNotesText {
2011-05-30 05:03:46 +00:00
font-size: 11px;
2011-04-24 19:50:28 +00:00
}
2011-04-25 10:13:13 +00:00
.media {}
2011-04-24 19:50:28 +00:00
.mediaTitle {
2011-05-30 05:03:46 +00:00
font-weight: bold;
font-size: 11px;
2011-04-25 10:13:13 +00:00
}
.mediaText {}
.imageList {}
.customNotes {
2011-05-30 05:03:46 +00:00
margin-top: 10px;
}
.customNotesTitle {
2011-05-30 05:03:46 +00:00
font-weight: bold;
font-size: 11px;
}
.customNotesText {
2011-05-30 05:03:46 +00:00
font-size: 11px;
}
2011-04-24 19:50:28 +00:00
.newPage {
2011-05-30 05:03:46 +00:00
page-break-before: always;
2011-04-24 19:50:28 +00:00
}
"""
2011-02-04 18:00:59 +00:00
2011-02-15 18:51:37 +00:00
2013-02-01 20:52:42 +00:00
class PrintServiceForm(QtGui.QDialog, Ui_PrintServiceDialog):
"""
The :class:`~openlp.core.ui.printserviceform.PrintServiceForm` class displays a dialog for printing the service.
"""
2013-01-26 07:39:07 +00:00
def __init__(self):
2011-02-04 18:00:59 +00:00
"""
Constructor
"""
2013-01-26 07:39:07 +00:00
QtGui.QDialog.__init__(self, self.main_window)
2011-02-05 19:48:21 +00:00
self.printer = QtGui.QPrinter()
self.printDialog = QtGui.QPrintDialog(self.printer, self)
self.document = QtGui.QTextDocument()
2011-02-19 20:23:23 +00:00
self.zoom = 0
2011-02-06 13:57:57 +00:00
self.setupUi(self)
2011-02-06 14:00:39 +00:00
# Load the settings for the dialog.
2012-05-17 15:13:09 +00:00
settings = Settings()
2011-02-04 20:10:32 +00:00
settings.beginGroup(u'advanced')
self.slideTextCheckBox.setChecked(settings.value(u'print slide text'))
self.pageBreakAfterText.setChecked(settings.value(u'add page break'))
2011-04-06 19:14:02 +00:00
if not self.slideTextCheckBox.isChecked():
self.pageBreakAfterText.setDisabled(True)
self.metaDataCheckBox.setChecked(settings.value(u'print file meta data'))
self.notesCheckBox.setChecked(settings.value(u'print notes'))
self.zoomComboBox.setCurrentIndex(settings.value(u'display size'))
2011-02-04 20:10:32 +00:00
settings.endGroup()
2011-02-06 07:16:42 +00:00
# Signals
2012-12-29 13:35:16 +00:00
QtCore.QObject.connect(self.printButton, QtCore.SIGNAL(u'triggered()'), self.printServiceOrder)
QtCore.QObject.connect(self.zoomOutButton, QtCore.SIGNAL(u'clicked()'), self.zoomOut)
QtCore.QObject.connect(self.zoomInButton, QtCore.SIGNAL(u'clicked()'), self.zoomIn)
QtCore.QObject.connect(self.zoomOriginalButton, QtCore.SIGNAL(u'clicked()'), self.zoomOriginal)
QtCore.QObject.connect(self.previewWidget, QtCore.SIGNAL(u'paintRequested(QPrinter *)'), self.paintRequested)
QtCore.QObject.connect(self.zoomComboBox, QtCore.SIGNAL(u'currentIndexChanged(int)'), self.displaySizeChanged)
QtCore.QObject.connect(self.plainCopy, QtCore.SIGNAL(u'triggered()'), self.copyText)
QtCore.QObject.connect(self.htmlCopy, QtCore.SIGNAL(u'triggered()'), self.copyHtmlText)
2013-01-27 07:36:04 +00:00
QtCore.QObject.connect(self.slideTextCheckBox, QtCore.SIGNAL(u'stateChanged(int)'),
self.onSlideTextCheckBoxChanged)
2011-02-19 17:33:24 +00:00
self.updatePreviewText()
2011-02-19 08:36:24 +00:00
def toggleOptions(self, checked):
2013-02-01 20:52:42 +00:00
"""
Toggle various options
"""
2011-02-19 08:36:24 +00:00
self.optionsWidget.setVisible(checked)
if checked:
left = self.optionsButton.pos().x()
top = self.toolbar.height()
self.optionsWidget.move(left, top)
self.titleLineEdit.setFocus()
else:
self.saveOptions()
2011-02-19 20:23:23 +00:00
self.updatePreviewText()
2011-02-04 18:00:59 +00:00
2011-02-06 07:16:42 +00:00
def updatePreviewText(self):
2011-02-04 18:00:59 +00:00
"""
2011-02-06 13:57:57 +00:00
Creates the html text and updates the html of *self.document*.
2011-02-04 18:00:59 +00:00
"""
2011-04-24 19:50:28 +00:00
html_data = self._addElement(u'html')
self._addElement(u'head', parent=html_data)
2012-05-17 18:57:01 +00:00
self._addElement(u'title', self.titleLineEdit.text(), html_data.head)
2012-12-29 13:35:16 +00:00
css_path = os.path.join(AppLocation.get_data_path(), u'service_print.css')
2011-04-13 09:19:16 +00:00
custom_css = get_text_file_string(css_path)
2011-04-24 19:50:28 +00:00
if not custom_css:
custom_css = DEFAULT_CSS
self._addElement(u'style', custom_css, html_data.head, attribute=(u'type', u'text/css'))
2011-04-24 19:50:28 +00:00
self._addElement(u'body', parent=html_data)
self._addElement(u'h1', cgi.escape(self.titleLineEdit.text()), html_data.body, classId=u'serviceTitle')
2013-02-03 19:23:12 +00:00
for index, item in enumerate(self.service_manager.service_items):
2011-04-24 19:50:28 +00:00
self._addPreviewItem(html_data.body, item[u'service_item'], index)
2011-04-06 19:14:02 +00:00
# Add the custom service notes:
2011-04-13 09:19:16 +00:00
if self.footerTextEdit.toPlainText():
2012-12-29 13:35:16 +00:00
div = self._addElement(u'div', parent=html_data.body, classId=u'customNotes')
self._addElement(
u'span', translate('OpenLP.ServiceManager', 'Custom Service Notes: '), div, classId=u'customNotesTitle')
2012-12-29 13:35:16 +00:00
self._addElement(u'span', cgi.escape(self.footerTextEdit.toPlainText()), div, classId=u'customNotesText')
2011-04-06 19:14:02 +00:00
self.document.setHtml(html.tostring(html_data))
2011-02-06 07:16:42 +00:00
self.previewWidget.updatePreview()
2011-04-24 19:50:28 +00:00
def _addPreviewItem(self, body, item, index):
2013-02-01 20:52:42 +00:00
"""
Add a preview item
"""
2011-04-24 19:50:28 +00:00
div = self._addElement(u'div', classId=u'item', parent=body)
# Add the title of the service item.
item_title = self._addElement(u'h2', parent=div, classId=u'itemTitle')
2012-12-29 13:35:16 +00:00
self._addElement(u'img', parent=item_title, attribute=(u'src', item.icon))
self._addElement(u'span', u' ' + cgi.escape(item.get_display_title()), item_title)
2011-04-24 19:50:28 +00:00
if self.slideTextCheckBox.isChecked():
# Add the text of the service item.
if item.is_text():
verse_def = None
for slide in item.get_frames():
if not verse_def or verse_def != slide[u'verseTag']:
2012-12-29 13:35:16 +00:00
text_div = self._addElement(u'div', parent=div, classId=u'itemText')
2011-04-24 19:50:28 +00:00
else:
self._addElement(u'br', parent=text_div)
self._addElement(u'span', slide[u'html'], text_div)
2011-04-24 19:50:28 +00:00
verse_def = slide[u'verseTag']
# Break the page before the div element.
if index != 0 and self.pageBreakAfterText.isChecked():
div.set(u'class', u'item newPage')
# Add the image names of the service item.
elif item.is_image():
ol = self._addElement(u'ol', parent=div, classId=u'imageList')
for slide in range(len(item.get_frames())):
self._addElement(u'li', item.get_frame_title(slide), ol)
# add footer
foot_text = item.foot_text
foot_text = foot_text.partition(u'<br>')[2]
if foot_text:
foot_text = cgi.escape(foot_text.replace(u'<br>', u'\n'))
2012-12-29 13:35:16 +00:00
self._addElement(u'div', foot_text.replace(u'\n', u'<br>'), parent=div, classId=u'itemFooter')
2011-04-24 19:50:28 +00:00
# Add service items' notes.
if self.notesCheckBox.isChecked():
if item.notes:
p = self._addElement(u'div', classId=u'itemNotes', parent=div)
self._addElement(u'span', translate('OpenLP.ServiceManager', 'Notes: '), p, classId=u'itemNotesTitle')
2012-12-29 13:35:16 +00:00
self._addElement(u'span', cgi.escape(item.notes).replace(u'\n', u'<br>'), p, classId=u'itemNotesText')
2011-04-24 19:50:28 +00:00
# Add play length of media files.
if item.is_media() and self.metaDataCheckBox.isChecked():
tme = item.media_length
if item.end_time > 0:
tme = item.end_time - item.start_time
title = self._addElement(u'div', classId=u'media', parent=div)
self._addElement(
u'span', translate('OpenLP.ServiceManager', 'Playing time: '), title, classId=u'mediaTitle')
self._addElement(u'span', unicode(datetime.timedelta(seconds=tme)), title, classId=u'mediaText')
2011-04-24 19:50:28 +00:00
def _addElement(self, tag, text=None, parent=None, classId=None, attribute=None):
2011-04-13 09:19:16 +00:00
"""
Creates a html element. If ``text`` is given, the element's text will
set and if a ``parent`` is given, the element is appended.
2011-04-13 09:42:18 +00:00
``tag``
The html tag, e. g. ``u'span'``. Defaults to ``None``.
``text``
The text for the tag. Defaults to ``None``.
``parent``
The parent element. Defaults to ``None``.
2011-04-24 19:50:28 +00:00
``classId``
Value for the class attribute
2011-04-13 09:42:18 +00:00
2011-04-24 19:50:28 +00:00
``attribute``
Tuple name/value pair to add as an optional attribute
2011-04-13 09:19:16 +00:00
"""
if text is not None:
2011-04-24 19:50:28 +00:00
element = html.fragment_fromstring(unicode(text), create_parent=tag)
else:
element = html.Element(tag)
2011-04-13 09:19:16 +00:00
if parent is not None:
parent.append(element)
2011-04-24 19:50:28 +00:00
if classId is not None:
element.set(u'class', classId)
2011-04-13 09:42:18 +00:00
if attribute is not None:
2011-04-24 19:50:28 +00:00
element.set(attribute[0], attribute[1])
2011-04-13 09:19:16 +00:00
return element
2011-02-06 07:16:42 +00:00
def paintRequested(self, printer):
"""
2011-02-06 13:57:57 +00:00
Paint the preview of the *self.document*.
2011-02-07 17:27:38 +00:00
``printer``
A *QPrinter* object.
2011-02-06 07:16:42 +00:00
"""
self.document.print_(printer)
2011-02-06 07:16:42 +00:00
2011-02-19 17:33:24 +00:00
def displaySizeChanged(self, display):
"""
The Zoom Combo box has changed so set up the size.
"""
if display == ZoomSize.Page:
self.previewWidget.fitInView()
elif display == ZoomSize.Width:
self.previewWidget.fitToWidth()
elif display == ZoomSize.OneHundred:
self.previewWidget.fitToWidth()
2011-02-19 20:23:23 +00:00
self.previewWidget.zoomIn(1)
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.SeventyFive:
self.previewWidget.fitToWidth()
2011-02-19 20:23:23 +00:00
self.previewWidget.zoomIn(0.75)
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.Fifty:
self.previewWidget.fitToWidth()
2011-02-19 20:23:23 +00:00
self.previewWidget.zoomIn(0.5)
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.TwentyFive:
self.previewWidget.fitToWidth()
2011-02-19 20:23:23 +00:00
self.previewWidget.zoomIn(0.25)
2012-05-17 15:13:09 +00:00
settings = Settings()
2011-02-19 17:33:24 +00:00
settings.beginGroup(u'advanced')
2012-05-17 15:13:09 +00:00
settings.setValue(u'display size', display)
2011-02-19 17:33:24 +00:00
settings.endGroup()
2011-02-15 18:51:37 +00:00
def copyText(self):
2011-02-18 17:38:39 +00:00
"""
Copies the display text to the clipboard as plain text
"""
2011-08-22 17:32:18 +00:00
self.update_song_usage()
cursor = QtGui.QTextCursor(self.document)
cursor.select(QtGui.QTextCursor.Document)
clipboard_text = cursor.selectedText()
# We now have the unprocessed unicode service text in the cursor
# So we replace u2028 with \n and u2029 with \n\n and a few others
clipboard_text = clipboard_text.replace(u'\u2028', u'\n')
clipboard_text = clipboard_text.replace(u'\u2029', u'\n\n')
clipboard_text = clipboard_text.replace(u'\u2018', u'\'')
clipboard_text = clipboard_text.replace(u'\u2019', u'\'')
clipboard_text = clipboard_text.replace(u'\u201c', u'"')
clipboard_text = clipboard_text.replace(u'\u201d', u'"')
clipboard_text = clipboard_text.replace(u'\u2026', u'...')
clipboard_text = clipboard_text.replace(u'\u2013', u'-')
clipboard_text = clipboard_text.replace(u'\u2014', u'-')
# remove the icon from the text
clipboard_text = clipboard_text.replace(u'\ufffc\xa0', u'')
# and put it all on the clipboard
2013-01-26 07:39:07 +00:00
self.main_window.clipboard.setText(clipboard_text)
2011-02-18 17:38:39 +00:00
def copyHtmlText(self):
"""
Copies the display text to the clipboard as Html
"""
2011-08-22 17:32:18 +00:00
self.update_song_usage()
2013-01-26 07:39:07 +00:00
self.main_window.clipboard.setText(self.document.toHtml())
2011-02-18 17:38:39 +00:00
2011-02-06 07:16:42 +00:00
def printServiceOrder(self):
2011-02-06 13:57:57 +00:00
"""
Called, when the *printButton* is clicked. Opens the *printDialog*.
"""
2011-02-06 07:16:42 +00:00
if not self.printDialog.exec_():
return
2011-08-26 10:21:47 +00:00
self.update_song_usage()
2011-02-06 13:57:57 +00:00
# Print the document.
2011-02-06 07:16:42 +00:00
self.document.print_(self.printer)
2011-02-06 13:57:57 +00:00
def zoomIn(self):
"""
Called when *zoomInButton* is clicked.
"""
self.previewWidget.zoomIn()
2011-02-19 20:23:23 +00:00
self.zoom -= 0.1
2011-02-06 13:57:57 +00:00
def zoomOut(self):
"""
Called when *zoomOutButton* is clicked.
"""
self.previewWidget.zoomOut()
2011-02-19 20:23:23 +00:00
self.zoom += 0.1
2011-02-06 13:57:57 +00:00
2011-02-19 17:33:24 +00:00
def zoomOriginal(self):
"""
Called when *zoomOutButton* is clicked.
"""
2011-02-19 20:23:23 +00:00
self.previewWidget.zoomIn(1 + self.zoom)
self.zoom = 0
2011-02-19 17:33:24 +00:00
2011-02-15 18:51:37 +00:00
def updateTextFormat(self, value):
"""
Called when html copy check box is selected.
"""
if value == QtCore.Qt.Checked:
self.copyTextButton.setText(UiStrings().CopyToHtml)
2011-02-15 18:51:37 +00:00
else:
self.copyTextButton.setText(UiStrings().CopyToText)
2011-02-15 18:51:37 +00:00
2011-04-06 19:14:02 +00:00
def onSlideTextCheckBoxChanged(self, state):
"""
2011-04-07 08:45:49 +00:00
Disable or enable the ``pageBreakAfterText`` checkbox as it should only
be enabled, when the ``slideTextCheckBox`` is enabled.
2011-04-06 19:14:02 +00:00
"""
self.pageBreakAfterText.setDisabled(state == QtCore.Qt.Unchecked)
2011-02-19 08:36:24 +00:00
def saveOptions(self):
2011-02-06 13:57:57 +00:00
"""
Save the settings and close the dialog.
"""
2011-02-04 20:10:32 +00:00
# Save the settings for this dialog.
2012-05-17 15:13:09 +00:00
settings = Settings()
2011-02-04 20:10:32 +00:00
settings.beginGroup(u'advanced')
2012-12-29 13:35:16 +00:00
settings.setValue(u'print slide text', self.slideTextCheckBox.isChecked())
settings.setValue(u'add page break', self.pageBreakAfterText.isChecked())
settings.setValue(u'print file meta data', self.metaDataCheckBox.isChecked())
2012-05-17 15:13:09 +00:00
settings.setValue(u'print notes', self.notesCheckBox.isChecked())
2011-04-24 19:54:48 +00:00
settings.endGroup()
2011-08-22 17:32:18 +00:00
def update_song_usage(self):
2013-02-01 20:52:42 +00:00
"""
Update the song usage
"""
# Only continue when we include the song's text.
if not self.slideTextCheckBox.isChecked():
return
2013-03-01 08:38:25 +00:00
for item in self.service_manager.service_items:
2011-08-22 17:32:18 +00:00
# Trigger Audit requests
2013-02-07 08:42:17 +00:00
Registry().register_function(u'print_service_started', [item[u'service_item']])
2013-01-26 07:39:07 +00:00
def _get_service_manager(self):
"""
Adds the service manager to the class dynamically
"""
if not hasattr(self, u'_service_manager'):
self._service_manager = Registry().get(u'service_manager')
return self._service_manager
service_manager = property(_get_service_manager)
def _get_main_window(self):
"""
Adds the main window to the class dynamically
"""
if not hasattr(self, u'_main_window'):
self._main_window = Registry().get(u'main_window')
return self._main_window
2013-02-05 08:05:28 +00:00
main_window = property(_get_main_window)