1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" The interface for an image resource. """
from collections.abc import Sequence
from traits.api import HasTraits, List, Str
from pyface.i_image import IImage
from pyface.resource.resource_path import resource_module, resource_path
class IImageResource(IImage):
""" The interface for an image resource.
An image resource describes the location of an image and provides a way
to create a toolkit-specific image on demand.
"""
# 'ImageResource' interface --------------------------------------------
#: The absolute path to the image.
absolute_path = Str()
#: The name of the image.
name = Str()
#: A list of directories, classes or instances that will be used to search
#: for the image (see the resource manager for more details).
search_path = List()
@classmethod
def image_size(cls, image):
""" Get the size of a toolkit image
Parameters
----------
image : toolkit image
A toolkit image to compute the size of.
Returns
-------
size : tuple
The (width, height) tuple giving the size of the image.
"""
class MImageResource(HasTraits):
""" The mixin class that contains common code for toolkit specific
implementations of the IImageResource interface.
Implements: __init__(), create_image()
"""
# Private interface ----------------------------------------------------
#: The image-not-found image. Note that it is not a trait.
_image_not_found = None
# ------------------------------------------------------------------------
# 'object' interface.
# ------------------------------------------------------------------------
def __init__(self, name, search_path=None):
self.name = name
if isinstance(search_path, str):
_path = [search_path]
elif isinstance(search_path, Sequence):
_path = search_path
elif search_path is not None:
_path = [search_path]
else:
_path = [resource_path()]
self.search_path = _path + [resource_module()]
# ------------------------------------------------------------------------
# 'ImageResource' interface.
# ------------------------------------------------------------------------
def create_image(self, size=None):
""" Creates a toolkit-specific image for this resource.
Parameters
----------
size : (int, int) or None
The desired size as a width, height tuple, or None if wanting
default image size.
Returns
-------
image : toolkit image
The toolkit image corresponding to the resource and the specified
size.
"""
ref = self._get_ref(size)
if ref is not None:
image = ref.load()
else:
image = self._get_image_not_found_image()
return image
# ------------------------------------------------------------------------
# Private interface.
# ------------------------------------------------------------------------
def _get_ref(self, size=None):
""" Return the resource manager reference to the image.
Parameters
----------
size : (int, int) or None
The desired size as a width, height tuple, or None if wanting
default image size.
Returns
-------
ref : ImageReference
The reference to the requested image.
"""
if self._ref is None:
from pyface.resource_manager import resource_manager
self._ref = resource_manager.locate_image(
self.name, self.search_path, size
)
return self._ref
def _get_image_not_found_image(self):
""" Returns the 'image not found' image.
Returns
-------
image : toolkit image
The 'not found' toolkit image.
"""
not_found = self._get_image_not_found()
if self is not not_found:
image = not_found.create_image()
else:
raise ValueError("cannot locate the file for 'image_not_found'")
return image
@classmethod
def _get_image_not_found(cls):
""" Returns the 'image not found' image resource.
Returns
-------
not_found : ImageResource
An image resource for the the 'not found' image.
"""
if cls._image_not_found is None:
from pyface.image_resource import ImageResource
cls._image_not_found = ImageResource("image_not_found")
return cls._image_not_found
|