Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/rev/ac96d7c2c7c4
Дата изменения: Unknown
Дата индексирования: Mon Oct 1 23:00:04 2012
Кодировка:
cca: ac96d7c2c7c4

cca

changeset 69:ac96d7c2c7c4

+ clean_field and random_fill (fix #13); correct critical mistake with changing of dicts while state_list changes
author is_rusinov
date Sat, 11 Dec 2010 22:32:04 +0300
parents c4a108b8ea02
children d415e749c701
files Interface.py
diffstat 1 files changed, 71 insertions(+), 26 deletions(-) [+]
line diff
     1.1 --- a/Interface.py	Sat Dec 11 22:30:01 2010 +0300
     1.2 +++ b/Interface.py	Sat Dec 11 22:32:04 2010 +0300
     1.3 @@ -1,4 +1,5 @@
     1.4  import math
     1.5 +import random
     1.6  import tkColorChooser
     1.7  from Tkinter import *
     1.8  
     1.9 @@ -144,7 +145,7 @@
    1.10  			if col >= 0 and row >= 0:
    1.11  				try:
    1.12  					index = (automata.symbols[automata.field[row][col]] + num_states + order) % num_states
    1.13 -					if self.char != None and self.keys.has_key(self.char):
    1.14 +					if self.char != None and self.char in self.keys:
    1.15  						index = self.keys[self.char]
    1.16  					automata.field[row][col] = automata.states[index].symbol
    1.17  					changed_cells.append((row, col))
    1.18 @@ -157,7 +158,7 @@
    1.19  		self.row1 = event.y / (self.cell_size + self.line_width) - self.offset_y
    1.20  		if self.col1 >= 0 and self.row1 >= 0:
    1.21  			self.new_state([(self.row1, self.col1)])
    1.22 -		
    1.23 +	
    1.24  	def motion1(self, event):# drawer
    1.25  		col = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
    1.26  		row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
    1.27 @@ -223,10 +224,10 @@
    1.28  		if selected != None:
    1.29  			index = selected
    1.30  			state = automata.states[index]
    1.31 -			print state
    1.32  			del automata.states[index]
    1.33  			automata.states.insert(0, state)
    1.34  			self.selected_state = 0
    1.35 +			self.refresh_dicts()
    1.36  			self.refresh_list()
    1.37  	
    1.38  	def to_bottom(self):# replace choosen state to botton
    1.39 @@ -234,10 +235,10 @@
    1.40  		if selected != None:
    1.41  			index = selected
    1.42  			state = automata.states[index]
    1.43 -			print state
    1.44  			del automata.states[index]
    1.45  			automata.states.append(state)
    1.46  			self.selected_state = len(automata.states) - 1
    1.47 +			self.refresh_dicts()
    1.48  			self.refresh_list()
    1.49  	
    1.50  	def upwards(self):
    1.51 @@ -246,10 +247,10 @@
    1.52  			index = selected
    1.53  			if index > 0:
    1.54  				state = automata.states[index]
    1.55 -				print state
    1.56  				del automata.states[index]
    1.57  				automata.states.insert(index - 1, state)
    1.58  				self.selected_state = index - 1
    1.59 +				self.refresh_dicts()
    1.60  				self.refresh_list()
    1.61  	
    1.62  	def downwards(self):
    1.63 @@ -258,31 +259,29 @@
    1.64  			index = selected
    1.65  			if index < state_list.size() - 1:
    1.66  				state = automata.states[index]
    1.67 -				print state
    1.68  				del automata.states[index]
    1.69  				automata.states.insert(index + 1, state)
    1.70  				self.selected_state = index + 1
    1.71 +				self.refresh_dicts()
    1.72  				self.refresh_list()
    1.73  	
    1.74  	def delete_state(self):# delete choosen state
    1.75  		selected = self.selected_state
    1.76 -		if selected != None:
    1.77 +		if selected != None and len(automata.states) != 1:
    1.78  			index = selected
    1.79 -			print automata.states[index]
    1.80 -			del automata.symbols[automata.states[index].symbol]
    1.81 -			for key in self.keys.keys():
    1.82 -				if self.keys[key] == index:
    1.83 -					del self.keys[key]
    1.84 -			del automata.st_sym[automata.states[index].symbol]
    1.85 +			symbol = automata.states[index].symbol
    1.86  			del automata.states[index]
    1.87 +			self.refresh_dicts()
    1.88  			if index in automata.states:
    1.89  				self.selected_state = index
    1.90 -			elif len(automata.states):
    1.91 +			else:
    1.92  				self.selected_state = len(automata.states) - 1
    1.93 -			else:
    1.94 -				self.selected_state = None
    1.95  			self.refresh_list()
    1.96  			self.select_item(self.selected_state)
    1.97 +			self.draw_changed_state(symbol, automata.states[0].symbol)
    1.98 +		else:
    1.99 +			error.config(text="Operation is refused")
   1.100 +			error.after(2000, self.clear_error)
   1.101  	
   1.102  	def add(self):# add new state
   1.103  		name = state_name.get()
   1.104 @@ -295,18 +294,22 @@
   1.105  					nums.append(i)
   1.106  		if self.keys.has_key(key):
   1.107  			error.config(text="State with such key has already existed")
   1.108 +			error.after(2000, self.clear_error)
   1.109  			state_key.focus()
   1.110  		elif len(key) != 1:
   1.111  			error.config(text="Bad key for state")
   1.112 +			error.after(2000, self.clear_error)
   1.113  			state_key.focus()
   1.114  		elif automata.symbols.has_key(symbol):
   1.115  			error.config(text="State with such symbol has already existed")
   1.116 +			error.after(2000, self.clear_error)
   1.117  			state_symbol.focus()
   1.118  		elif len(symbol) != 1:
   1.119  			error.config(text="Bad symbol for state")
   1.120 +			error.after(2000, self.clear_error)
   1.121  			state_symbol.focus()
   1.122  		else:
   1.123 -			state = State(name, symbol, color, nums)
   1.124 +			state = State(name, symbol, color, key, nums)
   1.125  			automata.states.append(state)
   1.126  			automata.symbols[symbol] = len(automata.states) - 1
   1.127  			self.keys[key] = len(automata.states) - 1
   1.128 @@ -330,27 +333,37 @@
   1.129  					nums.append(i)
   1.130  			if self.keys.has_key(key) and self.keys[key] != index:
   1.131  				error.config(text="State with such key has already existed")
   1.132 +				error.after(2000, self.clear_error)
   1.133  				state_key.focus()
   1.134  			elif len(key) != 1:
   1.135  				error.config(text="Bad key for state")
   1.136 +				error.after(2000, self.clear_error)
   1.137  				state_key.focus()
   1.138  			elif automata.symbols.has_key(symbol)  and automata.symbols[symbol] != index:
   1.139  				error.config(text="State with such symbol has already existed")
   1.140 +				error.after(2000, self.clear_error)
   1.141  				state_symbol.focus()
   1.142  			elif len(symbol) != 1:
   1.143  				error.config(text="Bad symbol for state")
   1.144 +				error.after(2000, self.clear_error)
   1.145  				state_symbol.focus()
   1.146  			else:
   1.147 -				state = State(name, symbol, color, nums)
   1.148 -				del automata.symbols[automata.states[index].symbol]
   1.149 -				automata.symbols[symbol] = index
   1.150 -				del automata.st_sym[automata.states[index].symbol]
   1.151 -				automata.st_sym[symbol] = state
   1.152 -				self.keys[key] = index
   1.153 -				error.config(text="")
   1.154 +				state = State(name, symbol, color, key, nums)
   1.155 +				self.draw_changed_state(automata.states[index].symbol, symbol)
   1.156  				automata.states[index] = state
   1.157 +				self.refresh_dicts()
   1.158  				self.refresh_list()
   1.159  	
   1.160 +	def draw_changed_state(self, symbol_old, symbol_new):
   1.161 +		cells = []
   1.162 +		for row in range(automata.height):
   1.163 +			for col in range(automata.width):
   1.164 +				if automata.field[row][col] == symbol_old:
   1.165 +					automata.field[row][col] = symbol_new
   1.166 +					cells.append((row, col))
   1.167 +		
   1.168 +		self.draw_cell(cells)
   1.169 +	
   1.170  	def show_size_window(self):
   1.171  		size_window.deiconify()
   1.172  	
   1.173 @@ -397,8 +410,40 @@
   1.174  		self.selected_state = int(selected[0])
   1.175  		if len(selected):
   1.176  			self.select_item(self.selected_state)
   1.177 +	
   1.178  	def choose_color(self, event):
   1.179  		state_color.config(bg=tkColorChooser.askcolor()[1])
   1.180 +	
   1.181 +	def clear_error(self):
   1.182 +		error.config(text="")
   1.183 +	
   1.184 +	def clean_field(self):
   1.185 +		symbol = automata.states[0].symbol
   1.186 +		cells = []
   1.187 +		for row in range(automata.height):
   1.188 +			for col in range(automata.width):
   1.189 +				if automata.field[row][col] != symbol:
   1.190 +					automata.field[row][col] = symbol
   1.191 +					cells.append((row, col))
   1.192 +		self.draw_cell(cells)
   1.193 +	
   1.194 +	def fill_randomly(self):
   1.195 +		cells = []
   1.196 +		for row in range(automata.height):
   1.197 +			for col in range(automata.width):
   1.198 +				automata.field[row][col] = automata.states[random.randint(0, len(automata.states) - 1)].symbol
   1.199 +				cells.append((row, col))
   1.200 +		self.draw_cell(cells)
   1.201 +	
   1.202 +	def refresh_dicts(self):
   1.203 +		automata.symbols = {}
   1.204 +		automata.st_sym = {}
   1.205 +		self.keys = {}
   1.206 +		for index, state in enumerate(automata.states):
   1.207 +			automata.symbols[state.symbol] = index
   1.208 +			automata.st_sym[state.symbol] = state
   1.209 +			self.keys[state.key] = index
   1.210 +	
   1.211  
   1.212  
   1.213  root = Tk()
   1.214 @@ -599,8 +644,8 @@
   1.215  menu_action.add_command(label="Zoom In", command=handlers.zoom_in)
   1.216  menu_action.add_command(label="Zoom Out", command=handlers.zoom_out)
   1.217  menu_action.add_separator()
   1.218 -menu_action.add_command(label="Clean field")
   1.219 -menu_action.add_command(label="Fill randomly")
   1.220 +menu_action.add_command(label="Clean field", command=handlers.clean_field)
   1.221 +menu_action.add_command(label="Fill randomly", command=handlers.fill_randomly)
   1.222  menu_action.add_separator()
   1.223  menu_action.add_command(label="Change size",command=handlers.show_size_window)
   1.224  menubar.add_cascade(label="Action", menu=menu_action)