From 785787d7f3e0a1a83fdb23dbbc0ce271d90c43d5 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 5 May 2012 14:23:42 +0200 Subject: [PATCH 1/4] added secondary criterion for the image queue to privilege images which were added earlier over images which were added later, when both have the same priority --- openlp/core/lib/imagemanager.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/openlp/core/lib/imagemanager.py b/openlp/core/lib/imagemanager.py index 0139cd001..660c3fe22 100644 --- a/openlp/core/lib/imagemanager.py +++ b/openlp/core/lib/imagemanager.py @@ -100,6 +100,7 @@ class Image(object): 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,11 +109,24 @@ class Image(object): 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): """ @@ -126,7 +140,7 @@ class PriorityQueue(Queue.PriorityQueue): """ 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 +149,8 @@ class PriorityQueue(Queue.PriorityQueue): ``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 +275,7 @@ class ImageManager(QtCore.QObject): 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 +296,7 @@ class ImageManager(QtCore.QObject): 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, From aa917e7aba45f1c6fc226b5f7270b84b7d68b6b6 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 5 May 2012 14:38:06 +0200 Subject: [PATCH 2/4] fixed cap --- openlp/core/lib/imagemanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlp/core/lib/imagemanager.py b/openlp/core/lib/imagemanager.py index 660c3fe22..9ef02a9a6 100644 --- a/openlp/core/lib/imagemanager.py +++ b/openlp/core/lib/imagemanager.py @@ -119,7 +119,7 @@ class PriorityQueue(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:: + last value the :class:`Image` instance itself:: (Priority.Normal, image._number, image) From 7f7ac0c54e9f9f429ec93c9140c03a2a600d062b Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 5 May 2012 14:40:31 +0200 Subject: [PATCH 3/4] extended doc string --- openlp/core/lib/imagemanager.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/openlp/core/lib/imagemanager.py b/openlp/core/lib/imagemanager.py index 9ef02a9a6..10cfce616 100644 --- a/openlp/core/lib/imagemanager.py +++ b/openlp/core/lib/imagemanager.py @@ -133,10 +133,11 @@ class PriorityQueue(Queue.PriorityQueue): 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 From 8b08fbbe3230ec1b77c4b142e73bc9a16152ac31 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 5 May 2012 20:34:26 +0200 Subject: [PATCH 4/4] docs and class variable fix --- openlp/core/lib/imagemanager.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/openlp/core/lib/imagemanager.py b/openlp/core/lib/imagemanager.py index 10cfce616..b32e36194 100644 --- a/openlp/core/lib/imagemanager.py +++ b/openlp/core/lib/imagemanager.py @@ -100,7 +100,7 @@ class Image(object): variables ``image`` and ``image_bytes`` to ``None`` and add the image object to the queue of images to process. """ - NUMBER = 0 + secondary_priority = 0 def __init__(self, name, path, source, background): self.name = name self.path = path @@ -109,19 +109,20 @@ class Image(object): self.priority = Priority.Normal self.source = source self.background = background - self._number = Image.NUMBER - Image.NUMBER += 1 + self.secondary_priority = Image.secondary_priority + Image.secondary_priority += 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:: + 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:: - (Priority.Normal, image._number, image) + (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 @@ -141,7 +142,7 @@ class PriorityQueue(Queue.PriorityQueue): """ self.remove(image) image.priority = new_priority - self.put((image.priority, image._number, image)) + self.put((image.priority, image.secondary_priority, image)) def remove(self, image): """ @@ -150,8 +151,8 @@ class PriorityQueue(Queue.PriorityQueue): ``image`` The image to remove. This should be an ``Image`` instance. """ - if (image.priority, image._number, image) in self.queue: - self.queue.remove((image.priority, image._number, image)) + if (image.priority, image.secondary_priority, image) in self.queue: + self.queue.remove((image.priority, image.secondary_priority, image)) class ImageManager(QtCore.QObject): @@ -276,7 +277,8 @@ class ImageManager(QtCore.QObject): if not name in self._cache: image = Image(name, path, source, background) self._cache[name] = image - self._conversion_queue.put((image.priority, image._number, image)) + self._conversion_queue.put( + (image.priority, image.secondary_priority, image)) else: log.debug(u'Image in cache %s:%s' % (name, path)) # We want only one thread.