author | Laurent Bessard |
Thu, 16 May 2013 00:31:07 +0200 | |
changeset 1152 | 0a8fbd2a00f7 |
parent 1120 | 35d772ec1a76 |
child 1166 | 2ed9675be08d |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
from time import time as gettime |
|
27 |
from math import * |
|
28 |
from types import * |
|
29 |
import datetime |
|
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
30 |
from threading import Lock,Timer |
814 | 31 |
|
32 |
#------------------------------------------------------------------------------- |
|
33 |
# Common constants |
|
34 |
#------------------------------------------------------------------------------- |
|
35 |
||
36 |
""" |
|
37 |
Definition of constants for dimensions of graphic elements |
|
38 |
""" |
|
39 |
||
40 |
# FBD and SFC constants |
|
41 |
MIN_MOVE = 5 # Minimum move before starting a element dragging |
|
42 |
CONNECTOR_SIZE = 8 # Size of connectors |
|
43 |
BLOCK_LINE_SIZE = 20 # Minimum size of each line in a block |
|
44 |
HANDLE_SIZE = 6 # Size of the squares for handles |
|
45 |
ANCHOR_DISTANCE = 5 # Distance where wire is automativally attached to a connector |
|
46 |
POINT_RADIUS = 2 # Radius of the point of wire ends |
|
47 |
MIN_SEGMENT_SIZE = 2 # Minimum size of the endling segments of a wire |
|
48 |
||
49 |
# LD constants |
|
50 |
LD_LINE_SIZE = 40 # Distance between two lines in a ladder rung |
|
51 |
LD_ELEMENT_SIZE = (21, 15) # Size (width, height) of a ladder element (contact or coil) |
|
52 |
LD_WIRE_SIZE = 30 # Size of a wire between two contact |
|
53 |
LD_WIRECOIL_SIZE = 70 # Size of a wire between a coil and a contact |
|
54 |
LD_POWERRAIL_WIDTH = 3 # Width of a Powerrail |
|
55 |
LD_OFFSET = (10, 10) # Distance (x, y) between each comment and rung of the ladder |
|
56 |
LD_COMMENT_DEFAULTSIZE = (600, 40) # Size (width, height) of a comment box |
|
57 |
||
58 |
# SFC constants |
|
59 |
SFC_STEP_DEFAULT_SIZE = (40, 30) # Default size of a SFC step |
|
60 |
SFC_TRANSITION_SIZE = (20, 2) # Size of a SFC transition |
|
61 |
SFC_DEFAULT_SEQUENCE_INTERVAL = 40 # Default size of the interval between two divergence branches |
|
62 |
SFC_SIMULTANEOUS_SEQUENCE_EXTRA = 20 # Size of extra lines for simultaneous divergence and convergence |
|
63 |
SFC_JUMP_SIZE = (12, 13) # Size of a SFC jump to step |
|
64 |
SFC_WIRE_MIN_SIZE = 25 # Size of a wire between two elements |
|
65 |
SFC_ACTION_MIN_SIZE = (100, 30) # Minimum size of an action block line |
|
66 |
||
67 |
# Type definition constants for graphic elements |
|
68 |
[INPUT, OUTPUT, INOUT] = range(3) |
|
69 |
[CONNECTOR, CONTINUATION] = range(2) |
|
70 |
[LEFTRAIL, RIGHTRAIL] = range(2) |
|
71 |
[CONTACT_NORMAL, CONTACT_REVERSE, CONTACT_RISING, CONTACT_FALLING] = range(4) |
|
72 |
[COIL_NORMAL, COIL_REVERSE, COIL_SET, COIL_RESET, COIL_RISING, COIL_FALLING] = range(6) |
|
73 |
[SELECTION_DIVERGENCE, SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE] = range(4) |
|
74 |
||
75 |
# Constants for defining the type of dragging that has been selected |
|
76 |
[HANDLE_MOVE, HANDLE_RESIZE, HANDLE_POINT, HANDLE_SEGMENT, HANDLE_CONNECTOR] = range(5) |
|
77 |
||
78 |
# List of value for resize handle that are valid |
|
79 |
VALID_HANDLES = [(1,1), (1,2), (1,3), (2,3), (3,3), (3,2), (3,1), (2,1)] |
|
80 |
||
81 |
# Contants for defining the direction of a connector |
|
82 |
[EAST, NORTH, WEST, SOUTH] = [(1,0), (0,-1), (-1,0), (0,1)] |
|
83 |
||
84 |
# Contants for defining which mode is selected for each view |
|
85 |
[MODE_SELECTION, MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, |
|
86 |
MODE_COIL, MODE_CONTACT, MODE_POWERRAIL, MODE_INITIALSTEP, MODE_STEP, |
|
87 |
MODE_TRANSITION, MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION, MODE_MOTION] = range(15) |
|
88 |
||
89 |
# Contants for defining alignment types for graphic group |
|
90 |
[ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM] = range(6) |
|
91 |
||
92 |
# Contants for defining which drawing mode is selected for app |
|
93 |
[FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2] |
|
94 |
||
95 |
# Color for Highlighting |
|
96 |
HIGHLIGHTCOLOR = wx.CYAN |
|
97 |
||
98 |
# Define highlight types |
|
99 |
ERROR_HIGHLIGHT = (wx.Colour(255, 255, 0), wx.RED) |
|
100 |
SEARCH_RESULT_HIGHLIGHT = (wx.Colour(255, 165, 0), wx.WHITE) |
|
101 |
||
102 |
# Define highlight refresh inhibition period in second |
|
103 |
REFRESH_HIGHLIGHT_PERIOD = 0.1 |
|
104 |
||
105 |
# Define tooltip wait for displaying period in second |
|
106 |
TOOLTIP_WAIT_PERIOD = 0.5 |
|
107 |
||
108 |
HANDLE_CURSORS = { |
|
109 |
(1, 1) : 2, |
|
110 |
(3, 3) : 2, |
|
111 |
(1, 3) : 3, |
|
112 |
(3, 1) : 3, |
|
113 |
(1, 2) : 4, |
|
114 |
(3, 2) : 4, |
|
115 |
(2, 1) : 5, |
|
116 |
(2, 3) : 5 |
|
117 |
} |
|
118 |
||
119 |
def round_scaling(x, n, constraint=0): |
|
120 |
fraction = float(x) / float(n) |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
121 |
if constraint == -1: |
814 | 122 |
xround = int(fraction) |
123 |
else: |
|
124 |
xround = round(fraction) |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
125 |
if constraint == 1 and xround < fraction: |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
126 |
xround += 1 |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
127 |
return int(xround * n) |
814 | 128 |
|
129 |
""" |
|
130 |
Basic vector operations for calculate wire points |
|
131 |
""" |
|
132 |
||
133 |
# Create a vector from two points and define if vector must be normal |
|
134 |
def vector(p1, p2, normal = True): |
|
135 |
vector = (p2.x - p1.x, p2.y - p1.y) |
|
136 |
if normal: |
|
137 |
return normalize(vector) |
|
138 |
return vector |
|
139 |
||
140 |
# Calculate the norm of a given vector |
|
141 |
def norm(v): |
|
142 |
return sqrt(v[0] * v[0] + v[1] * v[1]) |
|
143 |
||
144 |
# Normalize a given vector |
|
145 |
def normalize(v): |
|
146 |
v_norm = norm(v) |
|
147 |
# Verifie if it is not a null vector |
|
148 |
if v_norm > 0: |
|
149 |
return (v[0] / v_norm, v[1] / v_norm) |
|
150 |
else: |
|
151 |
return v |
|
152 |
||
153 |
# Calculate the scalar product of two vectors |
|
154 |
def is_null_vector(v): |
|
155 |
return v == (0, 0) |
|
156 |
||
157 |
# Calculate the scalar product of two vectors |
|
158 |
def add_vectors(v1, v2): |
|
159 |
return (v1[0] + v2[0], v1[1] + v2[1]) |
|
160 |
||
161 |
# Calculate the scalar product of two vectors |
|
162 |
def product(v1, v2): |
|
163 |
return v1[0] * v2[0] + v1[1] * v2[1] |
|
164 |
||
165 |
||
166 |
""" |
|
167 |
Function that calculates the nearest point of the grid defined by scaling for the given point |
|
168 |
""" |
|
169 |
||
170 |
def GetScaledEventPosition(event, dc, scaling): |
|
171 |
pos = event.GetLogicalPosition(dc) |
|
172 |
if scaling: |
|
173 |
pos.x = round(float(pos.x) / float(scaling[0])) * scaling[0] |
|
174 |
pos.y = round(float(pos.y) / float(scaling[1])) * scaling[1] |
|
175 |
return pos |
|
176 |
||
177 |
||
178 |
""" |
|
179 |
Function that choose a direction during the wire points generation |
|
180 |
""" |
|
181 |
||
182 |
def DirectionChoice(v_base, v_target, dir_target): |
|
183 |
dir_product = product(v_base, v_target) |
|
184 |
if dir_product < 0: |
|
185 |
return (-v_base[0], -v_base[1]) |
|
186 |
elif dir_product == 0 and product(v_base, dir_target) != 0: |
|
187 |
return dir_target |
|
188 |
return v_base |
|
189 |
||
190 |
SECOND = 1000000 |
|
191 |
MINUTE = 60 * SECOND |
|
192 |
HOUR = 60 * MINUTE |
|
193 |
DAY = 24 * HOUR |
|
194 |
||
195 |
def generate_time(value): |
|
196 |
microseconds = float(value.days * DAY + value.seconds * SECOND + value.microseconds) |
|
197 |
negative = microseconds < 0 |
|
198 |
microseconds = abs(microseconds) |
|
199 |
data = "T#" |
|
200 |
not_null = False |
|
201 |
if negative: |
|
202 |
data += "-" |
|
203 |
for val, format in [(int(microseconds) / DAY, "%dd"), |
|
204 |
((int(microseconds) % DAY) / HOUR, "%dh"), |
|
205 |
((int(microseconds) % HOUR) / MINUTE, "%dm"), |
|
206 |
((int(microseconds) % MINUTE) / SECOND, "%ds")]: |
|
207 |
if val > 0 or not_null: |
|
208 |
data += format % val |
|
209 |
not_null = True |
|
210 |
data += "%gms" % (microseconds % SECOND / 1000.) |
|
211 |
return data |
|
212 |
||
213 |
def generate_date(value): |
|
214 |
base_date = datetime.datetime(1970, 1, 1) |
|
215 |
date = base_date + value |
|
216 |
return date.strftime("DATE#%Y-%m-%d") |
|
217 |
||
218 |
def generate_datetime(value): |
|
219 |
base_date = datetime.datetime(1970, 1, 1) |
|
220 |
date_time = base_date + value |
|
221 |
return date_time.strftime("DT#%Y-%m-%d-%H:%M:%S.%f") |
|
222 |
||
223 |
def generate_timeofday(value): |
|
224 |
microseconds = float(value.days * DAY + value.seconds * SECOND + value.microseconds) |
|
225 |
negative = microseconds < 0 |
|
226 |
microseconds = abs(microseconds) |
|
227 |
data = "TOD#" |
|
228 |
for val, format in [(int(microseconds) / HOUR, "%2.2d:"), |
|
229 |
((int(microseconds) % HOUR) / MINUTE, "%2.2d:"), |
|
230 |
((int(microseconds) % MINUTE) / SECOND, "%2.2d."), |
|
231 |
(microseconds % SECOND, "%6.6d")]: |
|
232 |
data += format % val |
|
233 |
return data |
|
234 |
||
235 |
TYPE_TRANSLATOR = {"TIME": generate_time, |
|
236 |
"DATE": generate_date, |
|
237 |
"DT": generate_datetime, |
|
238 |
"TOD": generate_timeofday} |
|
239 |
||
240 |
def MiterPen(colour, width=1, style=wx.SOLID): |
|
241 |
pen = wx.Pen(colour, width, style) |
|
242 |
pen.SetJoin(wx.JOIN_MITER) |
|
243 |
pen.SetCap(wx.CAP_PROJECTING) |
|
244 |
return pen |
|
245 |
||
246 |
#------------------------------------------------------------------------------- |
|
247 |
# Debug Data Consumer Class |
|
248 |
#------------------------------------------------------------------------------- |
|
249 |
||
250 |
class DebugDataConsumer: |
|
251 |
||
252 |
def __init__(self): |
|
253 |
self.LastValue = None |
|
254 |
self.Value = None |
|
255 |
self.DataType = None |
|
256 |
self.LastForced = False |
|
257 |
self.Forced = False |
|
258 |
self.Inhibited = False |
|
259 |
||
260 |
def Inhibit(self, inhibit): |
|
261 |
self.Inhibited = inhibit |
|
262 |
if not inhibit and self.LastValue is not None: |
|
263 |
self.SetForced(self.LastForced) |
|
264 |
self.SetValue(self.LastValue) |
|
265 |
self.LastValue = None |
|
266 |
||
267 |
def SetDataType(self, data_type): |
|
268 |
self.DataType = data_type |
|
269 |
||
270 |
def NewValue(self, tick, value, forced=False): |
|
271 |
value = TYPE_TRANSLATOR.get(self.DataType, lambda x:x)(value) |
|
272 |
if self.Inhibited: |
|
273 |
self.LastValue = value |
|
274 |
self.LastForced = forced |
|
275 |
else: |
|
276 |
self.SetForced(forced) |
|
277 |
self.SetValue(value) |
|
278 |
||
279 |
def SetValue(self, value): |
|
280 |
self.Value = value |
|
281 |
||
282 |
def GetValue(self): |
|
283 |
return self.Value |
|
284 |
||
285 |
def SetForced(self, forced): |
|
286 |
self.Forced = forced |
|
287 |
||
288 |
def IsForced(self): |
|
289 |
return self.Forced |
|
290 |
||
291 |
#------------------------------------------------------------------------------- |
|
292 |
# Debug Viewer Class |
|
293 |
#------------------------------------------------------------------------------- |
|
294 |
||
295 |
REFRESH_PERIOD = 0.1 |
|
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
296 |
DEBUG_REFRESH_LOCK = Lock() |
814 | 297 |
|
298 |
class DebugViewer: |
|
299 |
||
300 |
def __init__(self, producer, debug, register_tick=True): |
|
301 |
self.DataProducer = None |
|
302 |
self.Debug = debug |
|
303 |
self.RegisterTick = register_tick |
|
304 |
self.Inhibited = False |
|
305 |
||
306 |
self.DataConsumers = {} |
|
307 |
||
308 |
self.LastRefreshTime = gettime() |
|
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
309 |
self.HasAcquiredLock = False |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
310 |
self.AccessLock = Lock() |
877
7e695249be8d
Fix bug debug viewers are block when a Graphic Viewer in debug mode is hidden
Laurent Bessard
parents:
875
diff
changeset
|
311 |
self.TimerAccessLock = Lock() |
814 | 312 |
|
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
313 |
self.LastRefreshTimer = None |
814 | 314 |
|
315 |
self.SetDataProducer(producer) |
|
316 |
||
317 |
def __del__(self): |
|
318 |
self.DataProducer = None |
|
319 |
self.DeleteDataConsumers() |
|
877
7e695249be8d
Fix bug debug viewers are block when a Graphic Viewer in debug mode is hidden
Laurent Bessard
parents:
875
diff
changeset
|
320 |
if self.LastRefreshTimer is not None: |
7e695249be8d
Fix bug debug viewers are block when a Graphic Viewer in debug mode is hidden
Laurent Bessard
parents:
875
diff
changeset
|
321 |
self.LastRefreshTimer.Stop() |
887
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
322 |
if self.HasAcquiredLock: |
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
323 |
DEBUG_REFRESH_LOCK.release() |
814 | 324 |
|
325 |
def SetDataProducer(self, producer): |
|
326 |
if self.RegisterTick and self.Debug: |
|
327 |
if producer is not None: |
|
328 |
producer.SubscribeDebugIECVariable("__tick__", self) |
|
329 |
if self.DataProducer is not None: |
|
1089
5cd1f8df71aa
Fixed bug when transferring program and a output located variable is forced
Laurent Bessard
parents:
1069
diff
changeset
|
330 |
self.DataProducer.UnsubscribeDebugIECVariable("__tick__", self) |
814 | 331 |
self.DataProducer = producer |
332 |
||
333 |
def IsDebugging(self): |
|
334 |
return self.Debug |
|
335 |
||
336 |
def Inhibit(self, inhibit): |
|
337 |
for consumer, iec_path in self.DataConsumers.iteritems(): |
|
338 |
consumer.Inhibit(inhibit) |
|
339 |
self.Inhibited = inhibit |
|
340 |
||
341 |
def AddDataConsumer(self, iec_path, consumer): |
|
342 |
if self.DataProducer is None: |
|
343 |
return None |
|
344 |
result = self.DataProducer.SubscribeDebugIECVariable(iec_path, consumer) |
|
345 |
if result is not None and consumer != self: |
|
346 |
self.DataConsumers[consumer] = iec_path |
|
347 |
consumer.SetDataType(self.GetDataType(iec_path)) |
|
348 |
return result |
|
349 |
||
350 |
def RemoveDataConsumer(self, consumer): |
|
351 |
iec_path = self.DataConsumers.pop(consumer, None) |
|
352 |
if iec_path is not None: |
|
353 |
self.DataProducer.UnsubscribeDebugIECVariable(iec_path, consumer) |
|
354 |
||
897
3cd39bc7dbad
Added support for defining extension file editor as DebugViewer
Laurent Bessard
parents:
887
diff
changeset
|
355 |
def RegisterVariables(self): |
1089
5cd1f8df71aa
Fixed bug when transferring program and a output located variable is forced
Laurent Bessard
parents:
1069
diff
changeset
|
356 |
if self.RegisterTick and self.Debug and self.DataProducer is not None: |
5cd1f8df71aa
Fixed bug when transferring program and a output located variable is forced
Laurent Bessard
parents:
1069
diff
changeset
|
357 |
self.DataProducer.SubscribeDebugIECVariable("__tick__", self) |
897
3cd39bc7dbad
Added support for defining extension file editor as DebugViewer
Laurent Bessard
parents:
887
diff
changeset
|
358 |
|
814 | 359 |
def GetDataType(self, iec_path): |
360 |
if self.DataProducer is not None: |
|
1102 | 361 |
data_type = self.DataProducer.GetDebugIECVariableType(iec_path.upper()) |
362 |
if data_type is not None: |
|
363 |
return data_type |
|
364 |
||
887
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
365 |
infos = self.DataProducer.GetInstanceInfos(iec_path) |
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
366 |
if infos is not None: |
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
367 |
return infos["type"] |
814 | 368 |
return None |
369 |
||
887
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
370 |
def IsNumType(self, data_type): |
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
371 |
return self.DataProducer.IsNumType(data_type) |
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
372 |
|
814 | 373 |
def ForceDataValue(self, iec_path, value): |
374 |
if self.DataProducer is not None: |
|
375 |
self.DataProducer.ForceDebugIECVariable(iec_path, value) |
|
376 |
||
377 |
def ReleaseDataValue(self, iec_path): |
|
378 |
if self.DataProducer is not None: |
|
379 |
self.DataProducer.ReleaseDebugIECVariable(iec_path) |
|
380 |
||
381 |
def DeleteDataConsumers(self): |
|
382 |
if self.DataProducer is not None: |
|
383 |
for consumer, iec_path in self.DataConsumers.iteritems(): |
|
384 |
self.DataProducer.UnsubscribeDebugIECVariable(iec_path, consumer) |
|
385 |
self.DataConsumers = {} |
|
386 |
||
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
387 |
def ShouldRefresh(self): |
942
2ba9d7e3be72
Fixed bug with LastRefreshTimer when closing debug viewer
Laurent Bessard
parents:
933
diff
changeset
|
388 |
if self: |
2ba9d7e3be72
Fixed bug with LastRefreshTimer when closing debug viewer
Laurent Bessard
parents:
933
diff
changeset
|
389 |
wx.CallAfter(self._ShouldRefresh) |
880 | 390 |
|
391 |
def _ShouldRefresh(self): |
|
945
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
392 |
if self: |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
393 |
if DEBUG_REFRESH_LOCK.acquire(False): |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
394 |
self.AccessLock.acquire() |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
395 |
self.HasAcquiredLock = True |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
396 |
self.AccessLock.release() |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
397 |
self.RefreshNewData() |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
398 |
else: |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
399 |
self.TimerAccessLock.acquire() |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
400 |
self.LastRefreshTimer = Timer(REFRESH_PERIOD, self.ShouldRefresh) |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
401 |
self.LastRefreshTimer.start() |
c1159acb0886
Added support for drag'n dropping non-numeric variables
Laurent Bessard
parents:
942
diff
changeset
|
402 |
self.TimerAccessLock.release() |
814 | 403 |
|
902
ffa8ee5ee2fe
Adding support for defining a time range for DebugVariablePanel graphics and navigating across the recording.
Laurent Bessard
parents:
897
diff
changeset
|
404 |
def NewDataAvailable(self, tick, *args, **kwargs): |
877
7e695249be8d
Fix bug debug viewers are block when a Graphic Viewer in debug mode is hidden
Laurent Bessard
parents:
875
diff
changeset
|
405 |
self.TimerAccessLock.acquire() |
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
406 |
if self.LastRefreshTimer is not None: |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
407 |
self.LastRefreshTimer.cancel() |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
408 |
self.LastRefreshTimer=None |
877
7e695249be8d
Fix bug debug viewers are block when a Graphic Viewer in debug mode is hidden
Laurent Bessard
parents:
875
diff
changeset
|
409 |
self.TimerAccessLock.release() |
887
d3c6c4ab8b28
Adding support for displaying graphs of debugged numeric variables in 2D and 3D in DebugVariablePanel
Laurent Bessard
parents:
880
diff
changeset
|
410 |
if self.IsShown() and not self.Inhibited: |
904
73f6333d50a4
Fix bug debug slow interface when refresh time of a DebugViewer is longer than debug refresh period
Laurent Bessard
parents:
902
diff
changeset
|
411 |
if gettime() - self.LastRefreshTime > REFRESH_PERIOD and DEBUG_REFRESH_LOCK.acquire(False): |
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
412 |
self.AccessLock.acquire() |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
413 |
self.HasAcquiredLock = True |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
414 |
self.AccessLock.release() |
814 | 415 |
self.LastRefreshTime = gettime() |
416 |
self.Inhibit(True) |
|
417 |
wx.CallAfter(self.RefreshViewOnNewData, *args, **kwargs) |
|
933 | 418 |
else: |
419 |
self.TimerAccessLock.acquire() |
|
420 |
self.LastRefreshTimer = Timer(REFRESH_PERIOD, self.ShouldRefresh) |
|
421 |
self.LastRefreshTimer.start() |
|
422 |
self.TimerAccessLock.release() |
|
877
7e695249be8d
Fix bug debug viewers are block when a Graphic Viewer in debug mode is hidden
Laurent Bessard
parents:
875
diff
changeset
|
423 |
elif not self.IsShown() and self.HasAcquiredLock: |
7e695249be8d
Fix bug debug viewers are block when a Graphic Viewer in debug mode is hidden
Laurent Bessard
parents:
875
diff
changeset
|
424 |
DebugViewer.RefreshNewData(self) |
814 | 425 |
|
426 |
def RefreshViewOnNewData(self, *args, **kwargs): |
|
427 |
if self: |
|
428 |
self.RefreshNewData(*args, **kwargs) |
|
429 |
||
430 |
def RefreshNewData(self, *args, **kwargs): |
|
431 |
self.Inhibit(False) |
|
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
432 |
self.AccessLock.acquire() |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
433 |
if self.HasAcquiredLock: |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
434 |
DEBUG_REFRESH_LOCK.release() |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
435 |
self.HasAcquiredLock = False |
904
73f6333d50a4
Fix bug debug slow interface when refresh time of a DebugViewer is longer than debug refresh period
Laurent Bessard
parents:
902
diff
changeset
|
436 |
if gettime() - self.LastRefreshTime > REFRESH_PERIOD: |
73f6333d50a4
Fix bug debug slow interface when refresh time of a DebugViewer is longer than debug refresh period
Laurent Bessard
parents:
902
diff
changeset
|
437 |
self.LastRefreshTime = gettime() |
875
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
438 |
self.AccessLock.release() |
a8952b79caec
Fix bug in Debug refresh lock that, with too much data to debug, flooded GUI and blocked it
Laurent Bessard
parents:
857
diff
changeset
|
439 |
|
814 | 440 |
#------------------------------------------------------------------------------- |
441 |
# Viewer Rubberband |
|
442 |
#------------------------------------------------------------------------------- |
|
443 |
||
444 |
""" |
|
445 |
Class that implements a rubberband |
|
446 |
""" |
|
447 |
||
448 |
class RubberBand: |
|
449 |
||
450 |
# Create a rubberband by indicated on which window it must be drawn |
|
451 |
def __init__(self, viewer): |
|
452 |
self.Viewer = viewer |
|
453 |
self.drawingSurface = viewer.Editor |
|
454 |
self.Reset() |
|
455 |
||
456 |
# Method that initializes the internal attributes of the rubberband |
|
457 |
def Reset(self): |
|
458 |
self.startPoint = None |
|
459 |
self.currentBox = None |
|
460 |
self.lastBox = None |
|
461 |
||
462 |
# Method that return if a box is currently edited |
|
463 |
def IsShown(self): |
|
464 |
return self.currentBox != None |
|
465 |
||
466 |
# Method that returns the currently edited box |
|
467 |
def GetCurrentExtent(self): |
|
468 |
if self.currentBox is None: |
|
469 |
return self.lastBox |
|
470 |
return self.currentBox |
|
471 |
||
472 |
# Method called when a new box starts to be edited |
|
473 |
def OnLeftDown(self, event, dc, scaling): |
|
474 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
475 |
# Save the point for calculate the box position and size |
|
476 |
self.startPoint = pos |
|
477 |
self.currentBox = wx.Rect(pos.x, pos.y, 0, 0) |
|
478 |
self.drawingSurface.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) |
|
479 |
self.Redraw() |
|
480 |
||
481 |
# Method called when dragging with a box edited |
|
482 |
def OnMotion(self, event, dc, scaling): |
|
483 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
484 |
# Save the last position and size of the box for erasing it |
|
485 |
self.lastBox = wx.Rect(self.currentBox.x, self.currentBox.y, self.currentBox.width, |
|
486 |
self.currentBox.height) |
|
487 |
# Calculate new position and size of the box |
|
488 |
if pos.x >= self.startPoint.x: |
|
489 |
self.currentBox.x = self.startPoint.x |
|
490 |
self.currentBox.width = pos.x - self.startPoint.x + 1 |
|
491 |
else: |
|
492 |
self.currentBox.x = pos.x |
|
493 |
self.currentBox.width = self.startPoint.x - pos.x + 1 |
|
494 |
if pos.y >= self.startPoint.y: |
|
495 |
self.currentBox.y = self.startPoint.y |
|
496 |
self.currentBox.height = pos.y - self.startPoint.y + 1 |
|
497 |
else: |
|
498 |
self.currentBox.y = pos.y |
|
499 |
self.currentBox.height = self.startPoint.y - pos.y + 1 |
|
500 |
self.Redraw() |
|
501 |
||
502 |
# Method called when dragging is stopped |
|
503 |
def OnLeftUp(self, event, dc, scaling): |
|
504 |
self.drawingSurface.SetCursor(wx.NullCursor) |
|
505 |
self.lastBox = self.currentBox |
|
506 |
self.currentBox = None |
|
507 |
self.Redraw() |
|
508 |
||
509 |
# Method that erase the last box and draw the new box |
|
510 |
def Redraw(self, dc = None): |
|
511 |
if dc is None: |
|
512 |
dc = self.Viewer.GetLogicalDC() |
|
513 |
scalex, scaley = dc.GetUserScale() |
|
514 |
dc.SetUserScale(1, 1) |
|
515 |
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) |
|
516 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
|
517 |
dc.SetLogicalFunction(wx.XOR) |
|
518 |
if self.lastBox: |
|
519 |
# Erase last box |
|
520 |
dc.DrawRectangle(self.lastBox.x * scalex, self.lastBox.y * scaley, |
|
521 |
self.lastBox.width * scalex, self.lastBox.height * scaley) |
|
522 |
if self.currentBox: |
|
523 |
# Draw current box |
|
524 |
dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, |
|
525 |
self.currentBox.width * scalex, self.currentBox.height * scaley) |
|
526 |
dc.SetUserScale(scalex, scaley) |
|
527 |
||
528 |
# Erase last box |
|
529 |
def Erase(self, dc = None): |
|
530 |
if dc is None: |
|
531 |
dc = self.Viewer.GetLogicalDC() |
|
532 |
scalex, scaley = dc.GetUserScale() |
|
533 |
dc.SetUserScale(1, 1) |
|
534 |
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) |
|
535 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
|
536 |
dc.SetLogicalFunction(wx.XOR) |
|
537 |
if self.lastBox: |
|
538 |
dc.DrawRectangle(self.lastBox.x * scalex, self.lastBox.y * scaley, |
|
539 |
self.lastBox.width * scalex, self.lastBox.height * scalex) |
|
540 |
dc.SetUserScale(scalex, scaley) |
|
541 |
||
542 |
# Draw current box |
|
543 |
def Draw(self, dc = None): |
|
544 |
if dc is None: |
|
545 |
dc = self.Viewer.GetLogicalDC() |
|
546 |
scalex, scaley = dc.GetUserScale() |
|
547 |
dc.SetUserScale(1, 1) |
|
548 |
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) |
|
549 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
|
550 |
dc.SetLogicalFunction(wx.XOR) |
|
551 |
if self.currentBox: |
|
552 |
# Draw current box |
|
553 |
dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, |
|
554 |
self.currentBox.width * scalex, self.currentBox.height * scaley) |
|
555 |
dc.SetUserScale(scalex, scaley) |
|
556 |
||
557 |
#------------------------------------------------------------------------------- |
|
558 |
# Viewer ToolTip |
|
559 |
#------------------------------------------------------------------------------- |
|
560 |
||
561 |
""" |
|
562 |
Class that implements a custom tool tip |
|
563 |
""" |
|
564 |
||
565 |
if wx.Platform == '__WXMSW__': |
|
566 |
faces = { 'times': 'Times New Roman', |
|
567 |
'mono' : 'Courier New', |
|
568 |
'helv' : 'Arial', |
|
569 |
'other': 'Comic Sans MS', |
|
570 |
'size' : 10, |
|
571 |
} |
|
572 |
else: |
|
573 |
faces = { 'times': 'Times', |
|
574 |
'mono' : 'Courier', |
|
575 |
'helv' : 'Helvetica', |
|
576 |
'other': 'new century schoolbook', |
|
577 |
'size' : 12, |
|
578 |
} |
|
579 |
||
580 |
TOOLTIP_MAX_CHARACTERS = 30 |
|
581 |
TOOLTIP_MAX_LINE = 5 |
|
582 |
||
583 |
class ToolTip(wx.PopupWindow): |
|
584 |
||
993 | 585 |
def __init__(self, parent, tip, restricted=True): |
814 | 586 |
wx.PopupWindow.__init__(self, parent) |
587 |
||
588 |
self.CurrentPosition = wx.Point(0, 0) |
|
993 | 589 |
self.Restricted = restricted |
814 | 590 |
|
591 |
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) |
|
592 |
self.SetTip(tip) |
|
593 |
||
993 | 594 |
self.Font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"]) |
595 |
||
814 | 596 |
self.Bind(wx.EVT_PAINT, self.OnPaint) |
993 | 597 |
|
598 |
def SetFont(self, font): |
|
599 |
self.Font = font |
|
600 |
self.RefreshTip() |
|
601 |
||
814 | 602 |
def SetTip(self, tip): |
603 |
lines = [] |
|
604 |
for line in tip.splitlines(): |
|
993 | 605 |
if self.Restricted and line != "": |
814 | 606 |
words = line.split() |
607 |
new_line = words[0] |
|
608 |
for word in words[1:]: |
|
609 |
if len(new_line + " " + word) <= TOOLTIP_MAX_CHARACTERS: |
|
610 |
new_line += " " + word |
|
611 |
else: |
|
612 |
lines.append(new_line) |
|
613 |
new_line = word |
|
614 |
lines.append(new_line) |
|
615 |
else: |
|
616 |
lines.append(line) |
|
993 | 617 |
if self.Restricted and len(lines) > TOOLTIP_MAX_LINE: |
814 | 618 |
self.Tip = lines[:TOOLTIP_MAX_LINE] |
619 |
if len(self.Tip[-1]) < TOOLTIP_MAX_CHARACTERS - 3: |
|
620 |
self.Tip[-1] += "..." |
|
621 |
else: |
|
622 |
self.Tip[-1] = self.Tip[-1][:TOOLTIP_MAX_CHARACTERS - 3] + "..." |
|
623 |
else: |
|
624 |
self.Tip = lines |
|
625 |
wx.CallAfter(self.RefreshTip) |
|
626 |
||
627 |
def MoveToolTip(self, pos): |
|
993 | 628 |
screen_size = wx.GetDisplaySize() |
629 |
w, h = self.GetTipExtent() |
|
630 |
self.CurrentPosition = wx.Point( |
|
631 |
max(0, min(pos.x, screen_size[0] - w - 4)), |
|
632 |
max(0, min(pos.y, screen_size[1] - h - 4))) |
|
814 | 633 |
self.SetPosition(pos) |
634 |
||
635 |
def GetTipExtent(self): |
|
636 |
max_width = 0 |
|
637 |
max_height = 0 |
|
638 |
for line in self.Tip: |
|
993 | 639 |
dc = wx.MemoryDC() |
640 |
dc.SetFont(self.Font) |
|
641 |
w, h = dc.GetTextExtent(line) |
|
814 | 642 |
max_width = max(max_width, w) |
643 |
max_height += h |
|
644 |
return max_width, max_height |
|
645 |
||
646 |
def RefreshTip(self): |
|
647 |
if self: |
|
648 |
w, h = self.GetTipExtent() |
|
649 |
self.SetSize(wx.Size(w + 4, h + 4)) |
|
650 |
self.SetPosition(self.CurrentPosition) |
|
651 |
self.Refresh() |
|
652 |
||
653 |
def OnPaint(self, event): |
|
654 |
dc = wx.AutoBufferedPaintDC(self) |
|
655 |
dc.Clear() |
|
656 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
657 |
dc.SetBrush(wx.Brush(wx.Colour(255, 238, 170))) |
|
993 | 658 |
dc.SetFont(self.Font) |
814 | 659 |
dc.BeginDrawing() |
660 |
w, h = self.GetTipExtent() |
|
661 |
dc.DrawRectangle(0, 0, w + 4, h + 4) |
|
662 |
offset = 0 |
|
663 |
for line in self.Tip: |
|
664 |
dc.DrawText(line, 2, offset + 2) |
|
665 |
w, h = dc.GetTextExtent(line) |
|
666 |
offset += h |
|
667 |
dc.EndDrawing() |
|
668 |
event.Skip() |
|
669 |
||
670 |
#------------------------------------------------------------------------------- |
|
671 |
# Helpers for highlighting text |
|
672 |
#------------------------------------------------------------------------------- |
|
673 |
||
674 |
def AddHighlight(highlights, infos): |
|
675 |
RemoveHighlight(highlights, infos) |
|
676 |
highlights.append(infos) |
|
677 |
||
678 |
def RemoveHighlight(highlights, infos): |
|
679 |
if infos in highlights: |
|
680 |
highlights.remove(infos) |
|
681 |
return True |
|
682 |
return False |
|
683 |
||
684 |
def ClearHighlight(highlights, highlight_type=None): |
|
685 |
if highlight_type is not None: |
|
686 |
return [highlight for highlight in highlights if highlight[2] != highlight_type] |
|
687 |
return [] |
|
688 |
||
689 |
def DrawHighlightedText(dc, text, highlights, x, y): |
|
690 |
current_pen = dc.GetPen() |
|
691 |
dc.SetPen(wx.TRANSPARENT_PEN) |
|
692 |
for start, end, highlight_type in highlights: |
|
693 |
dc.SetBrush(wx.Brush(highlight_type[0])) |
|
694 |
offset_width, offset_height = dc.GetTextExtent(text[:start[1]]) |
|
695 |
part = text[start[1]:end[1] + 1] |
|
696 |
part_width, part_height = dc.GetTextExtent(part) |
|
697 |
dc.DrawRectangle(x + offset_width, y, part_width, part_height) |
|
698 |
dc.SetTextForeground(highlight_type[1]) |
|
699 |
dc.DrawText(part, x + offset_width, y) |
|
700 |
dc.SetPen(current_pen) |
|
701 |
dc.SetTextForeground(wx.BLACK) |
|
702 |
||
703 |
#------------------------------------------------------------------------------- |
|
704 |
# Graphic element base class |
|
705 |
#------------------------------------------------------------------------------- |
|
706 |
||
707 |
""" |
|
708 |
Class that implements a generic graphic element |
|
709 |
""" |
|
710 |
||
711 |
class Graphic_Element: |
|
712 |
||
713 |
# Create a new graphic element |
|
714 |
def __init__(self, parent, id = None): |
|
715 |
self.Parent = parent |
|
716 |
self.Id = id |
|
717 |
self.oldPos = None |
|
718 |
self.StartPos = None |
|
719 |
self.CurrentDrag = None |
|
720 |
self.Handle = (None,None) |
|
721 |
self.Dragging = False |
|
722 |
self.Selected = False |
|
723 |
self.Highlighted = False |
|
724 |
self.Pos = wx.Point(0, 0) |
|
725 |
self.Size = wx.Size(0, 0) |
|
726 |
self.BoundingBox = wx.Rect(0, 0, 0, 0) |
|
727 |
self.Visible = False |
|
728 |
self.ToolTip = None |
|
729 |
self.ToolTipPos = None |
|
730 |
self.ToolTipTimer = wx.Timer(self.Parent, -1) |
|
731 |
self.Parent.Bind(wx.EVT_TIMER, self.OnToolTipTimer, self.ToolTipTimer) |
|
732 |
||
733 |
def __del__(self): |
|
734 |
self.ToolTipTimer.Stop() |
|
735 |
||
736 |
def GetDefinition(self): |
|
737 |
return [self.Id], [] |
|
738 |
||
739 |
def TestVisible(self, screen): |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
740 |
self.Visible = self.Selected or self.GetRedrawRect().Intersects(screen) |
814 | 741 |
|
742 |
def IsVisible(self): |
|
743 |
return self.Visible |
|
744 |
||
745 |
def SpreadCurrent(self): |
|
746 |
pass |
|
747 |
||
748 |
def GetConnectorTranslation(self, element): |
|
749 |
return {} |
|
750 |
||
751 |
def FindNearestConnector(self, position, connectors): |
|
752 |
distances = [] |
|
753 |
for connector in connectors: |
|
754 |
connector_pos = connector.GetRelPosition() |
|
755 |
distances.append((sqrt((self.Pos.x + connector_pos.x - position.x) ** 2 + |
|
756 |
(self.Pos.y + connector_pos.y - position.y) ** 2), |
|
757 |
connector)) |
|
758 |
distances.sort() |
|
759 |
if len(distances) > 0: |
|
760 |
return distances[0][1] |
|
761 |
return None |
|
762 |
||
763 |
def IsOfType(self, type, reference): |
|
764 |
return self.Parent.IsOfType(type, reference) |
|
765 |
||
766 |
def IsEndType(self, type): |
|
767 |
return self.Parent.IsEndType(type) |
|
768 |
||
769 |
def GetDragging(self): |
|
770 |
return self.Dragging |
|
771 |
||
772 |
# Make a clone of this element |
|
773 |
def Clone(self, parent): |
|
774 |
return Graphic_Element(parent, self.Id) |
|
775 |
||
776 |
# Changes the block position |
|
777 |
def SetPosition(self, x, y): |
|
778 |
self.Pos.x = x |
|
779 |
self.Pos.y = y |
|
780 |
self.RefreshConnected() |
|
781 |
self.RefreshBoundingBox() |
|
782 |
||
783 |
# Returns the block position |
|
784 |
def GetPosition(self): |
|
785 |
return self.Pos.x, self.Pos.y |
|
786 |
||
787 |
# Changes the element size |
|
788 |
def SetSize(self, width, height): |
|
789 |
self.Size.SetWidth(width) |
|
790 |
self.Size.SetHeight(height) |
|
791 |
self.RefreshConnectors() |
|
792 |
self.RefreshBoundingBox() |
|
793 |
||
794 |
# Returns the element size |
|
795 |
def GetSize(self): |
|
796 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
797 |
||
798 |
# Returns the minimum element size |
|
799 |
def GetMinSize(self): |
|
800 |
return 0, 0 |
|
801 |
||
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
802 |
# Set size of the element to the minimum size |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
803 |
def SetBestSize(self, scaling, x_factor=0.5, y_factor=0.5): |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
804 |
width, height = self.GetSize() |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
805 |
posx, posy = self.GetPosition() |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
806 |
min_width, min_height = self.GetMinSize() |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
807 |
if width < min_width: |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
808 |
self.Pos.x = max(0, self.Pos.x - (width - min_width) * x_factor) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
809 |
width = min_width |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
810 |
if height < min_height: |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
811 |
self.Pos.y = max(0, self.Pos.y - (height - min_height) * y_factor) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
812 |
height = min_height |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
813 |
if scaling is not None: |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
814 |
self.Pos.x = round_scaling(self.Pos.x, scaling[0]) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
815 |
self.Pos.y = round_scaling(self.Pos.y, scaling[1]) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
816 |
width = round_scaling(width, scaling[0], 1) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
817 |
height = round_scaling(height, scaling[1], 1) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
818 |
self.SetSize(width, height) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
819 |
return self.Pos.x - posx, self.Pos.y - posy |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
820 |
|
814 | 821 |
# Refresh the element Bounding Box |
822 |
def RefreshBoundingBox(self): |
|
823 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) |
|
824 |
||
825 |
# Refresh the element connectors position |
|
826 |
def RefreshConnectors(self): |
|
827 |
pass |
|
828 |
||
829 |
# Refresh the position of wires connected to element inputs and outputs |
|
830 |
def RefreshConnected(self): |
|
831 |
pass |
|
832 |
||
833 |
# Change the parent |
|
834 |
def SetParent(self, parent): |
|
835 |
self.Parent = parent |
|
836 |
||
837 |
# Override this method for defining the method to call for deleting this element |
|
838 |
def Delete(self): |
|
839 |
pass |
|
840 |
||
841 |
# Returns the Id |
|
842 |
def GetId(self): |
|
843 |
return self.Id |
|
844 |
||
845 |
# Returns if the point given is in the bounding box |
|
846 |
def HitTest(self, pt, connectors=True): |
|
847 |
if connectors: |
|
848 |
rect = self.BoundingBox |
|
849 |
else: |
|
850 |
rect = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) |
|
851 |
return rect.InsideXY(pt.x, pt.y) |
|
852 |
||
853 |
# Returns if the point given is in the bounding box |
|
854 |
def IsInSelection(self, rect): |
|
855 |
return rect.InsideXY(self.BoundingBox.x, self.BoundingBox.y) and rect.InsideXY(self.BoundingBox.x + self.BoundingBox.width, self.BoundingBox.y + self.BoundingBox.height) |
|
856 |
||
857 |
# Override this method for refreshing the bounding box |
|
858 |
def RefreshBoundingBox(self): |
|
859 |
pass |
|
860 |
||
861 |
# Returns the bounding box |
|
862 |
def GetBoundingBox(self): |
|
863 |
return self.BoundingBox |
|
864 |
||
865 |
# Returns the RedrawRect |
|
866 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
867 |
scalex, scaley = self.Parent.GetViewScale() |
|
868 |
rect = wx.Rect() |
|
869 |
rect.x = self.BoundingBox.x - int(HANDLE_SIZE / scalex) - 3 - abs(movex) |
|
870 |
rect.y = self.BoundingBox.y - int(HANDLE_SIZE / scaley) - 3 - abs(movey) |
|
871 |
rect.width = self.BoundingBox.width + 2 * (int(HANDLE_SIZE / scalex) + abs(movex) + 1) + 4 |
|
872 |
rect.height = self.BoundingBox.height + 2 * (int(HANDLE_SIZE / scaley) + abs(movey) + 1) + 4 |
|
873 |
return rect |
|
874 |
||
875 |
def Refresh(self, rect = None): |
|
876 |
if self.Visible: |
|
877 |
if rect is not None: |
|
878 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) |
|
879 |
else: |
|
880 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False) |
|
881 |
||
882 |
# Change the variable that indicates if this element is selected |
|
883 |
def SetSelected(self, selected): |
|
884 |
self.Selected = selected |
|
885 |
self.Refresh() |
|
886 |
||
887 |
# Change the variable that indicates if this element is highlighted |
|
888 |
def SetHighlighted(self, highlighted): |
|
889 |
self.Highlighted = highlighted |
|
890 |
self.Refresh() |
|
891 |
||
892 |
# Test if the point is on a handle of this element |
|
893 |
def TestHandle(self, event): |
|
894 |
dc = self.Parent.GetLogicalDC() |
|
895 |
scalex, scaley = dc.GetUserScale() |
|
896 |
pos = event.GetPosition() |
|
897 |
pt = wx.Point(*self.Parent.CalcUnscrolledPosition(pos.x, pos.y)) |
|
898 |
||
899 |
left = (self.BoundingBox.x - 2) * scalex - HANDLE_SIZE |
|
900 |
center = (self.BoundingBox.x + self.BoundingBox.width / 2) * scalex - HANDLE_SIZE / 2 |
|
901 |
right = (self.BoundingBox.x + self.BoundingBox.width + 2) * scalex |
|
902 |
||
903 |
top = (self.BoundingBox.y - 2) * scaley - HANDLE_SIZE |
|
904 |
middle = (self.BoundingBox.y + self.BoundingBox.height / 2) * scaley - HANDLE_SIZE / 2 |
|
905 |
bottom = (self.BoundingBox.y + self.BoundingBox.height + 2) * scaley |
|
906 |
||
907 |
extern_rect = wx.Rect(left, top, right + HANDLE_SIZE - left, bottom + HANDLE_SIZE - top) |
|
908 |
intern_rect = wx.Rect(left + HANDLE_SIZE, top + HANDLE_SIZE, right - left - HANDLE_SIZE, bottom - top - HANDLE_SIZE) |
|
909 |
||
910 |
# Verify that this element is selected |
|
911 |
if self.Selected and extern_rect.InsideXY(pt.x, pt.y) and not intern_rect.InsideXY(pt.x, pt.y): |
|
912 |
# Find if point is on a handle horizontally |
|
913 |
if left <= pt.x < left + HANDLE_SIZE: |
|
914 |
handle_x = 1 |
|
915 |
elif center <= pt.x < center + HANDLE_SIZE: |
|
916 |
handle_x = 2 |
|
917 |
elif right <= pt.x < right + HANDLE_SIZE: |
|
918 |
handle_x = 3 |
|
919 |
else: |
|
920 |
handle_x = 0 |
|
921 |
# Find if point is on a handle vertically |
|
922 |
if top <= pt.y < top + HANDLE_SIZE: |
|
923 |
handle_y = 1 |
|
924 |
elif middle <= pt.y < middle + HANDLE_SIZE: |
|
925 |
handle_y = 2 |
|
926 |
elif bottom <= pt.y < bottom + HANDLE_SIZE: |
|
927 |
handle_y = 3 |
|
928 |
else: |
|
929 |
handle_y = 0 |
|
930 |
# Verify that the result is valid |
|
931 |
if (handle_x, handle_y) in VALID_HANDLES: |
|
932 |
return handle_x, handle_y |
|
933 |
return 0, 0 |
|
934 |
||
935 |
# Method called when a LeftDown event have been generated |
|
936 |
def OnLeftDown(self, event, dc, scaling): |
|
937 |
pos = event.GetLogicalPosition(dc) |
|
938 |
# Test if an handle have been clicked |
|
939 |
handle = self.TestHandle(event) |
|
940 |
# Find which type of handle have been clicked, |
|
941 |
# Save a resize event and change the cursor |
|
942 |
cursor = HANDLE_CURSORS.get(handle, 1) |
|
943 |
wx.CallAfter(self.Parent.SetCurrentCursor, cursor) |
|
944 |
if cursor > 1: |
|
945 |
self.Handle = (HANDLE_RESIZE, handle) |
|
946 |
else: |
|
947 |
self.Handle = (HANDLE_MOVE, None) |
|
948 |
self.SetSelected(False) |
|
949 |
# Initializes the last position |
|
950 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
951 |
self.StartPos = wx.Point(self.Pos.x, self.Pos.y) |
|
952 |
self.CurrentDrag = wx.Point(0, 0) |
|
953 |
||
954 |
# Method called when a LeftUp event have been generated |
|
955 |
def OnLeftUp(self, event, dc, scaling): |
|
956 |
# If a dragging have been initiated |
|
957 |
if self.Dragging and self.oldPos: |
|
958 |
self.RefreshModel() |
|
959 |
self.Parent.RefreshBuffer() |
|
960 |
wx.CallAfter(self.Parent.SetCurrentCursor, 0) |
|
961 |
self.SetSelected(True) |
|
962 |
self.oldPos = None |
|
963 |
||
964 |
# Method called when a RightDown event have been generated |
|
965 |
def OnRightDown(self, event, dc, scaling): |
|
966 |
pass |
|
967 |
||
968 |
# Method called when a RightUp event have been generated |
|
969 |
def OnRightUp(self, event, dc, scaling): |
|
970 |
if self.Dragging and self.oldPos: |
|
971 |
self.RefreshModel() |
|
972 |
self.Parent.RefreshBuffer() |
|
973 |
wx.CallAfter(self.Parent.SetCurrentCursor, 0) |
|
974 |
self.SetSelected(True) |
|
975 |
self.oldPos = None |
|
976 |
if self.Parent.Debug: |
|
977 |
self.Parent.PopupForceMenu() |
|
978 |
||
979 |
# Method called when a LeftDClick event have been generated |
|
980 |
def OnLeftDClick(self, event, dc, scaling): |
|
981 |
pass |
|
982 |
||
983 |
# Method called when a Motion event have been generated |
|
984 |
def OnMotion(self, event, dc, scaling): |
|
985 |
# If the cursor is dragging and the element have been clicked |
|
986 |
if event.Dragging() and self.oldPos: |
|
987 |
# Calculate the movement of cursor |
|
988 |
pos = event.GetLogicalPosition(dc) |
|
989 |
movex = pos.x - self.oldPos.x |
|
990 |
movey = pos.y - self.oldPos.y |
|
991 |
# If movement is greater than MIN_MOVE then a dragging is initiated |
|
992 |
if not self.Dragging and (abs(movex) > MIN_MOVE or abs(movey) > MIN_MOVE): |
|
993 |
self.Dragging = True |
|
994 |
# If a dragging have been initiated, refreshes the element state |
|
995 |
if self.Dragging: |
|
996 |
dragx, dragy = self.ProcessDragging(movex, movey, event, scaling) |
|
997 |
if event.ControlDown() and self.Handle[0] == HANDLE_MOVE: |
|
998 |
self.oldPos.x = self.StartPos.x + self.CurrentDrag.x |
|
999 |
self.oldPos.y = self.StartPos.y + self.CurrentDrag.y |
|
1000 |
else: |
|
1001 |
self.oldPos.x += dragx |
|
1002 |
self.oldPos.y += dragy |
|
1003 |
return dragx, dragy |
|
1004 |
return movex, movey |
|
1005 |
# If cursor just pass over the element, changes the cursor if it is on a handle |
|
1006 |
else: |
|
1007 |
pos = event.GetLogicalPosition(dc) |
|
1008 |
handle = self.TestHandle(event) |
|
1009 |
# Find which type of handle have been clicked, |
|
1010 |
# Save a resize event and change the cursor |
|
1011 |
cursor = HANDLE_CURSORS.get(handle, 0) |
|
1012 |
wx.CallAfter(self.Parent.SetCurrentCursor, cursor) |
|
1013 |
return 0, 0 |
|
1014 |
||
1015 |
# Moves the element |
|
1016 |
def Move(self, dx, dy, exclude = []): |
|
1017 |
self.Pos.x += max(-self.BoundingBox.x, dx) |
|
1018 |
self.Pos.y += max(-self.BoundingBox.y, dy) |
|
1019 |
self.RefreshConnected(exclude) |
|
1020 |
self.RefreshBoundingBox() |
|
1021 |
||
1022 |
# Resizes the element from position and size given |
|
1023 |
def Resize(self, x, y, width, height): |
|
1024 |
self.Move(x, y) |
|
1025 |
self.SetSize(width, height) |
|
1026 |
||
1027 |
# Refreshes the element state according to move defined and handle selected |
|
1028 |
def ProcessDragging(self, movex, movey, event, scaling, width_fac = 1, height_fac = 1): |
|
1029 |
handle_type, handle = self.Handle |
|
1030 |
# If it is a resize handle, calculate the values from resizing |
|
1031 |
if handle_type == HANDLE_RESIZE: |
|
1032 |
if scaling is not None: |
|
1033 |
scaling = (scaling[0] * width_fac, scaling[1] * height_fac) |
|
1034 |
x = y = start_x = start_y = 0 |
|
1035 |
width, height = start_width, start_height = self.GetSize() |
|
1036 |
if handle[0] == 1: |
|
1037 |
movex = max(-self.BoundingBox.x, movex) |
|
1038 |
if scaling is not None: |
|
1039 |
movex = -(round_scaling(width - movex, scaling[0]) - width) |
|
1040 |
x = movex |
|
1041 |
if event.ShiftDown(): |
|
1042 |
width -= 2 * movex |
|
1043 |
else: |
|
1044 |
width -= movex |
|
1045 |
elif handle[0] == 3: |
|
1046 |
if scaling is not None: |
|
1047 |
movex = round_scaling(width + movex, scaling[0]) - width |
|
1048 |
if event.ShiftDown(): |
|
1049 |
movex = min(self.BoundingBox.x, movex) |
|
1050 |
x = -movex |
|
1051 |
width += 2 * movex |
|
1052 |
else: |
|
1053 |
width += movex |
|
1054 |
if handle[1] == 1: |
|
1055 |
movey = max(-self.BoundingBox.y, movey) |
|
1056 |
if scaling is not None: |
|
1057 |
movey = -(round_scaling(height - movey, scaling[1]) - height) |
|
1058 |
y = movey |
|
1059 |
if event.ShiftDown(): |
|
1060 |
height -= 2 * movey |
|
1061 |
else: |
|
1062 |
height -= movey |
|
1063 |
elif handle[1] == 3: |
|
1064 |
if scaling is not None: |
|
1065 |
movey = round_scaling(height + movey, scaling[1]) - height |
|
1066 |
if event.ShiftDown(): |
|
1067 |
movey = min(self.BoundingBox.y, movey) |
|
1068 |
y = -movey |
|
1069 |
height += 2 * movey |
|
1070 |
else: |
|
1071 |
height += movey |
|
1072 |
# Verify that new size is not lesser than minimum |
|
1073 |
min_width, min_height = self.GetMinSize() |
|
1074 |
if handle[0] != 2 and (width >= min_width or width > self.Size[0]): |
|
1075 |
start_x = x |
|
1076 |
start_width = width |
|
1077 |
else: |
|
1078 |
movex = 0 |
|
1079 |
if handle[1] != 2 and (height >= min_height or height > self.Size[1]): |
|
1080 |
start_y = y |
|
1081 |
start_height = height |
|
1082 |
else: |
|
1083 |
movey = 0 |
|
1084 |
if movex != 0 or movey != 0: |
|
1085 |
self.Resize(start_x, start_y, start_width, start_height) |
|
1086 |
return movex, movey |
|
1087 |
# If it is a move handle, Move this element |
|
1088 |
elif handle_type == HANDLE_MOVE: |
|
1089 |
movex = max(-self.BoundingBox.x, movex) |
|
1090 |
movey = max(-self.BoundingBox.y, movey) |
|
1091 |
if scaling is not None: |
|
1092 |
movex = round_scaling(self.Pos.x + movex, scaling[0]) - self.Pos.x |
|
1093 |
movey = round_scaling(self.Pos.y + movey, scaling[1]) - self.Pos.y |
|
1094 |
if event.ControlDown(): |
|
1095 |
self.CurrentDrag.x = self.CurrentDrag.x + movex |
|
1096 |
self.CurrentDrag.y = self.CurrentDrag.y + movey |
|
1097 |
if abs(self.CurrentDrag.x) > abs(self.CurrentDrag.y): |
|
1098 |
movex = self.StartPos.x + self.CurrentDrag.x - self.Pos.x |
|
1099 |
movey = self.StartPos.y - self.Pos.y |
|
1100 |
else: |
|
1101 |
movex = self.StartPos.x - self.Pos.x |
|
1102 |
movey = self.StartPos.y + self.CurrentDrag.y - self.Pos.y |
|
1103 |
self.Move(movex, movey) |
|
1104 |
return movex, movey |
|
1105 |
return 0, 0 |
|
1106 |
||
1107 |
def OnToolTipTimer(self, event): |
|
1108 |
value = self.GetToolTipValue() |
|
1109 |
if value is not None and self.ToolTipPos is not None: |
|
1110 |
self.ToolTip = ToolTip(self.Parent, value) |
|
1111 |
self.ToolTip.MoveToolTip(self.ToolTipPos) |
|
1112 |
self.ToolTip.Show() |
|
1113 |
||
1114 |
def GetToolTipValue(self): |
|
1115 |
return None |
|
1116 |
||
1117 |
def CreateToolTip(self, pos): |
|
1118 |
value = self.GetToolTipValue() |
|
1119 |
if value is not None: |
|
1120 |
self.ToolTipPos = pos |
|
1121 |
self.ToolTipTimer.Start(int(TOOLTIP_WAIT_PERIOD * 1000), oneShot=True) |
|
1122 |
||
1123 |
def MoveToolTip(self, pos): |
|
1124 |
if self.ToolTip is not None: |
|
1125 |
self.ToolTip.MoveToolTip(pos) |
|
1126 |
elif self.ToolTipPos is not None: |
|
1127 |
self.ToolTipPos = pos |
|
1128 |
self.ToolTipTimer.Start(int(TOOLTIP_WAIT_PERIOD * 1000), oneShot=True) |
|
1129 |
||
1130 |
def ClearToolTip(self): |
|
1131 |
self.ToolTipTimer.Stop() |
|
1132 |
self.ToolTipPos = None |
|
1133 |
if self.ToolTip is not None: |
|
1134 |
self.ToolTip.Destroy() |
|
1135 |
self.ToolTip = None |
|
1136 |
||
1137 |
# Override this method for defining the method to call for adding an highlight to this element |
|
1138 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
1139 |
pass |
|
1140 |
||
1141 |
# Override this method for defining the method to call for removing an highlight from this element |
|
1142 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
1143 |
pass |
|
1144 |
||
1145 |
# Override this method for defining the method to call for removing all the highlights of one particular type from this element |
|
1146 |
def ClearHighlight(self, highlight_type=None): |
|
1147 |
pass |
|
1148 |
||
1149 |
# Override this method for defining the method to call for refreshing the model of this element |
|
1150 |
def RefreshModel(self, move=True): |
|
1151 |
pass |
|
1152 |
||
1153 |
# Draws the highlightment of this element if it is highlighted (can be overwritten) |
|
1154 |
def DrawHighlightment(self, dc): |
|
1155 |
scalex, scaley = dc.GetUserScale() |
|
1156 |
dc.SetUserScale(1, 1) |
|
1157 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
1158 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
1159 |
dc.SetLogicalFunction(wx.AND) |
|
1160 |
dc.DrawRectangle(int(round((self.Pos.x - 1) * scalex)) - 2, |
|
1161 |
int(round((self.Pos.y - 1) * scaley)) - 2, |
|
1162 |
int(round((self.Size.width + 3) * scalex)) + 5, |
|
1163 |
int(round((self.Size.height + 3) * scaley)) + 5) |
|
1164 |
dc.SetLogicalFunction(wx.COPY) |
|
1165 |
dc.SetUserScale(scalex, scaley) |
|
1166 |
||
1167 |
# Draws the handles of this element if it is selected |
|
1168 |
def Draw(self, dc): |
|
1169 |
if not getattr(dc, "printing", False): |
|
1170 |
if self.Highlighted: |
|
1171 |
self.DrawHighlightment(dc) |
|
1172 |
if self.Selected: |
|
1173 |
scalex, scaley = dc.GetUserScale() |
|
1174 |
dc.SetUserScale(1, 1) |
|
1175 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
1176 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
1177 |
||
1178 |
left = (self.BoundingBox.x - 2) * scalex - HANDLE_SIZE |
|
1179 |
center = (self.BoundingBox.x + self.BoundingBox.width / 2) * scalex - HANDLE_SIZE / 2 |
|
1180 |
right = (self.BoundingBox.x + self.BoundingBox.width + 2) * scalex |
|
1181 |
||
1182 |
top = (self.BoundingBox.y - 2) * scaley - HANDLE_SIZE |
|
1183 |
middle = (self.BoundingBox.y + self.BoundingBox.height / 2) * scaley - HANDLE_SIZE / 2 |
|
1184 |
bottom = (self.BoundingBox.y + self.BoundingBox.height + 2) * scaley |
|
1185 |
||
1186 |
for x, y in [(left, top), (center, top), (right, top), |
|
1187 |
(left, middle), (right, middle), |
|
1188 |
(left, bottom), (center, bottom), (right, bottom)]: |
|
1189 |
dc.DrawRectangle(x, y, HANDLE_SIZE, HANDLE_SIZE) |
|
1190 |
||
1191 |
dc.SetUserScale(scalex, scaley) |
|
1192 |
||
1193 |
||
1194 |
#------------------------------------------------------------------------------- |
|
1195 |
# Group of graphic elements |
|
1196 |
#------------------------------------------------------------------------------- |
|
1197 |
||
1198 |
""" |
|
1199 |
Class that implements a group of graphic elements |
|
1200 |
""" |
|
1201 |
||
1202 |
class Graphic_Group(Graphic_Element): |
|
1203 |
||
1204 |
# Create a new group of graphic elements |
|
1205 |
def __init__(self, parent): |
|
1206 |
Graphic_Element.__init__(self, parent) |
|
1207 |
self.Elements = [] |
|
1208 |
self.RefreshWireExclusion() |
|
1209 |
self.RefreshBoundingBox() |
|
1210 |
||
1211 |
# Destructor |
|
1212 |
def __del__(self): |
|
1213 |
self.Elements = [] |
|
1214 |
||
1215 |
def GetDefinition(self): |
|
1216 |
blocks = [] |
|
1217 |
wires = [] |
|
1218 |
for element in self.Elements: |
|
1219 |
block, wire = element.GetDefinition() |
|
1220 |
blocks.extend(block) |
|
1221 |
wires.extend(wire) |
|
1222 |
return blocks, wires |
|
1223 |
||
1224 |
# Make a clone of this element |
|
1225 |
def Clone(self, parent, pos = None): |
|
1226 |
group = Graphic_Group(parent) |
|
1227 |
connectors = {} |
|
1228 |
exclude_names = {} |
|
1229 |
wires = [] |
|
1230 |
if pos is not None: |
|
1231 |
dx, dy = pos.x - self.BoundingBox.x, pos.y - self.BoundingBox.y |
|
1232 |
for element in self.Elements: |
|
1233 |
if isinstance(element, Wire): |
|
1234 |
wires.append(element) |
|
1235 |
else: |
|
1236 |
if pos is not None: |
|
1237 |
x, y = element.GetPosition() |
|
1238 |
new_pos = wx.Point(x + dx, y + dy) |
|
1239 |
newid = parent.GetNewId() |
|
1240 |
if parent.IsNamedElement(element): |
|
1241 |
name = parent.GenerateNewName(element, exclude_names) |
|
1242 |
exclude_names[name.upper()] = True |
|
1243 |
new_element = element.Clone(parent, newid, name, pos = new_pos) |
|
1244 |
else: |
|
1245 |
new_element = element.Clone(parent, newid, pos = new_pos) |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1246 |
new_element.SetBestSize(parent.Scaling) |
814 | 1247 |
else: |
1248 |
new_element = element.Clone(parent) |
|
1249 |
connectors.update(element.GetConnectorTranslation(new_element)) |
|
1250 |
group.SelectElement(new_element) |
|
1251 |
for element in wires: |
|
1252 |
if pos is not None: |
|
1253 |
new_wire = element.Clone(parent, connectors, dx, dy) |
|
1254 |
else: |
|
1255 |
new_wire = element.Clone(parent, connectors) |
|
1256 |
if new_wire is not None: |
|
1257 |
if pos is not None: |
|
1258 |
parent.AddWire(new_wire) |
|
1259 |
group.SelectElement(new_wire) |
|
1260 |
if pos is not None: |
|
1261 |
for element in group.Elements: |
|
1262 |
if not isinstance(element, Wire): |
|
1263 |
parent.AddBlockInModel(element) |
|
1264 |
return group |
|
1265 |
||
1266 |
def CanAddBlocks(self, parent): |
|
1267 |
valid = True |
|
1268 |
for element in self.Elements: |
|
1269 |
if not isinstance(element, Wire): |
|
1270 |
valid &= parent.CanAddElement(element) |
|
1271 |
return valid |
|
1272 |
||
1273 |
def IsVisible(self): |
|
1274 |
for element in self.Elements: |
|
1275 |
if element.IsVisible(): |
|
1276 |
return True |
|
1277 |
return False |
|
1278 |
||
1279 |
# Refresh the list of wire excluded |
|
1280 |
def RefreshWireExclusion(self): |
|
1281 |
self.WireExcluded = [] |
|
1282 |
for element in self.Elements: |
|
1283 |
if isinstance(element, Wire): |
|
1284 |
startblock = element.StartConnected.GetParentBlock() |
|
1285 |
endblock = element.EndConnected.GetParentBlock() |
|
1286 |
if startblock in self.Elements and endblock in self.Elements: |
|
1287 |
self.WireExcluded.append(element) |
|
1288 |
||
1289 |
# Returns the RedrawRect |
|
1290 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1291 |
rect = None |
|
1292 |
for element in self.Elements: |
|
1293 |
if rect is None: |
|
1294 |
rect = element.GetRedrawRect(movex, movey) |
|
1295 |
else: |
|
1296 |
rect = rect.Union(element.GetRedrawRect(movex, movey)) |
|
1297 |
return rect |
|
1298 |
||
1299 |
# Clean this group of elements |
|
1300 |
def Clean(self): |
|
1301 |
# Clean all the elements of the group |
|
1302 |
for element in self.Elements: |
|
1303 |
element.Clean() |
|
1304 |
||
1305 |
# Delete this group of elements |
|
1306 |
def Delete(self): |
|
1307 |
# Delete all the elements of the group |
|
1308 |
for element in self.Elements: |
|
1309 |
element.Delete() |
|
1310 |
self.WireExcluded = [] |
|
1311 |
||
1312 |
# Returns if the point given is in the bounding box of one of the elements of this group |
|
1313 |
def HitTest(self, pt, connectors=True): |
|
1314 |
result = False |
|
1315 |
for element in self.Elements: |
|
1316 |
result |= element.HitTest(pt, connectors) |
|
1317 |
return result |
|
1318 |
||
1319 |
# Returns if the element given is in this group |
|
1320 |
def IsElementIn(self, element): |
|
1321 |
return element in self.Elements |
|
1322 |
||
1323 |
# Change the elements of the group |
|
1324 |
def SetElements(self, elements): |
|
1325 |
self.Elements = elements |
|
1326 |
self.RefreshWireExclusion() |
|
1327 |
self.RefreshBoundingBox() |
|
1328 |
||
1329 |
# Returns the elements of the group |
|
1330 |
def GetElements(self): |
|
1331 |
return self.Elements |
|
1332 |
||
1333 |
# Align the group elements |
|
1334 |
def AlignElements(self, horizontally, vertically): |
|
1335 |
minx = self.BoundingBox.x + self.BoundingBox.width |
|
1336 |
miny = self.BoundingBox.y + self.BoundingBox.height |
|
1337 |
maxx = self.BoundingBox.x |
|
1338 |
maxy = self.BoundingBox.y |
|
1339 |
for element in self.Elements: |
|
1340 |
if not isinstance(element, Wire): |
|
1341 |
posx, posy = element.GetPosition() |
|
1342 |
width, height = element.GetSize() |
|
1343 |
minx = min(minx, posx) |
|
1344 |
miny = min(miny, posy) |
|
1345 |
maxx = max(maxx, posx + width) |
|
1346 |
maxy = max(maxy, posy + height) |
|
1347 |
for element in self.Elements: |
|
1348 |
if not isinstance(element, Wire): |
|
1349 |
posx, posy = element.GetPosition() |
|
1350 |
width, height = element.GetSize() |
|
1351 |
movex = movey = 0 |
|
1352 |
if horizontally == ALIGN_LEFT: |
|
1353 |
movex = minx - posx |
|
1354 |
elif horizontally == ALIGN_CENTER: |
|
1355 |
movex = (maxx + minx - width) / 2 - posx |
|
1356 |
elif horizontally == ALIGN_RIGHT: |
|
1357 |
movex = maxx - width - posx |
|
1358 |
if vertically == ALIGN_TOP: |
|
1359 |
movey = miny - posy |
|
1360 |
elif vertically == ALIGN_MIDDLE: |
|
1361 |
movey = (maxy + miny - height) / 2 - posy |
|
1362 |
elif vertically == ALIGN_BOTTOM: |
|
1363 |
movey = maxy - height - posy |
|
1364 |
if movex != 0 or movey != 0: |
|
1365 |
element.Move(movex, movey) |
|
1366 |
element.RefreshModel() |
|
1367 |
self.RefreshWireExclusion() |
|
1368 |
self.RefreshBoundingBox() |
|
1369 |
||
1370 |
# Remove or select the given element if it is or not in the group |
|
1371 |
def SelectElement(self, element): |
|
1372 |
if element in self.Elements: |
|
1373 |
self.Elements.remove(element) |
|
1374 |
else: |
|
1375 |
self.Elements.append(element) |
|
1376 |
self.RefreshWireExclusion() |
|
1377 |
self.RefreshBoundingBox() |
|
1378 |
||
1379 |
# Move this group of elements |
|
1380 |
def Move(self, movex, movey): |
|
1381 |
movex = max(-self.BoundingBox.x, movex) |
|
1382 |
movey = max(-self.BoundingBox.y, movey) |
|
1383 |
# Move all the elements of the group |
|
1384 |
for element in self.Elements: |
|
1385 |
if not isinstance(element, Wire): |
|
1386 |
element.Move(movex, movey, self.WireExcluded) |
|
1387 |
elif element in self.WireExcluded: |
|
1388 |
element.Move(movex, movey, True) |
|
1389 |
self.RefreshBoundingBox() |
|
1390 |
||
1391 |
# Refreshes the bounding box of this group of elements |
|
1392 |
def RefreshBoundingBox(self): |
|
1393 |
if len(self.Elements) > 0: |
|
1394 |
bbox = self.Elements[0].GetBoundingBox() |
|
1395 |
minx, miny = bbox.x, bbox.y |
|
1396 |
maxx = bbox.x + bbox.width |
|
1397 |
maxy = bbox.y + bbox.height |
|
1398 |
for element in self.Elements[1:]: |
|
1399 |
bbox = element.GetBoundingBox() |
|
1400 |
minx = min(minx, bbox.x) |
|
1401 |
miny = min(miny, bbox.y) |
|
1402 |
maxx = max(maxx, bbox.x + bbox.width) |
|
1403 |
maxy = max(maxy, bbox.y + bbox.height) |
|
1404 |
self.BoundingBox = wx.Rect(minx, miny, maxx - minx, maxy - miny) |
|
1405 |
else: |
|
1406 |
self.BoundingBox = wx.Rect(0, 0, 0, 0) |
|
1407 |
self.Pos = wx.Point(self.BoundingBox.x, self.BoundingBox.y) |
|
1408 |
self.Size = wx.Size(self.BoundingBox.width, self.BoundingBox.height) |
|
1409 |
||
1410 |
# Forbids to change the group position |
|
1411 |
def SetPosition(x, y): |
|
1412 |
pass |
|
1413 |
||
1414 |
# Returns the position of this group |
|
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1415 |
def GetPosition(self, exclude_wires=False): |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1416 |
if exclude_wires: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1417 |
posx = posy = None |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1418 |
for element in self.Elements: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1419 |
if not isinstance(element, Wire) or element in self.WireExcluded: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1420 |
bbox = element.GetBoundingBox() |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1421 |
if posx is None and posy is None: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1422 |
posx, posy = bbox.x, bbox.y |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1423 |
else: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1424 |
posx = min(posx, bbox.x) |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1425 |
posy = min(posy, bbox.y) |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1426 |
if posx is None and posy is None: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1427 |
return 0, 0 |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1428 |
return posx, posy |
814 | 1429 |
return self.BoundingBox.x, self.BoundingBox.y |
1430 |
||
1431 |
# Forbids to change the group size |
|
1432 |
def SetSize(width, height): |
|
1433 |
pass |
|
1434 |
||
1435 |
# Returns the size of this group |
|
1436 |
def GetSize(self): |
|
1437 |
return self.BoundingBox.width, self.BoundingBox.height |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1438 |
|
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1439 |
# Set size of the group elements to their minimum size |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1440 |
def SetBestSize(self, scaling): |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1441 |
max_movex = max_movey = 0 |
814 | 1442 |
for element in self.Elements: |
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1443 |
movex, movey = element.SetBestSize(scaling) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1444 |
max_movex = max(max_movex, movex) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1445 |
max_movey = max(max_movey, movey) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1446 |
return max_movex, max_movey |
814 | 1447 |
|
1448 |
# Refreshes the group elements to move defined and handle selected |
|
1449 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1450 |
handle_type, handle = self.Handle |
|
1451 |
# If it is a move handle, Move this group elements |
|
1452 |
if handle_type == HANDLE_MOVE: |
|
1453 |
movex = max(-self.BoundingBox.x, movex) |
|
1454 |
movey = max(-self.BoundingBox.y, movey) |
|
1455 |
if scaling is not None: |
|
1456 |
movex = round_scaling(movex, scaling[0]) |
|
1457 |
movey = round_scaling(movey, scaling[1]) |
|
1458 |
if event.ControlDown(): |
|
1459 |
self.CurrentDrag.x = self.CurrentDrag.x + movex |
|
1460 |
self.CurrentDrag.y = self.CurrentDrag.y + movey |
|
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1461 |
posx, posy = self.GetPosition(True) |
814 | 1462 |
if abs(self.CurrentDrag.x) > abs(self.CurrentDrag.y): |
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1463 |
movex = self.StartPos.x + self.CurrentDrag.x - posx |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1464 |
movey = self.StartPos.y - posy |
814 | 1465 |
else: |
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1466 |
movex = self.StartPos.x - posx |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1467 |
movey = self.StartPos.y + self.CurrentDrag.y - posy |
814 | 1468 |
self.Move(movex, movey) |
1469 |
return movex, movey |
|
1470 |
return 0, 0 |
|
1471 |
||
1472 |
# Change the variable that indicates if this element is highlighted |
|
1473 |
def SetHighlighted(self, highlighted): |
|
1474 |
for element in self.Elements: |
|
1475 |
element.SetHighlighted(highlighted) |
|
1476 |
||
1477 |
def HighlightPoint(self, pos): |
|
1478 |
for element in self.Elements: |
|
1479 |
if isinstance(element, Wire): |
|
1480 |
element.HighlightPoint(pos) |
|
1481 |
||
1482 |
# Method called when a LeftDown event have been generated |
|
1483 |
def OnLeftDown(self, event, dc, scaling): |
|
1484 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
|
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
1485 |
self.StartPos = wx.Point(*self.GetPosition(True)) |
814 | 1486 |
for element in self.Elements: |
1487 |
element.Handle = self.Handle |
|
1488 |
||
1489 |
# Change the variable that indicates if the elemente is selected |
|
1490 |
def SetSelected(self, selected): |
|
1491 |
for element in self.Elements: |
|
1492 |
element.SetSelected(selected) |
|
1493 |
||
1494 |
# Method called when a RightUp event has been generated |
|
1495 |
def OnRightUp(self, event, dc, scaling): |
|
1496 |
# Popup the menu with special items for a group |
|
1497 |
self.Parent.PopupGroupMenu() |
|
1498 |
||
1499 |
# Refreshes the model of all the elements of this group |
|
1047
efcc2283dd77
Fixed bug when using 'Adjust Block Size' contextual menu item on a group of selected elements
Laurent Bessard
parents:
993
diff
changeset
|
1500 |
def RefreshModel(self, move=True): |
814 | 1501 |
for element in self.Elements: |
1047
efcc2283dd77
Fixed bug when using 'Adjust Block Size' contextual menu item on a group of selected elements
Laurent Bessard
parents:
993
diff
changeset
|
1502 |
element.RefreshModel(move) |
814 | 1503 |
|
1069
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1504 |
# Draws the handles of this element if it is selected |
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1505 |
def Draw(self, dc): |
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1506 |
for element in self.Elements: |
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1507 |
element.Draw(dc) |
814 | 1508 |
|
1509 |
#------------------------------------------------------------------------------- |
|
1510 |
# Connector for all types of blocks |
|
1511 |
#------------------------------------------------------------------------------- |
|
1512 |
||
1513 |
""" |
|
1514 |
Class that implements a connector for any type of block |
|
1515 |
""" |
|
1516 |
||
1517 |
class Connector: |
|
1518 |
||
1519 |
# Create a new connector |
|
1520 |
def __init__(self, parent, name, type, position, direction, negated = False, edge = "none", onlyone = False): |
|
1521 |
self.ParentBlock = parent |
|
1522 |
self.Name = name |
|
1523 |
self.Type = type |
|
1524 |
self.Pos = position |
|
1525 |
self.Direction = direction |
|
1526 |
self.Wires = [] |
|
1527 |
if self.ParentBlock.IsOfType("BOOL", type): |
|
1528 |
self.Negated = negated |
|
1529 |
self.Edge = edge |
|
1530 |
else: |
|
1531 |
self.Negated = False |
|
1532 |
self.Edge = "none" |
|
1533 |
self.OneConnected = onlyone |
|
1534 |
self.Valid = True |
|
1535 |
self.Value = None |
|
1536 |
self.Forced = False |
|
1537 |
self.Selected = False |
|
1538 |
self.Highlights = [] |
|
1539 |
self.RefreshNameSize() |
|
1540 |
||
1541 |
def Flush(self): |
|
1542 |
self.ParentBlock = None |
|
1543 |
for wire, handle in self.Wires: |
|
1544 |
wire.Flush() |
|
1545 |
self.Wires = [] |
|
1546 |
||
1547 |
# Returns the RedrawRect |
|
1548 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1549 |
parent_pos = self.ParentBlock.GetPosition() |
|
1550 |
x = min(parent_pos[0] + self.Pos.x, parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE) |
|
1551 |
y = min(parent_pos[1] + self.Pos.y, parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE) |
|
1552 |
if self.Direction[0] == 0: |
|
1553 |
width = 5 |
|
1554 |
else: |
|
1555 |
width = CONNECTOR_SIZE |
|
1556 |
if self.Direction[1] == 0: |
|
1557 |
height = 5 |
|
1558 |
else: |
|
1559 |
height = CONNECTOR_SIZE |
|
1560 |
return wx.Rect(x - abs(movex), y - abs(movey), width + 2 * abs(movex), height + 2 * abs(movey)) |
|
1561 |
||
1562 |
# Change the connector selection |
|
1563 |
def SetSelected(self, selected): |
|
1564 |
self.Selected = selected |
|
1565 |
||
1566 |
# Make a clone of the connector |
|
1567 |
def Clone(self, parent = None): |
|
1568 |
if parent is None: |
|
1569 |
parent = self.ParentBlock |
|
1570 |
return Connector(parent, self.Name, self.Type, wx.Point(self.Pos[0], self.Pos[1]), |
|
1571 |
self.Direction, self.Negated) |
|
1572 |
||
1573 |
# Returns the connector parent block |
|
1574 |
def GetParentBlock(self): |
|
1575 |
return self.ParentBlock |
|
1576 |
||
1577 |
# Returns the connector type |
|
1578 |
def GetType(self, raw = False): |
|
1579 |
if self.ParentBlock.IsEndType(self.Type) or raw: |
|
1580 |
return self.Type |
|
1581 |
elif (self.Negated or self.Edge != "none") and self.ParentBlock.IsOfType("BOOL", self.Type): |
|
1582 |
return "BOOL" |
|
1583 |
else: |
|
1584 |
return self.ParentBlock.GetConnectionResultType(self, self.Type) |
|
1585 |
||
1586 |
# Returns the connector type |
|
1587 |
def GetConnectedType(self): |
|
1588 |
if self.ParentBlock.IsEndType(self.Type): |
|
1589 |
return self.Type |
|
1590 |
elif len(self.Wires) == 1: |
|
1591 |
return self.Wires[0][0].GetOtherConnectedType(self.Wires[0][1]) |
|
1592 |
return self.Type |
|
1593 |
||
1594 |
# Returns the connector type |
|
1595 |
def GetConnectedRedrawRect(self, movex, movey): |
|
1596 |
rect = None |
|
1597 |
for wire, handle in self.Wires: |
|
1598 |
if rect is None: |
|
1599 |
rect = wire.GetRedrawRect() |
|
1600 |
else: |
|
1601 |
rect = rect.Union(wire.GetRedrawRect()) |
|
1602 |
return rect |
|
1603 |
||
1604 |
# Returns if connector type is compatible with type given |
|
1605 |
def IsCompatible(self, type): |
|
1606 |
reference = self.GetType() |
|
1607 |
return self.ParentBlock.IsOfType(type, reference) or self.ParentBlock.IsOfType(reference, type) |
|
1608 |
||
1609 |
# Changes the connector name |
|
1610 |
def SetType(self, type): |
|
1611 |
self.Type = type |
|
1612 |
for wire, handle in self.Wires: |
|
1613 |
wire.SetValid(wire.IsConnectedCompatible()) |
|
1614 |
||
1615 |
# Returns the connector name |
|
1616 |
def GetName(self): |
|
1617 |
return self.Name |
|
1618 |
||
1619 |
# Changes the connector name |
|
1620 |
def SetName(self, name): |
|
1621 |
self.Name = name |
|
1622 |
self.RefreshNameSize() |
|
1623 |
||
1624 |
def RefreshForced(self): |
|
1625 |
self.Forced = False |
|
1626 |
for wire, handle in self.Wires: |
|
1627 |
self.Forced |= wire.IsForced() |
|
1628 |
||
1629 |
def RefreshValue(self): |
|
1630 |
self.Value = self.ReceivingCurrent() |
|
1631 |
||
1632 |
def RefreshValid(self): |
|
1633 |
self.Valid = True |
|
1634 |
for wire, handle in self.Wires: |
|
1635 |
self.Valid &= wire.GetValid() |
|
1636 |
||
1637 |
def ReceivingCurrent(self): |
|
1638 |
current = False |
|
1639 |
for wire, handle in self.Wires: |
|
1640 |
value = wire.GetValue() |
|
1641 |
if current != "undefined" and isinstance(value, BooleanType): |
|
1642 |
current |= wire.GetValue() |
|
1643 |
elif value == "undefined": |
|
1644 |
current = "undefined" |
|
1645 |
return current |
|
1646 |
||
1647 |
def SpreadCurrent(self, spreading): |
|
1648 |
for wire, handle in self.Wires: |
|
1649 |
wire.SetValue(spreading) |
|
1650 |
||
1651 |
# Changes the connector name size |
|
1652 |
def RefreshNameSize(self): |
|
1653 |
if self.Name != "": |
|
1654 |
self.NameSize = self.ParentBlock.Parent.GetTextExtent(self.Name) |
|
1655 |
else: |
|
1656 |
self.NameSize = 0, 0 |
|
1657 |
||
1658 |
# Returns the connector name size |
|
1659 |
def GetNameSize(self): |
|
1660 |
return self.NameSize |
|
1661 |
||
1662 |
# Returns the wires connected to the connector |
|
1663 |
def GetWires(self): |
|
1664 |
return self.Wires |
|
1665 |
||
1666 |
# Returns the parent block Id |
|
1667 |
def GetBlockId(self): |
|
1668 |
return self.ParentBlock.GetId() |
|
1669 |
||
1670 |
# Returns the connector relative position |
|
1671 |
def GetRelPosition(self): |
|
1672 |
return self.Pos |
|
1673 |
||
1674 |
# Returns the connector absolute position |
|
1675 |
def GetPosition(self, size = True): |
|
1676 |
parent_pos = self.ParentBlock.GetPosition() |
|
1677 |
# If the position of the end of the connector is asked |
|
1678 |
if size: |
|
1679 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
1680 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
1681 |
else: |
|
1682 |
x = parent_pos[0] + self.Pos.x |
|
1683 |
y = parent_pos[1] + self.Pos.y |
|
1684 |
return wx.Point(x, y) |
|
1685 |
||
1686 |
# Change the connector relative position |
|
1687 |
def SetPosition(self, pos): |
|
1688 |
self.Pos = pos |
|
1689 |
||
1690 |
# Returns the connector direction |
|
1691 |
def GetDirection(self): |
|
1692 |
return self.Direction |
|
1693 |
||
1694 |
# Change the connector direction |
|
1695 |
def SetDirection(self, direction): |
|
1696 |
self.Direction = direction |
|
1697 |
||
1698 |
# Connect a wire to this connector at the last place |
|
1699 |
def Connect(self, wire, refresh = True): |
|
1700 |
self.InsertConnect(len(self.Wires), wire, refresh) |
|
1701 |
||
1702 |
# Connect a wire to this connector at the place given |
|
1703 |
def InsertConnect(self, idx, wire, refresh = True): |
|
1704 |
if wire not in self.Wires: |
|
1705 |
self.Wires.insert(idx, wire) |
|
1054
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1706 |
if wire[1] == 0: |
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1707 |
wire[0].ConnectStartPoint(None, self) |
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1708 |
else: |
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1709 |
wire[0].ConnectEndPoint(None, self) |
814 | 1710 |
if refresh: |
1711 |
self.ParentBlock.RefreshModel(False) |
|
1712 |
||
1713 |
# Returns the index of the wire given in the list of connected |
|
1714 |
def GetWireIndex(self, wire): |
|
1715 |
for i, (tmp_wire, handle) in enumerate(self.Wires): |
|
1716 |
if tmp_wire == wire: |
|
1717 |
return i |
|
1718 |
return None |
|
1719 |
||
1720 |
# Unconnect a wire or all wires connected to the connector |
|
1721 |
def UnConnect(self, wire = None, unconnect = True, delete = False): |
|
1722 |
i = 0 |
|
1723 |
found = False |
|
1724 |
while i < len(self.Wires) and not found: |
|
1725 |
if not wire or self.Wires[i][0] == wire: |
|
1726 |
# If Unconnect haven't been called from a wire, disconnect the connector in the wire |
|
1727 |
if unconnect: |
|
1728 |
if self.Wires[i][1] == 0: |
|
1729 |
self.Wires[i][0].UnConnectStartPoint(delete) |
|
1730 |
else: |
|
1731 |
self.Wires[i][0].UnConnectEndPoint(delete) |
|
1732 |
# Remove wire from connected |
|
1733 |
if wire: |
|
1734 |
self.Wires.pop(i) |
|
1735 |
found = True |
|
1736 |
i += 1 |
|
1737 |
# If no wire defined, unconnect all wires |
|
1738 |
if not wire: |
|
1739 |
self.Wires = [] |
|
857
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset
|
1740 |
if not delete: |
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset
|
1741 |
self.RefreshValid() |
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset
|
1742 |
self.ParentBlock.RefreshModel(False) |
814 | 1743 |
|
1744 |
# Returns if connector has one or more wire connected |
|
1745 |
def IsConnected(self): |
|
1746 |
return len(self.Wires) > 0 |
|
1747 |
||
1748 |
# Move the wires connected |
|
1749 |
def MoveConnected(self, exclude = []): |
|
1750 |
if len(self.Wires) > 0: |
|
1751 |
# Calculate the new position of the end point |
|
1752 |
parent_pos = self.ParentBlock.GetPosition() |
|
1753 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
1754 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
1755 |
# Move the corresponding point on all the wires connected |
|
1756 |
for wire, index in self.Wires: |
|
1757 |
if wire not in exclude: |
|
1758 |
if index == 0: |
|
1759 |
wire.MoveStartPoint(wx.Point(x, y)) |
|
1760 |
else: |
|
1761 |
wire.MoveEndPoint(wx.Point(x, y)) |
|
1762 |
||
1763 |
# Refreshes the model of all the wires connected |
|
1764 |
def RefreshWires(self): |
|
1765 |
for wire in self.Wires: |
|
1766 |
wire[0].RefreshModel() |
|
1767 |
||
1768 |
# Refreshes the parent block model |
|
1769 |
def RefreshParentBlock(self): |
|
1770 |
self.ParentBlock.RefreshModel(False) |
|
1771 |
||
1772 |
# Highlight the parent block |
|
1773 |
def HighlightParentBlock(self, highlight): |
|
1774 |
self.ParentBlock.SetHighlighted(highlight) |
|
1775 |
self.ParentBlock.Refresh() |
|
1776 |
||
1777 |
# Returns all the blocks connected to this connector |
|
1778 |
def GetConnectedBlocks(self): |
|
1779 |
blocks = [] |
|
1780 |
for wire, handle in self.Wires: |
|
1781 |
# Get other connector connected to each wire |
|
1782 |
if handle == 0: |
|
1783 |
connector = wire.GetEndConnected() |
|
1784 |
else: |
|
1785 |
connector = wire.GetStartConnected() |
|
1786 |
# Get parent block for this connector |
|
1787 |
if connector: |
|
1788 |
block = connector.GetParentBlock() |
|
1789 |
if block not in blocks: |
|
1790 |
blocks.append(block) |
|
1791 |
return blocks |
|
1792 |
||
1793 |
# Returns the connector negated property |
|
1794 |
def IsNegated(self): |
|
1795 |
return self.Negated |
|
1796 |
||
1797 |
# Changes the connector negated property |
|
1798 |
def SetNegated(self, negated): |
|
1799 |
if self.ParentBlock.IsOfType("BOOL", self.Type): |
|
1800 |
self.Negated = negated |
|
1801 |
self.Edge = "none" |
|
1802 |
||
1803 |
# Returns the connector edge property |
|
1804 |
def GetEdge(self): |
|
1805 |
return self.Edge |
|
1806 |
||
1807 |
# Changes the connector edge property |
|
1808 |
def SetEdge(self, edge): |
|
1809 |
if self.ParentBlock.IsOfType("BOOL", self.Type): |
|
1810 |
self.Edge = edge |
|
1811 |
self.Negated = False |
|
1812 |
||
1813 |
# Tests if the point given is near from the end point of this connector |
|
1814 |
def TestPoint(self, pt, direction = None, exclude = True): |
|
1815 |
parent_pos = self.ParentBlock.GetPosition() |
|
1816 |
if (not (len(self.Wires) > 0 and self.OneConnected and exclude) or self.Type == "BOOL")\ |
|
1817 |
and direction is None or self.Direction == direction: |
|
1818 |
# Calculate a square around the end point of this connector |
|
1819 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
|
1820 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
|
1821 |
width = ANCHOR_DISTANCE * 2 + abs(self.Direction[0]) * CONNECTOR_SIZE |
|
1822 |
height = ANCHOR_DISTANCE * 2 + abs(self.Direction[1]) * CONNECTOR_SIZE |
|
1823 |
rect = wx.Rect(x, y, width, height) |
|
1824 |
return rect.InsideXY(pt.x, pt.y) |
|
1825 |
return False |
|
1826 |
||
1827 |
# Draws the highlightment of this element if it is highlighted |
|
1828 |
def DrawHighlightment(self, dc): |
|
1829 |
scalex, scaley = dc.GetUserScale() |
|
1830 |
dc.SetUserScale(1, 1) |
|
1831 |
pen = MiterPen(HIGHLIGHTCOLOR, 2 * scalex + 5) |
|
1832 |
pen.SetCap(wx.CAP_BUTT) |
|
1833 |
dc.SetPen(pen) |
|
1834 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
1835 |
dc.SetLogicalFunction(wx.AND) |
|
1836 |
parent_pos = self.ParentBlock.GetPosition() |
|
1837 |
posx = parent_pos[0] + self.Pos.x |
|
1838 |
posy = parent_pos[1] + self.Pos.y |
|
1839 |
xstart = parent_pos[0] + self.Pos.x |
|
1840 |
ystart = parent_pos[1] + self.Pos.y |
|
1841 |
if self.Direction[0] < 0: |
|
1842 |
xstart += 1 |
|
1843 |
if self.Direction[1] < 0: |
|
1844 |
ystart += 1 |
|
1845 |
xend = xstart + CONNECTOR_SIZE * self.Direction[0] |
|
1846 |
yend = ystart + CONNECTOR_SIZE * self.Direction[1] |
|
1847 |
dc.DrawLine(round((xstart + self.Direction[0]) * scalex), round((ystart + self.Direction[1]) * scaley), |
|
1848 |
round(xend * scalex), round(yend * scaley)) |
|
1849 |
dc.SetLogicalFunction(wx.COPY) |
|
1850 |
dc.SetUserScale(scalex, scaley) |
|
1851 |
||
1852 |
# Adds an highlight to the connector |
|
1853 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
1854 |
if highlight_type == ERROR_HIGHLIGHT: |
|
1855 |
for wire, handle in self.Wires: |
|
1856 |
wire.SetValid(False) |
|
1857 |
AddHighlight(self.Highlights, (start, end, highlight_type)) |
|
1858 |
||
1859 |
# Removes an highlight from the connector |
|
1860 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
1861 |
error = False |
|
1862 |
highlights = [] |
|
1863 |
for highlight in self.Highlights: |
|
1864 |
if highlight != (start, end, highlight_type): |
|
1865 |
highlights.append(highlight) |
|
1866 |
error |= highlight == ERROR_HIGHLIGHT |
|
1867 |
self.Highlights = highlights |
|
1868 |
if not error: |
|
1869 |
for wire, handle in self.Wires: |
|
1870 |
wire.SetValid(wire.IsConnectedCompatible()) |
|
1871 |
||
1872 |
# Removes all the highlights of one particular type from the connector |
|
1873 |
def ClearHighlight(self, highlight_type=None): |
|
1874 |
error = False |
|
1875 |
if highlight_type is None: |
|
1876 |
self.Highlights = [] |
|
1877 |
else: |
|
1878 |
highlights = [] |
|
1879 |
for highlight in self.Highlights: |
|
1880 |
if highlight[2] != highlight_type: |
|
1881 |
highlights.append(highlight) |
|
1882 |
error |= highlight == ERROR_HIGHLIGHT |
|
1883 |
self.Highlights = highlights |
|
1884 |
if not error: |
|
1885 |
for wire, handle in self.Wires: |
|
1886 |
wire.SetValid(wire.IsConnectedCompatible()) |
|
1887 |
||
1888 |
# Draws the connector |
|
1889 |
def Draw(self, dc): |
|
1890 |
if self.Selected: |
|
1891 |
dc.SetPen(MiterPen(wx.BLUE, 3)) |
|
1892 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
1893 |
#elif len(self.Highlights) > 0: |
|
1894 |
# dc.SetPen(MiterPen(self.Highlights[-1][1])) |
|
1895 |
# dc.SetBrush(wx.Brush(self.Highlights[-1][0])) |
|
1896 |
else: |
|
1897 |
if not self.Valid: |
|
1898 |
dc.SetPen(MiterPen(wx.RED)) |
|
1899 |
elif isinstance(self.Value, BooleanType) and self.Value: |
|
1900 |
if self.Forced: |
|
1901 |
dc.SetPen(MiterPen(wx.CYAN)) |
|
1902 |
else: |
|
1903 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
1904 |
elif self.Value == "undefined": |
|
1905 |
dc.SetPen(MiterPen(wx.NamedColour("orange"))) |
|
1906 |
elif self.Forced: |
|
1907 |
dc.SetPen(MiterPen(wx.BLUE)) |
|
1908 |
else: |
|
1909 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
1910 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
1911 |
parent_pos = self.ParentBlock.GetPosition() |
|
1912 |
||
1913 |
if getattr(dc, "printing", False): |
|
1914 |
name_size = dc.GetTextExtent(self.Name) |
|
1915 |
else: |
|
1916 |
name_size = self.NameSize |
|
1917 |
||
1918 |
if self.Negated: |
|
1919 |
# If connector is negated, draw a circle |
|
1920 |
xcenter = parent_pos[0] + self.Pos.x + (CONNECTOR_SIZE * self.Direction[0]) / 2 |
|
1921 |
ycenter = parent_pos[1] + self.Pos.y + (CONNECTOR_SIZE * self.Direction[1]) / 2 |
|
1922 |
dc.DrawCircle(xcenter, ycenter, CONNECTOR_SIZE / 2) |
|
1923 |
else: |
|
1924 |
xstart = parent_pos[0] + self.Pos.x |
|
1925 |
ystart = parent_pos[1] + self.Pos.y |
|
1926 |
if self.Edge == "rising": |
|
1927 |
# If connector has a rising edge, draw a right arrow |
|
1928 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart - 4) |
|
1929 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart + 4) |
|
1930 |
elif self.Edge == "falling": |
|
1931 |
# If connector has a falling edge, draw a left arrow |
|
1932 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart - 4) |
|
1933 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart + 4) |
|
1934 |
if self.Direction[0] < 0: |
|
1935 |
xstart += 1 |
|
1936 |
if self.Direction[1] < 0: |
|
1937 |
ystart += 1 |
|
1938 |
if self.Selected: |
|
1939 |
xend = xstart + (CONNECTOR_SIZE - 2) * self.Direction[0] |
|
1940 |
yend = ystart + (CONNECTOR_SIZE - 2) * self.Direction[1] |
|
1941 |
dc.DrawLine(xstart + 2 * self.Direction[0], ystart + 2 * self.Direction[1], xend, yend) |
|
1942 |
else: |
|
1943 |
xend = xstart + CONNECTOR_SIZE * self.Direction[0] |
|
1944 |
yend = ystart + CONNECTOR_SIZE * self.Direction[1] |
|
1945 |
dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend) |
|
1946 |
if self.Direction[0] != 0: |
|
1947 |
ytext = parent_pos[1] + self.Pos.y - name_size[1] / 2 |
|
1948 |
if self.Direction[0] < 0: |
|
1949 |
xtext = parent_pos[0] + self.Pos.x + 5 |
|
1950 |
else: |
|
1951 |
xtext = parent_pos[0] + self.Pos.x - (name_size[0] + 5) |
|
1952 |
if self.Direction[1] != 0: |
|
1953 |
xtext = parent_pos[0] + self.Pos.x - name_size[0] / 2 |
|
1954 |
if self.Direction[1] < 0: |
|
1955 |
ytext = parent_pos[1] + self.Pos.y + 5 |
|
1956 |
else: |
|
1957 |
ytext = parent_pos[1] + self.Pos.y - (name_size[1] + 5) |
|
1958 |
# Draw the text |
|
1959 |
dc.DrawText(self.Name, xtext, ytext) |
|
1960 |
if not getattr(dc, "printing", False): |
|
1961 |
DrawHighlightedText(dc, self.Name, self.Highlights, xtext, ytext) |
|
1962 |
||
1963 |
#------------------------------------------------------------------------------- |
|
1964 |
# Common Wire Element |
|
1965 |
#------------------------------------------------------------------------------- |
|
1966 |
||
1967 |
""" |
|
1968 |
Class that implements a wire for connecting two blocks |
|
1969 |
""" |
|
1970 |
||
1971 |
class Wire(Graphic_Element, DebugDataConsumer): |
|
1972 |
||
1973 |
# Create a new wire |
|
1974 |
def __init__(self, parent, start = None, end = None): |
|
1975 |
Graphic_Element.__init__(self, parent) |
|
1976 |
DebugDataConsumer.__init__(self) |
|
1977 |
self.StartPoint = start |
|
1978 |
self.EndPoint = end |
|
1979 |
self.StartConnected = None |
|
1980 |
self.EndConnected = None |
|
1981 |
# If the start and end points are defined, calculate the wire |
|
1982 |
if start and end: |
|
1983 |
self.ResetPoints() |
|
1984 |
self.GeneratePoints() |
|
1985 |
else: |
|
1986 |
self.Points = [] |
|
1987 |
self.Segments = [] |
|
1988 |
self.SelectedSegment = None |
|
1989 |
self.Valid = True |
|
1990 |
self.ValueSize = None |
|
1991 |
self.ComputedValue = None |
|
1992 |
self.OverStart = False |
|
1993 |
self.OverEnd = False |
|
1994 |
self.ComputingType = False |
|
1995 |
self.Font = parent.GetMiniFont() |
|
1996 |
||
1997 |
def GetDefinition(self): |
|
1998 |
if self.StartConnected is not None and self.EndConnected is not None: |
|
1999 |
startblock = self.StartConnected.GetParentBlock() |
|
2000 |
endblock = self.EndConnected.GetParentBlock() |
|
2001 |
return [], [(startblock.GetId(), endblock.GetId())] |
|
2002 |
return [], [] |
|
2003 |
||
2004 |
def Flush(self): |
|
2005 |
self.StartConnected = None |
|
2006 |
self.EndConnected = None |
|
2007 |
||
2008 |
def GetToolTipValue(self): |
|
2009 |
if self.Value is not None and self.Value != "undefined" and not isinstance(self.Value, BooleanType): |
|
878
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2010 |
wire_type = self.GetEndConnectedType() |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2011 |
if wire_type == "STRING": |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2012 |
return "'%s'"%self.Value |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2013 |
elif wire_type == "WSTRING": |
814 | 2014 |
return "\"%s\""%self.Value |
2015 |
else: |
|
2016 |
return str(self.Value) |
|
2017 |
return None |
|
2018 |
||
2019 |
# Returns the RedrawRect |
|
2020 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
2021 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
2022 |
if self.StartConnected: |
|
2023 |
rect = rect.Union(self.StartConnected.GetRedrawRect(movex, movey)) |
|
2024 |
if self.EndConnected: |
|
2025 |
rect = rect.Union(self.EndConnected.GetRedrawRect(movex, movey)) |
|
2026 |
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)): |
|
2027 |
self.ValueSize = self.Parent.GetMiniTextExtent(self.ComputedValue) |
|
2028 |
if self.ValueSize is not None: |
|
2029 |
width, height = self.ValueSize |
|
2030 |
if self.BoundingBox[2] > width * 4 or self.BoundingBox[3] > height * 4: |
|
2031 |
x = self.Points[0].x + width * self.StartPoint[1][0] / 2 |
|
2032 |
y = self.Points[0].y + height * (self.StartPoint[1][1] - 1) |
|
2033 |
rect = rect.Union(wx.Rect(x, y, width, height)) |
|
2034 |
x = self.Points[-1].x + width * self.EndPoint[1][0] / 2 |
|
2035 |
y = self.Points[-1].y + height * (self.EndPoint[1][1] - 1) |
|
2036 |
rect = rect.Union(wx.Rect(x, y, width, height)) |
|
2037 |
else: |
|
2038 |
middle = len(self.Segments) / 2 + len(self.Segments) % 2 - 1 |
|
2039 |
x = (self.Points[middle].x + self.Points[middle + 1].x - width) / 2 |
|
2040 |
if self.BoundingBox[3] > height and self.Segments[middle] in [NORTH, SOUTH]: |
|
2041 |
y = (self.Points[middle].y + self.Points[middle + 1].y - height) / 2 |
|
2042 |
else: |
|
2043 |
y = self.Points[middle].y - height |
|
2044 |
rect = rect.Union(wx.Rect(x, y, width, height)) |
|
2045 |
return rect |
|
2046 |
||
2047 |
def Clone(self, parent, connectors = {}, dx = 0, dy = 0): |
|
2048 |
start_connector = connectors.get(self.StartConnected, None) |
|
2049 |
end_connector = connectors.get(self.EndConnected, None) |
|
2050 |
if start_connector is not None and end_connector is not None: |
|
2051 |
wire = Wire(parent) |
|
2052 |
wire.SetPoints([(point.x + dx, point.y + dy) for point in self.Points]) |
|
2053 |
start_connector.Connect((wire, 0), False) |
|
2054 |
end_connector.Connect((wire, -1), False) |
|
2055 |
wire.ConnectStartPoint(start_connector.GetPosition(), start_connector) |
|
2056 |
wire.ConnectEndPoint(end_connector.GetPosition(), end_connector) |
|
2057 |
return wire |
|
2058 |
return None |
|
2059 |
||
2060 |
# Forbids to change the wire position |
|
2061 |
def SetPosition(x, y): |
|
2062 |
pass |
|
2063 |
||
2064 |
# Forbids to change the wire size |
|
2065 |
def SetSize(width, height): |
|
2066 |
pass |
|
2067 |
||
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
2068 |
# Forbids to et size of the group elements to their minimum size |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
2069 |
pass |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
2070 |
|
814 | 2071 |
# Moves and Resizes the element for fitting scaling |
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
2072 |
def SetBestSize(self, scaling): |
814 | 2073 |
if scaling is not None: |
2074 |
movex_max = movey_max = 0 |
|
2075 |
for idx, point in enumerate(self.Points): |
|
2076 |
if 0 < idx < len(self.Points) - 1: |
|
2077 |
movex = round_scaling(point.x, scaling[0]) - point.x |
|
2078 |
movey = round_scaling(point.y, scaling[1]) - point.y |
|
2079 |
if idx == 1: |
|
2080 |
if self.Segments[0][0] == 0: |
|
2081 |
movex = 0 |
|
2082 |
elif (point.x + movex - self.Points[0].x) * self.Segments[0][0] < MIN_SEGMENT_SIZE: |
|
2083 |
movex = round_scaling(self.Points[0].x + MIN_SEGMENT_SIZE * self.Segments[0][0], scaling[0], self.Segments[0][0]) - point.x |
|
2084 |
if self.Segments[0][1] == 0: |
|
2085 |
movey = 0 |
|
2086 |
elif (point.y + movey - self.Points[0].y) * self.Segments[0][1] < MIN_SEGMENT_SIZE: |
|
2087 |
movey = round_scaling(self.Points[0].y + MIN_SEGMENT_SIZE * self.Segments[0][1], scaling[0], self.Segments[0][1]) - point.y |
|
2088 |
elif idx == len(self.Points) - 2: |
|
2089 |
if self.Segments[-1][0] == 0: |
|
2090 |
movex = 0 |
|
2091 |
elif (self.Points[-1].x - (point.x + movex)) * self.Segments[-1][0] < MIN_SEGMENT_SIZE: |
|
2092 |
movex = round_scaling(self.Points[-1].x + MIN_SEGMENT_SIZE * self.Segments[0][0], scaling[0], self.Segments[0][0]) - point.x |
|
2093 |
if self.Segments[-1][1] == 0: |
|
2094 |
movey = 0 |
|
2095 |
elif (self.Points[-1].y - (point.y + movey)) * self.Segments[-1][1] < MIN_SEGMENT_SIZE: |
|
2096 |
movey = round_scaling(self.Points[-1].y - MIN_SEGMENT_SIZE * self.Segments[-1][1], scaling[1], -self.Segments[-1][1]) - point.y |
|
2097 |
movex_max = max(movex_max, movex) |
|
2098 |
movey_max = max(movey_max, movey) |
|
2099 |
point.x += movex |
|
2100 |
point.y += movey |
|
2101 |
return movex_max, movey_max |
|
2102 |
return 0, 0 |
|
2103 |
||
2104 |
# Returns connector to which start point is connected |
|
2105 |
def GetStartConnected(self): |
|
2106 |
return self.StartConnected |
|
2107 |
||
2108 |
# Returns connector to which start point is connected |
|
2109 |
def GetStartConnectedType(self): |
|
2110 |
if self.StartConnected and not self.ComputingType: |
|
2111 |
self.ComputingType = True |
|
2112 |
computed_type = self.StartConnected.GetType() |
|
2113 |
self.ComputingType = False |
|
2114 |
return computed_type |
|
2115 |
return None |
|
2116 |
||
2117 |
# Returns connector to which end point is connected |
|
2118 |
def GetEndConnected(self): |
|
2119 |
return self.EndConnected |
|
2120 |
||
2121 |
# Returns connector to which end point is connected |
|
2122 |
def GetEndConnectedType(self): |
|
2123 |
if self.EndConnected and not self.ComputingType: |
|
2124 |
self.ComputingType = True |
|
2125 |
computed_type = self.EndConnected.GetType() |
|
2126 |
self.ComputingType = False |
|
2127 |
return computed_type |
|
2128 |
return None |
|
2129 |
||
2130 |
def GetConnectionDirection(self): |
|
2131 |
if self.StartConnected is None and self.EndConnected is None: |
|
2132 |
return None |
|
2133 |
elif self.StartConnected is not None and self.EndConnected is None: |
|
2134 |
return (-self.StartPoint[1][0], -self.StartPoint[1][1]) |
|
2135 |
elif self.StartConnected is None and self.EndConnected is not None: |
|
2136 |
return self.EndPoint |
|
2137 |
elif self.Handle is not None: |
|
2138 |
handle_type, handle = self.Handle |
|
2139 |
# A point has been handled |
|
2140 |
if handle_type == HANDLE_POINT: |
|
2141 |
if handle == 0: |
|
2142 |
return self.EndPoint |
|
2143 |
else: |
|
2144 |
return (-self.StartPoint[1][0], -self.StartPoint[1][1]) |
|
2145 |
return None |
|
2146 |
||
2147 |
def GetOtherConnected(self, connector): |
|
2148 |
if self.StartConnected == connector: |
|
2149 |
return self.EndConnected |
|
2150 |
else: |
|
2151 |
return self.StartConnected |
|
2152 |
||
2153 |
def GetOtherConnectedType(self, handle): |
|
2154 |
if handle == 0: |
|
2155 |
return self.GetEndConnectedType() |
|
2156 |
else: |
|
2157 |
return self.GetStartConnectedType() |
|
2158 |
||
2159 |
def IsConnectedCompatible(self): |
|
2160 |
if self.StartConnected: |
|
2161 |
return self.StartConnected.IsCompatible(self.GetEndConnectedType()) |
|
2162 |
elif self.EndConnected: |
|
2163 |
return True |
|
2164 |
return False |
|
2165 |
||
2166 |
def SetForced(self, forced): |
|
2167 |
if self.Forced != forced: |
|
2168 |
self.Forced = forced |
|
2169 |
if self.StartConnected: |
|
2170 |
self.StartConnected.RefreshForced() |
|
2171 |
if self.EndConnected: |
|
2172 |
self.EndConnected.RefreshForced() |
|
2173 |
if self.Visible: |
|
2174 |
self.Parent.ElementNeedRefresh(self) |
|
2175 |
||
2176 |
def SetValue(self, value): |
|
2177 |
if self.Value != value: |
|
2178 |
self.Value = value |
|
2179 |
if value is not None and not isinstance(value, BooleanType): |
|
878
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2180 |
wire_type = self.GetEndConnectedType() |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2181 |
if wire_type == "STRING": |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2182 |
self.ComputedValue = "'%s'"%value |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
877
diff
changeset
|
2183 |
elif wire_type == "WSTRING": |
814 | 2184 |
self.ComputedValue = "\"%s\""%value |
2185 |
else: |
|
2186 |
self.ComputedValue = str(value) |
|
2187 |
if self.ToolTip is not None: |
|
2188 |
self.ToolTip.SetTip(self.ComputedValue) |
|
2189 |
if len(self.ComputedValue) > 4: |
|
2190 |
self.ComputedValue = self.ComputedValue[:4] + "..." |
|
2191 |
self.ValueSize = None |
|
2192 |
if self.StartConnected: |
|
2193 |
self.StartConnected.RefreshValue() |
|
2194 |
if self.EndConnected: |
|
2195 |
self.EndConnected.RefreshValue() |
|
2196 |
if self.Visible: |
|
2197 |
self.Parent.ElementNeedRefresh(self) |
|
2198 |
if isinstance(value, BooleanType) and self.StartConnected is not None: |
|
2199 |
block = self.StartConnected.GetParentBlock() |
|
2200 |
block.SpreadCurrent() |
|
2201 |
||
2202 |
# Unconnect the start and end points |
|
2203 |
def Clean(self): |
|
2204 |
if self.StartConnected: |
|
2205 |
self.UnConnectStartPoint() |
|
2206 |
if self.EndConnected: |
|
2207 |
self.UnConnectEndPoint() |
|
2208 |
||
2209 |
# Delete this wire by calling the corresponding method |
|
2210 |
def Delete(self): |
|
2211 |
self.Parent.DeleteWire(self) |
|
2212 |
||
2213 |
# Select a segment and not the whole wire. It's useful for Ladder Diagram |
|
2214 |
def SetSelectedSegment(self, segment): |
|
2215 |
# The last segment is indicated |
|
2216 |
if segment == -1: |
|
2217 |
segment = len(self.Segments) - 1 |
|
2218 |
# The selected segment is reinitialised |
|
2219 |
if segment == None: |
|
2220 |
if self.StartConnected: |
|
2221 |
self.StartConnected.SetSelected(False) |
|
2222 |
if self.EndConnected: |
|
2223 |
self.EndConnected.SetSelected(False) |
|
2224 |
# The segment selected is the first |
|
2225 |
elif segment == 0: |
|
2226 |
if self.StartConnected: |
|
2227 |
self.StartConnected.SetSelected(True) |
|
2228 |
if self.EndConnected: |
|
2229 |
# There is only one segment |
|
2230 |
if len(self.Segments) == 1: |
|
2231 |
self.EndConnected.SetSelected(True) |
|
2232 |
else: |
|
2233 |
self.EndConnected.SetSelected(False) |
|
2234 |
# The segment selected is the last |
|
2235 |
elif segment == len(self.Segments) - 1: |
|
2236 |
if self.StartConnected: |
|
2237 |
self.StartConnected.SetSelected(False) |
|
2238 |
if self.EndConnected: |
|
2239 |
self.EndConnected.SetSelected(True) |
|
2240 |
self.SelectedSegment = segment |
|
2241 |
self.Refresh() |
|
2242 |
||
2243 |
def SetValid(self, valid): |
|
2244 |
self.Valid = valid |
|
2245 |
if self.StartConnected: |
|
2246 |
self.StartConnected.RefreshValid() |
|
2247 |
if self.EndConnected: |
|
2248 |
self.EndConnected.RefreshValid() |
|
2249 |
||
2250 |
def GetValid(self): |
|
2251 |
return self.Valid |
|
2252 |
||
2253 |
# Reinitialize the wire points |
|
2254 |
def ResetPoints(self): |
|
2255 |
if self.StartPoint and self.EndPoint: |
|
2256 |
self.Points = [self.StartPoint[0], self.EndPoint[0]] |
|
2257 |
self.Segments = [self.StartPoint[1]] |
|
2258 |
else: |
|
2259 |
self.Points = [] |
|
2260 |
self.Segments = [] |
|
2261 |
||
2262 |
# Refresh the wire bounding box |
|
2263 |
def RefreshBoundingBox(self): |
|
2264 |
if len(self.Points) > 0: |
|
2265 |
# If startpoint or endpoint is connected, save the point radius |
|
2266 |
start_radius = end_radius = 0 |
|
2267 |
if not self.StartConnected: |
|
2268 |
start_radius = POINT_RADIUS |
|
2269 |
if not self.EndConnected: |
|
2270 |
end_radius = POINT_RADIUS |
|
2271 |
# Initialize minimum and maximum from the first point |
|
2272 |
minx, minbbxx = self.Points[0].x, self.Points[0].x - start_radius |
|
2273 |
maxx, maxbbxx = self.Points[0].x, self.Points[0].x + start_radius |
|
2274 |
miny, minbbxy = self.Points[0].y, self.Points[0].y - start_radius |
|
2275 |
maxy, maxbbxy = self.Points[0].y, self.Points[0].y + start_radius |
|
2276 |
# Actualize minimum and maximum with the other points |
|
2277 |
for point in self.Points[1:-1]: |
|
2278 |
minx, minbbxx = min(minx, point.x), min(minbbxx, point.x) |
|
2279 |
maxx, maxbbxx = max(maxx, point.x), max(maxbbxx, point.x) |
|
2280 |
miny, minbbxy = min(miny, point.y), min(minbbxy, point.y) |
|
2281 |
maxy, maxbbxy = max(maxy, point.y), max(maxbbxy, point.y) |
|
2282 |
if len(self.Points) > 1: |
|
2283 |
minx, minbbxx = min(minx, self.Points[-1].x), min(minbbxx, self.Points[-1].x - end_radius) |
|
2284 |
maxx, maxbbxx = max(maxx, self.Points[-1].x), max(maxbbxx, self.Points[-1].x + end_radius) |
|
2285 |
miny, minbbxy = min(miny, self.Points[-1].y), min(minbbxy, self.Points[-1].y - end_radius) |
|
2286 |
maxy, maxbbxy = max(maxy, self.Points[-1].y), max(maxbbxy, self.Points[-1].y + end_radius) |
|
2287 |
self.Pos.x, self.Pos.y = minx, miny |
|
2288 |
self.Size = wx.Size(maxx - minx, maxy - miny) |
|
2289 |
self.BoundingBox = wx.Rect(minbbxx, minbbxy, maxbbxx - minbbxx + 1, maxbbxy - minbbxy + 1) |
|
2290 |
||
2291 |
# Refresh the realpoints that permits to keep the proportionality in wire during resizing |
|
2292 |
def RefreshRealPoints(self): |
|
2293 |
if len(self.Points) > 0: |
|
2294 |
self.RealPoints = [] |
|
2295 |
# Calculate float relative position of each point with the minimum point |
|
2296 |
for point in self.Points: |
|
2297 |
self.RealPoints.append([float(point.x - self.Pos.x), float(point.y - self.Pos.y)]) |
|
2298 |
||
2299 |
# Returns the wire minimum size |
|
2300 |
def GetMinSize(self): |
|
2301 |
width = 1 |
|
2302 |
height = 1 |
|
2303 |
dir_product = product(self.StartPoint[1], self.EndPoint[1]) |
|
2304 |
# The directions are opposed |
|
2305 |
if dir_product < 0: |
|
2306 |
if self.StartPoint[0] != 0: |
|
2307 |
width = MIN_SEGMENT_SIZE * 2 |
|
2308 |
if self.StartPoint[1] != 0: |
|
2309 |
height = MIN_SEGMENT_SIZE * 2 |
|
2310 |
# The directions are the same |
|
2311 |
elif dir_product > 0: |
|
2312 |
if self.StartPoint[0] != 0: |
|
2313 |
width = MIN_SEGMENT_SIZE |
|
2314 |
if self.StartPoint[1] != 0: |
|
2315 |
height = MIN_SEGMENT_SIZE |
|
2316 |
# The directions are perpendiculars |
|
2317 |
else: |
|
2318 |
width = MIN_SEGMENT_SIZE |
|
2319 |
height = MIN_SEGMENT_SIZE |
|
2320 |
return width + 1, height + 1 |
|
2321 |
||
2322 |
# Returns if the point given is on one of the wire segments |
|
2323 |
def HitTest(self, pt, connectors=True): |
|
2324 |
test = False |
|
2325 |
for i in xrange(len(self.Points) - 1): |
|
2326 |
rect = wx.Rect(0, 0, 0, 0) |
|
2327 |
if i == 0 and self.StartConnected is not None: |
|
2328 |
x1 = self.Points[i].x - self.Segments[0][0] * CONNECTOR_SIZE |
|
2329 |
y1 = self.Points[i].y - self.Segments[0][1] * CONNECTOR_SIZE |
|
2330 |
else: |
|
2331 |
x1, y1 = self.Points[i].x, self.Points[i].y |
|
2332 |
if i == len(self.Points) - 2 and self.EndConnected is not None: |
|
2333 |
x2 = self.Points[i + 1].x + self.Segments[-1][0] * CONNECTOR_SIZE |
|
2334 |
y2 = self.Points[i + 1].y + self.Segments[-1][1] * CONNECTOR_SIZE |
|
2335 |
else: |
|
2336 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
2337 |
# Calculate a rectangle around the segment |
|
2338 |
rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
|
2339 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
|
2340 |
test |= rect.InsideXY(pt.x, pt.y) |
|
2341 |
return test |
|
2342 |
||
2343 |
# Returns the wire start or end point if the point given is on one of them |
|
2344 |
def TestPoint(self, pt): |
|
2345 |
# Test the wire start point |
|
2346 |
rect = wx.Rect(self.Points[0].x - ANCHOR_DISTANCE, self.Points[0].y - ANCHOR_DISTANCE, |
|
2347 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
|
2348 |
if rect.InsideXY(pt.x, pt.y): |
|
2349 |
return 0 |
|
2350 |
# Test the wire end point |
|
2351 |
if len(self.Points) > 1: |
|
2352 |
rect = wx.Rect(self.Points[-1].x - ANCHOR_DISTANCE, self.Points[-1].y - ANCHOR_DISTANCE, |
|
2353 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
|
2354 |
if rect.InsideXY(pt.x, pt.y): |
|
2355 |
return -1 |
|
2356 |
return None |
|
2357 |
||
2358 |
# Returns the wire segment if the point given is on it |
|
2359 |
def TestSegment(self, pt, all=False): |
|
2360 |
for i in xrange(len(self.Segments)): |
|
2361 |
# If wire is not in a Ladder Diagram, first and last segments are excluded |
|
2362 |
if all or 0 < i < len(self.Segments) - 1: |
|
2363 |
x1, y1 = self.Points[i].x, self.Points[i].y |
|
2364 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
2365 |
# Calculate a rectangle around the segment |
|
2366 |
rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
|
2367 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
|
2368 |
if rect.InsideXY(pt.x, pt.y): |
|
2369 |
return i, self.Segments[i] |
|
2370 |
return None |
|
2371 |
||
2372 |
# Define the wire points |
|
2373 |
def SetPoints(self, points, verify=True): |
|
2374 |
if len(points) > 1: |
|
2375 |
self.Points = [wx.Point(x, y) for x, y in points] |
|
2376 |
# Calculate the start and end directions |
|
2377 |
self.StartPoint = [None, vector(self.Points[0], self.Points[1])] |
|
2378 |
self.EndPoint = [None, vector(self.Points[-1], self.Points[-2])] |
|
2379 |
# Calculate the start and end points |
|
2380 |
self.StartPoint[0] = wx.Point(self.Points[0].x + CONNECTOR_SIZE * self.StartPoint[1][0], |
|
2381 |
self.Points[0].y + CONNECTOR_SIZE * self.StartPoint[1][1]) |
|
2382 |
self.EndPoint[0] = wx.Point(self.Points[-1].x + CONNECTOR_SIZE * self.EndPoint[1][0], |
|
2383 |
self.Points[-1].y + CONNECTOR_SIZE * self.EndPoint[1][1]) |
|
2384 |
self.Points[0] = self.StartPoint[0] |
|
2385 |
self.Points[-1] = self.EndPoint[0] |
|
2386 |
# Calculate the segments directions |
|
2387 |
self.Segments = [] |
|
2388 |
i = 0 |
|
2389 |
while i < len(self.Points) - 1: |
|
2390 |
if verify and 0 < i < len(self.Points) - 2 and \ |
|
2391 |
self.Points[i] == self.Points[i + 1] and \ |
|
2392 |
self.Segments[-1] == vector(self.Points[i + 1], self.Points[i + 2]): |
|
2393 |
for j in xrange(2): |
|
2394 |
self.Points.pop(i) |
|
2395 |
else: |
|
2396 |
segment = vector(self.Points[i], self.Points[i + 1]) |
|
2397 |
if is_null_vector(segment) and i > 0: |
|
2398 |
segment = (self.Segments[-1][1], self.Segments[-1][0]) |
|
2399 |
if i < len(self.Points) - 2: |
|
2400 |
next = vector(self.Points[i + 1], self.Points[i + 2]) |
|
2401 |
if next == segment or is_null_vector(add_vectors(segment, next)): |
|
2402 |
self.Points.insert(i + 1, wx.Point(self.Points[i + 1].x, self.Points[i + 1].y)) |
|
2403 |
self.Segments.append(segment) |
|
2404 |
i += 1 |
|
2405 |
self.RefreshBoundingBox() |
|
2406 |
self.RefreshRealPoints() |
|
2407 |
||
2408 |
# Returns the position of the point indicated |
|
2409 |
def GetPoint(self, index): |
|
2410 |
if index < len(self.Points): |
|
2411 |
return self.Points[index].x, self.Points[index].y |
|
2412 |
return None |
|
2413 |
||
2414 |
# Returns a list of the position of all wire points |
|
2415 |
def GetPoints(self, invert = False): |
|
2416 |
points = self.VerifyPoints() |
|
2417 |
points[0] = wx.Point(points[0].x - CONNECTOR_SIZE * self.StartPoint[1][0], |
|
2418 |
points[0].y - CONNECTOR_SIZE * self.StartPoint[1][1]) |
|
2419 |
points[-1] = wx.Point(points[-1].x - CONNECTOR_SIZE * self.EndPoint[1][0], |
|
2420 |
points[-1].y - CONNECTOR_SIZE * self.EndPoint[1][1]) |
|
2421 |
# An inversion of the list is asked |
|
2422 |
if invert: |
|
2423 |
points.reverse() |
|
2424 |
return points |
|
2425 |
||
2426 |
# Returns the position of the two selected segment points |
|
2427 |
def GetSelectedSegmentPoints(self): |
|
2428 |
if self.SelectedSegment != None and len(self.Points) > 1: |
|
2429 |
return self.Points[self.SelectedSegment:self.SelectedSegment + 2] |
|
2430 |
return [] |
|
2431 |
||
2432 |
# Returns if the selected segment is the first and/or the last of the wire |
|
2433 |
def GetSelectedSegmentConnections(self): |
|
2434 |
if self.SelectedSegment != None and len(self.Points) > 1: |
|
2435 |
return self.SelectedSegment == 0, self.SelectedSegment == len(self.Segments) - 1 |
|
2436 |
return (True, True) |
|
2437 |
||
2438 |
# Returns the connectors on which the wire is connected |
|
2439 |
def GetConnected(self): |
|
2440 |
connected = [] |
|
2441 |
if self.StartConnected and self.StartPoint[1] == WEST: |
|
2442 |
connected.append(self.StartConnected) |
|
2443 |
if self.EndConnected and self.EndPoint[1] == WEST: |
|
2444 |
connected.append(self.EndConnected) |
|
2445 |
return connected |
|
2446 |
||
2447 |
# Returns the id of the block connected to the first or the last wire point |
|
2448 |
def GetConnectedInfos(self, index): |
|
2449 |
if index == 0 and self.StartConnected: |
|
2450 |
return self.StartConnected.GetBlockId(), self.StartConnected.GetName() |
|
2451 |
elif index == -1 and self.EndConnected: |
|
2452 |
return self.EndConnected.GetBlockId(), self.EndConnected.GetName() |
|
2453 |
return None |
|
2454 |
||
2455 |
# Update the wire points position by keeping at most possible the current positions |
|
2456 |
def GeneratePoints(self, realpoints = True): |
|
2457 |
i = 0 |
|
2458 |
# Calculate the start enad end points with the minimum segment size in the right direction |
|
2459 |
end = wx.Point(self.EndPoint[0].x + self.EndPoint[1][0] * MIN_SEGMENT_SIZE, |
|
2460 |
self.EndPoint[0].y + self.EndPoint[1][1] * MIN_SEGMENT_SIZE) |
|
2461 |
start = wx.Point(self.StartPoint[0].x + self.StartPoint[1][0] * MIN_SEGMENT_SIZE, |
|
2462 |
self.StartPoint[0].y + self.StartPoint[1][1] * MIN_SEGMENT_SIZE) |
|
2463 |
# Evaluate the point till it's the last |
|
2464 |
while i < len(self.Points) - 1: |
|
2465 |
# The next point is the last |
|
2466 |
if i + 1 == len(self.Points) - 1: |
|
2467 |
# Calculate the direction from current point to end point |
|
2468 |
v_end = vector(self.Points[i], end) |
|
2469 |
# The current point is the first |
|
2470 |
if i == 0: |
|
2471 |
# If the end point is not in the start direction, a point is added |
|
2472 |
if v_end != self.Segments[0] or v_end == self.EndPoint[1]: |
|
2473 |
self.Points.insert(1, wx.Point(start.x, start.y)) |
|
2474 |
self.Segments.insert(1, DirectionChoice((self.Segments[0][1], |
|
2475 |
self.Segments[0][0]), v_end, self.EndPoint[1])) |
|
2476 |
# The current point is the second |
|
2477 |
elif i == 1: |
|
2478 |
# The previous direction and the target direction are mainly opposed, a point is added |
|
2479 |
if product(v_end, self.Segments[0]) < 0: |
|
2480 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
|
2481 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
|
2482 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
2483 |
# The previous direction and the end direction are the same or they are |
|
2484 |
# perpendiculars and the end direction points towards current segment |
|
2485 |
elif product(self.Segments[0], self.EndPoint[1]) >= 0 and product(self.Segments[1], self.EndPoint[1]) <= 0: |
|
2486 |
# Current point and end point are aligned |
|
2487 |
if self.Segments[0][0] != 0: |
|
2488 |
self.Points[1].x = end.x |
|
2489 |
if self.Segments[0][1] != 0: |
|
2490 |
self.Points[1].y = end.y |
|
2491 |
# If the previous direction and the end direction are the same, a point is added |
|
2492 |
if product(self.Segments[0], self.EndPoint[1]) > 0: |
|
2493 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
|
2494 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
|
2495 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
2496 |
else: |
|
2497 |
# Current point is positioned in the middle of start point |
|
2498 |
# and end point on the current direction and a point is added |
|
2499 |
if self.Segments[0][0] != 0: |
|
2500 |
self.Points[1].x = (end.x + start.x) / 2 |
|
2501 |
if self.Segments[0][1] != 0: |
|
2502 |
self.Points[1].y = (end.y + start.y) / 2 |
|
2503 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
|
2504 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
|
2505 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
2506 |
else: |
|
2507 |
# The previous direction and the end direction are perpendiculars |
|
2508 |
if product(self.Segments[i - 1], self.EndPoint[1]) == 0: |
|
2509 |
# The target direction and the end direction aren't mainly the same |
|
2510 |
if product(v_end, self.EndPoint[1]) <= 0: |
|
2511 |
# Current point and end point are aligned |
|
2512 |
if self.Segments[i - 1][0] != 0: |
|
2513 |
self.Points[i].x = end.x |
|
2514 |
if self.Segments[i - 1][1] != 0: |
|
2515 |
self.Points[i].y = end.y |
|
2516 |
# Previous direction is updated from the new point |
|
2517 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
2518 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
2519 |
else: |
|
2520 |
test = True |
|
2521 |
# If the current point is the third, test if the second |
|
2522 |
# point can be aligned with the end point |
|
2523 |
if i == 2: |
|
2524 |
test_point = wx.Point(self.Points[1].x, self.Points[1].y) |
|
2525 |
if self.Segments[1][0] != 0: |
|
2526 |
test_point.y = end.y |
|
2527 |
if self.Segments[1][1] != 0: |
|
2528 |
test_point.x = end.x |
|
2529 |
vector_test = vector(self.Points[0], test_point, False) |
|
2530 |
test = norm(vector_test) > MIN_SEGMENT_SIZE and product(self.Segments[0], vector_test) > 0 |
|
2531 |
# The previous point can be aligned |
|
2532 |
if test: |
|
2533 |
self.Points[i].x, self.Points[i].y = end.x, end.y |
|
2534 |
if self.Segments[i - 1][0] != 0: |
|
2535 |
self.Points[i - 1].y = end.y |
|
2536 |
if self.Segments[i - 1][1] != 0: |
|
2537 |
self.Points[i - 1].x = end.x |
|
2538 |
self.Segments[i] = (-self.EndPoint[1][0], -self.EndPoint[1][1]) |
|
2539 |
else: |
|
2540 |
# Current point is positioned in the middle of previous point |
|
2541 |
# and end point on the current direction and a point is added |
|
2542 |
if self.Segments[1][0] != 0: |
|
2543 |
self.Points[2].x = (self.Points[1].x + end.x) / 2 |
|
2544 |
if self.Segments[1][1] != 0: |
|
2545 |
self.Points[2].y = (self.Points[1].y + end.y) / 2 |
|
2546 |
self.Points.insert(3, wx.Point(self.Points[2].x, self.Points[2].y)) |
|
2547 |
self.Segments.insert(3, DirectionChoice((self.Segments[2][1], |
|
2548 |
self.Segments[2][0]), v_end, self.EndPoint[1])) |
|
2549 |
else: |
|
2550 |
# Current point is aligned with end point |
|
2551 |
if self.Segments[i - 1][0] != 0: |
|
2552 |
self.Points[i].x = end.x |
|
2553 |
if self.Segments[i - 1][1] != 0: |
|
2554 |
self.Points[i].y = end.y |
|
2555 |
# Previous direction is updated from the new point |
|
2556 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
2557 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
2558 |
# If previous direction and end direction are opposed |
|
2559 |
if product(self.Segments[i - 1], self.EndPoint[1]) < 0: |
|
2560 |
# Current point is positioned in the middle of previous point |
|
2561 |
# and end point on the current direction |
|
2562 |
if self.Segments[i - 1][0] != 0: |
|
2563 |
self.Points[i].x = (end.x + self.Points[i - 1].x) / 2 |
|
2564 |
if self.Segments[i - 1][1] != 0: |
|
2565 |
self.Points[i].y = (end.y + self.Points[i - 1].y) / 2 |
|
2566 |
# A point is added |
|
2567 |
self.Points.insert(i + 1, wx.Point(self.Points[i].x, self.Points[i].y)) |
|
2568 |
self.Segments.insert(i + 1, DirectionChoice((self.Segments[i][1], |
|
2569 |
self.Segments[i][0]), v_end, self.EndPoint[1])) |
|
2570 |
else: |
|
2571 |
# Current point is the first, and second is not mainly in the first direction |
|
2572 |
if i == 0 and product(vector(start, self.Points[1]), self.Segments[0]) < 0: |
|
2573 |
# If first and second directions aren't perpendiculars, a point is added |
|
2574 |
if product(self.Segments[0], self.Segments[1]) != 0: |
|
2575 |
self.Points.insert(1, wx.Point(start.x, start.y)) |
|
2576 |
self.Segments.insert(1, DirectionChoice((self.Segments[0][1], |
|
2577 |
self.Segments[0][0]), vector(start, self.Points[1]), self.Segments[1])) |
|
2578 |
else: |
|
2579 |
self.Points[1].x, self.Points[1].y = start.x, start.y |
|
2580 |
else: |
|
2581 |
# Next point is aligned with current point |
|
2582 |
if self.Segments[i][0] != 0: |
|
2583 |
self.Points[i + 1].y = self.Points[i].y |
|
2584 |
if self.Segments[i][1] != 0: |
|
2585 |
self.Points[i + 1].x = self.Points[i].x |
|
2586 |
# Current direction is updated from the new point |
|
2587 |
if product(vector(self.Points[i], self.Points[i + 1]), self.Segments[i]) < 0: |
|
2588 |
self.Segments[i] = (-self.Segments[i][0], -self.Segments[i][1]) |
|
2589 |
i += 1 |
|
2590 |
self.RefreshBoundingBox() |
|
2591 |
if realpoints: |
|
2592 |
self.RefreshRealPoints() |
|
2593 |
||
2594 |
# Verify that two consecutive points haven't the same position |
|
2595 |
def VerifyPoints(self): |
|
2596 |
points = [point for point in self.Points] |
|
2597 |
segments = [segment for segment in self.Segments] |
|
2598 |
i = 1 |
|
2599 |
while i < len(points) - 1: |
|
2600 |
if points[i] == points[i + 1] and segments[i - 1] == segments[i + 1]: |
|
2601 |
for j in xrange(2): |
|
2602 |
points.pop(i) |
|
2603 |
segments.pop(i) |
|
2604 |
else: |
|
2605 |
i += 1 |
|
2606 |
# If the wire isn't in a Ladder Diagram, save the new point list |
|
2607 |
if self.Parent.__class__.__name__ != "LD_Viewer": |
|
2608 |
self.Points = [point for point in points] |
|
2609 |
self.Segments = [segment for segment in segments] |
|
2610 |
self.RefreshBoundingBox() |
|
2611 |
self.RefreshRealPoints() |
|
2612 |
return points |
|
2613 |
||
2614 |
# Moves all the wire points except the first and the last if they are connected |
|
2615 |
def Move(self, dx, dy, endpoints = False): |
|
2616 |
for i, point in enumerate(self.Points): |
|
2617 |
if endpoints or not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2618 |
point.x += dx |
|
2619 |
point.y += dy |
|
2620 |
self.StartPoint[0] = self.Points[0] |
|
2621 |
self.EndPoint[0] = self.Points[-1] |
|
2622 |
self.GeneratePoints() |
|
2623 |
||
2624 |
# Resize the wire from position and size given |
|
2625 |
def Resize(self, x, y, width, height): |
|
2626 |
if len(self.Points) > 1: |
|
2627 |
# Calculate the new position of each point for testing the new size |
|
2628 |
minx, miny = self.Pos.x, self.Pos.y |
|
2629 |
lastwidth, lastheight = self.Size.width, self.Size.height |
|
2630 |
for i, point in enumerate(self.RealPoints): |
|
2631 |
# If start or end point is connected, it's not calculate |
|
2632 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2633 |
if i == 0: |
|
2634 |
dir = self.StartPoint[1] |
|
2635 |
elif i == len(self.Points) - 1: |
|
2636 |
dir = self.EndPoint[1] |
|
2637 |
else: |
|
2638 |
dir = (0, 0) |
|
2639 |
pointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0] * width / float(max(lastwidth, 1)))), |
|
2640 |
width - dir[0] * MIN_SEGMENT_SIZE)) |
|
2641 |
pointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1] * height / float(max(lastheight, 1)))), |
|
2642 |
height - dir[1] * MIN_SEGMENT_SIZE)) |
|
2643 |
self.Points[i] = wx.Point(minx + x + pointx, miny + y + pointy) |
|
2644 |
self.StartPoint[0] = self.Points[0] |
|
2645 |
self.EndPoint[0] = self.Points[-1] |
|
2646 |
self.GeneratePoints(False) |
|
2647 |
# Test if the wire position or size have changed |
|
2648 |
if x != 0 and minx == self.Pos.x: |
|
2649 |
x = 0 |
|
2650 |
width = lastwidth |
|
2651 |
if y != 0 and miny == self.Pos.y: |
|
2652 |
y = 0 |
|
2653 |
height = lastwidth |
|
2654 |
if width != lastwidth and lastwidth == self.Size.width: |
|
2655 |
width = lastwidth |
|
2656 |
if height != lastheight and lastheight == self.Size.height: |
|
2657 |
height = lastheight |
|
2658 |
# Calculate the real points from the new size, it's important for |
|
2659 |
# keeping a proportionality in the points position with the size |
|
2660 |
# during a resize dragging |
|
2661 |
for i, point in enumerate(self.RealPoints): |
|
2662 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2663 |
point[0] = point[0] * width / float(max(lastwidth, 1)) |
|
2664 |
point[1] = point[1] * height / float(max(lastheight, 1)) |
|
2665 |
# Calculate the correct position of the points from real points |
|
2666 |
for i, point in enumerate(self.RealPoints): |
|
2667 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2668 |
if i == 0: |
|
2669 |
dir = self.StartPoint[1] |
|
2670 |
elif i == len(self.Points) - 1: |
|
2671 |
dir = self.EndPoint[1] |
|
2672 |
else: |
|
2673 |
dir = (0, 0) |
|
2674 |
realpointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0])), |
|
2675 |
width - dir[0] * MIN_SEGMENT_SIZE)) |
|
2676 |
realpointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1])), |
|
2677 |
height - dir[1] * MIN_SEGMENT_SIZE)) |
|
2678 |
self.Points[i] = wx.Point(minx + x + realpointx, miny + y + realpointy) |
|
2679 |
self.StartPoint[0] = self.Points[0] |
|
2680 |
self.EndPoint[0] = self.Points[-1] |
|
2681 |
self.GeneratePoints(False) |
|
2682 |
||
2683 |
# Moves the wire start point and update the wire points |
|
2684 |
def MoveStartPoint(self, point): |
|
2685 |
if len(self.Points) > 1: |
|
2686 |
self.StartPoint[0] = point |
|
2687 |
self.Points[0] = point |
|
2688 |
self.GeneratePoints() |
|
2689 |
||
2690 |
# Changes the wire start direction and update the wire points |
|
2691 |
def SetStartPointDirection(self, dir): |
|
2692 |
if len(self.Points) > 1: |
|
2693 |
self.StartPoint[1] = dir |
|
2694 |
self.Segments[0] = dir |
|
2695 |
self.GeneratePoints() |
|
2696 |
||
2697 |
# Rotates the wire start direction by an angle of 90 degrees anticlockwise |
|
2698 |
def RotateStartPoint(self): |
|
2699 |
self.SetStartPointDirection((self.StartPoint[1][1], -self.StartPoint[1][0])) |
|
2700 |
||
2701 |
# Connects wire start point to the connector given and moves wire start point |
|
2702 |
# to given point |
|
2703 |
def ConnectStartPoint(self, point, connector): |
|
2704 |
if point: |
|
2705 |
self.MoveStartPoint(point) |
|
2706 |
self.StartConnected = connector |
|
2707 |
self.RefreshBoundingBox() |
|
2708 |
||
2709 |
# Unconnects wire start point |
|
2710 |
def UnConnectStartPoint(self, delete = False): |
|
2711 |
if delete: |
|
2712 |
self.StartConnected = None |
|
2713 |
self.Delete() |
|
2714 |
elif self.StartConnected: |
|
2715 |
self.StartConnected.UnConnect(self, unconnect = False) |
|
2716 |
self.StartConnected = None |
|
2717 |
self.RefreshBoundingBox() |
|
2718 |
||
2719 |
# Moves the wire end point and update the wire points |
|
2720 |
def MoveEndPoint(self, point): |
|
2721 |
if len(self.Points) > 1: |
|
2722 |
self.EndPoint[0] = point |
|
2723 |
self.Points[-1] = point |
|
2724 |
self.GeneratePoints() |
|
2725 |
||
2726 |
# Changes the wire end direction and update the wire points |
|
2727 |
def SetEndPointDirection(self, dir): |
|
2728 |
if len(self.Points) > 1: |
|
2729 |
self.EndPoint[1] = dir |
|
2730 |
self.GeneratePoints() |
|
2731 |
||
2732 |
# Rotates the wire end direction by an angle of 90 degrees anticlockwise |
|
2733 |
def RotateEndPoint(self): |
|
2734 |
self.SetEndPointDirection((self.EndPoint[1][1], -self.EndPoint[1][0])) |
|
2735 |
||
2736 |
# Connects wire end point to the connector given and moves wire end point |
|
2737 |
# to given point |
|
2738 |
def ConnectEndPoint(self, point, connector): |
|
2739 |
if point: |
|
2740 |
self.MoveEndPoint(point) |
|
2741 |
self.EndConnected = connector |
|
2742 |
self.RefreshBoundingBox() |
|
2743 |
||
2744 |
# Unconnects wire end point |
|
2745 |
def UnConnectEndPoint(self, delete = False): |
|
2746 |
if delete: |
|
2747 |
self.EndConnected = None |
|
2748 |
self.Delete() |
|
2749 |
elif self.EndConnected: |
|
2750 |
self.EndConnected.UnConnect(self, unconnect = False) |
|
2751 |
self.EndConnected = None |
|
2752 |
self.RefreshBoundingBox() |
|
2753 |
||
2754 |
# Moves the wire segment given by its index |
|
2755 |
def MoveSegment(self, idx, movex, movey, scaling): |
|
2756 |
if 0 < idx < len(self.Segments) - 1: |
|
2757 |
if self.Segments[idx] in (NORTH, SOUTH): |
|
2758 |
start_x = self.Points[idx].x |
|
2759 |
if scaling is not None: |
|
2760 |
movex = round_scaling(self.Points[idx].x + movex, scaling[0]) - self.Points[idx].x |
|
2761 |
if idx == 1 and (self.Points[1].x + movex - self.Points[0].x) * self.Segments[0][0] < MIN_SEGMENT_SIZE: |
|
2762 |
movex = round_scaling(self.Points[0].x + MIN_SEGMENT_SIZE * self.Segments[0][0], scaling[0], self.Segments[0][0]) - self.Points[idx].x |
|
2763 |
elif idx == len(self.Segments) - 2 and (self.Points[-1].x - (self.Points[-2].x + movex)) * self.Segments[-1][0] < MIN_SEGMENT_SIZE: |
|
2764 |
movex = round_scaling(self.Points[-1].x - MIN_SEGMENT_SIZE * self.Segments[-1][0], scaling[0], -self.Segments[-1][0]) - self.Points[idx].x |
|
2765 |
self.Points[idx].x += movex |
|
2766 |
self.Points[idx + 1].x += movex |
|
2767 |
self.GeneratePoints() |
|
2768 |
if start_x != self.Points[idx].x: |
|
2769 |
return self.Points[idx].x - start_x, 0 |
|
2770 |
elif self.Segments[idx] in (EAST, WEST): |
|
2771 |
start_y = self.Points[idx].y |
|
2772 |
if scaling is not None: |
|
2773 |
movey = round_scaling(self.Points[idx].y + movey, scaling[1]) - self.Points[idx].y |
|
2774 |
if idx == 1 and (self.Points[1].y + movey - self.Points[0].y) * self.Segments[0][1] < MIN_SEGMENT_SIZE: |
|
2775 |
movex = round_scaling(self.Points[0].y + MIN_SEGMENT_SIZE * self.Segments[0][1], scaling[0], self.Segments[0][1]) - self.Points[idx].y |
|
2776 |
elif idx == len(self.Segments) - 2 and (self.Points[-1].y - (self.Points[-2].y + movey)) * self.Segments[-1][1] < MIN_SEGMENT_SIZE: |
|
2777 |
movey = round_scaling(self.Points[idx].y - MIN_SEGMENT_SIZE * self.Segments[-1][1], scaling[1], -self.Segments[-1][1]) - self.Points[idx].y |
|
2778 |
self.Points[idx].y += movey |
|
2779 |
self.Points[idx + 1].y += movey |
|
2780 |
self.GeneratePoints() |
|
2781 |
if start_y != self.Points[idx].y: |
|
2782 |
return 0, self.Points[idx].y - start_y |
|
2783 |
return 0, 0 |
|
2784 |
||
2785 |
# Adds two points in the middle of the handled segment |
|
2786 |
def AddSegment(self): |
|
2787 |
handle_type, handle = self.Handle |
|
2788 |
if handle_type == HANDLE_SEGMENT: |
|
2789 |
segment, dir = handle |
|
2790 |
if len(self.Segments) > 1: |
|
2791 |
pointx = self.Points[segment].x |
|
2792 |
pointy = self.Points[segment].y |
|
2793 |
if dir[0] != 0: |
|
2794 |
pointx = (self.Points[segment].x + self.Points[segment + 1].x) / 2 |
|
2795 |
if dir[1] != 0: |
|
2796 |
pointy = (self.Points[segment].y + self.Points[segment + 1].y) / 2 |
|
2797 |
self.Points.insert(segment + 1, wx.Point(pointx, pointy)) |
|
2798 |
self.Segments.insert(segment + 1, (dir[1], dir[0])) |
|
2799 |
self.Points.insert(segment + 2, wx.Point(pointx, pointy)) |
|
2800 |
self.Segments.insert(segment + 2, dir) |
|
2801 |
else: |
|
2802 |
p1x = p2x = self.Points[segment].x |
|
2803 |
p1y = p2y = self.Points[segment].y |
|
2804 |
if dir[0] != 0: |
|
2805 |
p1x = (2 * self.Points[segment].x + self.Points[segment + 1].x) / 3 |
|
2806 |
p2x = (self.Points[segment].x + 2 * self.Points[segment + 1].x) / 3 |
|
2807 |
if dir[1] != 0: |
|
2808 |
p1y = (2 * self.Points[segment].y + self.Points[segment + 1].y) / 3 |
|
2809 |
p2y = (self.Points[segment].y + 2 * self.Points[segment + 1].y) / 3 |
|
2810 |
self.Points.insert(segment + 1, wx.Point(p1x, p1y)) |
|
2811 |
self.Segments.insert(segment + 1, (dir[1], dir[0])) |
|
2812 |
self.Points.insert(segment + 2, wx.Point(p1x, p1y)) |
|
2813 |
self.Segments.insert(segment + 2, dir) |
|
2814 |
self.Points.insert(segment + 3, wx.Point(p2x, p2y)) |
|
2815 |
self.Segments.insert(segment + 3, (dir[1], dir[0])) |
|
2816 |
self.Points.insert(segment + 4, wx.Point(p2x, p2y)) |
|
2817 |
self.Segments.insert(segment + 4, dir) |
|
2818 |
self.GeneratePoints() |
|
2819 |
||
2820 |
# Delete the handled segment by removing the two segment points |
|
2821 |
def DeleteSegment(self): |
|
2822 |
handle_type, handle = self.Handle |
|
2823 |
if handle_type == HANDLE_SEGMENT: |
|
2824 |
segment, dir = handle |
|
2825 |
for i in xrange(2): |
|
2826 |
self.Points.pop(segment) |
|
2827 |
self.Segments.pop(segment) |
|
2828 |
self.GeneratePoints() |
|
2829 |
self.RefreshModel() |
|
2830 |
||
2831 |
# Method called when a LeftDown event have been generated |
|
2832 |
def OnLeftDown(self, event, dc, scaling): |
|
2833 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
2834 |
# Test if a point have been handled |
|
2835 |
#result = self.TestPoint(pos) |
|
2836 |
#if result != None: |
|
2837 |
# self.Handle = (HANDLE_POINT, result) |
|
2838 |
# wx.CallAfter(self.Parent.SetCurrentCursor, 1) |
|
2839 |
#else: |
|
2840 |
# Test if a segment have been handled |
|
2841 |
result = self.TestSegment(pos) |
|
2842 |
if result != None: |
|
2843 |
if result[1] in (NORTH, SOUTH): |
|
2844 |
wx.CallAfter(self.Parent.SetCurrentCursor, 4) |
|
2845 |
elif result[1] in (EAST, WEST): |
|
2846 |
wx.CallAfter(self.Parent.SetCurrentCursor, 5) |
|
2847 |
self.Handle = (HANDLE_SEGMENT, result) |
|
2848 |
# Execute the default method for a graphic element |
|
2849 |
else: |
|
2850 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
|
2851 |
self.oldPos = pos |
|
2852 |
||
2853 |
# Method called when a RightUp event has been generated |
|
2854 |
def OnRightUp(self, event, dc, scaling): |
|
2855 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
2856 |
# Test if a segment has been handled |
|
2857 |
result = self.TestSegment(pos, True) |
|
2858 |
if result != None: |
|
2859 |
self.Handle = (HANDLE_SEGMENT, result) |
|
2860 |
# Popup the menu with special items for a wire |
|
2861 |
self.Parent.PopupWireMenu(0 < result[0] < len(self.Segments) - 1) |
|
2862 |
else: |
|
2863 |
# Execute the default method for a graphic element |
|
2864 |
Graphic_Element.OnRightUp(self, event, dc, scaling) |
|
2865 |
||
2866 |
# Method called when a LeftDClick event has been generated |
|
2867 |
def OnLeftDClick(self, event, dc, scaling): |
|
2868 |
rect = self.GetRedrawRect() |
|
2869 |
if event.ControlDown(): |
|
2870 |
direction = (self.StartPoint[1], self.EndPoint[1]) |
|
2871 |
if direction in [(EAST, WEST), (WEST, EAST)]: |
|
2872 |
avgy = (self.StartPoint[0].y + self.EndPoint[0].y) / 2 |
|
2873 |
if scaling is not None: |
|
2874 |
avgy = round(float(avgy) / scaling[1]) * scaling[1] |
|
2875 |
if self.StartConnected is not None: |
|
2876 |
movey = avgy - self.StartPoint[0].y |
|
2877 |
startblock = self.StartConnected.GetParentBlock() |
|
2878 |
startblock.Move(0, movey) |
|
2879 |
startblock.RefreshModel() |
|
2880 |
rect.Union(startblock.GetRedrawRect(0, movey)) |
|
2881 |
else: |
|
2882 |
self.MoveStartPoint(wx.Point(self.StartPoint[0].x, avgy)) |
|
2883 |
if self.EndConnected is not None: |
|
2884 |
movey = avgy - self.EndPoint[0].y |
|
2885 |
endblock = self.EndConnected.GetParentBlock() |
|
2886 |
endblock.Move(0, movey) |
|
2887 |
endblock.RefreshModel() |
|
2888 |
rect.Union(endblock.GetRedrawRect(0, movey)) |
|
2889 |
else: |
|
2890 |
self.MoveEndPoint(wx.Point(self.EndPoint[0].x, avgy)) |
|
2891 |
self.Parent.RefreshBuffer() |
|
2892 |
elif direction in [(NORTH, SOUTH), (SOUTH, NORTH)]: |
|
2893 |
avgx = (self.StartPoint[0].x + self.EndPoint[0].x) / 2 |
|
2894 |
if scaling is not None: |
|
2895 |
avgx = round(float(avgx) / scaling[0]) * scaling[0] |
|
2896 |
if self.StartConnected is not None: |
|
2897 |
movex = avgx - self.StartPoint[0].x |
|
2898 |
startblock = self.StartConnected.GetParentBlock() |
|
2899 |
startblock.Move(movex, 0) |
|
2900 |
startblock.RefreshModel() |
|
2901 |
rect.Union(startblock.GetRedrawRect(movex, 0)) |
|
2902 |
else: |
|
2903 |
self.MoveStartPoint(wx.Point(avgx, self.StartPoint[0].y)) |
|
2904 |
if self.EndConnected is not None: |
|
2905 |
movex = avgx - self.EndPoint[0].x |
|
2906 |
endblock = self.EndConnected.GetParentBlock() |
|
2907 |
endblock.Move(movex, 0) |
|
2908 |
endblock.RefreshModel() |
|
2909 |
rect.Union(endblock.GetRedrawRect(movex, 0)) |
|
2910 |
else: |
|
2911 |
self.MoveEndPoint(wx.Point(avgx, self.EndPoint[0].y)) |
|
2912 |
self.Parent.RefreshBuffer() |
|
2913 |
else: |
|
2914 |
self.ResetPoints() |
|
2915 |
self.GeneratePoints() |
|
2916 |
self.RefreshModel() |
|
2917 |
self.Parent.RefreshBuffer() |
|
2918 |
rect.Union(self.GetRedrawRect()) |
|
2919 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) |
|
2920 |
||
2921 |
# Method called when a Motion event has been generated |
|
2922 |
def OnMotion(self, event, dc, scaling): |
|
2923 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
2924 |
if not event.Dragging(): |
|
2925 |
# Test if a segment has been handled |
|
2926 |
result = self.TestSegment(pos) |
|
2927 |
if result: |
|
2928 |
if result[1] in (NORTH, SOUTH): |
|
2929 |
wx.CallAfter(self.Parent.SetCurrentCursor, 4) |
|
2930 |
elif result[1] in (EAST, WEST): |
|
2931 |
wx.CallAfter(self.Parent.SetCurrentCursor, 5) |
|
2932 |
return 0, 0 |
|
2933 |
else: |
|
2934 |
# Execute the default method for a graphic element |
|
2935 |
return Graphic_Element.OnMotion(self, event, dc, scaling) |
|
2936 |
else: |
|
2937 |
# Execute the default method for a graphic element |
|
2938 |
return Graphic_Element.OnMotion(self, event, dc, scaling) |
|
2939 |
||
2940 |
# Refreshes the wire state according to move defined and handle selected |
|
2941 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
2942 |
handle_type, handle = self.Handle |
|
2943 |
# A point has been handled |
|
2944 |
if handle_type == HANDLE_POINT: |
|
2945 |
movex = max(-self.Points[handle].x + POINT_RADIUS, movex) |
|
2946 |
movey = max(-self.Points[handle].y + POINT_RADIUS, movey) |
|
2947 |
if scaling is not None: |
|
2948 |
movex = round_scaling(self.Points[handle].x + movex, scaling[0]) - self.Points[handle].x |
|
2949 |
movey = round_scaling(self.Points[handle].y + movey, scaling[1]) - self.Points[handle].y |
|
2950 |
# Try to connect point to a connector |
|
2951 |
new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey) |
|
2952 |
connector = self.Parent.FindBlockConnector(new_pos, self.GetConnectionDirection()) |
|
2953 |
if connector: |
|
2954 |
if handle == 0 and self.EndConnected != connector: |
|
2955 |
connector.HighlightParentBlock(True) |
|
2956 |
connector.Connect((self, handle)) |
|
2957 |
self.SetStartPointDirection(connector.GetDirection()) |
|
2958 |
self.ConnectStartPoint(connector.GetPosition(), connector) |
|
2959 |
pos = connector.GetPosition() |
|
2960 |
movex = pos.x - self.oldPos.x |
|
2961 |
movey = pos.y - self.oldPos.y |
|
2962 |
if not connector.IsCompatible(self.GetEndConnectedType()): |
|
2963 |
self.SetValid(False) |
|
2964 |
self.Dragging = False |
|
2965 |
elif handle != 0 and self.StartConnected != connector: |
|
2966 |
connector.HighlightParentBlock(True) |
|
2967 |
connector.Connect((self, handle)) |
|
2968 |
self.SetEndPointDirection(connector.GetDirection()) |
|
2969 |
self.ConnectEndPoint(connector.GetPosition(), connector) |
|
2970 |
pos = connector.GetPosition() |
|
2971 |
movex = pos.x - self.oldPos.x |
|
2972 |
movey = pos.y - self.oldPos.y |
|
2973 |
if not connector.IsCompatible(self.GetStartConnectedType()): |
|
2974 |
self.SetValid(False) |
|
2975 |
self.Dragging = False |
|
2976 |
elif handle == 0: |
|
2977 |
self.MoveStartPoint(new_pos) |
|
2978 |
else: |
|
2979 |
self.MoveEndPoint(new_pos) |
|
2980 |
# If there is no connector, move the point |
|
2981 |
elif handle == 0: |
|
2982 |
self.SetValid(True) |
|
2983 |
if self.StartConnected: |
|
2984 |
self.StartConnected.HighlightParentBlock(False) |
|
2985 |
self.UnConnectStartPoint() |
|
2986 |
self.MoveStartPoint(new_pos) |
|
2987 |
else: |
|
2988 |
self.SetValid(True) |
|
2989 |
if self.EndConnected: |
|
2990 |
self.EndConnected.HighlightParentBlock(False) |
|
2991 |
self.UnConnectEndPoint() |
|
2992 |
self.MoveEndPoint(new_pos) |
|
2993 |
return movex, movey |
|
2994 |
# A segment has been handled, move a segment |
|
2995 |
elif handle_type == HANDLE_SEGMENT: |
|
2996 |
return self.MoveSegment(handle[0], movex, movey, scaling) |
|
2997 |
# Execute the default method for a graphic element |
|
2998 |
else: |
|
2999 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
3000 |
||
3001 |
# Refreshes the wire model |
|
3002 |
def RefreshModel(self, move=True): |
|
3003 |
if self.StartConnected and self.StartPoint[1] in [WEST, NORTH]: |
|
3004 |
self.StartConnected.RefreshParentBlock() |
|
3005 |
if self.EndConnected and self.EndPoint[1] in [WEST, NORTH]: |
|
3006 |
self.EndConnected.RefreshParentBlock() |
|
3007 |
||
3008 |
# Change the variable that indicates if this element is highlighted |
|
3009 |
def SetHighlighted(self, highlighted): |
|
3010 |
self.Highlighted = highlighted |
|
3011 |
if not highlighted: |
|
3012 |
self.OverStart = False |
|
3013 |
self.OverEnd = False |
|
3014 |
self.Refresh() |
|
3015 |
||
3016 |
def HighlightPoint(self, pos): |
|
3017 |
refresh = False |
|
3018 |
start, end = self.OverStart, self.OverEnd |
|
3019 |
self.OverStart = False |
|
3020 |
self.OverEnd = False |
|
3021 |
# Test if a point has been handled |
|
3022 |
result = self.TestPoint(pos) |
|
3023 |
if result != None: |
|
3024 |
if result == 0 and self.StartConnected is not None: |
|
3025 |
self.OverStart = True |
|
3026 |
elif result != 0 and self.EndConnected is not None: |
|
3027 |
self.OverEnd = True |
|
3028 |
if start != self.OverStart or end != self.OverEnd: |
|
3029 |
self.Refresh() |
|
3030 |
||
3031 |
# Draws the highlightment of this element if it is highlighted |
|
3032 |
def DrawHighlightment(self, dc): |
|
3033 |
scalex, scaley = dc.GetUserScale() |
|
3034 |
dc.SetUserScale(1, 1) |
|
3035 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR, (2 * scalex + 5))) |
|
3036 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
3037 |
dc.SetLogicalFunction(wx.AND) |
|
3038 |
# Draw the start and end points if they are not connected or the mouse is over them |
|
3039 |
if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): |
|
3040 |
dc.DrawCircle(round(self.Points[0].x * scalex), |
|
3041 |
round(self.Points[0].y * scaley), |
|
3042 |
(POINT_RADIUS + 1) * scalex + 2) |
|
3043 |
if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): |
|
3044 |
dc.DrawCircle(self.Points[-1].x * scalex, self.Points[-1].y * scaley, (POINT_RADIUS + 1) * scalex + 2) |
|
3045 |
# Draw the wire lines and the last point (it seems that DrawLines stop before the last point) |
|
3046 |
if len(self.Points) > 1: |
|
3047 |
points = [wx.Point(round((self.Points[0].x - self.Segments[0][0]) * scalex), |
|
3048 |
round((self.Points[0].y - self.Segments[0][1]) * scaley))] |
|
3049 |
points.extend([wx.Point(round(point.x * scalex), round(point.y * scaley)) for point in self.Points[1:-1]]) |
|
3050 |
points.append(wx.Point(round((self.Points[-1].x + self.Segments[-1][0]) * scalex), |
|
3051 |
round((self.Points[-1].y + self.Segments[-1][1]) * scaley))) |
|
3052 |
else: |
|
3053 |
points = [] |
|
3054 |
dc.DrawLines(points) |
|
3055 |
dc.SetLogicalFunction(wx.COPY) |
|
3056 |
dc.SetUserScale(scalex, scaley) |
|
3057 |
||
3058 |
if self.StartConnected is not None: |
|
3059 |
self.StartConnected.DrawHighlightment(dc) |
|
3060 |
self.StartConnected.Draw(dc) |
|
3061 |
if self.EndConnected is not None: |
|
3062 |
self.EndConnected.DrawHighlightment(dc) |
|
3063 |
self.EndConnected.Draw(dc) |
|
3064 |
||
3065 |
# Draws the wire lines and points |
|
3066 |
def Draw(self, dc): |
|
3067 |
Graphic_Element.Draw(self, dc) |
|
3068 |
if not self.Valid: |
|
3069 |
dc.SetPen(MiterPen(wx.RED)) |
|
3070 |
dc.SetBrush(wx.RED_BRUSH) |
|
3071 |
elif isinstance(self.Value, BooleanType) and self.Value: |
|
3072 |
if self.Forced: |
|
3073 |
dc.SetPen(MiterPen(wx.CYAN)) |
|
3074 |
dc.SetBrush(wx.CYAN_BRUSH) |
|
3075 |
else: |
|
3076 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
3077 |
dc.SetBrush(wx.GREEN_BRUSH) |
|
3078 |
elif self.Value == "undefined": |
|
3079 |
dc.SetPen(MiterPen(wx.NamedColour("orange"))) |
|
3080 |
dc.SetBrush(wx.Brush(wx.NamedColour("orange"))) |
|
3081 |
elif self.Forced: |
|
3082 |
dc.SetPen(MiterPen(wx.BLUE)) |
|
3083 |
dc.SetBrush(wx.BLUE_BRUSH) |
|
3084 |
else: |
|
3085 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
3086 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
3087 |
# Draw the start and end points if they are not connected or the mouse is over them |
|
3088 |
if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): |
|
3089 |
dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS) |
|
3090 |
if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): |
|
3091 |
dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS) |
|
3092 |
# Draw the wire lines and the last point (it seems that DrawLines stop before the last point) |
|
3093 |
if len(self.Points) > 1: |
|
3094 |
points = [wx.Point(self.Points[0].x - self.Segments[0][0], self.Points[0].y - self.Segments[0][1])] |
|
3095 |
points.extend([point for point in self.Points[1:-1]]) |
|
3096 |
points.append(wx.Point(self.Points[-1].x + self.Segments[-1][0], self.Points[-1].y + self.Segments[-1][1])) |
|
3097 |
else: |
|
3098 |
points = [] |
|
3099 |
dc.DrawLines(points) |
|
3100 |
# Draw the segment selected in red |
|
3101 |
if not getattr(dc, "printing", False) and self.SelectedSegment is not None: |
|
3102 |
dc.SetPen(MiterPen(wx.BLUE, 3)) |
|
3103 |
if self.SelectedSegment == len(self.Segments) - 1: |
|
3104 |
end = 0 |
|
3105 |
else: |
|
3106 |
end = 1 |
|
3107 |
dc.DrawLine(self.Points[self.SelectedSegment].x - 1, self.Points[self.SelectedSegment].y, |
|
3108 |
self.Points[self.SelectedSegment + 1].x + end, self.Points[self.SelectedSegment + 1].y) |
|
3109 |
if self.Value is not None and not isinstance(self.Value, BooleanType) and self.Value != "undefined": |
|
3110 |
dc.SetFont(self.Parent.GetMiniFont()) |
|
3111 |
dc.SetTextForeground(wx.NamedColour("purple")) |
|
3112 |
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)): |
|
3113 |
self.ValueSize = self.Parent.GetMiniTextExtent(self.ComputedValue) |
|
3114 |
if self.ValueSize is not None: |
|
3115 |
width, height = self.ValueSize |
|
3116 |
if self.BoundingBox[2] > width * 4 or self.BoundingBox[3] > height * 4: |
|
3117 |
x = self.Points[0].x + width * (self.StartPoint[1][0] - 1) / 2 |
|
3118 |
y = self.Points[0].y + height * (self.StartPoint[1][1] - 1) |
|
3119 |
dc.DrawText(self.ComputedValue, x, y) |
|
3120 |
x = self.Points[-1].x + width * (self.EndPoint[1][0] - 1) / 2 |
|
3121 |
y = self.Points[-1].y + height * (self.EndPoint[1][1] - 1) |
|
3122 |
dc.DrawText(self.ComputedValue, x, y) |
|
3123 |
else: |
|
3124 |
middle = len(self.Segments) / 2 + len(self.Segments) % 2 - 1 |
|
3125 |
x = (self.Points[middle].x + self.Points[middle + 1].x - width) / 2 |
|
3126 |
if self.BoundingBox[3] > height and self.Segments[middle] in [NORTH, SOUTH]: |
|
3127 |
y = (self.Points[middle].y + self.Points[middle + 1].y - height) / 2 |
|
3128 |
else: |
|
3129 |
y = self.Points[middle].y - height |
|
3130 |
dc.DrawText(self.ComputedValue, x, y) |
|
3131 |
dc.SetFont(self.Parent.GetFont()) |
|
3132 |
dc.SetTextForeground(wx.BLACK) |
|
3133 |
||
3134 |
||
3135 |
#------------------------------------------------------------------------------- |
|
3136 |
# Graphic comment element |
|
3137 |
#------------------------------------------------------------------------------- |
|
3138 |
||
3139 |
def FilterHighlightsByRow(highlights, row, length): |
|
3140 |
_highlights = [] |
|
3141 |
for start, end, highlight_type in highlights: |
|
3142 |
if start[0] <= row and end[0] >= row: |
|
3143 |
if start[0] < row: |
|
3144 |
start = (row, 0) |
|
3145 |
if end[0] > row: |
|
3146 |
end = (row, length) |
|
3147 |
_highlights.append((start, end, highlight_type)) |
|
3148 |
return _highlights |
|
3149 |
||
3150 |
def FilterHighlightsByColumn(highlights, start_col, end_col): |
|
3151 |
_highlights = [] |
|
3152 |
for start, end, highlight_type in highlights: |
|
3153 |
if end[1] > start_col and start[1] < end_col: |
|
3154 |
start = (start[0], max(start[1], start_col) - start_col) |
|
3155 |
end = (end[0], min(end[1], end_col) - start_col) |
|
3156 |
_highlights.append((start, end, highlight_type)) |
|
3157 |
return _highlights |
|
3158 |
||
3159 |
""" |
|
3160 |
Class that implements a comment |
|
3161 |
""" |
|
3162 |
||
3163 |
class Comment(Graphic_Element): |
|
3164 |
||
3165 |
# Create a new comment |
|
3166 |
def __init__(self, parent, content, id = None): |
|
3167 |
Graphic_Element.__init__(self, parent) |
|
3168 |
self.Id = id |
|
3169 |
self.Content = content |
|
3170 |
self.Pos = wx.Point(0, 0) |
|
3171 |
self.Size = wx.Size(0, 0) |
|
3172 |
self.Highlights = [] |
|
3173 |
||
3174 |
# Make a clone of this comment |
|
3175 |
def Clone(self, parent, id = None, pos = None): |
|
3176 |
comment = Comment(parent, self.Content, id) |
|
3177 |
if pos is not None: |
|
3178 |
comment.SetPosition(pos.x, pos.y) |
|
3179 |
comment.SetSize(self.Size[0], self.Size[1]) |
|
3180 |
return comment |
|
3181 |
||
3182 |
# Method for keeping compatibility with others |
|
3183 |
def Clean(self): |
|
3184 |
pass |
|
3185 |
||
3186 |
# Delete this comment by calling the corresponding method |
|
3187 |
def Delete(self): |
|
3188 |
self.Parent.DeleteComment(self) |
|
3189 |
||
3190 |
# Refresh the comment bounding box |
|
3191 |
def RefreshBoundingBox(self): |
|
3192 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
3193 |
||
3194 |
# Changes the comment size |
|
3195 |
def SetSize(self, width, height): |
|
3196 |
self.Size.SetWidth(width) |
|
3197 |
self.Size.SetHeight(height) |
|
3198 |
self.RefreshBoundingBox() |
|
3199 |
||
3200 |
# Returns the comment size |
|
3201 |
def GetSize(self): |
|
3202 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
3203 |
||
3204 |
# Returns the comment minimum size |
|
3205 |
def GetMinSize(self): |
|
3206 |
dc = wx.ClientDC(self.Parent) |
|
3207 |
min_width = 0 |
|
3208 |
min_height = 0 |
|
3209 |
# The comment minimum size is the maximum size of words in the content |
|
3210 |
for line in self.Content.splitlines(): |
|
3211 |
for word in line.split(" "): |
|
3212 |
wordwidth, wordheight = dc.GetTextExtent(word) |
|
3213 |
min_width = max(min_width, wordwidth) |
|
3214 |
min_height = max(min_height, wordheight) |
|
3215 |
return min_width + 20, min_height + 20 |
|
3216 |
||
3217 |
# Changes the comment position |
|
3218 |
def SetPosition(self, x, y): |
|
3219 |
self.Pos.x = x |
|
3220 |
self.Pos.y = y |
|
3221 |
self.RefreshBoundingBox() |
|
3222 |
||
3223 |
# Changes the comment content |
|
3224 |
def SetContent(self, content): |
|
3225 |
self.Content = content |
|
3226 |
min_width, min_height = self.GetMinSize() |
|
3227 |
self.Size[0] = max(self.Size[0], min_width) |
|
3228 |
self.Size[1] = max(self.Size[1], min_height) |
|
3229 |
self.RefreshBoundingBox() |
|
3230 |
||
3231 |
# Returns the comment content |
|
3232 |
def GetContent(self): |
|
3233 |
return self.Content |
|
3234 |
||
3235 |
# Returns the comment position |
|
3236 |
def GetPosition(self): |
|
3237 |
return self.Pos.x, self.Pos.y |
|
3238 |
||
3239 |
# Moves the comment |
|
3240 |
def Move(self, dx, dy, connected = True): |
|
3241 |
self.Pos.x += dx |
|
3242 |
self.Pos.y += dy |
|
3243 |
self.RefreshBoundingBox() |
|
3244 |
||
3245 |
# Resizes the comment with the position and the size given |
|
3246 |
def Resize(self, x, y, width, height): |
|
3247 |
self.Move(x, y) |
|
3248 |
self.SetSize(width, height) |
|
3249 |
||
3250 |
# Method called when a RightUp event have been generated |
|
3251 |
def OnRightUp(self, event, dc, scaling): |
|
3252 |
# Popup the default menu |
|
3253 |
self.Parent.PopupDefaultMenu() |
|
3254 |
||
3255 |
# Refreshes the wire state according to move defined and handle selected |
|
3256 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
3257 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE and self.Parent.CurrentLanguage == "LD": |
|
3258 |
movex = movey = 0 |
|
3259 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
3260 |
||
3261 |
# Refreshes the comment model |
|
3262 |
def RefreshModel(self, move=True): |
|
3263 |
self.Parent.RefreshCommentModel(self) |
|
3264 |
||
3265 |
# Method called when a LeftDClick event have been generated |
|
3266 |
def OnLeftDClick(self, event, dc, scaling): |
|
3267 |
# Edit the comment content |
|
3268 |
self.Parent.EditCommentContent(self) |
|
3269 |
||
3270 |
# Adds an highlight to the comment |
|
3271 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
3272 |
if infos[0] == "content": |
|
3273 |
AddHighlight(self.Highlights, (start, end, highlight_type)) |
|
3274 |
||
3275 |
# Removes an highlight from the comment |
|
3276 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
3277 |
RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
|
3278 |
||
3279 |
# Removes all the highlights of one particular type from the comment |
|
3280 |
def ClearHighlight(self, highlight_type=None): |
|
3281 |
self.Highlights = ClearHighlights(self.Highlights, highlight_type) |
|
3282 |
||
3283 |
# Draws the highlightment of this element if it is highlighted |
|
3284 |
def DrawHighlightment(self, dc): |
|
3285 |
scalex, scaley = dc.GetUserScale() |
|
3286 |
dc.SetUserScale(1, 1) |
|
3287 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
3288 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
3289 |
dc.SetLogicalFunction(wx.AND) |
|
3290 |
||
3291 |
left = (self.Pos.x - 1) * scalex - 2 |
|
3292 |
right = (self.Pos.x + self.Size[0] + 1) * scalex + 2 |
|
3293 |
top = (self.Pos.y - 1) * scaley - 2 |
|
3294 |
bottom = (self.Pos.y + self.Size[1] + 1) * scaley + 2 |
|
3295 |
angle_top = (self.Pos.x + self.Size[0] - 9) * scalex + 2 |
|
3296 |
angle_right = (self.Pos.y + 9) * scaley - 2 |
|
3297 |
||
3298 |
polygon = [wx.Point(left, top), wx.Point(angle_top, top), |
|
3299 |
wx.Point(right, angle_right), wx.Point(right, bottom), |
|
3300 |
wx.Point(left, bottom)] |
|
3301 |
dc.DrawPolygon(polygon) |
|
3302 |
||
3303 |
dc.SetLogicalFunction(wx.COPY) |
|
3304 |
dc.SetUserScale(scalex, scaley) |
|
3305 |
||
3306 |
# Draws the comment and its content |
|
3307 |
def Draw(self, dc): |
|
3308 |
Graphic_Element.Draw(self, dc) |
|
3309 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
3310 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
3311 |
# Draws the comment shape |
|
3312 |
polygon = [wx.Point(self.Pos.x, self.Pos.y), |
|
3313 |
wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
|
3314 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10), |
|
3315 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1]), |
|
3316 |
wx.Point(self.Pos.x, self.Pos.y + self.Size[1])] |
|
3317 |
dc.DrawPolygon(polygon) |
|
3318 |
lines = [wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
|
3319 |
wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y + 10), |
|
3320 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10)] |
|
3321 |
dc.DrawLines(lines) |
|
3322 |
# Draws the comment content |
|
3323 |
y = self.Pos.y + 10 |
|
3324 |
for idx, line in enumerate(self.Content.splitlines()): |
|
3325 |
first = True |
|
3326 |
linetext = "" |
|
3327 |
words = line.split(" ") |
|
3328 |
if not getattr(dc, "printing", False): |
|
3329 |
highlights = FilterHighlightsByRow(self.Highlights, idx, len(line)) |
|
3330 |
highlights_offset = 0 |
|
3331 |
for i, word in enumerate(words): |
|
3332 |
if first: |
|
3333 |
text = word |
|
3334 |
else: |
|
3335 |
text = linetext + " " + word |
|
3336 |
wordwidth, wordheight = dc.GetTextExtent(text) |
|
3337 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
3338 |
break |
|
3339 |
if wordwidth < self.Size[0] - 20: |
|
3340 |
if i < len(words) - 1: |
|
3341 |
linetext = text |
|
3342 |
first = False |
|
3343 |
else: |
|
3344 |
dc.DrawText(text, self.Pos.x + 10, y) |
|
3345 |
if not getattr(dc, "printing", False): |
|
3346 |
DrawHighlightedText(dc, text, FilterHighlightsByColumn(highlights, highlights_offset, highlights_offset + len(text)), self.Pos.x + 10, y) |
|
3347 |
highlights_offset += len(text) + 1 |
|
3348 |
y += wordheight + 5 |
|
3349 |
else: |
|
3350 |
if not first: |
|
3351 |
dc.DrawText(linetext, self.Pos.x + 10, y) |
|
3352 |
if not getattr(dc, "printing", False): |
|
3353 |
DrawHighlightedText(dc, linetext, FilterHighlightsByColumn(highlights, highlights_offset, highlights_offset + len(linetext)), self.Pos.x + 10, y) |
|
3354 |
highlights_offset += len(linetext) + 1 |
|
3355 |
if first or i == len(words) - 1: |
|
3356 |
if not first: |
|
3357 |
y += wordheight + 5 |
|
3358 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
3359 |
break |
|
3360 |
dc.DrawText(word, self.Pos.x + 10, y) |
|
3361 |
if not getattr(dc, "printing", False): |
|
3362 |
DrawHighlightedText(dc, word, FilterHighlightsByColumn(highlights, highlights_offset, highlights_offset + len(word)), self.Pos.x + 10, y) |
|
3363 |
highlights_offset += len(word) + 1 |
|
3364 |
else: |
|
3365 |
linetext = word |
|
3366 |
y += wordheight + 5 |
|
3367 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
3368 |
break |
|
3369 |