class ToolbarButton(Button): image = Str() _image = Instance(Image) color = 'black' width = Property(Int, depends_on='label, image') height = Property(Int, depends_on='label, image') # bounds are used for hit testing bounds = Property(List, depends_on='label, image') def __init__(self, *args, **kw): super(ToolbarButton, self).__init__(*args, **kw) image_resource = ImageResource(self.image) self._image = Image(image_resource.absolute_path) @cached_property def _get_width(self): gc = PlotGraphicsContext((100,100), dpi=72) gc.set_font(self.label_font) (w, h, descent, leading) = gc.get_full_text_extent(self.label) return max(self._image.width(), w) @cached_property def _get_height(self): gc = PlotGraphicsContext((100,100), dpi=72) gc.set_font(self.label_font) (w, h, descent, leading) = gc.get_full_text_extent(self.label) return self._image.height() + h @cached_property def _get_bounds(self): return [self.width, self.height] def _draw_actual_button(self, gc): x_offset = self.x + (self.width - self._image.width())/2 gc.draw_image(self._image, (x_offset, self.y+2, self._image.width(), self._image.height())) if self.label is not None and len(self.label) > 0: gc.set_font(self.label_font) (w, h, descent, leading) = gc.get_full_text_extent(self.label) if w < self.width: x_offset = self.x + (self.width - w)/2 else: x_offset = self.x gc.set_text_position(x_offset, self.y-8) gc.show_text(self.label)
def fromfile(cls, filename): """ Alternate constructor to create an ImageData from an image file on disk. 'filename' may be a file path or a file object. """ from enthought.kiva.image import Image img = Image(filename) imgdata = cls(data=img.bmp_array, transposed=False) fmt = img.format() if fmt == "rgb24": imgdata.value_depth = 3 elif fmt == "rgba32": imgdata.value_depth = 4 else: raise ValueError("Unknown image format in file %s: %s" % (filename, fmt)) return imgdata
def __init__(self, *args, **kw): super(ToolbarButton, self).__init__(*args, **kw) image_resource = ImageResource(self.image) self._image = Image(image_resource.absolute_path)