27 #include <QMouseEvent>
30 #include <qwt_global.h>
31 #include <qwt_plot_canvas.h>
33 #include <qwt_plot_curve.h>
34 #include <qwt_plot_grid.h>
35 #include <qwt_plot_marker.h>
36 #include <qwt_plot_picker.h>
37 #include <qwt_picker_machine.h>
38 #include <qwt_plot_layout.h>
39 #include <qwt_plot_renderer.h>
40 #include <qwt_plot_histogram.h>
41 #include <qwt_scale_map.h>
50 mFilter =
new QgsGraduatedHistogramEventFilter(
mPlot );
52 connect( mFilter, &QgsGraduatedHistogramEventFilter::mousePress,
this, &QgsGraduatedHistogramWidget::mousePress );
53 connect( mFilter, &QgsGraduatedHistogramEventFilter::mouseRelease,
this, &QgsGraduatedHistogramWidget::mouseRelease );
55 mHistoPicker =
new QwtPlotPicker(
mPlot->canvas() );
56 mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
57 mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
58 mHistoPicker->setStateMachine(
new QwtPickerDragPointMachine );
71 bool pickerEnabled =
false;
74 setToolTip( tr(
"Ranges are overlapping and can't be edited by the histogram" ) );
79 setToolTip( tr(
"Ranges have gaps and can't be edited by the histogram" ) );
82 else if ( mRenderer->
ranges().isEmpty() )
84 setToolTip( QString() );
89 setToolTip( QString() );
96 mHistoPicker->setEnabled( pickerEnabled );
97 mFilter->blockSignals( !pickerEnabled );
100 void QgsGraduatedHistogramWidget::mousePress(
double value )
102 mPressedValue = value;
104 int closestRangeIndex = 0;
105 int minPixelDistance = 9999;
106 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
108 if ( minPixelDistance <= 6 )
116 void QgsGraduatedHistogramWidget::mouseRelease(
double value )
118 int closestRangeIndex = 0;
119 int minPixelDistance = 9999;
120 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
122 if ( minPixelDistance <= 6 )
130 if ( value <= mRenderer->ranges().at( closestRangeIndex ).lowerValue() ||
131 value >= mRenderer->
ranges().at( closestRangeIndex + 1 ).upperValue() )
153 void QgsGraduatedHistogramWidget::findClosestRange(
double value,
int &closestRangeIndex,
int &pixelDistance )
const
157 double minDistance = std::numeric_limits<double>::max();
158 const int pressedPixel =
mPlot->canvasMap( QwtPlot::xBottom ).transform( value );
159 for (
int i = 0; i < ranges.count() - 1; ++i )
161 if ( std::fabs( mPressedValue - ranges.at( i ).upperValue() ) < minDistance )
163 closestRangeIndex = i;
164 minDistance = std::fabs( mPressedValue - ranges.at( i ).upperValue() );
165 pixelDistance = std::fabs( pressedPixel -
mPlot->canvasMap( QwtPlot::xBottom ).transform( ranges.at( i ).upperValue() ) );
172 QgsGraduatedHistogramEventFilter::QgsGraduatedHistogramEventFilter( QwtPlot *plot )
176 mPlot->canvas()->installEventFilter(
this );
179 bool QgsGraduatedHistogramEventFilter::eventFilter( QObject *
object, QEvent *event )
181 if ( !mPlot->isEnabled() )
182 return QObject::eventFilter(
object, event );
184 switch ( event->type() )
186 case QEvent::MouseButtonPress:
188 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
189 if ( mouseEvent->button() == Qt::LeftButton )
191 emit mousePress( posToValue( mouseEvent->pos() ) );
195 case QEvent::MouseButtonRelease:
197 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
198 if ( mouseEvent->button() == Qt::LeftButton )
200 emit mouseRelease( posToValue( mouseEvent->pos() ) );
208 return QObject::eventFilter(
object, event );
211 double QgsGraduatedHistogramEventFilter::posToValue( QPointF point )
const
216 return mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.x() );