Esempio n. 1
0
 def bootstrap_post_set_up(self):
     """
     process the bootstrap post setup request
     """
     self.progress_form = ThemeProgressForm(self)
     self.theme_form = ThemeForm(self)
     self.theme_form.path = self.theme_path
     self.file_rename_form = FileRenameForm()
Esempio n. 2
0
    def test_validate_current_page(self, mocked_setup):
        """
        Test the validateCurrentPage() method
        """
        # GIVEN: An instance of ThemeForm with some mocks
        theme_form = ThemeForm(None)
        theme_form.background_page = MagicMock()
        theme_form.page = MagicMock()

        # WHEN: validateCurrentPage() is called
        result = theme_form.validateCurrentPage()

        # THEN: The right methods were called, and the result is False
        assert result is True
Esempio n. 3
0
    def test_update_lines_text(self, mocked_setup):
        """
        Test the update_lines_text() method
        """
        # GIVEN: A ThemeForm instance with some mocked methods
        theme_form = ThemeForm(None)
        theme_form.main_line_count_label = MagicMock()

        # WHEN: calculate_lines() is called
        theme_form.update_lines_text(10)

        # THEN: The mocks should have been called correctly
        theme_form.main_line_count_label.setText.assert_called_once_with(
            '(approximately 10 lines per slide)')
Esempio n. 4
0
 def test_create_theme_wizard(self, mocked_setup):
     """
     Test creating a ThemeForm instance
     """
     # GIVEN: A ThemeForm class
     # WHEN: An object is created
     # THEN: There should be no problems
     ThemeForm(None)
Esempio n. 5
0
    def test_resize_event_dbze(self, mocked_setup, MockResizeEvent,
                               mocked_resizeEvent):
        """
        Test that the resizeEvent method handles a divide by zero exception correctly
        """
        # GIVEN: A ThemeForm instance with a number of mocked methods
        mocked_event = MagicMock()
        MockResizeEvent.return_value = mocked_event
        theme_form = ThemeForm(None)
        theme_form.size = MagicMock(return_value=1920)
        theme_form.preview_area_layout = MagicMock()
        theme_form.preview_box = MagicMock(**{'width.return_value': 300})
        mocked_renderer = MagicMock(**{
            'width.return_value': 1920,
            'height.return_value': 0
        })
        Registry().remove('renderer')
        Registry().register('renderer', mocked_renderer)

        # WHEN: resizeEvent() is called
        theme_form.resizeEvent()

        # THEN: The correct calls should have been made
        MockResizeEvent.assert_called_once_with(1920, 1920)
        mocked_resizeEvent.assert_called_once_with(theme_form, mocked_event)
        assert mocked_renderer.width.call_count == 2
        mocked_renderer.height.assert_called_once()
        theme_form.preview_area_layout.set_aspect_ratio.assert_called_once_with(
            1)
        theme_form.preview_box.set_scale.assert_called_once_with(
            float(300 / 1920))
Esempio n. 6
0
    def test_initialise_page_alignment(self, mocked_setup):
        """
        Test the initializePage() method with the alignment page
        """
        # GIVEN: An instance of ThemeForm with some mocks
        theme_form = ThemeForm(None)
        theme_form.background_page = MagicMock()
        theme_form.main_area_page = MagicMock()
        theme_form.footer_area_page = MagicMock()
        theme_form.alignment_page = MagicMock()
        theme_form.page = MagicMock(return_value=theme_form.alignment_page)
        theme_form.set_alignment_page_values = MagicMock()

        # WHEN: on_shadow_toggled is called
        theme_form.initializePage(0)

        # THEN: Everything is working right
        theme_form.set_alignment_page_values.assert_called_once()
Esempio n. 7
0
    def test_validate_current_page_with_image(self, mocked_setup,
                                              mocked_is_not_image_file,
                                              mocked_critical):
        """
        Test the validateCurrentPage() method
        """
        # GIVEN: An instance of ThemeForm with some mocks
        theme_form = ThemeForm(None)
        theme_form.background_page = MagicMock(
            background_type=BackgroundType.to_string(BackgroundType.Image),
            image_path=Path('picture.jpg'))
        theme_form.page = MagicMock(return_value=theme_form.background_page)
        mocked_is_not_image_file.return_value = True

        # WHEN: validateCurrentPage() is called
        result = theme_form.validateCurrentPage()

        # THEN: The right methods were called, and the result is False
        mocked_critical.assert_called_once_with(
            theme_form, 'Background Image Empty',
            'You have not selected a background image. '
            'Please select one before continuing.')
        assert result is False
