advanced_calculator.vala 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Compile: $ valac minimal.vala --pkg gtk+-3.0 --pkg gflow-0.10 --pkg gtkflow-0.10
  2. using Gtk;
  3. using GFlow;
  4. public class NumberGeneratorNode : GFlow.SimpleNode {
  5. private GFlow.SimpleSource number_source;
  6. private Gtk.SpinButton spin_button;
  7. public NumberGeneratorNode() {
  8. number_source = new GFlow.SimpleSource.with_type(Type.DOUBLE);
  9. number_source.name = "output";
  10. spin_button = new Gtk.SpinButton(new Gtk.Adjustment(0, 0, 100, 1, 10, 0), 0, 0);
  11. spin_button.set_size_request(50,20);
  12. spin_button.value_changed.connect(() => {
  13. number_source.set_value(spin_button.get_value());
  14. });
  15. name = "NumberGenerator";
  16. add_source(number_source);
  17. }
  18. public void add_to_view(GtkFlow.NodeView view) {
  19. view.add_with_child(this, spin_button);
  20. }
  21. }
  22. public class OperationNode : GFlow.SimpleNode {
  23. private double operand_a_value = 0;
  24. private double operand_b_value = 0;
  25. private string operation = "";
  26. private GFlow.SimpleSink summand_a;
  27. private GFlow.SimpleSink summand_b;
  28. private GFlow.SimpleSource result;
  29. private Gtk.ComboBoxText combobox;
  30. public OperationNode() {
  31. summand_a = new GFlow.SimpleSink.with_type(Type.DOUBLE);
  32. summand_a.name = "operand A";
  33. summand_a.changed.connect(value => {
  34. if (value == null) {
  35. return;
  36. }
  37. operand_a_value = value.get_double();
  38. publish_result();
  39. });
  40. add_sink(summand_a);
  41. summand_b = new GFlow.SimpleSink.with_type(Type.DOUBLE);
  42. summand_b.name = "operand B";
  43. summand_b.changed.connect(value => {
  44. if (value == null) {
  45. return;
  46. }
  47. operand_b_value = value.get_double();
  48. publish_result();
  49. });
  50. add_sink(summand_b);
  51. result = new GFlow.SimpleSource(Type.DOUBLE);
  52. result.name = "result";
  53. add_source(result);
  54. string[] operations = {"+", "-", "*", "/"};
  55. combobox = new Gtk.ComboBoxText();
  56. combobox.changed.connect(() => {
  57. operation = combobox.get_active_text();
  58. });
  59. combobox.set_entry_text_column(0);
  60. foreach (var operation in operations) {
  61. combobox.append_text(operation);
  62. }
  63. name = "Operation";
  64. }
  65. private void publish_result() {
  66. if (operation == "+") {
  67. set_result(operand_a_value + operand_b_value);
  68. } else if (operation == "-") {
  69. set_result(operand_a_value - operand_b_value);
  70. } else if (operation == "*") {
  71. set_result(operand_a_value * operand_b_value);
  72. } else if (operation == "/") {
  73. set_result(operand_a_value / operand_b_value);
  74. }
  75. }
  76. private void set_result(double operation_result) {
  77. print("setting result > %f\n", operation_result);
  78. result.set_value(operation_result);
  79. }
  80. public void add_to_view(GtkFlow.NodeView view) {
  81. view.add_with_child(this, combobox);
  82. }
  83. }
  84. public class PrintNode : GFlow.SimpleNode {
  85. private GFlow.SimpleSink number;
  86. private Gtk.Label childlabel;
  87. public PrintNode() {
  88. number = new GFlow.SimpleSink.with_type(Type.DOUBLE);
  89. number.name = "input";
  90. number.changed.connect(display_value);
  91. childlabel = new Gtk.Label("");
  92. name = "Output";
  93. add_sink(number);
  94. }
  95. public void add_to_view(GtkFlow.NodeView view) {
  96. view.add_with_child(this, childlabel);
  97. }
  98. private void display_value(Value? value) {
  99. if (value == null) {
  100. return;
  101. }
  102. childlabel.set_text(value.strdup_contents());
  103. }
  104. }
  105. public class AdvancedCalculatorWindow : Gtk.Window {
  106. construct {
  107. set_default_size(400, 300);
  108. destroy.connect(Gtk.main_quit);
  109. }
  110. private Gtk.HeaderBar header_bar;
  111. private GtkFlow.NodeView node_view;
  112. public AdvancedCalculatorWindow() {
  113. init_header_bar();
  114. init_window_layout();
  115. init_actions();
  116. }
  117. private void init_header_bar() {
  118. header_bar = new HeaderBar();
  119. header_bar.show_close_button = true;
  120. header_bar.title = "Advaned calculator demo";
  121. set_titlebar(header_bar);
  122. }
  123. private void init_window_layout() {
  124. var scrolled_window = new ScrolledWindow(null, null);
  125. node_view = new GtkFlow.NodeView();
  126. scrolled_window.add(node_view);
  127. add(scrolled_window);
  128. }
  129. private void init_actions() {
  130. add_number_node_action();
  131. add_operation_node_action();
  132. add_print_node_action();
  133. }
  134. private void add_number_node_action() {
  135. var button = new Gtk.Button.with_label("NumberGenerator");
  136. button.clicked.connect(() => {
  137. var node = new NumberGeneratorNode();
  138. node.add_to_view(node_view);
  139. });
  140. header_bar.add(button);
  141. }
  142. private void add_operation_node_action() {
  143. var button = new Gtk.Button.with_label("Operation");
  144. button.clicked.connect(() => {
  145. var node = new OperationNode();
  146. node.add_to_view(node_view);
  147. });
  148. header_bar.add(button);
  149. }
  150. private void add_print_node_action() {
  151. var button = new Gtk.Button.with_label("Print");
  152. button.clicked.connect(() => {
  153. var node = new PrintNode();
  154. node.add_to_view(node_view);
  155. });
  156. header_bar.add(button);
  157. }
  158. }
  159. public void main(string[] args) {
  160. Gtk.init(ref args);
  161. var window = new AdvancedCalculatorWindow();
  162. window.show_all();
  163. Gtk.main();
  164. }