openlp/openlp/core/lib/mediamanageritem.py

742 lines
30 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-12-28 22:06:43 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2012-12-29 20:56:56 +00:00
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-11-11 21:16:14 +00:00
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
2012-10-21 13:16:22 +00:00
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# 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-06-19 17:31:42 +00:00
"""
Provides the generic functions for interfacing plugins with the Media Manager.
"""
2010-05-27 20:56:34 +00:00
import logging
2009-06-27 15:33:03 +00:00
import os
2011-05-15 19:18:50 +00:00
import re
from PyQt4 import QtCore, QtGui
2013-02-07 08:42:17 +00:00
from openlp.core.lib import OpenLPToolbar, ServiceItem, StringContent, ListWidgetWithDnD, \
2013-03-17 09:21:18 +00:00
ServiceItemContext, Settings, Registry, UiStrings, translate
2011-12-30 21:40:13 +00:00
from openlp.core.lib.searchedit import SearchEdit
from openlp.core.lib.ui import create_widget_action, critical_error_message_box
2010-02-27 15:31:23 +00:00
log = logging.getLogger(__name__)
2013-02-01 19:58:18 +00:00
class MediaManagerItem(QtGui.QWidget):
"""
MediaManagerItem is a helper widget for plugins.
2009-07-01 20:21:13 +00:00
2013-03-07 08:05:43 +00:00
None of the following *need* to be used, feel free to override them completely in your plugin's implementation.
Alternatively, call them from your plugin before or after you've done extra things that you need to.
2009-07-01 20:21:13 +00:00
2009-09-03 21:41:34 +00:00
**Constructor Parameters**
2009-07-01 20:21:13 +00:00
2009-09-03 21:41:34 +00:00
``parent``
2013-03-07 08:05:43 +00:00
The parent widget. Usually this will be the *Media Manager* itself. This needs to be a class descended from
``QWidget``.
2010-09-15 17:55:27 +00:00
``plugin``
2013-03-07 08:05:43 +00:00
The plugin widget. Usually this will be the *Plugin* itself. This needs to be a class descended from ``Plugin``.
2010-09-15 17:55:27 +00:00
2009-09-03 21:41:34 +00:00
**Member Variables**
2013-03-07 08:05:43 +00:00
When creating a descendant class from this class for your plugin, the following member variables should be set.
2009-09-03 21:41:34 +00:00
2013-03-19 22:00:50 +00:00
``self.on_new_prompt``
2011-02-17 02:33:12 +00:00
2009-09-03 21:41:34 +00:00
Defaults to *'Select Image(s)'*.
2013-03-19 22:00:50 +00:00
``self.on_new_file_masks``
2013-03-07 08:05:43 +00:00
Defaults to *'Images (*.jpg *jpeg *.gif *.png *.bmp)'*. This assumes that the new action is to load a file. If
not, you need to override the ``OnNew`` method.
2009-09-03 21:41:34 +00:00
``self.PreviewFunction``
2013-03-07 08:05:43 +00:00
This must be a method which returns a QImage to represent the item (usually a preview). No scaling is required,
that is performed automatically by OpenLP when necessary. If this method is not defined, a default will be used
(treat the filename as an image).
"""
2013-08-31 18:17:38 +00:00
log.info('Media Item loaded')
2009-06-23 20:59:38 +00:00
2013-03-07 13:14:31 +00:00
def __init__(self, parent=None, plugin=None):
"""
Constructor to create the media manager item.
"""
2013-07-18 11:19:20 +00:00
super(MediaManagerItem, self).__init__()
self.hide()
2011-06-01 22:47:42 +00:00
self.whitespace = re.compile(r'[\W_]+', re.UNICODE)
2011-05-28 09:53:37 +00:00
self.plugin = plugin
2013-03-19 19:43:22 +00:00
visible_title = self.plugin.get_string(StringContent.VisibleName)
2013-08-31 18:17:38 +00:00
self.title = str(visible_title['title'])
2013-01-27 09:57:03 +00:00
Registry().register(self.plugin.name, self)
2013-03-19 19:43:22 +00:00
self.settings_section = self.plugin.name
2010-07-07 16:03:30 +00:00
self.toolbar = None
2013-03-19 22:00:50 +00:00
self.remote_triggered = None
self.single_service_item = True
self.quick_preview_allowed = False
2013-03-22 21:05:42 +00:00
self.has_search = False
2013-03-19 22:00:50 +00:00
self.page_layout = QtGui.QVBoxLayout(self)
self.page_layout.setSpacing(0)
self.page_layout.setMargin(0)
self.required_icons()
self.setupUi()
self.retranslateUi()
2013-03-20 18:35:28 +00:00
self.auto_select_id = -1
2013-03-22 21:05:42 +00:00
# Need to use event as called across threads and UI is updated
2013-08-31 18:17:38 +00:00
QtCore.QObject.connect(self, QtCore.SIGNAL('%s_go_live' % self.plugin.name), self.go_live_remote)
QtCore.QObject.connect(self, QtCore.SIGNAL('%s_add_to_service' % self.plugin.name), self.add_to_service_remote)
2013-03-19 22:00:50 +00:00
def required_icons(self):
2009-09-26 09:11:39 +00:00
"""
2013-03-07 08:05:43 +00:00
This method is called to define the icons for the plugin. It provides a default set and the plugin is able to
override the if required.
2009-09-26 09:11:39 +00:00
"""
2013-03-19 22:00:50 +00:00
self.has_import_icon = False
self.has_new_icon = True
self.has_edit_icon = True
self.has_file_icon = False
self.has_delete_icon = True
self.add_to_service_item = False
2009-09-26 09:11:39 +00:00
def retranslateUi(self):
2009-09-04 22:50:19 +00:00
"""
2013-03-07 08:05:43 +00:00
This method is called automatically to provide OpenLP with the opportunity to translate the ``MediaManagerItem``
to another language.
2009-09-04 22:50:19 +00:00
"""
pass
2013-03-19 22:00:50 +00:00
def add_toolbar(self):
"""
2013-03-07 08:05:43 +00:00
A method to help developers easily add a toolbar to the media manager item.
"""
2010-07-07 16:03:30 +00:00
if self.toolbar is None:
self.toolbar = OpenLPToolbar(self)
2013-03-19 22:00:50 +00:00
self.page_layout.addWidget(self.toolbar)
2009-06-23 20:53:06 +00:00
def setupUi(self):
2009-09-04 22:50:19 +00:00
"""
2013-03-07 08:05:43 +00:00
This method sets up the interface on the button. Plugin developers use this to add and create toolbars, and the
rest of the interface of the media manager item.
2009-09-04 22:50:19 +00:00
"""
2009-06-23 20:53:06 +00:00
# Add a toolbar
2013-03-19 22:00:50 +00:00
self.add_toolbar()
2011-01-18 20:15:56 +00:00
# Allow the plugin to define buttons at start of bar
2013-03-19 22:00:50 +00:00
self.add_start_header_bar()
2011-01-18 20:15:56 +00:00
# Add the middle of the tool bar (pre defined)
2013-03-19 22:00:50 +00:00
self.add_middle_header_bar()
2011-01-18 20:15:56 +00:00
# Allow the plugin to define buttons at end of bar
2013-03-19 22:00:50 +00:00
self.add_end_header_bar()
2011-01-18 20:15:56 +00:00
# Add the list view
2013-03-19 22:00:50 +00:00
self.add_list_view_to_toolbar()
2009-09-16 04:59:38 +00:00
2013-03-19 22:00:50 +00:00
def add_middle_header_bar(self):
2010-06-19 17:31:42 +00:00
"""
Create buttons for the media item toolbar
"""
toolbar_actions = []
## Import Button ##
2013-03-19 22:00:50 +00:00
if self.has_import_icon:
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['Import', StringContent.Import,
':/general/general_import.png', self.on_import_click])
2010-09-10 19:47:33 +00:00
## Load Button ##
2013-03-19 22:00:50 +00:00
if self.has_file_icon:
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['Load', StringContent.Load, ':/general/general_open.png', self.on_file_click])
2009-06-27 05:46:05 +00:00
## New Button ##
2013-03-19 22:00:50 +00:00
if self.has_new_icon:
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['New', StringContent.New, ':/general/general_new.png', self.on_new_click])
2009-06-27 05:46:05 +00:00
## Edit Button ##
2013-03-19 22:00:50 +00:00
if self.has_edit_icon:
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['Edit', StringContent.Edit, ':/general/general_edit.png', self.on_edit_click])
2009-06-27 05:46:05 +00:00
## Delete Button ##
2013-03-19 22:00:50 +00:00
if self.has_delete_icon:
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['Delete', StringContent.Delete,
':/general/general_delete.png', self.on_delete_click])
2009-07-01 20:21:13 +00:00
## Preview ##
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['Preview', StringContent.Preview,
':/general/general_preview.png', self.on_preview_click])
2011-02-02 13:22:43 +00:00
## Live Button ##
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['Live', StringContent.Live, ':/general/general_live.png', self.on_live_click])
2009-07-01 20:21:13 +00:00
## Add to service Button ##
2013-08-31 18:17:38 +00:00
toolbar_actions.append(['Service', StringContent.Service, ':/general/general_add.png', self.on_add_click])
for action in toolbar_actions:
if action[0] == StringContent.Preview:
self.toolbar.addSeparator()
2013-08-31 18:17:38 +00:00
self.toolbar.add_toolbar_action('%s%sAction' % (self.plugin.name, action[0]),
text=self.plugin.get_string(action[1])['title'], icon=action[2],
tooltip=self.plugin.get_string(action[1])['tooltip'],
triggers=action[3])
2009-09-16 04:59:38 +00:00
2013-03-19 22:00:50 +00:00
def add_list_view_to_toolbar(self):
2010-06-19 17:31:42 +00:00
"""
Creates the main widget for listing items the media item is tracking
"""
2011-01-12 19:31:46 +00:00
# Add the List widget
2013-03-19 22:00:50 +00:00
self.list_view = ListWidgetWithDnD(self, self.plugin.name)
self.list_view.setSpacing(1)
self.list_view.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.list_view.setAlternatingRowColors(True)
2013-08-31 18:17:38 +00:00
self.list_view.setObjectName('%sListView' % self.plugin.name)
2013-03-19 22:00:50 +00:00
# Add to page_layout
self.page_layout.addWidget(self.list_view)
2011-01-12 19:31:46 +00:00
# define and add the context menu
2013-03-19 22:00:50 +00:00
self.list_view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
if self.has_edit_icon:
create_widget_action(self.list_view,
2013-08-31 18:17:38 +00:00
text=self.plugin.get_string(StringContent.Edit)['title'],
icon=':/general/general_edit.png',
2013-03-19 22:00:50 +00:00
triggers=self.on_edit_click)
create_widget_action(self.list_view, separator=True)
if self.has_delete_icon:
create_widget_action(self.list_view,
2013-08-31 18:17:38 +00:00
'listView%s%sItem' % (self.plugin.name.title(), StringContent.Delete.title()),
text=self.plugin.get_string(StringContent.Delete)['title'],
icon=':/general/general_delete.png',
2013-03-19 22:00:50 +00:00
can_shortcuts=True, triggers=self.on_delete_click)
create_widget_action(self.list_view, separator=True)
create_widget_action(self.list_view,
2013-08-31 18:17:38 +00:00
'listView%s%sItem' % (self.plugin.name.title(), StringContent.Preview.title()),
text=self.plugin.get_string(StringContent.Preview)['title'],
icon=':/general/general_preview.png',
can_shortcuts=True,
2013-03-19 22:00:50 +00:00
triggers=self.on_preview_click)
create_widget_action(self.list_view,
2013-08-31 18:17:38 +00:00
'listView%s%sItem' % (self.plugin.name.title(), StringContent.Live.title()),
text=self.plugin.get_string(StringContent.Live)['title'],
icon=':/general/general_live.png',
can_shortcuts=True,
2013-03-19 22:00:50 +00:00
triggers=self.on_live_click)
create_widget_action(self.list_view,
2013-08-31 18:17:38 +00:00
'listView%s%sItem' % (self.plugin.name.title(), StringContent.Service.title()),
can_shortcuts=True,
2013-08-31 18:17:38 +00:00
text=self.plugin.get_string(StringContent.Service)['title'],
icon=':/general/general_add.png',
2013-03-19 22:00:50 +00:00
triggers=self.on_add_click)
if self.add_to_service_item:
create_widget_action(self.list_view, separator=True)
create_widget_action(self.list_view,
2012-12-28 22:06:43 +00:00
text=translate('OpenLP.MediaManagerItem', '&Add to selected Service Item'),
2013-08-31 18:17:38 +00:00
icon=':/general/general_add.png',
2013-03-19 22:00:50 +00:00
triggers=self.on_add_edit_click)
self.add_custom_context_actions()
# Create the context menu and add all actions from the list_view.
2011-05-13 13:00:20 +00:00
self.menu = QtGui.QMenu()
2013-03-19 22:00:50 +00:00
self.menu.addActions(self.list_view.actions())
self.list_view.doubleClicked.connect(self.on_double_clicked)
self.list_view.itemSelectionChanged.connect(self.on_selection_change)
self.list_view.customContextMenuRequested.connect(self.context_menu)
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def add_search_to_toolbar(self):
2011-12-30 21:40:13 +00:00
"""
Creates a search field with button and related signal handling.
"""
2013-03-19 22:00:50 +00:00
self.search_widget = QtGui.QWidget(self)
2013-08-31 18:17:38 +00:00
self.search_widget.setObjectName('search_widget')
2013-03-19 22:00:50 +00:00
self.search_layout = QtGui.QVBoxLayout(self.search_widget)
2013-08-31 18:17:38 +00:00
self.search_layout.setObjectName('search_layout')
2013-03-19 22:00:50 +00:00
self.search_text_layout = QtGui.QFormLayout()
2013-08-31 18:17:38 +00:00
self.search_text_layout.setObjectName('search_text_layout')
2013-03-19 22:00:50 +00:00
self.search_text_label = QtGui.QLabel(self.search_widget)
2013-08-31 18:17:38 +00:00
self.search_text_label.setObjectName('search_text_label')
2013-03-19 22:00:50 +00:00
self.search_text_edit = SearchEdit(self.search_widget)
2013-08-31 18:17:38 +00:00
self.search_text_edit.setObjectName('search_text_edit')
2013-03-19 22:00:50 +00:00
self.search_text_label.setBuddy(self.search_text_edit)
self.search_text_layout.addRow(self.search_text_label, self.search_text_edit)
self.search_layout.addLayout(self.search_text_layout)
self.search_button_layout = QtGui.QHBoxLayout()
2013-08-31 18:17:38 +00:00
self.search_button_layout.setObjectName('search_button_layout')
2013-03-19 22:00:50 +00:00
self.search_button_layout.addStretch()
self.search_text_button = QtGui.QPushButton(self.search_widget)
2013-08-31 18:17:38 +00:00
self.search_text_button.setObjectName('search_text_button')
2013-03-19 22:00:50 +00:00
self.search_button_layout.addWidget(self.search_text_button)
self.search_layout.addLayout(self.search_button_layout)
self.page_layout.addWidget(self.search_widget)
2011-12-30 21:40:13 +00:00
# Signals and slots
2013-03-19 22:00:50 +00:00
self.search_text_edit.returnPressed.connect(self.on_search_text_button_clicked)
self.search_text_button.clicked.connect(self.on_search_text_button_clicked)
self.search_text_edit.textChanged.connect(self.on_search_text_edit_changed)
2011-12-30 21:40:13 +00:00
2013-03-19 22:00:50 +00:00
def add_custom_context_actions(self):
"""
Implement this method in your descendent media manager item to
add any context menu items. This method is called automatically.
"""
pass
2009-06-23 20:53:06 +00:00
def initialise(self):
2009-09-04 22:50:19 +00:00
"""
Implement this method in your descendent media manager item to
2010-06-10 01:57:59 +00:00
do any UI or other initialisation. This method is called automatically.
2009-09-04 22:50:19 +00:00
"""
pass
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def add_start_header_bar(self):
2009-09-11 04:54:22 +00:00
"""
Slot at start of toolbar for plugin to addwidgets
2009-09-11 04:54:22 +00:00
"""
pass
2013-03-19 22:00:50 +00:00
def add_end_header_bar(self):
2009-09-11 04:54:22 +00:00
"""
Slot at end of toolbar for plugin to add widgets
2009-09-11 04:54:22 +00:00
"""
2009-06-27 05:46:05 +00:00
pass
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def on_file_click(self):
2010-06-19 17:31:42 +00:00
"""
Add a file to the list widget to make it available for showing
"""
2013-03-19 22:00:50 +00:00
files = QtGui.QFileDialog.getOpenFileNames(self, self.on_new_prompt,
2013-08-31 18:17:38 +00:00
Settings().value(self.settings_section + '/last directory'), self.on_new_file_masks)
log.info('New files(s) %s', files)
2010-03-09 19:43:11 +00:00
if files:
2013-02-03 19:23:12 +00:00
self.application.set_busy_cursor()
2013-03-19 22:00:50 +00:00
self.validate_and_load(files)
2013-02-03 19:23:12 +00:00
self.application.set_normal_cursor()
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def load_file(self, data):
2011-07-27 18:28:35 +00:00
"""
Turn file from Drag and Drop into an array so the Validate code can run it.
2011-07-27 18:28:35 +00:00
2013-03-14 11:32:58 +00:00
``data``
A dictionary containing the list of files to be loaded and the target
2011-07-27 18:28:35 +00:00
"""
2012-07-01 19:22:11 +00:00
new_files = []
error_shown = False
2013-01-30 10:57:06 +00:00
for file_name in data['files']:
2013-08-31 18:17:38 +00:00
file_type = file_name.split('.')[-1]
2013-03-19 22:00:50 +00:00
if file_type.lower() not in self.on_new_file_masks:
2012-07-01 19:22:11 +00:00
if not error_shown:
2012-12-28 22:06:43 +00:00
critical_error_message_box(translate('OpenLP.MediaManagerItem', 'Invalid File Type'),
2013-02-01 19:58:18 +00:00
translate('OpenLP.MediaManagerItem', 'Invalid File %s.\nSuffix not supported') % file_name)
2012-10-03 18:52:52 +00:00
error_shown = True
2011-08-02 05:07:09 +00:00
else:
2013-02-01 19:58:18 +00:00
new_files.append(file_name)
2012-07-01 19:22:11 +00:00
if new_files:
2013-03-19 22:00:50 +00:00
self.validate_and_load(new_files, data['target'])
2011-07-27 18:28:35 +00:00
2013-01-30 10:57:06 +00:00
def dnd_move_internal(self, target):
"""
Handle internal moving of media manager items
2011-07-27 18:28:35 +00:00
2013-03-14 11:32:58 +00:00
``target``
The target of the DnD action
"""
pass
2013-03-19 22:00:50 +00:00
def validate_and_load(self, files, target_group=None):
2011-07-27 18:28:35 +00:00
"""
Process a list for files either from the File Dialog or from Drag and
Drop
``files``
The files to be loaded.
2013-03-14 11:32:58 +00:00
``target_group``
The QTreeWidgetItem of the group that will be the parent of the added files
2011-07-27 18:28:35 +00:00
"""
names = []
2012-07-01 19:22:11 +00:00
full_list = []
2013-03-19 22:00:50 +00:00
for count in range(self.list_view.count()):
names.append(self.list_view.item(count).text())
full_list.append(self.list_view.item(count).data(QtCore.Qt.UserRole))
2012-07-01 19:22:11 +00:00
duplicates_found = False
files_added = False
2013-01-30 10:57:06 +00:00
for file_path in files:
2013-08-31 18:17:38 +00:00
filename = os.path.split(str(file_path))[1]
2011-07-27 18:28:35 +00:00
if filename in names:
2012-07-01 19:22:11 +00:00
duplicates_found = True
2011-07-27 18:28:35 +00:00
else:
2012-07-01 19:22:11 +00:00
files_added = True
full_list.append(file_path)
2012-07-01 19:22:11 +00:00
if full_list and files_added:
if target_group is None:
2013-03-19 22:00:50 +00:00
self.list_view.clear()
self.load_list(full_list, target_group)
2013-08-31 18:17:38 +00:00
last_dir = os.path.split(str(files[0]))[0]
Settings().setValue(self.settings_section + '/last directory', last_dir)
Settings().setValue('%s/%s files' % (self.settings_section, self.settings_section), self.get_file_list())
2012-07-01 19:22:11 +00:00
if duplicates_found:
2012-12-28 22:06:43 +00:00
critical_error_message_box(UiStrings().Duplicate,
translate('OpenLP.MediaManagerItem', 'Duplicate files were found on import and were ignored.'))
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def context_menu(self, point):
2013-02-01 19:58:18 +00:00
"""
Display a context menu
"""
2013-03-19 22:00:50 +00:00
item = self.list_view.itemAt(point)
# Decide if we have to show the context menu or not.
2011-05-13 13:00:20 +00:00
if item is None:
return
if not item.flags() & QtCore.Qt.ItemIsSelectable:
return
2013-03-19 22:00:50 +00:00
self.menu.exec_(self.list_view.mapToGlobal(point))
2011-05-13 13:00:20 +00:00
2013-03-19 22:00:50 +00:00
def get_file_list(self):
2010-06-19 17:31:42 +00:00
"""
Return the current list of files
"""
2012-07-01 19:22:11 +00:00
file_list = []
2013-08-31 18:17:38 +00:00
for index in range(self.list_view.count()):
2013-03-19 22:00:50 +00:00
bitem = self.list_view.item(index)
2012-05-19 15:10:05 +00:00
filename = bitem.data(QtCore.Qt.UserRole)
2012-07-01 19:22:11 +00:00
file_list.append(filename)
return file_list
2009-06-27 19:55:55 +00:00
2013-03-19 22:00:50 +00:00
def load_list(self, list, target_group):
2013-02-01 19:58:18 +00:00
"""
Load a list. Needs to be implemented by the plugin.
"""
2013-08-31 18:17:38 +00:00
raise NotImplementedError('MediaManagerItem.loadList needs to be defined by the plugin')
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def on_new_click(self):
2011-02-03 23:25:52 +00:00
"""
Hook for plugins to define behaviour for adding new items.
"""
pass
2009-06-27 05:46:05 +00:00
2013-03-19 22:00:50 +00:00
def on_edit_click(self):
2011-02-03 23:25:52 +00:00
"""
Hook for plugins to define behaviour for editing items.
"""
pass
2009-06-27 05:46:05 +00:00
2013-03-19 22:00:50 +00:00
def on_delete_click(self):
2013-02-01 19:58:18 +00:00
"""
Delete an item. Needs to be implemented by the plugin.
"""
2013-08-31 18:17:38 +00:00
raise NotImplementedError('MediaManagerItem.on_delete_click needs to be defined by the plugin')
2009-06-23 20:53:06 +00:00
2013-04-18 09:27:11 +00:00
def on_focus(self):
"""
Run when a tab in the media manager gains focus. This gives the media
item a chance to focus any elements it wants to.
"""
pass
2013-03-19 22:00:50 +00:00
def generate_slide_data(self, service_item, item=None, xml_version=False, remote=False,
2013-03-08 08:22:24 +00:00
context=ServiceItemContext.Live):
2013-02-01 19:58:18 +00:00
"""
Generate the slide data. Needs to be implemented by the plugin.
"""
2013-08-31 18:17:38 +00:00
raise NotImplementedError('MediaManagerItem.generate_slide_data needs to be defined by the plugin')
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def on_double_clicked(self):
"""
Allows the list click action to be determined dynamically
"""
2013-08-31 18:17:38 +00:00
if Settings().value('advanced/double click live'):
2013-03-19 22:00:50 +00:00
self.on_live_click()
else:
2013-03-19 22:00:50 +00:00
self.on_preview_click()
2013-03-19 22:00:50 +00:00
def on_selection_change(self):
"""
Allows the change of current item in the list to be actioned
"""
2013-08-31 18:17:38 +00:00
if Settings().value('advanced/single click preview') and self.quick_preview_allowed \
2013-03-20 18:35:28 +00:00
and self.list_view.selectedIndexes() and self.auto_select_id == -1:
2013-03-19 22:00:50 +00:00
self.on_preview_click(True)
2013-03-20 18:35:28 +00:00
def on_preview_click(self, keep_focus=False):
2010-06-19 17:31:42 +00:00
"""
2013-03-07 08:05:43 +00:00
Preview an item by building a service item then adding that service item to the preview slide controller.
2010-06-19 17:31:42 +00:00
"""
2013-03-19 22:00:50 +00:00
if not self.list_view.selectedIndexes() and not self.remote_triggered:
QtGui.QMessageBox.information(self, UiStrings().NISp,
2012-12-28 22:06:43 +00:00
translate('OpenLP.MediaManagerItem', 'You must select one or more items to preview.'))
2009-12-06 13:55:07 +00:00
else:
2013-08-31 18:17:38 +00:00
log.debug('%s Preview requested', self.plugin.name)
2013-03-19 22:00:50 +00:00
service_item = self.build_service_item()
2013-03-06 22:23:01 +00:00
if service_item:
service_item.from_plugin = True
self.preview_controller.add_service_item(service_item)
2013-03-20 18:35:28 +00:00
if keep_focus:
2013-03-19 22:00:50 +00:00
self.list_view.setFocus()
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def on_live_click(self):
2010-06-19 17:31:42 +00:00
"""
2013-03-07 08:05:43 +00:00
Send an item live by building a service item then adding that service item to the live slide controller.
2010-06-19 17:31:42 +00:00
"""
2013-03-19 22:00:50 +00:00
if not self.list_view.selectedIndexes():
QtGui.QMessageBox.information(self, UiStrings().NISp,
2012-12-28 22:06:43 +00:00
translate('OpenLP.MediaManagerItem', 'You must select one or more items to send live.'))
2009-12-06 13:55:07 +00:00
else:
2013-03-19 22:00:50 +00:00
self.go_live()
2013-03-22 21:05:42 +00:00
def go_live_remote(self, message):
"""
Remote Call wrapper
2013-04-14 15:59:57 +00:00
``message``
The passed data item_id:Remote.
2013-03-22 21:05:42 +00:00
"""
self.go_live(message[0], remote=message[1])
2013-03-19 22:00:50 +00:00
def go_live(self, item_id=None, remote=False):
2013-02-01 19:58:18 +00:00
"""
Make the currently selected item go live.
"""
2013-08-31 18:17:38 +00:00
log.debug('%s Live requested', self.plugin.name)
item = None
if item_id:
2013-03-19 22:00:50 +00:00
item = self.create_item_from_id(item_id)
service_item = self.build_service_item(item, remote=remote)
2013-03-06 22:23:01 +00:00
if service_item:
2011-05-14 12:26:05 +00:00
if not item_id:
2013-03-06 22:23:01 +00:00
service_item.from_plugin = True
2012-10-18 20:38:01 +00:00
if remote:
2013-03-06 22:23:01 +00:00
service_item.will_auto_start = True
self.live_controller.add_service_item(service_item)
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def create_item_from_id(self, item_id):
2013-02-01 19:58:18 +00:00
"""
Create a media item from an item id.
"""
item = QtGui.QListWidgetItem()
2012-05-17 15:13:09 +00:00
item.setData(QtCore.Qt.UserRole, item_id)
return item
2009-06-23 20:53:06 +00:00
2013-03-19 22:00:50 +00:00
def on_add_click(self):
2010-06-19 17:31:42 +00:00
"""
Add a selected item to the current service
"""
2013-03-19 22:00:50 +00:00
if not self.list_view.selectedIndexes():
QtGui.QMessageBox.information(self, UiStrings().NISp,
2012-12-28 22:06:43 +00:00
translate('OpenLP.MediaManagerItem', 'You must select one or more items to add.'))
2009-12-06 13:55:07 +00:00
else:
2013-01-23 21:05:25 +00:00
# Is it possible to process multiple list items to generate
2010-11-28 19:38:27 +00:00
# multiple service items?
2013-03-19 22:00:50 +00:00
if self.single_service_item:
2013-08-31 18:17:38 +00:00
log.debug('%s Add requested', self.plugin.name)
2013-03-19 22:00:50 +00:00
self.add_to_service(replace=self.remote_triggered)
2010-04-04 13:53:39 +00:00
else:
2013-03-19 22:00:50 +00:00
items = self.list_view.selectedIndexes()
2010-04-04 13:53:39 +00:00
for item in items:
2013-03-19 22:00:50 +00:00
self.add_to_service(item)
2011-05-19 23:09:42 +00:00
2013-03-22 21:05:42 +00:00
def add_to_service_remote(self, message):
"""
Remote Call wrapper
2013-04-14 15:59:57 +00:00
``message``
The passed data item:Remote.
2013-03-22 21:05:42 +00:00
"""
self.add_to_service(message[0], remote=message[1])
2011-05-19 23:09:42 +00:00
2013-03-19 22:00:50 +00:00
def add_to_service(self, item=None, replace=None, remote=False):
2013-02-01 19:58:18 +00:00
"""
Add this item to the current service.
"""
2013-03-19 22:00:50 +00:00
service_item = self.build_service_item(item, True, remote=remote, context=ServiceItemContext.Service)
2013-03-06 22:23:01 +00:00
if service_item:
service_item.from_plugin = False
self.service_manager.add_service_item(service_item, replace=replace)
2009-07-08 16:40:42 +00:00
2013-03-19 22:00:50 +00:00
def on_add_edit_click(self):
2010-06-19 17:31:42 +00:00
"""
Add a selected item to an existing item in the current service.
"""
2013-03-19 22:00:50 +00:00
if not self.list_view.selectedIndexes() and not self.remote_triggered:
QtGui.QMessageBox.information(self, UiStrings().NISp,
2012-12-28 22:06:43 +00:00
translate('OpenLP.MediaManagerItem', 'You must select one or more items.'))
2010-03-16 20:22:28 +00:00
else:
2013-08-31 18:17:38 +00:00
log.debug('%s Add requested', self.plugin.name)
2013-03-06 22:23:01 +00:00
service_item = self.service_manager.get_service_item()
if not service_item:
QtGui.QMessageBox.information(self, UiStrings().NISs,
2012-12-28 22:06:43 +00:00
translate('OpenLP.MediaManagerItem', 'You must select an existing service item to add to.'))
2013-03-06 22:23:01 +00:00
elif self.plugin.name == service_item.name:
2013-03-19 22:00:50 +00:00
self.generate_slide_data(service_item)
2013-03-06 22:23:01 +00:00
self.service_manager.add_service_item(service_item, replace=True)
2010-03-20 08:34:36 +00:00
else:
# Turn off the remote edit update message indicator
2012-12-28 22:06:43 +00:00
QtGui.QMessageBox.information(self, translate('OpenLP.MediaManagerItem', 'Invalid Service Item'),
translate('OpenLP.MediaManagerItem', 'You must select a %s service item.') % self.title)
2010-03-16 20:22:28 +00:00
2013-03-20 18:35:28 +00:00
def build_service_item(self, item=None, xml_version=False, remote=False, context=ServiceItemContext.Live):
2009-07-08 16:40:42 +00:00
"""
Common method for generating a service item
"""
2013-03-06 22:23:01 +00:00
service_item = ServiceItem(self.plugin)
2013-03-19 20:05:13 +00:00
service_item.add_icon(self.plugin.icon_path)
2013-03-20 18:35:28 +00:00
if self.generate_slide_data(service_item, item, xml_version, remote, context):
2013-03-06 22:23:01 +00:00
return service_item
else:
2010-07-26 15:19:11 +00:00
return None
2010-09-30 05:12:06 +00:00
2013-04-21 15:53:51 +00:00
def service_load(self, item):
2010-09-30 05:12:06 +00:00
"""
2013-03-07 08:05:43 +00:00
Method to add processing when a service has been loaded and individual service items need to be processed by the
plugins.
2013-04-21 15:53:51 +00:00
``item``
The item to be processed and returned.
2010-09-30 05:12:06 +00:00
"""
2013-04-21 15:53:51 +00:00
return item
2011-02-01 00:33:50 +00:00
2013-03-19 22:00:50 +00:00
def check_search_result(self):
2011-05-15 12:11:08 +00:00
"""
2013-03-19 22:00:50 +00:00
Checks if the list_view is empty and adds a "No Search Results" item.
2011-05-15 12:11:08 +00:00
"""
2013-03-19 22:00:50 +00:00
if self.list_view.count():
2011-05-15 12:11:08 +00:00
return
message = translate('OpenLP.MediaManagerItem', 'No Search Results')
item = QtGui.QListWidgetItem(message)
item.setFlags(QtCore.Qt.NoItemFlags)
font = QtGui.QFont()
font.setItalic(True)
item.setFont(font)
2013-03-19 22:00:50 +00:00
self.list_view.addItem(item)
2011-05-15 12:11:08 +00:00
2013-03-20 18:35:28 +00:00
def _get_id_of_item_to_generate(self, item, remote_item):
2011-02-01 00:33:50 +00:00
"""
Utility method to check items being submitted for slide generation.
``item``
The item to check.
2013-03-20 18:35:28 +00:00
``remote_item``
2011-02-01 00:33:50 +00:00
The id to assign if the slide generation was remotely triggered.
"""
if item is None:
2013-03-19 22:00:50 +00:00
if self.remote_triggered is None:
item = self.list_view.currentItem()
2011-02-01 00:33:50 +00:00
if item is None:
return False
2012-05-19 09:13:32 +00:00
item_id = item.data(QtCore.Qt.UserRole)
2011-02-01 00:33:50 +00:00
else:
2013-03-20 18:35:28 +00:00
item_id = remote_item
2011-02-01 00:33:50 +00:00
else:
2012-05-19 09:13:32 +00:00
item_id = item.data(QtCore.Qt.UserRole)
2011-04-29 08:45:36 +00:00
return item_id
2013-03-19 22:00:50 +00:00
def save_auto_select_id(self):
"""
Sorts out, what item to select after loading a list.
"""
# The item to select has not been set.
2013-03-20 18:35:28 +00:00
if self.auto_select_id == -1:
2013-03-19 22:00:50 +00:00
item = self.list_view.currentItem()
if item:
2013-03-20 18:35:28 +00:00
self.auto_select_id = item.data(QtCore.Qt.UserRole)
2013-03-20 18:35:28 +00:00
def search(self, string, show_error=True):
"""
Performs a plugin specific search for items containing ``string``
"""
2013-08-31 18:17:38 +00:00
raise NotImplementedError('Plugin.search needs to be defined by the plugin')
def _get_main_window(self):
"""
Adds the main window to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_main_window'):
self._main_window = Registry().get('main_window')
return self._main_window
main_window = property(_get_main_window)
def _get_renderer(self):
"""
Adds the Renderer to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_renderer'):
self._renderer = Registry().get('renderer')
return self._renderer
renderer = property(_get_renderer)
def _get_live_controller(self):
"""
Adds the live controller to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_live_controller'):
self._live_controller = Registry().get('live_controller')
return self._live_controller
live_controller = property(_get_live_controller)
def _get_preview_controller(self):
"""
Adds the preview controller to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_preview_controller'):
self._preview_controller = Registry().get('preview_controller')
return self._preview_controller
preview_controller = property(_get_preview_controller)
def _get_plugin_manager(self):
"""
Adds the plugin manager to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_plugin_manager'):
self._plugin_manager = Registry().get('plugin_manager')
return self._plugin_manager
plugin_manager = property(_get_plugin_manager)
def _get_media_controller(self):
"""
Adds the media controller to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_media_controller'):
self._media_controller = Registry().get('media_controller')
return self._media_controller
media_controller = property(_get_media_controller)
def _get_service_manager(self):
"""
2013-01-26 07:39:07 +00:00
Adds the service manager to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_service_manager'):
self._service_manager = Registry().get('service_manager')
return self._service_manager
2013-01-26 07:39:07 +00:00
service_manager = property(_get_service_manager)
2013-02-02 07:08:28 +00:00
def _get_theme_manager(self):
"""
Adds the theme manager to the class dynamically
"""
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_theme_manager'):
self._theme_manager = Registry().get('theme_manager')
2013-02-02 07:08:28 +00:00
return self._theme_manager
theme_manager = property(_get_theme_manager)
2013-02-03 19:23:12 +00:00
def _get_application(self):
2013-02-03 09:07:31 +00:00
"""
2013-06-21 05:16:35 +00:00
Adds the openlp to the class dynamically.
Windows needs to access the application in a dynamic manner.
2013-02-03 09:07:31 +00:00
"""
2013-08-31 18:17:38 +00:00
if os.name == 'nt':
return Registry().get('application')
2013-06-21 05:16:35 +00:00
else:
2013-08-31 18:17:38 +00:00
if not hasattr(self, '_application'):
self._application = Registry().get('application')
2013-06-21 05:16:35 +00:00
return self._application
2013-02-03 09:07:31 +00:00
2013-02-03 19:23:12 +00:00
application = property(_get_application)