colors.py 5.2 KB

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