different_types.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/python3
  2. import gi
  3. gi.require_version('Gtk', '3.0')
  4. gi.require_version('GFlow', '0.10')
  5. gi.require_version('GtkFlow', '0.10')
  6. from gi.repository import GLib
  7. from gi.repository import Gtk
  8. from gi.repository import GFlow
  9. from gi.repository import GtkFlow
  10. import sys
  11. class ExampleNode(GFlow.SimpleNode):
  12. def __new__(cls, *args, **kwargs):
  13. x = GFlow.SimpleNode.new()
  14. x.__class__ = cls
  15. return x
  16. class ConcatNode(ExampleNode):
  17. def __init__(self):
  18. self.string_a = GFlow.SimpleSink.with_type(str)
  19. self.string_b = GFlow.SimpleSink.with_type(str)
  20. self.string_a.set_name("string A")
  21. self.string_b.set_name("string B")
  22. self.add_sink(self.string_a)
  23. self.add_sink(self.string_b)
  24. self.val_a = None
  25. self.val_b = None
  26. self.result = GFlow.SimpleSource.with_type(str)
  27. self.result.set_name("output")
  28. self.add_source(self.result)
  29. self.result.set_value(None)
  30. self.string_a.connect("changed", self.do_concatenation, "a")
  31. self.string_b.connect("changed", self.do_concatenation, "b")
  32. self.set_name("Concatenation")
  33. def do_concatenation(self, dock, val=None, flow_id=None, affiliation=None):
  34. if affiliation == "a":
  35. self.val_a = val
  36. if affiliation == "b":
  37. self.val_b = val
  38. val_a = self.val_a or ""
  39. val_b = self.val_b or ""
  40. self.result.set_value(val_a+val_b)
  41. class ConversionNode(ExampleNode):
  42. def __init__(self):
  43. self.sink = GFlow.SimpleSink.with_type(float)
  44. self.sink.set_name("input")
  45. self.add_sink(self.sink)
  46. self.source = GFlow.SimpleSource.with_type(str)
  47. self.source.set_name("output")
  48. self.add_source(self.source)
  49. self.source.set_value(None)
  50. self.sink.connect("changed", self.do_conversion)
  51. self.set_name("Number2String")
  52. def do_conversion(self, dock, val=None, flow_id=None):
  53. if val is not None:
  54. self.source.set_value(str(val))
  55. else:
  56. self.source.set_value(None)
  57. class StringNode(ExampleNode):
  58. def __init__(self):
  59. ExampleNode.__init__(self)
  60. self.source = GFlow.SimpleSource.with_type(str)
  61. self.source.set_name("output")
  62. self.add_source(self.source)
  63. self.entry = Gtk.Entry()
  64. self.entry.connect("changed", self.do_changed)
  65. self.set_name("String")
  66. def do_changed(self, widget=None, data=None):
  67. self.source.set_value(self.entry.get_text())
  68. class OperationNode(ExampleNode):
  69. def __init__(self):
  70. self.summand_a = GFlow.SimpleSink.with_type(float)
  71. self.summand_b = GFlow.SimpleSink.with_type(float)
  72. self.summand_a.set_name("operand A")
  73. self.summand_b.set_name("operand B")
  74. self.add_sink(self.summand_a)
  75. self.add_sink(self.summand_b)
  76. self.val_a = None
  77. self.val_b = None
  78. self.result = GFlow.SimpleSource.with_type(float)
  79. self.result.set_name("result")
  80. self.add_source(self.result)
  81. self.result.set_value(None)
  82. operations = ["+", "-", "*", "/"]
  83. self.combobox = Gtk.ComboBoxText()
  84. self.combobox.connect("changed", self.do_calculations)
  85. self.combobox.set_entry_text_column(0)
  86. for op in operations:
  87. self.combobox.append_text(op)
  88. self.summand_a.connect("changed", self.do_calculations, "a")
  89. self.summand_b.connect("changed", self.do_calculations, "b")
  90. self.set_name("Operation")
  91. def do_calculations(self, dock, val=None, flow_id=None, affiliation=None):
  92. op = self.combobox.get_active_text()
  93. if affiliation == "a":
  94. self.val_a = val
  95. if affiliation == "b":
  96. self.val_b = val
  97. if self.val_a is None or self.val_b is None:
  98. self.result.set_value(None)
  99. return
  100. if op == "+":
  101. self.result.set_value(self.val_a+self.val_b)
  102. elif op == "-":
  103. self.result.set_value(self.val_a-self.val_b)
  104. elif op == "*":
  105. self.result.set_value(self.val_a*self.val_b)
  106. elif op == "/":
  107. self.result.set_value(self.val_a/self.val_b)
  108. else:
  109. self.result.set_value(None)
  110. class NumberNode(ExampleNode):
  111. def __init__(self, number=0):
  112. self.number = GFlow.SimpleSource.with_type(float)
  113. self.number.set_name("output")
  114. self.add_source(self.number)
  115. adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
  116. self.spinbutton = Gtk.SpinButton()
  117. self.spinbutton.set_adjustment(adjustment)
  118. self.spinbutton.set_size_request(50,20)
  119. self.spinbutton.connect("value_changed", self.do_value_changed)
  120. self.set_name("NumberGenerator")
  121. def do_value_changed(self, widget=None, data=None, flow_id=None):
  122. self.number.set_value(float(self.spinbutton.get_value()))
  123. class PrintNode(ExampleNode):
  124. def __init__(self):
  125. self.sink = GFlow.SimpleSink.with_type(str)
  126. self.sink.set_name("")
  127. self.sink.connect("changed", self.do_printing)
  128. self.add_sink(self.sink)
  129. self.childlabel = Gtk.Label()
  130. self.set_name("Output")
  131. def do_printing(self, dock, val=None, flow_id=None):
  132. if val is not None:
  133. self.childlabel.set_text(val)
  134. else:
  135. self.childlabel.set_text("")
  136. class TypesExampleApplication(object):
  137. def __init__(self):
  138. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  139. self.nv = GtkFlow.NodeView.new()
  140. self.nv.set_show_types(True)
  141. self.nv.set_placeholder("Please click the buttons above to spawn nodes.")
  142. hbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  143. create_numbernode_button = Gtk.Button.new_with_label("Create NumberNode")
  144. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  145. hbox.add(create_numbernode_button)
  146. create_addnode_button = Gtk.Button.new_with_label("Create OperationNode")
  147. create_addnode_button.connect("clicked", self.do_create_addnode)
  148. hbox.add(create_addnode_button)
  149. create_printnode_button = Gtk.Button.new_with_label("Create PrintNode")
  150. create_printnode_button.connect("clicked", self.do_create_printnode)
  151. hbox.add(create_printnode_button)
  152. create_concatnode_button = Gtk.Button.new_with_label("Create ConcatenationNode")
  153. create_concatnode_button.connect("clicked", self.do_create_concatnode)
  154. hbox.add(create_concatnode_button)
  155. create_stringnode_button = Gtk.Button.new_with_label("Create StringNode")
  156. create_stringnode_button.connect("clicked", self.do_create_stringnode)
  157. hbox.add(create_stringnode_button)
  158. create_conversionnode_button = Gtk.Button.new_with_label("Create ConversionNode")
  159. create_conversionnode_button.connect("clicked", self.do_create_conversionnode)
  160. hbox.add(create_conversionnode_button)
  161. hbox.add(Gtk.Separator())
  162. show_types_button = Gtk.Button.new_with_label("Show Types")
  163. show_types_button.connect("clicked", self.do_show_types)
  164. hbox.add(show_types_button)
  165. hide_types_button = Gtk.Button.new_with_label("Hide Types")
  166. hide_types_button.connect("clicked", self.do_hide_types)
  167. hbox.add(hide_types_button)
  168. vbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  169. vbox.pack_start(hbox, False, False, 0)
  170. vbox.pack_start(self.nv, True, True, 0)
  171. w.add(vbox)
  172. w.show_all()
  173. w.connect("destroy", self.do_quit)
  174. Gtk.main()
  175. def do_create_addnode(self, widget=None, data=None):
  176. n = OperationNode()
  177. self.nv.add_with_child(n, n.combobox)
  178. def do_create_numbernode(self, widget=None, data=None):
  179. n = NumberNode()
  180. self.nv.add_with_child(n, n.spinbutton)
  181. def do_create_printnode(self, widget=None, data=None):
  182. n = PrintNode()
  183. self.nv.add_with_child(n, n.childlabel)
  184. def do_create_concatnode(self, widget=None, data=None):
  185. n = ConcatNode()
  186. self.nv.add_node(n)
  187. def do_create_stringnode(self, widget=None, data=None):
  188. n = StringNode()
  189. self.nv.add_with_child(n, n.entry)
  190. def do_create_conversionnode(self, widget=None, data=None):
  191. self.nv.add_node(ConversionNode())
  192. def do_show_types(self, widget=None, data=None):
  193. self.nv.set_show_types(True)
  194. def do_hide_types(self, widget=None, data=None):
  195. self.nv.set_show_types(False)
  196. def do_quit(self, widget=None, data=None):
  197. Gtk.main_quit()
  198. sys.exit(0)
  199. if __name__ == "__main__":
  200. TypesExampleApplication()