openlp/openlp/core/ui/themeform.py

446 lines
23 KiB
Python
Raw Normal View History

2010-10-03 07:42:02 +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 21:34:23 +00:00
"""
The Theme wizard
"""
2010-10-03 07:42:02 +00:00
import logging
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtGui, QtWidgets
2010-10-03 07:42:02 +00:00
from openlp.core.common import is_not_image_file
from openlp.core.common.enum import ServiceItemType
2017-10-07 07:05:07 +00:00
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.theme import BackgroundType
from openlp.core.lib.ui import critical_error_message_box
2018-10-02 04:39:42 +00:00
from openlp.core.ui.themelayoutform import ThemeLayoutForm
from openlp.core.ui.themewizard import Ui_ThemeWizard
2010-10-03 07:42:02 +00:00
log = logging.getLogger(__name__)
2013-02-01 21:34:23 +00:00
2015-11-07 00:49:40 +00:00
class ThemeForm(QtWidgets.QWizard, Ui_ThemeWizard, RegistryProperties):
2010-10-03 07:42:02 +00:00
"""
This is the Theme Import Wizard, which allows easy creation and editing of
OpenLP themes.
2010-10-03 07:42:02 +00:00
"""
2013-08-31 18:17:38 +00:00
log.info('ThemeWizardForm loaded')
2010-10-03 07:42:02 +00:00
def __init__(self, parent):
"""
Instantiate the wizard, and run any extra setup we need to.
2014-03-17 19:05:55 +00:00
:param parent: The QWidget-derived parent of the wizard.
2010-10-03 07:42:02 +00:00
"""
2021-08-25 21:14:19 +00:00
super(ThemeForm, self).__init__(parent, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint |
QtCore.Qt.WindowCloseButtonHint)
self._setup()
def _setup(self):
"""
Set up the class. This method is mocked out by the tests.
"""
self.setup_ui(self)
self.can_update_theme = True
2017-09-26 16:39:13 +00:00
self.temp_background_filename = None
2013-12-26 17:36:00 +00:00
self.theme_layout_form = ThemeLayoutForm(self)
self.customButtonClicked.connect(self.on_custom_1_button_clicked)
self.currentIdChanged.connect(self.on_current_id_changed)
Registry().register_function('theme_line_count', self.update_lines_text)
self.main_area_page.font_name_changed.connect(self.calculate_lines)
self.main_area_page.font_size_changed.connect(self.calculate_lines)
self.main_area_page.line_spacing_changed.connect(self.calculate_lines)
self.main_area_page.is_outline_enabled_changed.connect(self.on_outline_toggled)
self.main_area_page.outline_size_changed.connect(self.calculate_lines)
self.main_area_page.is_shadow_enabled_changed.connect(self.on_shadow_toggled)
self.main_area_page.shadow_size_changed.connect(self.calculate_lines)
self.footer_area_page.font_name_changed.connect(self.update_theme)
self.footer_area_page.font_size_changed.connect(self.update_theme)
2021-08-25 21:14:19 +00:00
self.setOption(QtWidgets.QWizard.HaveHelpButton, True)
self.helpRequested.connect(self.provide_help)
def provide_help(self):
"""
Provide help within the wizard by opening the appropriate page of the openlp manual in the user's browser
"""
QtGui.QDesktopServices.openUrl(QtCore.QUrl("https://manual.openlp.org/themes.html"))
2010-11-08 06:11:04 +00:00
2014-04-02 07:04:12 +00:00
def set_defaults(self):
2010-11-05 19:20:41 +00:00
"""
Set up display at start of theme edit.
"""
self.restart()
2013-12-26 17:36:00 +00:00
self.set_background_page_values()
self.set_main_area_page_values()
self.set_footer_area_page_values()
self.set_alignment_page_values()
self.set_position_page_values()
self.set_preview_page_values()
2010-11-05 19:20:41 +00:00
def calculate_lines(self, *args):
2010-11-23 20:48:55 +00:00
"""
Calculate the number of lines on a page by rendering text
"""
# Do not trigger on start up
if self.currentPage() != self.welcome_page:
2013-12-26 17:36:00 +00:00
self.update_theme()
self.theme_manager.generate_image(self.theme, True)
2010-11-23 20:48:55 +00:00
2013-12-26 17:36:00 +00:00
def update_lines_text(self, lines):
2010-11-23 20:48:55 +00:00
"""
Updates the lines on a page on the wizard
2016-02-27 14:03:52 +00:00
:param lines: then number of lines to be displayed
2010-11-23 20:48:55 +00:00
"""
2013-12-26 17:36:00 +00:00
self.main_line_count_label.setText(
2012-12-29 15:25:29 +00:00
translate('OpenLP.ThemeForm', '(approximately %d lines per slide)') % int(lines))
2010-11-23 20:48:55 +00:00
def resizeEvent(self, event=None):
"""
Rescale the theme preview thumbnail on resize events.
"""
if not event:
event = QtGui.QResizeEvent(self.size(), self.size())
2015-11-07 00:49:40 +00:00
QtWidgets.QWizard.resizeEvent(self, event)
try:
self.display_aspect_ratio = self.renderer.width() / self.renderer.height()
except ZeroDivisionError:
self.display_aspect_ratio = 1
# Make sure we don't resize before the widgets are actually created
if hasattr(self, 'preview_area_layout'):
self.preview_area_layout.set_aspect_ratio(self.display_aspect_ratio)
self.application.process_events()
self.preview_box.set_scale(float(self.preview_box.width()) / self.renderer.width())
def validateCurrentPage(self):
2013-02-01 21:34:23 +00:00
"""
Validate the current page
"""
if self.page(self.currentId()) == self.background_page:
background_image = BackgroundType.to_string(BackgroundType.Image)
background_video = BackgroundType.to_string(BackgroundType.Video)
background_stream = BackgroundType.to_string(BackgroundType.Stream)
if self.background_page.background_type == background_image and \
is_not_image_file(self.background_page.image_path):
QtWidgets.QMessageBox.critical(self, translate('OpenLP.ThemeWizard', 'Background Image Empty'),
translate('OpenLP.ThemeWizard', 'You have not selected a '
'background image. Please select one before continuing.'))
return False
elif self.background_page.background_type == background_video and \
not self.background_page.video_path:
QtWidgets.QMessageBox.critical(self, translate('OpenLP.ThemeWizard', 'Background Video Empty'),
translate('OpenLP.ThemeWizard', 'You have not selected a '
'background video. Please select one before continuing.'))
return False
elif self.background_page.background_type == background_stream and \
not self.background_page.stream_mrl.strip():
QtWidgets.QMessageBox.critical(self, translate('OpenLP.ThemeWizard', 'Background Stream Empty'),
translate('OpenLP.ThemeWizard', 'You have not selected a '
'background stream. Please select one before continuing.'))
return False
else:
return True
return True
2013-12-26 17:36:00 +00:00
def on_current_id_changed(self, page_id):
"""
2012-07-06 06:25:21 +00:00
Detects Page changes and updates as appropriate.
2016-02-27 14:03:52 +00:00
:param page_id: current page number
"""
2013-12-26 17:36:00 +00:00
enabled = self.page(page_id) == self.area_position_page
2015-11-07 00:49:40 +00:00
self.setOption(QtWidgets.QWizard.HaveCustomButton1, enabled)
2013-12-26 17:36:00 +00:00
if self.page(page_id) == self.preview_page:
self.update_theme()
self.resizeEvent()
self.preview_box.clear_slides()
self.preview_box.show()
self.preview_box.generate_preview(self.theme, False, False)
2013-12-26 17:36:00 +00:00
def on_custom_1_button_clicked(self, number):
2011-10-02 16:38:05 +00:00
"""
2011-10-03 16:53:54 +00:00
Generate layout preview and display the form.
2011-10-02 16:38:05 +00:00
"""
2013-12-26 17:36:00 +00:00
self.update_theme()
width = self.renderer.width()
height = self.renderer.height()
pixmap = QtGui.QPixmap(width, height)
pixmap.fill(QtCore.Qt.white)
paint = QtGui.QPainter(pixmap)
2011-09-28 20:43:30 +00:00
paint.setPen(QtGui.QPen(QtCore.Qt.blue, 2))
2022-01-21 21:32:09 +00:00
main_rect = QtCore.QRect(int(self.theme.font_main_x), int(self.theme.font_main_y),
int(self.theme.font_main_width - 1), int(self.theme.font_main_height - 1))
paint.drawRect(main_rect)
2011-09-28 20:43:30 +00:00
paint.setPen(QtGui.QPen(QtCore.Qt.red, 2))
2022-01-21 21:32:09 +00:00
footer_rect = QtCore.QRect(int(self.theme.font_footer_x), int(self.theme.font_footer_y),
int(self.theme.font_footer_width - 1), int(self.theme.font_footer_height - 1))
paint.drawRect(footer_rect)
paint.end()
2015-11-07 00:49:40 +00:00
self.theme_layout_form.exec(pixmap)
2011-10-02 07:52:28 +00:00
def on_outline_toggled(self, is_enabled):
2010-10-16 07:21:24 +00:00
"""
2010-10-16 13:54:57 +00:00
Change state as Outline check box changed
2010-10-16 07:21:24 +00:00
"""
if self.can_update_theme:
self.theme.font_main_outline = is_enabled
2013-12-26 17:36:00 +00:00
self.calculate_lines()
2010-10-11 16:14:36 +00:00
def on_shadow_toggled(self, is_enabled):
2010-10-16 07:21:24 +00:00
"""
Change state as Shadow check box changed
"""
if self.can_update_theme:
self.theme.font_main_shadow = is_enabled
2013-12-26 17:36:00 +00:00
self.calculate_lines()
2010-10-03 07:42:02 +00:00
2015-11-07 00:49:40 +00:00
def exec(self, edit=False):
2010-10-03 07:42:02 +00:00
"""
Run the wizard.
"""
2016-05-20 16:22:06 +00:00
log.debug('Editing theme {name}'.format(name=self.theme.theme_name))
self.temp_background_filename = self.theme.background_source
self.can_update_theme = False
2014-04-02 07:04:12 +00:00
self.set_defaults()
self.can_update_theme = True
2013-12-26 17:36:00 +00:00
self.theme_name_label.setVisible(not edit)
self.theme_name_edit.setVisible(not edit)
2011-01-09 18:51:06 +00:00
self.edit_mode = edit
if edit:
2016-05-20 16:22:06 +00:00
self.setWindowTitle(translate('OpenLP.ThemeWizard', 'Edit Theme - {name}'
).format(name=self.theme.theme_name))
self.next()
else:
self.setWindowTitle(UiStrings().NewTheme)
2015-11-07 00:49:40 +00:00
return QtWidgets.QWizard.exec(self)
2010-10-03 07:42:02 +00:00
2013-03-07 12:59:35 +00:00
def initializePage(self, page_id):
2010-11-08 06:11:04 +00:00
"""
Set up the pages for Initial run through dialog
"""
2016-05-20 16:22:06 +00:00
log.debug('initializePage {page}'.format(page=page_id))
2013-12-26 17:36:00 +00:00
wizard_page = self.page(page_id)
if wizard_page == self.background_page:
self.set_background_page_values()
elif wizard_page == self.main_area_page:
self.set_main_area_page_values()
elif wizard_page == self.footer_area_page:
self.set_footer_area_page_values()
elif wizard_page == self.alignment_page:
self.set_alignment_page_values()
elif wizard_page == self.area_position_page:
self.set_position_page_values()
def set_background_page_values(self):
2010-10-16 07:21:24 +00:00
"""
Handle the display and state of the Background page.
2010-10-16 07:21:24 +00:00
"""
self.background_page.background_type = self.theme.background_type
2013-12-26 17:36:00 +00:00
if self.theme.background_type == BackgroundType.to_string(BackgroundType.Solid):
self.background_page.color = self.theme.background_color
2012-12-29 15:25:29 +00:00
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Gradient):
self.background_page.gradient_start = self.theme.background_start_color
self.background_page.gradient_end = self.theme.background_end_color
self.background_page.gradient_type = self.theme.background_direction
2012-12-29 15:25:29 +00:00
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Image):
self.background_page.image_color = self.theme.background_border_color
if self.theme.background_source and self.theme.background_source.exists():
self.background_page.image_path = self.theme.background_source
else:
self.background_page.image_path = self.theme.background_filename
2016-04-30 15:40:23 +00:00
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Video):
self.background_page.video_color = self.theme.background_border_color
if self.theme.background_source and self.theme.background_source.exists():
self.background_page.video_path = self.theme.background_source
else:
self.background_page.video_path = self.theme.background_filename
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Stream):
self.background_page.stream_color = self.theme.background_border_color
self.background_page.stream_mrl = self.theme.background_source
2010-10-03 07:42:02 +00:00
2013-12-26 17:36:00 +00:00
def set_main_area_page_values(self):
2010-10-16 07:21:24 +00:00
"""
Handle the display and state of the Main Area page.
2010-10-16 07:21:24 +00:00
"""
self.main_area_page.font_name = self.theme.font_main_name
self.main_area_page.font_color = self.theme.font_main_color
self.main_area_page.font_size = self.theme.font_main_size
self.main_area_page.line_spacing = self.theme.font_main_line_adjustment
self.main_area_page.is_outline_enabled = self.theme.font_main_outline
self.main_area_page.outline_color = self.theme.font_main_outline_color
self.main_area_page.outline_size = self.theme.font_main_outline_size
self.main_area_page.is_shadow_enabled = self.theme.font_main_shadow
self.main_area_page.shadow_color = self.theme.font_main_shadow_color
self.main_area_page.shadow_size = self.theme.font_main_shadow_size
self.main_area_page.is_bold = self.theme.font_main_bold
self.main_area_page.is_italic = self.theme.font_main_italics
2013-12-26 17:36:00 +00:00
def set_footer_area_page_values(self):
2010-10-16 07:21:24 +00:00
"""
Handle the display and state of the Footer Area page.
2010-10-16 07:21:24 +00:00
"""
self.footer_area_page.font_name = self.theme.font_footer_name
self.footer_area_page.font_color = self.theme.font_footer_color
self.footer_area_page.is_bold = self.theme.font_footer_bold
self.footer_area_page.is_italic = self.theme.font_footer_italics
self.footer_area_page.font_size = self.theme.font_footer_size
2010-10-11 16:14:36 +00:00
2013-12-26 17:36:00 +00:00
def set_position_page_values(self):
2010-10-16 13:54:57 +00:00
"""
2013-12-26 17:36:00 +00:00
Handle the display and state of the _position page.
2010-10-16 13:54:57 +00:00
"""
2010-11-16 17:33:41 +00:00
# Main Area
self.area_position_page.use_main_default_location = not self.theme.font_main_override
self.area_position_page.main_x = int(self.theme.font_main_x)
self.area_position_page.main_y = int(self.theme.font_main_y)
self.area_position_page.main_height = int(self.theme.font_main_height)
self.area_position_page.main_width = int(self.theme.font_main_width)
2010-11-16 17:33:41 +00:00
# Footer
self.area_position_page.use_footer_default_location = not self.theme.font_footer_override
self.area_position_page.footer_x = int(self.theme.font_footer_x)
self.area_position_page.footer_y = int(self.theme.font_footer_y)
self.area_position_page.footer_height = int(self.theme.font_footer_height)
self.area_position_page.footer_width = int(self.theme.font_footer_width)
2010-10-09 10:59:49 +00:00
2013-12-26 17:36:00 +00:00
def set_alignment_page_values(self):
2010-11-08 06:11:04 +00:00
"""
Handle the display and state of the Alignments page.
2010-11-08 06:11:04 +00:00
"""
self.alignment_page.horizontal_align = self.theme.display_horizontal_align
self.alignment_page.vertical_align = self.theme.display_vertical_align
self.alignment_page.is_transition_enabled = self.theme.display_slide_transition
self.alignment_page.transition_type = self.theme.display_slide_transition_type
self.alignment_page.transition_speed = self.theme.display_slide_transition_speed
2019-12-14 11:44:42 +00:00
self.alignment_page.transition_direction = self.theme.display_slide_transition_direction
self.alignment_page.is_transition_reverse_enabled = self.theme.display_slide_transition_reverse
2010-10-17 18:58:42 +00:00
2013-12-26 17:36:00 +00:00
def set_preview_page_values(self):
"""
Handle the display and state of the Preview page.
"""
self.theme_name_edit.setText(self.theme.theme_name)
self.preview_box.set_theme(self.theme, service_item_type=ServiceItemType.Text)
2010-10-13 19:40:01 +00:00
2013-12-26 17:36:00 +00:00
def update_theme(self):
2010-10-16 07:21:24 +00:00
"""
Update the theme object from the UI for fields not already updated
when the are changed.
"""
if not self.can_update_theme:
return
2013-12-26 17:36:00 +00:00
log.debug('update_theme')
# background page
self.theme.background_type = self.background_page.background_type
if self.theme.background_type == BackgroundType.to_string(BackgroundType.Solid):
self.theme.background_color = self.background_page.color
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Gradient):
self.theme.background_direction = self.background_page.gradient_type
self.theme.background_start_color = self.background_page.gradient_start
self.theme.background_end_color = self.background_page.gradient_end
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Image):
self.theme.background_border_color = self.background_page.image_color
self.theme.background_source = self.background_page.image_path
self.theme.background_filename = self.background_page.image_path
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Video):
self.theme.background_border_color = self.background_page.video_color
self.theme.background_source = self.background_page.video_path
self.theme.background_filename = self.background_page.video_path
elif self.theme.background_type == BackgroundType.to_string(BackgroundType.Stream):
self.theme.background_border_color = self.background_page.stream_color
self.theme.background_source = self.background_page.stream_mrl
self.theme.background_filename = self.background_page.stream_mrl
2010-10-13 19:40:01 +00:00
# main page
self.theme.font_main_name = self.main_area_page.font_name
self.theme.font_main_color = self.main_area_page.font_color
self.theme.font_main_size = self.main_area_page.font_size
self.theme.font_main_line_adjustment = self.main_area_page.line_spacing
2022-01-21 21:32:09 +00:00
self.theme.font_main_outline = self.main_area_page.is_outline_enabled
self.theme.font_main_outline_color = self.main_area_page.outline_color
self.theme.font_main_outline_size = self.main_area_page.outline_size
2022-01-21 21:32:09 +00:00
self.theme.font_main_shadow = self.main_area_page.is_shadow_enabled
self.theme.font_main_shadow_size = self.main_area_page.shadow_size
2022-01-21 21:32:09 +00:00
self.main_area_page.shadow_color = self.theme.font_main_shadow_color
self.theme.font_main_bold = self.main_area_page.is_bold
self.theme.font_main_italics = self.main_area_page.is_italic
2010-10-13 19:40:01 +00:00
# footer page
self.theme.font_footer_name = self.footer_area_page.font_name
self.theme.font_footer_color = self.footer_area_page.font_color
self.theme.font_footer_size = self.footer_area_page.font_size
self.theme.font_footer_bold = self.footer_area_page.is_bold
self.theme.font_footer_italics = self.footer_area_page.is_italic
2019-10-26 10:00:07 +00:00
# position page (main)
self.theme.font_main_override = not self.area_position_page.use_main_default_location
2019-10-26 10:00:07 +00:00
if self.theme.font_main_override:
self.theme.font_main_x = self.area_position_page.main_x
self.theme.font_main_y = self.area_position_page.main_y
self.theme.font_main_height = self.area_position_page.main_height
self.theme.font_main_width = self.area_position_page.main_width
2019-10-26 10:00:07 +00:00
else:
self.theme.set_default_header()
# position page (footer)
self.theme.font_footer_override = not self.area_position_page.use_footer_default_location
2019-10-26 10:00:07 +00:00
if self.theme.font_footer_override:
self.theme.font_footer_x = self.area_position_page.footer_x
self.theme.font_footer_y = self.area_position_page.footer_y
self.theme.font_footer_height = self.area_position_page.footer_height
self.theme.font_footer_width = self.area_position_page.footer_width
2019-10-26 10:00:07 +00:00
else:
self.theme.set_default_footer()
# alignment page
self.theme.display_horizontal_align = self.alignment_page.horizontal_align
self.theme.display_vertical_align = self.alignment_page.vertical_align
self.theme.display_slide_transition = self.alignment_page.is_transition_enabled
self.theme.display_slide_transition_type = self.alignment_page.transition_type
self.theme.display_slide_transition_speed = self.alignment_page.transition_speed
2019-12-14 11:44:42 +00:00
self.theme.display_slide_transition_direction = self.alignment_page.transition_direction
self.theme.display_slide_transition_reverse = self.alignment_page.is_transition_reverse_enabled
2010-10-09 11:20:47 +00:00
2010-11-05 19:20:41 +00:00
def accept(self):
"""
Lets save the theme as Finish has been triggered
2010-11-05 19:20:41 +00:00
"""
2010-11-08 06:11:04 +00:00
# Save the theme name
self.theme.theme_name = self.theme_name_edit.text()
2010-11-18 17:40:20 +00:00
if not self.theme.theme_name:
critical_error_message_box(
translate('OpenLP.ThemeWizard', 'Theme Name Missing'),
2012-12-29 15:25:29 +00:00
translate('OpenLP.ThemeWizard', 'There is no name for this theme. Please enter one.'))
2010-11-05 19:20:41 +00:00
return
2013-08-31 18:17:38 +00:00
if self.theme.theme_name == '-1' or self.theme.theme_name == 'None':
critical_error_message_box(
translate('OpenLP.ThemeWizard', 'Theme Name Invalid'),
2012-12-29 15:25:29 +00:00
translate('OpenLP.ThemeWizard', 'Invalid theme name. Please enter one.'))
2010-11-21 20:45:22 +00:00
return
2017-09-26 16:39:13 +00:00
destination_path = None
2016-04-30 15:40:23 +00:00
if self.theme.background_type == BackgroundType.to_string(BackgroundType.Image) or \
self.theme.background_type == BackgroundType.to_string(BackgroundType.Video):
2017-09-26 16:39:13 +00:00
file_name = self.theme.background_filename.name
destination_path = self.path / self.theme.theme_name / file_name
if self.theme.background_type == BackgroundType.to_string(BackgroundType.Stream):
destination_path = self.theme.background_source
if not self.edit_mode and not self.theme_manager.check_if_theme_exists(self.theme.theme_name):
2011-01-09 19:15:40 +00:00
return
# Set the theme background to the cache location
self.theme.background_filename = destination_path
self.theme_manager.save_theme(self.theme)
self.theme_manager.save_preview(self.theme.theme_name, self.preview_box.save_screenshot())
2015-11-07 00:49:40 +00:00
return QtWidgets.QDialog.accept(self)