forked from openlp/openlp
refactor some code
This commit is contained in:
parent
af33d7f97f
commit
3e8c91cbd5
@ -112,17 +112,29 @@ class PriorityQueue(Queue.PriorityQueue):
|
|||||||
"""
|
"""
|
||||||
Customised ``Queue.PriorityQueue``.
|
Customised ``Queue.PriorityQueue``.
|
||||||
"""
|
"""
|
||||||
def remove(self, item):
|
def modify_priority(self, image, new_priority):
|
||||||
"""
|
"""
|
||||||
Removes the given ``item`` from the queue.
|
Modifies the priority of the given ``image``.
|
||||||
|
|
||||||
``item``
|
``image``
|
||||||
The item to remove. This should be a tuple::
|
The image to remove. This should be an ``Image`` instance.
|
||||||
|
|
||||||
``(Priority, Image)``
|
``new_priority``
|
||||||
|
The new priority for the image.
|
||||||
"""
|
"""
|
||||||
if item in self.queue:
|
self.remove(image)
|
||||||
self.queue.remove(item)
|
image.priority = new_priority
|
||||||
|
self.put((image.priority, image))
|
||||||
|
|
||||||
|
def remove(self, image):
|
||||||
|
"""
|
||||||
|
Removes the given ``image`` from the queue.
|
||||||
|
|
||||||
|
``image``
|
||||||
|
The image to remove. This should be an ``Image`` instance.
|
||||||
|
"""
|
||||||
|
if (image.priority, image) in self.queue:
|
||||||
|
self.queue.remove((image.priority, image))
|
||||||
|
|
||||||
|
|
||||||
class ImageManager(QtCore.QObject):
|
class ImageManager(QtCore.QObject):
|
||||||
@ -168,9 +180,7 @@ class ImageManager(QtCore.QObject):
|
|||||||
log.debug(u'get_image %s' % name)
|
log.debug(u'get_image %s' % name)
|
||||||
image = self._cache[name]
|
image = self._cache[name]
|
||||||
if image.image is None:
|
if image.image is None:
|
||||||
self._conversion_queue.remove((image.priority, image))
|
self._conversion_queue.modify_priority(image, Priority.High)
|
||||||
image.priority = Priority.High
|
|
||||||
self._conversion_queue.put((image.priority, image))
|
|
||||||
while image.image is None:
|
while image.image is None:
|
||||||
log.debug(u'get_image - waiting')
|
log.debug(u'get_image - waiting')
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
@ -184,9 +194,7 @@ class ImageManager(QtCore.QObject):
|
|||||||
log.debug(u'get_image_bytes %s' % name)
|
log.debug(u'get_image_bytes %s' % name)
|
||||||
image = self._cache[name]
|
image = self._cache[name]
|
||||||
if image.image_bytes is None:
|
if image.image_bytes is None:
|
||||||
self._conversion_queue.remove((image.priority, image))
|
self._conversion_queue.modify_priority(image, Priority.Urgent)
|
||||||
image.priority = Priority.Urgent
|
|
||||||
self._conversion_queue.put((image.priority, image))
|
|
||||||
while image.image_bytes is None:
|
while image.image_bytes is None:
|
||||||
log.debug(u'get_image_bytes - waiting')
|
log.debug(u'get_image_bytes - waiting')
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
@ -198,8 +206,7 @@ class ImageManager(QtCore.QObject):
|
|||||||
"""
|
"""
|
||||||
log.debug(u'del_image %s' % name)
|
log.debug(u'del_image %s' % name)
|
||||||
if name in self._cache:
|
if name in self._cache:
|
||||||
self._conversion_queue.remove(
|
self._conversion_queue.remove(self._cache[name])
|
||||||
(self._cache[name].priority, self._cache[name]))
|
|
||||||
del self._cache[name]
|
del self._cache[name]
|
||||||
|
|
||||||
def add_image(self, name, path):
|
def add_image(self, name, path):
|
||||||
@ -238,18 +245,14 @@ class ImageManager(QtCore.QObject):
|
|||||||
# Set the priority to Lowest and stop here as we need to process
|
# Set the priority to Lowest and stop here as we need to process
|
||||||
# more important images first.
|
# more important images first.
|
||||||
if image.priority == Priority.Normal:
|
if image.priority == Priority.Normal:
|
||||||
self._conversion_queue.remove((image.priority, image))
|
self._conversion_queue.modify_priority(image, Priority.Lowest)
|
||||||
image.priority = Priority.Lowest
|
|
||||||
self._conversion_queue.put((image.priority, image))
|
|
||||||
return
|
return
|
||||||
# For image with high priority we set the priority to Low, as the
|
# For image with high priority we set the priority to Low, as the
|
||||||
# byte stream might be needed earlier the byte stream of image with
|
# byte stream might be needed earlier the byte stream of image with
|
||||||
# Normal priority. We stop here as we need to process more important
|
# Normal priority. We stop here as we need to process more important
|
||||||
# images first.
|
# images first.
|
||||||
elif image.priority == Priority.High:
|
elif image.priority == Priority.High:
|
||||||
self._conversion_queue.remove((image.priority, image))
|
self._conversion_queue.modify_priority(image, Priority.Low)
|
||||||
image.priority = Priority.Low
|
|
||||||
self._conversion_queue.put((image.priority, image))
|
|
||||||
return
|
return
|
||||||
# Generate the byte stream for the image.
|
# Generate the byte stream for the image.
|
||||||
if image.image_bytes is None:
|
if image.image_bytes is None:
|
||||||
|
Loading…
Reference in New Issue
Block a user