Beispiel #1
0
    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
Beispiel #2
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
Beispiel #3
0
class Selector():
    
    ##
    # 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("<KeyPress-Delete>", self.delete)
        self.canvas.bind("<1>", self.selecionaItem)
        self.canvas.bind('<B1-Motion>',self.trataItem)
        self.canvas.bind('<ButtonRelease-1>',self.selecionaItem)
        self.canvas.bind('<Motion>',self.rescaleCursor)

    ##
    # Delete the selected item
    # @param event : Tk event
    def delete(self,event):
        item = CURRENT
        self.coordinates.deleta_caixa_selecao()
        if item:
            self.canvas.delete(item)
    
    ##
    # Select the item and add an bounding rectangle
    # @param event : Tk event
    def selecionaItem(self,event):
        
        if self.canvas.type(CURRENT) != None:
            self.coordinates.highlight(CURRENT)
            
    ##
    # Put the focus on the canvas
    # @param event : Tk event
    def trataItem(self,event):
        corners = ["highlightTR","highlightTL","highlightBR","highlightBL"]
        if self.canvas.type(CURRENT) == None or self.canvas.gettags(CURRENT)[0] in corners:
            print(self.canvas.gettags(CURRENT)[0])
            
            return
        item = CURRENT
        if item:
            if self.canvas.type(CURRENT) != 'image':
                (x1,y1,x2,y2) = self.canvas.coords(item)
                x = x1+int((x2-x1)/2)
                y = y1+int((y2-y1)/2)
            else:
                (x,y) = self.canvas.coords(item)
            self.move(event,item,x,y)
            #self.dimensiona(event,item,x,y)
            # move focus to item
            self.canvas.focus_set() # move focus to canvas
            self.canvas.focus(CURRENT) # set focus to text item
    
    ##
    # Change the cursor icon if point one of the selection corner
    # @param event : Tk event
    def rescaleCursor(self,event):
        if self.canvas.type(CURRENT) == None:
            return
        item=CURRENT
        if item:
            if "highlightTR" in self.canvas.gettags(item):
                cursor='top_right_corner'
            elif "highlightTL" in self.canvas.gettags(item):
                cursor='top_left_corner'
            elif "highlightBR" in self.canvas.gettags(item):
                cursor='bottom_right_corner'
            elif "highlightBL" in self.canvas.gettags(item):
                cursor='bottom_left_corner'
            else:
                cursor='arrow'
            self.canvas.config(cursor=cursor)
            
    ##
    # move the selected item to the new coordonates x and y
    # @param item : item to move
    # @param x1 : new x
    # @param y1 : new y
    def move(self,event,item,x1,y1):
        x2 = self.canvas.canvasx(event.x)
        y2 = self.canvas.canvasy(event.y)
        self.canvas.move(item,x2-x1,y2-y1)