objects.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/python3
  2. import gi
  3. gi.require_version('Gtk', '3.0')
  4. gi.require_version('GtkFlow', '0.10')
  5. gi.require_version('GFlow', '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 Node(GFlow.SimpleNode):
  12. def __new__(cls, *args, **kwargs):
  13. x = GFlow.SimpleNode.new()
  14. x.__class__ = cls
  15. return x
  16. class Point(object):
  17. def __init__(self, x=0, y=0):
  18. self.x = x
  19. self.y = y
  20. class PointConstructorNode(Node):
  21. def __init__(self):
  22. self.x_sink = GFlow.SimpleSink.with_type(float)
  23. self.y_sink = GFlow.SimpleSink.with_type(float)
  24. self.x_sink.set_name("X")
  25. self.y_sink.set_name("Y")
  26. self.x = 0.0
  27. self.y = 0.0
  28. self.add_sink(self.x_sink)
  29. self.add_sink(self.y_sink)
  30. self.result = GFlow.SimpleSource.with_type(object)
  31. self.result.set_name("result")
  32. self.add_source(self.result)
  33. self.result.set_value(None)
  34. self.x_sink.connect("changed", self.do_calculations, "x")
  35. self.y_sink.connect("changed", self.do_calculations, "y")
  36. self.set_name("Point Constructor")
  37. def do_calculations(self, dock, val=None, flow_id=None, affiliation=None):
  38. if affiliation == "x":
  39. self.x = val
  40. elif affiliation == "y":
  41. self.y = val
  42. else:
  43. return
  44. if self.x is None or self.y is None:
  45. self.result.set_value(None)
  46. return
  47. p = Point(x=self.x, y=self.y)
  48. self.result.set_value(p)
  49. class PointSplitterNode(Node):
  50. def __init__(self):
  51. self.p_sink = GFlow.SimpleSink.new(object)
  52. self.p_sink.set_name("point")
  53. self.p = None
  54. self.add_sink(self.p_sink)
  55. self.result_x = GFlow.SimpleSource.with_type(float)
  56. self.result_y = GFlow.SimpleSource.with_type(float)
  57. self.result_x.set_name("X")
  58. self.result_y.set_name("Y")
  59. self.add_source(self.result_x)
  60. self.add_source(self.result_y)
  61. self.result_x.set_value(None)
  62. self.result_y.set_value(None)
  63. self.p_sink.connect("changed", self.do_calculations)
  64. self.set_name("Point Splitter")
  65. def do_calculations(self, dock, val=None, flow_id=None):
  66. self.p = val
  67. if val is None:
  68. self.result_x.set_value(None)
  69. self.result_y.set_value(None)
  70. return
  71. self.result_x.set_value(self.p.x)
  72. self.result_y.set_value(self.p.y)
  73. class NumberNode(Node):
  74. def __init__(self, number=0.0):
  75. self.number = GFlow.SimpleSource.with_type(float)
  76. self.number.set_value(number)
  77. self.number.set_name("output")
  78. self.add_source(self.number)
  79. adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
  80. self.spinbutton = Gtk.SpinButton()
  81. self.spinbutton.set_adjustment(adjustment)
  82. self.spinbutton.set_size_request(50,20)
  83. self.spinbutton.connect("value_changed", self.do_value_changed)
  84. self.number.set_value(float(self.spinbutton.get_value()))
  85. self.set_name("NumberGenerator")
  86. def do_value_changed(self, widget=None, data=None):
  87. self.number.set_value(float(self.spinbutton.get_value()))
  88. class PrintNode(Node):
  89. def __init__(self):
  90. self.number = GFlow.SimpleSink.with_type(float)
  91. self.number.set_name("input")
  92. self.number.connect("changed", self.do_printing)
  93. self.add_sink(self.number)
  94. self.childlabel = Gtk.Label()
  95. self.set_name("Output")
  96. def do_printing(self, dock, val=None, flow_id=None):
  97. if val is not None:
  98. self.childlabel.set_text(str(val))
  99. else:
  100. self.childlabel.set_text("")
  101. class Calculator(object):
  102. def __init__(self):
  103. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  104. self.nv = GtkFlow.NodeView.new()
  105. self.nv.set_show_types(True)
  106. self.nv.set_placeholder("Please click the buttons above to spawn nodes.")
  107. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  108. create_numbernode_button = Gtk.Button.new_with_label("Create NumberNode")
  109. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  110. hbox.add(create_numbernode_button)
  111. create_constructor_button = Gtk.Button.new_with_label("Create Constructor")
  112. create_constructor_button.connect("clicked", self.do_create_constructor)
  113. hbox.add(create_constructor_button)
  114. create_splitter_button = Gtk.Button.new_with_label("Create Splitter")
  115. create_splitter_button.connect("clicked", self.do_create_splitter)
  116. hbox.add(create_splitter_button)
  117. create_printnode_button = Gtk.Button.new_with_label("Create PrintNode")
  118. create_printnode_button.connect("clicked", self.do_create_printnode)
  119. hbox.add(create_printnode_button)
  120. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  121. vbox.pack_start(hbox, False, False, 0)
  122. vbox.pack_start(self.nv, True, True, 0)
  123. w.add(vbox)
  124. w.show_all()
  125. w.connect("destroy", self.do_quit)
  126. Gtk.main()
  127. def do_create_constructor(self, widget=None, data=None):
  128. n = PointConstructorNode()
  129. self.nv.add_node(n)
  130. self.nv.set_node_renderer(n, GtkFlow.NodeRendererType.DOCKLINE)
  131. def do_create_splitter(self, widget=None, data=None):
  132. n = PointSplitterNode()
  133. self.nv.add_node(n)
  134. self.nv.set_node_renderer(n, GtkFlow.NodeRendererType.DOCKLINE)
  135. def do_create_numbernode(self, widget=None, data=None):
  136. n = NumberNode()
  137. self.nv.add_with_child(n, n.spinbutton)
  138. self.nv.set_node_renderer(n, GtkFlow.NodeRendererType.DOCKLINE)
  139. def do_create_printnode(self, widget=None, data=None):
  140. n = PrintNode()
  141. self.nv.add_with_child(n, n.childlabel)
  142. def do_quit(self, widget=None, data=None):
  143. Gtk.main_quit()
  144. sys.exit(0)
  145. if __name__ == "__main__":
  146. Calculator()