connecting_line.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <class name="ConnectingLine">
  2. <relationships>
  3. <association name="parent" class="A" min="1" max="1" />
  4. <inheritance class="SCCDWidget" priority='0'/>
  5. <association name="elements" class="CanvasElement"/>
  6. <association name="prompt" class="PromptWindow"/>
  7. </relationships>
  8. <constructor>
  9. <parameter name="parent"/>
  10. <parameter name="cs_element"/>
  11. <super class="SCCDWidget">
  12. <parameter expr="True"/>
  13. </super>
  14. <body>
  15. self.containing_canvas = parent
  16. self.cs_element = cs_element
  17. self.as_element = cs_element['__asid']
  18. self.diff_results = {}
  19. self.elements = set()
  20. </body>
  21. </constructor>
  22. <destructor>
  23. <body>
  24. for f in self.elements:
  25. self.containing_canvas.delete(f)
  26. </body>
  27. </destructor>
  28. <method name="add_to_layer">
  29. <parameter name="layer"/>
  30. <parameter name="ID"/>
  31. <body>
  32. # Code adapted from: https://stackoverflow.com/questions/9576063/stacking-order-maintenance-in-tkinter
  33. layer_tag = "layer %s" % layer
  34. if layer_tag not in self.containing_canvas.layers: self.containing_canvas.layers.append(layer_tag)
  35. self.containing_canvas.itemconfigure(ID, tags=[layer_tag])
  36. for layer in sorted(self.containing_canvas.layers):
  37. self.containing_canvas.lift(layer)
  38. </body>
  39. </method>
  40. <scxml initial="init">
  41. <state id="init">
  42. <transition cond="self.cs_element['__source'] in self.containing_canvas.group_location and self.cs_element['__target'] in self.containing_canvas.group_location" target="../main">
  43. <script>
  44. source_x = self.containing_canvas.group_location[self.cs_element['__source']][0] + self.cs_element['offsetSourceX']
  45. source_y = self.containing_canvas.group_location[self.cs_element['__source']][1] + self.cs_element['offsetSourceY']
  46. target_x = self.containing_canvas.group_location[self.cs_element['__target']][0] + self.cs_element['offsetTargetX']
  47. target_y = self.containing_canvas.group_location[self.cs_element['__target']][1] + self.cs_element['offsetTargetY']
  48. middle_x = (source_x + target_x) / 2
  49. middle_y = (source_y + target_y) / 2
  50. self.containing_canvas.group_location[self.cs_element['id']] = (middle_x, middle_y)
  51. if self.cs_element['arrow']:
  52. lw = self.cs_element['lineWidth']
  53. line1 = self.containing_canvas.create_line(source_x, source_y, middle_x, middle_y, fill=self.cs_element['lineColour'], width=self.cs_element['lineWidth'], arrow=tk.LAST, arrowshape=(lw*8, lw*10, lw*3))
  54. else:
  55. line1 = self.containing_canvas.create_line(source_x, source_y, middle_x, middle_y, fill=self.cs_element['lineColour'], width=self.cs_element['lineWidth'])
  56. line2 = self.containing_canvas.create_line(middle_x, middle_y, target_x, target_y, fill=self.cs_element['lineColour'], width=self.cs_element['lineWidth'])
  57. self.cs_element['elem'] = (line1, line2)
  58. self.elements.add(line1)
  59. self.elements.add(line2)
  60. self.add_to_layer(self.cs_element["layer"], line1)
  61. self.add_to_layer(self.cs_element["layer"], line2)
  62. self.set_bindable_and_tagorid(self.containing_canvas, line1)
  63. self.set_bindable_and_tagorid(self.containing_canvas, line2)
  64. </script>
  65. </transition>
  66. </state>
  67. <state id="main">
  68. <transition event="middle-click" cond="id(self) == ID and not control_pressed" target="../update_attrs">
  69. <parameter name="ID"/>
  70. </transition>
  71. <transition event="right-click" cond="id(self) == ID and not control_pressed" target=".">
  72. <parameter name="ID"/>
  73. <raise event="create_line" scope="narrow" target="'parent'">
  74. <parameter expr="self.cs_element['id']"/>
  75. <parameter expr="self.as_element"/>
  76. <parameter expr="(self.last_x, self.last_y)"/>
  77. </raise>
  78. </transition>
  79. <transition event="right-click" cond="id(self) == ID and control_pressed" target="../remove_element">
  80. <parameter name="ID"/>
  81. </transition>
  82. <transition event="middle-click" cond="id(self) == ID and control_pressed" target=".">
  83. <parameter name="ID"/>
  84. <raise event="modify_defined_attrs" scope="narrow" target="'parent/parent'">
  85. <parameter expr="self.as_element"/>
  86. </raise>
  87. </transition>
  88. <transition event="moved_element" cond="element == self.cs_element['__source'] or element == self.cs_element['__target']" target=".">
  89. <parameter name="element"/>
  90. <parameter name="new_location"/>
  91. <script>
  92. source_x, source_y, _, _ = self.containing_canvas.coords(self.cs_element['elem'][0])
  93. _, _, target_x, target_y = self.containing_canvas.coords(self.cs_element['elem'][1])
  94. if element == self.cs_element['__source']:
  95. # Changed source of our link
  96. source_x = new_location[0] + self.cs_element['offsetSourceX']
  97. source_y = new_location[1] + self.cs_element['offsetSourceY']
  98. if element == self.cs_element['__target']:
  99. # Changed target of our link
  100. target_x = new_location[0] + self.cs_element['offsetTargetX']
  101. target_y = new_location[1] + self.cs_element['offsetTargetY']
  102. self.middle_x = (source_x + target_x) / 2
  103. self.middle_y = (source_y + target_y) / 2
  104. self.containing_canvas.coords(self.cs_element['elem'][0], source_x, source_y, self.middle_x, self.middle_y)
  105. self.containing_canvas.coords(self.cs_element['elem'][1], self.middle_x, self.middle_y, target_x, target_y)
  106. </script>
  107. <raise event="moved_group" scope="narrow" target="'parent'">
  108. <parameter expr="self.cs_element['id']"/>
  109. <parameter expr="(self.middle_x, self.middle_y)"/>
  110. </raise>
  111. </transition>
  112. <transition event="deleted_element" cond="element == self.cs_element['__source'] or element == self.cs_element['__target']" target="../deleted">
  113. <parameter name="element"/>
  114. <script>
  115. for f in self.elements:
  116. self.containing_canvas.delete(f)
  117. self.elements = {}
  118. </script>
  119. <raise event="deleted_group" scope="narrow" target="'parent'">
  120. <parameter expr="self.cs_element['id']"/>
  121. </raise>
  122. </transition>
  123. </state>
  124. <state id="deleted"/>
  125. <state id="remove_element">
  126. <onentry>
  127. <raise event="mv_request" scope="broad">
  128. <parameter expr="'delete_element'"/>
  129. <parameter expr="[current_model, self.as_element]"/>
  130. </raise>
  131. <raise event="deleted_group" scope="narrow" target="'parent'">
  132. <parameter expr="self.cs_element['id']"/>
  133. </raise>
  134. <script>
  135. for f in self.elements:
  136. self.containing_canvas.delete(f)
  137. self.elements = {}
  138. </script>
  139. </onentry>
  140. <transition event="mv_response" target="../main">
  141. </transition>
  142. </state>
  143. <state id="update_attrs" initial="query">
  144. <state id="query">
  145. <onentry>
  146. <raise event="mv_request" scope="broad">
  147. <parameter expr="'read_attrs'"/>
  148. <parameter expr="[current_model, self.as_element]"/>
  149. </raise>
  150. </onentry>
  151. <transition event="mv_response" target="../prompt">
  152. <parameter name="result"/>
  153. <script>
  154. self.prev_results = {k: json.dumps(v) for k, v in result.items()}
  155. </script>
  156. </transition>
  157. </state>
  158. <state id="prompt">
  159. <onentry>
  160. <raise event="create_instance" scope="cd">
  161. <parameter expr="'prompt'"/>
  162. <parameter expr="'PromptWindow'"/>
  163. <parameter expr="self.prev_results"/>
  164. <parameter expr="{}"/>
  165. </raise>
  166. </onentry>
  167. <transition event="instance_created" target="../wait_for_results">
  168. <parameter name="name"/>
  169. <raise event="start_instance" scope="cd">
  170. <parameter expr="name"/>
  171. </raise>
  172. <script>
  173. self.remaining_responses = 0
  174. </script>
  175. </transition>
  176. </state>
  177. <state id="wait_for_results">
  178. <transition event="prompt_results" target=".">
  179. <parameter name="results"/>
  180. <script>
  181. for k in results:
  182. if results[k] != self.prev_results[k]:
  183. self.diff_results[k] = results[k]
  184. self.remaining_responses = len(self.diff_results)
  185. </script>
  186. </transition>
  187. <transition event="close_window" cond="self.remaining_responses > 0" target="../process_results">
  188. <raise event="delete_instance" scope="cd">
  189. <parameter expr="'prompt'"/>
  190. </raise>
  191. </transition>
  192. <transition event="close_window" cond="self.remaining_responses == 0" target="../../main">
  193. <raise event="delete_instance" scope="cd">
  194. <parameter expr="'prompt'"/>
  195. </raise>
  196. </transition>
  197. </state>
  198. <state id="process_results">
  199. <transition cond="len(self.diff_results) > 0" target=".">
  200. <script>
  201. k, v = self.diff_results.popitem()
  202. </script>
  203. <raise event="mv_request" scope="broad">
  204. <parameter expr="'attr_assign'"/>
  205. <parameter expr="[current_model, self.as_element, k, json.loads(v)]"/>
  206. </raise>
  207. </transition>
  208. <transition cond="self.remaining_responses == 0" target="../mark_dirty"/>
  209. <transition event="mv_response" target=".">
  210. <script>
  211. self.remaining_responses -= 1
  212. </script>
  213. </transition>
  214. </state>
  215. <state id="mark_dirty">
  216. <onentry>
  217. <raise event="mv_request" scope="broad">
  218. <parameter expr="'attr_assign'"/>
  219. <parameter expr="[current_rendered_model, self.cs_element['id'], 'dirty', True]"/>
  220. </raise>
  221. </onentry>
  222. <transition event="mv_response" target="../../main">
  223. <raise event="minimal_rerender_model" scope="narrow" target="'parent/parent'"/>
  224. </transition>
  225. </state>
  226. </state>
  227. </scxml>
  228. </class>