Ejemplo n.º 1
0
class ObjectsDisplay(gtk.VBox):
    __gsignals__ = {
        "object-input" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        "status-changed" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),        
    }      
    def __init__(self, output_spec, context, **kwargs):
        super(ObjectsDisplay, self).__init__(**kwargs)
        self.__context = context
        self.__box = gtk.VBox()        
        self.add(self.__box)
        self.__scroll = gtk.ScrolledWindow()
        self.__scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        vadjust = self.__scroll.get_vadjustment()
        vadjust.connect('value-changed', self.__on_scroll_value_changed)        
        self.__search = None
        self.__inputarea = None
        self.__output_type = None
        self.__old_focus = None
        self.__box.pack_start(self.__scroll, expand=True)
        self.__display = None
        self.__add_display(output_spec)
        self.__doing_autoscroll = False
        self.__user_scrolled = False
        self.__autoscroll_id = 0
        self._common_supertype = None

    def __add_display(self, output_spec, force=False):
        if output_spec != 'any':
            self.__display = ClassRendererMapping.getInstance().lookup(output_spec, self.__context)
        if not self.__display and force:
            self.__display = DefaultObjectsRenderer(self.__context)        
        if self.__display:
            self.__display.connect('status-changed', self.__on_status_changed)
            self.__display_widget = self.__display.get_widget()
            self.__display_widget.show_all()
            self.__scroll.add(self.__display_widget)
            self.__output_type = output_spec
            
    def __on_status_changed(self, renderer):
        self.emit('status-changed')
        self.do_autoscroll()

    def start_search(self, old_focus):
        try:
            self.__display.start_search()
            return True
        except NotImplementedError, e:
            pass        
        if self.__search is None:
            self.__search = self.__display.get_search()
            if self.__search is not True:
                self.__box.pack_start(self.__search, expand=False)
                self.__search.connect("close", self.__on_search_close)
        self.__old_focus = old_focus
        if self.__search is not True:
            self.__search.show_all()
            self.__search.focus()
            return True
        return False
Ejemplo n.º 2
0
 def __add_display(self, output_spec, force=False):
     if output_spec != 'any':
         self.__display = ClassRendererMapping.getInstance().lookup(
             output_spec, self.__context)
     if not self.__display and force:
         self.__display = DefaultObjectsRenderer(self.__context)
     if self.__display:
         self.__display.connect('status-changed', self.__on_status_changed)
         self.__display_widget = self.__display.get_widget()
         self.__display_widget.show_all()
         self.__scroll.add(self.__display_widget)
         self.__output_type = output_spec
Ejemplo n.º 3
0
 def __add_display(self, output_spec, force=False):
     if output_spec != 'any':
         self.__display = ClassRendererMapping.getInstance().lookup(output_spec, self.__context)
     if not self.__display and force:
         self.__display = DefaultObjectsRenderer(self.__context)        
     if self.__display:
         self.__display.connect('status-changed', self.__on_status_changed)
         self.__display_widget = self.__display.get_widget()
         self.__display_widget.show_all()
         self.__scroll.add(self.__display_widget)
         self.__output_type = output_spec
Ejemplo n.º 4
0
class ObjectsDisplay(gtk.VBox):
    __gsignals__ = {
        "object-input" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        "status-changed" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),        
    }      
    def __init__(self, output_spec, context, **kwargs):
        super(ObjectsDisplay, self).__init__(**kwargs)
        self.__context = context
        self.__box = gtk.VBox()        
        self.add(self.__box)
        self.__scroll = gtk.ScrolledWindow()
        self.__scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        vadjust = self.__scroll.get_vadjustment()
        vadjust.connect('value-changed', self.__on_scroll_value_changed)        
        self.__search = None
        self.__inputarea = None
        self.__output_type = None
        self.__old_focus = None
        self.__box.pack_start(self.__scroll, expand=True)
        self.__display = None
        self.__add_display(output_spec)
        self.__doing_autoscroll = False
        self.__user_scrolled = False
        self.__autoscroll_id = 0
        self._common_supertype = None

    def __add_display(self, output_spec, force=False):
        if output_spec != 'any':
            self.__display = ClassRendererMapping.getInstance().lookup(output_spec, self.__context)
        if not self.__display and force:
            self.__display = DefaultObjectsRenderer(self.__context)        
        if self.__display:
            self.__display.connect('status-changed', self.__on_status_changed)
            self.__display_widget = self.__display.get_widget()
            self.__display_widget.show_all()
            self.__scroll.add(self.__display_widget)
            self.__output_type = output_spec
            
    def __on_status_changed(self, renderer):
        self.emit('status-changed')
        self.do_autoscroll()

    def start_search(self, old_focus):
        try:
            self.__display.start_search()
            return True
        except NotImplementedError as e:
            pass        
        if self.__search is None:
            self.__search = self.__display.get_search()
            if self.__search is not True:
                self.__box.pack_start(self.__search, expand=False)
                self.__search.connect("close", self.__on_search_close)
        self.__old_focus = old_focus
        if self.__search is not True:
            self.__search.show_all()
            self.__search.focus()
            return True
        return False

    def __on_search_close(self, search):
        if self.__search is not True:
            self.__search.hide()
        if self.__old_focus:
            self.__old_focus.grab_focus()
            
    def supports_input(self):
        return self.__display and self.__display.supports_input()
            
    def start_input(self, old_focus):
        if self.__inputarea is None:
            self.__inputarea = self.__display.get_input()
            if self.__inputarea is not True:
                self.__box.pack_start(self.__inputarea, expand=False)
                self.__inputarea.connect("close", self.__on_inputarea_close)
                self.__inputarea.connect("object-input", self.__on_object_input)
        self.__old_focus = old_focus
        if self.__inputarea is not True:
            self.__inputarea.show_all()
            self.__inputarea.focus()

    def __on_object_input(self, ia, obj, *args):
        _logger.debug("got interactive object input: %s", obj)
        self.emit('object-input', obj)

    def __on_inputarea_close(self, search):
        if self.__inputarea is not True:
            self.__inputarea.hide()
        if self.__old_focus:
            self.__old_focus.grab_focus()            
            
    def get_opt_formats(self):
        if self.__display:
            return self.__display.get_opt_formats()
        return []

    def get_status_str(self):
        return self.__display and self.__display.get_status_str()

    def get_objects(self):
        if self.__display:
            for obj in self.__display.get_objects():
                yield obj
        else:
            raise ValueError("Can't get object snapshot, no display")
            
    def get_selected_objects(self):
        if self.__display:
            for obj in self.__display.get_selected_objects():
                yield obj
        else:
            raise ValueError("Can't get object snapshot, no display")            
            
    def get_output_type(self):
        """Return the typespec for the current pipeline.  See Pipeline
        for a description of typespecs."""
        return self.__output_type
                
    def __recurse_get_common_superclass(self, c1, c2):
        for base in c1.__bases__:
            if base == c2:
                return base
            tmp = self.__recurse_get_common_superclass(base, c2)
            if tmp:
                return tmp
        
    def __get_common_superclass(self, c1, c2):
        if c1 == c2:
            return c1
        if isinstance(c2, c1):
            (c1, c2) = (c2, c1)
        elif not isinstance(c1, c2):
            return object
        return self.__recurse_get_common_superclass(c1, c2)        
                
    def get_output_common_supertype(self):
        """Return the common Python supertype inspected from the current stream."""
        return self._common_supertype
                
    def append_object(self, obj, fmt=None, **kwargs):
        if fmt is None:
            otype = type(obj)
        # This is kind of a hack.
        elif fmt in ('bytearray/chunked' 'x-filedescriptor/special'):
            otype = str            
        # If we don't have a display at this point, it means we have a dynamically-typed
        # object stream.  In that case, force the issue and add the default display.
        if not self.__display:
            self.__add_display(otype, force=True)

        # Determine common supertype so we can display it.
        if self._common_supertype is None:
            self._common_supertype = otype
        elif self._common_supertype is not object:
            self._common_supertype = self.__get_common_superclass(otype, self._common_supertype)       
        # Actually append.
        if fmt is not None:
            kwargs['fmt'] = fmt
        self.__display.append_obj(obj, **kwargs)
            
    def __vadjust(self, pos, full, forceuser=False):
        adjustment = self.__scroll.get_vadjustment()
        if not full:
            val = self.__scroll.get_vadjustment().page_increment
            if not pos:
                val = 0 - val;
            newval = adjustment.value + val
        else:
            if pos:
                newval = adjustment.upper
            else:
                newval = adjustment.lower
        newval = max(min(newval, adjustment.upper-adjustment.page_size), adjustment.lower)
        adjustment.value = newval

    def __on_scroll_value_changed(self, vadjust):
        upper = vadjust.upper - vadjust.page_size
        if upper - vadjust.value < (vadjust.page_size/3):
            self.__user_scrolled = False
        else:
            self.__user_scrolled = True         

    def scroll_up(self, full, forceuser=True):
        self.__vadjust(False, full)
        
    def scroll_down(self, full, forceuser=True):
        self.__vadjust(True, full)
        
    def do_copy(self):
        if self.__display:
            return self.__display.do_copy()
        return False

    def __idle_do_autoscroll(self):
        vadjust = self.__scroll.get_vadjustment()
        vadjust.value = max(vadjust.lower, vadjust.upper - vadjust.page_size)
        self.__autoscroll_id = 0

    def do_autoscroll(self):
        if self.__display and self.__display.get_autoscroll():
            if not self.__user_scrolled:
                if self.__autoscroll_id == 0:
                    self.__autoscroll_id = gobject.timeout_add(150, self.__idle_do_autoscroll)
