cycles.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/python3
  2. """
  3. This example shall explain how it is possible to do use cyclic graphs in
  4. libgtkflow. This example features a counter-node that will trigger itself
  5. to count up to a specific numeric value (self.target). To achieve the desired
  6. effect, please connect the counted-source with the clock-sink, so the node
  7. will propagate the desired signal to itself.
  8. """
  9. import gi
  10. gi.require_version('Gtk', '3.0')
  11. from gi.repository import GLib
  12. from gi.repository import Gtk
  13. from gi.repository import GFlow
  14. from gi.repository import GtkFlow
  15. import sys
  16. class ExampleNode(GFlow.SimpleNode):
  17. def __new__(cls, *args, **kwargs):
  18. x = GFlow.SimpleNode.new()
  19. x.__class__ = cls
  20. return x
  21. class StarterNode(ExampleNode):
  22. def __init__(self):
  23. self.emitter = GFlow.SimpleSource.new(float(0))
  24. self.emitter.set_name("emitter")
  25. self.add_source(self.emitter)
  26. self.button = Gtk.Button.new_with_label("Start!")
  27. self.button.connect("clicked", self.do_send_start)
  28. self.set_name("Counter")
  29. def do_send_start(self, dock, val=None):
  30. self.emitter.set_value(1.0)
  31. class CountNode(ExampleNode):
  32. def __init__(self):
  33. self.counter = 0.0
  34. self.target = 10.0
  35. self.enable = GFlow.SimpleSink.new(float(0))
  36. self.clock = GFlow.SimpleSink.new(float(0))
  37. self.enable.set_name("enable")
  38. self.clock.set_name("clock")
  39. self.add_sink(self.enable)
  40. self.add_sink(self.clock)
  41. self.result = GFlow.SimpleSource.new(float(0))
  42. self.counted = GFlow.SimpleSource.new(float(0))
  43. self.result.set_name("result")
  44. self.counted.set_name("counted")
  45. self.add_source(self.result)
  46. self.add_source(self.counted)
  47. self.enable.connect("changed", self.do_calculations)
  48. self.clock.connect("changed", self.do_calculations)
  49. self.set_name("Counter")
  50. def do_calculations(self, dock, val=None):
  51. enable = self.enable.get_value(0)
  52. if enable != 1.0:
  53. return
  54. if self.counter < self.target:
  55. self.counter += 1.0
  56. self.counted.set_value(self.counter)
  57. self.result.set_value(self.counter)
  58. class PrintNode(ExampleNode):
  59. def __init__(self):
  60. self.number = GFlow.SimpleSink.new(float(0))
  61. self.number.set_name("input")
  62. self.number.connect("changed", self.do_printing)
  63. self.add_sink(self.number)
  64. self.childlabel = Gtk.Label()
  65. self.set_name("Output")
  66. def do_printing(self, dock):
  67. n = self.number.get_value(0)
  68. if n is not None:
  69. print (n)
  70. self.childlabel.set_text(str(n))
  71. else:
  72. self.childlabel.set_text("")
  73. class CountDemo(object):
  74. def __init__(self):
  75. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  76. self.nv = GtkFlow.NodeView.new()
  77. # This deactivates nodeview's self-check for recursions
  78. self.nv.set_allow_recursion(True)
  79. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  80. create_starternode_button = Gtk.Button("Create StarterNode")
  81. create_starternode_button.connect("clicked", self.do_create_starternode)
  82. hbox.add(create_starternode_button)
  83. create_countnode_button = Gtk.Button("Create CountNode")
  84. create_countnode_button.connect("clicked", self.do_create_countnode)
  85. hbox.add(create_countnode_button)
  86. create_printnode_button = Gtk.Button("Create PrintNode")
  87. create_printnode_button.connect("clicked", self.do_create_printnode)
  88. hbox.add(create_printnode_button)
  89. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  90. vbox.pack_start(hbox, False, False, 0)
  91. vbox.pack_start(self.nv, True, True, 0)
  92. w.add(vbox)
  93. w.add(self.nv)
  94. w.show_all()
  95. w.connect("destroy", self.do_quit)
  96. Gtk.main()
  97. def do_create_starternode(self, widget=None, data=None):
  98. n = StarterNode()
  99. self.nv.add_with_child(n, n.button)
  100. def do_create_countnode(self, widget=None, data=None):
  101. n = CountNode()
  102. self.nv.add_node(n)
  103. def do_create_printnode(self, widget=None, data=None):
  104. n = PrintNode()
  105. self.nv.add_with_child(n, n.childlabel)
  106. def do_quit(self, widget=None, data=None):
  107. Gtk.main_quit()
  108. sys.exit(0)
  109. if __name__ == "__main__":
  110. CountDemo()