18#include "moc_qgscurveeditorwidget.cpp"
27#include <qwt_global.h>
28#include <qwt_plot_canvas.h>
30#include <qwt_plot_curve.h>
31#include <qwt_plot_grid.h>
32#include <qwt_plot_marker.h>
33#include <qwt_plot_picker.h>
34#include <qwt_picker_machine.h>
35#include <qwt_plot_layout.h>
36#include <qwt_symbol.h>
37#include <qwt_legend.h>
38#include <qwt_scale_div.h>
39#include <qwt_scale_map.h>
41#include <qwt_plot_renderer.h>
42#include <qwt_plot_histogram.h>
48 mPlot =
new QwtPlot();
49 mPlot->setMinimumSize( QSize( 0, 100 ) );
50 mPlot->setAxisScale( QwtPlot::yLeft, 0, 1 );
51 mPlot->setAxisScale( QwtPlot::yRight, 0, 1 );
52 mPlot->setAxisScale( QwtPlot::xBottom, 0, 1 );
53 mPlot->setAxisScale( QwtPlot::xTop, 0, 1 );
55 QVBoxLayout *vlayout =
new QVBoxLayout();
56 vlayout->addWidget( mPlot );
60 mPlot->setFrameStyle( QFrame::NoFrame );
61 QFrame *plotCanvasFrame =
dynamic_cast<QFrame *
>( mPlot->canvas() );
62 if ( plotCanvasFrame )
63 plotCanvasFrame->setFrameStyle( QFrame::NoFrame );
65 mPlot->enableAxis( QwtPlot::yLeft,
false );
66 mPlot->enableAxis( QwtPlot::xBottom,
false );
69 QwtPlotGrid *grid =
new QwtPlotGrid();
70 const QwtScaleDiv gridDiv( 0.0, 1.0, QList<double>(), QList<double>(), QList<double>() << 0.2 << 0.4 << 0.6 << 0.8 );
71 grid->setXDiv( gridDiv );
72 grid->setYDiv( gridDiv );
73 grid->setPen( QPen( QColor( 0, 0, 0, 50 ) ) );
74 grid->attach( mPlot );
76 mPlotCurve =
new QwtPlotCurve();
77 mPlotCurve->setTitle( QStringLiteral(
"Curve" ) );
78 mPlotCurve->setPen( QPen( QColor( 30, 30, 30 ), 0.0 ) ),
79 mPlotCurve->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
80 mPlotCurve->attach( mPlot );
82 mPlotFilter =
new QgsCurveEditorPlotEventFilter( mPlot );
83 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mousePress,
this, &QgsCurveEditorWidget::plotMousePress );
84 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mouseRelease,
this, &QgsCurveEditorWidget::plotMouseRelease );
85 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mouseMove,
this, &QgsCurveEditorWidget::plotMouseMove );
87 mPlotCurve->setVisible(
true );
93 if ( mGatherer && mGatherer->isRunning() )
95 connect( mGatherer.get(), &QgsHistogramValuesGatherer::finished, mGatherer.get(), &QgsHistogramValuesGatherer::deleteLater );
97 ( void )mGatherer.release();
112 mGatherer.reset(
new QgsHistogramValuesGatherer() );
113 connect( mGatherer.get(), &QgsHistogramValuesGatherer::calculatedHistogram,
this, [ = ]
115 mHistogram.reset( new QgsHistogram( mGatherer->histogram() ) );
120 const bool changed = mGatherer->layer() != layer || mGatherer->expression() != expression;
123 mGatherer->setExpression( expression );
124 mGatherer->setLayer( layer );
126 if ( mGatherer->isRunning() )
130 while ( mGatherer->isRunning() )
132 QCoreApplication::processEvents();
145 mMinValueRange = minValueRange;
151 mMaxValueRange = maxValueRange;
157 if ( event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace )
160 if ( mCurrentPlotMarkerIndex > 0 && mCurrentPlotMarkerIndex < cp.count() - 1 )
162 cp.removeAt( mCurrentPlotMarkerIndex );
170void QgsCurveEditorWidget::plotMousePress( QPointF point )
172 mCurrentPlotMarkerIndex = findNearestControlPoint( point );
173 if ( mCurrentPlotMarkerIndex < 0 )
177 mCurrentPlotMarkerIndex = findNearestControlPoint( point );
184int QgsCurveEditorWidget::findNearestControlPoint( QPointF point )
const
186 double minDist = 3.0 / mPlot->width();
187 int currentPlotMarkerIndex = -1;
189 const QList< QgsPointXY > controlPoints = mCurve.
controlPoints();
191 for (
int i = 0; i < controlPoints.count(); ++i )
193 const QgsPointXY currentPoint = controlPoints.at( i );
195 currentDist = std::pow( point.x() - currentPoint.
x(), 2.0 ) + std::pow( point.y() - currentPoint.
y(), 2.0 );
196 if ( currentDist < minDist )
198 minDist = currentDist;
199 currentPlotMarkerIndex = i;
202 return currentPlotMarkerIndex;
206void QgsCurveEditorWidget::plotMouseRelease( QPointF )
210void QgsCurveEditorWidget::plotMouseMove( QPointF point )
212 if ( mCurrentPlotMarkerIndex < 0 )
216 bool removePoint =
false;
217 if ( mCurrentPlotMarkerIndex == 0 )
219 point.setX( std::min( point.x(), cp.at( 1 ).x() - 0.01 ) );
223 removePoint = point.x() <= cp.at( mCurrentPlotMarkerIndex - 1 ).x();
225 if ( mCurrentPlotMarkerIndex == cp.count() - 1 )
227 point.setX( std::max( point.x(), cp.at( mCurrentPlotMarkerIndex - 1 ).x() + 0.01 ) );
232 removePoint = removePoint || point.x() >= cp.at( mCurrentPlotMarkerIndex + 1 ).x();
237 cp.removeAt( mCurrentPlotMarkerIndex );
238 mCurrentPlotMarkerIndex = -1;
242 cp[ mCurrentPlotMarkerIndex ] =
QgsPointXY( point.x(), point.y() );
249void QgsCurveEditorWidget::addPlotMarker(
double x,
double y,
bool isSelected )
251 const QColor borderColor( 0, 0, 0 );
253 const QColor brushColor = isSelected ? borderColor : QColor( 255, 255, 255, 0 );
255 QwtPlotMarker *marker =
new QwtPlotMarker();
256 marker->setSymbol(
new QwtSymbol( QwtSymbol::Ellipse, QBrush( brushColor ), QPen( borderColor, isSelected ? 2 : 1 ), QSize( 8, 8 ) ) );
257 marker->setValue( x, y );
258 marker->attach( mPlot );
259 marker->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
263void QgsCurveEditorWidget::updateHistogram()
269 const QBrush histoBrush( QColor( 0, 0, 0, 70 ) );
271 delete mPlotHistogram;
272 mPlotHistogram = createPlotHistogram( histoBrush );
273 QVector<QwtIntervalSample> dataHisto;
276 QList<double> edges = mHistogram->binEdges( bins );
277 const QList<int> counts = mHistogram->counts( bins );
280 const double max = *std::max_element( counts.constBegin(), counts.constEnd() );
285 std::transform( edges.begin(), edges.end(), edges.begin(),
286 [
this](
double d ) ->
double { return ( d - mMinValueRange ) / ( mMaxValueRange - mMinValueRange ); } );
289 for (
int bin = 0; bin < bins; ++bin )
291 const double binValue = counts.at( bin ) / max;
293 const double upperEdge = edges.at( bin + 1 );
295 dataHisto << QwtIntervalSample( binValue, edges.at( bin ), upperEdge );
298 mPlotHistogram->setSamples( dataHisto );
299 mPlotHistogram->attach( mPlot );
303void QgsCurveEditorWidget::updatePlot()
306 const auto constMMarkers = mMarkers;
307 for ( QwtPlotMarker *marker : constMMarkers )
314 QPolygonF curvePoints;
319 for (
const QgsPointXY &point : constControlPoints )
322 addPlotMarker( point.x(), point.y(), mCurrentPlotMarkerIndex == i );
328 for (
double p = 0; p <= 1.0; p += 0.01 )
332 std::sort( x.begin(), x.end() );
333 const QVector< double > y = mCurve.
y( x );
335 for (
int j = 0; j < x.count(); ++j )
337 curvePoints << QPointF( x.at( j ), y.at( j ) );
340 mPlotCurve->setSamples( curvePoints );
344QwtPlotHistogram *QgsCurveEditorWidget::createPlotHistogram(
const QBrush &brush,
const QPen &pen )
const
346 QwtPlotHistogram *histogram =
new QwtPlotHistogram( QString() );
347 histogram->setBrush( brush );
348 if ( pen != Qt::NoPen )
350 histogram->setPen( pen );
352 else if ( brush.color().lightness() > 200 )
355 p.setColor( brush.color().darker( 150 ) );
357 p.setCosmetic(
true );
358 histogram->setPen( p );
362 histogram->setPen( QPen( Qt::NoPen ) );
369QgsCurveEditorPlotEventFilter::QgsCurveEditorPlotEventFilter( QwtPlot *plot )
373 mPlot->canvas()->installEventFilter(
this );
376bool QgsCurveEditorPlotEventFilter::eventFilter( QObject *
object, QEvent *event )
378 if ( !mPlot->isEnabled() )
379 return QObject::eventFilter(
object, event );
381 switch ( event->type() )
383 case QEvent::MouseButtonPress:
385 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
386 if ( mouseEvent->button() == Qt::LeftButton )
388 emit mousePress( mapPoint( mouseEvent->pos() ) );
392 case QEvent::MouseMove:
394 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
395 if ( mouseEvent->buttons() & Qt::LeftButton )
398 emit mouseMove( mapPoint( mouseEvent->pos() ) );
402 case QEvent::MouseButtonRelease:
404 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
405 if ( mouseEvent->button() == Qt::LeftButton )
407 emit mouseRelease( mapPoint( mouseEvent->pos() ) );
415 return QObject::eventFilter(
object, event );
418QPointF QgsCurveEditorPlotEventFilter::mapPoint( QPointF point )
const
423 return QPointF( mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.x() ),
424 mPlot->canvasMap( QwtPlot::yLeft ).invTransform( point.y() ) );
A class to represent a 2D point.
Represents a vector layer which manages a vector based data sets.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)