openlp/openlp/core/ui/printserviceform.py

394 lines
15 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 #
# --------------------------------------------------------------------------- #
2015-12-31 22:46:06 +00:00
# Copyright (c) 2008-2016 OpenLP Developers #
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
"""
2011-02-04 18:00:59 +00:00
import datetime
import os
2014-02-23 15:41:09 +00:00
import html
import lxml.html
2011-02-04 18:00:59 +00:00
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
2011-02-04 18:00:59 +00:00
2014-03-16 21:25:23 +00:00
from openlp.core.common import Registry, RegistryProperties, Settings, UiStrings, translate
2013-12-13 17:44:05 +00:00
from openlp.core.lib import get_text_file_string
2011-02-19 17:33:24 +00:00
from openlp.core.ui.printservicedialog import Ui_PrintServiceDialog, ZoomSize
2013-10-13 13:51:13 +00:00
from openlp.core.common 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
2015-11-07 00:49:40 +00:00
class PrintServiceForm(QtWidgets.QDialog, Ui_PrintServiceDialog, RegistryProperties):
2013-02-01 20:52:42 +00:00
"""
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
"""
super(PrintServiceForm, self).__init__(Registry().get('main_window'),
2016-01-09 16:26:14 +00:00
QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint)
2015-11-07 00:49:40 +00:00
self.printer = QtPrintSupport.QPrinter()
self.print_dialog = QtPrintSupport.QPrintDialog(self.printer, self)
2011-02-05 19:48:21 +00:00
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()
2013-08-31 18:17:38 +00:00
settings.beginGroup('advanced')
self.slide_text_check_box.setChecked(settings.value('print slide text'))
self.page_break_after_text.setChecked(settings.value('add page break'))
2013-03-07 10:25:27 +00:00
if not self.slide_text_check_box.isChecked():
self.page_break_after_text.setDisabled(True)
2013-08-31 18:17:38 +00:00
self.meta_data_check_box.setChecked(settings.value('print file meta data'))
self.notes_check_box.setChecked(settings.value('print notes'))
self.zoom_combo_box.setCurrentIndex(settings.value('display size'))
2011-02-04 20:10:32 +00:00
settings.endGroup()
2011-02-06 07:16:42 +00:00
# Signals
2013-03-07 10:25:27 +00:00
self.print_button.triggered.connect(self.print_service_order)
self.zoom_out_button.clicked.connect(self.zoom_out)
self.zoom_in_button.clicked.connect(self.zoom_in)
self.zoom_original_button.clicked.connect(self.zoom_original)
self.preview_widget.paintRequested.connect(self.paint_requested)
self.zoom_combo_box.currentIndexChanged.connect(self.display_size_changed)
self.plain_copy.triggered.connect(self.copy_text)
self.html_copy.triggered.connect(self.copy_html_text)
self.slide_text_check_box.stateChanged.connect(self.on_slide_text_check_box_changed)
self.update_preview_text()
def toggle_options(self, checked):
2013-02-01 20:52:42 +00:00
"""
Toggle various options
"""
2013-03-07 10:25:27 +00:00
self.options_widget.setVisible(checked)
2011-02-19 08:36:24 +00:00
if checked:
2013-03-07 10:25:27 +00:00
left = self.options_button.pos().x()
2011-02-19 08:36:24 +00:00
top = self.toolbar.height()
2013-03-07 10:25:27 +00:00
self.options_widget.move(left, top)
self.title_line_edit.setFocus()
2011-02-19 08:36:24 +00:00
else:
2013-03-07 10:25:27 +00:00
self.save_options()
self.update_preview_text()
2011-02-04 18:00:59 +00:00
2013-03-07 10:25:27 +00:00
def update_preview_text(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
"""
2013-08-31 18:17:38 +00:00
html_data = self._add_element('html')
self._add_element('head', parent=html_data)
self._add_element('title', self.title_line_edit.text(), html_data.head)
2015-10-16 16:30:13 +00:00
css_path = os.path.join(AppLocation.get_data_path(), 'serviceprint', '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
2013-08-31 18:17:38 +00:00
self._add_element('style', custom_css, html_data.head, attribute=('type', 'text/css'))
self._add_element('body', parent=html_data)
2013-12-24 15:55:01 +00:00
self._add_element('h1', html.escape(self.title_line_edit.text()), html_data.body, classId='serviceTitle')
2013-02-03 19:23:12 +00:00
for index, item in enumerate(self.service_manager.service_items):
2013-08-31 18:17:38 +00:00
self._add_preview_item(html_data.body, item['service_item'], index)
2011-04-06 19:14:02 +00:00
# Add the custom service notes:
2013-03-07 10:25:27 +00:00
if self.footer_text_edit.toPlainText():
2013-08-31 18:17:38 +00:00
div = self._add_element('div', parent=html_data.body, classId='customNotes')
2013-03-07 10:25:27 +00:00
self._add_element(
2013-08-31 18:17:38 +00:00
'span', translate('OpenLP.ServiceManager', 'Custom Service Notes: '), div, classId='customNotesTitle')
2013-12-24 15:55:01 +00:00
self._add_element('span', html.escape(self.footer_text_edit.toPlainText()), div, classId='customNotesText')
2014-02-23 15:41:09 +00:00
self.document.setHtml(lxml.html.tostring(html_data).decode())
2013-03-07 10:25:27 +00:00
self.preview_widget.updatePreview()
2011-02-06 07:16:42 +00:00
2013-03-07 10:25:27 +00:00
def _add_preview_item(self, body, item, index):
2013-02-01 20:52:42 +00:00
"""
Add a preview item
"""
2013-08-31 18:17:38 +00:00
div = self._add_element('div', classId='item', parent=body)
2011-04-24 19:50:28 +00:00
# Add the title of the service item.
2013-08-31 18:17:38 +00:00
item_title = self._add_element('h2', parent=div, classId='itemTitle')
self._add_element('img', parent=item_title, attribute=('src', item.icon))
2013-12-24 15:55:01 +00:00
self._add_element('span', ' ' + html.escape(item.get_display_title()), item_title)
2013-03-07 10:25:27 +00:00
if self.slide_text_check_box.isChecked():
2011-04-24 19:50:28 +00:00
# Add the text of the service item.
if item.is_text():
verse_def = None
verse_html = None
2011-04-24 19:50:28 +00:00
for slide in item.get_frames():
if not verse_def or verse_def != slide['verseTag'] or verse_html == slide['html']:
2013-08-31 18:17:38 +00:00
text_div = self._add_element('div', parent=div, classId='itemText')
2011-04-24 19:50:28 +00:00
else:
2013-08-31 18:17:38 +00:00
self._add_element('br', parent=text_div)
self._add_element('span', slide['html'], text_div)
verse_def = slide['verseTag']
verse_html = slide['html']
2011-04-24 19:50:28 +00:00
# Break the page before the div element.
2013-03-07 10:25:27 +00:00
if index != 0 and self.page_break_after_text.isChecked():
2013-08-31 18:17:38 +00:00
div.set('class', 'item newPage')
2011-04-24 19:50:28 +00:00
# Add the image names of the service item.
elif item.is_image():
2013-08-31 18:17:38 +00:00
ol = self._add_element('ol', parent=div, classId='imageList')
2011-04-24 19:50:28 +00:00
for slide in range(len(item.get_frames())):
2013-08-31 18:17:38 +00:00
self._add_element('li', item.get_frame_title(slide), ol)
2011-04-24 19:50:28 +00:00
# add footer
foot_text = item.foot_text
2013-08-31 18:17:38 +00:00
foot_text = foot_text.partition('<br>')[2]
2011-04-24 19:50:28 +00:00
if foot_text:
2013-12-24 15:55:01 +00:00
foot_text = html.escape(foot_text.replace('<br>', '\n'))
2013-08-31 18:17:38 +00:00
self._add_element('div', foot_text.replace('\n', '<br>'), parent=div, classId='itemFooter')
2011-04-24 19:50:28 +00:00
# Add service items' notes.
2013-03-07 10:25:27 +00:00
if self.notes_check_box.isChecked():
2011-04-24 19:50:28 +00:00
if item.notes:
2013-08-31 18:17:38 +00:00
p = self._add_element('div', classId='itemNotes', parent=div)
self._add_element('span', translate('OpenLP.ServiceManager', 'Notes: '), p, classId='itemNotesTitle')
2013-12-24 15:55:01 +00:00
self._add_element('span', html.escape(item.notes).replace('\n', '<br>'), p, classId='itemNotesText')
2011-04-24 19:50:28 +00:00
# Add play length of media files.
2013-03-07 10:25:27 +00:00
if item.is_media() and self.meta_data_check_box.isChecked():
2011-04-24 19:50:28 +00:00
tme = item.media_length
if item.end_time > 0:
tme = item.end_time - item.start_time
2013-08-31 18:17:38 +00:00
title = self._add_element('div', classId='media', parent=div)
2013-03-07 10:25:27 +00:00
self._add_element(
2013-08-31 18:17:38 +00:00
'span', translate('OpenLP.ServiceManager', 'Playing time: '), title, classId='mediaTitle')
self._add_element('span', str(datetime.timedelta(seconds=tme)), title, classId='mediaText')
2011-04-24 19:50:28 +00:00
2013-03-07 10:25:27 +00:00
def _add_element(self, tag, text=None, parent=None, classId=None, attribute=None):
2011-04-13 09:19:16 +00:00
"""
2014-03-17 19:05:55 +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.
2014-05-02 06:42:17 +00:00
:param tag: The html tag, e. g. ``'span'``. Defaults to ``None``.
2014-03-17 19:05:55 +00:00
:param text: The text for the tag. Defaults to ``None``.
:param parent: The parent element. Defaults to ``None``.
:param classId: Value for the class attribute
:param attribute: Tuple name/value pair to add as an optional attribute
2011-04-13 09:19:16 +00:00
"""
if text is not None:
2014-02-23 15:41:09 +00:00
element = lxml.html.fragment_fromstring(str(text), create_parent=tag)
2011-04-24 19:50:28 +00:00
else:
2014-02-23 15:41:09 +00:00
element = lxml.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:
2013-08-31 18:17:38 +00:00
element.set('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
2013-03-07 10:25:27 +00:00
def paint_requested(self, printer):
2011-02-06 07:16:42 +00:00
"""
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
2013-03-07 10:25:27 +00:00
def display_size_changed(self, display):
2011-02-19 17:33:24 +00:00
"""
The Zoom Combo box has changed so set up the size.
"""
if display == ZoomSize.Page:
2013-03-07 10:25:27 +00:00
self.preview_widget.fitInView()
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.Width:
2013-03-07 10:25:27 +00:00
self.preview_widget.fitToWidth()
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.OneHundred:
2013-03-07 10:25:27 +00:00
self.preview_widget.fitToWidth()
self.preview_widget.zoomIn(1)
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.SeventyFive:
2013-03-07 10:25:27 +00:00
self.preview_widget.fitToWidth()
self.preview_widget.zoomIn(0.75)
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.Fifty:
2013-03-07 10:25:27 +00:00
self.preview_widget.fitToWidth()
self.preview_widget.zoomIn(0.5)
2011-02-19 17:33:24 +00:00
elif display == ZoomSize.TwentyFive:
2013-03-07 10:25:27 +00:00
self.preview_widget.fitToWidth()
self.preview_widget.zoomIn(0.25)
2012-05-17 15:13:09 +00:00
settings = Settings()
2013-08-31 18:17:38 +00:00
settings.beginGroup('advanced')
settings.setValue('display size', display)
2011-02-19 17:33:24 +00:00
settings.endGroup()
2013-03-07 10:25:27 +00:00
def copy_text(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
2013-08-31 18:17:38 +00:00
clipboard_text = clipboard_text.replace('\u2028', '\n')
clipboard_text = clipboard_text.replace('\u2029', '\n\n')
clipboard_text = clipboard_text.replace('\u2018', '\'')
clipboard_text = clipboard_text.replace('\u2019', '\'')
clipboard_text = clipboard_text.replace('\u201c', '"')
clipboard_text = clipboard_text.replace('\u201d', '"')
clipboard_text = clipboard_text.replace('\u2026', '...')
clipboard_text = clipboard_text.replace('\u2013', '-')
clipboard_text = clipboard_text.replace('\u2014', '-')
# remove the icon from the text
2013-08-31 18:17:38 +00:00
clipboard_text = clipboard_text.replace('\ufffc\xa0', '')
# 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
2013-03-07 10:25:27 +00:00
def copy_html_text(self):
2011-02-18 17:38:39 +00:00
"""
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
2013-03-07 10:25:27 +00:00
def print_service_order(self):
2011-02-06 13:57:57 +00:00
"""
2013-03-07 10:25:27 +00:00
Called, when the *print_button* is clicked. Opens the *print_dialog*.
2011-02-06 13:57:57 +00:00
"""
2015-11-07 00:49:40 +00:00
if not self.print_dialog.exec():
2011-02-06 07:16:42 +00:00
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
2013-03-07 10:25:27 +00:00
def zoom_in(self):
2011-02-06 13:57:57 +00:00
"""
2013-03-07 10:25:27 +00:00
Called when *zoom_in_button* is clicked.
2011-02-06 13:57:57 +00:00
"""
2013-03-07 10:25:27 +00:00
self.preview_widget.zoomIn()
2011-02-19 20:23:23 +00:00
self.zoom -= 0.1
2013-03-07 10:25:27 +00:00
def zoom_out(self):
2011-02-06 13:57:57 +00:00
"""
2013-03-07 10:25:27 +00:00
Called when *zoom_out_button* is clicked.
2011-02-06 13:57:57 +00:00
"""
2013-03-07 10:25:27 +00:00
self.preview_widget.zoomOut()
2011-02-19 20:23:23 +00:00
self.zoom += 0.1
2011-02-06 13:57:57 +00:00
2013-03-07 10:25:27 +00:00
def zoom_original(self):
2011-02-19 17:33:24 +00:00
"""
2013-03-07 10:25:27 +00:00
Called when *zoom_out_button* is clicked.
2011-02-19 17:33:24 +00:00
"""
2013-03-07 10:25:27 +00:00
self.preview_widget.zoomIn(1 + self.zoom)
2011-02-19 20:23:23 +00:00
self.zoom = 0
2011-02-19 17:33:24 +00:00
2013-03-07 10:25:27 +00:00
def update_text_format(self, value):
2011-02-15 18:51:37 +00:00
"""
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
2013-03-07 10:25:27 +00:00
def on_slide_text_check_box_changed(self, state):
2011-04-06 19:14:02 +00:00
"""
2013-03-07 10:25:27 +00:00
Disable or enable the ``page_break_after_text`` checkbox as it should only
be enabled, when the ``slide_text_check_box`` is enabled.
2011-04-06 19:14:02 +00:00
"""
2013-03-07 10:25:27 +00:00
self.page_break_after_text.setDisabled(state == QtCore.Qt.Unchecked)
2011-04-06 19:14:02 +00:00
2013-03-07 10:25:27 +00:00
def save_options(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()
2013-08-31 18:17:38 +00:00
settings.beginGroup('advanced')
settings.setValue('print slide text', self.slide_text_check_box.isChecked())
settings.setValue('add page break', self.page_break_after_text.isChecked())
settings.setValue('print file meta data', self.meta_data_check_box.isChecked())
settings.setValue('print notes', self.notes_check_box.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.
2013-03-07 10:25:27 +00:00
if not self.slide_text_check_box.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
2014-03-20 19:10:31 +00:00
Registry().register_function('print_service_started', [item['service_item']])