openlp/openlp/plugins/alerts/forms/alertform.py

226 lines
9.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-06 09:10:17 +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/>. #
##########################################################################
2021-08-25 21:14:19 +00:00
from PyQt5 import QtCore, QtWidgets, QtGui
2010-02-17 19:13:04 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import translate
from openlp.core.common.registry import Registry
from openlp.plugins.alerts.lib.db import AlertItem
2018-10-02 04:39:42 +00:00
2020-06-06 16:05:36 +00:00
from openlp.plugins.alerts.forms.alertdialog import AlertDialog
2010-02-14 13:27:32 +00:00
2020-06-06 16:05:36 +00:00
class AlertForm(QtWidgets.QDialog, AlertDialog):
2010-02-14 13:27:32 +00:00
"""
2010-06-10 01:57:59 +00:00
Provide UI for the alert system
2010-02-14 13:27:32 +00:00
"""
2011-01-22 12:21:43 +00:00
def __init__(self, plugin):
2010-02-14 13:27:32 +00:00
"""
2010-06-10 01:57:59 +00:00
Initialise the alert form
2010-02-14 13:27:32 +00:00
"""
super(AlertForm, self).__init__(Registry().get('main_window'), QtCore.Qt.WindowSystemMenuHint |
QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
2010-07-31 00:46:15 +00:00
self.manager = plugin.manager
2011-05-28 09:53:37 +00:00
self.plugin = plugin
2010-04-21 17:21:56 +00:00
self.item_id = None
self.setup_ui(self)
2013-02-14 21:31:17 +00:00
self.display_button.clicked.connect(self.on_display_clicked)
self.display_close_button.clicked.connect(self.on_display_close_clicked)
self.alert_text_edit.textChanged.connect(self.on_text_changed)
self.new_button.clicked.connect(self.on_new_click)
self.save_button.clicked.connect(self.on_save_all)
self.alert_list_widget.doubleClicked.connect(self.on_double_click)
self.alert_list_widget.clicked.connect(self.on_single_click)
self.alert_list_widget.currentRowChanged.connect(self.on_current_row_changed)
2015-11-07 00:49:40 +00:00
def exec(self):
"""
Execute the dialog and return the exit code.
"""
2013-02-14 21:31:17 +00:00
self.display_button.setEnabled(False)
self.display_close_button.setEnabled(False)
2013-08-31 18:17:38 +00:00
self.alert_text_edit.setText('')
2015-11-07 00:49:40 +00:00
return QtWidgets.QDialog.exec(self)
2013-02-14 21:31:17 +00:00
def load_list(self):
2010-12-12 10:33:33 +00:00
"""
Loads the list with alerts.
"""
2013-02-14 21:31:17 +00:00
self.alert_list_widget.clear()
2013-01-01 16:33:41 +00:00
alerts = self.manager.get_all_objects(AlertItem, order_by_ref=AlertItem.text)
2010-02-14 13:27:32 +00:00
for alert in alerts:
2015-11-07 00:49:40 +00:00
item_name = QtWidgets.QListWidgetItem(alert.text)
2012-05-17 15:13:09 +00:00
item_name.setData(QtCore.Qt.UserRole, alert.id)
2013-02-14 21:31:17 +00:00
self.alert_list_widget.addItem(item_name)
2017-09-24 08:39:54 +00:00
if alert.text == self.alert_text_edit.text():
self.item_id = alert.id
2013-02-14 21:31:17 +00:00
self.alert_list_widget.setCurrentRow(self.alert_list_widget.row(item_name))
2013-02-14 21:31:17 +00:00
def on_display_clicked(self):
"""
Display the current alert text.
"""
2013-02-14 21:31:17 +00:00
self.trigger_alert(self.alert_text_edit.text())
2013-02-14 21:31:17 +00:00
def on_display_close_clicked(self):
"""
Close the alert preview.
"""
2013-02-14 21:31:17 +00:00
if self.trigger_alert(self.alert_text_edit.text()):
self.close()
2013-04-18 17:17:00 +00:00
def on_delete_button_clicked(self):
2010-12-12 10:33:33 +00:00
"""
Deletes the selected item.
"""
2013-02-14 21:31:17 +00:00
item = self.alert_list_widget.currentItem()
if item:
2012-05-19 09:13:32 +00:00
item_id = item.data(QtCore.Qt.UserRole)
2010-06-12 23:00:14 +00:00
self.manager.delete_object(AlertItem, item_id)
2013-02-14 21:31:17 +00:00
row = self.alert_list_widget.row(item)
self.alert_list_widget.takeItem(row)
2010-12-12 10:33:33 +00:00
self.item_id = None
2013-08-31 18:17:38 +00:00
self.alert_text_edit.setText('')
2013-02-14 21:31:17 +00:00
def on_new_click(self):
"""
Create a new alert.
"""
2013-02-14 21:31:17 +00:00
if not self.alert_text_edit.text():
2015-11-07 00:49:40 +00:00
QtWidgets.QMessageBox.information(self,
translate('AlertsPlugin.AlertForm', 'New Alert'),
translate('AlertsPlugin.AlertForm',
'You haven\'t specified any text for your alert. \n'
'Please type in some text before clicking New.'))
else:
2010-02-14 13:27:32 +00:00
alert = AlertItem()
2013-02-14 21:31:17 +00:00
alert.text = self.alert_text_edit.text()
2010-06-28 13:38:29 +00:00
self.manager.save_object(alert)
2013-02-14 21:31:17 +00:00
self.load_list()
2013-02-14 21:31:17 +00:00
def on_save_all(self):
2010-06-10 01:57:59 +00:00
"""
2010-12-12 10:33:33 +00:00
Save the alert, we are editing.
2010-06-10 01:57:59 +00:00
"""
2010-04-21 17:21:56 +00:00
if self.item_id:
2010-06-12 23:00:14 +00:00
alert = self.manager.get_object(AlertItem, self.item_id)
2013-02-14 21:31:17 +00:00
alert.text = self.alert_text_edit.text()
2010-06-28 13:38:29 +00:00
self.manager.save_object(alert)
2010-04-21 17:21:56 +00:00
self.item_id = None
2013-02-14 21:31:17 +00:00
self.load_list()
self.save_button.setEnabled(False)
2010-02-14 13:27:32 +00:00
2013-02-14 21:31:17 +00:00
def on_text_changed(self):
2010-06-10 01:57:59 +00:00
"""
2012-05-17 18:57:01 +00:00
Enable save button when data has been changed by editing the form.
2010-06-10 01:57:59 +00:00
"""
2010-12-12 10:33:33 +00:00
# Only enable the button, if we are editing an item.
if self.item_id:
2013-02-14 21:31:17 +00:00
self.save_button.setEnabled(True)
if self.alert_text_edit.text():
self.display_button.setEnabled(True)
self.display_close_button.setEnabled(True)
else:
2013-02-14 21:31:17 +00:00
self.display_button.setEnabled(False)
self.display_close_button.setEnabled(False)
2013-02-14 21:31:17 +00:00
def on_double_click(self):
2010-02-14 13:27:32 +00:00
"""
2012-05-17 18:57:01 +00:00
List item has been double clicked to display it.
2010-02-14 13:27:32 +00:00
"""
2013-02-14 21:31:17 +00:00
item = self.alert_list_widget.selectedIndexes()[0]
2014-01-01 09:33:07 +00:00
list_item = self.alert_list_widget.item(item.row())
self.trigger_alert(list_item.text())
self.alert_text_edit.setText(list_item.text())
self.item_id = list_item.data(QtCore.Qt.UserRole)
2013-02-14 21:31:17 +00:00
self.save_button.setEnabled(False)
2010-02-14 13:27:32 +00:00
2013-02-14 21:31:17 +00:00
def on_single_click(self):
2010-02-14 13:27:32 +00:00
"""
2013-04-18 17:17:00 +00:00
List item has been single clicked to add it to the edit field so it can be changed.
2010-02-14 13:27:32 +00:00
"""
2013-02-14 21:31:17 +00:00
item = self.alert_list_widget.selectedIndexes()[0]
2014-01-01 09:33:07 +00:00
list_item = self.alert_list_widget.item(item.row())
self.alert_text_edit.setText(list_item.text())
self.item_id = list_item.data(QtCore.Qt.UserRole)
2010-12-12 10:33:33 +00:00
# If the alert does not contain '<>' we clear the ParameterEdit field.
2013-08-31 18:17:38 +00:00
if self.alert_text_edit.text().find('<>') == -1:
self.parameter_edit.setText('')
2013-02-14 21:31:17 +00:00
self.save_button.setEnabled(False)
2010-02-14 13:27:32 +00:00
2013-02-14 21:31:17 +00:00
def trigger_alert(self, text):
2010-12-12 10:33:33 +00:00
"""
Prepares the alert text for displaying.
2014-01-01 09:57:06 +00:00
:param text: The alert text.
2010-12-12 10:33:33 +00:00
"""
2011-02-03 19:33:48 +00:00
if not text:
return False
# We found '<>' in the alert text, but the ParameterEdit field is empty.
2014-01-01 09:33:07 +00:00
if text.find('<>') != -1 and not self.parameter_edit.text() and \
2015-11-07 00:49:40 +00:00
QtWidgets.QMessageBox.question(self,
translate('AlertsPlugin.AlertForm', 'No Parameter Found'),
translate('AlertsPlugin.AlertForm',
'You have not entered a parameter to be replaced.\n'
'Do you want to continue anyway?')
2015-11-07 00:49:40 +00:00
) == QtWidgets.QMessageBox.No:
2013-02-14 21:31:17 +00:00
self.parameter_edit.setFocus()
2011-02-03 19:33:48 +00:00
return False
# The ParameterEdit field is not empty, but we have not found '<>'
# in the alert text.
2014-01-01 09:33:07 +00:00
elif text.find('<>') == -1 and self.parameter_edit.text() and \
2015-11-07 00:49:40 +00:00
QtWidgets.QMessageBox.question(self,
translate('AlertsPlugin.AlertForm', 'No Placeholder Found'),
translate('AlertsPlugin.AlertForm',
'The alert text does not contain \'<>\'.\n'
'Do you want to continue anyway?')
2015-11-07 00:49:40 +00:00
) == QtWidgets.QMessageBox.No:
2013-02-14 21:31:17 +00:00
self.parameter_edit.setFocus()
2011-02-03 19:33:48 +00:00
return False
2013-08-31 18:17:38 +00:00
text = text.replace('<>', self.parameter_edit.text())
2013-02-14 21:31:17 +00:00
self.plugin.alerts_manager.display_alert(text)
2011-02-03 19:33:48 +00:00
return True
2011-02-03 19:11:27 +00:00
2013-02-14 21:31:17 +00:00
def on_current_row_changed(self, row):
2011-02-03 19:11:27 +00:00
"""
2013-04-18 17:17:00 +00:00
Called when the *alert_list_widget*'s current row has been changed. This enables or disables buttons which
require an item to act on.
2011-02-03 19:11:27 +00:00
2014-01-01 09:57:06 +00:00
:param row: The row (int). If there is no current row, the value is -1.
2011-02-03 19:11:27 +00:00
"""
if row == -1:
2013-02-14 21:31:17 +00:00
self.display_button.setEnabled(False)
self.display_close_button.setEnabled(False)
self.save_button.setEnabled(False)
self.delete_button.setEnabled(False)
2011-02-03 19:11:27 +00:00
else:
2013-02-14 21:31:17 +00:00
self.display_button.setEnabled(True)
self.display_close_button.setEnabled(True)
self.delete_button.setEnabled(True)
2011-02-03 19:11:27 +00:00
# We do not need to enable the save button, as it is only enabled
2013-02-14 21:31:17 +00:00
# when typing text in the "alert_text_edit".
2021-08-25 21:14:19 +00:00
def provide_help(self):
"""
Provide help within the form by opening the appropriate page of the openlp manual in the user's browser
"""
QtGui.QDesktopServices.openUrl(QtCore.QUrl("https://manual.openlp.org/alert.html"))