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>
49 mFilter =
new QgsGraduatedHistogramEventFilter(
mPlot );
51 connect( mFilter, &QgsGraduatedHistogramEventFilter::mousePress,
this, &QgsGraduatedHistogramWidget::mousePress );
52 connect( mFilter, &QgsGraduatedHistogramEventFilter::mouseRelease,
this, &QgsGraduatedHistogramWidget::mouseRelease );
54 mHistoPicker =
new QwtPlotPicker(
mPlot->canvas() );
55 mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
56 mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
57 mHistoPicker->setStateMachine(
new QwtPickerDragPointMachine );
70 bool pickerEnabled =
false;
73 setToolTip( tr(
"Ranges are overlapping and can't be edited by the histogram" ) );
78 setToolTip( tr(
"Ranges have gaps and can't be edited by the histogram" ) );
81 else if ( mRenderer->
ranges().isEmpty() )
83 setToolTip( QString() );
88 setToolTip( QString() );
95 mHistoPicker->setEnabled( pickerEnabled );
96 mFilter->blockSignals( !pickerEnabled );
99 void QgsGraduatedHistogramWidget::mousePress(
double value )
101 mPressedValue = value;
103 int closestRangeIndex = 0;
104 int minPixelDistance = 9999;
105 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
107 if ( minPixelDistance <= 6 )
115 void QgsGraduatedHistogramWidget::mouseRelease(
double value )
117 int closestRangeIndex = 0;
118 int minPixelDistance = 9999;
119 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
121 if ( minPixelDistance <= 6 )
129 if ( value <= mRenderer->ranges().at( closestRangeIndex ).lowerValue() ||
130 value >= mRenderer->
ranges().at( closestRangeIndex + 1 ).upperValue() )
152 void QgsGraduatedHistogramWidget::findClosestRange(
double value,
int &closestRangeIndex,
int &pixelDistance )
const
156 double minDistance = std::numeric_limits<double>::max();
157 int pressedPixel =
mPlot->canvasMap( QwtPlot::xBottom ).transform( value );
158 for (
int i = 0; i < ranges.count() - 1; ++i )
160 if ( std::fabs( mPressedValue - ranges.at( i ).upperValue() ) < minDistance )
162 closestRangeIndex = i;
163 minDistance = std::fabs( mPressedValue - ranges.at( i ).upperValue() );
164 pixelDistance = std::fabs( pressedPixel -
mPlot->canvasMap( QwtPlot::xBottom ).transform( ranges.at( i ).upperValue() ) );
171 QgsGraduatedHistogramEventFilter::QgsGraduatedHistogramEventFilter( QwtPlot *plot )
175 mPlot->canvas()->installEventFilter(
this );
178 bool QgsGraduatedHistogramEventFilter::eventFilter( QObject *
object, QEvent *event )
180 if ( !mPlot->isEnabled() )
181 return QObject::eventFilter(
object, event );
183 switch ( event->type() )
185 case QEvent::MouseButtonPress:
187 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
188 if ( mouseEvent->button() == Qt::LeftButton )
190 emit mousePress( posToValue( mouseEvent->pos() ) );
194 case QEvent::MouseButtonRelease:
196 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
197 if ( mouseEvent->button() == Qt::LeftButton )
199 emit mouseRelease( posToValue( mouseEvent->pos() ) );
207 return QObject::eventFilter(
object, event );
210 double QgsGraduatedHistogramEventFilter::posToValue( QPointF point )
const
215 return mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.x() );