Modifying cursor behaviour

edited December 2015 in KLayout Development

Hi Matthias,

Thanks for creating a great layout viewing and editing tool.

One of the ways in which the editor could be improved is if there was a second cursor (shaped like a diamond or cross-hairs) that snapped to the grid so that one could see where a path or polygon's starting point will be.

I've perused the code and it seems that such behaviours are handled with ViewService objects (to catch mouse events) and ViewObjects (to draw dynamic objects on the canvas) - is this correct?

Can you provide any hints as to the general approach to take to implement this behaviour?

Thanks for any help.



  • edited November -1

    Hi Simon,

    thanks :-)

    A good starting point for such an implementation is ShapeEditService::do_mouse_move_inactive in This method gets called when the mouse is moved while there is no shape being drawn. The current implementation snaps the coordinates to the grid (or other vertices/edges) and displays the results in the status line.



  • Hi Matthias,

    It's taken a while to come back to this (only four years) but I've finally had a go and I'm a bit stuck.

    Issue 1: I'm using the marker thing. All the other uses of it use what looks like a scaling of 1.0/layout().dbu(). When I try this in do_mouse_move_inactive, KLayout crashes. I'm guessing that layout().dbu() is returniong zero and it's a divide-by-zero issue. Is there something I need to do to make layout().dbu() return the correct answer and not zero?

    Issue 2: I'm trying to draw a box around the snapped co-ordinates. I'd prefer a diamond but I'll get the box working first then upgrade. How to I offset the co-ordinates by a number of pixels rather than a number of (I assume) nano-meters? Otherwise the size of my box will change with zoom factor...

    The code below doesn't crash but isn't doing anything at all it seems. I change the scale factor to 1.0 to avoid the crash but this is wrong. I attempt to make a box that's offset by 10nm from the cursor point by adding/subracting 10 from the x and y co-ordinates. I have no idea if I'm doing this wrong.

    Thanks for any help with this.


    Here is the code:

    ShapeEditService::do_mouse_move_inactive (const db::DPoint &p)
      //  display the next (snapped) position where editing would start
      db::DPoint pp = snap (p);
      //  new code from simon_ch---------------------------------------------------
      db::DPoint pp1 = db::DPoint(p.x()+10,p.y()+10);
      db::DPoint pp2 = db::DPoint(p.x()-10,p.y()-10);
      db::Box b = db::Box (trans () * pp1, trans () * pp2);
      lay::Marker *marker = new lay::Marker (view (), cv_index ());
      //marker->set (b, db::VCplxTrans (1.0 / layout ().dbu ()) * trans ().inverted ());
      marker->set (b, db::VCplxTrans (1.0) * trans ().inverted ());
      set_edit_marker (marker);
      // end new code -------------------------------------------------------------
      std::string pos = std::string ("x: ") + tl::micron_to_string (pp.x ()) + 
                        std::string ("  y: ") + tl::micron_to_string (pp.y ());
      view ()->message (pos);
Sign In or Register to comment.