Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual Component Interface #829

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions nengo_gui/components/netgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@ def get_extra_info(self, obj):

info['sp_targets'] = (
nengo_gui.components.spa_plot.SpaPlot.applicable_targets(obj))

# the line number information is injected via nengo_gui.exec_env
if hasattr(obj,"_line_number"):
info['line_number'] = obj._line_number
return info

def send_pan_and_zoom(self, client):
Expand Down
29 changes: 29 additions & 0 deletions nengo_gui/exec_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import sys
from nengo.utils.compat import StringIO

import nengo


# list of Simulators to check for
known_modules = ['nengo', 'nengo_ocl', 'nengo_distilled',
Expand Down Expand Up @@ -45,6 +47,22 @@ def __init__(self, *args, **kwargs):
return DummySimulator


# create a wrapper class for NengoObjects that records the line number
# they were created on
def make_line_number_class(cls):
class LineNumberClass(cls):
_original_class = cls
def __init__(self, *args, **kwargs):
for fn, ln, func, line in reversed(traceback.extract_stack()):
if fn == compiled_filename:
self._line_number = ln
break
else:
self._line_number = None
super(LineNumberClass, self).__init__(*args, **kwargs)
return LineNumberClass


# thread local storage for storing whether we are executing a script
flag = threading.local()

Expand Down Expand Up @@ -110,6 +128,12 @@ def __enter__(self):
self.simulators[mod] = mod.Simulator
mod.Simulator = make_dummy(mod.Simulator)

# install wrapper classes to keep track of creation line numbers
for name in ['Node', 'Network', 'Connection', 'Ensemble']:
setattr(nengo, name,
make_line_number_class(getattr(nengo, name)))


def __exit__(self, exc_type, exc_value, traceback):
for mod, cls in self.simulators.items():
mod.Simulator = cls
Expand All @@ -121,3 +145,8 @@ def __exit__(self, exc_type, exc_value, traceback):
if self.added_directory is not None:
sys.path.remove(self.added_directory)
self.added_directory = None

# remove line number wrapper classes
for name in ['Node', 'Network', 'Connection', 'Ensemble']:
setattr(nengo, name,
getattr(nengo, name)._original_class)
3 changes: 2 additions & 1 deletion nengo_gui/static/ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Nengo.Ace = function (uid, args) {
var code_div = document.createElement('div')
code_div.id = 'editor'
$('#rightpane').append(code_div);
this.editor = ace.edit('editor')
this.editor = ace.edit('editor');
this.editor.$blockScrolling = Infinity;
this.editor.getSession().setMode("ace/mode/python");
this.editor.gotoLine(1);
this.marker = null;
Expand Down
16 changes: 15 additions & 1 deletion nengo_gui/static/components/netgraph.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ g.net rect{
.netgraph g text {
text-anchor: middle;
dominant-baseline: text-before-edge;
cursor: default !important;
}

.netgraph g text:active {
Expand Down Expand Up @@ -63,3 +62,18 @@ rect.view{
dominant-baseline: text-before-edge;
}

.node:hover > rect{
filter: url(#drop-shadow);
}

.ens:hover > g{
filter: url(#drop-shadow);
}

.net:hover > rect{
filter: url(#drop-shadow);
}

.conn:hover{
filter: url(#drop-shadow);
}
38 changes: 28 additions & 10 deletions nengo_gui/static/components/netgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Nengo.NetGraph = function(parent, args) {
this.offsetX = 0; // global x,y pan offset
this.offsetY = 0;

// override the random position chosen by the server for a new item.
// This is used by the drag-and-drop system to ensure that the newly
// created items appear at the position they were dropped at.
this.override_positions = {};
this.override_sizes = {}

var scale = 1.0;
Object.defineProperty(this, 'scale', {
// global scaling factor
Expand Down Expand Up @@ -127,7 +133,7 @@ Nengo.NetGraph = function(parent, args) {

this.width = $(this.svg).width();
this.height = $(this.svg).height();

this.tool_height = $(toolbar.toolbar).height();

/** three separate layers, so that expanded networks are at the back,
Expand Down Expand Up @@ -157,6 +163,9 @@ Nengo.NetGraph = function(parent, args) {
defs.appendChild(s);

this.svg.insertBefore(defs, this.svg.firstChild);
Nengo.create_filter([1,1,1],2,"drop-shadow");
Nengo.create_filter([2,0,0],1,"red-drop-shadow");
Nengo.create_filter([0,2,0],1,"green-drop-shadow");

/** connect to server */
this.ws = Nengo.create_websocket(args.uid);
Expand Down Expand Up @@ -303,8 +312,10 @@ Nengo.NetGraph = function(parent, args) {
if (self.menu.visible_any()) {
self.menu.hide_any();
} else {
self.menu.show(event.clientX, event.clientY,
self.generate_menu());
if(Nengo.vpl.edit_mode == false){
self.menu.show(event.clientX, event.clientY,
self.generate_menu());
}
}
});

Expand Down Expand Up @@ -348,9 +359,9 @@ Nengo.NetGraph.prototype.on_message = function(event) {
item.y = data.pos[1];
item.width = data.size[0];
item.height = data.size[1];

item.redraw();

this.scaleMiniMap();

} else if (data.type === 'config') {
Expand All @@ -373,7 +384,8 @@ Nengo.NetGraph.prototype.on_message = function(event) {
if (item === undefined) {
item = this.svg_conns[data.uid];
}

delete this.override_positions[data.uid];
delete this.override_sizes[data.uid];
item.remove();

} else if (data.type === 'reconnect') {
Expand Down Expand Up @@ -447,6 +459,12 @@ Nengo.NetGraph.prototype.create_object = function(info) {
var item_mini = new Nengo.NetGraphItem(this, info, true);
this.minimap_objects[info.uid] = item_mini;

if(this.override_positions.hasOwnProperty(info.uid)){
info.pos[0] = this.override_positions[info.uid][0];
info.pos[1] = this.override_positions[info.uid][1];
info.size[0] = this.override_sizes[info.uid][0];
info.size[1] = this.override_sizes[info.uid][1];
}
var item = new Nengo.NetGraphItem(this, info, false, item_mini);
this.svg_objects[info.uid] = item;

Expand All @@ -461,7 +479,7 @@ Nengo.NetGraph.prototype.create_object = function(info) {
Nengo.NetGraph.prototype.create_connection = function(info) {
var conn_mini = new Nengo.NetGraphConnection(this, info, true);
this.minimap_conns[info.uid] = conn_mini;

var conn = new Nengo.NetGraphConnection(this, info, false, conn_mini);
this.svg_conns[info.uid] = conn;
};
Expand All @@ -480,7 +498,7 @@ Nengo.NetGraph.prototype.on_resize = function(event) {
var new_width = item.get_screen_width() / this.scale;
var new_height = item.get_screen_height() / this.scale;
item.width = new_width/(2*width);
item.height = new_height/(2*height);
item.height = new_height/(2*height);
}
}
}
Expand Down Expand Up @@ -583,7 +601,7 @@ Nengo.NetGraph.prototype.create_minimap = function () {

this.mm_width = $(this.minimap).width();
this.mm_height = $(this.minimap).height();

// default display minimap
this.mm_display = true;
this.toggleMiniMap();
Expand Down Expand Up @@ -665,7 +683,7 @@ Nengo.NetGraph.prototype.scaleMiniMap = function () {
* main viewport and scale the viewbox to reflect that. */
Nengo.NetGraph.prototype.scaleMiniMapViewBox = function () {
if (!this.mm_display) { return; }

var mm_w = this.mm_width
var mm_h = this.mm_height

Expand Down
30 changes: 26 additions & 4 deletions nengo_gui/static/components/netgraph_conn.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ Nengo.NetGraphConnection = function(ng, info, minimap, mini_conn) {

/** create the line and its arrowhead marker */
this.g = ng.createSVGElement('g');
this.g.classList.add('conn');
this.menu = new Nengo.Menu(this.ng.parent);
var self = this;
$(this.g).bind('contextmenu', function(event) {
event.preventDefault();
event.stopPropagation();
if (self.menu.visible_any()) {
self.menu.hide_any();
} else {
self.menu.show(event.clientX, event.clientY,
self.generate_menu());
}
});

this.create_line();

Expand Down Expand Up @@ -190,15 +203,15 @@ Nengo.NetGraphConnection.prototype.find_post = function() {
Nengo.NetGraphConnection.prototype.set_pres = function(pres) {
this.pres = pres;
this.set_pre(this.find_pre());

if (!this.minimap) {
this.mini_conn.set_pres(pres);
}
}
Nengo.NetGraphConnection.prototype.set_posts = function(posts) {
this.posts = posts;
this.set_post(this.find_post());

if (!this.minimap) {
this.mini_conn.set_posts(posts);
}
Expand Down Expand Up @@ -236,7 +249,7 @@ Nengo.NetGraphConnection.prototype.remove = function() {
this.removed = true;

delete this.ng.svg_conns[this.uid];

if (!this.minimap) {
this.mini_conn.remove();
}
Expand Down Expand Up @@ -341,7 +354,7 @@ Nengo.NetGraphConnection.prototype.redraw = function() {
this.marker.setAttribute('transform',
'translate(' + mx + ',' + my + ') rotate('+ angle +')');
}

if (!this.minimap && this.ng.mm_display) {
this.mini_conn.redraw();
}
Expand Down Expand Up @@ -372,3 +385,12 @@ Nengo.NetGraphConnection.prototype.intersect_length = function(theta, alpha, wid

return [x,y];
}

Nengo.NetGraphConnection.prototype.generate_menu = function(){
var self = this;
var items = [];
items.push(['Delete Connection',function(){
Nengo.vpl.delete_connection(self.pre.uid,self.post.uid);
}]);
return items;
}
Loading