change queue name; remove image from queue when deleted from cache

This commit is contained in:
Andreas Preikschat 2011-06-27 13:28:58 +02:00
parent efe256c42a
commit e50ca971d6
1 changed files with 16 additions and 14 deletions

View File

@ -138,7 +138,7 @@ class ImageManager(QtCore.QObject):
self.height = current_screen[u'size'].height() self.height = current_screen[u'size'].height()
self._cache = {} self._cache = {}
self._imageThread = ImageThread(self) self._imageThread = ImageThread(self)
self._clean_queue = PriorityQueue() self._conversion_queue = PriorityQueue()
def update_display(self): def update_display(self):
""" """
@ -150,12 +150,12 @@ class ImageManager(QtCore.QObject):
self.height = current_screen[u'size'].height() self.height = current_screen[u'size'].height()
# 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.
self._clean_queue = PriorityQueue() self._conversion_queue = PriorityQueue()
for key, image in self._cache.iteritems(): for key, image in self._cache.iteritems():
image.priority = Priority.Normal image.priority = Priority.Normal
image.image = None image.image = None
image.image_bytes = None image.image_bytes = None
self._clean_queue.put((image.priority, image)) self._conversion_queue.put((image.priority, image))
# We want only one thread. # We want only one thread.
if not self._imageThread.isRunning(): if not self._imageThread.isRunning():
self._imageThread.start() self._imageThread.start()
@ -168,9 +168,9 @@ 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._clean_queue.remove((image.priority, image)) self._conversion_queue.remove((image.priority, image))
image.priority = Priority.High image.priority = Priority.High
self._clean_queue.put((image.priority, image)) 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 +184,9 @@ 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._clean_queue.remove((image.priority, image)) self._conversion_queue.remove((image.priority, image))
image.priority = Priority.Urgent image.priority = Priority.Urgent
self._clean_queue.put((image.priority, image)) 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,6 +198,8 @@ 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._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):
@ -208,7 +210,7 @@ class ImageManager(QtCore.QObject):
if not name in self._cache: if not name in self._cache:
image = Image(name, path) image = Image(name, path)
self._cache[name] = image self._cache[name] = image
self._clean_queue.put((image.priority, image)) self._conversion_queue.put((image.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.
@ -220,7 +222,7 @@ class ImageManager(QtCore.QObject):
Controls the processing called from a ``QtCore.QThread``. Controls the processing called from a ``QtCore.QThread``.
""" """
log.debug(u'_process - started') log.debug(u'_process - started')
while not self._clean_queue.empty(): while not self._conversion_queue.empty():
self._process_cache() self._process_cache()
log.debug(u'_process - ended') log.debug(u'_process - ended')
@ -229,25 +231,25 @@ class ImageManager(QtCore.QObject):
Actually does the work. Actually does the work.
""" """
log.debug(u'_process_cache') log.debug(u'_process_cache')
image = self._clean_queue.get()[1] image = self._conversion_queue.get()[1]
# 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)
# 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._clean_queue.remove((image.priority, image)) self._conversion_queue.remove((image.priority, image))
image.priority = Priority.Lowest image.priority = Priority.Lowest
self._clean_queue.put((image.priority, image)) 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._clean_queue.remove((image.priority, image)) self._conversion_queue.remove((image.priority, image))
image.priority = Priority.Low image.priority = Priority.Low
self._clean_queue.put((image.priority, image)) 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: