Пример #1
0
class Line:

    CURRENT_COLOR = "black"
    TAG = "reta"
    i = 0

    def __init__(self, canvas):

        self.canvas = canvas
        self.coordinates = Coordinates(self.canvas)

    def addEvents(self):
        self.canvas.bind("<1>", self.recomecaLinha)
        self.canvas.bind("<B1-Motion>", self.desenhaReta)

    def recomecaLinha(self, event):
        self.ultimo_P = self.coordinates.coordenadas()
        self.i = self.i + 1
        self.TAG = "reta" + str(self.i)

    def desenhaReta(self, event):

        (x, y) = self.coordinates.coordenadas()

        try:
            P = (x, y)
            self.canvas.delete(self.TAG)
            self.canvas.create_line(self.ultimo_P, P, fill=self.CURRENT_COLOR, tag=self.TAG)
        except:
            return
Пример #2
0
class Rectangule():
    
    CURRENT_COLOR = "black"
    TAG = 'retangulo'
    i = 0
    
    ##
    # Funcao init
    # @param canvas : canvas where to draw the rectangle
    # 
    def __init__(self, canvas):
        
        self.canvas = canvas
        self.coordinates = Coordinates(self.canvas)

            
    ##
    # Faze binding entre botoes e funcaos 
    def addEvents(self):
        self.canvas.bind("<1>", self.recomecaLinha)
        self.canvas.bind('<B1-Motion>',self.desenhaRectangulo)
        
    ##
    # Restart a new rectangle
    # @param event : Tk event
    def recomecaLinha(self,event):
        self.ultimo_P=self.coordinates.coordenadas()
        self.i = self.i+1
        self.TAG = 'retangulo' + str(self.i)
        
    ##
    # Draw the rectanle on the canvas
    # @param event : Tk event
    def desenhaRectangulo(self,event):
        (x,y) = self.coordinates.coordenadas()
        
        try:
             P = (x,y)
             self.canvas.delete(self.TAG) 
             self.canvas.create_rectangle(self.ultimo_P,P,outline=self.CURRENT_COLOR,fill='white',tag=self.TAG)
        except:
            return
Пример #3
0
class Designer:

    CURRENT_COLOR = "black"

    ##
    # Funcao init
    # @param canvas : canvas where to draw
    def __init__(self, canvas):

        self.canvas = canvas
        self.coordinates = Coordinates(self.canvas)

    ##
    # Faze binding entre botoes e funcaos
    def addEvents(self):
        self.canvas.bind("<B1-Motion>", self.desenhaLinha)
        self.canvas.bind("<1>", self.recomecaLinha)

    ##
    # Draw the line
    # @param event : Tk event
    def desenhaLinha(self, event):
        (x, y) = self.coordinates.coordenadas()

        try:
            P = (x, y)
            self.canvas.create_line(self.ultimo_P, P, fill=self.CURRENT_COLOR)
            self.ultimo_P = P
        except:
            self.ultimo_P = (x, y)

    ##
    # Restart a new line
    # @param event : Tk event
    def recomecaLinha(self, event):
        self.ultimo_P = self.coordinates.coordenadas()
Пример #4
0
class Writer():
    
    CURRENT_COLOR = "black"
    
    ##
    # Funcao init
    # @param canvas : canvas where to write
    # 
    def __init__(self, canvas):
        
        self.canvas = canvas
        self.coordinates = Coordinates(self.canvas)
        
    ##
    # Faze binding entre botoes e funcaos
    # 
    def addEvents(self):
        self.canvas.bind("<Double-Button-1>", self.set_focus)
        self.canvas.bind("<1>", self.set_cursor)
        self.canvas.bind("<Key>", self.handle_key)
        
    ##
    # Has the canvas the focus?
    # @return true if object has focus or false if not
    def has_focus(self):
        return self.canvas.focus()

    ##
    # Has the canvas the selection?
    # @return true if object has selection or false if not
    def has_selection(self):
        # hack to work around bug in Tkinter 1.101 (Python 1.5.1)
        return self.canvas.tk.call(self.canvas._w, 'select', 'item')

    ##
    # Set the focus on the canvas
    # @param event : Tk event
    def set_focus(self, event):
       
        
        self.set_cursor(event)
        (x,y) = self.coordinates.coordenadas()
               
        self.canvas.create_text(x, y,text='     ',fill=self.CURRENT_COLOR)

        if self.canvas.type(CURRENT) != "text":
           return

        self.coordinates.highlight(CURRENT)

        # move focus to item
        self.canvas.focus_set() # move focus to canvas
        self.canvas.focus(CURRENT) # set focus to text item
        self.canvas.select_from(CURRENT, 0)
        self.canvas.select_to(CURRENT, END)


    ##
    # Set the cursor on the item
    # @param event : Tk event
    def set_cursor(self,event):
        # move insertion cursor
        item = self.has_focus()
        if not item:
            self.canvas.select_clear()
            return # or do something else
        
        
        # translate to the canvas coordinate system
        x = self.canvas.canvasx(event.x)
        y = self.canvas.canvasy(event.y)

        self.canvas.icursor(item, "@%d,%d" % (x, y))
        self.canvas.select_clear()

    ##
    # Keys handler
    # @param event : Tk event
    def handle_key(self, event):
        # widget-wide key dispatcher
        item = self.has_focus()
        if not item:
            return

        insert = self.canvas.index(item, INSERT)

        if event.char >= " ":
            # printable character
            if self.has_selection():
                self.canvas.dchars(item, SEL_FIRST, SEL_LAST)
                self.canvas.select_clear()
            self.canvas.insert(item, "insert", event.char)
            self.coordinates.highlight(item)

        elif event.keysym == "BackSpace":
            if self.has_selection():
                self.canvas.dchars(item, SEL_FIRST, SEL_LAST)
                self.canvas.select_clear()
            else:
                if insert > 0:
                    self.canvas.dchars(item, insert-1, insert)
            Coordinates.highlight(item)

        # navigation
        elif event.keysym == "Home":
            self.canvas.icursor(item, 0)
            self.canvas.select_clear()
        elif event.keysym == "End":
            self.canvas.icursor(item, END)
            self.canvas.select_clear()
        elif event.keysym == "Right":
            self.canvas.icursor(item, insert+1)
            self.canvas.select_clear()
        elif event.keysym == "Left":
            self.canvas.icursor(item, insert-1)
            self.canvas.select_clear()
        elif event.keysym == "Return":
            self.canvas.insert(item, insert, '\n')
            self.canvas.select_clear()

        else:
            pass # print event.keysym