different_types.py 7.9 KB

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