1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #!/usr/bin/python3
- import gi
- gi.require_version('Gtk', '3.0')
- gi.require_version('GtkFlow', '0.10')
- gi.require_version('GFlow', '0.10')
- from gi.repository import GLib
- from gi.repository import Gtk
- from gi.repository import GFlow
- from gi.repository import GtkFlow
- import sys
- class CalculatorNode(GFlow.SimpleNode):
- def __new__(cls, *args, **kwargs):
- x = GFlow.SimpleNode.new()
- x.__class__ = cls
- return x
- class NumberNode(CalculatorNode):
- def __init__(self, number=0):
- self.number = GFlow.SimpleSource.new(float(number))
- self.number.set_name("output")
- self.add_source(self.number)
- adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
- self.spinbutton = Gtk.SpinButton()
- self.spinbutton.set_adjustment(adjustment)
- self.spinbutton.set_size_request(50,20)
- self.spinbutton.connect("value_changed", self.do_value_changed)
- self.number.set_value(float(self.spinbutton.get_value()))
- self.set_name("NumberGenerator")
- def do_value_changed(self, widget=None, data=None):
- self.number.set_value(float(self.spinbutton.get_value()))
- class PrintNode(CalculatorNode):
- def __init__(self):
- self.number = GFlow.SimpleSink.new(float(0))
- self.number.set_name("input")
- self.number.connect("changed", self.do_printing)
- self.number.set_max_sources(10)
- self.add_sink(self.number)
- self.childlabel = Gtk.Label()
- self.set_name("Output")
- def do_printing(self, dock):
- report = ""
- for i in range(len(self.number.get_sources())):
- n = self.number.get_value(i)
- if n is not None:
- report += "Dock #{} contains value: {}\n".format(i, n)
- self.childlabel.set_text(report)
- class Calculator(object):
- def __init__(self):
- w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
- self.nv = GtkFlow.NodeView.new()
- self.nv.set_placeholder("Please click the buttons above to spawn nodes.")
- hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
- create_numbernode_button = Gtk.Button("Create NumberNode")
- create_numbernode_button.connect("clicked", self.do_create_numbernode)
- hbox.add(create_numbernode_button)
- create_printnode_button = Gtk.Button("Create PrintNode")
- create_printnode_button.connect("clicked", self.do_create_printnode)
- hbox.add(create_printnode_button)
- vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
- vbox.pack_start(hbox, False, False, 0)
- vbox.pack_start(self.nv, True, True, 0)
-
- w.add(vbox)
- w.add(self.nv)
- w.show_all()
- w.connect("destroy", self.do_quit)
- Gtk.main()
- def do_create_numbernode(self, widget=None, data=None):
- n = NumberNode()
- self.nv.add_with_child(n, n.spinbutton)
- def do_create_printnode(self, widget=None, data=None):
- n = PrintNode()
- self.nv.add_with_child(n, n.childlabel)
- def do_quit(self, widget=None, data=None):
- Gtk.main_quit()
- sys.exit(0)
- if __name__ == "__main__":
- Calculator()
|