Esempio n. 8
0
    def test_on_shadow_toggled(self, mocked_setup):
        """
        Test the on_shadow_toggled() method
        """
        # GIVEN: An instance of ThemeForm with some mocks
        theme_form = ThemeForm(None)
        theme_form.can_update_theme = True
        theme_form.theme = MagicMock()
        theme_form.calculate_lines = MagicMock()

        # WHEN: on_shadow_toggled is called
        theme_form.on_shadow_toggled(True)

        # THEN: Everything is working right
        assert theme_form.theme.font_main_shadow is True
        theme_form.calculate_lines.assert_called_once()
Esempio n. 9
0
    def test_calculate_lines(self, mocked_setup):
        """
        Test the calculate_lines() method
        """
        # GIVEN: A ThemeForm instance with some mocked methods
        theme_form = ThemeForm(None)
        theme_form.theme = None
        theme_form.welcome_page = None
        theme_form.currentPage = MagicMock()
        theme_form.update_theme = MagicMock()
        mocked_theme_manager = MagicMock()
        Registry().register('theme_manager', mocked_theme_manager)

        # WHEN: calculate_lines() is called
        theme_form.calculate_lines()

        # THEN: The mocks should have been called correctly
        theme_form.currentPage.assert_called_once()
        theme_form.update_theme.assert_called_once()
        mocked_theme_manager.generate_image.assert_called_once_with(None, True)
Esempio n. 10
0
    def test_setup(self):
        """
        Test the _setup method
        """
        # GIVEN: A ThemeForm instance
        with patch('openlp.core.ui.themeform.ThemeForm._setup'):
            theme_form = ThemeForm(None)
        theme_form.setup_ui = MagicMock()
        theme_form.main_area_page = MagicMock()
        theme_form.footer_area_page = MagicMock()

        # WHEN: _setup() is called
        theme_form._setup()

        # THEN: The right calls should have been made
        theme_form.setup_ui.assert_called_once_with(theme_form)
        assert theme_form.can_update_theme is True
        assert theme_form.temp_background_filename is None
        assert isinstance(theme_form.theme_layout_form, ThemeLayoutForm)
        theme_form.main_area_page.font_name_changed.connect.assert_called_once_with(
            theme_form.calculate_lines)
        theme_form.main_area_page.font_size_changed.connect.assert_called_once_with(
            theme_form.calculate_lines)
        theme_form.main_area_page.line_spacing_changed.connect.assert_called_once_with(
            theme_form.calculate_lines)
        theme_form.main_area_page.is_outline_enabled_changed.connect.assert_called_once_with(
            theme_form.on_outline_toggled)
        theme_form.main_area_page.outline_size_changed.connect.assert_called_once_with(
            theme_form.calculate_lines)
        theme_form.main_area_page.is_shadow_enabled_changed.connect.assert_called_once_with(
            theme_form.on_shadow_toggled)
        theme_form.main_area_page.shadow_size_changed.connect.assert_called_once_with(
            theme_form.calculate_lines)
        theme_form.footer_area_page.font_name_changed.connect.assert_called_once_with(
            theme_form.update_theme)
        theme_form.footer_area_page.font_size_changed.connect.assert_called_once_with(
            theme_form.update_theme)
