cycles.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. from gi.repository import GLib
  10. from gi.repository import Gtk
  11. from gi.repository import GFlow
  12. from gi.repository import GtkFlow
  13. import sys
  14. class ExampleNode(GFlow.SimpleNode):
  15. def __new__(cls, *args, **kwargs):
  16. x = GFlow.SimpleNode.new()
  17. x.__class__ = cls
  18. return x
  19. class StarterNode(ExampleNode):
  20. def __init__(self):
  21. self.emitter = GFlow.SimpleSource.new(float(0))
  22. self.emitter.set_name("emitter")
  23. self.add_source(self.emitter)
  24. self.button = Gtk.Button.new_with_label("Start!")
  25. self.button.connect("clicked", self.do_send_start)
  26. self.set_name("Counter")
  27. def do_send_start(self, dock, val=None):
  28. self.emitter.set_value(1.0)
  29. class CountNode(ExampleNode):
  30. def __init__(self):
  31. self.counter = 0.0
  32. self.target = 10.0
  33. self.enable = GFlow.SimpleSink.new(float(0))
  34. self.clock = GFlow.SimpleSink.new(float(0))
  35. self.enable.set_name("enable")
  36. self.clock.set_name("clock")
  37. self.add_sink(self.enable)
  38. self.add_sink(self.clock)
  39. self.result = GFlow.SimpleSource.new(float(0))
  40. self.counted = GFlow.SimpleSource.new(float(0))
  41. self.result.set_name("result")
  42. self.counted.set_name("counted")
  43. self.add_source(self.result)
  44. self.add_source(self.counted)
  45. self.enable.connect("changed", self.do_calculations)
  46. self.clock.connect("changed", self.do_calculations)
  47. self.set_name("Counter")
  48. def do_calculations(self, dock, val=None):
  49. enable = self.enable.get_value(0)
  50. if enable != 1.0:
  51. return
  52. if self.counter < self.target:
  53. self.counter += 1.0
  54. self.counted.set_value(self.counter)
  55. self.result.set_value(self.counter)
  56. class PrintNode(ExampleNode):
  57. def __init__(self):
  58. self.number = GFlow.SimpleSink.new(float(0))
  59. self.number.set_name("input")
  60. self.number.connect("changed", self.do_printing)
  61. self.add_sink(self.number)
  62. self.childlabel = Gtk.Label()
  63. self.set_name("Output")
  64. def do_printing(self, dock):
  65. n = self.number.get_value(0)
  66. if n is not None:
  67. print (n)
  68. self.childlabel.set_text(str(n))
  69. else:
  70. self.childlabel.set_text("")
  71. class CountDemo(object):
  72. def __init__(self):
  73. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  74. self.nv = GtkFlow.NodeView.new()
  75. # This deactivates nodeview's self-check for recursions
  76. self.nv.set_allow_recursion(True)
  77. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  78. create_starternode_button = Gtk.Button("Create StarterNode")
  79. create_starternode_button.connect("clicked", self.do_create_starternode)
  80. hbox.add(create_starternode_button)
  81. create_countnode_button = Gtk.Button("Create CountNode")
  82. create_countnode_button.connect("clicked", self.do_create_countnode)
  83. hbox.add(create_countnode_button)
  84. create_printnode_button = Gtk.Button("Create PrintNode")
  85. create_printnode_button.connect("clicked", self.do_create_printnode)
  86. hbox.add(create_printnode_button)
  87. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  88. vbox.pack_start(hbox, False, False, 0)
  89. vbox.pack_start(self.nv, True, True, 0)
  90. w.add(vbox)
  91. w.add(self.nv)
  92. w.show_all()
  93. w.connect("destroy", self.do_quit)
  94. Gtk.main()
  95. def do_create_starternode(self, widget=None, data=None):
  96. n = StarterNode()
  97. self.nv.add_with_child(n, n.button)
  98. def do_create_countnode(self, widget=None, data=None):
  99. n = CountNode()
  100. self.nv.add_node(n)
  101. def do_create_printnode(self, widget=None, data=None):
  102. n = PrintNode()
  103. self.nv.add_with_child(n, n.childlabel)
  104. def do_quit(self, widget=None, data=None):
  105. Gtk.main_quit()
  106. sys.exit(0)
  107. if __name__ == "__main__":
  108. CountDemo()