123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <class name="ConnectingLine">
- <relationships>
- <association name="parent" class="A" min="1" max="1" />
- <inheritance class="SCCDWidget" priority='0'/>
- <association name="elements" class="CanvasElement"/>
- <association name="prompt" class="PromptWindow"/>
- </relationships>
- <constructor>
- <parameter name="parent"/>
- <parameter name="cs_element"/>
- <super class="SCCDWidget">
- <parameter expr="True"/>
- </super>
- <body>
- self.containing_canvas = parent
- self.cs_element = cs_element
- self.as_element = cs_element['__asid']
- self.diff_results = {}
- self.elements = set()
- </body>
- </constructor>
- <destructor>
- <body>
- for f in self.elements:
- self.containing_canvas.delete(f)
- </body>
- </destructor>
- <method name="add_to_layer">
- <parameter name="layer"/>
- <parameter name="ID"/>
- <body>
- # Code adapted from: https://stackoverflow.com/questions/9576063/stacking-order-maintenance-in-tkinter
- layer_tag = "layer %s" % layer
- if layer_tag not in self.containing_canvas.layers: self.containing_canvas.layers.append(layer_tag)
- self.containing_canvas.itemconfigure(ID, tags=[layer_tag])
- for layer in sorted(self.containing_canvas.layers):
- self.containing_canvas.lift(layer)
- </body>
- </method>
- <scxml initial="init">
- <state id="init">
- <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">
- <script>
- source_x = self.containing_canvas.group_location[self.cs_element['__source']][0] + self.cs_element['offsetSourceX']
- source_y = self.containing_canvas.group_location[self.cs_element['__source']][1] + self.cs_element['offsetSourceY']
- target_x = self.containing_canvas.group_location[self.cs_element['__target']][0] + self.cs_element['offsetTargetX']
- target_y = self.containing_canvas.group_location[self.cs_element['__target']][1] + self.cs_element['offsetTargetY']
- middle_x = (source_x + target_x) / 2
- middle_y = (source_y + target_y) / 2
- self.containing_canvas.group_location[self.cs_element['id']] = (middle_x, middle_y)
- if self.cs_element['arrow']:
- lw = self.cs_element['lineWidth']
- 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))
- else:
- line1 = self.containing_canvas.create_line(source_x, source_y, middle_x, middle_y, fill=self.cs_element['lineColour'], width=self.cs_element['lineWidth'])
- line2 = self.containing_canvas.create_line(middle_x, middle_y, target_x, target_y, fill=self.cs_element['lineColour'], width=self.cs_element['lineWidth'])
- self.cs_element['elem'] = (line1, line2)
- self.elements.add(line1)
- self.elements.add(line2)
-
- self.add_to_layer(self.cs_element["layer"], line1)
- self.add_to_layer(self.cs_element["layer"], line2)
- self.set_bindable_and_tagorid(self.containing_canvas, line1)
- self.set_bindable_and_tagorid(self.containing_canvas, line2)
- </script>
- </transition>
- </state>
- <state id="main">
- <transition event="middle-click" cond="id(self) == ID and not control_pressed" target="../update_attrs">
- <parameter name="ID"/>
- </transition>
- <transition event="right-click" cond="id(self) == ID and not control_pressed" target=".">
- <parameter name="ID"/>
- <raise event="create_line" scope="narrow" target="'parent'">
- <parameter expr="self.cs_element['id']"/>
- <parameter expr="self.as_element"/>
- <parameter expr="(self.last_x, self.last_y)"/>
- </raise>
- </transition>
- <transition event="right-click" cond="id(self) == ID and control_pressed" target="../remove_element">
- <parameter name="ID"/>
- </transition>
- <transition event="middle-click" cond="id(self) == ID and control_pressed" target=".">
- <parameter name="ID"/>
- <raise event="modify_defined_attrs" scope="narrow" target="'parent/parent'">
- <parameter expr="self.as_element"/>
- </raise>
- </transition>
- <transition event="moved_element" cond="element == self.cs_element['__source'] or element == self.cs_element['__target']" target=".">
- <parameter name="element"/>
- <parameter name="new_location"/>
- <script>
- source_x, source_y, _, _ = self.containing_canvas.coords(self.cs_element['elem'][0])
- _, _, target_x, target_y = self.containing_canvas.coords(self.cs_element['elem'][1])
- if element == self.cs_element['__source']:
- # Changed source of our link
- source_x = new_location[0] + self.cs_element['offsetSourceX']
- source_y = new_location[1] + self.cs_element['offsetSourceY']
- if element == self.cs_element['__target']:
- # Changed target of our link
- target_x = new_location[0] + self.cs_element['offsetTargetX']
- target_y = new_location[1] + self.cs_element['offsetTargetY']
- self.middle_x = (source_x + target_x) / 2
- self.middle_y = (source_y + target_y) / 2
- self.containing_canvas.coords(self.cs_element['elem'][0], source_x, source_y, self.middle_x, self.middle_y)
- self.containing_canvas.coords(self.cs_element['elem'][1], self.middle_x, self.middle_y, target_x, target_y)
- </script>
- <raise event="moved_group" scope="narrow" target="'parent'">
- <parameter expr="self.cs_element['id']"/>
- <parameter expr="(self.middle_x, self.middle_y)"/>
- </raise>
- </transition>
- <transition event="deleted_element" cond="element == self.cs_element['__source'] or element == self.cs_element['__target']" target="../deleted">
- <parameter name="element"/>
- <script>
- for f in self.elements:
- self.containing_canvas.delete(f)
- self.elements = {}
- </script>
- <raise event="deleted_group" scope="narrow" target="'parent'">
- <parameter expr="self.cs_element['id']"/>
- </raise>
- </transition>
- </state>
-
- <state id="deleted"/>
- <state id="remove_element">
- <onentry>
- <raise event="mv_request" scope="broad">
- <parameter expr="'delete_element'"/>
- <parameter expr="[current_model, self.as_element]"/>
- </raise>
- <raise event="deleted_group" scope="narrow" target="'parent'">
- <parameter expr="self.cs_element['id']"/>
- </raise>
- <script>
- for f in self.elements:
- self.containing_canvas.delete(f)
- self.elements = {}
- </script>
- </onentry>
- <transition event="mv_response" target="../main">
- </transition>
- </state>
- <state id="update_attrs" initial="query">
- <state id="query">
- <onentry>
- <raise event="mv_request" scope="broad">
- <parameter expr="'read_attrs'"/>
- <parameter expr="[current_model, self.as_element]"/>
- </raise>
- </onentry>
- <transition event="mv_response" target="../prompt">
- <parameter name="result"/>
- <script>
- self.prev_results = {k: json.dumps(v) for k, v in result.items()}
- </script>
- </transition>
- </state>
- <state id="prompt">
- <onentry>
- <raise event="create_instance" scope="cd">
- <parameter expr="'prompt'"/>
- <parameter expr="'PromptWindow'"/>
- <parameter expr="self.prev_results"/>
- <parameter expr="{}"/>
- </raise>
- </onentry>
- <transition event="instance_created" target="../wait_for_results">
- <parameter name="name"/>
- <raise event="start_instance" scope="cd">
- <parameter expr="name"/>
- </raise>
- <script>
- self.remaining_responses = 0
- </script>
- </transition>
- </state>
- <state id="wait_for_results">
- <transition event="prompt_results" target=".">
- <parameter name="results"/>
- <script>
- for k in results:
- if results[k] != self.prev_results[k]:
- self.diff_results[k] = results[k]
- self.remaining_responses = len(self.diff_results)
- </script>
- </transition>
- <transition event="close_window" cond="self.remaining_responses > 0" target="../process_results">
- <raise event="delete_instance" scope="cd">
- <parameter expr="'prompt'"/>
- </raise>
- </transition>
- <transition event="close_window" cond="self.remaining_responses == 0" target="../../main">
- <raise event="delete_instance" scope="cd">
- <parameter expr="'prompt'"/>
- </raise>
- </transition>
- </state>
- <state id="process_results">
- <transition cond="len(self.diff_results) > 0" target=".">
- <script>
- k, v = self.diff_results.popitem()
- </script>
- <raise event="mv_request" scope="broad">
- <parameter expr="'attr_assign'"/>
- <parameter expr="[current_model, self.as_element, k, json.loads(v)]"/>
- </raise>
- </transition>
- <transition cond="self.remaining_responses == 0" target="../mark_dirty"/>
- <transition event="mv_response" target=".">
- <script>
- self.remaining_responses -= 1
- </script>
- </transition>
- </state>
- <state id="mark_dirty">
- <onentry>
- <raise event="mv_request" scope="broad">
- <parameter expr="'attr_assign'"/>
- <parameter expr="[current_rendered_model, self.cs_element['id'], 'dirty', True]"/>
- </raise>
- </onentry>
- <transition event="mv_response" target="../../main">
- <raise event="minimal_rerender_model" scope="narrow" target="'parent/parent'"/>
- </transition>
- </state>
- </state>
- </scxml>
- </class>
|