colors.py 5.7 KB

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