Correction d'une faute d'orthographe
[memoirecycle.git] / connection.py
blobefa704d5c11e0ef9f5be65e712d11b52bd15e620
1 import wx
2 import logging
3 import element
4 logger = logging.getLogger()
6 matchKind=set([('toElem','fromElem')])
8 def match(kindA, kindB):
9 ''' Check if Connection's kind are matching '''
10 if ((kindA, kindB) in matchKind) or ((kindB, kindA) in matchKind):
11 return True
12 else:
13 logger.warning('Problem with match in Connection')
14 return False
18 class Connection(object):
19 def __init__(self, elem, kind,dpos):
20 ''' A Connection connect an Element and another Connection.
21 A Connection always contains its own Element (elem).
22 Its link contains a Connection (or is None) '''
23 self.__dpos = dpos
24 self.__elem = elem
25 self.__kind = kind # see kind in matchKind
26 self.__link = None
27 self.__bmp = wx.Image("images/"+self.__kind+'.png').ConvertToBitmap()
29 @property
30 def bmp(self):
31 ''' Give the picture you see on panel. When a State is connected, its Connections are not visible. '''
32 if type(self.__elem) == element.State and self.__link != None:
33 self.__bmp.SetSize((0,0))
34 else:
35 self.__bmp.SetSize((12,12))
36 return self.__bmp
38 @property
39 def dpos(self):
40 ''' Connection's pictures relative position'''
41 if type(self.__elem) == element.State and self.__link != None:
42 # drawn line in panel from connection start at the centre of state if connection.link != None
43 return ( (self.__elem.zone[1][0]-self.__elem.zone[0][0])/2,(self.__elem.zone[1][1]-self.__elem.zone[0][1])/2)
44 else:
45 return self.__dpos
47 @property
48 def elem(self):
49 return self.__elem
51 @property
52 def kind(self):
53 return self.__kind
55 @property
56 def link(self):
57 return self.__link
59 @link.setter
60 def link(self,conn):
61 """ link setter is used when trying to connect or deconnect two Connection """
62 if conn == None: # = deconnection
63 if self.__link == None:
64 pass
65 else :
66 oldLink = self.__link
67 self.__link.__link = None
68 self.__link = None
69 self.elem.connectionChanged()
70 oldLink.elem.connectionChanged()
71 elif type(self.elem)== element.State and type(conn.elem) == element.State:
72 logger.info('Do not connect two states together')
73 elif type(self.elem) == element.State or type(conn.elem) == element.State:
74 if match(conn.__kind, self.__kind): # = connection to another conn OR change connection
75 oldLink = self.__link
76 oldConnLink = conn.__link
77 if oldConnLink != None :
78 # detach the previously connected
79 conn.__link.__link = None
80 oldLink.__link = None
81 self.__link = conn # connect to the new one
82 conn.__link = self
83 #The 4 connections involved tell they have changed
84 self.elem.connectionChanged()
85 conn.elem.connectionChanged()
86 if oldLink != None:
87 oldLink.elem.connectionChanged()
88 if oldConnLink != None:
89 oldConnLink.elem.connectionChanged()
90 else:
91 logger.warning('Problem with kind in link.setter')
92 else:
94 logger.info('Please only connect state with transformation')