creating interactive Qt plots

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

creating interactive Qt plots

Andreas Weber-6
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)

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: creating interactive Qt plots

Andreas Weber-6
Am 31.12.2017 um 08:26 schrieb Andreas Weber:
> ich wanted to add some examples to the wiki which shows how to interact
> with plots using callbacks.

Perhaps this is better:

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

h.points = rand (2, 3);         # 3 random points
h.line = [];
h.marker = [];
set (gcf, "userdata", h)

function down_fig (hsrc, evt)

  h = get (hsrc, "userdata");
  if (isempty (h.marker))
    hold on
    h.marker = plot (NA, NA, "o", "markersize", 15, "color", "green");
    hold off
  endif

  set (hsrc, "userdata", h);
  drag_fig (hsrc, evt);
endfunction

function drag_fig (hsrc, evt)

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

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

    # find nearest point
    d = h.points - c;
    [~, idx] = min (hypot (d(1, :), d(2, :)));
    h.points(:, idx) = c;
  endif

  # draw / update the line
  tmp = [h.points h.points(:,1)]; # duplicate first point to close triangle
  if (isempty (h.line))
    h.line = plot (tmp(1, :), tmp(2, :), "-o");
  else
    set (h.line, "xdata", tmp(1, :));
    set (h.line, "ydata", tmp(2, :));
  endif

  set (hsrc, "userdata", h);

endfunction

function up_fig (hsrc, evt)

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

endfunction

set (gcf, "windowbuttondownfcn", @down_fig);
set (gcf, "windowbuttonmotionfcn", @drag_fig)
set (gcf, "windowbuttonupfcn", @up_fig)

# first update
drag_fig (gcf, [])


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: creating interactive Qt plots

mmuetzel
In reply to this post by Andreas Weber-6
Andreas Weber-6 wrote

> 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.

Hi Andy,

this Mathwork's page gives a good overview of the "PickableParts" and
"HitTest" properties. So what you are describing is the correct behaviour as
long as you leave these properties to their default. That said, I haven't
checked whether these properties work the same in Octave.

Guten Rutsch
Markus

PS: I like your example code. Maybe you could add some logic to prevent
concurrent execution of "drag_fig" (like setting a persistent variable to
true while the callback is running). I found that this is necessary in
Matlab when a function might be called from different callbacks (the
"buttondownfcn" and the "windowbuttonmotionfcn" in your second variant). I
don't know how "forgiving" Octave is in this respect.



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: creating interactive Qt plots

mmuetzel
Reply | Threaded
Open this post in threaded view
|

Re: creating interactive Qt plots

Pantxo
mmuetzel wrote

> Forgot to paste the link:
> https://de.mathworks.com/help/matlab/creating_plots/capturing-mouse-clicks.html
>
>
>
>
> --
> Sent from:
> http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
>
> _______________________________________________
> Help-octave mailing list

> Help-octave@

> https://lists.gnu.org/mailman/listinfo/help-octave

Hi,

I have posted a tentative implementation of "pickableparts" here:

https://savannah.gnu.org/bugs/index.php?52795

Testing is very welcome.

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Aw: Re: creating interactive Qt plots

C.H.M. Broeders
Gesendet: Mittwoch, 03. Januar 2018 um 15:39 Uhr
Von: Pantxo <[hidden email]>
An: [hidden email]
Betreff: Re: creating interactive Qt plots
mmuetzel wrote
> Forgot to paste the link:
> https://de.mathworks.com/help/matlab/creating_plots/capturing-mouse-clicks.html
>
>
>
>
> --
> Sent from:
> http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
>
> _______________________________________________
> Help-octave mailing list

> Help-octave@

> https://lists.gnu.org/mailman/listinfo/help-octave

Hi,

I have posted a tentative implementation of "pickableparts" here:

https://savannah.gnu.org/bugs/index.php?52795

Testing is very welcome.

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

Hi,
is it possible to give a link with info how to test this patch?
Best greetings,
C. Broeders
--
C.H.M. Broeders,       http://chmblh.eu

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave