Example #1
0
 def draw(self, context):
   if self.svg:      
     context.save()
     buf = self._get_buffer(context)
     context.rotate(math.radians(self.svg_rotate))
     context.scale(1.0 / float(self.svg.props.width)  / self.resolutionx, \
                   1.0 / float(self.svg.props.height) / self.resolutiony)
     context.set_source_surface(buf, - self.svg.props.width  * self.resolutionx / 2.0, \
                                     - self.svg.props.height * self.resolutiony / 2.0)
     context.paint()
     context.restore()    
     if self.draw_bounding_box:       # draw bounding box
       context.set_line_width(0.02)
       context.set_source_rgb(1.0, 0.0, 0.0)
       context.rectangle(-0.5, -0.5, 1.0, 1.0)
       context.stroke()
     
   if self.viewcode:
     if not self.editor:
       self.editor = TextEditor(self.code)
     context.save()
     context.rotate(math.radians(-self.rotation)) # unrotate code editors :-)
     context.scale(float(self.editor.width  / self.width), \
                   float(self.editor.height / self.height))
     context.translate(-0.5, -1.7) # make space for thought buggle
     self.editor.draw(context, self.x, self.y)
     context.restore()
Example #2
0
 def __init__(self):
   Thing.__init__(self)
   TextEditor.__init__(self, "> ")
   self.cursorindex = 2
   
   self.x = 0.0
   self.y = 0.0
   self.width = 0.2
   self.height = 0.2
   
   fontDescription = pango.FontDescription("Monospace 10")
   self.layout.set_font_description(fontDescription)
   
   self.lock_scale = False
   self.lock_position = True
   
   self.first = True
Example #3
0
def create_dialog(obj, obj_name):
    """Creates the editor dialog and returns a tuple (dialog, func) where func
    is the function to be called with the dialog instance as argument, after 
    quitting the dialog box
    
    The role of this intermediate function is to allow easy monkey-patching.
    (uschmitt suggested this indirection here so that he can monkey patch 
    oedit to show eMZed related data)
    """
    # Local import
    from widgets.arrayeditor import ArrayEditor
    from widgets.texteditor import TextEditor
    from widgets.collectionseditor import CollectionsEditor
    from utilities.utils import ndarray, FakeObject, Image, is_known_type, DataFrame, Series

    if DataFrame is not FakeObject:
        from widgets.dataframeeditor import DataFrameEditor

    conv_func = lambda data: data
    readonly = not is_known_type(obj)
    if isinstance(obj, ndarray) and ndarray is not FakeObject:
        dialog = ArrayEditor()
        if not dialog.setup_and_check(obj, title=obj_name, readonly=readonly):
            return
    elif isinstance(obj, Image) and Image is not FakeObject \
      and ndarray is not FakeObject:
        dialog = ArrayEditor()
        import numpy as np
        data = np.array(obj)
        if not dialog.setup_and_check(data, title=obj_name, readonly=readonly):
            return
        # from spyder.pil_patch import Image
        conv_func = lambda data: Image.fromarray(data, mode=obj.mode)
    elif isinstance(obj, (DataFrame, Series)) and DataFrame is not FakeObject:
        dialog = DataFrameEditor()
        if not dialog.setup_and_check(obj):
            return
    elif str(obj):
        dialog = TextEditor(obj, title=obj_name, readonly=readonly)
    else:
        dialog = CollectionsEditor()
        dialog.setup(obj, title=obj_name, readonly=readonly)

    def end_func(dialog):
        return conv_func(dialog.get_value())

    return dialog, end_func
