-
Notifications
You must be signed in to change notification settings - Fork 1
/
place.py
190 lines (159 loc) · 6.02 KB
/
place.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python
import copy
import pygtk
import gtk
import cairo
import component
import font
from math import pi
class Place(component.Component):
"""
The Place class is used to characterise a place and it inherits from the general parent class Component. Via a defined GraphicsObject the place can be drawn directly on the referenced surface.
"""
def __init__(self):
""" Constructor of Place and default values will be initialised. """
# call constructor of parent class
super(Place, self).__init__()
self._radius = 0.
self._marking = 0
self._step_size = 1
self._capacity = 0
self._font_marking = font.Font() #self._get_default_font()
def __init__(self, position = [0., 0.], radius = 0., edge = (0., 0., 0.), fill = (255., 255., 255.)):
""" Constructor of Place and default values will be initialised for the not defined parameter. """
# call constructor of parent class
super(Place, self).__init__()
self._pos = position
self._radius = radius
self._edge = edge
self._fill = fill
self._marking = 0
self._step_size = 1
self._capacity = 0
self._font_marking = font.Font() #self._get_default_font()
@property
def radius(self):
""" Return radius. """
return self._radius
@property
def marking(self):
""" Return marking. """
return self._marking
@property
def step(self):
""" Return step size. """
return self._step_size
@property
def capacity(self):
""" Return capacity. """
return self._capacity
@property
def font_marking(self):
""" Return the font of the marking (Font-Object). """
return self._font_marking
@radius.setter
def radius(self, r):
""" Set radius. """
self._radius = r
if self._radius < 0:
self._radius *= -1
@marking.setter
def marking(self, m):
""" Set marking. """
self._marking = m
if self._marking < 0:
self._marking *= -1
if self._marking > self._capacity and self._capacity > 0:
self._marking = self._capacity
@step.setter
def step(self, s):
""" Set step size. """
self._step = s
if self._step < 0:
self._step *= -1
@capacity.setter
def capacity(self, c):
""" Set capacity. """
self._capacity = c
if self._capacity < 0:
self._capacity *= -1
@font_marking.setter
def font_marking(self, font):
""" Set font for marking (Font-Object). """
self._font_marking = font
def increase_marking_steps(self, s):
""" Increase the marking by the defined step size. """
for i in range(int(s)):
self._marking += self._step_size
if self._marking > self._capacity and self._capacity > 0:
self._marking = self._capacity
def decrease_marking_steps(self, s):
""" Decrease the marking by the defined step size. """
for i in range(int(s)):
self._marking -= self._step_size
if self._marking < 0:
self._marking = 0
def increase_marking(self, c):
""" Increase the marking by the defined value c. """
self._marking += c
if self._marking > self._capacity and self._capacity > 0:
self._marking = self._capacity
def decrease_marking(self, c):
""" Decrease the marking by the defined value c. """
self._marking -= c
if self._marking < 0:
self._marking = 0
def zoom(self, factor):
""" Scale the component by a given factor. The text size will be kept as it is defined. """
# check if the factor is negative
if factor < 0:
# multiply by -1 because the factor is negative
factor *= -1
# rescale the radius
self._radius = int(self._radius * factor)
# check if the radius reached the lower limit to prevent a size zero
if self._radius < 2:
self._radius = 2
# rescale the positions
self._pos = [int(self._pos[0] * factor), int(self._pos[1] * factor)]
def draw(self, ctx):
""" Draw the place onto the defined GraphicsContext ctx. """
# check if a colour to fill the place is set
if self._fill != None:
# set the properties for filling the place
ctx.set_source_rgb(*self._fill)
ctx.fill_preserve()
# set the general place properties to draw the place
ctx.set_source_rgb(*self._edge)
# draw the place
ctx.arc(self._pos[0] + self.x_offset, self._pos[1] + self.y_offset, self._radius, 0., 2. * pi)
self._add_text(ctx, [self._pos[0] + self.x_offset, self._pos[1] + self.y_offset], self._font_marking, int(self._marking))
self._add_text(ctx, [self._pos[0] + self.x_offset, self._pos[1] + self.y_offset - self._radius - 10],
self._font_label, self._label)
ctx.stroke()
ctx.save()
def clone(self):
""" Duplication of the current place and the duplicate will be returned. """
# duplicate
comp = Place()
# general component properties
comp.key = self.key
comp.label = self.label
comp.description = self.description
comp.x_offset = self.x_offset
comp.y_offset = self.y_offset
comp.font_label = self.font_label.clone()
comp.font_description = self.font_description.clone()
comp.position = copy.deepcopy(self.position)
comp.rgb_edge = copy.deepcopy(self.rgb_edge)
comp.rgb_fill = copy.deepcopy(self.rgb_fill)
comp.count_inputs = self.count_inputs
comp.count_outputs = self.count_outputs
# place specific properties
comp.radius = self.radius
comp.marking = self.marking
comp.capacity = self.capacity
comp.step = self.step
comp.font_marking = self.font_marking
# return duplicate
return comp