general thumb clean ups

This commit is contained in:
Andreas Preikschat 2011-06-12 17:17:01 +02:00
parent 60a2a53fb1
commit 1cc95fddb6
5 changed files with 67 additions and 60 deletions

View File

@ -137,6 +137,52 @@ 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):
"""
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``.
"""
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 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(image_path, thumb_path):
"""
Validates whether an image's thumb still exists and if is up to date.
**Note**, you must **not** call this function, before checking the
existence of the image.
``image_path``
The path to the image.
``thumb_path``
The path to the thumb.
"""
if not os.path.exists(unicode(thumb_path)):
return False
image_date = os.stat(unicode(image_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=QtCore.Qt.black):
"""
Resize an image to fit on the current screen.
@ -151,7 +197,7 @@ def resize_image(image_path, width, height, background=QtCore.Qt.black):
The new image height.
``background``
The background colour defaults to black.
The background colour. Defaults to ``QtCore.Qt.black``.
"""
log.debug(u'resize_image - start')
reader = QtGui.QImageReader(image_path)

View File

@ -374,44 +374,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

@ -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
@ -364,7 +364,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:
@ -478,15 +478,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
@ -122,13 +122,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.setData(QtCore.Qt.UserRole, QtCore.QVariant(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
@ -190,10 +191,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 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')