update image in cache when timestamp changed; docs

This commit is contained in:
Andreas Preikschat 2012-06-18 14:07:26 +02:00
parent 6e0ccae954
commit f2884f0593
4 changed files with 48 additions and 15 deletions

View File

@ -264,6 +264,7 @@ def resize_image(image_path, width, height, background=u'#000000'):
if image_ratio == resize_ratio: if image_ratio == resize_ratio:
# We neither need to centre the image nor add "bars" to the image. # We neither need to centre the image nor add "bars" to the image.
return preview return preview
#FIXME: change variables to real_width and real_height
realw = preview.width() realw = preview.width()
realh = preview.height() realh = preview.height()
# and move it to the centre of the preview space # and move it to the centre of the preview space

View File

@ -31,6 +31,7 @@ A Thread is used to convert the image to a byte array so the user does not need
to wait for the conversion to happen. to wait for the conversion to happen.
""" """
import logging import logging
import os
import time import time
import Queue import Queue
@ -96,19 +97,39 @@ class Priority(object):
class Image(object): class Image(object):
""" """
This class represents an image. To mark an image as *dirty* set the instance This class represents an image. To mark an image as *dirty* call the
variables ``image`` and ``image_bytes`` to ``None`` and add the image object :class:`ImageManager`'s ``_resetImage`` method with the Image instance as
to the queue of images to process. argument.
""" """
secondary_priority = 0 secondary_priority = 0
def __init__(self, name, path, source, background): def __init__(self, name, path, source, background):
"""
Create an image for the :class:`ImageManager`'s cache.
``name``
The image name. This does not have to be part of the ``path``. It
can be of any value. It can be considered an ID.
``path``
The image's file path. This should be an existing file path.
``source``
The source describes the image's origin. Possible values are
``image`` and ``theme``.
``background``
A ``QtGui.QColor`` object specifying the colour to be used to fill
the gabs if the image's ratio does not match with the display ratio.
"""
self.name = name self.name = name
self.path = path self.path = path
self.image = None self.image = None
self.image_bytes = None self.image_bytes = None
self.priority = Priority.Normal
self.source = source self.source = source
self.background = background self.background = background
self.timestamp = os.stat(path).st_mtime
self.priority = Priority.Normal
self.secondary_priority = Image.secondary_priority self.secondary_priority = Image.secondary_priority
Image.secondary_priority += 1 Image.secondary_priority += 1
@ -117,7 +138,7 @@ class PriorityQueue(Queue.PriorityQueue):
""" """
Customised ``Queue.PriorityQueue``. Customised ``Queue.PriorityQueue``.
Each item in the queue must be tuple with three values. The first value Each item in the queue must be a tuple with three values. The first value
is the :class:`Image`'s ``priority`` attribute, the second value is the :class:`Image`'s ``priority`` attribute, the second value
the :class:`Image`'s ``secondary_priority`` attribute. The last value the the :class:`Image`'s ``secondary_priority`` attribute. The last value the
:class:`Image` instance itself:: :class:`Image` instance itself::
@ -186,7 +207,7 @@ class ImageManager(QtCore.QObject):
for image in self._cache.values(): for image in self._cache.values():
self._resetImage(image) self._resetImage(image)
def updateImages(self, imageType, background): def updateImagesBorder(self, source, background):
""" """
Border has changed so update all the images affected. Border has changed so update all the images affected.
""" """
@ -194,23 +215,27 @@ class ImageManager(QtCore.QObject):
# Mark the images as dirty for a rebuild by setting the image and byte # Mark the images as dirty for a rebuild by setting the image and byte
# stream to None. # stream to None.
for image in self._cache.values(): for image in self._cache.values():
if image.source == imageType: if image.source == source:
image.background = background image.background = background
self._resetImage(image) self._resetImage(image)
def updateImage(self, name, imageType, background): def updateImageBorder(self, name, source, background):
""" """
Border has changed so update the image affected. Border has changed so update the image affected.
""" """
log.debug(u'updateImage') log.debug(u'updateImage')
# Mark the images as dirty for a rebuild by setting the image and byte # Mark the image as dirty for a rebuild by setting the image and byte
# stream to None. # stream to None.
for image in self._cache.values(): image = self._cache[name]
if image.source == imageType and image.name == name: if image.source == source:
image.background = background image.background = background
self._resetImage(image) self._resetImage(image)
def _resetImage(self, image): def _resetImage(self, image):
"""
Mark the given :class:`Image` instance as dirt by setting its ``image``
and ``image_bytes`` attributes to None.
"""
image.image = None image.image = None
image.image_bytes = None image.image_bytes = None
self._conversionQueue.modify_priority(image, Priority.Normal) self._conversionQueue.modify_priority(image, Priority.Normal)
@ -281,6 +306,12 @@ class ImageManager(QtCore.QObject):
self._conversionQueue.put( self._conversionQueue.put(
(image.priority, image.secondary_priority, image)) (image.priority, image.secondary_priority, image))
else: else:
image = self._cache[name]
if os.path.isfile(path) and \
image.timestamp != os.stat(path).st_mtime:
image.path = path
image.timestamp = os.stat(path).st_mtime
self._resetImage(image)
log.debug(u'Image in cache %s:%s' % (name, path)) log.debug(u'Image in cache %s:%s' % (name, path))
# We want only one thread. # We want only one thread.
if not self.imageThread.isRunning(): if not self.imageThread.isRunning():

View File

@ -668,7 +668,7 @@ class ThemeManager(QtGui.QWidget):
self._writeTheme(theme, image_from, image_to) self._writeTheme(theme, image_from, image_to)
if theme.background_type == \ if theme.background_type == \
BackgroundType.to_string(BackgroundType.Image): BackgroundType.to_string(BackgroundType.Image):
self.mainwindow.imageManager.updateImage(theme.theme_name, self.mainwindow.imageManager.updateImageBorder(theme.theme_name,
u'theme', QtGui.QColor(theme.background_border_color)) u'theme', QtGui.QColor(theme.background_border_color))
self.mainwindow.imageManager.processUpdates() self.mainwindow.imageManager.processUpdates()
self.loadThemes() self.loadThemes()

View File

@ -97,4 +97,5 @@ class ImagePlugin(Plugin):
""" """
background = QtGui.QColor(Settings().value(self.settingsSection background = QtGui.QColor(Settings().value(self.settingsSection
+ u'/background color', QtCore.QVariant(u'#000000'))) + u'/background color', QtCore.QVariant(u'#000000')))
self.liveController.imageManager.updateImages(u'image', background) self.liveController.imageManager.updateImagesBorder(
u'image', background)