canvas_element.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <class name="CanvasElement">
  2. <relationships>
  3. <association name="parent" class="A" min="1" max="1" />
  4. <inheritance class="SCCDWidget" priority='0'/>
  5. <association name="prompt" class="PromptWindow"/>
  6. </relationships>
  7. <constructor>
  8. <parameter name="parent"/>
  9. <parameter name="coordinates"/>
  10. <parameter name="cs_element"/>
  11. <parameter name="as_element"/>
  12. <super class="SCCDWidget">
  13. <parameter expr="True"/>
  14. </super>
  15. <body>
  16. self.containing_canvas = parent
  17. self.coordinates = coordinates
  18. self.elements = {}
  19. self.tmp = {}
  20. self.cs_element = cs_element
  21. self.as_element = as_element
  22. self.diff_results = {}
  23. </body>
  24. </constructor>
  25. <destructor>
  26. <body>
  27. for f in self.elements:
  28. self.containing_canvas.delete(f)
  29. </body>
  30. </destructor>
  31. <method name="add_to_layer">
  32. <parameter name="layer"/>
  33. <parameter name="ID"/>
  34. <body>
  35. # Code adapted from: https://stackoverflow.com/questions/9576063/stacking-order-maintenance-in-tkinter
  36. layer_tag = "layer %s" % layer
  37. if layer_tag not in self.containing_canvas.layers: self.containing_canvas.layers.append(layer_tag)
  38. self.containing_canvas.itemconfigure(ID, tags=[layer_tag])
  39. for layer in sorted(self.containing_canvas.layers):
  40. self.containing_canvas.lift(layer)
  41. </body>
  42. </method>
  43. <method name="move_group">
  44. <parameter name="new_location"/>
  45. <body>
  46. for f in self.elements:
  47. old_coords = self.containing_canvas.coords(f)
  48. new_x = new_location[0] + self.elements[f]['x']
  49. new_y = new_location[1] + self.elements[f]['y']
  50. if len(old_coords) == 2:
  51. self.containing_canvas.coords(f, (new_x, new_y))
  52. elif len(old_coords) == 4:
  53. height = old_coords[3] - old_coords[1]
  54. width = old_coords[2] - old_coords[0]
  55. self.containing_canvas.coords(f, (new_x, new_y, new_x + width, new_y + height))
  56. </body>
  57. </method>
  58. <scxml initial="init">
  59. <state id="init" initial="init">
  60. <onexit>
  61. <raise event="canvas_element_ready" target="'parent'"/>
  62. </onexit>
  63. <state id="init">
  64. <transition cond="self.as_element not in self.containing_canvas.to_move" target="../../main">
  65. <script>
  66. print("Not found: %s in %s" % (self.as_element, self.containing_canvas.to_move))
  67. </script>
  68. </transition>
  69. <transition cond="self.as_element in self.containing_canvas.to_move" target="../update_mv">
  70. <script>
  71. self.coordinates = self.containing_canvas.to_move.pop(self.as_element)
  72. self.move_group(self.coordinates)
  73. </script>
  74. </transition>
  75. </state>
  76. <state id="update_mv" initial="check">
  77. <state id="check">
  78. <transition cond="current_rendered_model" target="../x"/>
  79. <transition cond="not current_rendered_model" target="../../../main">
  80. <raise event="move" scope="narrow" target="'parent/parent'">
  81. <parameter expr="self.as_element"/>
  82. <parameter expr="self.coordinates"/>
  83. </raise>
  84. </transition>
  85. </state>
  86. <state id="x">
  87. <onentry>
  88. <raise event="mv_request" scope="broad">
  89. <parameter expr="'attr_assign'"/>
  90. <parameter expr="[current_rendered_model, self.cs_element, 'x', self.coordinates[0]]"/>
  91. </raise>
  92. </onentry>
  93. <transition event="mv_response" target="../y"/>
  94. <transition event="mv_exception" target="../../../main">
  95. <script>
  96. print("Could not update X attribute")
  97. </script>
  98. </transition>
  99. </state>
  100. <state id="y">
  101. <onentry>
  102. <raise event="mv_request" scope="broad">
  103. <parameter expr="'attr_assign'"/>
  104. <parameter expr="[current_rendered_model, self.cs_element, 'y', self.coordinates[1]]"/>
  105. </raise>
  106. </onentry>
  107. <transition event="mv_response" target="../../../main"/>
  108. <transition event="mv_exception" target="../../../main">
  109. <script>
  110. print("Could not update Y attribute")
  111. </script>
  112. </transition>
  113. </state>
  114. </state>
  115. </state>
  116. <state id="main">
  117. <transition event="draw_element" target=".">
  118. <parameter name="element"/>
  119. <script>
  120. elem_x = self.coordinates[0] + element["x"]
  121. elem_y = self.coordinates[1] + element["y"]
  122. if element["type"] == "Rectangle":
  123. result = self.containing_canvas.create_rectangle(elem_x, elem_y, elem_x + element["width"], elem_y + element["height"], fill=element["fillColour"], outline=element["lineColour"])
  124. elif element["type"] == "Text":
  125. result = self.containing_canvas.create_text(elem_x, elem_y, fill=element["lineColour"], text=element["text"], anchor=tk.NW)
  126. elif element["type"] == "Line":
  127. result = self.containing_canvas.create_line(elem_x, elem_y, self.coordinates[0] + element["targetX"], self.coordinates[1] + element["targetY"], fill=element["lineColour"], width=element["lineWidth"], arrow=tk.LAST if element["arrow"] else tk.NONE)
  128. else:
  129. raise Exception("Undefined render format: " + str(element))
  130. result = None
  131. if result is not None:
  132. self.elements[result] = element
  133. self.set_bindable_and_tagorid(self.containing_canvas, result)
  134. self.add_to_layer(element["layer"], result)
  135. </script>
  136. </transition>
  137. <transition event="left-click" cond="id(self) == ID and not control_pressed" target="../dragging">
  138. <parameter name="ID"/>
  139. <script>
  140. self.original_coords = self.last_x, self.last_y
  141. </script>
  142. </transition>
  143. <transition event="middle-click" cond="id(self) == ID and not control_pressed" target="../update_attrs">
  144. <parameter name="ID"/>
  145. </transition>
  146. <transition event="middle-click" cond="id(self) == ID and control_pressed and current_metamodel == 'formalisms/SimpleClassDiagrams'" target=".">
  147. <parameter name="ID"/>
  148. <raise event="modify_defined_attrs" scope="narrow" target="'parent/parent'">
  149. <parameter expr="self.as_element"/>
  150. <parameter expr="self.cs_element"/>
  151. </raise>
  152. </transition>
  153. <transition event="right-click" cond="id(self) == ID and not control_pressed" target=".">
  154. <parameter name="ID"/>
  155. <raise event="create_line" scope="narrow" target="'parent'">
  156. <parameter expr="self.cs_element"/>
  157. <parameter expr="self.as_element"/>
  158. <parameter expr="(self.last_x, self.last_y)"/>
  159. </raise>
  160. </transition>
  161. <transition event="right-click" cond="id(self) == ID and control_pressed" target="../remove_element">
  162. <parameter name="ID"/>
  163. </transition>
  164. </state>
  165. <state id="remove_element">
  166. <onentry>
  167. <raise event="mv_request" scope="broad">
  168. <parameter expr="'delete_element'"/>
  169. <parameter expr="[current_model, self.as_element]"/>
  170. </raise>
  171. <raise event="deleted_group" scope="narrow" target="'parent'">
  172. <parameter expr="self.cs_element"/>
  173. </raise>
  174. <script>
  175. for f in self.elements:
  176. self.containing_canvas.delete(f)
  177. self.elements = {}
  178. </script>
  179. </onentry>
  180. <transition event="mv_response" target="../main"/>
  181. <transition event="mv_exception" target="../main">
  182. <parameter name="exception_name"/>
  183. <parameter name="description"/>
  184. <script>
  185. print("Could not remove element!")
  186. </script>
  187. </transition>
  188. </state>
  189. <state id="dragging">
  190. <transition event="motion" cond="id(self) == ID" target=".">
  191. <parameter name="ID"/>
  192. <script>
  193. new_x = self.coordinates[0] + (self.last_x - self.original_coords[0])
  194. new_y = self.coordinates[1] + (self.last_y - self.original_coords[1])
  195. self.move_group((new_x, new_y))
  196. </script>
  197. <raise event="moved_group" scope="narrow" target="'parent'">
  198. <parameter expr="self.cs_element"/>
  199. <parameter expr="(new_x, new_y)"/>
  200. </raise>
  201. </transition>
  202. <transition event="left-release" cond="id(self) == ID" target="../update_mv">
  203. <parameter name="ID"/>
  204. <script>
  205. delta_x = self.original_coords[0] - self.last_x
  206. delta_y = self.original_coords[1] - self.last_y
  207. self.coordinates = self.coordinates[0] - delta_x, self.coordinates[1] - delta_y
  208. </script>
  209. </transition>
  210. </state>
  211. <state id="update_attrs" initial="query">
  212. <state id="query">
  213. <onentry>
  214. <raise event="mv_request" scope="broad">
  215. <parameter expr="'read_attrs'"/>
  216. <parameter expr="[current_model, self.as_element]"/>
  217. </raise>
  218. </onentry>
  219. <transition event="mv_exception" target="../../main">
  220. <parameter name="exception_name"/>
  221. <parameter name="description"/>
  222. <script>
  223. print("Could not read attributes of element!")
  224. </script>
  225. </transition>
  226. <transition event="mv_response" target="../translate_AL">
  227. <parameter name="result"/>
  228. <script>
  229. self.prev_results = {}
  230. for k, v in result.items():
  231. if not isinstance(v, dict):
  232. if v is not None:
  233. try:
  234. self.prev_results[k] = json.dumps(v)
  235. except:
  236. self.prev_results[k] = "!! ERROR -- cannot decode attribute !!"
  237. else:
  238. self.prev_results[k] = ""
  239. self.al = {k: v for k, v in result.items() if isinstance(v, dict)}
  240. self.al_keys = self.al.keys()
  241. </script>
  242. </transition>
  243. </state>
  244. <state id="translate_AL" initial="check">
  245. <state id="check">
  246. <transition cond="not self.al" target="../../prompt"/>
  247. <transition cond="self.al" target="../process">
  248. <script>
  249. self.attr_name, self.attr_dict = self.al.popitem()
  250. </script>
  251. <raise event="mv_request" scope="broad">
  252. <parameter expr="'AL_text'"/>
  253. <parameter expr="[self.attr_dict['AL']]"/>
  254. </raise>
  255. </transition>
  256. </state>
  257. <state id="process">
  258. <transition event="mv_exception" target="../../../main">
  259. <parameter name="exception_name"/>
  260. <parameter name="description"/>
  261. <script>
  262. print("Could not read AL text of element!")
  263. </script>
  264. </transition>
  265. <transition event="mv_response" target="../check">
  266. <parameter name="result"/>
  267. <script>
  268. self.prev_results[self.attr_name] = result.rstrip() + "\n"
  269. </script>
  270. </transition>
  271. </state>
  272. </state>
  273. <state id="prompt">
  274. <onentry>
  275. <raise event="create_instance" scope="cd">
  276. <parameter expr="'prompt'"/>
  277. <parameter expr="'PromptWindow'"/>
  278. <parameter expr="self.prev_results"/>
  279. <parameter expr="{}"/>
  280. </raise>
  281. </onentry>
  282. <transition event="instance_created" target="../wait_for_results">
  283. <parameter name="name"/>
  284. <raise event="start_instance" scope="cd">
  285. <parameter expr="name"/>
  286. </raise>
  287. <script>
  288. self.remaining_responses = 0
  289. </script>
  290. </transition>
  291. </state>
  292. <state id="wait_for_results">
  293. <transition event="prompt_results" target=".">
  294. <parameter name="results"/>
  295. <script>
  296. for k in results:
  297. if results[k] != self.prev_results[k]:
  298. if k in self.al_keys:
  299. if results[k].strip() != self.prev_results[k].strip():
  300. self.diff_results[k] = results[k]
  301. else:
  302. self.diff_results[k] = results[k]
  303. self.remaining_responses = len(self.diff_results)
  304. </script>
  305. </transition>
  306. <transition event="close_window" cond="self.remaining_responses > 0" target="../process_results">
  307. <raise event="delete_instance" scope="cd">
  308. <parameter expr="'prompt'"/>
  309. </raise>
  310. </transition>
  311. <transition event="close_window" cond="self.remaining_responses == 0" target="../../main">
  312. <raise event="delete_instance" scope="cd">
  313. <parameter expr="'prompt'"/>
  314. </raise>
  315. </transition>
  316. </state>
  317. <state id="process_results">
  318. <onentry>
  319. <script>
  320. </script>
  321. </onentry>
  322. <transition cond="len(self.diff_results) > 0" target=".">
  323. <script>
  324. k, v = self.diff_results.popitem()
  325. if k in self.al_keys:
  326. op = "attr_assign_code"
  327. params = [current_model, self.as_element, k, v]
  328. else:
  329. op = "attr_assign"
  330. if v != "":
  331. try:
  332. v = json.loads(v)
  333. except:
  334. print("WARNING: Could not decode attr value as JSON: " + v)
  335. print("Attempting to send as string!")
  336. v = str(v)
  337. else:
  338. v = "null"
  339. params = [current_model, self.as_element, k, v]
  340. </script>
  341. <raise event="mv_request" scope="broad">
  342. <parameter expr="op"/>
  343. <parameter expr="params"/>
  344. </raise>
  345. </transition>
  346. <transition cond="self.remaining_responses == 0" target="../mark_dirty"/>
  347. <transition event="mv_exception" target=".">
  348. <parameter name="exception_name"/>
  349. <parameter name="description"/>
  350. <script>
  351. print("Got exception: " + str(exception_name))
  352. print("Description: " + str(description))
  353. self.remaining_responses -= 1
  354. </script>
  355. </transition>
  356. <transition event="mv_response" target=".">
  357. <script>
  358. self.remaining_responses -= 1
  359. </script>
  360. </transition>
  361. </state>
  362. <state id="mark_dirty" initial="check">
  363. <state id="check">
  364. <transition cond="current_rendered_model" target="../do"/>
  365. <transition cond="not current_rendered_model" target="../../../main">
  366. <raise event="minimal_rerender_model" scope="narrow" target="'parent/parent'"/>
  367. </transition>
  368. </state>
  369. <state id="do">
  370. <onentry>
  371. <raise event="mv_request" scope="broad">
  372. <parameter expr="'attr_assign'"/>
  373. <parameter expr="[current_rendered_model, self.cs_element, 'dirty', True]"/>
  374. </raise>
  375. </onentry>
  376. <transition event="mv_response" target="../../../main">
  377. <raise event="minimal_rerender_model" scope="narrow" target="'parent/parent'"/>
  378. </transition>
  379. <transition event="mv_exception" target="../../../main">
  380. <parameter name="exception_name"/>
  381. <parameter name="description"/>
  382. <script>
  383. print("Could not read assign attribute!")
  384. </script>
  385. </transition>
  386. </state>
  387. </state>
  388. </state>
  389. <state id="update_mv" initial="check">
  390. <state id="check">
  391. <transition cond="current_rendered_model" target="../x"/>
  392. <transition cond="not current_rendered_model" target="../../main">
  393. <raise event="move" scope="narrow" target="'parent/parent'">
  394. <parameter expr="self.as_element"/>
  395. <parameter expr="self.coordinates"/>
  396. </raise>
  397. </transition>
  398. </state>
  399. <state id="x">
  400. <onentry>
  401. <script>
  402. print("Moving element %s in model %s" % (self.cs_element, current_rendered_model))
  403. </script>
  404. <raise event="mv_request" scope="broad">
  405. <parameter expr="'attr_assign'"/>
  406. <parameter expr="[current_rendered_model, self.cs_element, 'x', self.coordinates[0]]"/>
  407. </raise>
  408. </onentry>
  409. <transition event="mv_response" target="../y"/>
  410. <transition event="mv_exception" target="../../main">
  411. <parameter name="exception_name"/>
  412. <parameter name="description"/>
  413. <script>
  414. print("Could not update X attribute!")
  415. </script>
  416. </transition>
  417. </state>
  418. <state id="y">
  419. <onentry>
  420. <raise event="mv_request" scope="broad">
  421. <parameter expr="'attr_assign'"/>
  422. <parameter expr="[current_rendered_model, self.cs_element, 'y', self.coordinates[1]]"/>
  423. </raise>
  424. </onentry>
  425. <transition event="mv_response" target="../../main"/>
  426. <transition event="mv_exception" target="../../main">
  427. <parameter name="exception_name"/>
  428. <parameter name="description"/>
  429. <script>
  430. print("Could not update Y attribute!")
  431. </script>
  432. </transition>
  433. </state>
  434. </state>
  435. </scxml>
  436. </class>