openlp/openlp/core/lib/__init__.py

348 lines
12 KiB
Python
Raw Normal View History

2010-09-10 19:47:33 +00:00
# -*- coding: utf-8 -*-
2012-12-28 22:06:43 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2012-12-27 16:27:59 +00:00
2010-09-10 19:47:33 +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 #
2010-09-10 19:47:33 +00:00
# --------------------------------------------------------------------------- #
# 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.
"""
2013-08-31 18:17:38 +00:00
2013-02-06 19:49:39 +00:00
from distutils.version import LooseVersion
2010-09-10 19:47:33 +00:00
import logging
2012-04-12 14:16:12 +00:00
import os
2010-09-10 19:47:33 +00:00
2012-02-17 18:51:01 +00:00
from PyQt4 import QtCore, QtGui, Qt
2010-09-10 19:47:33 +00:00
2013-10-13 20:36:42 +00:00
from openlp.core.common import translate
2010-09-10 19:47:33 +00:00
log = logging.getLogger(__name__)
2013-03-23 07:07:06 +00:00
class ServiceItemContext(object):
"""
The context in which a Service Item is being generated
"""
Preview = 0
Live = 1
Service = 2
2012-07-01 18:41:59 +00:00
class ImageSource(object):
"""
2013-03-07 12:30:24 +00:00
This enumeration class represents different image sources. An image sources states where an image is used. This
enumeration class is need in the context of the :class:~openlp.core.lib.imagemanager`.
2012-07-01 19:41:12 +00:00
``ImagePlugin``
This states that an image is being used by the image plugin.
``Theme``
This says, that the image is used by a theme.
2012-07-01 18:41:59 +00:00
"""
ImagePlugin = 1
Theme = 2
class MediaType(object):
"""
An enumeration class for types of media.
"""
Audio = 1
Video = 2
class ServiceItemAction(object):
"""
2013-03-07 12:30:24 +00:00
Provides an enumeration for the required action moving between service items by left/right arrow keys
"""
Previous = 1
PreviousLastSlide = 2
Next = 3
2012-04-22 19:50:18 +00:00
2010-09-10 19:47:33 +00:00
def get_text_file_string(text_file):
"""
2013-03-07 12:30:24 +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.
2010-09-10 19:47:33 +00:00
``textfile``
The name of the file.
"""
if not os.path.isfile(text_file):
return False
file_handle = None
2013-03-27 09:25:39 +00:00
content = None
2010-09-10 19:47:33 +00:00
try:
2013-08-31 18:17:38 +00:00
file_handle = open(text_file, 'r')
if not file_handle.read(3) == '\xEF\xBB\xBF':
# no BOM was found
file_handle.seek(0)
2010-09-10 19:47:33 +00:00
content = file_handle.read()
except (IOError, UnicodeError):
2013-08-31 18:17:38 +00:00
log.exception('Failed to open text file %s' % text_file)
2010-09-10 19:47:33 +00:00
finally:
if file_handle:
file_handle.close()
2013-03-27 09:25:39 +00:00
return content
2010-09-10 19:47:33 +00:00
2012-04-22 19:50:18 +00:00
2013-03-07 12:30:24 +00:00
def str_to_bool(string_value):
2010-09-10 19:47:33 +00:00
"""
Convert a string version of a boolean into a real boolean.
2013-03-07 12:30:24 +00:00
``string_value``
2010-09-10 19:47:33 +00:00
The string value to examine and convert to a boolean type.
"""
2013-03-07 12:30:24 +00:00
if isinstance(string_value, bool):
return string_value
2013-08-31 18:17:38 +00:00
return str(string_value).strip().lower() in ('true', 'yes', 'y')
2010-09-10 19:47:33 +00:00
2012-04-22 19:50:18 +00:00
2010-09-10 19:47:33 +00:00
def build_icon(icon):
"""
2013-03-07 12:30:24 +00:00
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.
2010-09-10 19:47:33 +00:00
``icon``
2013-03-07 12:30:24 +00:00
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``. However, the **recommended** way is to specify a resource string.
2010-09-10 19:47:33 +00:00
"""
button_icon = QtGui.QIcon()
if isinstance(icon, QtGui.QIcon):
button_icon = icon
2013-08-31 18:17:38 +00:00
elif isinstance(icon, str):
if icon.startswith(':/'):
2012-12-28 22:06:43 +00:00
button_icon.addPixmap(QtGui.QPixmap(icon), QtGui.QIcon.Normal, QtGui.QIcon.Off)
2010-09-10 19:47:33 +00:00
else:
2012-12-28 22:06:43 +00:00
button_icon.addPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(icon)), QtGui.QIcon.Normal, QtGui.QIcon.Off)
2010-09-10 19:47:33 +00:00
elif isinstance(icon, QtGui.QImage):
2012-12-28 22:06:43 +00:00
button_icon.addPixmap(QtGui.QPixmap.fromImage(icon), QtGui.QIcon.Normal, QtGui.QIcon.Off)
2010-09-10 19:47:33 +00:00
return button_icon
2012-04-22 19:50:18 +00:00
2010-09-10 19:47:33 +00:00
def image_to_byte(image):
"""
2013-03-07 12:30:24 +00:00
Resize an image to fit on the current screen for the web and returns it as a byte stream.
2010-09-10 19:47:33 +00:00
``image``
The image to converted.
"""
2013-08-31 18:17:38 +00:00
log.debug('image_to_byte - start')
2010-09-10 19:47:33 +00:00
byte_array = QtCore.QByteArray()
# use buffer to store pixmap into byteArray
buffie = QtCore.QBuffer(byte_array)
buffie.open(QtCore.QIODevice.WriteOnly)
2010-10-15 15:33:06 +00:00
image.save(buffie, "PNG")
2013-08-31 18:17:38 +00:00
log.debug('image_to_byte - end')
2010-09-10 19:47:33 +00:00
# convert to base64 encoding so does not get missed!
2013-03-27 09:41:01 +00:00
return bytes(byte_array.toBase64()).decode('utf-8')
2010-09-10 19:47:33 +00:00
2012-04-22 19:50:18 +00:00
2011-06-12 15:59:46 +00:00
def create_thumb(image_path, thumb_path, return_icon=True, size=None):
2011-06-12 15:17:01 +00:00
"""
2013-03-07 12:30:24 +00:00
Create a thumbnail from the given image path and depending on ``return_icon`` it returns an icon from this thumb.
2011-06-12 15:17:01 +00:00
``image_path``
The image file to create the icon from.
``thumb_path``
The filename to save the thumbnail to.
``return_icon``
2013-03-07 12:30:24 +00:00
States if an icon should be build and returned from the thumb. Defaults to ``True``.
2011-06-12 15:59:46 +00:00
``size``
2013-05-18 08:44:03 +00:00
Allows to state a own size (QtCore.QSize) to use. Defaults to ``None``, which means that a default height of 88
is used.
2011-06-12 15:17:01 +00:00
"""
ext = os.path.splitext(thumb_path)[1].lower()
reader = QtGui.QImageReader(image_path)
2011-06-12 15:59:46 +00:00
if size is None:
2013-04-24 19:05:34 +00:00
ratio = reader.size().width() / reader.size().height()
2011-06-12 15:59:46 +00:00
reader.setScaledSize(QtCore.QSize(int(ratio * 88), 88))
else:
reader.setScaledSize(size)
2011-06-12 15:17:01 +00:00
thumb = reader.read()
thumb.save(thumb_path, ext[1:])
if not return_icon:
return
if os.path.exists(thumb_path):
2013-08-31 18:17:38 +00:00
return build_icon(str(thumb_path))
2011-06-12 15:17:01 +00:00
# Fallback for files with animation support.
2013-08-31 18:17:38 +00:00
return build_icon(str(image_path))
2011-06-12 15:17:01 +00:00
2012-04-22 19:50:18 +00:00
2011-06-12 15:59:46 +00:00
def validate_thumb(file_path, thumb_path):
2011-06-12 15:17:01 +00:00
"""
2013-03-07 12:30:24 +00:00
Validates whether an file's thumb still exists and if is up to date. **Note**, you must **not** call this function,
before checking the existence of the file.
2011-06-12 15:17:01 +00:00
2011-06-12 15:59:46 +00:00
``file_path``
2011-06-20 19:56:01 +00:00
The path to the file. The file **must** exist!
2011-06-12 15:17:01 +00:00
``thumb_path``
The path to the thumb.
"""
if not os.path.exists(thumb_path):
2011-06-12 15:17:01 +00:00
return False
image_date = os.stat(file_path).st_mtime
thumb_date = os.stat(thumb_path).st_mtime
2011-06-12 15:17:01 +00:00
return image_date <= thumb_date
2012-04-22 19:50:18 +00:00
2013-08-31 18:17:38 +00:00
def resize_image(image_path, width, height, background='#000000'):
2010-09-10 19:47:33 +00:00
"""
Resize an image to fit on the current screen.
2011-06-07 07:30:50 +00:00
``image_path``
The path to the image to resize.
2010-09-10 19:47:33 +00:00
``width``
The new image width.
``height``
The new image height.
2011-03-10 19:17:05 +00:00
``background``
2011-10-01 07:28:45 +00:00
The background colour. Defaults to black.
DO NOT REMOVE THE DEFAULT BACKGROUND VALUE!
2010-09-10 19:47:33 +00:00
"""
2013-08-31 18:17:38 +00:00
log.debug('resize_image - start')
2011-06-09 10:41:02 +00:00
reader = QtGui.QImageReader(image_path)
2011-06-07 07:30:50 +00:00
# The image's ratio.
2013-04-24 19:05:34 +00:00
image_ratio = reader.size().width() / reader.size().height()
resize_ratio = width / height
2011-06-07 07:30:50 +00:00
# Figure out the size we want to resize the image to (keep aspect ratio).
if image_ratio == resize_ratio:
size = QtCore.QSize(width, height)
elif image_ratio < resize_ratio:
# Use the image's height as reference for the new size.
size = QtCore.QSize(image_ratio * height, height)
2010-10-15 15:33:06 +00:00
else:
2011-06-07 07:30:50 +00:00
# Use the image's width as reference for the new size.
size = QtCore.QSize(width, 1 / (image_ratio / width))
reader.setScaledSize(size)
preview = reader.read()
if image_ratio == resize_ratio:
# We neither need to centre the image nor add "bars" to the image.
return preview
2012-07-01 18:45:14 +00:00
real_width = preview.width()
real_height = preview.height()
2010-09-10 19:47:33 +00:00
# and move it to the centre of the preview space
2012-12-28 22:06:43 +00:00
new_image = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32_Premultiplied)
2010-09-10 19:47:33 +00:00
painter = QtGui.QPainter(new_image)
painter.fillRect(new_image.rect(), QtGui.QColor(background))
2013-04-24 19:05:34 +00:00
painter.drawImage((width - real_width) // 2, (height - real_height) // 2, preview)
2010-09-10 19:47:33 +00:00
return new_image
2012-04-22 19:50:18 +00:00
2010-09-10 19:47:33 +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():
QtGui.QMessageBox.information(list_widget.parent(),
2013-12-24 08:56:50 +00:00
translate('OpenLP.MediaManagerItem', 'No Items Selected'), message)
2010-09-10 19:47:33 +00:00
return False
return True
2012-04-22 19:50:18 +00:00
2010-09-10 19:47:33 +00:00
def clean_tags(text):
"""
Remove Tags from text for display
"""
2013-08-31 18:17:38 +00:00
text = text.replace('<br>', '\n')
text = text.replace('{br}', '\n')
text = text.replace('&nbsp;', ' ')
for tag in FormattingTags.get_html_tags():
2013-08-31 18:17:38 +00:00
text = text.replace(tag['start tag'], '')
text = text.replace(tag['end tag'], '')
2010-09-10 19:47:33 +00:00
return text
2012-04-22 19:50:18 +00:00
2010-09-10 19:47:33 +00:00
def expand_tags(text):
"""
Expand tags HTML for display
"""
for tag in FormattingTags.get_html_tags():
2013-08-31 18:17:38 +00:00
text = text.replace(tag['start tag'], tag['start html'])
text = text.replace(tag['end tag'], tag['end html'])
2010-09-10 19:47:33 +00:00
return text
2012-04-22 19:50:18 +00:00
2013-10-13 17:23:52 +00:00
def create_separated_list(string_list):
2012-02-16 20:36:35 +00:00
"""
2013-03-07 12:30:24 +00:00
Returns a string that represents a join of a list of strings with a localized separator. This function corresponds
to QLocale::createSeparatedList which was introduced in Qt 4.8 and implements the algorithm from
http://www.unicode.org/reports/tr35/#ListPatterns
2012-02-16 20:36:35 +00:00
2013-10-13 17:23:52 +00:00
``string_list``
2012-02-16 20:36:35 +00:00
List of unicode strings
"""
2013-08-31 18:17:38 +00:00
if LooseVersion(Qt.PYQT_VERSION_STR) >= LooseVersion('4.9') and \
LooseVersion(Qt.qVersion()) >= LooseVersion('4.8'):
2013-10-13 17:23:52 +00:00
return QtCore.QLocale().createSeparatedList(string_list)
if not string_list:
2013-08-31 18:17:38 +00:00
return ''
2013-10-13 17:23:52 +00:00
elif len(string_list) == 1:
return string_list[0]
elif len(string_list) == 2:
return translate('OpenLP.core.lib', '%s and %s',
2013-12-24 08:56:50 +00:00
'Locale list separator: 2 items') % (string_list[0], string_list[1])
2012-02-16 20:36:35 +00:00
else:
merged = translate('OpenLP.core.lib', '%s, and %s',
2013-12-24 08:56:50 +00:00
'Locale list separator: end') % (string_list[-2], string_list[-1])
2013-10-13 17:23:52 +00:00
for index in reversed(list(range(1, len(string_list) - 2))):
merged = translate('OpenLP.core.lib', '%s, %s',
2013-12-24 08:56:50 +00:00
'Locale list separator: middle') % (string_list[index], merged)
2013-10-13 17:23:52 +00:00
return translate('OpenLP.core.lib', '%s, %s', 'Locale list separator: start') % (string_list[0], merged)
2012-02-16 20:36:35 +00:00
2012-04-22 19:50:18 +00:00
2013-09-15 19:27:40 +00:00
from .filedialog import FileDialog
from .screen import ScreenList
from .listwidgetwithdnd import ListWidgetWithDnD
from .treewidgetwithdnd import TreeWidgetWithDnD
from .formattingtags import FormattingTags
from .spelltextedit import SpellTextEdit
from .plugin import PluginStatus, StringContent, Plugin
from .pluginmanager import PluginManager
from .settingstab import SettingsTab
from .serviceitem import ServiceItem, ServiceItemType, ItemCapabilities
from .htmlbuilder import build_html, build_lyrics_format_css, build_lyrics_outline_css
from .toolbar import OpenLPToolbar
from .dockwidget import OpenLPDockWidget
from .imagemanager import ImageManager
from .renderer import Renderer
2013-09-15 19:42:29 +00:00
from .mediamanageritem import MediaManagerItem