Esempio n. 11
0
class ThemeManager(QtWidgets.QWidget, RegistryBase, Ui_ThemeManager, LogMixin,
                   RegistryProperties):
    """
    Manages the orders of Theme.
    """
    def __init__(self, parent=None):
        """
        Constructor
        """
        super(ThemeManager, self).__init__(parent)
        self.settings_section = 'themes'
        # Variables
        self._theme_list = {}
        self.old_background_image_path = None

    def get_global_theme(self):
        return self.get_theme_data(self.global_theme)

    def get_theme_data(self, theme_name):
        """
        Gets a theme given a name, returns the default theme if missing
        """
        theme = Theme()
        if theme_name in self._theme_list:
            theme = self._theme_list[theme_name]
        else:
            self.log_warning(
                'Missing requested theme data for "{}", using default theme'.
                format(theme_name))
        return theme

    def bootstrap_initialise(self):
        """
        process the bootstrap initialise setup request
        """
        self.setup_ui(self)
        self.global_theme = Settings().value(self.settings_section +
                                             '/global theme')
        self.build_theme_path()

    def bootstrap_post_set_up(self):
        """
        process the bootstrap post setup request
        """
        self.progress_form = ThemeProgressForm(self)
        self.theme_form = ThemeForm(self)
        self.theme_form.path = self.theme_path
        self.file_rename_form = FileRenameForm()

    def bootstrap_completion(self):
        """
        process the bootstrap completion request
        """
        self.upgrade_themes(
        )  # TODO: Can be removed when upgrade path from OpenLP 2.4 no longer needed
        self.load_themes()
        Registry().register_function('theme_update_global',
                                     self.change_global_from_tab)

    def upgrade_themes(self):
        """
        Upgrade the xml files to json.

        :rtype: None
        """
        # Wait for 2 seconds to allow some other things to start processing first
        wait_for(lambda: False, timeout=1)
        xml_file_paths = AppLocation.get_section_data_path('themes').glob(
            '*/*.xml')
        for xml_file_path in xml_file_paths:
            theme_data = get_text_file_string(xml_file_path)
            theme = self._create_theme_from_xml(theme_data, self.theme_path)
            self.save_theme(theme)
            xml_file_path.unlink()

    def build_theme_path(self):
        """
        Set up the theme path variables

        :rtype: None
        """
        self.theme_path = AppLocation.get_section_data_path(
            self.settings_section)
        self.thumb_path = self.theme_path / 'thumbnails'
        create_paths(self.theme_path, self.thumb_path)

    def check_list_state(self, item, field=None):
        """
        If Default theme selected remove delete button.
        Note for some reason a dummy field is required.  Nothing is passed!

        :param field:
        :param item: Service Item to process
        """
        if item is None:
            return
        real_theme_name = item.data(QtCore.Qt.UserRole)
        theme_name = item.text()
        # If default theme restrict actions
        if real_theme_name == theme_name:
            self.delete_toolbar_action.setVisible(True)
        else:
            self.delete_toolbar_action.setVisible(False)

    def context_menu(self, point):
        """
        Build the Right Click Context menu and set state depending on the type of theme.

        :param point: The position of the mouse so the correct item can be found.
        """
        item = self.theme_list_widget.itemAt(point)
        if item is None:
            return
        real_theme_name = item.data(QtCore.Qt.UserRole)
        theme_name = str(item.text())
        visible = real_theme_name == theme_name
        self.delete_action.setVisible(visible)
        self.rename_action.setVisible(visible)
        self.global_action.setVisible(visible)
        self.menu.exec(self.theme_list_widget.mapToGlobal(point))

    def change_global_from_tab(self):
        """
        Change the global theme when it is changed through the Themes settings tab
        """
        self.global_theme = Settings().value(self.settings_section +
                                             '/global theme')
        self.log_debug(
            'change_global_from_tab {text}'.format(text=self.global_theme))
        for count in range(0, self.theme_list_widget.count()):
            # reset the old name
            item = self.theme_list_widget.item(count)
            old_name = item.text()
            new_name = item.data(QtCore.Qt.UserRole)
            if old_name != new_name:
                self.theme_list_widget.item(count).setText(new_name)
            # Set the new name
            if self.global_theme == new_name:
                name = translate('OpenLP.ThemeManager',
                                 '{text} (default)').format(text=new_name)
                self.theme_list_widget.item(count).setText(name)
                self.delete_toolbar_action.setVisible(
                    item not in self.theme_list_widget.selectedItems())

    def change_global_from_screen(self, index=-1):
        """
        Change the global theme when a theme is double clicked upon in the Theme Manager list.

        :param index:
        """
        selected_row = self.theme_list_widget.currentRow()
        for count in range(0, self.theme_list_widget.count()):
            item = self.theme_list_widget.item(count)
            old_name = item.text()
            # reset the old name
            if old_name != item.data(QtCore.Qt.UserRole):
                self.theme_list_widget.item(count).setText(
                    item.data(QtCore.Qt.UserRole))
            # Set the new name
            if count == selected_row:
                self.global_theme = self.theme_list_widget.item(count).text()
                name = translate(
                    'OpenLP.ThemeManager',
                    '{text} (default)').format(text=self.global_theme)
                self.theme_list_widget.item(count).setText(name)
                Settings().setValue(self.settings_section + '/global theme',
                                    self.global_theme)
                Registry().execute('theme_update_global')
                self._push_themes()

    def on_add_theme(self, field=None):
        """
        Loads a new theme with the default settings and then launches the theme editing form for the user to make
        their customisations.
        :param field:
        """
        theme = Theme()
        theme.set_default_header_footer()
        self.theme_form.theme = theme
        self.theme_form.exec()
        self.load_themes()

    def on_rename_theme(self, field=None):
        """
        Renames an existing theme to a new name
        :param field:
        """
        if self._validate_theme_action(
                translate('OpenLP.ThemeManager',
                          'You must select a theme to rename.'),
                translate('OpenLP.ThemeManager', 'Rename Confirmation'),
                translate('OpenLP.ThemeManager', 'Rename {theme_name} theme?'),
                False, False):
            item = self.theme_list_widget.currentItem()
            old_theme_name = item.data(QtCore.Qt.UserRole)
            self.file_rename_form.file_name_edit.setText(old_theme_name)
            if self.file_rename_form.exec():
                new_theme_name = self.file_rename_form.file_name_edit.text()
                if old_theme_name == new_theme_name:
                    return
                if self.check_if_theme_exists(new_theme_name):
                    old_theme_data = self.get_theme_data(old_theme_name)
                    self.clone_theme_data(old_theme_data, new_theme_name)
                    self.delete_theme(old_theme_name)
                    for plugin in State().list_plugins():
                        if plugin.uses_theme(old_theme_name):
                            plugin.rename_theme(old_theme_name, new_theme_name)
                    self.renderer.set_theme(
                        self.get_theme_data(new_theme_name))
                    self.load_themes()

    def on_copy_theme(self, field=None):
        """
        Copies an existing theme to a new name
        :param field:
        """
        item = self.theme_list_widget.currentItem()
        old_theme_name = item.data(QtCore.Qt.UserRole)
        self.file_rename_form.file_name_edit.setText(
            translate('OpenLP.ThemeManager', 'Copy of {name}',
                      'Copy of <theme name>').format(name=old_theme_name))
        if self.file_rename_form.exec(True):
            new_theme_name = self.file_rename_form.file_name_edit.text()
            if self.check_if_theme_exists(new_theme_name):
                theme_data = self.get_theme_data(old_theme_name)
                self.clone_theme_data(theme_data, new_theme_name)

    def clone_theme_data(self, theme_data, new_theme_name):
        """
        Takes a theme and makes a new copy of it as well as saving it.

        :param Theme theme_data: The theme to be used
        :param str new_theme_name: The new theme name of the theme
        :rtype: None
        """
        old_background = None
        if theme_data.background_type == 'image' or theme_data.background_type == 'video':
            old_background = theme_data.background_filename
            theme_data.background_filename = self.theme_path / new_theme_name / theme_data.background_filename.name
        theme_data.theme_name = new_theme_name
        theme_data.extend_image_filename(self.theme_path)
        self.save_theme(theme_data, background_override=old_background)
        self.load_themes()

    def on_edit_theme(self, field=None):
        """
        Loads the settings for the theme that is to be edited and launches the
        theme editing form so the user can make their changes.
        :param field:
        """
        if check_item_selected(
                self.theme_list_widget,
                translate('OpenLP.ThemeManager',
                          'You must select a theme to edit.')):
            item = self.theme_list_widget.currentItem()
            theme = self.get_theme_data(item.data(QtCore.Qt.UserRole))
            if theme.background_type == 'image' or theme.background_type == 'video':
                self.old_background_image_path = theme.background_filename
            self.theme_form.theme = theme
            self.theme_form.exec(True)
            self.old_background_image_path = None
            self.renderer.set_theme(theme)
            self.load_themes()

    def on_delete_theme(self, field=None):
        """
        Delete a theme triggered by the UI.
        :param field:
        """
        if self._validate_theme_action(
                translate('OpenLP.ThemeManager',
                          'You must select a theme to delete.'),
                translate('OpenLP.ThemeManager', 'Delete Confirmation'),
                translate('OpenLP.ThemeManager',
                          'Delete {theme_name} theme?')):
            item = self.theme_list_widget.currentItem()
            theme = item.text()
            row = self.theme_list_widget.row(item)
            self.theme_list_widget.takeItem(row)
            self.delete_theme(theme)
            # self.renderer.set_theme(self.get_theme_data(item.data(QtCore.Qt.UserRole)))
            # As we do not reload the themes, push out the change. Reload the
            # list as the internal lists and events need to be triggered.
            self._push_themes()

    def delete_theme(self, theme):
        """
        Delete a theme.

        :param theme: The theme to delete.
        """
        self._theme_list.pop(theme)
        thumb = '{name}.png'.format(name=theme)
        delete_file(self.theme_path / thumb)
        delete_file(self.thumb_path / thumb)
        try:
            shutil.rmtree(self.theme_path / theme)
        except OSError:
            self.log_exception(
                'Error deleting theme {name}'.format(name=theme))

    def on_export_theme(self, checked=None):
        """
        Export the theme to a zip file

        :param bool checked: Sent by the QAction.triggered signal. It's not used in this method.
        :rtype: None
        """
        item = self.theme_list_widget.currentItem()
        if item is None:
            critical_error_message_box(message=translate(
                'OpenLP.ThemeManager', 'You have not selected a theme.'))
            return
        theme_name = item.data(QtCore.Qt.UserRole)
        export_path, filter_used = \
            FileDialog.getSaveFileName(self.main_window,
                                       translate('OpenLP.ThemeManager',
                                                 'Save Theme - ({name})').format(name=theme_name),
                                       Settings().value(self.settings_section + '/last directory export'),
                                       translate('OpenLP.ThemeManager', 'OpenLP Themes (*.otz)'),
                                       translate('OpenLP.ThemeManager', 'OpenLP Themes (*.otz)'))
        self.application.set_busy_cursor()
        if export_path:
            Settings().setValue(
                self.settings_section + '/last directory export',
                export_path.parent)
            if self._export_theme(export_path.with_suffix('.otz'), theme_name):
                QtWidgets.QMessageBox.information(
                    self, translate('OpenLP.ThemeManager', 'Theme Exported'),
                    translate('OpenLP.ThemeManager',
                              'Your theme has been successfully exported.'))
        self.application.set_normal_cursor()

    def _export_theme(self, theme_path, theme_name):
        """
        Create the zipfile with the theme contents.

        :param Path theme_path: Location where the zip file will be placed
        :param str theme_name: The name of the theme to be exported
        :return: The success of creating the zip file
        :rtype: bool
        """
        try:
            with zipfile.ZipFile(theme_path, 'w') as theme_zip:
                source_path = self.theme_path / theme_name
                for file_path in source_path.iterdir():
                    theme_zip.write(file_path, Path(theme_name,
                                                    file_path.name))
            return True
        except OSError as ose:
            self.log_exception('Export Theme Failed')
            critical_error_message_box(
                translate('OpenLP.ThemeManager', 'Theme Export Failed'),
                translate(
                    'OpenLP.ThemeManager',
                    'The {theme_name} export failed because this error occurred: {err}'
                ).format(theme_name=theme_name, err=ose.strerror))
            if theme_path.exists():
                shutil.rmtree(theme_path, ignore_errors=True)
            return False

    def on_import_theme(self, checked=None):
        """
        Opens a file dialog to select the theme file(s) to import before attempting to extract OpenLP themes from
        those files. This process will only load version 2 themes.

        :param bool checked: Sent by the QAction.triggered signal. It's not used in this method.
        :rtype: None
        """
        file_paths, filter_used = FileDialog.getOpenFileNames(
            self, translate('OpenLP.ThemeManager', 'Select Theme Import File'),
            Settings().value(self.settings_section + '/last directory import'),
            translate('OpenLP.ThemeManager', 'OpenLP Themes (*.otz)'))
        self.log_info('New Themes {file_paths}'.format(file_paths=file_paths))
        if not file_paths:
            return
        self.application.set_busy_cursor()
        new_themes = []
        for file_path in file_paths:
            new_themes.append(self.unzip_theme(file_path, self.theme_path))
        Settings().setValue(self.settings_section + '/last directory import',
                            file_path.parent)
        self.update_preview_images(new_themes)
        self.load_themes()
        self.application.set_normal_cursor()

    def load_first_time_themes(self):
        """
        Imports any themes on start up and makes sure there is at least one theme
        """
        self.application.set_busy_cursor()
        theme_paths = AppLocation.get_files(self.settings_section, '.otz')
        new_themes = []
        for theme_path in theme_paths:
            theme_path = self.theme_path / theme_path
            new_themes.append(self.unzip_theme(theme_path, self.theme_path))
            delete_file(theme_path)
        # No themes have been found so create one
        if not theme_paths:
            theme = Theme()
            theme.theme_name = UiStrings().Default
            self.save_theme(theme)
            Settings().setValue(self.settings_section + '/global theme',
                                theme.theme_name)
            new_themes = [theme.theme_name]
        if new_themes:
            self.update_preview_images(new_themes)
        self.application.set_normal_cursor()

    def load_themes(self):
        """
        Loads the theme lists and triggers updates across the whole system using direct calls or core functions and
        events for the plugins.
        The plugins will call back in to get the real list if they want it.
        """
        self._theme_list.clear()
        self.theme_list_widget.clear()
        files = AppLocation.get_files(self.settings_section, '/*.json')
        # Sort the themes by its name considering language specific
        files.sort(key=lambda file_name: get_locale_key(str(file_name)))
        # now process the file list of png files
        for file in files:
            # check to see file is in theme root directory
            theme_path = self.theme_path / file
            if theme_path.exists():
                text_name = theme_path.stem
                if text_name == self.global_theme:
                    name = translate('OpenLP.ThemeManager',
                                     '{name} (default)').format(name=text_name)
                else:
                    name = text_name
                thumb_path = self.thumb_path / '{name}.png'.format(
                    name=text_name)
                item_name = QtWidgets.QListWidgetItem(name)
                if validate_thumb(theme_path, thumb_path):
                    icon = build_icon(thumb_path)
                else:
                    icon = create_thumb(theme_path, thumb_path)
                item_name.setIcon(icon)
                item_name.setData(QtCore.Qt.UserRole, text_name)
                self.theme_list_widget.addItem(item_name)
                self._theme_list[text_name] = self._get_theme_data(text_name)
        self._push_themes()

    def _push_themes(self):
        """
        Notify listeners that the theme list has been updated
        """
        Registry().execute('theme_update_list', self.get_theme_names())

    def get_theme_names(self):
        """
        Return the list of loaded themes
        """
        return [theme_name for theme_name in self._theme_list]

    def _get_theme_data(self, theme_name):
        """
        Returns a theme object from a JSON file

        :param str theme_name: Name of the theme to load from file
        :return:  The theme object.
        :rtype: Theme
        """
        theme_name = str(theme_name)
        theme_file_path = self.theme_path / theme_name / '{file_name}.json'.format(
            file_name=theme_name)
        theme_data = get_text_file_string(theme_file_path)
        if not theme_data:
            self.log_debug('No theme data - using default theme')
            return Theme()
        return self._create_theme_from_json(theme_data, self.theme_path)

    def over_write_message_box(self, theme_name):
        """
        Display a warning box to the user that a theme already exists

        :param theme_name: Name of the theme.
        :return: Confirm if the theme is to be overwritten.
        """
        ret = QtWidgets.QMessageBox.question(
            self,
            translate('OpenLP.ThemeManager', 'Theme Already Exists'),
            translate(
                'OpenLP.ThemeManager', 'Theme {name} already exists. '
                'Do you want to replace it?').format(name=theme_name),
            defaultButton=QtWidgets.QMessageBox.No)
        return ret == QtWidgets.QMessageBox.Yes

    def unzip_theme(self, file_path, directory_path):
        """
        Unzip the theme, remove the preview file if stored. Generate a new preview file. Check the XML theme version
        and upgrade if necessary.
        :param Path file_path:
        :param Path directory_path:
        """
        self.log_debug('Unzipping theme {name}'.format(name=file_path))
        file_xml = None
        abort_import = True
        json_theme = False
        theme_name = ""
        try:
            with zipfile.ZipFile(file_path) as theme_zip:
                json_file = [
                    name for name in theme_zip.namelist()
                    if os.path.splitext(name)[1].lower() == '.json'
                ]
                if len(json_file) != 1:
                    # TODO: remove XML handling after once the upgrade path from 2.4 is no longer required
                    xml_file = [
                        name for name in theme_zip.namelist()
                        if os.path.splitext(name)[1].lower() == '.xml'
                    ]
                    if len(xml_file) != 1:
                        self.log_error(
                            'Theme contains "{val:d}" theme files'.format(
                                val=len(xml_file)))
                        raise ValidationError
                    xml_tree = ElementTree(
                        element=XML(theme_zip.read(xml_file[0]))).getroot()
                    theme_version = xml_tree.get('version', default=None)
                    if not theme_version or float(theme_version) < 2.0:
                        self.log_error('Theme version is less than 2.0')
                        raise ValidationError
                    theme_name = xml_tree.find('name').text.strip()
                else:
                    new_theme = Theme()
                    new_theme.load_theme(
                        theme_zip.read(json_file[0]).decode("utf-8"))
                    theme_name = new_theme.theme_name
                    json_theme = True
                theme_folder = directory_path / theme_name
                if theme_folder.exists(
                ) and not self.over_write_message_box(theme_name):
                    abort_import = True
                    return
                else:
                    abort_import = False
                for zipped_file in theme_zip.namelist():
                    zipped_file_rel_path = Path(zipped_file)
                    split_name = zipped_file_rel_path.parts
                    if split_name[-1] == '' or len(split_name) == 1:
                        # is directory or preview file
                        continue
                    full_name = directory_path / zipped_file_rel_path
                    create_paths(full_name.parent)
                    if zipped_file_rel_path.suffix.lower(
                    ) == '.xml' or zipped_file_rel_path.suffix.lower(
                    ) == '.json':
                        file_xml = str(theme_zip.read(zipped_file), 'utf-8')
                        with full_name.open('w', encoding='utf-8') as out_file:
                            out_file.write(file_xml)
                    else:
                        with full_name.open('wb') as out_file:
                            out_file.write(theme_zip.read(zipped_file))
        except (OSError, ValidationError, zipfile.BadZipFile):
            self.log_exception('Importing theme from zip failed {name}'.format(
                name=file_path))
            critical_error_message_box(
                translate('OpenLP.ThemeManager', 'Import Error'),
                translate(
                    'OpenLP.ThemeManager',
                    'There was a problem importing {file_name}.\n\nIt is corrupt, '
                    'inaccessible or not a valid theme.').format(
                        file_name=file_path))
        finally:
            if not abort_import:
                # As all files are closed, we can create the Theme.
                if file_xml:
                    if json_theme:
                        self._create_theme_from_json(file_xml, self.theme_path)
                    else:
                        self._create_theme_from_xml(file_xml, self.theme_path)
                return theme_name
            else:
                return None

    def check_if_theme_exists(self, theme_name):
        """
        Check if theme already exists and displays error message

        :param str theme_name:  Name of the Theme to test
        :return: True or False if theme exists
        :rtype: bool
        """
        if (self.theme_path / theme_name).exists():
            critical_error_message_box(
                translate('OpenLP.ThemeManager', 'Validation Error'),
                translate('OpenLP.ThemeManager',
                          'A theme with this name already exists.'))
            return False
        return True

    def save_theme(self, theme, image=None, background_override=None):
        """
        Writes the theme to the disk and including the background image and thumbnail if necessary

        :param Theme theme: The theme data object.
        :param image: The theme thumbnail. Optionally.
        :param background_override: Background to use rather than background_source. Optionally.
        :rtype: None
        """
        name = theme.theme_name
        theme_pretty = theme.export_theme(self.theme_path)
        theme_dir = self.theme_path / name
        create_paths(theme_dir)
        theme_path = theme_dir / '{file_name}.json'.format(file_name=name)
        try:
            theme_path.write_text(theme_pretty)
        except OSError:
            self.log_exception('Saving theme to file failed')
        if theme.background_source and theme.background_filename:
            background_file = background_override
            # Use theme source image if override doesn't exist
            if not background_file or not background_file.exists():
                background_file = theme.background_source
            if self.old_background_image_path and theme.background_filename != self.old_background_image_path:
                delete_file(self.old_background_image_path)
            if not background_file.exists():
                self.log_warning(
                    'Background does not exist, retaining cached background')
            elif background_file != theme.background_filename:
                try:
                    shutil.copyfile(background_file, theme.background_filename)
                except OSError:
                    self.log_exception('Failed to save theme image')
        if image:
            sample_path_name = self.theme_path / '{file_name}.png'.format(
                file_name=name)
            if sample_path_name.exists():
                sample_path_name.unlink()
            image.save(str(sample_path_name), 'png')
            thumb_path = self.thumb_path / '{name}.png'.format(name=name)
            create_thumb(sample_path_name, thumb_path, False)
        else:
            self.update_preview_images([name])

    def save_preview(self, theme_name, preview_pixmap):
        """
        Save the preview QPixmap object to a file
        """
        sample_path_name = self.theme_path / '{file_name}.png'.format(
            file_name=theme_name)
        if sample_path_name.exists():
            sample_path_name.unlink()
        preview_pixmap.save(str(sample_path_name), 'png')
        thumb_path = self.thumb_path / '{name}.png'.format(name=theme_name)
        create_thumb(sample_path_name, thumb_path, False)

    def update_preview_images(self, theme_name_list=None):
        """
        Called to update the themes' preview images.

        :param theme_name_list: A list of theme names in the theme data folder
        """
        theme_name_list = theme_name_list or self.get_theme_names()
        self.progress_form.theme_list = theme_name_list
        self.progress_form.show()
        for theme_name in theme_name_list:
            theme_data = self._get_theme_data(theme_name)
            preview_pixmap = self.progress_form.get_preview(
                theme_name, theme_data)
            self.save_preview(theme_name, preview_pixmap)
        self.progress_form.close()
        self.load_themes()

    def generate_image(self, theme_data, force_page=False):
        """
        Call the renderer to build a Sample Image

        :param theme_data: The theme to generated a preview for.
        :param force_page: Flag to tell message lines per page need to be generated.
        :rtype: QtGui.QPixmap
        """
        return self.renderer.generate_preview(theme_data, force_page)

    @staticmethod
    def _create_theme_from_xml(theme_xml, image_path):
        """
        Return a theme object using information parsed from XML

        :param theme_xml: The Theme data object.
        :param Path image_path: Where the theme image is stored
        :return: Theme data.
        :rtype: Theme
        """
        theme = Theme()
        theme.parse(theme_xml)
        theme.extend_image_filename(image_path)
        return theme

    def _create_theme_from_json(self, theme_json, image_path):
        """
        Return a theme object using information parsed from JSON

        :param theme_json: The Theme data object.
        :param Path image_path: Where the theme image is stored
        :return: Theme data.
        :rtype: Theme
        """
        theme = Theme()
        theme.load_theme(theme_json, self.theme_path)
        theme.extend_image_filename(image_path)
        return theme

    def _validate_theme_action(self,
                               select_text,
                               confirm_title,
                               confirm_text,
                               test_plugin=True,
                               confirm=True):
        """
        Check to see if theme has been selected and the destructive action is allowed.

        :param select_text: Text for message box if no item selected.
        :param confirm_title: Confirm message title to be displayed.
        :param confirm_text: Confirm message text to be displayed.
        :param test_plugin: Do we check the plugins for theme usage.
        :param confirm: Do we display a confirm box before run checks.
        :return: True or False depending on the validity.
        """
        self.global_theme = Settings().value(self.settings_section +
                                             '/global theme')
        if check_item_selected(self.theme_list_widget, select_text):
            item = self.theme_list_widget.currentItem()
            theme = item.text()
            # confirm deletion
            if confirm:
                answer = QtWidgets.QMessageBox.question(
                    self,
                    confirm_title,
                    confirm_text.format(theme_name=theme),
                    defaultButton=QtWidgets.QMessageBox.No)
                if answer == QtWidgets.QMessageBox.No:
                    return False
            # should be the same unless default
            if theme != item.data(QtCore.Qt.UserRole):
                critical_error_message_box(message=translate(
                    'OpenLP.ThemeManager',
                    'You are unable to delete the default theme.'))
                return False
            # check for use in the system else where.
            if test_plugin:
                plugin_usage = ""
                for plugin in State().list_plugins():
                    used_count = plugin.uses_theme(theme)
                    if used_count:
                        plugin_usage = "{plug}{text}".format(
                            plug=plugin_usage,
                            text=(translate(
                                'OpenLP.ThemeManager',
                                '{count} time(s) by {plugin}').format(
                                    name=used_count, plugin=plugin.name)))
                        plugin_usage = "{text}\n".format(text=plugin_usage)
                if plugin_usage:
                    critical_error_message_box(
                        translate('OpenLP.ThemeManager',
                                  'Unable to delete theme'),
                        translate('OpenLP.ThemeManager',
                                  'Theme is currently used \n\n{text}').format(
                                      text=plugin_usage))

                    return False
            return True
        return False
