plotzoomer.cpp
Go to the documentation of this file.
00001 #include "plotzoomer.h"
00002 #include <QMouseEvent>
00003 #include <QApplication>
00004 #include "qwt_scale_map.h"
00005 #include "qwt_plot.h"
00006 
00007 PlotZoomer::PlotZoomer(QWidget *canvas):
00008     QwtPlotZoomer(canvas, true),
00009     _mouse_pressed(false),
00010     _zoom_enabled(false),
00011     _keep_aspect_ratio(true)
00012 {
00013     this->setTrackerMode(AlwaysOff);
00014 }
00015 
00016 void PlotZoomer::widgetMousePressEvent(QMouseEvent *me)
00017 {
00018     _mouse_pressed = false;
00019     auto patterns = this->mousePattern();
00020     for (QwtEventPattern::MousePattern& pattern: patterns)
00021     {
00022         if( this->mouseMatch(pattern, me) ){
00023             _mouse_pressed = true;
00024            // this->setTrackerMode(AlwaysOn);
00025             _initial_pos = me->pos();
00026         }
00027         break;
00028     }
00029     QwtPlotPicker::widgetMousePressEvent( me );
00030 }
00031 
00032 void PlotZoomer::widgetMouseMoveEvent(QMouseEvent *me)
00033 {
00034     static QCursor zoom_cursor(QPixmap(":/icons/resources/light/zoom_in.png"));
00035 
00036     if( _mouse_pressed )
00037     {
00038         auto patterns = this->mousePattern();
00039         for (QwtEventPattern::MousePattern& pattern: patterns)
00040         {
00041             QRect rect( me->pos(), _initial_pos );
00042             QRectF zoomRect = invTransform( rect.normalized() );
00043 
00044             if( zoomRect.width()  > minZoomSize().width() &&
00045                 zoomRect.height() > minZoomSize().height()    )
00046             {
00047                 if( !_zoom_enabled)
00048                 {
00049                     _zoom_enabled = true;
00050                     this->setRubberBand( RectRubberBand );
00051                     this->setTrackerMode(AlwaysOff);
00052                     QApplication::setOverrideCursor(zoom_cursor);
00053                 }
00054             }
00055             else if( _zoom_enabled)
00056             {
00057                _zoom_enabled = false;
00058                this->setRubberBand( NoRubberBand );
00059                QApplication::restoreOverrideCursor();
00060             }
00061             break;
00062         }
00063     }
00064     QwtPlotPicker::widgetMouseMoveEvent( me );
00065 }
00066 
00067 void PlotZoomer::widgetMouseReleaseEvent(QMouseEvent *me)
00068 {
00069     _mouse_pressed = false;
00070     _zoom_enabled = false;
00071     QwtPlotPicker::widgetMouseReleaseEvent( me );
00072     this->setTrackerMode(AlwaysOff);
00073 }
00074 
00075 bool PlotZoomer::accept(QPolygon &pa) const
00076 {
00077     QApplication::restoreOverrideCursor();
00078 
00079     if ( pa.count() < 2 )
00080         return false;
00081 
00082     QRect rect = QRect( pa[0], pa[int( pa.count() ) - 1] );
00083     QRectF zoomRect = invTransform( rect.normalized() );
00084 
00085     if ( zoomRect.width()  < minZoomSize().width() &&
00086          zoomRect.height() < minZoomSize().height() ){
00087         return false;
00088     }
00089     return QwtPlotZoomer::accept(pa);
00090 }
00091 
00092 void PlotZoomer::zoom(const QRectF &zoomRect)
00093 {
00094     QRectF rect = zoomRect;
00095 
00096     if( _keep_aspect_ratio )
00097     {
00098         const QRectF cr = canvas()->contentsRect();
00099         const double canvas_ratio = cr.width() / cr.height();
00100         const double zoom_ratio   = zoomRect.width() / zoomRect.height();
00101 
00102         if( zoom_ratio < canvas_ratio )
00103         {
00104             double new_width = zoomRect.height() * canvas_ratio;
00105             double increment = new_width - zoomRect.width();
00106             rect.setWidth( new_width );
00107             rect.moveLeft( rect.left() - 0.5*increment );
00108         }
00109         else{
00110             double new_height = zoomRect.width() / canvas_ratio;
00111             double increment = new_height - zoomRect.height();
00112             rect.setHeight( new_height );
00113             rect.moveTop( rect.top() - 0.5*increment );
00114         }
00115     }
00116     QwtPlotZoomer::zoom( rect );
00117 }
00118 
00119 QSizeF PlotZoomer::minZoomSize() const
00120 {
00121     return QSizeF(scaleRect().width() * 0.02,
00122                   scaleRect().height() * 0.02);
00123 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:04