colors.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.result.set_value(None)
  36. self.add_button = Gtk.Button.new_with_mnemonic("Add")
  37. self.remove_button = Gtk.Button.new_with_mnemonic("Rem")
  38. self.btnbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL,0)
  39. self.btnbox.add(self.add_button)
  40. self.btnbox.add(self.remove_button)
  41. self.add_button.connect("clicked", self.add_summand)
  42. self.remove_button.connect("clicked", self.remove_summand)
  43. self.set_name("Operation")
  44. def do_calculations(self, dock, val=None):
  45. res = 0
  46. if len(self.summands) == 0:
  47. self.result.set_value(None)
  48. return
  49. for summand in self.summands:
  50. val = summand.get_value(0)
  51. if val is None:
  52. self.result.set_value(None)
  53. return
  54. res += val
  55. self.result.set_value(res)
  56. class NumberNode(ExampleNode):
  57. def __init__(self, number=0):
  58. self.number = GFlow.SimpleSource.new(float(number))
  59. self.number.set_name("output")
  60. self.add_source(self.number)
  61. adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
  62. self.spinbutton = Gtk.SpinButton()
  63. self.spinbutton.set_adjustment(adjustment)
  64. self.spinbutton.set_size_request(50,20)
  65. self.spinbutton.connect("value_changed", self.do_value_changed)
  66. self.set_name("NumberGenerator")
  67. def do_value_changed(self, widget=None, data=None):
  68. self.number.set_value(float(self.spinbutton.get_value()))
  69. class PrintNode(ExampleNode):
  70. def __init__(self):
  71. self.number = GFlow.SimpleSink.new(float(0))
  72. self.number.set_name("")
  73. self.number.connect("changed", self.do_printing)
  74. self.add_sink(self.number)
  75. self.childlabel = Gtk.Label()
  76. self.set_name("Output")
  77. def do_printing(self, dock):
  78. n = self.number.get_value(0)
  79. if n is None:
  80. self.childlabel.set_text("")
  81. else:
  82. self.childlabel.set_text(str(n))
  83. class Calculator(object):
  84. def __init__(self):
  85. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  86. self.nv = GtkFlow.NodeView.new()
  87. self.nv.connect_after("color-calculation", self.do_color_request)
  88. self.sw = Gtk.ScrolledWindow()
  89. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  90. create_numbernode_button = Gtk.Button.new_with_label("Create NumberNode")
  91. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  92. hbox.add(create_numbernode_button)
  93. create_addnode_button = Gtk.Button.new_with_label("Create OperationNode")
  94. create_addnode_button.connect("clicked", self.do_create_addnode)
  95. hbox.add(create_addnode_button)
  96. create_printnode_button = Gtk.Button.new_with_label("Create PrintNode")
  97. create_printnode_button.connect("clicked", self.do_create_printnode)
  98. hbox.add(create_printnode_button)
  99. self.sw.add(self.nv)
  100. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  101. vbox.pack_start(hbox, False, False, 0)
  102. vbox.pack_start(self.sw, True, True, 0)
  103. w.add(vbox)
  104. w.show_all()
  105. w.connect("destroy", self.do_quit)
  106. Gtk.main()
  107. def do_color_request(self, nodeview, value):
  108. if type(value) == float:
  109. if value > 100:
  110. return "ff0000"
  111. elif value < 0:
  112. return "000000"
  113. else:
  114. return "%02x0000"%(int(value/100 * 255),)
  115. print (type (value))
  116. return "000000"
  117. def do_create_addnode(self, widget=None, data=None):
  118. n = AddNode()
  119. self.nv.add_with_child(n, n.btnbox)
  120. def do_create_numbernode(self, widget=None, data=None):
  121. n = NumberNode()
  122. self.nv.add_with_child(n, n.spinbutton)
  123. def do_create_printnode(self, widget=None, data=None):
  124. n = PrintNode()
  125. self.nv.add_with_child(n, n.childlabel)
  126. def do_quit(self, widget=None, data=None):
  127. Gtk.main_quit()
  128. sys.exit(0)
  129. if __name__ == "__main__":
  130. Calculator()