help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

creating interactive Qt plots


From: Andreas Weber
Subject: creating interactive Qt plots
Date: Sun, 31 Dec 2017 08:26:22 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2

Dear all,
ich wanted to add some examples to the wiki which shows how to interact
with plots using callbacks.

The idea was to have a triangle and enables the user to drag the corners
around (code follows at the bottom)

https://josoansi.de/files/vokoscreen-2017-12-31_08-17-43.mp4

My first surprise was that the marker and lines have (now?) their own
callback and the figure callback isn't triggered when a line or a marker
is clicked even when there is no callback for the line/marker.

But my main question is: can this be made better/easier/more readable?

Any suggestions are highly welcomed.


clear all
graphics_toolkit qt
set (0, "defaultlinelinewidth", 2);

h.points = rand (2, 3);         # 3 random points
tmp = [h.points h.points(:,1)]; # duplicate first for plotting

h.line = plot (tmp(1,:), tmp(2,:));
hold on

# quick & dirty: create as many separate markers as points
for k = 1:columns (h.points)
  h.marker(k) = plot (h.points(1, k), h.points(2, k), "o", "markersize",
10, "color", "green");
endfor
hold off
h.drag_marker = [];

set (gcf, "userdata", h)

function click_marker (hsrc, evt)

  h = get (gcbf, "userdata");
  set (h.marker, "markerfacecolor", "none");

  ## color marker red
  set (hsrc, "markerfacecolor", "red");

  h.drag_marker = hsrc;
  set (gcbf, "userdata", h);

endfunction

function drag_fig (hsrc, evt)

  # evt 1:left button, 2:middle button, 3:right button
  h = get (gcbf, "userdata");

  if (! isempty (h.drag_marker))
    c = get (gca, "currentpoint")([1 3])';
    set (h.drag_marker, "xdata", c(1));
    set (h.drag_marker, "ydata", c(2));

    # update the line
    x = cell2mat (get(h.marker, "xdata"));
    y = cell2mat (get(h.marker, "ydata"));
    set (h.line, "xdata", [x;x(1)]);
    set (h.line, "ydata", [y;y(1)]);

  endif

endfunction

function stop_drag (hsrc, evt)

  h = get (gcbf, "userdata");
  h.drag_marker = [];
  set (gcbf, "userdata", h);

endfunction

set (h.marker, "buttondownfcn", @click_marker);
set (gcf, "windowbuttonmotionfcn", @drag_fig)
set (gcf, "windowbuttonupfcn", @stop_drag)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]