different_types.py 7.8 KB

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