def __init__(self, **kwargs): self._buffer = _IconBuffer() self._alpha = 1.0 Gtk.EventBox.__init__(self) self.set_visible_window(False) self.set_above_child(True) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.TOUCH_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK) for key, value in kwargs.iteritems(): self.set_property(key, value) from sugar3.graphics.palette import CursorInvoker self._palette_invoker = CursorInvoker() self._palette_invoker.attach(self) self.connect('destroy', self.__destroy_cb)
def __init__(self, **kwargs): self._buffer = _IconBuffer() self._alpha = 1.0 Gtk.EventBox.__init__(self) self.set_visible_window(False) self.set_above_child(True) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.TOUCH_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK) # Connect after the default so that the palette can silence events # for example, after a touch palette invocation self.connect_after('button-release-event', self.__button_release_event_cb) for key, value in kwargs.iteritems(): self.set_property(key, value) from sugar3.graphics.palette import CursorInvoker self._palette_invoker = CursorInvoker() self._palette_invoker.attach(self) self.connect('destroy', self.__destroy_cb)
def __init__(self, **kwargs): self._buffer = _IconBuffer() self._alpha = 1.0 Gtk.EventBox.__init__(self) self.set_visible_window(False) self.set_above_child(True) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.TOUCH_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK) # Connect after the default so that the palette can silence events # for example, after a touch palette invocation self.connect_after('button-release-event', self.__button_release_event_cb) for key, value in six.iteritems(kwargs): self.set_property(key, value) from sugar3.graphics.palette import CursorInvoker self._palette_invoker = CursorInvoker() self._palette_invoker.attach(self) self.connect('destroy', self.__destroy_cb)
class EventIcon(Gtk.EventBox): """ An Icon class that provides access to mouse events and that can act as a cursor-positioned palette invoker. """ __gtype_name__ = 'SugarEventIcon' def __init__(self, **kwargs): self._buffer = _IconBuffer() self._alpha = 1.0 Gtk.EventBox.__init__(self) self.set_visible_window(False) self.set_above_child(True) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.TOUCH_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK) for key, value in kwargs.iteritems(): self.set_property(key, value) from sugar3.graphics.palette import CursorInvoker self._palette_invoker = CursorInvoker() self._palette_invoker.attach(self) self.connect('destroy', self.__destroy_cb) def do_draw(self, cr): surface = self._buffer.get_surface() if surface: allocation = self.get_allocation() x = (allocation.width - surface.get_width()) / 2 y = (allocation.height - surface.get_height()) / 2 cr.set_source_surface(surface, x, y) if self._alpha == 1.0: cr.paint() else: cr.paint_with_alpha(self._alpha) def do_get_preferred_height(self): surface = self._buffer.get_surface() if surface: height = surface.get_height() elif self._buffer.height: height = self._buffer.height else: height = 0 return (height, height) def do_get_preferred_width(self): surface = self._buffer.get_surface() if surface: width = surface.get_width() elif self._buffer.width: width = self._buffer.width else: width = 0 return (width, width) def __destroy_cb(self, icon): if self._palette_invoker is not None: self._palette_invoker.detach() def set_file_name(self, value): if self._buffer.file_name != value: self._buffer.file_name = value self.queue_draw() def get_file_name(self): return self._buffer.file_name file_name = GObject.property( type=object, getter=get_file_name, setter=set_file_name) def set_icon_name(self, value): if self._buffer.icon_name != value: self._buffer.icon_name = value self.queue_draw() def get_icon_name(self): return self._buffer.icon_name icon_name = GObject.property( type=object, getter=get_icon_name, setter=set_icon_name) def set_xo_color(self, value): if self._buffer.xo_color != value: self._buffer.xo_color = value self.queue_draw() xo_color = GObject.property( type=object, getter=None, setter=set_xo_color) def set_fill_color(self, value): if self._buffer.fill_color != value: self._buffer.fill_color = value self.queue_draw() def get_fill_color(self): return self._buffer.fill_color fill_color = GObject.property( type=object, getter=get_fill_color, setter=set_fill_color) def set_stroke_color(self, value): if self._buffer.stroke_color != value: self._buffer.stroke_color = value self.queue_draw() def get_stroke_color(self): return self._buffer.stroke_color stroke_color = GObject.property( type=object, getter=get_stroke_color, setter=set_stroke_color) def set_background_color(self, value): if self._buffer.background_color != value: self._buffer.background_color = value self.queue_draw() def get_background_color(self): return self._buffer.background_color background_color = GObject.property( type=object, getter=get_background_color, setter=set_background_color) def set_size(self, value): if self._buffer.width != value: self._buffer.width = value self._buffer.height = value self.queue_resize() def get_size(self): return self._buffer.width pixel_size = GObject.property( type=object, getter=get_size, setter=set_size) def set_scale(self, value): if self._buffer.scale != value: self._buffer.scale = value self.queue_resize() def get_scale(self): return self._buffer.scale scale = GObject.property( type=float, getter=get_scale, setter=set_scale) def set_alpha(self, alpha): if self._alpha != alpha: self._alpha = alpha self.queue_draw() alpha = GObject.property( type=float, setter=set_alpha) def set_cache(self, value): self._buffer.cache = value def get_cache(self): return self._buffer.cache cache = GObject.property( type=bool, default=False, getter=get_cache, setter=set_cache) def set_badge_name(self, value): if self._buffer.badge_name != value: self._buffer.badge_name = value self.queue_draw() def get_badge_name(self): return self._buffer.badge_name badge_name = GObject.property( type=object, getter=get_badge_name, setter=set_badge_name) def create_palette(self): return None def get_palette(self): return self._palette_invoker.palette def set_palette(self, palette): self._palette_invoker.palette = palette palette = GObject.property( type=object, setter=set_palette, getter=get_palette) def get_palette_invoker(self): return self._palette_invoker def set_palette_invoker(self, palette_invoker): self._palette_invoker.detach() self._palette_invoker = palette_invoker palette_invoker = GObject.property( type=object, setter=set_palette_invoker, getter=get_palette_invoker) def set_tooltip(self, text): from sugar3.graphics.palette import Palette self.set_palette(Palette(text))
class EventIcon(Gtk.EventBox): ''' An Icon class that provides access to mouse events and that can act as a cursor-positioned palette invoker. The palette invoker can be used in 3 ways: 1. Set the palette during your constructor, see :any:`set_palette` 2. Override the create_palette method, see :any:`create_palette` 3. Set the tooltip, see :any:`create_tooltip` Otherwise, the icon setup api is the same as the basic :class:`Icon`. This EventIcon class supports the icon_name, stroke_color, fill_color, file_name, xo_color, pixel_size, scale and alpha keyword arguments as the :class:`Icon`. The added arguments are as follows: Keyword Args: background_color (Gdk.Color): the color to draw the icon on top of. It defaults to None, which means no background is drawn (transparent). The alpha channel of the Gdk.Color is disregarded. cache (bool): if True, the icon file contents will be cached to reduce disk usage palette (sugar3.graphics.palette.Palette): a palette to connect ''' __gsignals__ = { 'activate': (GObject.SignalFlags.RUN_FIRST, None, []), } __gtype_name__ = 'SugarEventIcon' def __init__(self, **kwargs): self._buffer = _IconBuffer() self._alpha = 1.0 Gtk.EventBox.__init__(self) self.set_visible_window(False) self.set_above_child(True) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.TOUCH_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK) # Connect after the default so that the palette can silence events # for example, after a touch palette invocation self.connect_after('button-release-event', self.__button_release_event_cb) for key, value in kwargs.iteritems(): self.set_property(key, value) from sugar3.graphics.palette import CursorInvoker self._palette_invoker = CursorInvoker() self._palette_invoker.attach(self) self.connect('destroy', self.__destroy_cb) def do_draw(self, cr): '''Gtk widget implementation method''' surface = self._buffer.get_surface() if surface: allocation = self.get_allocation() x = (allocation.width - surface.get_width()) / 2 y = (allocation.height - surface.get_height()) / 2 cr.set_source_surface(surface, x, y) if self._alpha == 1.0: cr.paint() else: cr.paint_with_alpha(self._alpha) def do_get_preferred_height(self): '''Gtk widget implementation method''' surface = self._buffer.get_surface() if surface: height = surface.get_height() elif self._buffer.height: height = self._buffer.height else: height = 0 return (height, height) def do_get_preferred_width(self): '''Gtk widget implementation method''' surface = self._buffer.get_surface() if surface: width = surface.get_width() elif self._buffer.width: width = self._buffer.width else: width = 0 return (width, width) def __destroy_cb(self, icon): if self._palette_invoker is not None: self._palette_invoker.detach() def set_file_name(self, value): if self._buffer.file_name != value: self._buffer.file_name = value self.queue_draw() def get_file_name(self): return self._buffer.file_name file_name = GObject.property( type=object, getter=get_file_name, setter=set_file_name) def set_icon_name(self, value): if self._buffer.icon_name != value: self._buffer.icon_name = value self.queue_draw() def get_icon_name(self): return self._buffer.icon_name icon_name = GObject.property( type=object, getter=get_icon_name, setter=set_icon_name) def set_xo_color(self, value): if self._buffer.xo_color != value: self._buffer.xo_color = value self.queue_draw() xo_color = GObject.property( type=object, getter=None, setter=set_xo_color) def set_fill_color(self, value): if self._buffer.fill_color != value: self._buffer.fill_color = value self.queue_draw() def get_fill_color(self): return self._buffer.fill_color fill_color = GObject.property( type=object, getter=get_fill_color, setter=set_fill_color) def set_stroke_color(self, value): if self._buffer.stroke_color != value: self._buffer.stroke_color = value self.queue_draw() def get_stroke_color(self): return self._buffer.stroke_color stroke_color = GObject.property( type=object, getter=get_stroke_color, setter=set_stroke_color) def set_background_color(self, value): ''' Args: value (Gdk.Color): color use as background (alpha is ignored), or None meaning no background is drawn (transparent) ''' if self._buffer.background_color != value: self._buffer.background_color = value self.queue_draw() def get_background_color(self): ''' Returns: Gdk.Color, current background color, may be None ''' return self._buffer.background_color background_color = GObject.property( type=object, getter=get_background_color, setter=set_background_color) ''' event_icon.props.get_background_color -> see :any:`set_background_color` and :any:`get_background_color` ''' def set_size(self, value): if self._buffer.width != value: self._buffer.width = value self._buffer.height = value self.queue_resize() def get_size(self): return self._buffer.width pixel_size = GObject.property( type=object, getter=get_size, setter=set_size) def set_scale(self, value): if self._buffer.scale != value: self._buffer.scale = value self.queue_resize() def get_scale(self): return self._buffer.scale scale = GObject.property( type=float, getter=get_scale, setter=set_scale) def set_alpha(self, alpha): if self._alpha != alpha: self._alpha = alpha self.queue_draw() alpha = GObject.property( type=float, setter=set_alpha) def set_cache(self, value): ''' Sugar caches icon file contents in a smart cache. Currently, we use a LRU (Least Recently Used) algorithm to manage the cache. Args: value (bool): if True, the icon file will be cached in the LRU ''' self._buffer.cache = value def get_cache(self): ''' Returns: bool, if the icon file will be saved in the LRU ''' return self._buffer.cache cache = GObject.property( type=bool, default=False, getter=get_cache, setter=set_cache) ''' event_icon.props.cache -> see :any:`set_cache` and :any:`get_cache` ''' def set_badge_name(self, value): if self._buffer.badge_name != value: self._buffer.badge_name = value self.queue_draw() def get_badge_name(self): return self._buffer.badge_name badge_name = GObject.property( type=object, getter=get_badge_name, setter=set_badge_name) def create_palette(self): ''' The create_palette function is called when the palette needs to be invoked. For example, when the user has right clicked the icon or the user has hovered over the icon for a long time. The create_palette will only be called once or zero times. The palette returned will be stored and re-used if the user invokes the palette multiple times. Your create_palette implementation does not need to :any:`Gtk.Widget.show` the palette, as this will be done by the invoker. However, you still need to show the menu items, etc that you place in the palette. Returns: sugar3.graphics.palette.Palette, or None to indicate that you do not want a palette shown The default implementation returns None, to indicate no palette should be shown. ''' return None def get_palette(self): ''' Gets the current palette, either set by :any:`set_palette` or cached after a call to :any:`create_palette` ''' return self._palette_invoker.palette def set_palette(self, palette): ''' Sets the palette to show. If the palette is not None, this will override the palette set by create_palette. Args: palette (sugar3.graphics.palette.Palette): palette or None ''' self._palette_invoker.palette = palette palette = GObject.property( type=object, setter=set_palette, getter=get_palette) ''' event_icon.props.palette -> see :any:`get_palette` and :any:`set_palette` ''' def get_palette_invoker(self): return self._palette_invoker def set_palette_invoker(self, palette_invoker): self._palette_invoker.detach() self._palette_invoker = palette_invoker palette_invoker = GObject.property( type=object, setter=set_palette_invoker, getter=get_palette_invoker) def set_tooltip(self, text): ''' Creates a palette with the tooltip text. This will override any current palette set through :any:`set_palette` or that will ever be returned by :any:`create_palette`. Args: text (str): tooltip text ''' from sugar3.graphics.palette import Palette self.set_palette(Palette(text)) def __button_release_event_cb(self, icon, event): if event.button == 1: alloc = self.get_allocation() if 0 < event.x < alloc.width and 0 < event.y < alloc.height: self.emit('activate')
class EventIcon(Gtk.EventBox): ''' An Icon class that provides access to mouse events and that can act as a cursor-positioned palette invoker. The palette invoker can be used in 3 ways: 1. Set the palette during your constructor, see :any:`set_palette` 2. Override the create_palette method, see :any:`create_palette` 3. Set the tooltip, see :any:`create_tooltip` Otherwise, the icon setup api is the same as the basic :class:`Icon`. This EventIcon class supports the icon_name, stroke_color, fill_color, file_name, xo_color, pixel_size, scale and alpha keyword arguments as the :class:`Icon`. The added arguments are as follows: Keyword Args: background_color (Gdk.Color): the color to draw the icon on top of. It defaults to None, which means no background is drawn (transparent). The alpha channel of the Gdk.Color is disregarded. cache (bool): if True, the icon file contents will be cached to reduce disk usage palette (sugar3.graphics.palette.Palette): a palette to connect ''' __gsignals__ = { 'activate': (GObject.SignalFlags.RUN_FIRST, None, []), } __gtype_name__ = 'SugarEventIcon' def __init__(self, **kwargs): self._buffer = _IconBuffer() self._alpha = 1.0 Gtk.EventBox.__init__(self) self.set_visible_window(False) self.set_above_child(True) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.TOUCH_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK) # Connect after the default so that the palette can silence events # for example, after a touch palette invocation self.connect_after('button-release-event', self.__button_release_event_cb) for key, value in six.iteritems(kwargs): self.set_property(key, value) from sugar3.graphics.palette import CursorInvoker self._palette_invoker = CursorInvoker() self._palette_invoker.attach(self) self.connect('destroy', self.__destroy_cb) def do_draw(self, cr): '''Gtk widget implementation method''' surface = self._buffer.get_surface() if surface: allocation = self.get_allocation() x = (allocation.width - surface.get_width()) / 2 y = (allocation.height - surface.get_height()) / 2 cr.set_source_surface(surface, x, y) if self._alpha == 1.0: cr.paint() else: cr.paint_with_alpha(self._alpha) def do_get_preferred_height(self): '''Gtk widget implementation method''' surface = self._buffer.get_surface() if surface: height = surface.get_height() elif self._buffer.height: height = self._buffer.height else: height = 0 return (height, height) def do_get_preferred_width(self): '''Gtk widget implementation method''' surface = self._buffer.get_surface() if surface: width = surface.get_width() elif self._buffer.width: width = self._buffer.width else: width = 0 return (width, width) def __destroy_cb(self, icon): if self._palette_invoker is not None: self._palette_invoker.detach() def set_file_name(self, value): if self._buffer.file_name != value: self._buffer.file_name = value self.queue_draw() def get_file_name(self): return self._buffer.file_name file_name = GObject.Property( type=object, getter=get_file_name, setter=set_file_name) def set_icon_name(self, value): if self._buffer.icon_name != value: self._buffer.icon_name = value self.queue_draw() def get_icon_name(self): return self._buffer.icon_name icon_name = GObject.Property( type=object, getter=get_icon_name, setter=set_icon_name) def set_xo_color(self, value): if self._buffer.xo_color != value: self._buffer.xo_color = value self.queue_draw() xo_color = GObject.Property( type=object, getter=None, setter=set_xo_color) def set_fill_color(self, value): if self._buffer.fill_color != value: self._buffer.fill_color = value self.queue_draw() def get_fill_color(self): return self._buffer.fill_color fill_color = GObject.Property( type=object, getter=get_fill_color, setter=set_fill_color) def set_stroke_color(self, value): if self._buffer.stroke_color != value: self._buffer.stroke_color = value self.queue_draw() def get_stroke_color(self): return self._buffer.stroke_color stroke_color = GObject.Property( type=object, getter=get_stroke_color, setter=set_stroke_color) def set_background_color(self, value): ''' Args: value (Gdk.Color): color use as background (alpha is ignored), or None meaning no background is drawn (transparent) ''' if self._buffer.background_color != value: self._buffer.background_color = value self.queue_draw() def get_background_color(self): ''' Returns: Gdk.Color, current background color, may be None ''' return self._buffer.background_color background_color = GObject.Property( type=object, getter=get_background_color, setter=set_background_color) ''' event_icon.props.get_background_color -> see :any:`set_background_color` and :any:`get_background_color` ''' def set_size(self, value): if self._buffer.width != value: self._buffer.width = value self._buffer.height = value self.queue_resize() def get_size(self): return self._buffer.width pixel_size = GObject.Property( type=object, getter=get_size, setter=set_size) def set_scale(self, value): if self._buffer.scale != value: self._buffer.scale = value self.queue_resize() def get_scale(self): return self._buffer.scale scale = GObject.Property( type=float, getter=get_scale, setter=set_scale) def set_alpha(self, alpha): if self._alpha != alpha: self._alpha = alpha self.queue_draw() alpha = GObject.Property( type=float, setter=set_alpha) def set_cache(self, value): ''' Sugar caches icon file contents in a smart cache. Currently, we use a LRU (Least Recently Used) algorithm to manage the cache. Args: value (bool): if True, the icon file will be cached in the LRU ''' self._buffer.cache = value def get_cache(self): ''' Returns: bool, if the icon file will be saved in the LRU ''' return self._buffer.cache cache = GObject.Property( type=bool, default=False, getter=get_cache, setter=set_cache) ''' event_icon.props.cache -> see :any:`set_cache` and :any:`get_cache` ''' def set_badge_name(self, value): if self._buffer.badge_name != value: self._buffer.badge_name = value self.queue_draw() def get_badge_name(self): return self._buffer.badge_name badge_name = GObject.Property( type=object, getter=get_badge_name, setter=set_badge_name) def create_palette(self): ''' The create_palette function is called when the palette needs to be invoked. For example, when the user has right clicked the icon or the user has hovered over the icon for a long time. The create_palette will only be called once or zero times. The palette returned will be stored and re-used if the user invokes the palette multiple times. Your create_palette implementation does not need to :any:`Gtk.Widget.show` the palette, as this will be done by the invoker. However, you still need to show the menu items, etc that you place in the palette. Returns: sugar3.graphics.palette.Palette, or None to indicate that you do not want a palette shown The default implementation returns None, to indicate no palette should be shown. ''' return None def get_palette(self): ''' Gets the current palette, either set by :any:`set_palette` or cached after a call to :any:`create_palette` ''' return self._palette_invoker.palette def set_palette(self, palette): ''' Sets the palette to show. If the palette is not None, this will override the palette set by create_palette. Args: palette (sugar3.graphics.palette.Palette): palette or None ''' self._palette_invoker.palette = palette palette = GObject.Property( type=object, setter=set_palette, getter=get_palette) ''' event_icon.props.palette -> see :any:`get_palette` and :any:`set_palette` ''' def get_palette_invoker(self): return self._palette_invoker def set_palette_invoker(self, palette_invoker): self._palette_invoker.detach() self._palette_invoker = palette_invoker palette_invoker = GObject.Property( type=object, setter=set_palette_invoker, getter=get_palette_invoker) def set_tooltip(self, text): ''' Creates a palette with the tooltip text. This will override any current palette set through :any:`set_palette` or that will ever be returned by :any:`create_palette`. Args: text (str): tooltip text ''' from sugar3.graphics.palette import Palette self.set_palette(Palette(text)) def __button_release_event_cb(self, icon, event): if event.button == 1: alloc = self.get_allocation() if 0 < event.x < alloc.width and 0 < event.y < alloc.height: self.emit('activate')