main.vala 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. class CustomDockLabelFactory : GtkFlow.NodeDockLabelWidgetFactory {
  2. private Gtk.SpinButton button;
  3. private GFlow.Node? node;
  4. public CustomDockLabelFactory(GFlow.Node node) {
  5. base(node);
  6. }
  7. public override Gtk.Widget create_dock_label(GFlow.Dock dock) {
  8. this.node = dock.node;
  9. var test_node = dock.node as TestNode;
  10. if (dock == test_node.source1) {
  11. this.button = new Gtk.SpinButton.with_range(0.0,1.0,0.01);
  12. button.halign = Gtk.Align.END;
  13. button.changed.connect (this.changed);
  14. return button;
  15. }
  16. return base.create_dock_label (dock);
  17. }
  18. private void changed() {
  19. }
  20. }
  21. class CustomNode : GtkFlow.Node {
  22. public CustomNode(TestNode node) {
  23. base.with_margin (node, 50, new CustomDockLabelFactory(node));
  24. set_title (custom_title_factory(this));
  25. var CSS = ".gtkflow_node { background: linear-gradient(0deg, rgba(150,111,136,1) 0%, rgba(175,175,222,1) 35%, rgba(0,212,255,1) 100%); }";
  26. Gtk.CssProvider custom_css = new Gtk.CssProvider();
  27. custom_css.load_from_data(CSS.data);
  28. get_style_context().add_provider(custom_css,Gtk.STYLE_PROVIDER_PRIORITY_USER);
  29. }
  30. private Gtk.Widget custom_title_factory (GtkFlow.Node node) {
  31. var custom_header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 3);
  32. var title_label = new Gtk.Label("");
  33. title_label.set_markup ("<b>%s</b>".printf(node.n.name));
  34. title_label.hexpand = true;
  35. title_label.halign = Gtk.Align.START;
  36. custom_header.append(title_label);
  37. var buttons_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  38. var delete_button = new Gtk.Button();
  39. delete_button.set_label ("Delete");
  40. delete_button.clicked.connect(node.remove);
  41. buttons_box.append(delete_button);
  42. var consumer_select = new Gtk.DropDown.from_strings ({"abc"});
  43. buttons_box.append (consumer_select);
  44. custom_header.append(buttons_box);
  45. return custom_header;
  46. }
  47. }
  48. class TestNode : GFlow.SimpleNode {
  49. public GFlow.SimpleSource source1;
  50. public GFlow.SimpleSource source2;
  51. public GFlow.SimpleSink sink1;
  52. public TestNode(string name) {
  53. this.name = name;
  54. try {
  55. this.source1 = new GFlow.SimpleSource (123);
  56. this.source1.name = "%s source 1".printf(name);
  57. this.add_source(source1);
  58. this.source2 = new GFlow.SimpleSource.with_type(typeof(int));
  59. this.source2.name = "%s source 2".printf(name);
  60. this.add_source(source2);
  61. this.sink1 = new GFlow.SimpleSink.with_type (typeof(int));
  62. this.sink1.name = "%s sink 1".printf(name);
  63. this.add_sink(sink1);
  64. this.sink1.max_sources = 10;
  65. } catch (GFlow.NodeError e) {
  66. warning("Couldn't build node");
  67. }
  68. }
  69. public void register_colors(GtkFlow.NodeView nv) {
  70. var src_widget1 = nv.retrieve_dock(this.source1);
  71. var src_widget2 = nv.retrieve_dock(this.source2);
  72. var snk_widget1 = nv.retrieve_dock(this.sink1);
  73. src_widget1.resolve_color.connect_after((d,v)=>{ return {1.0f,0.0f,0.0f,1.0f};});
  74. src_widget2.resolve_color.connect_after((d,v)=>{ return {0.0f,1.0f,0.0f,1.0f};});
  75. snk_widget1.resolve_color.connect_after((d,v)=>{ return {0.0f,0.0f,1.0f,1.0f};});
  76. }
  77. }
  78. int main (string[] args) {
  79. var app = new Gtk.Application(
  80. "de.grindhold.GtkFlow4Example",
  81. ApplicationFlags.FLAGS_NONE
  82. );
  83. app.activate.connect(() => {
  84. var win = new Gtk.ApplicationWindow(app);
  85. win.set_default_size (400, 300);
  86. var btn = new Gtk.Button.with_label("Spawn Node");
  87. var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  88. var sw = new Gtk.ScrolledWindow();
  89. var nv = new GtkFlow.NodeView();
  90. var mm = new GtkFlow.Minimap();
  91. sw.child=nv;
  92. sw.vexpand=true;
  93. mm.nodeview = nv;
  94. btn.clicked.connect(()=>{
  95. var node = new TestNode("TestNode");
  96. var nn = new CustomNode (node);
  97. nv.add(nn);
  98. });
  99. var n1 = new TestNode("foo");
  100. var gn1 = new GtkFlow.Node(n1);
  101. var n2 = new TestNode("bar");
  102. gn1.add_child(new Gtk.Button.with_label("Button!"));
  103. gn1.highlight_color = {0.6f,1.0f,0.0f,0.3f};
  104. nv.add(gn1);
  105. n1.register_colors(nv);
  106. nv.add(new GtkFlow.Node(n2));
  107. n2.register_colors(nv);
  108. try {
  109. n1.source1.link(n2.sink1);
  110. } catch (Error e) {
  111. warning("Could not link nodes: %s", e.message);
  112. }
  113. win.child = box;
  114. box.append(btn);
  115. box.append(mm);
  116. box.append(sw);
  117. win.present();
  118. });
  119. return app.run(args);
  120. }