Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/raw-rev/6f328d849ba8
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 04:16:16 2012
Кодировка:

# HG changeset patch
# User is_rusinov
# Date 1292065365 -10800
# Node ID 6f328d849ba87e8d38c518f8cde34723a2851c22
# Parent bbe2a8e64826ae0d4e9e08240237b0ee68a8b296
+ mouse motion, save listbox focus

diff -r bbe2a8e64826 -r 6f328d849ba8 Interface.py
--- a/Interface.py Sat Dec 11 00:47:07 2010 +0300
+++ b/Interface.py Sat Dec 11 14:02:45 2010 +0300
@@ -1,3 +1,5 @@
+import math
+import tkColorChooser
from Tkinter import *

from State import *
@@ -13,12 +15,11 @@
self.offset_x = offset_x
self.offset_y = offset_y
self.after_id = 0
- self.enter1_bound = False
- self.enter3_bound = False
self.mouse_offset_x = 0
self.mouse_offset_y = 0
self.mouse_zoom = 0
self.zoom_divisor = 1
+ self.selected_state = None
self.is_started = False
self.char = None
self.keys = dict()
@@ -113,53 +114,77 @@
fill=color, outline="", tag="cell")
self.cells[row].append(cell)

+ def draw_line(self, x1, y1, x2, y2, order=1):
+ answer = []
+ if abs(x1 - x2) > abs(y1 - y2):
+ dx = x2 - x1
+ abs_dx = abs(dx)
+ dy = float(y2 - y1)
+ while x1 != x2:
+ x1 = x1 + dx / abs_dx
+ y1 = y1 + dy / abs_dx
+ answer.append((self.rounding(y1), x1))
+ else:
+ dx = float(x2 - x1)
+ dy = y2 - y1
+ abs_dy = abs(dy)
+ while y1 != y2:
+ x1 = x1 + dx / abs_dy
+ y1 = y1 + dy / abs_dy
+ answer.append((y1, self.rounding(x1)))
+ self.new_state(answer, order)
+
+ def rounding(self, num):
+ return int(num + math.copysign(0.5, num))
+
+ def new_state(self, cells, order=1):
+ num_states = len(automata.states)
+ changed_cells = []
+ for row, col in cells:
+ if col >= 0 and row >= 0:
+ try:
+ index = (automata.symbols[automata.field[row][col]] + num_states + order) % num_states
+ if self.char != None and self.keys.has_key(self.char):
+ index = self.keys[self.char]
+ automata.field[row][col] = automata.states[index].symbol
+ changed_cells.append((row, col))
+ except:
+ pass
+ self.draw_cell(changed_cells)
+
def press1(self, event):# drawer
- if self.enter1_bound:
- canvas.tag_unbind("cell", "")
- self.enter1_bound = False
- else:
- column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
- row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
- index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states)
- if self.char != None and self.keys.has_key(self.char):
- index = self.keys[self.char]
- automata.field[row][column] = automata.states[index].symbol
- self.draw_cell([(row, column)])
-
- def d_press1(self, event):# drawer
- canvas.tag_bind("cell", "", self.enter1)
- self.enter1_bound = True
-
- def enter1(self, event):
- column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
+ self.col1 = event.x / (self.cell_size + self.line_width) - self.offset_x
+ self.row1 = event.y / (self.cell_size + self.line_width) - self.offset_y
+ if self.col1 >= 0 and self.row1 >= 0:
+ self.new_state([(self.row1, self.col1)])
+
+ def motion1(self, event):# drawer
+ col = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
- index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states)
- if self.char != None and self.keys.has_key(self.char):
- index = self.keys[self.char]
- automata.field[row][column] = automata.states[index].symbol
- self.draw_cell([(row, column)])
+ if not (self.col1 == col and self.row1 == row):
+ if abs(self.col1 - col) <= 1 and abs(self.row1 - row) <= 1:
+ self.new_state([(row, col)])
+ else:
+ self.draw_line(self.col1, self.row1, col, row)
+ self.col1 = col
+ self.row1 = row

def press3(self, event):# drawer
- if self.enter3_bound:
- canvas.tag_unbind("cell", "")
- self.enter3_bound = False
- else:
- column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
- row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
- index = (automata.symbols[automata.field[row][column]] + len(automata.states) - 1) % len(automata.states)
- automata.field[row][column] = automata.states[index].symbol
- self.draw_cell([(row, column)])
+ self.col3 = event.x / (self.cell_size + self.line_width) - self.offset_x
+ self.row3 = event.y / (self.cell_size + self.line_width) - self.offset_y
+ if self.col1 >= 0 and self.row1 >= 0:
+ self.new_state([(self.row3, self.col3)], -1)

