layout.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 CalculatorNode(GFlow.SimpleNode):
  8. def __new__(cls, *args, **kwargs):
  9. x = GFlow.SimpleNode.new()
  10. x.__class__ = cls
  11. return x
  12. class OperationNode(CalculatorNode):
  13. def __init__(self):
  14. self.summand_a = GFlow.SimpleSink.new(float(0))
  15. self.summand_b = GFlow.SimpleSink.new(float(0))
  16. self.summand_a.set_name("operand A")
  17. self.summand_b.set_name("operand B")
  18. self.add_sink(self.summand_a)
  19. self.add_sink(self.summand_b)
  20. self.result = GFlow.SimpleSource.new(float(0))
  21. self.result.set_name("result")
  22. self.add_source(self.result)
  23. self.result.set_value(None)
  24. operations = ["+", "-", "*", "/"]
  25. self.combobox = Gtk.ComboBoxText()
  26. self.combobox.connect("changed", self.do_calculations)
  27. self.combobox.set_entry_text_column(0)
  28. for op in operations:
  29. self.combobox.append_text(op)
  30. self.summand_a.connect("changed", self.do_calculations)
  31. self.summand_b.connect("changed", self.do_calculations)
  32. self.set_name("Operation")
  33. def do_calculations(self, dock, val=None):
  34. op = self.combobox.get_active_text()
  35. val_a = self.summand_a.get_value(0)
  36. val_b = self.summand_b.get_value(0)
  37. if val_a is None or val_b is None:
  38. self.result.set_value(None)
  39. return
  40. if op == "+":
  41. self.result.set_value(val_a+val_b)
  42. elif op == "-":
  43. self.result.set_value(val_a-val_b)
  44. elif op == "*":
  45. self.result.set_value(val_a*val_b)
  46. elif op == "/":
  47. self.result.set_value(val_a/val_b)
  48. else:
  49. self.result.set_value(None)
  50. class NumberNode(CalculatorNode):
  51. def __init__(self, number=0):
  52. self.number = GFlow.SimpleSource.new(float(number))
  53. self.number.set_name("output")
  54. self.add_source(self.number)
  55. adjustment = Gtk.Adjustment(0, 0, 100, 1, 10, 0)
  56. self.spinbutton = Gtk.SpinButton()
  57. self.spinbutton.set_adjustment(adjustment)
  58. self.spinbutton.set_size_request(50,20)
  59. self.spinbutton.connect("value_changed", self.do_value_changed)
  60. self.number.set_value(float(self.spinbutton.get_value()))
  61. self.set_name("NumberGenerator")
  62. def do_value_changed(self, widget=None, data=None):
  63. self.number.set_value(float(self.spinbutton.get_value()))
  64. class PrintNode(CalculatorNode):
  65. def __init__(self):
  66. self.number = GFlow.SimpleSink.new(float(0))
  67. self.number.set_name("input")
  68. self.number.connect("changed", self.do_printing)
  69. self.add_sink(self.number)
  70. self.childlabel = Gtk.Label()
  71. self.set_name("Output")
  72. def do_printing(self, dock):
  73. n = self.number.get_value(0)
  74. if n is not None:
  75. self.childlabel.set_text(str(n))
  76. else:
  77. self.childlabel.set_text("")
  78. class Calculator(object):
  79. def __init__(self):
  80. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  81. self.nv = GtkFlow.NodeView.new()
  82. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  83. create_numbernode_button = Gtk.Button("Create NumberNode")
  84. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  85. hbox.add(create_numbernode_button)
  86. create_addnode_button = Gtk.Button("Create OperationNode")
  87. create_addnode_button.connect("clicked", self.do_create_addnode)
  88. hbox.add(create_addnode_button)
  89. create_printnode_button = Gtk.Button("Create PrintNode")
  90. create_printnode_button.connect("clicked", self.do_create_printnode)
  91. hbox.add(create_printnode_button)
  92. layout_button = Gtk.Button("layout")
  93. layout_button.connect("clicked", self.do_layout)
  94. hbox.add(layout_button)
  95. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  96. vbox.pack_start(hbox, False, False, 0)
  97. vbox.pack_start(self.nv, True, True, 0)
  98. w.add(vbox)
  99. w.add(self.nv)
  100. w.show_all()
  101. w.connect("destroy", self.do_quit)
  102. Gtk.main()
  103. def do_create_addnode(self, widget=None, data=None):
  104. n = OperationNode()
  105. self.nv.add_with_child(n, n.combobox)
  106. def do_create_numbernode(self, widget=None, data=None):
  107. n = NumberNode()
  108. self.nv.add_with_child(n, n.spinbutton)
  109. def do_create_printnode(self, widget=None, data=None):
  110. n = PrintNode()
  111. self.nv.add_with_child(n, n.childlabel)
  112. def do_layout(self, widget=None, data=None):
  113. print("foo")
  114. self.nv.layout(GtkFlow.ForceLayout.new())
  115. def do_quit(self, widget=None, data=None):
  116. Gtk.main_quit()
  117. sys.exit(0)
  118. if __name__ == "__main__":
  119. Calculator()