Results 1 to 5 of 5

Thread: Get the plot curve to which the mouse is pointing to

  1. #1
    Join Date
    Jan 2009
    Posts
    17
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Get the plot curve to which the mouse is pointing to

    Hi,

    I have a QwtPlot showing several QwtPlotCurves.
    As the y-values of the curves are spread over a big range, the curves are created by applying a scale factor to the values. That means i.e. curve 1 is created with values divided by 100, curve 2 is created with values multiplied by 10 and so on.
    (By the way, I use only one y-axis as the number of curves can change dynamically. The data unit as well as the scale factor are indicated at the legend, i.e. [ft / 100])
    For better user friendlyness it's needed to indicate the correct original y-value when the mouse is pointing to a certain curve, i.e. 12'300ft. For that purpose I need to figure out the curve the mouse is pointing to in order to apply the correct scale factor to the transformed value.

    Any idea about that?
    Thank's in advance.

    Best Regards
    Stefan

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get the plot curve to which the mouse is pointing to

    A QwtPlotPicker gives you the current position of the mouse ( in screen and plot coordinates ). Then you need to find the closest points of your curves. You can use QwtPlotCurve::closestPoint(), but in most cases you can find a much faster implementation depending on the characteristics of your data.

    When you need to compare the mouse position with the lines between the points you need the pixel position of these points ( use QwtPlot::canvasMap ).

    Maybe looking at the CanvasPicker of the eventfilter example helps.

    Uwe

  3. The following user says thank you to Uwe for this useful post:

    StefanK (9th January 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    17
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get the plot curve to which the mouse is pointing to

    I found a solution by implementing a custom PlotPicker and evaluating following method within the trackerText() method.

    Qt Code:
    1. bool PlotPicker::curveSelected(const QPoint pMousePos, PlotCurve* &pPlotCurve, double &pYValue) const
    2. {
    3. double lDist = 10e10;
    4. int lIndex = -1;
    5.  
    6. // get nearest curve point by iterating over plot items, filtering for curves and calculating minimum distance
    7. const QwtPlotItemList& lPlotItemList = plot()->itemList();
    8. for ( QwtPlotItemIterator lPlotItemIterator = lPlotItemList.begin();
    9. lPlotItemIterator != lPlotItemList.end(); ++lPlotItemIterator )
    10. {
    11. if ( (*lPlotItemIterator)->rtti() == QwtPlotItem::Rtti_PlotCurve )
    12. {
    13. PlotCurve *lTmpCurve = (PlotCurve*)(*lPlotItemIterator);
    14.  
    15. double lTmpDist;
    16. int lTmpIndex = lTmpCurve->closestPoint(pMousePos, &lTmpDist);
    17. if ( lTmpDist < lDist && lTmpIndex > -1)
    18. {
    19. pPlotCurve = lTmpCurve;
    20. lDist = lTmpDist;
    21. lIndex = lTmpIndex;
    22. }
    23. }
    24. }
    25.  
    26. // check if mouse position is in tolerance
    27. if ( pPlotCurve && lDist < _pix_tolerance_ )
    28. {
    29. pYValue = pPlotCurve->y(lIndex);
    30. return true;
    31. }
    32. pPlotCurve = 0;
    33. return false;
    34. }
    35.  
    36. QwtText PlotPicker::trackerText(const QPoint &pMousePos) const
    37. {
    38. QwtText lText;
    39. double lSelectedYValue = 0.0;
    40. PlotCurve* lSelectedCurve = 0;
    41. if(curveSelected(pMousePos, lSelectedCurve, lSelectedYValue)) {
    42. // ... the code creating the label at the cursor position
    43. }
    44. return lText;
    45. }
    To copy to clipboard, switch view to plain text mode 

    In advance I implemented there also a vertical marker following the cursor position. By adding a horizontal marker we would get crosshair functionality...

    Qt Code:
    1. PlotPicker::PlotPicker(QwtPlot *pPlot) :
    2. QwtPlotPicker(pPlot->canvas())
    3. {
    4. // create vertical mouse marker
    5. mMouseMarker = new QwtPlotMarker();
    6. mMouseMarker->attach(pPlot);
    7. mMouseMarker->setVisible(false);
    8. mMouseMarker->setLineStyle(QwtPlotMarker::VLine);
    9. mMouseMarker->setLinePen(QPen(Qt::DotLine));
    10. }
    11.  
    12. void PlotPicker::widgetMouseMoveEvent(QMouseEvent *pMouseEvent)
    13. {
    14. QwtPlotPicker::widgetMouseMoveEvent(pMouseEvent);
    15. qreal lXPosition = invTransform(pMouseEvent->pos()).x();
    16. mMouseMarker->setXValue(lXPosition);
    17. mMouseMarker->setVisible(true);
    18. plot()->replot();
    19. }
    20.  
    21. void PlotPicker::widgetLeaveEvent(QEvent *pEvent)
    22. {
    23. QwtPlotPicker::widgetLeaveEvent(pEvent);
    24. mMouseMarker->setVisible(false);
    25. plot()->replot();
    26. }
    To copy to clipboard, switch view to plain text mode 

    ... and it works!

    Regards
    Stefan

  5. The following user says thank you to StefanK for this useful post:

    cnbp173 (31st January 2009)

  6. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get the plot curve to which the mouse is pointing to

    Your solution adds a marker inside the plot, what means the plot needs to be re-rendered ( replot ) for each mouse movement !
    This might be no problem for trivial scenes, but is by far too slow for more complex ones.

    The rubberband of the picker is implemented using a overlay widget, what is a much faster solution. ( F.e. press + move the mouse in the bode example. ) The picker offers various built in styles ( including crosshair, horizontal/vertical lines ) and you can add your own by overloading QwtPlotPicker::drawRubberband().

    Together with overloading QwtPlotPicker::trackerText() you should be able to implement a much faster solution than using markers.

    Uwe

  7. The following 2 users say thank you to Uwe for this useful post:

    Eos Pengwern (30th November 2009), StefanK (9th January 2009)

  8. #5
    Join Date
    Jan 2009
    Posts
    17
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get the plot curve to which the mouse is pointing to

    I miss the rubberbands remaining always on. The question has already been asked by this thread.
    http://www.qtcentre.org/forum/f-qwt-...ght=rubberband
    What I did by the vertical marker is exactly that, as I did'nt find out another way.

    Regards
    Stefan

Similar Threads

  1. Moving plot marker with the mouse
    By viridis in forum Qwt
    Replies: 2
    Last Post: 23rd September 2008, 21:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.