openlp/openlp/core/ui/printserviceform.py

365 lines
14 KiB
Python
Raw Normal View History

2011-02-04 18:00:59 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2011 Raoul Snyman #
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
2011-03-24 19:04:02 +00:00
# Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, Armin Köhler, #
# Andreas Preikschat, Mattias Põldaru, Christian Richter, Philip Ridout, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Frode Woldsund #
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 #
###############################################################################
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
2011-04-13 09:19:16 +00:00
from openlp.core.lib import translate, get_text_file_string
2011-02-15 18:51:37 +00:00
from openlp.core.lib.ui import UiStrings
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 {
font-weight:600;
font-size:x-large;
color:black;
}
.itemTitle {
font-weight:600;
font-size:large;
color:black;
}
.itemText {
color:black;
}
.itemFooter {
font-size:8px;
color:black;
}
.itemNotesTitle {
font-weight:bold;
font-size:12px;
color:black;
}
.itemNotesText {
font-size:11px;
color:black;
}
.customNotesTitle {
font-weight:bold;
font-size:11px;
color:black;
}
.customNotesText {
font-size:11px;
color:black;
}
"""
2011-02-04 18:00:59 +00:00
2011-02-19 17:33:24 +00:00
class PrintServiceForm(QtGui.QDialog, Ui_PrintServiceDialog):
2011-02-15 18:51:37 +00:00
2011-03-19 10:08:05 +00:00
def __init__(self, mainWindow, serviceManager):
2011-02-04 18:00:59 +00:00
"""
Constructor
"""
2011-03-19 10:08:05 +00:00
QtGui.QDialog.__init__(self, mainWindow)
self.mainWindow = mainWindow
2011-02-06 13:57:57 +00:00
self.serviceManager = serviceManager
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.
2011-02-04 20:10:32 +00:00
settings = QtCore.QSettings()
settings.beginGroup(u'advanced')
2011-02-19 08:36:24 +00:00
self.slideTextCheckBox.setChecked(settings.value(
2011-02-04 20:10:32 +00:00
u'print slide text', QtCore.QVariant(False)).toBool())
2011-04-06 19:14:02 +00:00
self.pageBreakAfterText.setChecked(settings.value(
u'enable page break', QtCore.QVariant(False)).toBool())
if not self.slideTextCheckBox.isChecked():
self.pageBreakAfterText.setDisabled(True)
2011-02-19 08:36:24 +00:00
self.metaDataCheckBox.setChecked(settings.value(
2011-02-04 20:10:32 +00:00
u'print file meta data', QtCore.QVariant(False)).toBool())
2011-02-19 08:36:24 +00:00
self.notesCheckBox.setChecked(settings.value(
2011-02-04 20:10:32 +00:00
u'print notes', QtCore.QVariant(False)).toBool())
2011-02-19 20:23:23 +00:00
self.zoomComboBox.setCurrentIndex(settings.value(
u'display size', QtCore.QVariant(0)).toInt()[0])
2011-02-04 20:10:32 +00:00
settings.endGroup()
2011-02-06 07:16:42 +00:00
# Signals
2011-02-19 17:33:24 +00:00
QtCore.QObject.connect(self.printButton,
QtCore.SIGNAL(u'triggered()'), self.printServiceOrder)
QtCore.QObject.connect(self.closeButton,
QtCore.SIGNAL(u'triggered()'), self.accept)
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)
2011-02-19 20:23:23 +00:00
QtCore.QObject.connect(self.plainCopy,
QtCore.SIGNAL(u'triggered()'), self.copyText)
QtCore.QObject.connect(self.htmlCopy,
QtCore.SIGNAL(u'triggered()'), self.copyHtmlText)
2011-04-06 19:14:02 +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):
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-06 19:14:02 +00:00
html_data = html.fromstring(
u'<title>%s</title>' % unicode(self.titleLineEdit.text()))
css_path = os.path.join(
AppLocation.get_data_path(), u'servicePrint.css')
if not os.path.isfile(css_path):
# Create default css file.
css_file = open(css_path, u'w')
css_file.write(DEFAULT_CSS)
css_file.close()
2011-04-13 09:19:16 +00:00
custom_css = get_text_file_string(css_path)
style = self._addChildToParent(u'style', custom_css, html_data.head)
style.set(u'type', u'text/css')
self._addChildToParent(u'body', parent=html_data)
service_title = self._addChildToParent(
u'span', unicode(self.titleLineEdit.text()), html_data.body)
2011-04-07 08:45:49 +00:00
service_title.set(u'class', u'serviceTitle')
2011-04-06 19:14:02 +00:00
for index, item in enumerate(self.serviceManager.serviceItems):
2011-02-04 18:00:59 +00:00
item = item[u'service_item']
2011-04-13 09:19:16 +00:00
div = self._addChildToParent(u'div', parent=html_data.body)
2011-02-04 18:00:59 +00:00
# Add the title of the service item.
2011-04-13 09:19:16 +00:00
item_title = self._addChildToParent(u'h2', parent=div)
2011-04-07 08:45:49 +00:00
item_title.set(u'class', u'itemTitle')
2011-04-13 09:19:16 +00:00
icon = self._addChildToParent(u'img', parent=item_title)
2011-04-06 19:14:02 +00:00
icon.set(u'src', item.icon)
2011-04-13 09:19:16 +00:00
self._fromstring(
u'<span> %s</span>' % item.get_display_title(), item_title)
2011-02-19 08:36:24 +00:00
if self.slideTextCheckBox.isChecked():
2011-04-06 19:14:02 +00:00
# Add the text of the service item.
2011-02-04 18:00:59 +00:00
if item.is_text():
2011-04-06 19:14:02 +00:00
verse_def = None
2011-02-04 18:00:59 +00:00
for slide in item.get_frames():
2011-04-06 19:14:02 +00:00
if not verse_def or verse_def != slide[u'verseTag']:
2011-04-13 09:19:16 +00:00
p = self._addChildToParent(u'p', parent=div)
2011-04-07 08:45:49 +00:00
p.set(u'class', u'itemText')
2011-02-15 18:51:37 +00:00
else:
2011-04-13 09:19:16 +00:00
self._addChildToParent(u'br', parent=p)
self._fromstring(u'<span>%s</span>' % slide[u'html'], p)
2011-04-06 19:14:02 +00:00
verse_def = slide[u'verseTag']
# Break the page before the div element.
if index != 0 and self.pageBreakAfterText.isChecked():
div.set(u'style', u'page-break-before:always')
# Add the image names of the service item.
2011-02-04 18:00:59 +00:00
elif item.is_image():
2011-04-13 09:19:16 +00:00
ol = self._addChildToParent(u'ol', parent=div)
2011-02-04 18:00:59 +00:00
for slide in range(len(item.get_frames())):
2011-04-13 09:19:16 +00:00
self._addChildToParent(u'li', item.get_frame_title(slide), ol)
2011-04-06 19:14:02 +00:00
# add footer
2011-02-04 18:00:59 +00:00
if item.foot_text:
2011-04-13 09:19:16 +00:00
p = self._fromstring(item.foot_text, div)
2011-04-07 08:45:49 +00:00
p.set(u'class', u'itemFooter')
2011-02-04 18:00:59 +00:00
# Add service items' notes.
2011-02-19 08:36:24 +00:00
if self.notesCheckBox.isChecked():
2011-02-04 18:00:59 +00:00
if item.notes:
2011-04-13 09:19:16 +00:00
p = self._addChildToParent(u'p', parent=div)
title = self._addChildToParent(u'span', unicode(
translate('OpenLP.ServiceManager', 'Notes:')), p)
2011-04-07 08:45:49 +00:00
title.set(u'class', u'itemNotesTitle')
2011-04-13 09:19:16 +00:00
text = self._fromstring(u'<span> %s</span>' %
item.notes.replace(u'\n', u'<br />'), p)
2011-04-07 08:45:49 +00:00
text.set(u'class', u'itemNotesText')
2011-02-04 18:00:59 +00:00
# Add play length of media files.
2011-02-19 08:36:24 +00:00
if item.is_media() and self.metaDataCheckBox.isChecked():
2011-03-19 12:40:33 +00:00
tme = item.media_length
if item.end_time > 0:
tme = item.end_time - item.start_time
2011-04-13 09:19:16 +00:00
title = self._fromstring(u'<p><strong>%s</strong> </p>' %
translate('OpenLP.ServiceManager', 'Playing time:'), div)
self._fromstring(u'<span>%s</span>' %
unicode(datetime.timedelta(seconds=tme)), title)
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():
footer_title = self._addChildToParent(u'span', translate(
'OpenLP.ServiceManager', u'Custom Service Notes:'), div)
footer_title.set(u'class', u'customNotesTitle')
footer_text = self._addChildToParent(u'span',
u' %s' % self.footerTextEdit.toPlainText(), div)
footer_text.set(u'class', 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-13 09:19:16 +00:00
def _addChildToParent(self, tag, text=None, parent=None):
"""
Creates a html element. If ``text`` is given, the element's text will
set and if a ``parent`` is given, the element is appended.
"""
element = html.Element(tag)
if text is not None:
element.text = text
if parent is not None:
parent.append(element)
return element
def _fromstring(self, string, parent):
"""
This is used to create a child html element from a string.
"""
element = html.fromstring(string)
parent.append(element)
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)
2011-02-19 17:33:24 +00:00
settings = QtCore.QSettings()
settings.beginGroup(u'advanced')
2011-02-20 14:25:14 +00:00
settings.setValue(u'display size', QtCore.QVariant(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
"""
self.mainWindow.clipboard.setText(
2011-03-19 10:08:05 +00:00
self.document.toPlainText())
2011-02-18 17:38:39 +00:00
def copyHtmlText(self):
"""
Copies the display text to the clipboard as Html
"""
self.mainWindow.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-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)
else:
self.copyTextButton.setText(UiStrings.CopyToText)
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.
settings = QtCore.QSettings()
settings.beginGroup(u'advanced')
settings.setValue(u'print slide text',
2011-02-19 08:36:24 +00:00
QtCore.QVariant(self.slideTextCheckBox.isChecked()))
2011-04-06 19:14:02 +00:00
settings.setValue(u'enable page break',
QtCore.QVariant(self.pageBreakAfterText.isChecked()))
2011-02-04 20:10:32 +00:00
settings.setValue(u'print file meta data',
2011-02-19 08:36:24 +00:00
QtCore.QVariant(self.metaDataCheckBox.isChecked()))
2011-02-04 20:10:32 +00:00
settings.setValue(u'print notes',
2011-02-19 08:36:24 +00:00
QtCore.QVariant(self.notesCheckBox.isChecked()))
2011-02-04 20:10:32 +00:00
settings.endGroup()