openlp/openlp/core/lib/mediamanageritem.py

403 lines
15 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2009-12-31 12:52:01 +00:00
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard #
# --------------------------------------------------------------------------- #
# 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 types
2009-06-27 15:33:03 +00:00
import os
from PyQt4 import QtCore, QtGui
from openlp.core.lib.toolbar import *
2009-10-24 16:40:36 +00:00
from openlp.core.lib import contextMenuAction, contextMenuSeparator
2009-06-23 20:59:38 +00:00
from serviceitem import ServiceItem
2010-02-27 15:31:23 +00:00
log = logging.getLogger(__name__)
class MediaManagerItem(QtGui.QWidget):
"""
MediaManagerItem is a helper widget for plugins.
2009-07-01 20:21:13 +00:00
None of the following *need* to be used, feel free to override
2009-10-24 16:40:36 +00:00
them completely in your plugin's implementation. Alternatively,
2009-09-03 21:41:34 +00:00
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``
The parent widget. Usually this will be the *Media Manager*
itself. This needs to be a class descended from ``QWidget``.
2009-09-03 21:41:34 +00:00
``icon``
Either a ``QIcon``, a resource path, or a file name. This is
the icon which is displayed in the *Media Manager*.
2009-09-03 21:41:34 +00:00
``title``
The title visible on the item in the *Media Manager*.
2009-09-03 21:41:34 +00:00
**Member Variables**
When creating a descendant class from this class for your plugin,
the following member variables should be set.
2009-10-28 01:36:24 +00:00
``self.PluginNameShort``
The shortened (usually singular) name for the plugin e.g. *'Song'*
for the Songs plugin.
``self.PluginNameVisible``
The user visible name for a plugin which should use a suitable
2009-10-30 17:44:16 +00:00
translation function.
2009-09-03 21:41:34 +00:00
``self.ConfigSection``
The section in the configuration where the items in the media
manager are stored. This could potentially be
2009-10-28 01:36:24 +00:00
``self.PluginNameShort.lower()``.
2009-09-03 21:41:34 +00:00
``self.OnNewPrompt``
Defaults to *'Select Image(s)'*.
``self.OnNewFileMasks``
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.
``self.ListViewWithDnD_class``
This must be a **class**, not an object, descended from
``openlp.core.lib.BaseListWithDnD`` that is not used in any
other part of OpenLP.
``self.PreviewFunction``
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).
"""
2009-06-23 20:59:38 +00:00
log.info(u'Media Item loaded')
def __init__(self, parent=None, icon=None, title=None):
"""
Constructor to create the media manager item.
"""
QtGui.QWidget.__init__(self)
self.parent = parent
if type(icon) is QtGui.QIcon:
self.icon = icon
elif type(icon) is types.StringType:
self.icon.addPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(icon)),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
else:
self.icon = None
2009-11-03 18:14:25 +00:00
if title:
self.title = title
self.Toolbar = None
2009-11-04 17:48:46 +00:00
self.remoteTriggered = None
2009-10-30 17:44:16 +00:00
self.ServiceItemIconName = None
self.PageLayout = QtGui.QVBoxLayout(self)
2009-10-17 18:52:42 +00:00
self.PageLayout.setSpacing(0)
self.PageLayout.setContentsMargins(4, 0, 4, 0)
2009-09-26 09:11:39 +00:00
self.requiredIcons()
self.initPluginNameVisible()
self.setupUi()
self.retranslateUi()
def initPluginNameVisible(self):
pass
2009-09-26 09:11:39 +00:00
def requiredIcons(self):
"""
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.
"""
self.hasNewIcon = True
self.hasEditIcon = True
self.hasFileIcon = False
self.hasDeleteIcon = True
def retranslateUi(self):
2009-09-04 22:50:19 +00:00
"""
This method is called automatically to provide OpenLP with the
opportunity to translate the ``MediaManagerItem`` to another
language.
"""
pass
def addToolbar(self):
"""
2009-09-04 22:50:19 +00:00
A method to help developers easily add a toolbar to the media
manager item.
"""
if self.Toolbar is None:
2009-05-20 20:17:20 +00:00
self.Toolbar = OpenLPToolbar(self)
self.PageLayout.addWidget(self.Toolbar)
2009-09-21 19:57:36 +00:00
def addToolbarButton(
self, title, tooltip, icon, slot=None, checkable=False):
"""
A method to help developers easily add a button to the toolbar.
2009-09-04 22:50:19 +00:00
``title``
The title of the button.
``tooltip``
The tooltip to be displayed when the mouse hovers over the
button.
``icon``
The icon of the button. This can be an instance of QIcon, or a
string cotaining either the absolute path to the image, or an
internal resource path starting with ':/'.
``slot``
The method to call when the button is clicked.
``objectname``
The name of the button.
"""
2009-09-04 22:50:19 +00:00
# NB different order (when I broke this out, I didn't want to
# break compatability), but it makes sense for the icon to
# come before the tooltip (as you have to have an icon, but
# not neccesarily a tooltip)
self.Toolbar.addToolbarButton(title, icon, tooltip, slot, checkable)
def addToolbarSeparator(self):
"""
A very simple method to add a separator to the toolbar.
"""
self.Toolbar.addSeparator()
2009-06-23 20:53:06 +00:00
def setupUi(self):
2009-09-04 22:50:19 +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-06-23 20:53:06 +00:00
# Add a toolbar
self.addToolbar()
2009-09-16 04:59:38 +00:00
#Allow the plugin to define buttons at start of bar
self.addStartHeaderBar()
2009-09-16 04:59:38 +00:00
#Add the middle of the tool bar (pre defined)
self.addMiddleHeaderBar()
#Allow the plugin to define buttons at end of bar
self.addEndHeaderBar()
#Add the list view
self.addListViewToToolBar()
def addMiddleHeaderBar(self):
2009-06-23 20:53:06 +00:00
# Create buttons for the toolbar
2009-06-27 05:46:05 +00:00
## File Button ##
if self.hasFileIcon:
2009-09-21 18:49:06 +00:00
self.addToolbarButton(
2009-10-28 01:36:24 +00:00
u'Load %s' % self.PluginNameShort,
u'%s %s' % (self.trUtf8('Load a new'), self.PluginNameVisible),
2009-12-02 09:20:40 +00:00
u':/%s_load.png' % self.IconPath, self.onFileClick)
2009-06-27 05:46:05 +00:00
## New Button ##
if self.hasNewIcon:
2009-09-21 18:49:06 +00:00
self.addToolbarButton(
2009-10-28 01:36:24 +00:00
u'New %s' % self.PluginNameShort,
u'%s %s' % (self.trUtf8('Add a new'), self.PluginNameVisible),
2009-12-02 09:20:40 +00:00
u':/%s_new.png' % self.IconPath, self.onNewClick)
2009-06-27 05:46:05 +00:00
## Edit Button ##
if self.hasEditIcon:
self.addToolbarButton(
2009-10-28 01:36:24 +00:00
u'Edit %s' % self.PluginNameShort,
u'%s %s' % (self.trUtf8('Edit the selected'),
2009-10-30 17:44:16 +00:00
self.PluginNameVisible),
2009-12-02 09:20:40 +00:00
u':/%s_edit.png' % self.IconPath, self.onEditClick)
2009-06-27 05:46:05 +00:00
## Delete Button ##
2009-09-26 09:11:39 +00:00
if self.hasDeleteIcon:
self.addToolbarButton(
2009-10-28 01:36:24 +00:00
u'Delete %s' % self.PluginNameShort,
self.trUtf8('Delete the selected item'),
2009-12-02 09:20:40 +00:00
u':/%s_delete.png' % self.IconPath, self.onDeleteClick)
2009-06-23 20:53:06 +00:00
## Separator Line ##
self.addToolbarSeparator()
2009-07-01 20:21:13 +00:00
## Preview ##
2009-06-23 20:53:06 +00:00
self.addToolbarButton(
2009-10-28 01:36:24 +00:00
u'Preview %s' % self.PluginNameShort,
self.trUtf8('Preview the selected item'),
u':/system/system_preview.png', self.onPreviewClick)
2009-06-23 20:53:06 +00:00
## Live Button ##
self.addToolbarButton(
2009-10-28 01:36:24 +00:00
u'Go Live',
self.trUtf8('Send the selected item live'),
u':/system/system_live.png', self.onLiveClick)
2009-07-01 20:21:13 +00:00
## Add to service Button ##
2009-06-23 20:53:06 +00:00
self.addToolbarButton(
u'Add %s to Service' % self.PluginNameShort,
self.trUtf8('Add the selected item(s) to the service'),
u':/system/system_add.png', self.onAddClick)
2009-09-16 04:59:38 +00:00
def addListViewToToolBar(self):
#Add the List widget
self.ListView = self.ListViewWithDnD_class(self)
self.ListView.uniformItemSizes = True
self.ListView.setGeometry(QtCore.QRect(10, 100, 256, 591))
self.ListView.setSpacing(1)
2009-09-21 17:56:36 +00:00
self.ListView.setSelectionMode(
QtGui.QAbstractItemView.ExtendedSelection)
self.ListView.setAlternatingRowColors(True)
self.ListView.setDragEnabled(True)
2009-10-28 01:36:24 +00:00
self.ListView.setObjectName(u'%sListView' % self.PluginNameShort)
2009-06-27 05:46:05 +00:00
#Add tp PageLayout
self.PageLayout.addWidget(self.ListView)
2009-06-23 20:53:06 +00:00
#define and add the context menu
self.ListView.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
2009-06-27 05:46:05 +00:00
if self.hasEditIcon:
self.ListView.addAction(
contextMenuAction(
self.ListView, u':/%s_new.png' % self.IconPath,
u'%s %s' % (self.trUtf8('&Edit'), self.PluginNameVisible),
self.onEditClick))
2009-09-11 04:54:22 +00:00
self.ListView.addAction(contextMenuSeparator(self.ListView))
self.ListView.addAction(
contextMenuAction(
self.ListView, u':/system/system_preview.png',
u'%s %s' % (self.trUtf8('&Preview'), self.PluginNameVisible),
self.onPreviewClick))
self.ListView.addAction(
contextMenuAction(
self.ListView, u':/system/system_live.png',
self.trUtf8('&Show Live'), self.onLiveClick))
self.ListView.addAction(
contextMenuAction(
self.ListView, u':/system/system_add.png',
self.trUtf8('&Add to Service'), self.onAddClick))
QtCore.QObject.connect(
self.ListView, QtCore.SIGNAL(u'doubleClicked(QModelIndex)'),
self.onPreviewClick)
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
do any UI or other initialisation. This method is called
automatically.
"""
pass
2009-06-23 20:53:06 +00:00
def addStartHeaderBar(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
def addEndHeaderBar(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
2009-06-27 05:46:05 +00:00
def onFileClick(self):
files = QtGui.QFileDialog.getOpenFileNames(
2009-10-30 17:44:16 +00:00
self, self.OnNewPrompt,
self.parent.config.get_last_dir(), self.OnNewFileMasks)
2009-06-27 19:55:55 +00:00
log.info(u'New files(s)%s', unicode(files))
2010-03-09 19:43:11 +00:00
if files:
2009-06-23 20:53:06 +00:00
self.loadList(files)
dir, filename = os.path.split(unicode(files[0]))
self.parent.config.set_last_dir(dir)
self.parent.config.set_list(self.ConfigSection, self.getFileList())
2009-06-23 20:53:06 +00:00
2009-06-27 19:55:55 +00:00
def getFileList(self):
count = 0
filelist = []
while count < self.ListView.count():
2009-09-21 17:56:36 +00:00
bitem = self.ListView.item(count)
filename = unicode((bitem.data(QtCore.Qt.UserRole)).toString())
filelist.append(filename)
2009-06-27 19:55:55 +00:00
count += 1
return filelist
2009-06-23 20:53:06 +00:00
def loadList(self, list):
raise NotImplementedError(u'MediaManagerItem.loadList needs to be '
u'defined by the plugin')
2009-06-23 20:53:06 +00:00
2009-06-27 05:46:05 +00:00
def onNewClick(self):
raise NotImplementedError(u'MediaManagerItem.onNewClick needs to be '
u'defined by the plugin')
2009-06-27 05:46:05 +00:00
def onEditClick(self):
raise NotImplementedError(u'MediaManagerItem.onEditClick needs to be '
u'defined by the plugin')
2009-06-27 05:46:05 +00:00
2009-06-23 20:53:06 +00:00
def onDeleteClick(self):
raise NotImplementedError(u'MediaManagerItem.onDeleteClick needs to '
u'be defined by the plugin')
2009-06-23 20:53:06 +00:00
def generateSlideData(self, item):
raise NotImplementedError(u'MediaManagerItem.generateSlideData needs '
u'to be defined by the plugin')
2009-06-23 20:53:06 +00:00
def onPreviewClick(self):
2009-11-04 17:48:46 +00:00
if not self.ListView.selectedIndexes() and not self.remoteTriggered:
2009-11-03 01:12:35 +00:00
QtGui.QMessageBox.information(self,
self.trUtf8('No items selected...'),
self.trUtf8('You must select one or more items'))
2009-12-06 13:55:07 +00:00
else:
log.debug(self.PluginNameShort + u' Preview requested')
2009-12-06 13:55:07 +00:00
service_item = self.buildServiceItem()
if service_item:
service_item.fromPlugin = True
self.parent.preview_controller.addServiceItem(service_item)
2009-06-23 20:53:06 +00:00
def onLiveClick(self):
2009-11-03 01:12:35 +00:00
if not self.ListView.selectedIndexes():
QtGui.QMessageBox.information(self,
self.trUtf8('No items selected...'),
self.trUtf8('You must select one or more items'))
2009-12-06 13:55:07 +00:00
else:
log.debug(self.PluginNameShort + u' Live requested')
2009-12-06 13:55:07 +00:00
service_item = self.buildServiceItem()
if service_item:
service_item.fromPlugin = True
self.parent.live_controller.addServiceItem(service_item)
2009-06-23 20:53:06 +00:00
def onAddClick(self):
2009-11-04 17:48:46 +00:00
if not self.ListView.selectedIndexes() and not self.remoteTriggered:
2009-11-03 01:12:35 +00:00
QtGui.QMessageBox.information(self,
self.trUtf8('No items selected...'),
self.trUtf8('You must select one or more items'))
2009-12-06 13:55:07 +00:00
else:
log.debug(self.PluginNameShort + u' Add requested')
2009-12-06 13:55:07 +00:00
service_item = self.buildServiceItem()
if service_item:
service_item.fromPlugin = False
self.parent.service_manager.addServiceItem(service_item)
2009-07-08 16:40:42 +00:00
def buildServiceItem(self):
"""
Common method for generating a service item
"""
2009-06-23 20:53:06 +00:00
service_item = ServiceItem(self.parent)
2009-11-03 18:14:25 +00:00
if self.ServiceItemIconName:
service_item.addIcon(self.ServiceItemIconName)
else:
service_item.addIcon(
u':/media/media_' + self.PluginNameShort.lower() + u'.png')
if self.generateSlideData(service_item):
return service_item
else:
return None