scrolling_multidock.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/python3
  2. import gi
  3. gi.require_version('Gtk', '3.0')
  4. from gi.repository import GLib
  5. from gi.repository import Gtk
  6. from gi.repository import GFlow
  7. from gi.repository import GtkFlow
  8. import sys
  9. class ExampleNode(GFlow.SimpleNode):
  10. def __new__(cls, *args, **kwargs):
  11. x = GFlow.SimpleNode.new()
  12. x.__class__ = cls
  13. return x
  14. class AddNode(ExampleNode):
  15. def add_summand(self, widget=None, data=None):
  16. summand_a = GFlow.SimpleSink.new(float(0))
  17. summand_a.set_name("operand %i"%(len(self.summands),))
  18. self.add_sink(summand_a)
  19. summand_a.connect("changed", self.do_calculations)
  20. self.summands.append(summand_a)
  21. self.do_calculations(None)
  22. def remove_summand(self, widget=None, data=None):
  23. if len(self.summands) == 0:
  24. return
  25. summand = self.summands[len(self.summands)-1]
  26. summand.unlink_all()
  27. self.remove_sink(summand)
  28. self.summands.remove(summand)
  29. self.do_calculations(None)
  30. def __init__(self):
  31. self.summands = []
  32. self.result = GFlow.SimpleSource.new(float(0))
  33. self.result.set_name("result")
  34. self.add_source(self.result)
  35. self.add_button = Gtk.Button.new_with_mnemonic("Add")
  36. self.remove_button = Gtk.Button.new_with_mnemonic("Rem")
  37. self.btnbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL,0)
  38. self.btnbox.add(self.add_button)
  39. self.btnbox.add(self.remove_button)
  40. self.add_button.connect("clicked", self.add_summand)
  41. self.remove_button.connect("clicked", self.remove_summand)
  42. self.set_name("Operation")
  43. def do_calculations(self, dock, val=None):
  44. res = 0
  45. for summand in self.summands:
  46. try:
  47. val = summand.get_value(0)
  48. res += val
  49. except:
  50. self.result.set_value(None)
  51. return
  52. self.result.set_value(res)
  53. class OperationNode(ExampleNode):
  54. def __init__(self):
  55. self.summand_a = GFlow.SimpleSink.new(float(0))
  56. self.summand_b = GFlow.SimpleSink.new(float(0))
  57. self.summand_a.set_name("operand A")
  58. self.summand_b.set_name("operand B")
  59. self.add_sink(self.summand_a)
  60. self.add_sink(self.summand_b)
  61. self.result = GFlow.SimpleSource.new(float(0))
  62. self.result.set_name("result")
  63. self.add_source(self.result)
  64. operations = ["+", "-", "*", "/"]
  65. self.combobox = Gtk.ComboBoxText()
  66. self.combobox.connect("changed", self.do_calculations)
  67. self.combobox.set_entry_text_column(0)
  68. for op in operations:
  69. self.combobox.append_text(op)
  70. self.summand_a.connect("changed", self.do_calculations)
  71. self.summand_b.connect("changed", self.do_calculations)
  72. self.set_name("Operation")
  73. def do_calculations(self, dock, val=None):
  74. op = self.combobox.get_active_text()
  75. try:
  76. val_a = self.summand_a.get_value()
  77. val_b = self.summand_b.get_value()
  78. except:
  79. self.result.invalidate()
  80. return
  81. if op == "+":
  82. self.result.set_value(val_a+val_b)
  83. elif op == "-":
  84. self.result.set_value(val_a-val_b)
  85. elif op == "*":
  86. self.result.set_value(val_a*val_b)
  87. elif op == "/":
  88. self.result.set_value(val_a/val_b)
  89. else:
  90. self.result.invalidate()
  91. class NumberNode(ExampleNode):
  92. def __init__(self, number=0):
  93. self.number = GFlow.SimpleSource.new(float(number))
  94. self.number.set_name("output")
  95. self.add_source(self.number)
  96. adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
  97. self.spinbutton = Gtk.SpinButton()
  98. self.spinbutton.set_adjustment(adjustment)
  99. self.spinbutton.set_size_request(50,20)
  100. self.spinbutton.connect("value_changed", self.do_value_changed)
  101. self.set_name("NumberGenerator")
  102. def do_value_changed(self, widget=None, data=None):
  103. self.number.set_value(float(self.spinbutton.get_value()))
  104. class PrintNode(ExampleNode):
  105. def __init__(self):
  106. self.number = GFlow.SimpleSink.new(float(0))
  107. self.number.set_name("")
  108. self.number.connect("changed", self.do_printing)
  109. self.add_sink(self.number)
  110. self.childlabel = Gtk.Label()
  111. self.set_name("Output")
  112. def do_printing(self, dock):
  113. try:
  114. n = self.number.get_value(0)
  115. print (n)
  116. self.childlabel.set_text(str(n))
  117. except:
  118. self.childlabel.set_text("")
  119. class Calculator(object):
  120. def __init__(self):
  121. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  122. self.nv = GtkFlow.NodeView.new()
  123. self.sw = Gtk.ScrolledWindow()
  124. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  125. create_numbernode_button = Gtk.Button("Create NumberNode")
  126. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  127. hbox.add(create_numbernode_button)
  128. create_addnode_button = Gtk.Button("Create OperationNode")
  129. create_addnode_button.connect("clicked", self.do_create_addnode)
  130. hbox.add(create_addnode_button)
  131. create_printnode_button = Gtk.Button("Create PrintNode")
  132. create_printnode_button.connect("clicked", self.do_create_printnode)
  133. hbox.add(create_printnode_button)
  134. self.sw.add_with_viewport(self.nv)
  135. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  136. vbox.pack_start(hbox, False, False, 0)
  137. vbox.pack_start(self.sw, True, True, 0)
  138. w.add(vbox)
  139. w.show_all()
  140. w.connect("destroy", self.do_quit)
  141. Gtk.main()
  142. def do_create_addnode(self, widget=None, data=None):
  143. n = AddNode()
  144. self.nv.add_with_child(n, n.btnbox)
  145. def do_create_numbernode(self, widget=None, data=None):
  146. n = NumberNode()
  147. self.nv.add_with_child(n, n.spinbutton)
  148. def do_create_printnode(self, widget=None, data=None):
  149. n = PrintNode()
  150. self.nv.add_with_child(n, n.childlabel)
  151. def do_quit(self, widget=None, data=None):
  152. Gtk.main_quit()
  153. sys.exit(0)
  154. if __name__ == "__main__":
  155. Calculator()