← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~googol/openlp/image-queue into lp:openlp

 

Andreas Preikschat has proposed merging lp:~googol/openlp/image-queue into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~googol/openlp/image-queue/+merge/104834

Hello,

I have added a secondary criterion for the image queue to privilege images which were added later over images which were added earlier when both have the same priority.

NOTE: Do not be confused by the commit message. I mixed things up :-( (And if you do not know what I am talking about, then don't waste any further second...)
-- 
https://code.launchpad.net/~googol/openlp/image-queue/+merge/104834
Your team OpenLP Core is requested to review the proposed merge of lp:~googol/openlp/image-queue into lp:openlp.
=== modified file 'openlp/core/lib/imagemanager.py'
--- openlp/core/lib/imagemanager.py	2012-03-12 22:12:16 +0000
+++ openlp/core/lib/imagemanager.py	2012-05-05 12:44:18 +0000
@@ -100,6 +100,7 @@
     variables ``image`` and ``image_bytes`` to ``None`` and add the image object
     to the queue of images to process.
     """
+    NUMBER = 0
     def __init__(self, name, path, source, background):
         self.name = name
         self.path = path
@@ -108,25 +109,39 @@
         self.priority = Priority.Normal
         self.source = source
         self.background = background
+        self._number = Image.NUMBER
+        Image.NUMBER += 1
 
 
 class PriorityQueue(Queue.PriorityQueue):
     """
     Customised ``Queue.PriorityQueue``.
+
+    Each item in the queue must be tuple with three values. The fist value
+    is the priority, the second value the image's ``_number`` attribute. The
+    last value the :class:`Image` instance itself::
+
+        (Priority.Normal, image._number, 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):
         """
         Modifies the priority of the given ``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``
-            The image's new priority.
+            The image's new priority. See the :class:`Priority` class for
+            priorities.
         """
         self.remove(image)
         image.priority = new_priority
-        self.put((image.priority, image))
+        self.put((image.priority, image._number, image))
 
     def remove(self, image):
         """
@@ -135,8 +150,8 @@
         ``image``
             The image to remove. This should be an ``Image`` instance.
         """
-        if (image.priority, image) in self.queue:
-            self.queue.remove((image.priority, image))
+        if (image.priority, image._number, image) in self.queue:
+            self.queue.remove((image.priority, image._number, image))
 
 
 class ImageManager(QtCore.QObject):
@@ -261,7 +276,7 @@
         if not name in self._cache:
             image = Image(name, path, source, background)
             self._cache[name] = image
-            self._conversion_queue.put((image.priority, image))
+            self._conversion_queue.put((image.priority, image._number, image))
         else:
             log.debug(u'Image in cache %s:%s' % (name, path))
         # We want only one thread.
@@ -282,7 +297,7 @@
         Actually does the work.
         """
         log.debug(u'_process_cache')
-        image = self._conversion_queue.get()[1]
+        image = self._conversion_queue.get()[2]
         # Generate the QImage for the image.
         if image.image is None:
             image.image = resize_image(image.path, self.width, self.height,


Follow ups