openlp/openlp/core/ui/printserviceform.py

410 lines
16 KiB
Python
Raw Normal View History

2011-02-04 18:00:59 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-01 10:10:57 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2013-02-01 20:52:42 +00:00
"""
The actual print service dialog
"""
2011-02-04 18:00:59 +00:00
import datetime
2014-02-23 15:41:09 +00:00
import html
2011-02-04 18:00:59 +00:00
2017-12-28 08:27:44 +00:00
import lxml.html
2018-10-02 04:39:42 +00:00
from PyQt5 import QtCore, QtGui, QtPrintSupport, QtWidgets
2011-02-04 18:00:59 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.applocation import AppLocation
from openlp.core.common.i18n import UiStrings, translate
2017-10-23 22:09:57 +00:00
from openlp.core.common.mixins import RegistryProperties
from openlp.core.common.registry import Registry
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
2018-10-02 04:39:42 +00:00
DEFAULT_CSS = """/*
Edit this file to customize the service order print. Note, that not all CSS
properties are supported. See:
2016-11-16 19:39:23 +00:00
https://doc.qt.io/qt-5/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
}
2017-02-02 21:55:05 +00:00
table.line {}
table.segment {
float: left;
}
td.chord {
font-size: 80%;
}
td.lyrics {
}
"""
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'), QtCore.Qt.WindowSystemMenuHint |
QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
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
self.setup_ui(self)
2011-02-06 14:00:39 +00:00
# Load the settings for the dialog.
self.settings.beginGroup('advanced')
self.slide_text_check_box.setChecked(self.settings.value('print slide text'))
self.page_break_after_text.setChecked(self.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)
self.meta_data_check_box.setChecked(self.settings.value('print file meta data'))
self.notes_check_box.setChecked(self.settings.value('print notes'))
self.zoom_combo_box.setCurrentIndex(self.settings.value('display size'))
self.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)
css_path = 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)
2018-04-17 20:50:27 +00:00
self._add_element('h1', html.escape(self.title_line_edit.text()), html_data.body, class_id='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)
2017-02-02 21:55:05 +00:00
if not self.show_chords_check_box.isChecked():
# Remove chord row and spacing span elements when not printing chords
for chord_row in html_data.find_class('chordrow'):
chord_row.drop_tree()
2017-02-13 20:53:37 +00:00
for spacing_span in html_data.find_class('chordspacing'):
2017-02-02 21:55:05 +00:00
spacing_span.drop_tree()
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():
2018-04-17 20:50:27 +00:00
div = self._add_element('div', parent=html_data.body, class_id='customNotes')
2013-03-07 10:25:27 +00:00
self._add_element(
'span', translate('OpenLP.ServiceManager', 'Service Notes: '), div, class_id='customNotesTitle')
2018-04-17 20:50:27 +00:00
self._add_element('span', html.escape(self.footer_text_edit.toPlainText()), div, class_id='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
"""
2018-04-17 20:50:27 +00:00
div = self._add_element('div', class_id='item', parent=body)
2011-04-24 19:50:28 +00:00
# Add the title of the service item.
2018-04-17 20:50:27 +00:00
item_title = self._add_element('h2', parent=div, class_id='itemTitle')
2013-12-24 15:55:01 +00:00
self._add_element('span', '&nbsp;' + 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
for slide in item.print_slides:
2019-07-31 16:26:25 +00:00
if not verse_def or verse_def != slide['verse'] or verse_html == slide['text']:
2018-04-17 20:50:27 +00:00
text_div = self._add_element('div', parent=div, class_id='itemText')
2019-07-31 16:26:25 +00:00
elif 'chordspacing' not in slide['text']:
2013-08-31 18:17:38 +00:00
self._add_element('br', parent=text_div)
2019-07-31 16:26:25 +00:00
self._add_element('span', slide['text'], text_div)
verse_def = slide['verse']
verse_html = slide['text']
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():
2018-04-17 20:50:27 +00:00
ol = self._add_element('ol', parent=div, class_id='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
footer_html = item.footer_html
footer_html = footer_html.partition('<br>')[2]
if footer_html:
footer_html = html.escape(footer_html.replace('<br>', '\n'))
2019-04-12 19:16:00 +00:00
self._add_element('div', footer_html.replace('\n', '<br>'), parent=div, class_id='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:
2018-04-17 20:50:27 +00:00
p = self._add_element('div', class_id='itemNotes', parent=div)
self._add_element('span', translate('OpenLP.ServiceManager', 'Notes: '), p, class_id='itemNotesTitle')
self._add_element('span', html.escape(item.notes).replace('\n', '<br>'), p, class_id='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
2018-04-17 20:50:27 +00:00
title = self._add_element('div', class_id='media', parent=div)
2013-03-07 10:25:27 +00:00
self._add_element(
2018-04-17 20:50:27 +00:00
'span', translate('OpenLP.ServiceManager', 'Playing time: '), title, class_id='mediaTitle')
self._add_element('span', str(datetime.timedelta(seconds=tme)), title, class_id='mediaText')
2011-04-24 19:50:28 +00:00
2018-04-17 20:50:27 +00:00
def _add_element(self, tag, text=None, parent=None, class_id=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``.
2018-04-17 20:50:27 +00:00
:param class_id: Value for the class attribute
2014-03-17 19:05:55 +00:00
: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)
2018-04-17 20:50:27 +00:00
if class_id is not None:
element.set('class', class_id)
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)
self.settings.beginGroup('advanced')
self.settings.setValue('display size', display)
self.settings.endGroup()
2011-02-19 17:33:24 +00:00
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-06 13:57:57 +00:00
"""
# Save the settings for this dialog.
self.settings.beginGroup('advanced')
self.settings.setValue('print slide text', self.slide_text_check_box.isChecked())
self.settings.setValue('add page break', self.page_break_after_text.isChecked())
self.settings.setValue('print file meta data', self.meta_data_check_box.isChecked())
self.settings.setValue('print notes', self.notes_check_box.isChecked())
self.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']])