- use create_thumb and validate_thumb functions in other parts of the code too

- performance improvement

bzr-revno: 1766
This commit is contained in:
Andreas Preikschat 2011-10-02 14:02:25 +02:00
commit 59640dd04f
7 changed files with 86 additions and 76 deletions

View File

@ -144,6 +144,59 @@ def image_to_byte(image):
# convert to base64 encoding so does not get missed!
return byte_array.toBase64()
def create_thumb(image_path, thumb_path, return_icon=True, size=None):
"""
Create a thumbnail from the given image path and depending on
``return_icon`` it returns an icon from this thumb.
``image_path``
The image file to create the icon from.
``thumb_path``
The filename to save the thumbnail to.
``return_icon``
States if an icon should be build and returned from the thumb. Defaults
to ``True``.
``size``
Allows to state a own size to use. Defaults to ``None``, which means
that a default height of 88 is used.
"""
ext = os.path.splitext(thumb_path)[1].lower()
reader = QtGui.QImageReader(image_path)
if size is None:
ratio = float(reader.size().width()) / float(reader.size().height())
reader.setScaledSize(QtCore.QSize(int(ratio * 88), 88))
else:
reader.setScaledSize(size)
thumb = reader.read()
thumb.save(thumb_path, ext[1:])
if not return_icon:
return
if os.path.exists(thumb_path):
return build_icon(unicode(thumb_path))
# Fallback for files with animation support.
return build_icon(unicode(image_path))
def validate_thumb(file_path, thumb_path):
"""
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.
``file_path``
The path to the file. The file **must** exist!
``thumb_path``
The path to the thumb.
"""
if not os.path.exists(unicode(thumb_path)):
return False
image_date = os.stat(unicode(file_path)).st_mtime
thumb_date = os.stat(unicode(thumb_path)).st_mtime
return image_date <= thumb_date
def resize_image(image_path, width, height, background=u'#000000'):
"""
Resize an image to fit on the current screen.
@ -158,7 +211,7 @@ def resize_image(image_path, width, height, background=u'#000000'):
The new image height.
``background``
The background colour defaults to black.
The background colour. Defaults to black.
DO NOT REMOVE THE DEFAULT BACKGROUND VALUE!
"""

View File

@ -425,44 +425,6 @@ class MediaManagerItem(QtGui.QWidget):
count += 1
return filelist
def validate(self, image, thumb):
"""
Validates whether an image still exists and, if it does, is the
thumbnail representation of the image up to date.
"""
if not os.path.exists(unicode(image)):
return False
if os.path.exists(thumb):
imageDate = os.stat(unicode(image)).st_mtime
thumbDate = os.stat(unicode(thumb)).st_mtime
# If image has been updated rebuild icon
if imageDate > thumbDate:
self.iconFromFile(image, thumb)
else:
self.iconFromFile(image, thumb)
return True
def iconFromFile(self, image_path, thumb_path):
"""
Create a thumbnail icon from a given image.
``image_path``
The image file to create the icon from.
``thumb_path``
The filename to save the thumbnail to.
"""
ext = os.path.splitext(thumb_path)[1].lower()
reader = QtGui.QImageReader(image_path)
ratio = float(reader.size().width()) / float(reader.size().height())
reader.setScaledSize(QtCore.QSize(int(ratio * 88), 88))
thumb = reader.read()
thumb.save(thumb_path, ext[1:])
if os.path.exists(thumb_path):
return build_icon(unicode(thumb_path))
# Fallback for files with animation support.
return build_icon(unicode(image_path))
def loadList(self, list):
raise NotImplementedError(u'MediaManagerItem.loadList needs to be '
u'defined by the plugin')

View File

@ -669,14 +669,14 @@ class SlideController(QtGui.QWidget):
label.setMargin(4)
label.setScaledContents(True)
if self.serviceItem.is_command():
image = QtGui.QImage(frame[u'image'])
label.setPixmap(QtGui.QPixmap(frame[u'image']))
else:
# If current slide set background to image
if framenumber == slideno:
self.serviceItem.bg_image_bytes = \
self.imageManager.get_image_bytes(frame[u'title'])
image = self.imageManager.get_image(frame[u'title'])
label.setPixmap(QtGui.QPixmap.fromImage(image))
label.setPixmap(QtGui.QPixmap.fromImage(image))
self.previewListWidget.setCellWidget(framenumber, 0, label)
slideHeight = width * self.parent().renderer.screen_ratio
row += 1

View File

