From bb507acf3fcab9a0ecd1b05d554dc40467cc0205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20M=C3=A1t=C3=A9?= Date: Fri, 11 Feb 2022 19:13:50 +0000 Subject: [PATCH] Option to hide custom formatting tag content in main window --- openlp/core/display/render.py | 7 ++-- openlp/core/lib/formattingtags.py | 36 ++++++++++--------- openlp/core/ui/formattingtagcontroller.py | 8 +++-- openlp/core/ui/formattingtagdialog.py | 15 +++++--- openlp/core/ui/formattingtagform.py | 26 ++++++++++++-- openlp/plugins/songs/lib/openlyricsxml.py | 5 ++- tests/openlp_core/display/test_render.py | 6 ++-- .../openlp_core/ui/test_formattingtagsform.py | 3 ++ .../songs/test_openlyricsxml.py | 6 ++-- 9 files changed, 78 insertions(+), 34 deletions(-) diff --git a/openlp/core/display/render.py b/openlp/core/display/render.py index 82091a84a..f127bf84e 100644 --- a/openlp/core/display/render.py +++ b/openlp/core/display/render.py @@ -157,8 +157,11 @@ def remove_tags(text, can_remove_chords=False): text = text.replace('', '') text = text.replace('', '') for tag in FormattingTags.get_html_tags(): - text = text.replace(tag['start tag'], '') - text = text.replace(tag['end tag'], '') + if tag['hidden']: + text = re.sub(r'' + tag['start tag'] + ".*?" + tag['end tag'], '', text) + else: + text = text.replace(tag['start tag'], '') + text = text.replace(tag['end tag'], '') # Remove ChordPro tags if can_remove_chords: text = remove_chords(text) diff --git a/openlp/core/lib/formattingtags.py b/openlp/core/lib/formattingtags.py index 67a228a69..c28d3f8a3 100644 --- a/openlp/core/lib/formattingtags.py +++ b/openlp/core/lib/formattingtags.py @@ -64,90 +64,91 @@ class FormattingTags(object): 'start tag': '{r}', 'start html': '', 'end tag': '{/r}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Black'), 'start tag': '{b}', 'start html': '', 'end tag': '{/b}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Blue'), 'start tag': '{bl}', 'start html': '', 'end tag': '{/bl}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Yellow'), 'start tag': '{y}', 'start html': '', 'end tag': '{/y}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Green'), 'start tag': '{g}', 'start html': '', 'end tag': '{/g}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Pink'), 'start tag': '{pk}', 'start html': '', 'end tag': '{/pk}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Orange'), 'start tag': '{o}', 'start html': '', 'end tag': '{/o}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Purple'), 'start tag': '{pp}', 'start html': '', 'end tag': '{/pp}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'White'), 'start tag': '{w}', 'start html': '', 'end tag': '{/w}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Superscript'), 'start tag': '{su}', 'start html': '', 'end tag': '{/su}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Subscript'), 'start tag': '{sb}', 'start html': '', 'end tag': '{/sb}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Paragraph'), 'start tag': '{p}', 'start html': '

', 'end tag': '{/p}', 'end html': '

', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Bold'), 'start tag': '{st}', 'start html': '', 'end tag': '{/st}', 'end html': '', - 'protected': True, 'temporary': False + 'protected': True, 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Italics'), 'start tag': '{it}', 'start html': '', 'end tag': '{/it}', - 'end html': '', 'protected': True, 'temporary': False + 'end html': '', 'protected': True, 'temporary': False, + 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Underline'), 'start tag': '{u}', 'start html': '', 'end tag': '{/u}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': translate('OpenLP.FormattingTags', 'Break'), 'start tag': '{br}', 'start html': '
', 'end tag': '', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }] # Append the base tags. FormattingTags.add_html_tags(base_tags) @@ -156,6 +157,9 @@ class FormattingTags(object): # If we have some user ones added them as well if user_expands_string: user_tags = json.loads(user_expands_string) + for tag in user_tags: + if 'hidden' not in tag.keys(): + tag['hidden'] = False FormattingTags.add_html_tags(user_tags) @staticmethod diff --git a/openlp/core/ui/formattingtagcontroller.py b/openlp/core/ui/formattingtagcontroller.py index 54f377dc0..249de5774 100644 --- a/openlp/core/ui/formattingtagcontroller.py +++ b/openlp/core/ui/formattingtagcontroller.py @@ -52,7 +52,7 @@ class FormattingTagController(object): self.protected_tags = [tag for tag in FormattingTags.html_expands if tag.get('protected')] self.custom_tags = [] - def validate_for_save(self, desc, tag, start_html, end_html): + def validate_for_save(self, desc, tag, start_html, end_html, hidden): """ Validate a custom tag and add to the tags array if valid.. @@ -68,6 +68,9 @@ class FormattingTagController(object): `end_html` The end html tag. + `hidden` + option to hide tag content from main window. + """ for line_number, html1 in enumerate(self.protected_tags): if self._strip(html1['start tag']) == tag: @@ -86,7 +89,8 @@ class FormattingTagController(object): 'end tag': '{{/{tag}}}'.format(tag=tag), 'end html': end_html, 'protected': False, - 'temporary': False + 'temporary': False, + 'hidden': hidden } self.custom_tags.append(tag) diff --git a/openlp/core/ui/formattingtagdialog.py b/openlp/core/ui/formattingtagdialog.py index 8fa5fc021..63463650a 100644 --- a/openlp/core/ui/formattingtagdialog.py +++ b/openlp/core/ui/formattingtagdialog.py @@ -38,7 +38,7 @@ class Ui_FormattingTagDialog(object): """ formatting_tag_dialog.setObjectName('formatting_tag_dialog') formatting_tag_dialog.setWindowIcon(UiIcons().main_icon) - formatting_tag_dialog.resize(725, 548) + formatting_tag_dialog.resize(835, 548) self.list_data_grid_layout = QtWidgets.QVBoxLayout(formatting_tag_dialog) self.list_data_grid_layout.setContentsMargins(8, 8, 8, 8) self.list_data_grid_layout.setObjectName('list_data_grid_layout') @@ -72,7 +72,7 @@ class Ui_FormattingTagDialog(object): self.tag_table_widget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.tag_table_widget.setCornerButtonEnabled(False) self.tag_table_widget.setObjectName('tag_table_widget') - self.tag_table_widget.setColumnCount(4) + self.tag_table_widget.setColumnCount(5) self.tag_table_widget.setRowCount(0) self.tag_table_widget.horizontalHeader().setStretchLastSection(True) item = QtWidgets.QTableWidgetItem() @@ -83,6 +83,8 @@ class Ui_FormattingTagDialog(object): self.tag_table_widget.setHorizontalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tag_table_widget.setHorizontalHeaderItem(3, item) + item = QtWidgets.QTableWidgetItem() + self.tag_table_widget.setHorizontalHeaderItem(4, item) self.list_data_grid_layout.addWidget(self.tag_table_widget) self.edit_button_layout = QtWidgets.QHBoxLayout() self.new_button = QtWidgets.QPushButton(formatting_tag_dialog) @@ -126,6 +128,9 @@ class Ui_FormattingTagDialog(object): self.tag_table_widget.horizontalHeaderItem(1).setText(translate('OpenLP.FormattingTagDialog', 'Tag')) self.tag_table_widget.horizontalHeaderItem(2).setText(translate('OpenLP.FormattingTagDialog', 'Start HTML')) self.tag_table_widget.horizontalHeaderItem(3).setText(translate('OpenLP.FormattingTagDialog', 'End HTML')) - self.tag_table_widget.setColumnWidth(0, 120) - self.tag_table_widget.setColumnWidth(1, 80) - self.tag_table_widget.setColumnWidth(2, 330) + self.tag_table_widget.horizontalHeaderItem(4).setText(translate('OpenLP.FormattingTagDialog', + 'Hide content from Live/Preview')) + self.tag_table_widget.setColumnWidth(0, 80) + self.tag_table_widget.setColumnWidth(1, 40) + self.tag_table_widget.setColumnWidth(2, 320) + self.tag_table_widget.setColumnWidth(3, 150) diff --git a/openlp/core/ui/formattingtagform.py b/openlp/core/ui/formattingtagform.py index 835759424..0c74ed647 100644 --- a/openlp/core/ui/formattingtagform.py +++ b/openlp/core/ui/formattingtagform.py @@ -39,6 +39,7 @@ class EditColumn(object): Tag = 1 StartHtml = 2 EndHtml = 3 + Hidden = 4 class FormattingTagForm(QtWidgets.QDialog, Ui_FormattingTagDialog, FormattingTagController): @@ -96,6 +97,15 @@ class FormattingTagForm(QtWidgets.QDialog, Ui_FormattingTagDialog, FormattingTag self.tag_table_widget.setItem(new_row, 2, QtWidgets.QTableWidgetItem(translate('OpenLP.FormattingTagForm', ''))) self.tag_table_widget.setItem(new_row, 3, QtWidgets.QTableWidgetItem('')) + hiddenwidget = QtWidgets.QWidget() + hiddencheckbox = QtWidgets.QCheckBox() + hiddenlayout = QtWidgets.QHBoxLayout() + hiddenlayout.addWidget(hiddencheckbox) + hiddenlayout.setAlignment(QtCore.Qt.AlignCenter) + hiddenlayout.setContentsMargins(0, 0, 0, 0) + hiddenwidget.setLayout(hiddenlayout) + hiddencheckbox.setCheckState(QtCore.Qt.Unchecked) + self.tag_table_widget.setCellWidget(new_row, 4, hiddenwidget) self.tag_table_widget.resizeRowsToContents() self.tag_table_widget.scrollToBottom() self.tag_table_widget.selectRow(new_row) @@ -119,7 +129,9 @@ class FormattingTagForm(QtWidgets.QDialog, Ui_FormattingTagDialog, FormattingTag error = self.services.validate_for_save(self.tag_table_widget.item(count, 0).text(), self.tag_table_widget.item(count, 1).text(), self.tag_table_widget.item(count, 2).text(), - self.tag_table_widget.item(count, 3).text()) + self.tag_table_widget.item(count, 3).text(), + self.tag_table_widget.cellWidget(count, 4).children()[1].isChecked() + ) if error: QtWidgets.QMessageBox.warning(self, translate('OpenLP.FormattingTagForm', 'Validation Error'), error) self.tag_table_widget.selectRow(count) @@ -155,6 +167,15 @@ class FormattingTagForm(QtWidgets.QDialog, Ui_FormattingTagDialog, FormattingTag self.tag_table_widget.setItem(line, 1, QtWidgets.QTableWidgetItem(self._strip(html['start tag']))) self.tag_table_widget.setItem(line, 2, QtWidgets.QTableWidgetItem(html['start html'])) self.tag_table_widget.setItem(line, 3, QtWidgets.QTableWidgetItem(html['end html'])) + hiddenwidget = QtWidgets.QWidget() + hiddencheckbox = QtWidgets.QCheckBox() + hiddenlayout = QtWidgets.QHBoxLayout() + hiddenlayout.addWidget(hiddencheckbox) + hiddenlayout.setAlignment(QtCore.Qt.AlignCenter) + hiddenlayout.setContentsMargins(0, 0, 0, 0) + hiddenwidget.setLayout(hiddenlayout) + hiddencheckbox.setCheckState(QtCore.Qt.Checked if html['hidden'] else QtCore.Qt.Unchecked) + self.tag_table_widget.setCellWidget(line, 4, hiddenwidget) self.tag_table_widget.resizeRowsToContents() # Permanent (persistent) tags do not have this key html['temporary'] = False @@ -172,7 +193,8 @@ class FormattingTagForm(QtWidgets.QDialog, Ui_FormattingTagDialog, FormattingTag # only process for editable rows if self.tag_table_widget.item(pre_row, 0): item = self.tag_table_widget.item(pre_row, pre_col) - text = item.text() + if pre_col is not EditColumn.Hidden: + text = item.text() errors = None if pre_col is EditColumn.Description: if not text: diff --git a/openlp/plugins/songs/lib/openlyricsxml.py b/openlp/plugins/songs/lib/openlyricsxml.py index b31e2ad3a..c569541db 100644 --- a/openlp/plugins/songs/lib/openlyricsxml.py +++ b/openlp/plugins/songs/lib/openlyricsxml.py @@ -458,6 +458,8 @@ class OpenLyrics(object): if tag['end html']: element_close = self._add_text_to_element('close', element) element_close.text = etree.CDATA(tag['end html']) + element_hidden = self._add_text_to_element('hidden', element) + element_hidden.text = etree.CDATA(str(tag['hidden'])) def _add_text_with_tags_to_lines(self, verse_element, text, tags_element): """ @@ -602,7 +604,8 @@ class OpenLyrics(object): 'protected': False, # Add 'temporary' key in case the formatting tag should not be saved otherwise it is supposed that # formatting tag is permanent. - 'temporary': temporary + 'temporary': temporary, + 'hidden': True if hasattr(tag, 'hidden') and tag.hidden.text == 'True' else False } found_tags.append(openlp_tag) existing_tag_ids = [tag['start tag'] for tag in FormattingTags.get_html_tags()] diff --git a/tests/openlp_core/display/test_render.py b/tests/openlp_core/display/test_render.py index fdf0200ca..b0e281f6a 100644 --- a/tests/openlp_core/display/test_render.py +++ b/tests/openlp_core/display/test_render.py @@ -76,21 +76,21 @@ def test_render_tags(mocked_get_tags, settings): 'start tag': '{b}', 'start html': '', 'end tag': '{/b}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': 'Yellow', 'start tag': '{y}', 'start html': '', 'end tag': '{/y}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False }, { 'desc': 'Green', 'start tag': '{g}', 'start html': '', 'end tag': '{/g}', 'end html': '', 'protected': True, - 'temporary': False + 'temporary': False, 'hidden': False } ] string_to_pass = '{b}black{/b}{y}yellow{/y}' diff --git a/tests/openlp_core/ui/test_formattingtagsform.py b/tests/openlp_core/ui/test_formattingtagsform.py index 8a95897dc..6cfe7fac6 100644 --- a/tests/openlp_core/ui/test_formattingtagsform.py +++ b/tests/openlp_core/ui/test_formattingtagsform.py @@ -63,8 +63,11 @@ def test_on_new_clicked(tagform_env): # WHEN: on_new_clicked is run (i.e. the Add new button was clicked) with patch('openlp.core.ui.formattingtagform.QtWidgets.QTableWidgetItem') as MockedQTableWidgetItem: + mockedwidget = patch('openlp.core.ui.formattingtagform.QtWidgets.QWidget') mocked_table_widget = MagicMock() + mocked_widget = MagicMock() MockedQTableWidgetItem.return_value = mocked_table_widget + mockedwidget.return_value = mocked_widget form.on_new_clicked() # THEN: A new row should be added to the table diff --git a/tests/openlp_plugins/songs/test_openlyricsxml.py b/tests/openlp_plugins/songs/test_openlyricsxml.py index ee12950c1..e31b0a028 100644 --- a/tests/openlp_plugins/songs/test_openlyricsxml.py +++ b/tests/openlp_plugins/songs/test_openlyricsxml.py @@ -31,12 +31,12 @@ from tests.utils.constants import RESOURCE_PATH TEST_PATH = RESOURCE_PATH / 'songs' / 'openlyrics' START_TAGS = [{"protected": False, "desc": "z", "start tag": "{z}", "end html": "", "temporary": False, - "end tag": "{/z}", "start html": "strong>"}] + "end tag": "{/z}", "start html": "strong>", "hidden": False}] RESULT_TAGS = [{"temporary": False, "protected": False, "desc": "z", "start tag": "{z}", "start html": "strong>", - "end html": "", "end tag": "{/z}"}, + "end html": "", "end tag": "{/z}", "hidden": False}, {"temporary": False, "end tag": "{/c}", "desc": "c", "start tag": "{c}", "start html": "", "end html": "", - "protected": False}] + "protected": False, "hidden": False}] VERSE_LINES_07_XML = '\ Amazing grace, how sweet the sound\ That saved a wretch like me\