2010-10-22 18:56:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-12-28 22:06:43 +00:00
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
2010-10-22 18:56:58 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
2013-06-30 05:35:57 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
2017-12-29 09:15:48 +00:00
|
|
|
# Copyright (c) 2008-2018 OpenLP Developers #
|
2010-10-22 18:56:58 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
|
|
# more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License along #
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
2013-03-01 10:55:14 +00:00
|
|
|
Provides the store and management for Images automatically caching them and resizing them when needed. Only one copy of
|
|
|
|
each image is needed in the system. A Thread is used to convert the image to a byte array so the user does not need to
|
|
|
|
wait for the conversion to happen.
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2012-06-18 12:07:26 +00:00
|
|
|
import os
|
2013-08-31 18:17:38 +00:00
|
|
|
import queue
|
2017-12-28 08:27:44 +00:00
|
|
|
import time
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
from PyQt5 import QtCore
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
|
|
|
from openlp.core.common.settings import Settings
|
2017-10-10 07:08:44 +00:00
|
|
|
from openlp.core.display.screens import ScreenList
|
|
|
|
from openlp.core.lib import resize_image, image_to_byte
|
2017-12-20 14:17:07 +00:00
|
|
|
from openlp.core.threading import ThreadWorker, run_thread
|
2010-10-22 18:56:58 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2013-02-01 19:58:18 +00:00
|
|
|
|
2017-12-20 14:17:07 +00:00
|
|
|
class ImageWorker(ThreadWorker):
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2017-12-20 14:17:07 +00:00
|
|
|
A thread worker class to speed up the display of images. This is threaded so it loads the frames and generates
|
2013-03-01 10:55:14 +00:00
|
|
|
byte stream in background.
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
|
|
|
def __init__(self, manager):
|
2013-02-01 19:58:18 +00:00
|
|
|
"""
|
|
|
|
Constructor for the thread class.
|
|
|
|
|
|
|
|
``manager``
|
|
|
|
The image manager.
|
|
|
|
"""
|
2017-12-20 14:17:07 +00:00
|
|
|
super().__init__()
|
2013-02-02 20:54:34 +00:00
|
|
|
self.image_manager = manager
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2017-12-20 14:17:07 +00:00
|
|
|
def start(self):
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2018-01-04 06:01:35 +00:00
|
|
|
Start the worker
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2017-03-05 09:23:28 +00:00
|
|
|
self.image_manager.process()
|
2017-12-20 14:17:07 +00:00
|
|
|
self.quit.emit()
|
2011-06-25 04:42:49 +00:00
|
|
|
|
2018-01-04 06:01:35 +00:00
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stop the worker
|
|
|
|
"""
|
|
|
|
self.image_manager.stop_manager = True
|
|
|
|
|
2011-06-25 04:42:49 +00:00
|
|
|
|
|
|
|
class Priority(object):
|
|
|
|
"""
|
|
|
|
Enumeration class for different priorities.
|
|
|
|
|
2011-06-25 05:34:07 +00:00
|
|
|
``Lowest``
|
2013-03-01 10:55:14 +00:00
|
|
|
Only the image's byte stream has to be generated. But neither the ``QImage`` nor the byte stream has been
|
|
|
|
requested yet.
|
2011-06-25 05:34:07 +00:00
|
|
|
|
2011-06-25 04:42:49 +00:00
|
|
|
``Low``
|
2013-03-01 10:55:14 +00:00
|
|
|
Only the image's byte stream has to be generated. Because the image's ``QImage`` has been requested previously
|
|
|
|
it is reasonable to assume that the byte stream will be needed before the byte stream of other images whose
|
|
|
|
``QImage`` were not generated due to a request.
|
2011-06-25 04:42:49 +00:00
|
|
|
|
|
|
|
``Normal``
|
2013-03-01 10:55:14 +00:00
|
|
|
The image's byte stream as well as the image has to be generated. Neither the ``QImage`` nor the byte stream has
|
|
|
|
been requested yet.
|
2011-06-25 04:42:49 +00:00
|
|
|
|
|
|
|
``High``
|
2013-03-01 10:55:14 +00:00
|
|
|
The image's byte stream as well as the image has to be generated. The ``QImage`` for this image has been
|
|
|
|
requested. **Note**, this priority is only set when the ``QImage`` has not been generated yet.
|
2011-06-25 04:42:49 +00:00
|
|
|
|
|
|
|
``Urgent``
|
2013-03-01 10:55:14 +00:00
|
|
|
The image's byte stream as well as the image has to be generated. The byte stream for this image has been
|
|
|
|
requested. **Note**, this priority is only set when the byte stream has not been generated yet.
|
2011-06-25 04:42:49 +00:00
|
|
|
"""
|
2011-06-25 05:34:07 +00:00
|
|
|
Lowest = 4
|
2011-06-25 04:42:49 +00:00
|
|
|
Low = 3
|
|
|
|
Normal = 2
|
|
|
|
High = 1
|
|
|
|
Urgent = 0
|
2010-10-22 18:56:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Image(object):
|
2011-06-25 05:34:07 +00:00
|
|
|
"""
|
2013-03-06 22:11:30 +00:00
|
|
|
This class represents an image. To mark an image as *dirty* call the :class:`ImageManager`'s ``_reset_image`` method
|
2013-03-01 10:55:14 +00:00
|
|
|
with the Image instance as argument.
|
2011-06-25 05:34:07 +00:00
|
|
|
"""
|
2012-05-05 18:34:26 +00:00
|
|
|
secondary_priority = 0
|
2012-06-30 15:19:33 +00:00
|
|
|
|
2014-07-15 18:52:59 +00:00
|
|
|
def __init__(self, path, source, background, width=-1, height=-1):
|
2012-06-18 12:07:26 +00:00
|
|
|
"""
|
|
|
|
Create an image for the :class:`ImageManager`'s cache.
|
2012-06-30 15:19:33 +00:00
|
|
|
|
2014-03-17 19:05:55 +00:00
|
|
|
:param path: The image's file path. This should be an existing file path.
|
|
|
|
:param source: The source describes the image's origin. Possible values are described in the
|
2013-03-01 10:55:14 +00:00
|
|
|
:class:`~openlp.core.lib.ImageSource` class.
|
2014-03-20 19:10:31 +00:00
|
|
|
:param background: A ``QtGui.QColor`` object specifying the colour to be used to fill the gabs if the image's
|
|
|
|
ratio does not match with the display ratio.
|
2014-07-15 18:52:59 +00:00
|
|
|
:param width: The width of the image, defaults to -1 meaning that the screen width will be used.
|
|
|
|
:param height: The height of the image, defaults to -1 meaning that the screen height will be used.
|
2012-06-18 12:07:26 +00:00
|
|
|
"""
|
2017-05-30 18:42:35 +00:00
|
|
|
if not os.path.exists(path):
|
|
|
|
raise FileNotFoundError('{path} not found'.format(path=path))
|
2011-06-25 04:42:49 +00:00
|
|
|
self.path = path
|
|
|
|
self.image = None
|
|
|
|
self.image_bytes = None
|
2012-07-01 18:41:59 +00:00
|
|
|
self.priority = Priority.Normal
|
2011-08-20 11:45:06 +00:00
|
|
|
self.source = source
|
|
|
|
self.background = background
|
2012-11-16 19:15:36 +00:00
|
|
|
self.timestamp = 0
|
2014-07-15 18:52:59 +00:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2017-05-30 18:42:35 +00:00
|
|
|
self.timestamp = os.stat(path).st_mtime
|
2012-05-05 18:34:26 +00:00
|
|
|
self.secondary_priority = Image.secondary_priority
|
|
|
|
Image.secondary_priority += 1
|
2011-06-25 04:42:49 +00:00
|
|
|
|
|
|
|
|
2013-08-31 18:17:38 +00:00
|
|
|
class PriorityQueue(queue.PriorityQueue):
|
2011-06-25 04:42:49 +00:00
|
|
|
"""
|
2018-01-04 06:01:35 +00:00
|
|
|
Customised ``queue.PriorityQueue``.
|
2012-05-05 12:23:42 +00:00
|
|
|
|
2013-03-01 10:55:14 +00:00
|
|
|
Each item in the queue must be a 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::
|
2012-05-05 12:23:42 +00:00
|
|
|
|
2012-05-05 18:34:26 +00:00
|
|
|
(image.priority, image.secondary_priority, image)
|
2012-05-05 12:23:42 +00:00
|
|
|
|
2013-03-01 10:55:14 +00:00
|
|
|
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
|
2012-05-05 12:23:42 +00:00
|
|
|
priority. In such case the image which has been added earlier is privileged.
|
2011-06-25 04:42:49 +00:00
|
|
|
"""
|
2011-07-07 14:53:05 +00:00
|
|
|
def modify_priority(self, image, new_priority):
|
2011-06-25 04:42:49 +00:00
|
|
|
"""
|
2011-07-07 14:53:05 +00:00
|
|
|
Modifies the priority of the given ``image``.
|
2011-06-25 04:42:49 +00:00
|
|
|
|
2014-03-17 19:05:55 +00:00
|
|
|
:param image: The image to remove. This should be an :class:`Image` instance.
|
|
|
|
:param new_priority: The image's new priority. See the :class:`Priority` class for priorities.
|
2011-06-25 04:42:49 +00:00
|
|
|
"""
|
2011-07-07 14:53:05 +00:00
|
|
|
self.remove(image)
|
|
|
|
image.priority = new_priority
|
2012-05-05 18:34:26 +00:00
|
|
|
self.put((image.priority, image.secondary_priority, image))
|
2011-07-07 14:53:05 +00:00
|
|
|
|
|
|
|
def remove(self, image):
|
|
|
|
"""
|
|
|
|
Removes the given ``image`` from the queue.
|
|
|
|
|
2014-03-17 19:05:55 +00:00
|
|
|
:param image: The image to remove. This should be an ``Image`` instance.
|
2011-07-07 14:53:05 +00:00
|
|
|
"""
|
2012-05-05 18:34:26 +00:00
|
|
|
if (image.priority, image.secondary_priority, image) in self.queue:
|
|
|
|
self.queue.remove((image.priority, image.secondary_priority, image))
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2011-02-07 15:55:02 +00:00
|
|
|
|
2010-10-22 18:56:58 +00:00
|
|
|
class ImageManager(QtCore.QObject):
|
|
|
|
"""
|
|
|
|
Image Manager handles the conversion and sizing of images.
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
log.info('Image Manager loaded')
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2011-05-05 11:51:47 +00:00
|
|
|
def __init__(self):
|
2013-02-01 19:58:18 +00:00
|
|
|
"""
|
2013-02-02 20:54:34 +00:00
|
|
|
Constructor for the image manager.
|
2013-02-01 19:58:18 +00:00
|
|
|
"""
|
2013-07-18 11:15:17 +00:00
|
|
|
super(ImageManager, self).__init__()
|
2013-08-31 18:17:38 +00:00
|
|
|
Registry().register('image_manager', self)
|
2013-02-02 20:54:34 +00:00
|
|
|
current_screen = ScreenList().current
|
2013-08-31 18:17:38 +00:00
|
|
|
self.width = current_screen['size'].width()
|
|
|
|
self.height = current_screen['size'].height()
|
2010-10-22 18:56:58 +00:00
|
|
|
self._cache = {}
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue = PriorityQueue()
|
|
|
|
self.stop_manager = False
|
2013-08-31 18:17:38 +00:00
|
|
|
Registry().register_function('images_regenerate', self.process_updates)
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2013-02-02 20:54:34 +00:00
|
|
|
def update_display(self):
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2011-06-25 04:42:49 +00:00
|
|
|
Screen has changed size so rebuild the cache to new size.
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
log.debug('update_display')
|
2013-02-02 20:54:34 +00:00
|
|
|
current_screen = ScreenList().current
|
2013-08-31 18:17:38 +00:00
|
|
|
self.width = current_screen['size'].width()
|
|
|
|
self.height = current_screen['size'].height()
|
2013-03-01 10:55:14 +00:00
|
|
|
# Mark the images as dirty for a rebuild by setting the image and byte stream to None.
|
2013-08-31 18:17:38 +00:00
|
|
|
for image in list(self._cache.values()):
|
2013-02-02 20:54:34 +00:00
|
|
|
self._reset_image(image)
|
2011-08-20 11:45:06 +00:00
|
|
|
|
2013-02-02 20:54:34 +00:00
|
|
|
def update_images_border(self, source, background):
|
2011-08-20 13:21:25 +00:00
|
|
|
"""
|
2011-08-21 05:33:07 +00:00
|
|
|
Border has changed so update all the images affected.
|
2011-08-20 13:21:25 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
log.debug('update_images_border')
|
2013-03-01 10:55:14 +00:00
|
|
|
# Mark the images as dirty for a rebuild by setting the image and byte stream to None.
|
2013-08-31 18:17:38 +00:00
|
|
|
for image in list(self._cache.values()):
|
2012-06-18 12:07:26 +00:00
|
|
|
if image.source == source:
|
2011-08-20 13:21:25 +00:00
|
|
|
image.background = background
|
2013-02-02 20:54:34 +00:00
|
|
|
self._reset_image(image)
|
2011-08-21 05:33:07 +00:00
|
|
|
|
2014-07-15 18:52:59 +00:00
|
|
|
def update_image_border(self, path, source, background, width=-1, height=-1):
|
2011-08-21 05:33:07 +00:00
|
|
|
"""
|
|
|
|
Border has changed so update the image affected.
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
log.debug('update_image_border')
|
2013-03-01 10:55:14 +00:00
|
|
|
# Mark the image as dirty for a rebuild by setting the image and byte stream to None.
|
2014-07-15 18:52:59 +00:00
|
|
|
image = self._cache[(path, source, width, height)]
|
2012-06-18 12:07:26 +00:00
|
|
|
if image.source == source:
|
|
|
|
image.background = background
|
2013-02-02 20:54:34 +00:00
|
|
|
self._reset_image(image)
|
2011-08-21 16:13:45 +00:00
|
|
|
|
2013-02-02 20:54:34 +00:00
|
|
|
def _reset_image(self, image):
|
2012-06-18 12:07:26 +00:00
|
|
|
"""
|
2013-03-01 10:55:14 +00:00
|
|
|
Mark the given :class:`Image` instance as dirty by setting its ``image`` and ``image_bytes`` attributes to None.
|
2012-06-18 12:07:26 +00:00
|
|
|
"""
|
2011-08-21 16:13:45 +00:00
|
|
|
image.image = None
|
|
|
|
image.image_bytes = None
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue.modify_priority(image, Priority.Normal)
|
2011-08-20 13:21:25 +00:00
|
|
|
|
2013-02-02 20:54:34 +00:00
|
|
|
def process_updates(self):
|
2011-08-20 11:45:06 +00:00
|
|
|
"""
|
|
|
|
Flush the queue to updated any data to update
|
|
|
|
"""
|
2017-12-20 14:17:07 +00:00
|
|
|
try:
|
|
|
|
worker = ImageWorker(self)
|
|
|
|
run_thread(worker, 'image_manager')
|
|
|
|
except KeyError:
|
|
|
|
# run_thread() will throw a KeyError if this thread already exists, so ignore it so that we don't
|
|
|
|
# try to start another thread when one is already running
|
|
|
|
pass
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2014-07-15 18:52:59 +00:00
|
|
|
def get_image(self, path, source, width=-1, height=-1):
|
2010-10-23 07:23:49 +00:00
|
|
|
"""
|
2013-03-01 10:55:14 +00:00
|
|
|
Return the ``QImage`` from the cache. If not present wait for the background thread to process it.
|
2016-06-27 20:55:36 +00:00
|
|
|
|
|
|
|
:param: path: The image path
|
|
|
|
:param: source: The source of the image
|
|
|
|
:param: background: The image background colour
|
|
|
|
:param: width: The processed image width
|
|
|
|
:param: height: The processed image height
|
2010-10-23 07:23:49 +00:00
|
|
|
"""
|
2016-06-27 20:55:36 +00:00
|
|
|
log.debug('get_image {path} {source} {width} {height}'.format(path=path, source=source,
|
|
|
|
width=width, height=height))
|
2014-07-15 18:52:59 +00:00
|
|
|
image = self._cache[(path, source, width, height)]
|
2011-06-25 04:42:49 +00:00
|
|
|
if image.image is None:
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue.modify_priority(image, Priority.High)
|
2011-09-20 15:41:04 +00:00
|
|
|
# make sure we are running and if not give it a kick
|
2013-02-02 20:54:34 +00:00
|
|
|
self.process_updates()
|
2011-06-25 04:42:49 +00:00
|
|
|
while image.image is None:
|
2013-08-31 18:17:38 +00:00
|
|
|
log.debug('getImage - waiting')
|
2011-06-25 04:42:49 +00:00
|
|
|
time.sleep(0.1)
|
2011-07-07 15:15:48 +00:00
|
|
|
elif image.image_bytes is None:
|
2013-03-01 10:55:14 +00:00
|
|
|
# Set the priority to Low, because the image was requested but the byte stream was not generated yet.
|
|
|
|
# However, we only need to do this, when the image was generated before it was requested (otherwise this is
|
|
|
|
# already taken care of).
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue.modify_priority(image, Priority.Low)
|
2011-06-25 04:42:49 +00:00
|
|
|
return image.image
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2014-07-15 18:52:59 +00:00
|
|
|
def get_image_bytes(self, path, source, width=-1, height=-1):
|
2010-10-23 07:23:49 +00:00
|
|
|
"""
|
2013-03-01 10:55:14 +00:00
|
|
|
Returns the byte string for an image. If not present wait for the background thread to process it.
|
2016-06-27 20:55:36 +00:00
|
|
|
|
|
|
|
:param: path: The image path
|
|
|
|
:param: source: The source of the image
|
|
|
|
:param: background: The image background colour
|
|
|
|
:param: width: The processed image width
|
|
|
|
:param: height: The processed image height
|
2010-10-23 07:23:49 +00:00
|
|
|
"""
|
2016-06-27 20:55:36 +00:00
|
|
|
log.debug('get_image_bytes {path} {source} {width} {height}'.format(path=path, source=source,
|
|
|
|
width=width, height=height))
|
2014-07-15 18:52:59 +00:00
|
|
|
image = self._cache[(path, source, width, height)]
|
2011-06-25 04:42:49 +00:00
|
|
|
if image.image_bytes is None:
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue.modify_priority(image, Priority.Urgent)
|
2011-09-20 15:41:04 +00:00
|
|
|
# make sure we are running and if not give it a kick
|
2013-02-02 20:54:34 +00:00
|
|
|
self.process_updates()
|
2011-06-25 04:42:49 +00:00
|
|
|
while image.image_bytes is None:
|
2013-08-31 18:17:38 +00:00
|
|
|
log.debug('getImageBytes - waiting')
|
2010-10-23 07:23:49 +00:00
|
|
|
time.sleep(0.1)
|
2011-06-25 04:42:49 +00:00
|
|
|
return image.image_bytes
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2014-07-15 18:52:59 +00:00
|
|
|
def add_image(self, path, source, background, width=-1, height=-1):
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2011-06-25 04:42:49 +00:00
|
|
|
Add image to cache if it is not already there.
|
2016-06-27 20:55:36 +00:00
|
|
|
|
|
|
|
:param: path: The image path
|
|
|
|
:param: source: The source of the image
|
|
|
|
:param: background: The image background colour
|
|
|
|
:param: width: The processed image width
|
|
|
|
:param: height: The processed image height
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2016-06-27 20:55:36 +00:00
|
|
|
log.debug('add_image {path} {source} {width} {height}'.format(path=path, source=source,
|
|
|
|
width=width, height=height))
|
2014-07-15 18:52:59 +00:00
|
|
|
if not (path, source, width, height) in self._cache:
|
|
|
|
image = Image(path, source, background, width, height)
|
|
|
|
self._cache[(path, source, width, height)] = image
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue.put((image.priority, image.secondary_priority, image))
|
2013-03-01 10:55:14 +00:00
|
|
|
# Check if the there are any images with the same path and check if the timestamp has changed.
|
2013-08-31 18:17:38 +00:00
|
|
|
for image in list(self._cache.values()):
|
2012-11-16 19:15:36 +00:00
|
|
|
if os.path.exists(path):
|
2012-12-28 22:06:43 +00:00
|
|
|
if image.path == path and image.timestamp != os.stat(path).st_mtime:
|
2012-11-16 19:15:36 +00:00
|
|
|
image.timestamp = os.stat(path).st_mtime
|
2013-02-02 20:54:34 +00:00
|
|
|
self._reset_image(image)
|
2018-01-04 06:01:35 +00:00
|
|
|
self.process_updates()
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2017-03-05 09:23:28 +00:00
|
|
|
def process(self):
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2011-06-25 04:42:49 +00:00
|
|
|
Controls the processing called from a ``QtCore.QThread``.
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
2017-03-05 09:23:28 +00:00
|
|
|
log.debug('process - started')
|
2013-02-02 20:54:34 +00:00
|
|
|
while not self._conversion_queue.empty() and not self.stop_manager:
|
|
|
|
self._process_cache()
|
2013-08-31 18:17:38 +00:00
|
|
|
log.debug('_process - ended')
|
2010-10-22 18:56:58 +00:00
|
|
|
|
2013-02-02 20:54:34 +00:00
|
|
|
def _process_cache(self):
|
2010-10-22 18:56:58 +00:00
|
|
|
"""
|
|
|
|
Actually does the work.
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
log.debug('_processCache')
|
2013-02-02 20:54:34 +00:00
|
|
|
image = self._conversion_queue.get()[2]
|
2011-06-25 04:42:49 +00:00
|
|
|
# Generate the QImage for the image.
|
|
|
|
if image.image is None:
|
2013-11-07 20:13:15 +00:00
|
|
|
# Let's see if the image was requested with specific dimensions
|
|
|
|
width = self.width if image.width == -1 else image.width
|
|
|
|
height = self.height if image.height == -1 else image.height
|
2017-04-30 09:05:28 +00:00
|
|
|
image.image = resize_image(image.path, width, height, image.background,
|
|
|
|
Settings().value('advanced/ignore aspect ratio'))
|
2013-03-01 10:55:14 +00:00
|
|
|
# Set the priority to Lowest and stop here as we need to process more important images first.
|
2011-06-25 05:34:07 +00:00
|
|
|
if image.priority == Priority.Normal:
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue.modify_priority(image, Priority.Lowest)
|
2011-06-25 05:34:07 +00:00
|
|
|
return
|
2013-03-01 10:55:14 +00:00
|
|
|
# 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 Normal priority. We stop here as we need to process more important images first.
|
2011-06-25 05:34:07 +00:00
|
|
|
elif image.priority == Priority.High:
|
2013-02-02 20:54:34 +00:00
|
|
|
self._conversion_queue.modify_priority(image, Priority.Low)
|
2011-06-25 04:42:49 +00:00
|
|
|
return
|
|
|
|
# Generate the byte stream for the image.
|
|
|
|
if image.image_bytes is None:
|
|
|
|
image.image_bytes = image_to_byte(image.image)
|