added secondary priority for the image queue to privilege images which were added earlier over images which were added later, when both have the same priority

bzr-revno: 1968
This commit is contained in:
Andreas Preikschat 2012-05-05 21:53:34 +02:00
commit be26adc0b8

View File

@ -100,6 +100,7 @@ class Image(object):
variables ``image`` and ``image_bytes`` to ``None`` and add the image object variables ``image`` and ``image_bytes`` to ``None`` and add the image object
to the queue of images to process. to the queue of images to process.
""" """
secondary_priority = 0
def __init__(self, name, path, source, background): def __init__(self, name, path, source, background):
self.name = name self.name = name
self.path = path self.path = path
@ -108,25 +109,40 @@ class Image(object):
self.priority = Priority.Normal self.priority = Priority.Normal
self.source = source self.source = source
self.background = background self.background = background
self.secondary_priority = Image.secondary_priority
Image.secondary_priority += 1
class PriorityQueue(Queue.PriorityQueue): class PriorityQueue(Queue.PriorityQueue):
""" """
Customised ``Queue.PriorityQueue``. Customised ``Queue.PriorityQueue``.
Each item in the queue must be tuple with three values. The first value
is the :class:`Image`'s ``priority`` attribute, the second value
the :class:`Image`'s ``secondary_priority`` attribute. The last value the
:class:`Image` instance itself::
(image.priority, image.secondary_priority, image)
Doing this, the :class:`Queue.PriorityQueue` will sort the images according
to their priorities, but also according to there number. However, the number
only has an impact on the result if there are more images with the same
priority. In such case the image which has been added earlier is privileged.
""" """
def modify_priority(self, image, new_priority): def modify_priority(self, image, new_priority):
""" """
Modifies the priority of the given ``image``. Modifies the priority of the given ``image``.
``image`` ``image``
The image to remove. This should be an ``Image`` instance. The image to remove. This should be an :class:`Image` instance.
``new_priority`` ``new_priority``
The image's new priority. The image's new priority. See the :class:`Priority` class for
priorities.
""" """
self.remove(image) self.remove(image)
image.priority = new_priority image.priority = new_priority
self.put((image.priority, image)) self.put((image.priority, image.secondary_priority, image))
def remove(self, image): def remove(self, image):
""" """
@ -135,8 +151,8 @@ class PriorityQueue(Queue.PriorityQueue):
``image`` ``image``
The image to remove. This should be an ``Image`` instance. The image to remove. This should be an ``Image`` instance.
""" """
if (image.priority, image) in self.queue: if (image.priority, image.secondary_priority, image) in self.queue:
self.queue.remove((image.priority, image)) self.queue.remove((image.priority, image.secondary_priority, image))
class ImageManager(QtCore.QObject): class ImageManager(QtCore.QObject):
@ -261,7 +277,8 @@ class ImageManager(QtCore.QObject):
if not name in self._cache: if not name in self._cache:
image = Image(name, path, source, background) image = Image(name, path, source, background)
self._cache[name] = image self._cache[name] = image
self._conversion_queue.put((image.priority, image)) self._conversion_queue.put(
(image.priority, image.secondary_priority, image))
else: else:
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.
@ -282,7 +299,7 @@ class ImageManager(QtCore.QObject):
Actually does the work. Actually does the work.
""" """
log.debug(u'_process_cache') log.debug(u'_process_cache')
image = self._conversion_queue.get()[1] image = self._conversion_queue.get()[2]
# Generate the QImage for the image. # Generate the QImage for the image.
if image.image is None: if image.image is None:
image.image = resize_image(image.path, self.width, self.height, image.image = resize_image(image.path, self.width, self.height,