summaryrefslogtreecommitdiff
path: root/src/hal/utils/halgui/design.py
blob: fe5c8f93f46bf622887ad1138e527f22de517c6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import re

from canvas import HalCanvas, ComponentList
from data import get_comp, get_pin, get_sig

class Design(gtk.Frame):
	def __init__(self, parent):
		self.app = parent
		gtk.Frame.__init__(self)

		self.file_name = ""
		self.width = 640
		self.height = 480
		self.m_frame = 5

		self.complist = []
		self.siglist = []

		sw = gtk.ScrolledWindow()

		canvas_frame = gtk.Frame(label = "HAL Editor")
		clist_frame = gtk.Frame(label = "Components")

		self.canvas = HalCanvas(self)
		canvas_frame.add(sw)
		sw.add(self.canvas)
		sw.set_shadow_type(gtk.SHADOW_IN)

		self.clist = ComponentList(self)
		clist_frame.add(self.clist)

		paned = gtk.HPaned()
		paned.set_border_width(2)
		paned.pack1(canvas_frame, True, True)
		paned.pack2(clist_frame, False, True)
		paned.set_position(800)

		self.add(paned)

	def update(self):
		dellist = self.complist[:]
		if self.file_name:
			f = open(self.file_name)
		else:
			return # This must be handled
		state = 0
		for line in f:
			#line = re.sub(r"(\d+)\s*(\(\d+\))", r"\1\2", line)
			if not line.strip():
				state = 0
			elif state == 0:
				if line.strip() == "Loaded HAL Components:":
					state = 1
				if line.strip() == "Component Pins:":
					state = 2
			elif state == 1:
				num, space, name = line.split()
				if re.match("\D", num):
					continue
				comp = get_comp(self, num)
				comp.name = name
				comp.pins_in = []
				comp.pins_out = []
				comp.redraw()
				try: dellist.remove(comp)
				except ValueError: pass
			elif state == 2:
				dlist = line.split()
				sigd, sig = None, None
				num, dtype, perm, value, name = dlist[:5]
				if len(dlist) == 7:
					sigd, sig = dlist[-2:]
				if re.match("\D", num):
					continue
				comp = get_comp(self, num)
				name = '.'.join(name.split('.')[-2:])
				pin = get_pin(comp, name, dtype, perm, value)
				if perm.find('R') >= 0:
					comp.pins_in.append(pin)
				if perm.find('W') >= 0:
					comp.pins_out.append(pin)
				if sig:
					signal = get_sig(self, sig, sigd)
					signal.addpin(pin)
				comp.redraw()
			for comp in dellist[:]:
				del comp

		self.rearrange()

	def rearrange(self):
		comps = self.complist[:]
		start = []
		for comp in comps:
			pin = None
			for pin in comp.pins_in:
				if pin.signal:
					break
			if not pin or not pin.signal:
				start.append(comp)

		cx, cy = 20, 20

		while comps:
			if start:
				comp = start.pop()
				if comp in comps:
					comps.remove(comp)
			else:
				comp = comps.pop()

			comp.x, comp.y = cx, cy
			cx += comp.widget.width + 40
			comp.redraw()

			sig = None
			for pin in comp.pins_out:
				if pin.signal:
					sig = pin.signal
					#sig.x, sig.y = cx, cy
					sig.move((cx, cy))
					cy += 40
					if sig.pinlist[0].component == comp:
						newcomp = sig.pinlist[1].component
					else:
						newcomp = sig.pinlist[0].component
					if not newcomp in start:
						start.append(newcomp)
			if sig:
				cx += 60
				cy = 20