Esempio n. 12
0
    def test_set_defaults(self, mocked_setup):
        """
        Test that the right methods are called by the set_defaults() method
        """
        # GIVEN: A ThemeForm instance with mocked methods
        theme_form = ThemeForm(None)
        theme_form.restart = MagicMock()
        theme_form.set_background_page_values = MagicMock()
        theme_form.set_main_area_page_values = MagicMock()
        theme_form.set_footer_area_page_values = MagicMock()
        theme_form.set_alignment_page_values = MagicMock()
        theme_form.set_position_page_values = MagicMock()
        theme_form.set_preview_page_values = MagicMock()

        # WHEN: set_defaults() is called
        theme_form.set_defaults()

        # THEN: all the mocks are called
        theme_form.restart.assert_called_once()
        theme_form.set_background_page_values.assert_called_once()
        theme_form.set_main_area_page_values.assert_called_once()
        theme_form.set_footer_area_page_values.assert_called_once()
        theme_form.set_alignment_page_values.assert_called_once()
        theme_form.set_position_page_values.assert_called_once()
        theme_form.set_preview_page_values.assert_called_once()
Esempio n. 13
0
    def test_on_current_id_changed_preview(self, mocked_setup):
        """
        Test the on_current_id_changed() method
        """
        # GIVEN: An instance of ThemeForm with some mocks
        theme_form = ThemeForm(None)
        theme_form.theme = 'my fake theme'
        theme_form.area_position_page = MagicMock()
        theme_form.preview_page = MagicMock()
        theme_form.page = MagicMock(return_value=theme_form.preview_page)
        theme_form.update_theme = MagicMock()
        theme_form.preview_box = MagicMock(**{'width.return_value': 300})
        theme_form.preview_area_layout = MagicMock()
        theme_form.resizeEvent = MagicMock()
        mocked_renderer = MagicMock(**{
            'width.return_value': 1920,
            'height.return_value': 0
        })
        Registry().remove('renderer')
        Registry().register('renderer', mocked_renderer)

        # WHEN: on_current_id_changed() is called
        theme_form.on_current_id_changed(1)

        # THEN: The right options should have been set
        theme_form.update_theme.assert_called_once()
        theme_form.preview_box.set_theme.assert_called_once_with(
            'my fake theme')
        theme_form.preview_box.clear_slides.assert_called_once()
        theme_form.preview_box.set_scale.assert_called_once_with(
            float(300 / 1920))
        theme_form.preview_area_layout.set_aspect_ratio(16 / 9)
        theme_form.resizeEvent.assert_called_once()
        theme_form.preview_box.show.assert_called_once()
        theme_form.preview_box.generate_preview.assert_called_once_with(
            'my fake theme', False, False)