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)
class GeoMarker(GeoPrimitive): #### 'GeoPrimitive' interface ################################################### bounds = (1, 1) # The anchor point of the marker (in relative coordinates) anchor = coordinate_trait((0.5, 0.)) filename = Str _marker = Instance(Image) def _filename_changed(self, new): self._marker = Image(new) ########################################################################### # Protected 'Component' interface. ########################################################################### def _render_primitive(self, gc, view_bounds=None, mode='default'): """ Draw the component. """ x, y = self.position anchor_x, anchor_y = self.anchor w, h = self._marker.width(), self._marker.height() gc.draw_image(self._marker, (x-anchor_x*w, y-anchor_y*h, w, h)) return
class GeoMarker(GeoPrimitive): #### 'GeoPrimitive' interface ################################################### bounds = (1, 1) # The anchor point of the marker (in relative coordinates) anchor = coordinate_trait((0.5, 0.)) filename = Str _marker = Instance(Image) def _filename_changed(self, new): self._marker = Image(new) ########################################################################### # Protected 'Component' interface. ########################################################################### def _render_primitive(self, gc, view_bounds=None, mode='default'): """ Draw the component. """ x, y = self.position anchor_x, anchor_y = self.anchor w, h = self._marker.width(), self._marker.height() gc.draw_image(self._marker, (x - anchor_x * w, y - anchor_y * h, w, h)) return
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"): if exists(self.image_file): gc.save_state() img = KivaImage(self.image_file) w, h = img.width(), img.height() self.bounds = [w, h] gc.draw_image(img, (self.x, self.y, w, h)) gc.restore_state()
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"): if exists(self.image_file): gc.save_state() # self.image_file.seek(0) img = KivaImage(self.image_file) x = gc.width() * 0.7 y = gc.height() * 0.15 w, h = img.width(), img.height() # Use Image's ability to draw itself onto a gc to paint the window. gc.draw_image(img, (x, y, w, h)) self.position = [x, y] self.bounds = [w, h] gc.restore_state()
def test_initialization(self): image = Image(self.filename) self.assertEqual(image.width(), 100) self.assertEqual(image.height(), 120) self.assertEqual(image.format(), 'rgb24')