Results 1 to 8 of 8

Thread: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

  1. #1
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    Hello,

    I'd like to plot a spectrogram with fixed aspect ratio (1:1) and have zooming/unzooming feature. I added the following code to the spectrogram example:
    Qt Code:
    1. QwtPlotRescaler *d_rescaler = new QwtPlotRescaler(canvas());
    2. d_rescaler->setRescalePolicy(QwtPlotRescaler::Fitting);
    3. d_rescaler->setReferenceAxis(QwtPlot::xBottom);
    4. d_rescaler->setAspectRatio(QwtPlot::yLeft, 1.0);
    5. d_rescaler->setAspectRatio(QwtPlot::yRight, 0.0);
    6. d_rescaler->setAspectRatio(QwtPlot::xTop, 0.0);
    7. d_rescaler->setExpandingDirection(QwtPlotRescaler::ExpandBoth);
    8. d_rescaler->setIntervalHint(Qt::XAxis, d_spectrogram->data()->interval( Qt::XAxis ));
    9. d_rescaler->setIntervalHint(Qt::YAxis, d_spectrogram->data()->interval( Qt::YAxis ));
    To copy to clipboard, switch view to plain text mode 

    When I zoom in selecting a rectangular region the original aspect ratio (1:1)
    is not preserved. Also when I resize the window the zoom is not preserved, it gets
    unzoomed to original size.

    What is the proper way of having a zoom and fixed aspect ratio using spectrogram?

    Thanks,
    Dusan

  2. #2
    Join Date
    Apr 2011
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    Hi Dusan,
    Did you ever resolve this issue? If so, could you post your solution? I would like to do the same thing!

  3. #3
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    I finally got a chance to try something that will solve my issues with zooming the spectrogram plot and preserving
    aspect ratio. My workaround requires modifying two files in qwt library (qwt_plot.h and qwt-plot.cpp). The idea is to
    pass the information about the intended aspect ratio (the size of the plot) to the qwt_plot object using on the information
    available in the user's application based on the data to be plotted.

    Attached are 5 files. If you want to test this replace qwt_plot.h and qwt_plot.cpp in qwt/src and recompile the library, and then replace plot.h and plot.cpp
    in examples/spectrogram and recompile the example program. This version of the example program plots the world map in proper aspect ratio
    regardless of the application window size or zoom.

    I'd like to here if someone has more elegant solution, especially if it doesn't require qwt source code modifications.

    Dusan
    Attached Files Attached Files

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

    Default Re: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    Quote Originally Posted by dusan View Post
    I'd like to here if someone has more elegant solution, especially if it doesn't require qwt source code modifications.
    As far as I can see all what your patch does is to pass a rectangle to be used by the layout system instead of the geometry of the plot widget.

    You would have the same effect simply by adjusting the margins ( QWidget::setContentsMargins() ) . Another possible ( IMHO cleaner ) solution is to insert a container widget as parent of the plot widget and to implement its resizeEvent ( setting the geometry of the the plot widget instead of using a QLayout ).

    But I believe that you took the wrong approach at all: better manipulate the zoomer to select rectangles for the specific aspect ratio. Have a look at its base classes and you will find many ways to manipulate the selection of the rectangle. F.e. with overloading QwtPlotPicker::move() you could translate the x ( or y coordinate ) so that the rubberband shows the effective zoom rectangle - taking the aspect ratio into count.

    Uwe

  5. #5
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    Uwe, thanks for the explanation.

    Quote Originally Posted by Uwe View Post
    You would have the same effect simply by adjusting the margins ( QWidget::setContentsMargins() ) .
    I think I actually tried something like that (using setContentsMargins)
    but I think the problem was that the margins need to be reset every time the plot is resized,
    and setting the margins in resize event will trigger new resize event. What's the proper place
    to set the margins.

    Quote Originally Posted by Uwe View Post
    But I believe that you took the wrong approach at all: better manipulate the zoomer to select rectangles for the specific aspect ratio. Have a look at its base classes and you will find many ways to manipulate the selection of the rectangle. F.e. with overloading QwtPlotPicker::move() you could translate the x ( or y coordinate ) so that the rubberband shows the effective zoom rectangle - taking the aspect ratio into count.
    Unfortunately I think I do not understand how to do this. Would you mind making an example?

    Thanks,
    Dusan

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

    Default Re: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    Quote Originally Posted by dusan View Post
    I think I actually tried something like that (using setContentsMargins)
    but I think the problem was that the margins need to be reset every time the plot is resized, ...
    Yes of course, its not different to your implementation - only a different way to set such a rectangle.
    ... and setting the margins in resize event will trigger new resize event.
    Setting margins to widget triggers resize events for its children ( when they are organized in a layout ) but it shouldn't cause any resize event for the widget itsself - why should it ?
    Unfortunately I think I do not understand how to do this.
    Study QwtPlotZoomer and its base classes.

    Uwe

  7. #7
    Join Date
    Dec 2014
    Posts
    9
    Thanks
    2

    Default Re: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    Thread is really old, but here's my solution:

    Qt Code:
    1. class MyPlotZoomer : public QwtPlotZoomer {
    2.  
    3. // ...
    4.  
    5. private:
    6. int zx;
    7. int zy;
    8.  
    9. protected:
    10. virtual void widgetMousePressEvent(QMouseEvent *me);
    11. virtual void move(const QPoint &p);
    12. };
    13.  
    14. void MyPlotZoomer::widgetMousePressEvent(QMouseEvent *me)
    15. {
    16. if ( mouseMatch( MouseSelect1, me ) )
    17. {
    18. zx = me->pos().x();
    19. zy = me->pos().y();
    20. }
    21.  
    22. QwtPlotPicker::widgetMousePressEvent(me);
    23. }
    24.  
    25. void MyPlotZoomer::move(const QPoint &p)
    26. {
    27. QPoint square;
    28.  
    29. if( abs( p.x() - zx )
    30. >
    31. abs( p.y() - zy ) )
    32. {
    33. square.setX( p.x() );
    34.  
    35. if( p.y() > zy )
    36. square.setY( zy + abs( p.x() - zx ) );
    37. else
    38. square.setY( zy - abs( p.x() - zx ) );
    39. }
    40. else
    41. {
    42. square.setY( p.y() );
    43.  
    44. if( p.x() > zx )
    45. square.setX( zx + abs( p.y() - zy ) );
    46. else
    47. square.setX( zx - abs( p.y() - zy ) );
    48. }
    49.  
    50. QwtPlotZoomer::move( square );
    51. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Dec 2014
    Posts
    9
    Thanks
    2

    Default Re: spectrogram with fixed aspect ratio (1:1) and zooming/unzooming

    Ok, this is bad, I cannot edit my incorrect post. It looks like all you need to do in QWT 6.3 is to overload QwtPlotZoomer::zoom and make it call QwtPlotRescaller::rescale()

    Qt Code:
    1. class MyPlotZoomer : public QwtPlotZoomer
    2. {
    3. public:
    4. MyPlotZoomer(QwtPlotCanvas* widget, bool doReplot=true);
    5. QwtPlotRescaler * myPlotRescaler;
    6.  
    7. virtual void zoom(const QRectF& rect);
    8. }
    9.  
    10. MyPlotZoomer::MyPlotZoomer(QwtPlotCanvas* widget, bool doReplot) :
    11. QwtPlotZoomer(widget, doReplot)
    12. {
    13. myPlotRescaler = new QwtPlotRescaler( widget );
    14. //For example:
    15. myPlotRescaler->setRescalePolicy( QwtPlotRescaler::Expanding );
    16. myPlotRescaler->setExpandingDirection( QwtPlotRescaler::ExpandBoth );
    17. myPlotRescaler->setReferenceAxis(QwtPlot::xBottom);
    18. myPlotRescaler->setAspectRatio(QwtPlot::yLeft, 1.0);
    19. myPlotRescaler->setAspectRatio(QwtPlot::yRight, 0.0);
    20. myPlotRescaler->setAspectRatio(QwtPlot::xTop, 0.0);
    21. }
    22.  
    23. MyPlotZoomer::zoom(const QRectF& rect)
    24. {
    25. QwtPlotZoomer::zoom(rect);
    26.  
    27. //Most important
    28. myPlotRescaler->rescale();
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by gavinmagnus; 18th November 2016 at 11:53.

Similar Threads

  1. Replies: 5
    Last Post: 11th June 2011, 15:29
  2. keeping aspect ratio while resizing
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2009, 20:12
  3. Preserving aspect ratio of image
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2009, 13:39
  4. QProcess+mPlayer+Aspect Ratio
    By IGHOR in forum Qt Programming
    Replies: 0
    Last Post: 22nd March 2008, 03:14
  5. aspect ratio of top-level widgets
    By urbangipsy in forum Qt Programming
    Replies: 3
    Last Post: 16th January 2007, 21:49

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.