openlp/openlp/plugins/custom/forms/editcustomform.py

248 lines
10 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2013-01-05 22:17:30 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2017-12-29 09:15:48 +00:00
# Copyright (c) 2008-2018 OpenLP Developers #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2010-03-21 23:58:01 +00:00
2009-11-06 17:36:18 +00:00
import logging
from PyQt5 import QtCore, QtWidgets
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import translate
from openlp.core.common.registry import Registry
2013-02-03 21:46:56 +00:00
from openlp.core.lib.ui import critical_error_message_box, find_and_set_in_combo_box
from openlp.plugins.custom.forms.editcustomdialog import Ui_CustomEditDialog
from openlp.plugins.custom.forms.editcustomslideform import EditCustomSlideForm
from openlp.plugins.custom.lib.customxmlhandler import CustomXMLBuilder, CustomXMLParser
2010-06-15 02:08:22 +00:00
from openlp.plugins.custom.lib.db import CustomSlide
2018-10-02 04:39:42 +00:00
2010-02-27 15:31:23 +00:00
log = logging.getLogger(__name__)
2015-11-07 00:49:40 +00:00
class EditCustomForm(QtWidgets.QDialog, Ui_CustomEditDialog):
"""
Class documentation goes here.
"""
2013-08-31 18:17:38 +00:00
log.info('Custom Editor loaded')
2013-03-19 20:05:13 +00:00
def __init__(self, media_item, parent, manager):
"""
Constructor
"""
super(EditCustomForm, self).__init__(parent, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint |
QtCore.Qt.WindowCloseButtonHint)
self.manager = manager
2013-03-19 20:05:13 +00:00
self.media_item = media_item
self.setup_ui(self)
2011-02-04 18:51:20 +00:00
# Create other objects and forms.
2013-02-19 21:23:56 +00:00
self.edit_slide_form = EditCustomSlideForm(self)
# Connecting signals and slots
2013-02-19 21:23:56 +00:00
self.preview_button.clicked.connect(self.on_preview_button_clicked)
self.add_button.clicked.connect(self.on_add_button_clicked)
self.edit_button.clicked.connect(self.on_edit_button_clicked)
self.edit_all_button.clicked.connect(self.on_edit_all_button_clicked)
self.slide_list_view.currentRowChanged.connect(self.on_current_row_changed)
self.slide_list_view.doubleClicked.connect(self.on_edit_button_clicked)
2013-08-31 18:17:38 +00:00
Registry().register_function('theme_update_list', self.load_themes)
2013-02-07 11:33:47 +00:00
def load_themes(self, theme_list):
"""
Load a list of themes into the themes combo box.
2014-01-01 10:56:23 +00:00
:param theme_list: The list of themes to load.
"""
2013-02-19 21:23:56 +00:00
self.theme_combo_box.clear()
2013-08-31 18:17:38 +00:00
self.theme_combo_box.addItem('')
2013-02-19 21:23:56 +00:00
self.theme_combo_box.addItems(theme_list)
2013-02-19 21:23:56 +00:00
def load_custom(self, id, preview=False):
"""
Called when editing or creating a new custom.
2014-01-01 10:56:23 +00:00
:param id: The custom's id. If zero, then a new custom is created.
:param preview: States whether the custom is edited while being previewed in the preview panel.
"""
2013-02-19 21:23:56 +00:00
self.slide_list_view.clear()
2011-02-04 18:51:20 +00:00
if id == 0:
2013-02-19 21:23:56 +00:00
self.custom_slide = CustomSlide()
2013-08-31 18:17:38 +00:00
self.title_edit.setText('')
self.credit_edit.setText('')
2013-02-19 21:23:56 +00:00
self.theme_combo_box.setCurrentIndex(0)
2011-02-04 18:51:20 +00:00
else:
2013-02-19 21:23:56 +00:00
self.custom_slide = self.manager.get_object(CustomSlide, id)
self.title_edit.setText(self.custom_slide.title)
self.credit_edit.setText(self.custom_slide.credits)
2013-12-24 07:02:47 +00:00
custom_xml = CustomXMLParser(self.custom_slide.text)
slide_list = custom_xml.get_verses()
2013-06-30 18:37:12 +00:00
self.slide_list_view.addItems([slide[1] for slide in slide_list])
2013-02-19 21:23:56 +00:00
theme = self.custom_slide.theme_name
find_and_set_in_combo_box(self.theme_combo_box, theme)
self.title_edit.setFocus()
# If not preview hide the preview button.
2013-02-19 21:23:56 +00:00
self.preview_button.setVisible(preview)
def accept(self):
"""
Override the QDialog method to check if the custom slide has been saved before closing the dialog.
"""
2013-08-31 18:17:38 +00:00
log.debug('accept')
2013-02-19 21:23:56 +00:00
if self.save_custom():
2015-11-07 00:49:40 +00:00
QtWidgets.QDialog.accept(self)
2013-02-19 21:23:56 +00:00
def save_custom(self):
"""
Saves the custom.
"""
2011-02-04 18:51:20 +00:00
if not self._validate():
return False
2010-07-03 01:33:40 +00:00
sxml = CustomXMLBuilder()
2013-02-19 21:23:56 +00:00
for count in range(self.slide_list_view.count()):
2013-08-31 18:17:38 +00:00
sxml.add_verse_to_lyrics('custom', str(count + 1), self.slide_list_view.item(count).text())
2013-02-19 21:23:56 +00:00
self.custom_slide.title = self.title_edit.text()
2013-08-31 18:17:38 +00:00
self.custom_slide.text = str(sxml.extract_xml(), 'utf-8')
2013-02-19 21:23:56 +00:00
self.custom_slide.credits = self.credit_edit.text()
self.custom_slide.theme_name = self.theme_combo_box.currentText()
success = self.manager.save_object(self.custom_slide)
2013-03-20 18:35:28 +00:00
self.media_item.auto_select_id = self.custom_slide.id
return success
2013-02-19 21:23:56 +00:00
def on_up_button_clicked(self):
"""
Move a slide up in the list when the "Up" button is clicked.
"""
2013-12-24 07:02:47 +00:00
selected_row = self.slide_list_view.currentRow()
if selected_row != 0:
qw = self.slide_list_view.takeItem(selected_row)
self.slide_list_view.insertItem(selected_row - 1, qw)
self.slide_list_view.setCurrentRow(selected_row - 1)
2013-02-19 21:23:56 +00:00
def on_down_button_clicked(self):
"""
Move a slide down in the list when the "Down" button is clicked.
"""
2013-12-24 07:02:47 +00:00
selected_row = self.slide_list_view.currentRow()
# zero base arrays
2013-12-24 07:02:47 +00:00
if selected_row != self.slide_list_view.count() - 1:
qw = self.slide_list_view.takeItem(selected_row)
self.slide_list_view.insertItem(selected_row + 1, qw)
self.slide_list_view.setCurrentRow(selected_row + 1)
def on_add_button_clicked(self):
"""
Add a new blank slide.
"""
2013-08-31 18:17:38 +00:00
self.edit_slide_form.set_text('')
2015-11-07 00:49:40 +00:00
if self.edit_slide_form.exec():
2013-02-19 21:23:56 +00:00
self.slide_list_view.addItems(self.edit_slide_form.get_text())
def on_edit_button_clicked(self):
"""
Edit the currently selected slide.
"""
2013-03-28 21:49:11 +00:00
self.edit_slide_form.set_text(self.slide_list_view.currentItem().text())
2015-11-07 00:49:40 +00:00
if self.edit_slide_form.exec():
2013-02-19 21:23:56 +00:00
self.update_slide_list(self.edit_slide_form.get_text())
2009-09-09 17:40:12 +00:00
def on_edit_all_button_clicked(self):
2010-10-09 19:20:07 +00:00
"""
Edits all slides.
"""
2013-08-31 18:17:38 +00:00
slide_text = ''
2013-02-19 21:23:56 +00:00
for row in range(self.slide_list_view.count()):
item = self.slide_list_view.item(row)
slide_text += item.text()
2013-02-19 21:23:56 +00:00
if row != self.slide_list_view.count() - 1:
2013-08-31 18:17:38 +00:00
slide_text += '\n[===]\n'
2013-03-28 21:49:11 +00:00
self.edit_slide_form.set_text(slide_text)
2015-11-07 00:49:40 +00:00
if self.edit_slide_form.exec():
2013-02-19 21:23:56 +00:00
self.update_slide_list(self.edit_slide_form.get_text(), True)
2011-02-04 18:51:20 +00:00
def on_preview_button_clicked(self):
2011-02-04 18:51:20 +00:00
"""
Save the custom item and preview it.
"""
2013-08-31 18:17:38 +00:00
log.debug('onPreview')
2013-02-19 21:23:56 +00:00
if self.save_custom():
2013-08-31 18:17:38 +00:00
Registry().execute('custom_preview')
2009-09-11 04:54:22 +00:00
2013-02-19 21:23:56 +00:00
def update_slide_list(self, slides, edit_all=False):
2010-10-09 19:20:07 +00:00
"""
Updates the slide list after editing slides.
2010-10-09 19:20:07 +00:00
2014-01-01 10:56:23 +00:00
:param slides: A list of all slides which have been edited.
:param edit_all: Indicates if all slides or only one slide has been edited.
2010-10-09 19:20:07 +00:00
"""
if edit_all:
2013-02-19 21:23:56 +00:00
self.slide_list_view.clear()
self.slide_list_view.addItems(slides)
2009-09-11 04:54:22 +00:00
else:
2013-02-19 21:23:56 +00:00
old_row = self.slide_list_view.currentRow()
# Create a list with all (old/unedited) slides.
2013-02-19 21:23:56 +00:00
old_slides = [self.slide_list_view.item(row).text() for row in range(self.slide_list_view.count())]
self.slide_list_view.clear()
old_slides.pop(old_row)
# Insert all slides to make the old_slides list complete.
for slide in slides:
old_slides.insert(old_row, slide)
old_row += 1
2013-02-19 21:23:56 +00:00
self.slide_list_view.addItems(old_slides)
self.slide_list_view.repaint()
2013-02-19 21:23:56 +00:00
def on_delete_button_clicked(self):
2011-02-04 18:51:20 +00:00
"""
Removes the current row from the list.
"""
2013-02-19 21:23:56 +00:00
self.slide_list_view.takeItem(self.slide_list_view.currentRow())
self.on_current_row_changed(self.slide_list_view.currentRow())
2011-02-04 18:51:20 +00:00
def on_current_row_changed(self, row):
2011-02-04 18:51:20 +00:00
"""
2013-02-19 21:23:56 +00:00
Called when the *slide_list_view*'s current row has been changed. This
2011-02-04 18:51:20 +00:00
enables or disables buttons which require an slide to act on.
2011-02-04 18:54:30 +00:00
2014-01-01 10:56:23 +00:00
:param row: The row (int). If there is no current row, the value is -1.
2011-02-04 18:51:20 +00:00
"""
if row == -1:
2013-02-19 21:23:56 +00:00
self.delete_button.setEnabled(False)
self.edit_button.setEnabled(False)
self.up_button.setEnabled(False)
self.down_button.setEnabled(False)
2011-02-04 18:51:20 +00:00
else:
2013-02-19 21:23:56 +00:00
self.delete_button.setEnabled(True)
self.edit_button.setEnabled(True)
2011-02-04 18:51:20 +00:00
# Decide if the up/down buttons should be enabled or not.
2013-02-19 21:23:56 +00:00
self.down_button.setEnabled(self.slide_list_view.count() - 1 != row)
self.up_button.setEnabled(row != 0)
2010-10-10 13:29:08 +00:00
2009-09-09 17:30:33 +00:00
def _validate(self):
"""
Checks whether a custom is valid or not.
"""
# We must have a title.
2013-02-19 21:23:56 +00:00
if not self.title_edit.displayText():
self.title_edit.setFocus()
2013-01-05 22:17:30 +00:00
critical_error_message_box(message=translate('CustomPlugin.EditCustomForm', 'You need to type in a title.'))
2011-02-04 18:51:20 +00:00
return False
2010-11-29 07:32:08 +00:00
# We must have at least one slide.
2013-02-19 21:23:56 +00:00
if self.slide_list_view.count() == 0:
2013-01-05 22:17:30 +00:00
critical_error_message_box(message=translate('CustomPlugin.EditCustomForm',
2013-12-24 07:02:47 +00:00
'You need to add at least one slide.'))
2011-02-04 18:51:20 +00:00
return False
return True