openlp/openlp/core/lib/__init__.py

222 lines
7.7 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 #
2010-03-21 23:58:01 +00:00
# Gorven, Scott Guerrieri, Christian Richter, 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 #
###############################################################################
"""
The :mod:`lib` module contains most of the components and libraries that make
OpenLP work.
"""
2009-11-08 14:16:02 +00:00
import logging
import os.path
2009-05-20 20:17:20 +00:00
import types
2009-09-25 23:06:54 +00:00
2009-09-29 12:51:38 +00:00
from PyQt4 import QtCore, QtGui
2009-05-21 05:15:51 +00:00
2009-11-08 14:16:02 +00:00
log = logging.getLogger(__name__)
2010-04-20 19:09:14 +00:00
def translate(context, text, comment=None):
"""
A special shortcut method to wrap around the Qt4 translation functions.
This abstracts the translation procedure so that we can change it if at a
later date if necessary, without having to redo the whole of OpenLP.
``context``
The translation context, used to give each string a context or a
namespace.
``text``
The text to put into the translation tables for translation.
2010-06-15 15:42:52 +00:00
``comment``
An identifying string for when the same text is used in different roles
within the same context.
"""
2010-05-27 08:42:56 +00:00
return QtCore.QCoreApplication.translate(context, text, comment)
2009-05-21 05:15:51 +00:00
def get_text_file_string(text_file):
"""
2010-06-19 10:41:13 +00:00
Open a file and return its content as unicode string. If the supplied file
name is not a file then the function returns False. If there is an error
loading the file or the content can't be decoded then the function will
return None.
``textfile``
The name of the file.
"""
if not os.path.isfile(text_file):
return False
file_handle = None
content_string = None
2009-10-01 23:43:16 +00:00
try:
file_handle = open(text_file, u'r')
2010-06-19 10:41:13 +00:00
content = file_handle.read()
content_string = content.decode(u'utf-8')
except (IOError, UnicodeError):
2010-05-26 16:01:45 +00:00
log.exception(u'Failed to open text file %s' % text_file)
2009-10-01 23:43:16 +00:00
finally:
if file_handle:
file_handle.close()
return content_string
2009-05-21 05:15:51 +00:00
def str_to_bool(stringvalue):
"""
Convert a string version of a boolean into a real boolean.
``stringvalue``
The string value to examine and convert to a boolean type.
"""
if isinstance(stringvalue, bool):
return stringvalue
return unicode(stringvalue).strip().lower() in (u'true', u'yes', u'y')
2009-05-21 05:15:51 +00:00
def build_icon(icon):
"""
Build a QIcon instance from an existing QIcon, a resource location, or a
physical file location. If the icon is a QIcon instance, that icon is
simply returned. If not, it builds a QIcon instance from the resource or
file name.
``icon``
The icon to build. This can be a QIcon, a resource string in the form
``:/resource/file.png``, or a file location like ``/path/to/file.png``.
"""
2010-06-19 13:18:38 +00:00
button_icon = QtGui.QIcon()
if isinstance(icon, QtGui.QIcon):
2010-06-12 20:22:58 +00:00
button_icon = icon
elif isinstance(icon, basestring):
2009-05-21 05:15:51 +00:00
if icon.startswith(u':/'):
2010-06-12 20:22:58 +00:00
button_icon.addPixmap(QtGui.QPixmap(icon), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
2009-05-21 05:15:51 +00:00
else:
2010-06-12 20:22:58 +00:00
button_icon.addPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(icon)),
2009-12-06 19:22:41 +00:00
QtGui.QIcon.Normal, QtGui.QIcon.Off)
elif isinstance(icon, QtGui.QImage):
2010-06-12 20:22:58 +00:00
button_icon.addPixmap(QtGui.QPixmap.fromImage(icon),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
return button_icon
2009-05-21 05:15:51 +00:00
2010-06-12 20:22:58 +00:00
def context_menu_action(base, icon, text, slot):
"""
Utility method to help build context menus for plugins
2010-06-15 15:42:52 +00:00
``base``
The parent menu to add this menu item to
``icon``
An icon for this action
``text``
The text to display for this action
``slot``
The code to run when this action is triggered
"""
2009-09-29 12:51:38 +00:00
action = QtGui.QAction(text, base)
2009-11-03 18:14:25 +00:00
if icon:
action.setIcon(build_icon(icon))
2009-09-29 12:51:38 +00:00
QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered()'), slot)
return action
2010-06-12 20:22:58 +00:00
def context_menu(base, icon, text):
"""
Utility method to help build context menus for plugins
2010-06-15 15:42:52 +00:00
``base``
The parent object to add this menu to
``icon``
An icon for this menu
``text``
The text to display for this menu
"""
action = QtGui.QMenu(text, base)
action.setIcon(build_icon(icon))
return action
2010-06-12 20:22:58 +00:00
def context_menu_separator(base):
2010-05-25 23:07:50 +00:00
"""
Add a separator to a context menu
2010-06-15 15:42:52 +00:00
``base``
The menu object to add the separator to
2010-05-25 23:07:50 +00:00
"""
action = QtGui.QAction(u'', base)
action.setSeparator(True)
return action
2009-05-21 05:15:51 +00:00
def resize_image(image, width, height):
"""
Resize an image to fit on the current screen.
``image``
The image to resize.
"""
2010-01-22 18:59:36 +00:00
preview = QtGui.QImage(image)
if not preview.isNull():
2010-07-11 20:53:53 +00:00
if preview.width() == width and preview.height == height:
2010-07-10 22:21:14 +00:00
return preview
preview = preview.scaled(width, height, QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation)
2010-02-26 23:47:59 +00:00
realw = preview.width()
realh = preview.height()
# and move it to the centre of the preview space
2010-06-12 20:22:58 +00:00
new_image = QtGui.QImage(width, height,
2010-05-27 08:42:56 +00:00
QtGui.QImage.Format_ARGB32_Premultiplied)
2010-06-12 20:22:58 +00:00
new_image.fill(QtCore.Qt.black)
painter = QtGui.QPainter(new_image)
painter.drawImage((width - realw) / 2, (height - realh) / 2, preview)
2010-06-12 20:22:58 +00:00
return new_image
2010-06-24 15:50:40 +00:00
def check_item_selected(list_widget, message):
"""
Check if a list item is selected so an action may be performed on it
``list_widget``
The list to check for selected items
``message``
The message to give the user if no item is selected
"""
if not list_widget.selectedIndexes():
2010-06-24 19:04:18 +00:00
QtGui.QMessageBox.information(list_widget.parent(),
2010-06-24 15:50:40 +00:00
translate('MediaManagerItem', 'No Items Selected'), message)
return False
return True
2009-09-04 22:50:19 +00:00
from eventreceiver import Receiver
2009-07-08 17:18:48 +00:00
from settingsmanager import SettingsManager
2009-09-21 18:59:14 +00:00
from plugin import PluginStatus, Plugin
2009-07-08 17:18:48 +00:00
from pluginmanager import PluginManager
from settingstab import SettingsTab
from serviceitem import ServiceItem
from serviceitem import ServiceItemType
2010-04-03 07:10:31 +00:00
from serviceitem import ItemCapabilities
from toolbar import OpenLPToolbar
2009-09-19 11:25:01 +00:00
from dockwidget import OpenLPDockWidget
2010-07-03 13:26:29 +00:00
from theme import ThemeLevel, ThemeXML
from renderer import Renderer
from rendermanager import RenderManager
from mediamanageritem import MediaManagerItem
2009-06-29 05:13:06 +00:00
from baselistwithdnd import BaseListWithDnD