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() )
150 void QgsGraduatedHistogramWidget::findClosestRange(
double value,
int &closestRangeIndex,
int &pixelDistance )
const 154 double minDistance = std::numeric_limits<double>::max();
155 int pressedPixel =
mPlot->canvasMap( QwtPlot::xBottom ).transform( value );
156 for (
int i = 0; i < ranges.count() - 1; ++i )
158 if ( std::fabs( mPressedValue - ranges.at( i ).upperValue() ) < minDistance )
160 closestRangeIndex = i;
161 minDistance = std::fabs( mPressedValue - ranges.at( i ).upperValue() );
162 pixelDistance = std::fabs( pressedPixel -
mPlot->canvasMap( QwtPlot::xBottom ).transform( ranges.at( i ).upperValue() ) );
169 QgsGraduatedHistogramEventFilter::QgsGraduatedHistogramEventFilter( QwtPlot *plot )
173 mPlot->canvas()->installEventFilter(
this );
176 bool QgsGraduatedHistogramEventFilter::eventFilter( QObject *
object, QEvent *event )
178 if ( !
mPlot->isEnabled() )
179 return QObject::eventFilter(
object, event );
181 switch ( event->type() )
183 case QEvent::MouseButtonPress:
185 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
186 if ( mouseEvent->button() == Qt::LeftButton )
188 emit mousePress( posToValue( mouseEvent->pos() ) );
192 case QEvent::MouseButtonRelease:
194 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
195 if ( mouseEvent->button() == Qt::LeftButton )
197 emit mouseRelease( posToValue( mouseEvent->pos() ) );
205 return QObject::eventFilter(
object, event );
208 double QgsGraduatedHistogramEventFilter::posToValue( QPointF point )
const 213 return mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.x() );
bool updateRangeUpperValue(int rangeIndex, double value)
QList< QgsRendererRange > QgsRangeList
bool rangesOverlap() const
Tests whether classes assigned to the renderer have ranges which overlap.
void addBreak(double breakValue, bool updateSymbols=true)
Add a breakpoint by splitting existing classes so that the specified value becomes a break between tw...
bool rangesHaveGaps() const
Tests whether classes assigned to the renderer have gaps between the ranges.
bool updateRangeLowerValue(int rangeIndex, double value)
const QgsRangeList & ranges() const