openlp/openlp/core/lib/__init__.py

189 lines
6.8 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-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):
"""
Open a file and return the contents of the file. If the supplied file name
is not a file then the function returns False. If there is an error
loading the file 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')
content_string = file_handle.read()
2009-10-01 23:43:16 +00:00
except IOError:
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 stringvalue is True or stringvalue is False:
return stringvalue
2009-05-21 05:15:51 +00:00
return stringvalue.strip().lower() in (u'true', u'yes', u'y')
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``.
"""
2009-05-21 05:15:51 +00:00
ButtonIcon = None
if isinstance(icon, QtGui.QIcon):
2009-05-21 05:15:51 +00:00
ButtonIcon = icon
elif isinstance(icon, basestring):
2009-09-29 12:51:38 +00:00
ButtonIcon = QtGui.QIcon()
2009-05-21 05:15:51 +00:00
if icon.startswith(u':/'):
2009-09-29 12:51:38 +00:00
ButtonIcon.addPixmap(
QtGui.QPixmap(icon), QtGui.QIcon.Normal, QtGui.QIcon.Off)
2009-05-21 05:15:51 +00:00
else:
2009-12-06 19:22:41 +00:00
ButtonIcon.addPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(icon)),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
elif isinstance(icon, QtGui.QImage):
2009-09-29 12:51:38 +00:00
ButtonIcon = QtGui.QIcon()
ButtonIcon.addPixmap(
QtGui.QPixmap.fromImage(icon), QtGui.QIcon.Normal, QtGui.QIcon.Off)
2009-05-21 05:15:51 +00:00
return ButtonIcon
def contextMenuAction(base, icon, text, slot):
"""
Utility method to help build context menus for plugins
"""
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
def contextMenu(base, icon, text):
"""
Utility method to help build context menus for plugins
"""
action = QtGui.QMenu(text, base)
action.setIcon(build_icon(icon))
return action
def contextMenuSeparator(base):
2010-05-25 23:07:50 +00:00
"""
Add a separator to a context menu
"""
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():
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-05-27 08:42:56 +00:00
newImage = QtGui.QImage(width, height,
QtGui.QImage.Format_ARGB32_Premultiplied)
newImage.fill(QtCore.Qt.black)
painter = QtGui.QPainter(newImage)
painter.drawImage((width - realw) / 2, (height - realh) / 2, preview)
return newImage
class ThemeLevel(object):
2010-05-25 23:07:50 +00:00
"""
Provides an enumeration for the level a theme applies to
"""
Global = 1
Service = 2
Song = 3
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 xmlrootclass import XmlRootClass
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
from songxmlhandler import SongXMLBuilder, SongXMLParser
from themexmlhandler import ThemeXML
from renderer import Renderer
from rendermanager import RenderManager
from mediamanageritem import MediaManagerItem
2010-05-28 00:26:49 +00:00
from basemodel import BaseModel
2009-06-29 05:13:06 +00:00
from baselistwithdnd import BaseListWithDnD