openlp/openlp/plugins/alerts/alertsplugin.py

264 lines
9.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2013-01-01 16:33:41 +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 #
###############################################################################
import logging
2015-11-07 00:49:40 +00:00
from PyQt5 import QtGui
2016-08-10 20:52:27 +00:00
from openlp.core.api.http import register_endpoint
2016-03-31 16:34:22 +00:00
from openlp.core.common.actions import ActionList
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import UiStrings, translate
from openlp.core.common.settings import Settings
from openlp.core.lib.db import Manager
2018-10-02 04:39:42 +00:00
from openlp.core.lib.plugin import Plugin, StringContent
from openlp.core.lib.theme import VerticalType
2017-07-04 23:13:51 +00:00
from openlp.core.lib.ui import create_action
from openlp.core.ui import AlertLocation
2018-04-13 17:52:40 +00:00
from openlp.core.ui.icons import UiIcons
2016-08-10 20:52:27 +00:00
from openlp.plugins.alerts.endpoint import api_alerts_endpoint, alerts_endpoint
2018-10-27 01:53:43 +00:00
from openlp.plugins.alerts.forms.alertform import AlertForm
from openlp.plugins.alerts.lib.alertsmanager import AlertsManager
from openlp.plugins.alerts.lib.alertstab import AlertsTab
from openlp.plugins.alerts.lib.db import init_schema
2018-10-02 04:39:42 +00:00
log = logging.getLogger(__name__)
JAVASCRIPT = """
function show_alert(alerttext, position){
var text = document.getElementById('alert');
text.innerHTML = alerttext;
if(alerttext == '') {
text.style.visibility = 'hidden';
return 0;
}
if(position == ''){
position = getComputedStyle(text, '').verticalAlign;
}
switch(position)
{
case 'top':
text.style.top = '0px';
break;
case 'middle':
text.style.top = ((window.innerHeight - text.clientHeight) / 2)
+ 'px';
break;
case 'bottom':
text.style.top = (window.innerHeight - text.clientHeight)
+ 'px';
break;
}
text.style.visibility = 'visible';
return text.clientHeight;
}
2011-10-15 06:32:01 +00:00
function update_css(align, font, size, color, bgcolor){
var text = document.getElementById('alert');
2011-10-16 07:59:21 +00:00
text.style.fontSize = size + "pt";
text.style.fontFamily = font;
2011-10-15 06:32:01 +00:00
text.style.color = color;
text.style.backgroundColor = bgcolor;
switch(align)
{
case 'top':
text.style.top = '0px';
break;
case 'middle':
text.style.top = ((window.innerHeight - text.clientHeight) / 2)
+ 'px';
break;
case 'bottom':
text.style.top = (window.innerHeight - text.clientHeight)
+ 'px';
break;
}
2011-10-15 06:32:01 +00:00
}
"""
CSS = """
#alert {{
position: absolute;
left: 0px;
top: 0px;
z-index: 10;
width: 100%;
vertical-align: {vertical_align};
font-family: {font_family};
font-size: {font_size:d}pt;
color: {color};
background-color: {background_color};
word-wrap: break-word;
}}
"""
HTML = """
<div id="alert" style="visibility:hidden"></div>
"""
2013-01-10 20:41:16 +00:00
__default_settings__ = {
2013-08-31 18:17:38 +00:00
'alerts/font face': QtGui.QFont().family(),
'alerts/font size': 40,
'alerts/db type': 'sqlite',
2015-02-11 20:56:13 +00:00
'alerts/db username': '',
'alerts/db password': '',
'alerts/db hostname': '',
'alerts/db database': '',
2013-08-31 18:17:38 +00:00
'alerts/location': AlertLocation.Bottom,
'alerts/background color': '#660000',
'alerts/font color': '#ffffff',
'alerts/timeout': 5
2013-02-14 21:31:17 +00:00
}
class AlertsPlugin(Plugin):
2014-01-01 09:33:07 +00:00
"""
The Alerts Plugin Class
"""
2013-08-31 18:17:38 +00:00
log.info('Alerts Plugin loaded')
2013-01-23 21:05:25 +00:00
def __init__(self):
2014-01-01 09:33:07 +00:00
"""
Class __init__ method
"""
2013-08-31 18:17:38 +00:00
super(AlertsPlugin, self).__init__('alerts', __default_settings__, settings_tab_class=AlertsTab)
self.weight = -3
2018-05-08 19:45:34 +00:00
self.icon_path = UiIcons().alert
self.icon = self.icon_path
2014-03-16 21:25:23 +00:00
AlertsManager(self)
2013-08-31 18:17:38 +00:00
self.manager = Manager('alerts', init_schema)
2016-08-12 18:59:45 +00:00
self.alert_form = AlertForm(self)
2016-08-10 20:52:27 +00:00
register_endpoint(alerts_endpoint)
register_endpoint(api_alerts_endpoint)
2013-03-19 19:43:22 +00:00
def add_tools_menu_item(self, tools_menu):
"""
2013-04-18 17:17:00 +00:00
Give the alerts plugin the opportunity to add items to the **Tools** menu.
2014-01-01 09:57:06 +00:00
:param tools_menu: The actual **Tools** menu item, so that your actions can use it as their parent.
"""
2013-08-31 18:17:38 +00:00
log.info('add tools menu')
self.tools_alert_item = create_action(tools_menu, 'toolsAlertItem',
2014-01-01 09:33:07 +00:00
text=translate('AlertsPlugin', '&Alert'),
2018-04-13 17:52:40 +00:00
icon=UiIcons().alert,
2014-01-01 09:33:07 +00:00
statustip=translate('AlertsPlugin', 'Show an alert message.'),
visible=False, can_shortcuts=True, triggers=self.on_alerts_trigger)
2013-03-19 19:43:22 +00:00
self.main_window.tools_menu.addAction(self.tools_alert_item)
def initialise(self):
2014-01-01 09:33:07 +00:00
"""
Initialise plugin
"""
2013-08-31 18:17:38 +00:00
log.info('Alerts Initialising')
2013-08-01 15:08:03 +00:00
super(AlertsPlugin, self).initialise()
2013-03-19 19:43:22 +00:00
self.tools_alert_item.setVisible(True)
2011-04-09 16:11:02 +00:00
action_list = ActionList.get_instance()
2013-03-19 19:43:22 +00:00
action_list.add_action(self.tools_alert_item, UiStrings().Tools)
def finalise(self):
2010-11-03 17:19:44 +00:00
"""
Tidy up on exit
"""
2013-08-31 18:17:38 +00:00
log.info('Alerts Finalising')
2010-11-03 17:19:44 +00:00
self.manager.finalise()
2013-08-01 15:08:03 +00:00
super(AlertsPlugin, self).finalise()
2013-03-19 19:43:22 +00:00
self.tools_alert_item.setVisible(False)
2011-04-09 16:11:02 +00:00
action_list = ActionList.get_instance()
2013-08-31 18:17:38 +00:00
action_list.remove_action(self.tools_alert_item, 'Tools')
2013-03-19 19:43:22 +00:00
def toggle_alerts_state(self):
2014-01-01 09:33:07 +00:00
"""
Switch the alerts state
"""
2013-03-19 19:43:22 +00:00
self.alerts_active = not self.alerts_active
2013-08-31 18:17:38 +00:00
Settings().setValue(self.settings_section + '/active', self.alerts_active)
2013-03-19 19:43:22 +00:00
def on_alerts_trigger(self):
2014-01-01 09:33:07 +00:00
"""
Start of the Alerts dialog triggered from the main menu.
"""
2013-03-19 19:43:22 +00:00
self.alert_form.load_list()
2015-11-07 00:49:40 +00:00
self.alert_form.exec()
2016-01-04 00:18:01 +00:00
@staticmethod
def about():
2014-01-01 09:33:07 +00:00
"""
Plugin Alerts about method
:return: text
"""
about_text = translate('AlertsPlugin', '<strong>Alerts Plugin</strong>'
2014-01-01 09:33:07 +00:00
'<br />The alert plugin controls the displaying of alerts on the display screen.')
return about_text
2013-02-19 21:23:56 +00:00
def set_plugin_text_strings(self):
"""
Called to define all translatable texts of the plugin
"""
2014-04-12 20:19:22 +00:00
# Name PluginList
2013-03-19 19:43:22 +00:00
self.text_strings[StringContent.Name] = {
2013-08-31 18:17:38 +00:00
'singular': translate('AlertsPlugin', 'Alert', 'name singular'),
'plural': translate('AlertsPlugin', 'Alerts', 'name plural')
}
2014-04-12 20:19:22 +00:00
# Name for MediaDockManager, SettingsManager
2013-03-19 19:43:22 +00:00
self.text_strings[StringContent.VisibleName] = {
2013-08-31 18:17:38 +00:00
'title': translate('AlertsPlugin', 'Alerts', 'container title')
2011-05-05 13:02:12 +00:00
}
2016-01-04 00:18:01 +00:00
@staticmethod
def get_display_javascript():
2011-10-16 15:39:49 +00:00
"""
Add Javascript to the main display.
"""
return JAVASCRIPT
2013-03-19 19:43:22 +00:00
def get_display_css(self):
2011-10-16 15:39:49 +00:00
"""
Add CSS to the main display.
"""
2013-03-19 19:43:22 +00:00
align = VerticalType.Names[self.settings_tab.location]
return CSS.format(vertical_align=align,
font_family=self.settings_tab.font_face,
font_size=self.settings_tab.font_size,
color=self.settings_tab.font_color,
background_color=self.settings_tab.background_color)
2016-01-04 00:18:01 +00:00
@staticmethod
def get_display_html():
2011-10-16 15:39:49 +00:00
"""
Add HTML to the main display.
"""
return HTML
2011-10-15 06:32:01 +00:00
2013-03-19 19:43:22 +00:00
def refresh_css(self, frame):
2011-10-16 15:39:49 +00:00
"""
2014-01-01 09:57:06 +00:00
Trigger an update of the CSS in the main display.
2011-10-16 15:39:49 +00:00
2014-01-01 09:57:06 +00:00
:param frame: The Web frame holding the page.
2011-10-16 15:39:49 +00:00
"""
2013-03-25 07:13:40 +00:00
align = VerticalType.Names[self.settings_tab.location]
2017-09-29 18:06:04 +00:00
frame.runJavaScript('update_css("{align}", "{face}", "{size}", "{color}", '
'"{background}")'.format(align=align,
face=self.settings_tab.font_face,
size=self.settings_tab.font_size,
color=self.settings_tab.font_color,
background=self.settings_tab.background_color))