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 175
|
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/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!
#
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" An image resource. """
# Standard library imports.
import os
# Enthought library imports.
from enthought.resource.resource_path import resource_path
from enthought.traits.api import Any, HasPrivateTraits, List, Property, Str
# Local imports.
from resource_manager import resource_manager
class ImageResource(HasPrivateTraits):
""" 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 = Property(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
#### Private interface ####################################################
# The resource manager reference for the image.
_ref = Any
###########################################################################
# 'object' interface.
###########################################################################
def __init__(self, name, search_path=None):
""" Creates a new image resource. """
self.name = name
if search_path is not None:
self.search_path = search_path
else:
self.search_path = [resource_path()]
return
###########################################################################
# 'ImageResource' interface.
###########################################################################
#### Properties ###########################################################
def _get_absolute_path(self):
""" Returns the absolute path to the image. """
# fixme: This doesn't quite wotk the new notion of image size. We
# should find out who is actually using this trait, and for what!
ref = self._get_ref()
if ref is not None:
absolute_path = os.path.abspath(self._ref.filename)
else:
absolute_path = IMAGE_NOT_FOUND.absolute_path
return absolute_path
#### Methods ##############################################################
def create_image(self, size=None):
""" Creates a toolkit-specific image for this resource. """
ref = self._get_ref(size)
if ref is not None:
image = ref.load()
else:
image = self._get_image_not_found_image()
return image
def create_bitmap(self, size=None):
""" Creates a bitmap for this resource. """
ref = self._get_ref(size)
if ref is not None:
image = ref.load()
else:
image = self._get_image_not_found_image()
return image.ConvertToBitmap()
def create_icon(self, size=None):
""" Creates a toolkit-specific icon for this resource.
This assumes that the file is in an icon file (i.e., it has the '.ico'
extension).
"""
# Major package imports.
import wx
ref = self._get_ref(size)
if ref is not None:
icon = wx.Icon(self.absolute_path, wx.BITMAP_TYPE_ICO)
# If we can't find the '.ico' file then we create an icon from the
# 'image not found' image!
else:
image = self._get_image_not_found_image()
# We have to convert the image to a bitmap first and then create
# an icon from that.
bmp = image.ConvertToBitmap()
icon = wx.EmptyIcon()
icon.CopyFromBitmap(bmp)
return icon
###########################################################################
# Private interface.
###########################################################################
def _get_ref(self, size=None):
""" Return the resource manager reference to the image. """
if self._ref is None:
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. """
if self is not IMAGE_NOT_FOUND:
image = IMAGE_NOT_FOUND.create_image()
# If we can't find the 'image not found' image then the installer must
# be broken!
else:
raise ValueError("Rick's installer is broken")
return image
# The image that is used if the specified image could not be found.
IMAGE_NOT_FOUND = ImageResource('image_not_found')
#### EOF ######################################################################
|