- def d_press3(self, event):# drawer
- canvas.tag_bind("cell", "", self.enter3)
- self.enter3_bound = True
-
- def enter3(self, event):
- column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
+ def motion3(self, event):# drawer
+ col = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
- index = (automata.symbols[automata.field[row][column]] - 1) % len(automata.states)
- automata.field[row][column] = automata.states[index].symbol
- self.draw_cell([(row, column)])
+ if not (self.col3 == col and self.row3 == row):
+ if abs(self.col3 - col) <= 1 and abs(self.row3 - row) <= 1:
+ self.new_state([(row, col)], -1)
+ else:
+ self.draw_line(self.col3, self.row3, col, row, -1)
+ self.col3 = col
+ self.row3 = row

def press_key(self, event):# drawer
self.char = event.char
@@ -194,73 +219,68 @@
self.draw()

def to_top(self):# replace choosen state to top
- selected = state_list.curselection()
- if len(selected):
- index = int(selected[0])
+ selected = self.selected_state
+ if selected != None:
+ index = selected
state = automata.states[index]
print state
del automata.states[index]
automata.states.insert(0, state)
+ self.selected_state = 0
self.refresh_list()
- state_list.selection_set(0)
- print automata.states

def to_bottom(self):# replace choosen state to botton
- selected = state_list.curselection()
- if len(selected):
- index = int(selected[0])
+ selected = self.selected_state
+ if selected != None:
+ index = selected
state = automata.states[index]
print state
del automata.states[index]
automata.states.append(state)
+ self.selected_state = len(automata.states) - 1
self.refresh_list()
- state_list.selection_set("end")
- print automata.states

def upwards(self):
- selected = state_list.curselection()
- if len(selected):
- index = int(selected[0])
+ selected = self.selected_state
+ if selected != None:
+ index = selected
if index > 0:
state = automata.states[index]
print state
del automata.states[index]
automata.states.insert(index - 1, state)
+ self.selected_state = index - 1
self.refresh_list()
- state_list.selection_set(index - 1)
- print automata.states

def downwards(self):
- selected = state_list.curselection()
- if len(selected):
- index = int(selected[0])
+ selected = self.selected_state
+ if selected != None:
+ index = selected
if index < state_list.size() - 1:
state = automata.states[index]
print state
del automata.states[index]
automata.states.insert(index + 1, state)
+ self.selected_state = index + 1
self.refresh_list()
- state_list.selection_set(index + 1)
- print automata.states

def delete_state(self):# delete choosen state
- selected = state_list.curselection()
- if len(selected):
- index = int(selected[0])
+ selected = self.selected_state
+ if selected != None:
+ index = selected
print automata.states[index]
del automata.symbols[automata.states[index].symbol]
for key in self.keys.keys():
if self.keys[key] == index:
del self.keys[key]
- del automata.states[index]
+ self.selected_state = index
self.refresh_list()
- print automata.states

def add(self):# add new state
name = state_name.get()
symbol = state_symbol.get()
key = state_key.get().lower()
- color = state_color.get()
+ color = state_color.cget("bg")
nums = []
for i, value in enumerate(ckeckbox_nums):
if value:
@@ -286,13 +306,13 @@
self.refresh_list()

def change(self):# change chosen state
- selected = state_list.curselection()
- if len(selected):
- index = int(selected[0])
+ selected = self.selected_state
+ if selected != None:
+ index = selected
name = state_name.get()
symbol = state_symbol.get()
key = state_key.get().lower()
- color = state_color.get()
+ color = state_color.cget("bg")
nums = []
for i, value in enumerate(ckeckbox_nums):
print i, value.get()
@@ -334,9 +354,11 @@
state_list.delete(0, "end")
for state in automata.states:
state_list.insert("end", state)
+ if self.selected_state != None:
+ state_list.selection_set(self.selected_state)
def list_mouse_release(self, event):
- print 'hello'
selected = state_list.curselection()
+ self.selected_state = int(selected[0])
if len(selected):
index = int(selected[0])
state = automata.states[index]
@@ -348,10 +370,12 @@
if self.keys[key] == index:
state_key.delete(0, "end")
state_key.insert(0, key)
- state_color.delete(0, "end")
- state_color.insert(0, state.color)
+ state_color.config(bg=state.color)
for i in range(9):
ckeckbox_nums[i].set(i in state.nums)
+
+ def choose_color(self, event):
+ state_color.config(bg=tkColorChooser.askcolor()[1])



@@ -365,9 +389,9 @@
handlers = Handlers()

canvas.tag_bind("cell", "<1>", handlers.press1)
-canvas.tag_bind("cell", "", handlers.d_press1)
+canvas.tag_bind("cell", "", handlers.motion1)
canvas.tag_bind("cell", "<3>", handlers.press3)
-canvas.tag_bind("cell", "", handlers.d_press3)
+canvas.tag_bind("cell", "", handlers.motion3)
canvas.bind_all("", handlers.press_key)
canvas.bind_all("", handlers.release_key)
#canvas.bind("", handlers.press1_ctrl)
@@ -432,6 +456,7 @@
Label(info_frame, text="Color").grid(row=3, column=0)
state_color = Label(info_frame, background="white", cursor="plus")
state_color.grid(row=3, column=1, sticky="ew")
+state_color.bind('<1>', handlers.choose_color)
info_frame.pack(side="top")