Skip to content

Commit

Permalink
Rewrite to widget only, lot of bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikedPaladin committed Jun 12, 2023
1 parent 6189929 commit 8098163
Show file tree
Hide file tree
Showing 22 changed files with 1,588 additions and 2,134 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# GtkFlowGraph
[![Chat on telegram](https://img.shields.io/badge/chat-on%20telegram-0088cc.svg)](http://t.me/codefaq)
Fork of https://notabug.org/grindhold/libgtkflow

![Screenshot](./result.png)

## TODO

A lot
* Add CSS classes to all widgets
* Add interface to create custom renderers for lines
* Render selection rectangle using CSS style
* Add grid support
* ...more
226 changes: 95 additions & 131 deletions example/main.vala
Original file line number Diff line number Diff line change
@@ -1,86 +1,74 @@
public class NumberGeneratorNode : GFlow.SimpleNode {
private GFlow.SimpleSource number_source;
public class NumberGeneratorNode : Flow.Node {
private Flow.Source number_source;
public Gtk.SpinButton spin_button;

public NumberGeneratorNode() {
try {
number_source = new GFlow.SimpleSource.with_type(Type.DOUBLE);
number_source.name = "output";

spin_button = new Gtk.SpinButton(new Gtk.Adjustment(0, 0, 100, 1, 10, 0), 0, 0);
spin_button.set_size_request(50,20);
spin_button.value_changed.connect(() => {
try {
number_source.set_value(spin_button.get_value());
} catch (Error e) {
warning("Couldn't set node value %s", e.message);
}
});

name = "NumberGenerator";
add_source(number_source);
} catch (GFlow.NodeError e) {
warning("Couldn't build node: %s", e.message);
}
}

public void register_colors(GtkFlow.NodeView nv) {
var number_widget = nv.retrieve_dock(number_source);
number_widget.resolve_color.connect_after((d,v) => {
return { 1, 0, 0, 1};
set_label_name("NumberGenerator");

number_source = new Flow.Source.with_type(Type.DOUBLE);
number_source.set_value(0d);
number_source.color = { 1, 0, 0, 1 };
number_source.name = "output";

spin_button = new Gtk.SpinButton(new Gtk.Adjustment(0, 0, 100, 1, 10, 0), 0, 0);
spin_button.set_size_request(50,20);
spin_button.value_changed.connect(() => {
number_source.set_value(spin_button.get_value());
});

add_source(number_source);
}
}

public class OperationNode : GFlow.SimpleNode {
public class OperationNode : Flow.Node {
private double operand_a_value = 0;
private double operand_b_value = 0;
private string operation = "";
private string operation = "+";

private GFlow.SimpleSink summand_a;
private GFlow.SimpleSink summand_b;
private Flow.Sink summand_a;
private Flow.Sink summand_b;

private GFlow.SimpleSource result;
private Flow.Source result;
public Gtk.DropDown drop_down;

public OperationNode() {
try {
name = "Operation";
summand_a = new GFlow.SimpleSink.with_type(Type.DOUBLE);
summand_a.name = "operand A";
summand_a.changed.connect(value => {
if (value == null) {
return;
}
operand_a_value = value.get_double();
publish_result();
});
add_sink(summand_a);

summand_b = new GFlow.SimpleSink.with_type(Type.DOUBLE);
summand_b.name = "operand B";
summand_b.changed.connect(value => {
if (value == null) {
return;
}
operand_b_value = value.get_double();
publish_result();
});
add_sink(summand_b);

result = new GFlow.SimpleSource(Type.DOUBLE);
result.name = "result";
add_source(result);
string[] operations = {"+", "-", "*", "/"};
drop_down = new Gtk.DropDown.from_strings(operations);
drop_down.notify["selected-item"].connect(() => {
operation = operations[drop_down.get_selected()];
});
} catch (GFlow.NodeError e) {
warning("Couldn't build node: %s", e.message);
}
set_label_name("Operation");

result = new Flow.Source(Type.DOUBLE);
result.color = { 0, 1, 1, 1 };
result.name = "result";
add_source(result);

summand_a = new Flow.Sink.with_type(Type.DOUBLE);
summand_a.color = { 1, 0, 0, 1 };
summand_a.name = "operand A";
summand_a.changed.connect(@value => {
if (@value == null) {
return;
}
operand_a_value = @value.get_double();
publish_result();
});
add_sink(summand_a);

summand_b = new Flow.Sink.with_type(Type.DOUBLE);
summand_b.color = { 1, 0, 0, 1 };
summand_b.name = "operand B";
summand_b.changed.connect(@value => {
if (@value == null) {
return;
}
operand_b_value = @value.get_double();
publish_result();
});
add_sink(summand_b);

string[] operations = {"+", "-", "*", "/"};
drop_down = new Gtk.DropDown.from_strings(operations);
drop_down.notify["selected-item"].connect(() => {
operation = operations[drop_down.get_selected()];
publish_result();
});
}

private void publish_result() {
Expand All @@ -96,46 +84,25 @@ public class OperationNode : GFlow.SimpleNode {
}

private void set_result(double operation_result) {
try {
result.set_value(operation_result);
} catch (Error e) {
warning("Couldn't set node value %s", e.message);
}
}

public void register_colors(GtkFlow.NodeView nv) {
var a_widget = nv.retrieve_dock(summand_a);
var b_widget = nv.retrieve_dock(summand_b);
var result = nv.retrieve_dock(result);
a_widget.resolve_color.connect_after((d,v) => {
return { 1, 0, 0, 1};
});
b_widget.resolve_color.connect_after((d,v) => {
return { 1, 0, 0, 1};
});
result.resolve_color.connect_after((d,v) => {
return { 0, 0, 1, 1};
});
result.set_value(operation_result);
}
}

public class PrintNode : GFlow.SimpleNode {
private GFlow.SimpleSink number;
public class PrintNode : Flow.Node {
private Flow.Sink number;
public Gtk.Label label;

public PrintNode() {
try {
number = new GFlow.SimpleSink.with_type(Type.DOUBLE);
number.name = "input";
number.changed.connect(display_value);

label = new Gtk.Label("");

name = "Output";
add_sink(number);
} catch (GFlow.NodeError e) {
warning("Couldn't build node: %s", e.message);
}
set_label_name("Output");

number = new Flow.Sink.with_type(Type.DOUBLE);
number.color = { 0, 0, 1, 1 };
number.name = "input";
number.changed.connect(display_value);

label = new Gtk.Label("");

add_sink(number);
}

private void display_value(Value? value) {
Expand All @@ -144,13 +111,6 @@ public class PrintNode : GFlow.SimpleNode {
}
label.set_text(value.strdup_contents());
}

public void register_colors(GtkFlow.NodeView nv) {
var number_widget = nv.retrieve_dock(number);
number_widget.resolve_color.connect_after((d,v) => {
return { 0, 0, 1, 1};
});
}
}

public class AdvancedCalculatorWindow : Gtk.ApplicationWindow {
Expand All @@ -163,19 +123,24 @@ public class AdvancedCalculatorWindow : Gtk.ApplicationWindow {
}

private Gtk.HeaderBar header_bar;
private Gtk.Box menu_content;
private Gtk.Overlay overlay;

private GtkFlow.NodeView node_view;
private GtkFlow.Minimap minimap;
private Flow.NodeView node_view;
private Flow.Minimap minimap;

public AdvancedCalculatorWindow(Gtk.Application app) {
application = app;
}

private void init_header_bar() {
var title_widget = new Gtk.Label("");
title_widget.set_markup("<b>GtkFlowGraph</b>");

header_bar = new Gtk.HeaderBar() {
title_widget = new Gtk.Label("GtkFlowGraph")
title_widget = title_widget
};

set_titlebar(header_bar);
}

Expand All @@ -186,14 +151,17 @@ public class AdvancedCalculatorWindow : Gtk.ApplicationWindow {
overlay = new Gtk.Overlay();
overlay.set_child(scrolled_window);

node_view = new GtkFlow.NodeView();
node_view = new Flow.NodeView();
menu_content = new Gtk.Box(Gtk.Orientation.VERTICAL, 10);
node_view.menu_content = menu_content;

var minimap_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0) {
halign = Gtk.Align.END,
valign = Gtk.Align.END,
can_target = false
};

minimap = new GtkFlow.Minimap() {
minimap = new Flow.Minimap() {
nodeview = node_view,
can_target = false
};
Expand All @@ -215,46 +183,43 @@ public class AdvancedCalculatorWindow : Gtk.ApplicationWindow {
private void add_number_node_action() {
var button = new Gtk.Button.with_label("NumberGenerator");
button.clicked.connect(() => {
var node = new NumberGeneratorNode();
var gtk_node = new GtkFlow.Node(node);
var gtk_node = new NumberGeneratorNode();

gtk_node.add_child(node.spin_button);
gtk_node.add_child(gtk_node.spin_button);
gtk_node.highlight_color = { 0.6f, 1.0f, 0.0f, 0.3f };
node_view.add(gtk_node);
node_view.move(gtk_node, 20, 20);
node.register_colors(node_view);
});
header_bar.pack_start(button);
button.set_has_frame(false);
menu_content.append(button);
}

private void add_operation_node_action() {
var button = new Gtk.Button.with_label("Operation");
button.clicked.connect(() => {
var node = new OperationNode();
var gtk_node = new GtkFlow.Node(node);
var gtk_node = new OperationNode();

gtk_node.add_child(node.drop_down);
gtk_node.add_child(gtk_node.drop_down);
gtk_node.highlight_color = { 0.6f, 0, 1, 0.3f};
node_view.add(gtk_node);
node_view.move(gtk_node, 220, 20);
node.register_colors(node_view);
});
header_bar.pack_start(button);
button.set_has_frame(false);
menu_content.append(button);
}

private void add_print_node_action() {
var button = new Gtk.Button.with_label("Print");
button.clicked.connect(() => {
var node = new PrintNode();
var gtk_node = new GtkFlow.Node(node);
var gtk_node = new PrintNode();

gtk_node.add_child(node.label);
gtk_node.add_child(gtk_node.label);
gtk_node.highlight_color = { 0.6f ,1, 1, 0.3f };
node_view.add(gtk_node);
node_view.move(gtk_node, 400, 20);
node.register_colors(node_view);
});
header_bar.pack_start(button);
button.set_has_frame(false);
menu_content.append(button);
}
}

Expand All @@ -272,4 +237,3 @@ int main(string[] argv) {

return app.run(argv);
}

16 changes: 8 additions & 8 deletions example/meson.build
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
project_deps = [
dependency('glib-2.0'),
dependency('gobject-2.0'),
example_deps = [
dependency('gtk4'),
lib_dependency
]

project_sources = [
example_sources = [
'main.vala',
]

executable('gtkflowgraph-sample',
project_sources,
executable(
'gtkflowgraph-sample',

example_sources,
vala_args: '--target-glib=2.58',
dependencies: project_deps,
install: true,
dependencies: example_deps,
install: true
)
Binary file modified result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8098163

Please sign in to comment.