Example #4
0
class Thing(Proto):

  def __init__(self):
    Proto.__init__(self)
    self.x = 0.0
    self.y = 0.0
    self.width = 1.0
    self.height = 1.0
    self.rotation = 0.0


    self.can_drag = True
    self.draw_bounding_box = False
    self.viewcode = False
    self.code = "This is not the code"
    self.editor = None

    # svg code
    self.svg = None
    self.svg_rotate = 0.0
    self._buffer = None
    self.resolutionx = 15.0 # TheWorld.width / 10.0  #15.0
    self.resolutiony = 15.0 # TheWorld.height / 10.0
    # svg's are saved width,height = 100 pts

    # handy counter until I can figure out how to
    # handle setting vars outside myfn scope
    self.counter = 0
    
    # ignore workspace scaling, translation and rotation
    self.lock_scale = False
    self.lock_position = False
    self.lock_rotation = False
  

  def __get_center_x(self):
    return self.x + (self.width / 2.0)
  center_x = property(__get_center_x)
  
  def __get_center_y(self):
    return self.y + (self.height / 2.0)
  center_y = property(__get_center_y)
  


  # TODO update_class(self, code)

  # NOTE: After we update an instance it is no longer affected by updates to the class 
  def update_instance(self): 
    self.code = self.editor.text
    s = "def f(self):\n"
    for line in self.code.splitlines():
      s+= "  " + line + "\n"
    s += "self.myfn = f"
    exec(s)
    print "updated", self.__class__.__name__
    
    
  def myfn(self, arg):
    pass
    
  def nullfn(self, arg):
    pass

  def tick(self):
    if self.viewcode: # pause the object when viewing code
      return
    
    self.counter += 1 # handy counter
    if self.myfn:
      try:
        self.myfn(self)
      except:
        self.myfn = self.nullfn # disable fn processing
        #print "============================================================"
        for e in sys.exc_info():
          print e
        #print "============================================================"

      
    
    
  def draw(self, context):
    if self.svg:      
      context.save()
      buf = self._get_buffer(context)
      context.rotate(math.radians(self.svg_rotate))
      context.scale(1.0 / float(self.svg.props.width)  / self.resolutionx, \
                    1.0 / float(self.svg.props.height) / self.resolutiony)
      context.set_source_surface(buf, - self.svg.props.width  * self.resolutionx / 2.0, \
                                      - self.svg.props.height * self.resolutiony / 2.0)
      context.paint()
      context.restore()    
      if self.draw_bounding_box:       # draw bounding box
        context.set_line_width(0.02)
        context.set_source_rgb(1.0, 0.0, 0.0)
        context.rectangle(-0.5, -0.5, 1.0, 1.0)
        context.stroke()
      
    if self.viewcode:
      if not self.editor:
        self.editor = TextEditor(self.code)
      context.save()
      context.rotate(math.radians(-self.rotation)) # unrotate code editors :-)
      context.scale(float(self.editor.width  / self.width), \
                    float(self.editor.height / self.height))
      context.translate(-0.5, -1.7) # make space for thought buggle
      self.editor.draw(context, self.x, self.y)
      context.restore()
      
      # draw bounding box
      #context.set_line_width(0.02)
      #context.set_source_rgb(1.0, 0.0, 0.0)
      #context.rectangle(-0.5, -0.5, 1.0, 1.0)
      #context.stroke()


  def hit_test(self, x, y):
    return MathLib.rect_test(x, y, self.x, self.y, self.width, self.height)
  
  
  def _get_buffer(self, context):
    if self._buffer:
      return self._buffer
    self._buffer = self._make_buffer(context, 1.0)
    return self._buffer
  

  def _make_buffer(self, context, scale):
    surface = context.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, \
                                                  int(self.svg.props.width * self.resolutionx), \
                                                  int(self.svg.props.height * self.resolutiony))
    ctx = cairo.Context(surface)
    ctx.scale(self.resolutionx, self.resolutiony)
    self.svg.render_cairo(ctx)

    # draw bounding box
    #ctx.set_line_width(0.4)
    #ctx.set_source_rgb(1.0, 0.0, 0.0)
    #ctx.rectangle(0.0, 0.0, self.svg.props.width, self.svg.props.height)
    #ctx.stroke()

    del ctx
    return surface

 
  def do_key_press_event(self, event):
    if self.viewcode and self.editor:
      self.editor.do_key_press_event(event)


  def do_button_press_event(self, event):
    pass