コード例 #1
0
ファイル: controlador.py プロジェクト: rocel/stage
class ButtonHandler():
    
    CURRENT_COLOR = "black"
    
    ##
    # Funcao init
    # @param canvas : main canvas where we will draw
    def __init__(self,canvas):
        
        self.botoesHandlers = {}
        self.botoesColors = {}
        self.canvas = canvas
        
        self.EVENT_COLORS = {
            "white": self.white,
            "black": self.black,
            "red": self.red,
            "green": self.green,
            "blue": self.blue,
            "cyan": self.cyan,
            "yellow": self.yellow,
            "magenta": self.magenta
        }
        
    ##
    # Funcao de criacao dos botaos de cor
    # @param frame : Frame onde criar os botaos
    def addBtColors(self,frame):
        self.botoesColors = {}
        indice=0
        for i in self.EVENT_COLORS:
            self.botoesColors[i] = Button(frame,background=i)
            self.botoesColors[i].bind("<Button-1>",self.EVENT_COLORS[i])
            self.botoesColors[i].grid(row=0,column=indice)
            self.botoesColors[i]['relief'] = RAISED
            indice=indice+1
    
    ##
    # Defini branco como current color
    # @param event : Tk event 
    def white(self,event):
        self.CURRENT_COLOR = "white"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
    ##
    # Defini preto como current color
    # @param event : Tk event 
    def black(self,event):
        self.CURRENT_COLOR = "black"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
    ##
    # Defini vermelho como current color
    # @param event : Tk event 
    def red(self,event):
        self.CURRENT_COLOR = "red"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
    ##
    # Defini verde como current color
    # @param event : Tk event 
    def green(self,event):
        self.CURRENT_COLOR = "green"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
    ##
    # Defini azul como current color
    # @param event : Tk event 
    def blue(self,event):
        self.CURRENT_COLOR = "blue"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
    ##
    # Defini ciano como current color
    # @param event : Tk event 
    def cyan(self,event):
        self.CURRENT_COLOR = "cyan"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
    ##
    # Defini amarelo como current color
    # @param event : Tk event 
    def yellow(self,event):
        self.CURRENT_COLOR = "yellow"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
    ##
    # Defini magenta como current color
    # @param event : Tk event 
    def magenta(self,event):
        self.CURRENT_COLOR = "magenta"
        self.desabilitaBotoes(self.CURRENT_COLOR)
        self.atribuiCor()
        
    ##
    # Defini CURRENT_COLOR como current color em os modules
    def atribuiCor(self):
        if hasattr(self,"designer"):
            self.designer.CURRENT_COLOR = self.CURRENT_COLOR
        if hasattr(self,"writer"):
            self.writer.CURRENT_COLOR = self.CURRENT_COLOR
        if hasattr(self,"painter"):
            self.painter.CURRENT_COLOR = self.CURRENT_COLOR
        if hasattr(self,"elipse"):
            self.elipse.CURRENT_COLOR = self.CURRENT_COLOR
        if hasattr(self,"retangulo"):
            self.retangulo.CURRENT_COLOR = self.CURRENT_COLOR
        
    ##
    # Defini o botao Design e instancia o objeto Designer
    # @param botao : o botao desenho
    # 
    def addBtDesign(self,botao):
        self.botoesHandlers['desenho'] = botao
        self.botoesHandlers['desenho'].bind("<Button-1>",self.desenhar)
        self.botoesHandlers['desenho'].grid(row=2,column=1)
        self.designer = Designer(self.canvas)
    
    ##
    # Defini o botao Texto e instancia o objeto Writer
    # @param botao : o botao texto
    # 
    def addBtText(self,botao):
        self.botoesHandlers['texto'] = botao
        self.botoesHandlers['texto'].bind("<Button-1>",self.escrever)
        self.botoesHandlers['texto'].grid(row=3,column=1)
        self.writer = Writer(self.canvas)
        
    ##
    # Defini o botao selecionar e instancia o objeto Selector
    # @param botao : o botao selecionar
    # 
    def addBtSelection(self,botao):
        self.botoesHandlers['selecionar'] = botao
        self.botoesHandlers['selecionar'].bind("<Button-1>",self.selecionar)
        self.botoesHandlers['selecionar'].grid(row=4,column=1)
        self.selector = Selector(self.canvas)
        self.botoesHandlers['selecionar']['relief'] = SUNKEN
        self.selector.addEvents()
    
    ##
    # Defini o botao pintar e instancia o objeto Painter
    # @param botao : o botao pintar
    # 
    def addBtPaint(self,botao):
        self.botoesHandlers['pintar'] = botao
        self.botoesHandlers['pintar'].bind("<Button-1>",self.pintar)
        self.botoesHandlers['pintar'].grid(row=5,column=1)
        self.painter = Painter(self.canvas)
        
    ##
    # Defini o botao elipse e instancia o objeto Elipse
    # @param botao : o botao elipse 
    def addBtOval(self,botao):
        self.botoesHandlers['elipse'] = botao
        self.botoesHandlers['elipse'].bind("<Button-1>",self.elipseF)
        self.botoesHandlers['elipse'].grid(row=6,column=1)
        self.elipse = Elipse(self.canvas)
        
    def addBtLine(self,botao):
        self.botoesHandlers['reta'] = botao
        self.botoesHandlers['reta'].bind("<Button-1>",self.funcao_reta)
        self.botoesHandlers['reta'].grid(row=7,column=1)
        self.reta = Line(self.canvas)
        
    def addBtRectangule(self,botao):
        self.botoesHandlers['retangulo'] = botao
        self.botoesHandlers['retangulo'].bind("<Button-1>",self.retanguloF)
        self.botoesHandlers['retangulo'].grid(row=8,column=1)
        self.retangulo = Rectangule(self.canvas)
        
    ##
    # Retira os eventos 
    # 
    def retiraEventos(self):
        #self.canvas.unbind_all(('<B1-Motion>','<ButtonRelease-1>','<1>','<Double-Button-1>','<Key>'))
        self.canvas.unbind('<B1-Motion>')
        self.canvas.unbind('<ButtonRelease-1>')
        self.canvas.unbind('<1>')
        self.canvas.unbind('<Double-Button-1>')
        self.canvas.unbind('<Key>')
        
    ##
    # Desabilita o botao
    # @param botao : o botao
    # 
    def desabilitaBotoes(self,botao):
        listaBotoes=[]
        if botao in self.botoesHandlers:
            listaBotoes = self.botoesHandlers
        else:
            listaBotoes = self.botoesColors
        
        for i in listaBotoes:
            if i != botao:
                if listaBotoes[i]["relief"] == SUNKEN:
                    listaBotoes[i]['relief'] = RAISED
        
    ##
    # Manipula o botao
    # @param botao : o botao a manipular
    # 
    def manipulaBotao(self,botao):
        if self.botoesHandlers[botao]["relief"] == RAISED:
            self.retiraEventos()
            self.desabilitaBotoes(botao)
            self.botoesHandlers[botao]['relief'] = SUNKEN
    
    ##
    # Chamada a funcao do Designer
    # @param event : Tk event
    # 
    def desenhar(self,event):
        self.manipulaBotao('desenho')
        self.designer.addEvents()
    ##
    # Chamada a funcao do Writer
    # @param event : Tk event
    # 
    def escrever(self,event):
        self.manipulaBotao('texto')
        self.writer.addEvents()
    ##
    # Chamada a funcao do Selector
    # @param event : Tk event
    # 
    def selecionar(self,event):
        self.manipulaBotao('selecionar')
        self.selector.addEvents()
    ##
    # Chamada a funcao do Painter
    # @param event : Tk event
    # 
    def pintar(self,event):
        self.manipulaBotao('pintar')
        self.painter.addEvents()
    ##
    # Chamada a funcao do Elipse
    # @param event : Tk event
    # 
    def elipseF(self,event):
        self.manipulaBotao('elipse')
        self.elipse.addEvents()
    ##
    # Chamada a funcao do Rectangulo
    # @param event : Tk event
    # 
    def retanguloF(self,event):
        self.manipulaBotao('retangulo')
        self.retangulo.addEvents()
        
     ##
    # Chamada a funcao da Reta
    # @param event : Tk event
    # 
    def funcao_reta(self,event):
        self.manipulaBotao('reta')
        self.reta.addEvents()
コード例 #2
0
ファイル: controlador.py プロジェクト: rocel/stage
 def addBtDesign(self,botao):
     self.botoesHandlers['desenho'] = botao
     self.botoesHandlers['desenho'].bind("<Button-1>",self.desenhar)
     self.botoesHandlers['desenho'].grid(row=2,column=1)
     self.designer = Designer(self.canvas)