scrolling_multidock.py 6.1 KB

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