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 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 40 #include <qwt_plot_renderer.h> 41 #include <qwt_plot_histogram.h> 43 #include "../raster/qwt5_histogram_item.h" 49 , mRenderer( nullptr )
50 , mHistoPicker( nullptr )
56 mFilter =
new QgsGraduatedHistogramEventFilter(
mPlot );
58 connect( mFilter, SIGNAL( mousePress(
double ) ),
this, SLOT( mousePress(
double ) ) );
59 connect( mFilter, SIGNAL( mouseRelease(
double ) ),
this, SLOT( mouseRelease(
double ) ) );
61 mHistoPicker =
new QwtPlotPicker(
mPlot->canvas() );
62 mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
63 mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
64 #if defined(QWT_VERSION) && QWT_VERSION>=0x060000 65 mHistoPicker->setStateMachine(
new QwtPickerDragPointMachine );
67 mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
86 bool pickerEnabled =
false;
89 setToolTip(
tr(
"Ranges are overlapping and can't be edited by the histogram" ) );
94 setToolTip(
tr(
"Ranges have gaps and can't be edited by the histogram" ) );
106 pickerEnabled =
true;
111 mHistoPicker->setEnabled( pickerEnabled );
112 mFilter->blockSignals( !pickerEnabled );
115 void QgsGraduatedHistogramWidget::mousePress(
double value )
117 mPressedValue = value;
119 int closestRangeIndex = 0;
120 int minPixelDistance = 9999;
121 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
123 if ( minPixelDistance <= 6 )
131 void QgsGraduatedHistogramWidget::mouseRelease(
double value )
133 int closestRangeIndex = 0;
134 int minPixelDistance = 9999;
135 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
137 if ( minPixelDistance <= 6 )
145 if ( value <= mRenderer->ranges().at( closestRangeIndex ).lowerValue() ||
146 value >= mRenderer->
ranges().
at( closestRangeIndex + 1 ).upperValue() )
166 void QgsGraduatedHistogramWidget::findClosestRange(
double value,
int &closestRangeIndex,
int& pixelDistance )
const 170 double minDistance = DBL_MAX;
171 int pressedPixel =
mPlot->canvasMap( QwtPlot::xBottom ).transform( value );
172 for (
int i = 0; i < ranges.
count() - 1; ++i )
174 if ( qAbs( mPressedValue - ranges.
at( i ).upperValue() ) < minDistance )
176 closestRangeIndex = i;
177 minDistance = qAbs( mPressedValue - ranges.
at( i ).upperValue() );
178 pixelDistance = qAbs( pressedPixel -
mPlot->canvasMap( QwtPlot::xBottom ).transform( ranges.
at( i ).upperValue() ) );
185 QgsGraduatedHistogramEventFilter::QgsGraduatedHistogramEventFilter( QwtPlot *plot )
189 mPlot->canvas()->installEventFilter(
this );
194 if ( !
mPlot->isEnabled() )
197 switch ( event->
type() )
199 case QEvent::MouseButtonPress:
202 if ( mouseEvent->
button() == Qt::LeftButton )
204 emit mousePress( posToValue( mouseEvent->
pos() ) );
208 case QEvent::MouseButtonRelease:
211 if ( mouseEvent->
button() == Qt::LeftButton )
213 emit mouseRelease( posToValue( mouseEvent->
pos() ) );
224 double QgsGraduatedHistogramEventFilter::posToValue(
QPointF point )
const 229 return mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.
x() );
QList< QgsRendererRangeV2 > QgsRangeList
void addBreak(double breakValue, bool updateSymbols=true)
Add a breakpoint by splitting existing classes so that the specified value becomes a break between tw...
const T & at(int i) const
QString tr(const char *sourceText, const char *disambiguation, int n)
int count(const T &value) const
const QgsRangeList & ranges() const
bool rangesHaveGaps() const
Tests whether classes assigned to the renderer have gaps between the ranges.
bool updateRangeLowerValue(int rangeIndex, double value)
virtual bool eventFilter(QObject *watched, QEvent *event)
bool rangesOverlap() const
Tests whether classes assigned to the renderer have ranges which overlap.
bool updateRangeUpperValue(int rangeIndex, double value)
const QPoint & pos() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)