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, bool doReplot): 00008 QwtPlotZoomer(canvas,doReplot), 00009 _mouse_pressed(false), 00010 _zoom_enabled(false) 00011 { 00012 this->setTrackerMode(AlwaysOff); 00013 } 00014 00015 void PlotZoomer::widgetMousePressEvent(QMouseEvent *me) 00016 { 00017 _mouse_pressed = false; 00018 auto patterns = this->mousePattern(); 00019 for (QwtEventPattern::MousePattern& pattern: patterns) 00020 { 00021 if( this->mouseMatch(pattern, me) ){ 00022 _mouse_pressed = true; 00023 this->setTrackerMode(AlwaysOn); 00024 _initial_pos = me->pos(); 00025 } 00026 break; 00027 } 00028 QwtPlotPicker::widgetMousePressEvent( me ); 00029 } 00030 00031 void PlotZoomer::widgetMouseMoveEvent(QMouseEvent *me) 00032 { 00033 static QCursor zoom_cursor(QPixmap(":/icons/resources/zoom_in_32px.png")); 00034 00035 if( _mouse_pressed ) 00036 { 00037 auto patterns = this->mousePattern(); 00038 for (QwtEventPattern::MousePattern& pattern: patterns) 00039 { 00040 QRect rect( me->pos(), _initial_pos ); 00041 QRectF zoomRect = invTransform( rect.normalized() ); 00042 00043 if( zoomRect.width() > minZoomSize().width() && 00044 zoomRect.height() > minZoomSize().height() ) 00045 { 00046 if( !_zoom_enabled) 00047 { 00048 _zoom_enabled = true; 00049 this->setRubberBand( RectRubberBand ); 00050 this->setTrackerMode(AlwaysOff); 00051 QApplication::setOverrideCursor(zoom_cursor); 00052 } 00053 } 00054 else if( _zoom_enabled) 00055 { 00056 _zoom_enabled = false; 00057 this->setRubberBand( NoRubberBand ); 00058 QApplication::restoreOverrideCursor(); 00059 } 00060 break; 00061 } 00062 } 00063 QwtPlotPicker::widgetMouseMoveEvent( me ); 00064 } 00065 00066 void PlotZoomer::widgetMouseReleaseEvent(QMouseEvent *me) 00067 { 00068 _mouse_pressed = false; 00069 _zoom_enabled = false; 00070 QwtPlotPicker::widgetMouseReleaseEvent( me ); 00071 this->setTrackerMode(AlwaysOff); 00072 } 00073 00074 bool PlotZoomer::accept(QPolygon &pa) const 00075 { 00076 QApplication::restoreOverrideCursor(); 00077 00078 if ( pa.count() < 2 ) 00079 return false; 00080 00081 QRect rect = QRect( pa[0], pa[int( pa.count() ) - 1] ); 00082 QRectF zoomRect = invTransform( rect.normalized() ); 00083 00084 if ( zoomRect.width() < minZoomSize().width() && 00085 zoomRect.height() < minZoomSize().height() ){ 00086 return false; 00087 } 00088 return true; 00089 } 00090 00091 QSizeF PlotZoomer::minZoomSize() const 00092 { 00093 return QSizeF(scaleRect().width() * 0.02, 00094 scaleRect().height() * 0.02); 00095 }