openlp/openlp/plugins/media/lib/mediaitem.py

436 lines
21 KiB
Python
Raw Normal View History

2009-05-15 05:15:53 +00:00
# -*- coding: utf-8 -*-
2012-12-09 09:26:47 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2009-05-15 05:15:53 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2013-12-24 08:56:50 +00:00
# Copyright (c) 2008-2014 Raoul Snyman #
# Portions copyright (c) 2008-2014 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 #
###############################################################################
2009-05-15 05:15:53 +00:00
import logging
import os
from datetime import time
2009-05-15 05:15:53 +00:00
from PyQt4 import QtCore, QtGui
2014-03-16 21:25:23 +00:00
from openlp.core.common import Registry, RegistryProperties, AppLocation, Settings, check_directory_exists, UiStrings,\
translate
2014-03-21 18:23:35 +00:00
from openlp.core.lib import ItemCapabilities, MediaManagerItem, MediaType, ServiceItem, ServiceItemContext, \
2013-10-13 20:36:42 +00:00
build_icon, check_item_selected
from openlp.core.lib.ui import critical_error_message_box, create_horizontal_adjusting_combo_box
2012-10-20 19:52:04 +00:00
from openlp.core.ui import DisplayController, Display, DisplayControllerType
from openlp.core.ui.media import get_media_players, set_media_players, parse_optical_path, format_milliseconds
2013-10-13 13:51:13 +00:00
from openlp.core.utils import get_locale_key
from openlp.core.ui.media.vlcplayer import VLC_AVAILABLE
2014-04-22 20:27:15 +00:00
if VLC_AVAILABLE:
from openlp.plugins.media.forms.mediaclipselectorform import MediaClipSelectorForm
2010-02-27 09:55:44 +00:00
log = logging.getLogger(__name__)
2011-03-24 19:04:02 +00:00
2013-04-19 19:03:16 +00:00
2013-08-31 18:17:38 +00:00
CLAPPERBOARD = ':/media/slidecontroller_multimedia.png'
OPTICAL = ':/media/media_optical.png'
2013-08-31 18:17:38 +00:00
VIDEO_ICON = build_icon(':/media/media_video.png')
AUDIO_ICON = build_icon(':/media/media_audio.png')
OPTICAL_ICON = build_icon(OPTICAL)
2013-08-31 18:17:38 +00:00
ERROR_ICON = build_icon(':/general/general_delete.png')
2011-07-27 18:28:35 +00:00
2013-04-19 19:03:16 +00:00
2014-03-16 21:25:23 +00:00
class MediaMediaItem(MediaManagerItem, RegistryProperties):
2009-05-15 05:15:53 +00:00
"""
This is the custom media manager item for Media Slides.
2009-05-15 05:15:53 +00:00
"""
2013-08-31 18:17:38 +00:00
log.info('%s MediaMediaItem loaded', __name__)
2009-05-15 05:15:53 +00:00
2013-03-07 13:14:31 +00:00
def __init__(self, parent, plugin):
2013-08-31 18:17:38 +00:00
self.icon_path = 'images/image'
2010-04-30 16:30:25 +00:00
self.background = False
2013-08-31 18:17:38 +00:00
self.automatic = ''
2013-07-18 19:36:52 +00:00
super(MediaMediaItem, self).__init__(parent, plugin)
def setup_item(self):
"""
Do some additional setup.
"""
2013-03-19 22:00:50 +00:00
self.single_service_item = False
2013-03-23 06:46:41 +00:00
self.has_search = True
self.media_object = None
self.display_controller = DisplayController(self.parent())
self.display_controller.controller_layout = QtGui.QVBoxLayout()
self.media_controller.register_controller(self.display_controller)
self.media_controller.set_controls_visible(self.display_controller, False)
2014-01-11 17:52:01 +00:00
self.display_controller.preview_display = Display(self.display_controller)
self.display_controller.preview_display.hide()
self.display_controller.preview_display.setGeometry(QtCore.QRect(0, 0, 300, 300))
2013-08-31 18:17:38 +00:00
self.display_controller.preview_display.screen = {'size': self.display_controller.preview_display.geometry()}
self.display_controller.preview_display.setup()
self.media_controller.setup_display(self.display_controller.preview_display, False)
2013-08-31 18:17:38 +00:00
Registry().register_function('video_background_replaced', self.video_background_replaced)
Registry().register_function('mediaitem_media_rebuild', self.rebuild_players)
Registry().register_function('config_screen_changed', self.display_setup)
2011-07-27 18:28:35 +00:00
# Allow DnD from the desktop
2013-03-19 22:00:50 +00:00
self.list_view.activateDnD()
2009-05-15 05:15:53 +00:00
2009-10-31 16:17:26 +00:00
def retranslateUi(self):
"""
This method is called automatically to provide OpenLP with the opportunity to translate the ``MediaManagerItem``
to another language.
"""
2013-03-19 22:00:50 +00:00
self.on_new_prompt = translate('MediaPlugin.MediaItem', 'Select Media')
2013-04-19 19:15:12 +00:00
self.replace_action.setText(UiStrings().ReplaceBG)
self.replace_action.setToolTip(UiStrings().ReplaceLiveBG)
self.reset_action.setText(UiStrings().ResetBG)
self.reset_action.setToolTip(UiStrings().ResetLiveBG)
2012-11-22 22:02:40 +00:00
self.automatic = UiStrings().Automatic
2013-04-19 19:15:12 +00:00
self.display_type_label.setText(translate('MediaPlugin.MediaItem', 'Use Player:'))
2013-03-19 22:00:50 +00:00
def required_icons(self):
"""
Set which icons the media manager tab should show
"""
MediaManagerItem.required_icons(self)
self.has_file_icon = True
self.has_new_icon = False
self.has_edit_icon = False
2009-09-26 09:11:39 +00:00
2013-03-19 22:00:50 +00:00
def add_list_view_to_toolbar(self):
"""
Creates the main widget for listing items.
"""
2013-03-19 22:00:50 +00:00
MediaManagerItem.add_list_view_to_toolbar(self)
2013-04-19 19:15:12 +00:00
self.list_view.addAction(self.replace_action)
2010-04-30 16:30:25 +00:00
def add_start_header_bar(self):
"""
Adds buttons to the start of the header bar.
"""
if 'vlc' in get_media_players()[0]:
diable_optical_button_text = False
optical_button_text = translate('MediaPlugin.MediaItem', 'Load CD/DVD')
optical_button_tooltip = translate('MediaPlugin.MediaItem', 'Load CD/DVD')
else:
diable_optical_button_text = True
optical_button_text = translate('MediaPlugin.MediaItem', 'Load CD/DVD')
optical_button_tooltip = translate('MediaPlugin.MediaItem',
'Load CD/DVD - only supported when VLC is installed and enabled')
self.load_optical = self.toolbar.add_toolbar_action('load_optical', icon=OPTICAL_ICON, text=optical_button_text,
tooltip=optical_button_tooltip,
triggers=self.on_load_optical)
if diable_optical_button_text:
self.load_optical.setDisabled(True)
2013-03-19 22:00:50 +00:00
def add_end_header_bar(self):
"""
Adds buttons to the end of the header bar.
"""
2010-12-29 09:14:13 +00:00
# Replace backgrounds do not work at present so remove functionality.
2013-08-31 18:17:38 +00:00
self.replace_action = self.toolbar.add_toolbar_action('replace_action', icon=':/slides/slide_blank.png',
2013-12-31 20:29:03 +00:00
triggers=self.on_replace_click)
2013-08-31 18:17:38 +00:00
self.reset_action = self.toolbar.add_toolbar_action('reset_action', icon=':/system/system_close.png',
2013-12-31 20:29:03 +00:00
visible=False, triggers=self.on_reset_click)
2013-04-19 19:15:12 +00:00
self.media_widget = QtGui.QWidget(self)
2013-08-31 18:17:38 +00:00
self.media_widget.setObjectName('media_widget')
2013-04-19 19:15:12 +00:00
self.display_layout = QtGui.QFormLayout(self.media_widget)
self.display_layout.setMargin(self.display_layout.spacing())
2013-08-31 18:17:38 +00:00
self.display_layout.setObjectName('display_layout')
2013-04-19 19:15:12 +00:00
self.display_type_label = QtGui.QLabel(self.media_widget)
2013-08-31 18:17:38 +00:00
self.display_type_label.setObjectName('display_type_label')
2013-04-19 19:15:12 +00:00
self.display_type_combo_box = create_horizontal_adjusting_combo_box(
2013-08-31 18:17:38 +00:00
self.media_widget, 'display_type_combo_box')
2013-04-19 19:15:12 +00:00
self.display_type_label.setBuddy(self.display_type_combo_box)
self.display_layout.addRow(self.display_type_label, self.display_type_combo_box)
# Add the Media widget to the page layout.
self.page_layout.addWidget(self.media_widget)
2013-12-31 20:29:03 +00:00
self.display_type_combo_box.currentIndexChanged.connect(self.override_player_changed)
2012-12-30 19:49:48 +00:00
2013-12-31 20:29:03 +00:00
def override_player_changed(self, index):
"""
2014-03-08 20:53:22 +00:00
The Player has been overridden
2014-03-08 20:53:22 +00:00
:param index: Index
"""
2012-03-16 21:52:15 +00:00
player = get_media_players()[0]
if index == 0:
set_media_players(player)
else:
2014-04-12 20:19:22 +00:00
set_media_players(player, player[index - 1])
2010-04-30 16:30:25 +00:00
2013-12-31 20:29:03 +00:00
def on_reset_click(self):
"""
2011-08-29 19:55:58 +00:00
Called to reset the Live background with the media selected,
"""
self.media_controller.media_reset(self.live_controller)
2013-04-19 19:15:12 +00:00
self.reset_action.setVisible(False)
2010-07-02 18:21:45 +00:00
2013-02-07 11:33:47 +00:00
def video_background_replaced(self):
"""
Triggered by main display on change of serviceitem.
"""
2013-04-19 19:15:12 +00:00
self.reset_action.setVisible(False)
2013-12-31 20:29:03 +00:00
def on_replace_click(self):
"""
2011-08-29 19:55:58 +00:00
Called to replace Live background with the media selected.
"""
2013-03-19 22:00:50 +00:00
if check_item_selected(self.list_view,
translate('MediaPlugin.MediaItem',
2013-12-31 20:29:03 +00:00
'You must select a media file to replace the background with.')):
2013-03-19 22:00:50 +00:00
item = self.list_view.currentItem()
filename = item.data(QtCore.Qt.UserRole)
if os.path.exists(filename):
service_item = ServiceItem()
2013-08-31 18:17:38 +00:00
service_item.title = 'webkit'
service_item.processor = 'webkit'
(path, name) = os.path.split(filename)
service_item.add_from_command(path, name, CLAPPERBOARD)
2013-03-06 22:50:49 +00:00
if self.media_controller.video(DisplayControllerType.Live, service_item, video_behind_text=True):
2013-04-19 19:15:12 +00:00
self.reset_action.setVisible(True)
else:
critical_error_message_box(UiStrings().LiveBGError,
translate('MediaPlugin.MediaItem',
2013-12-31 20:29:03 +00:00
'There was no display item to amend.'))
else:
critical_error_message_box(UiStrings().LiveBGError,
2013-12-31 20:29:03 +00:00
translate('MediaPlugin.MediaItem',
'There was a problem replacing your background, '
'the media file "%s" no longer exists.') % filename)
2010-04-30 16:30:25 +00:00
2013-04-19 19:15:12 +00:00
def generate_slide_data(self, service_item, item=None, xml_version=False, remote=False,
2014-03-08 20:53:22 +00:00
context=ServiceItemContext.Service):
2013-03-19 22:00:50 +00:00
"""
Generate the slide data. Needs to be implemented by the plugin.
2014-03-08 20:53:22 +00:00
:param service_item: The service item to be built on
:param item: The Song item to be used
:param xml_version: The xml version (not used)
:param remote: Triggered from remote
:param context: Why is it being generated
2013-03-19 22:00:50 +00:00
"""
2010-04-05 07:22:21 +00:00
if item is None:
2013-03-19 22:00:50 +00:00
item = self.list_view.currentItem()
2010-04-05 07:22:21 +00:00
if item is None:
return False
filename = item.data(QtCore.Qt.UserRole)
# Special handling if the filename is a optical clip
if filename.startswith('optical:'):
(name, title, audio_track, subtitle_track, start, end, clip_name) = parse_optical_path(filename)
if not os.path.exists(name):
if not remote:
# Optical disc is no longer present
critical_error_message_box(
translate('MediaPlugin.MediaItem', 'Missing Media File'),
translate('MediaPlugin.MediaItem', 'The optical disc %s is no longer available.') % name)
return False
service_item.processor = self.display_type_combo_box.currentText()
service_item.add_from_command(filename, name, CLAPPERBOARD)
service_item.title = clip_name
# Set the length
self.media_controller.media_setup_optical(name, title, audio_track, subtitle_track, start, end, None, None)
service_item.set_media_length((end - start) / 1000)
service_item.start_time = start / 1000
service_item.end_time = end / 1000
service_item.add_capability(ItemCapabilities.IsOptical)
else:
if not os.path.exists(filename):
if not remote:
# File is no longer present
critical_error_message_box(
translate('MediaPlugin.MediaItem', 'Missing Media File'),
translate('MediaPlugin.MediaItem', 'The file %s no longer exists.') % filename)
return False
(path, name) = os.path.split(filename)
service_item.title = name
service_item.processor = self.display_type_combo_box.currentText()
service_item.add_from_command(path, name, CLAPPERBOARD)
# Only get start and end times if going to a service
if context == ServiceItemContext.Service:
# Start media and obtain the length
if not self.media_controller.media_length(service_item):
return False
2012-09-09 06:54:09 +00:00
service_item.add_capability(ItemCapabilities.CanAutoStartForLive)
2013-07-08 21:26:31 +00:00
service_item.add_capability(ItemCapabilities.CanEditTitle)
service_item.add_capability(ItemCapabilities.RequiresMedia)
2013-08-31 18:17:38 +00:00
if Settings().value(self.settings_section + '/media auto start') == QtCore.Qt.Checked:
2012-09-09 06:54:09 +00:00
service_item.will_auto_start = True
# force a non-existent theme
service_item.theme = -1
return True
def initialise(self):
"""
Initialize media item.
"""
2013-03-19 22:00:50 +00:00
self.list_view.clear()
self.list_view.setIconSize(QtCore.QSize(88, 50))
2013-12-31 20:29:03 +00:00
self.service_path = os.path.join(AppLocation.get_section_data_path(self.settings_section), 'thumbnails')
check_directory_exists(self.service_path)
2013-08-31 18:17:38 +00:00
self.load_list(Settings().value(self.settings_section + '/media files'))
self.rebuild_players()
2014-04-22 20:13:36 +00:00
if VLC_AVAILABLE:
self.media_clip_selector_form = MediaClipSelectorForm(self, self.main_window, None)
2011-09-22 18:22:35 +00:00
def rebuild_players(self):
2011-09-22 18:22:35 +00:00
"""
2013-04-19 19:15:12 +00:00
Rebuild the tab in the media manager when changes are made in the settings.
2011-09-22 18:22:35 +00:00
"""
2013-12-31 20:29:03 +00:00
self.populate_display_types()
2013-03-19 22:00:50 +00:00
self.on_new_file_masks = translate('MediaPlugin.MediaItem', 'Videos (%s);;Audio (%s);;%s (*)') % (
2013-08-31 18:17:38 +00:00
' '.join(self.media_controller.video_extensions_list),
' '.join(self.media_controller.audio_extensions_list), UiStrings().AllFiles)
2011-12-01 18:07:15 +00:00
2013-02-07 11:33:47 +00:00
def display_setup(self):
"""
Setup media controller display.
"""
2013-03-06 22:59:25 +00:00
self.media_controller.setup_display(self.display_controller.preview_display, False)
2011-12-01 18:07:15 +00:00
2013-12-31 20:29:03 +00:00
def populate_display_types(self):
2011-09-22 18:22:35 +00:00
"""
2013-04-19 19:15:12 +00:00
Load the combobox with the enabled media players, allowing user to select a specific player if settings allow.
2011-09-22 18:22:35 +00:00
"""
2013-12-31 20:29:03 +00:00
# block signals to avoid unnecessary override_player_changed Signals while combo box creation
2013-04-19 19:15:12 +00:00
self.display_type_combo_box.blockSignals(True)
self.display_type_combo_box.clear()
2013-12-31 20:29:03 +00:00
used_players, override_player = get_media_players()
2013-03-06 17:46:19 +00:00
media_players = self.media_controller.media_players
2013-12-31 20:29:03 +00:00
current_index = 0
for player in used_players:
2011-09-22 18:22:35 +00:00
# load the drop down selection
2013-04-19 19:15:12 +00:00
self.display_type_combo_box.addItem(media_players[player].original_name)
2013-12-31 20:29:03 +00:00
if override_player == player:
current_index = len(self.display_type_combo_box)
2013-04-19 19:15:12 +00:00
if self.display_type_combo_box.count() > 1:
self.display_type_combo_box.insertItem(0, self.automatic)
2013-12-31 20:29:03 +00:00
self.display_type_combo_box.setCurrentIndex(current_index)
if override_player:
2013-04-19 19:15:12 +00:00
self.media_widget.show()
2011-09-22 18:22:35 +00:00
else:
2013-04-19 19:15:12 +00:00
self.media_widget.hide()
self.display_type_combo_box.blockSignals(False)
2013-03-19 22:00:50 +00:00
def on_delete_click(self):
"""
Remove a media item from the list.
"""
2013-03-19 22:00:50 +00:00
if check_item_selected(self.list_view,
2013-12-31 20:29:03 +00:00
translate('MediaPlugin.MediaItem', 'You must select a media file to delete.')):
2013-03-19 22:00:50 +00:00
row_list = [item.row() for item in self.list_view.selectedIndexes()]
row_list.sort(reverse=True)
for row in row_list:
2013-03-19 22:00:50 +00:00
self.list_view.takeItem(row)
2013-08-31 18:17:38 +00:00
Settings().setValue(self.settings_section + '/media files', self.get_file_list())
2013-03-19 22:00:50 +00:00
def load_list(self, media, target_group=None):
"""
2014-03-08 20:53:22 +00:00
Load the media list
2014-03-08 20:53:22 +00:00
:param media: The media
:param target_group:
"""
2013-12-31 20:29:03 +00:00
media.sort(key=lambda file_name: get_locale_key(os.path.split(str(file_name))[1]))
2011-07-27 18:28:35 +00:00
for track in media:
2011-09-22 18:22:35 +00:00
track_info = QtCore.QFileInfo(track)
if track.startswith('optical:'):
# Handle optical based item
(file_name, title, audio_track, subtitle_track, start, end, clip_name) = parse_optical_path(track)
item_name = QtGui.QListWidgetItem(clip_name)
item_name.setIcon(OPTICAL_ICON)
item_name.setData(QtCore.Qt.UserRole, track)
2014-06-17 07:27:12 +00:00
item_name.setToolTip('%s@%s-%s' % (file_name, format_milliseconds(start), format_milliseconds(end)))
elif not os.path.exists(track):
# File doesn't exist, mark as error.
2013-12-31 20:29:03 +00:00
file_name = os.path.split(str(track))[1]
item_name = QtGui.QListWidgetItem(file_name)
2013-04-19 19:15:12 +00:00
item_name.setIcon(ERROR_ICON)
item_name.setData(QtCore.Qt.UserRole, track)
item_name.setToolTip(track)
2012-11-04 14:25:48 +00:00
elif track_info.isFile():
# Normal media file handling.
2013-12-31 20:29:03 +00:00
file_name = os.path.split(str(track))[1]
item_name = QtGui.QListWidgetItem(file_name)
if '*.%s' % (file_name.split('.')[-1].lower()) in self.media_controller.audio_extensions_list:
2013-04-19 19:15:12 +00:00
item_name.setIcon(AUDIO_ICON)
else:
2013-04-19 19:15:12 +00:00
item_name.setIcon(VIDEO_ICON)
item_name.setData(QtCore.Qt.UserRole, track)
item_name.setToolTip(track)
2013-03-19 22:00:50 +00:00
self.list_view.addItem(item_name)
2011-04-29 16:41:26 +00:00
2013-04-19 19:15:12 +00:00
def get_list(self, type=MediaType.Audio):
"""
Get the list of media, optional select media type.
:param type: Type to get, defaults to audio.
:return: The media list
"""
2013-08-31 18:17:38 +00:00
media = Settings().value(self.settings_section + '/media files')
media.sort(key=lambda filename: get_locale_key(os.path.split(str(filename))[1]))
if type == MediaType.Audio:
2013-04-19 19:15:12 +00:00
extension = self.media_controller.audio_extensions_list
else:
2013-04-19 19:15:12 +00:00
extension = self.media_controller.video_extensions_list
2013-08-31 18:17:38 +00:00
extension = [x[1:] for x in extension]
media = [x for x in media if os.path.splitext(x)[1] in extension]
return media
2013-12-31 20:29:03 +00:00
def search(self, string, show_error):
"""
Performs a search for items containing ``string``
:param string: String to be displayed
:param show_error: Should the error be shown (True)
:return: The search result.
"""
2013-08-31 18:17:38 +00:00
files = Settings().value(self.settings_section + '/media files')
results = []
string = string.lower()
for file in files:
2013-08-31 18:17:38 +00:00
filename = os.path.split(str(file))[1]
if filename.lower().find(string) > -1:
results.append([file, filename])
return results
def on_load_optical(self):
"""
When the load optical button is clicked, open the clip selector window.
"""
self.media_clip_selector_form.exec_()
def add_optical_clip(self, optical):
"""
Add a optical based clip to the mediamanager, called from media_clip_selector_form.
:param optical: The clip to add.
"""
full_list = self.get_file_list()
# If the clip already is in the media list it isn't added and an error message is displayed.
if optical in full_list:
critical_error_message_box(translate('MediaPlugin.MediaItem', 'Mediaclip already saved'),
translate('MediaPlugin.MediaItem', 'This mediaclip has already been saved'))
return
# Append the optical string to the media list
full_list.append(optical)
self.load_list([optical])
Settings().setValue(self.settings_section + '/media files', self.get_file_list())