Ejemplo n.º 5
0
class ObjectsDisplay(gtk.VBox):
    __gsignals__ = {
        "object-input": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                         (gobject.TYPE_PYOBJECT, )),
        "status-changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
    }

    def __init__(self, output_spec, context, **kwargs):
        super(ObjectsDisplay, self).__init__(**kwargs)
        self.__context = context
        self.__box = gtk.VBox()
        self.add(self.__box)
        self.__scroll = gtk.ScrolledWindow()
        self.__scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        vadjust = self.__scroll.get_vadjustment()
        vadjust.connect('value-changed', self.__on_scroll_value_changed)
        self.__search = None
        self.__inputarea = None
        self.__output_type = None
        self.__old_focus = None
        self.__box.pack_start(self.__scroll, expand=True)
        self.__display = None
        self.__add_display(output_spec)
        self.__doing_autoscroll = False
        self.__user_scrolled = False
        self.__autoscroll_id = 0
        self._common_supertype = None

    def __add_display(self, output_spec, force=False):
        if output_spec != 'any':
            self.__display = ClassRendererMapping.getInstance().lookup(
                output_spec, self.__context)
        if not self.__display and force:
            self.__display = DefaultObjectsRenderer(self.__context)
        if self.__display:
            self.__display.connect('status-changed', self.__on_status_changed)
            self.__display_widget = self.__display.get_widget()
            self.__display_widget.show_all()
            self.__scroll.add(self.__display_widget)
            self.__output_type = output_spec

    def __on_status_changed(self, renderer):
        self.emit('status-changed')
        self.do_autoscroll()

    def start_search(self, old_focus):
        try:
            self.__display.start_search()
            return True
        except NotImplementedError as e:
            pass
        if self.__search is None:
            self.__search = self.__display.get_search()
            if self.__search is not True:
                self.__box.pack_start(self.__search, expand=False)
                self.__search.connect("close", self.__on_search_close)
        self.__old_focus = old_focus
        if self.__search is not True:
            self.__search.show_all()
            self.__search.focus()
            return True
        return False

    def __on_search_close(self, search):
        if self.__search is not True:
            self.__search.hide()
        if self.__old_focus:
            self.__old_focus.grab_focus()

    def supports_input(self):
        return self.__display and self.__display.supports_input()

    def start_input(self, old_focus):
        if self.__inputarea is None:
            self.__inputarea = self.__display.get_input()
            if self.__inputarea is not True:
                self.__box.pack_start(self.__inputarea, expand=False)
                self.__inputarea.connect("close", self.__on_inputarea_close)
                self.__inputarea.connect("object-input",
                                         self.__on_object_input)
        self.__old_focus = old_focus
        if self.__inputarea is not True:
            self.__inputarea.show_all()
            self.__inputarea.focus()

    def __on_object_input(self, ia, obj, *args):
        _logger.debug("got interactive object input: %s", obj)
        self.emit('object-input', obj)

    def __on_inputarea_close(self, search):
        if self.__inputarea is not True:
            self.__inputarea.hide()
        if self.__old_focus:
            self.__old_focus.grab_focus()

    def get_opt_formats(self):
        if self.__display:
            return self.__display.get_opt_formats()
        return []

    def get_status_str(self):
        return self.__display and self.__display.get_status_str()

    def get_objects(self):
        if self.__display:
            for obj in self.__display.get_objects():
                yield obj
        else:
            raise ValueError("Can't get object snapshot, no display")

    def get_selected_objects(self):
        if self.__display:
            for obj in self.__display.get_selected_objects():
                yield obj
        else:
            raise ValueError("Can't get object snapshot, no display")

    def get_output_type(self):
        """Return the typespec for the current pipeline.  See Pipeline
        for a description of typespecs."""
        return self.__output_type

    def __recurse_get_common_superclass(self, c1, c2):
        for base in c1.__bases__:
            if base == c2:
                return base
            tmp = self.__recurse_get_common_superclass(base, c2)
            if tmp:
                return tmp

    def __get_common_superclass(self, c1, c2):
        if c1 == c2:
            return c1
        if isinstance(c2, c1):
            (c1, c2) = (c2, c1)
        elif not isinstance(c1, c2):
            return object
        return self.__recurse_get_common_superclass(c1, c2)

    def get_output_common_supertype(self):
        """Return the common Python supertype inspected from the current stream."""
        return self._common_supertype

    def append_object(self, obj, fmt=None, **kwargs):
        if fmt is None:
            otype = type(obj)
        # This is kind of a hack.
        elif fmt in ('bytearray/chunked' 'x-filedescriptor/special'):
            otype = str
        # If we don't have a display at this point, it means we have a dynamically-typed
        # object stream.  In that case, force the issue and add the default display.
        if not self.__display:
            self.__add_display(otype, force=True)

        # Determine common supertype so we can display it.
        if self._common_supertype is None:
            self._common_supertype = otype
        elif self._common_supertype is not object:
            self._common_supertype = self.__get_common_superclass(
                otype, self._common_supertype)
        # Actually append.
        if fmt is not None:
            kwargs['fmt'] = fmt
        self.__display.append_obj(obj, **kwargs)

    def __vadjust(self, pos, full, forceuser=False):
        adjustment = self.__scroll.get_vadjustment()
        if not full:
            val = self.__scroll.get_vadjustment().page_increment
            if not pos:
                val = 0 - val
            newval = adjustment.value + val
        else:
            if pos:
                newval = adjustment.upper
            else:
                newval = adjustment.lower
        newval = max(min(newval, adjustment.upper - adjustment.page_size),
                     adjustment.lower)
        adjustment.value = newval

    def __on_scroll_value_changed(self, vadjust):
        upper = vadjust.upper - vadjust.page_size
        if upper - vadjust.value < (vadjust.page_size / 3):
            self.__user_scrolled = False
        else:
            self.__user_scrolled = True

    def scroll_up(self, full, forceuser=True):
        self.__vadjust(False, full)

    def scroll_down(self, full, forceuser=True):
        self.__vadjust(True, full)

    def do_copy(self):
        if self.__display:
            return self.__display.do_copy()
        return False

    def __idle_do_autoscroll(self):
        vadjust = self.__scroll.get_vadjustment()
        vadjust.value = max(vadjust.lower, vadjust.upper - vadjust.page_size)
        self.__autoscroll_id = 0

    def do_autoscroll(self):
        if self.__display and self.__display.get_autoscroll():
            if not self.__user_scrolled:
                if self.__autoscroll_id == 0:
                    self.__autoscroll_id = gobject.timeout_add(
                        150, self.__idle_do_autoscroll)