@ -36,7 +36,7 @@ from PyQt4 import QtCore, QtGui
from openlp.core.lib import OpenLPToolbar, get_text_file_string, build_icon, \
Receiver, SettingsManager, translate, check_item_selected, \
check_directory_exists
check_directory_exists, create_thumb, validate_thumb
from openlp.core.lib.theme import ThemeXML, BackgroundType, VerticalType, \
BackgroundGradientType
from openlp.core.lib.ui import UiStrings, critical_error_message_box, \
@ -359,7 +359,7 @@ class ThemeManager(QtGui.QWidget):
The theme to delete.
"""
self.themelist.remove(theme)
thumb = theme + u'.png'
thumb = u'%s.png' % theme
delete_file(os.path.join(self.path, thumb))
delete_file(os.path.join(self.thumbPath, thumb))
try:
@ -473,15 +473,12 @@ class ThemeManager(QtGui.QWidget):
name = textName
thumb = os.path.join(self.thumbPath, u'%s.png' % textName)
item_name = QtGui.QListWidgetItem(name)
if os.path.exists(thumb):
if validate_thumb(theme, thumb):
icon = build_icon(thumb)
else:
icon = build_icon(theme)
pixmap = icon.pixmap(QtCore.QSize(88, 50))
pixmap.save(thumb, u'png')
icon = create_thumb(theme, thumb)
item_name.setIcon(icon)
item_name.setData(QtCore.Qt.UserRole,
QtCore.QVariant(textName))
item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(textName))
self.themeListWidget.addItem(item_name)
self.themelist.append(textName)
self._pushThemes()
@ -658,9 +655,7 @@ class ThemeManager(QtGui.QWidget):
os.unlink(samplepathname)
frame.save(samplepathname, u'png')
thumb = os.path.join(self.thumbPath, u'%s.png' % name)
icon = build_icon(frame)
pixmap = icon.pixmap(QtCore.QSize(88, 50))
pixmap.save(thumb, u'png')
create_thumb(samplepathname, thumb, False)
log.debug(u'Theme image written to %s', samplepathname)
def updatePreviewImages(self):

View File

@ -33,7 +33,7 @@ from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, build_icon, ItemCapabilities, \
SettingsManager, translate, check_item_selected, check_directory_exists, \
Receiver
Receiver, create_thumb, validate_thumb
from openlp.core.lib.ui import UiStrings, critical_error_message_box
from openlp.core.utils import AppLocation, delete_file, get_images_filter
@ -127,13 +127,13 @@ class ImageMediaItem(MediaManagerItem):
self.plugin.formparent.incrementProgressBar()
filename = os.path.split(unicode(imageFile))[1]
thumb = os.path.join(self.servicePath, filename)
if os.path.exists(thumb):
if self.validate(imageFile, thumb):
if not os.path.exists(imageFile):
icon = build_icon(u':/general/general_delete.png')
else:
if validate_thumb(imageFile, thumb):
icon = build_icon(thumb)
else:
icon = build_icon(u':/general/general_delete.png')
else:
icon = self.iconFromFile(imageFile, thumb)
icon = create_thumb(imageFile, thumb)
item_name = QtGui.QListWidgetItem(filename)
item_name.setIcon(icon)
item_name.setToolTip(imageFile)

View File

@ -32,7 +32,8 @@ import locale
from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, build_icon, SettingsManager, \
translate, check_item_selected, Receiver, ItemCapabilities
translate, check_item_selected, Receiver, ItemCapabilities, create_thumb, \
validate_thumb
from openlp.core.lib.ui import UiStrings, critical_error_message_box, \
media_item_combo_box
from openlp.plugins.presentations.lib import MessageListener
@ -193,10 +194,13 @@ class PresentationMediaItem(MediaManagerItem):
doc.load_presentation()
preview = doc.get_thumbnail_path(1, True)
doc.close_presentation()
if preview and self.validate(preview, thumb):
icon = build_icon(thumb)
else:
if not (preview and os.path.exists(preview)):
icon = build_icon(u':/general/general_delete.png')
else:
if validate_thumb(preview, thumb):
icon = build_icon(thumb)
else:
icon = create_thumb(preview, thumb)
else:
if initialLoad:
icon = build_icon(u':/general/general_delete.png')

View File

@ -31,7 +31,8 @@ import shutil
from PyQt4 import QtCore
from openlp.core.lib import Receiver, resize_image
from openlp.core.lib import Receiver, check_directory_exists, create_thumb, \
resize_image, validate_thumb
from openlp.core.utils import AppLocation
log = logging.getLogger(__name__)
@ -97,8 +98,7 @@ class PresentationDocument(object):
self.slidenumber = 0
self.controller = controller
self.filepath = name
if not os.path.isdir(self.get_thumbnail_folder()):
os.mkdir(self.get_thumbnail_folder())
check_directory_exists(self.get_thumbnail_folder())
def load_presentation(self):
"""
@ -145,15 +145,13 @@ class PresentationDocument(object):
def check_thumbnails(self):
"""
Returns true if the thumbnail images look to exist and are more
recent than the powerpoint
Returns ``True`` if the thumbnail images exist and are more recent than
the powerpoint file.
"""
lastimage = self.get_thumbnail_path(self.get_slide_count(), True)
if not (lastimage and os.path.isfile(lastimage)):
return False
imgdate = os.stat(lastimage).st_mtime
pptdate = os.stat(self.filepath).st_mtime
return imgdate >= pptdate
return validate_thumb(self.filepath, lastimage)
def close_presentation(self):
"""
@ -246,8 +244,8 @@ class PresentationDocument(object):
if self.check_thumbnails():
return
if os.path.isfile(file):
img = resize_image(file, 320, 240)
img.save(self.get_thumbnail_path(idx, False))
thumb_path = self.get_thumbnail_path(idx, False)
create_thumb(file, thumb_path, False, QtCore.QSize(320, 240))
def get_thumbnail_path(self, slide_no, check_exists):
"""
@ -387,10 +385,8 @@ class PresentationController(object):
AppLocation.get_section_data_path(self.settings_section),
u'thumbnails')
self.thumbnail_prefix = u'slide'
if not os.path.isdir(self.thumbnail_folder):
os.makedirs(self.thumbnail_folder)
if not os.path.isdir(self.temp_folder):
os.makedirs(self.temp_folder)
check_directory_exists(self.thumbnail_folder)
check_directory_exists(self.temp_folder)
def enabled(self):
"""