28#include "moc_qgscurveeditorwidget.cpp"
31#include <qwt_global.h>
32#include <qwt_plot_canvas.h>
34#include <qwt_plot_curve.h>
35#include <qwt_plot_grid.h>
36#include <qwt_plot_marker.h>
37#include <qwt_plot_picker.h>
38#include <qwt_picker_machine.h>
39#include <qwt_plot_layout.h>
40#include <qwt_symbol.h>
41#include <qwt_legend.h>
42#include <qwt_scale_div.h>
43#include <qwt_scale_map.h>
45#include <qwt_plot_renderer.h>
46#include <qwt_plot_histogram.h>
52 mPlot =
new QwtPlot();
53 mPlot->setMinimumSize( QSize( 0, 100 ) );
54 mPlot->setAxisScale( QwtPlot::yLeft, 0, 1 );
55 mPlot->setAxisScale( QwtPlot::yRight, 0, 1 );
56 mPlot->setAxisScale( QwtPlot::xBottom, 0, 1 );
57 mPlot->setAxisScale( QwtPlot::xTop, 0, 1 );
59 QVBoxLayout *vlayout =
new QVBoxLayout();
60 vlayout->addWidget( mPlot );
64 mPlot->setFrameStyle( QFrame::NoFrame );
65 QFrame *plotCanvasFrame =
dynamic_cast<QFrame *
>( mPlot->canvas() );
66 if ( plotCanvasFrame )
67 plotCanvasFrame->setFrameStyle( QFrame::NoFrame );
69 mPlot->enableAxis( QwtPlot::yLeft,
false );
70 mPlot->enableAxis( QwtPlot::xBottom,
false );
73 QwtPlotGrid *grid =
new QwtPlotGrid();
74 const QwtScaleDiv gridDiv( 0.0, 1.0, QList<double>(), QList<double>(), QList<double>() << 0.2 << 0.4 << 0.6 << 0.8 );
75 grid->setXDiv( gridDiv );
76 grid->setYDiv( gridDiv );
77 grid->setPen( QPen( QColor( 0, 0, 0, 50 ) ) );
78 grid->attach( mPlot );
80 mPlotCurve =
new QwtPlotCurve();
81 mPlotCurve->setTitle( QStringLiteral(
"Curve" ) );
82 mPlotCurve->setPen( QPen( QColor( 30, 30, 30 ), 0.0 ) ),
83 mPlotCurve->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
84 mPlotCurve->attach( mPlot );
86 mPlotFilter =
new QgsCurveEditorPlotEventFilter( mPlot );
87 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mousePress,
this, &QgsCurveEditorWidget::plotMousePress );
88 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mouseRelease,
this, &QgsCurveEditorWidget::plotMouseRelease );
89 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mouseMove,
this, &QgsCurveEditorWidget::plotMouseMove );
91 mPlotCurve->setVisible(
true );
97 if ( mGatherer && mGatherer->isRunning() )
99 connect( mGatherer.get(), &QgsHistogramValuesGatherer::finished, mGatherer.get(), &QgsHistogramValuesGatherer::deleteLater );
101 ( void ) mGatherer.release();
116 mGatherer = std::make_unique<QgsHistogramValuesGatherer>();
117 connect( mGatherer.get(), &QgsHistogramValuesGatherer::calculatedHistogram,
this, [
this] {
118 mHistogram = std::make_unique<QgsHistogram>( mGatherer->histogram() );
123 const bool changed = mGatherer->layer() != layer || mGatherer->expression() != expression;
126 mGatherer->setExpression( expression );
127 mGatherer->setLayer( layer );
129 if ( mGatherer->isRunning() )
133 while ( mGatherer->isRunning() )
135 QCoreApplication::processEvents();
148 mMinValueRange = minValueRange;
154 mMaxValueRange = maxValueRange;
160 if ( event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace )
162 QList<QgsPointXY> cp = mCurve.controlPoints();
163 if ( mCurrentPlotMarkerIndex > 0 && mCurrentPlotMarkerIndex < cp.count() - 1 )
165 cp.removeAt( mCurrentPlotMarkerIndex );
166 mCurve.setControlPoints( cp );
173void QgsCurveEditorWidget::plotMousePress( QPointF point )
175 mCurrentPlotMarkerIndex = findNearestControlPoint( point );
176 if ( mCurrentPlotMarkerIndex < 0 )
180 mCurrentPlotMarkerIndex = findNearestControlPoint( point );
187int QgsCurveEditorWidget::findNearestControlPoint( QPointF point )
const
189 double minDist = 3.0 / mPlot->width();
190 int currentPlotMarkerIndex = -1;
192 const QList<QgsPointXY> controlPoints = mCurve.controlPoints();
194 for (
int i = 0; i < controlPoints.count(); ++i )
196 const QgsPointXY currentPoint = controlPoints.at( i );
198 currentDist = std::pow( point.x() - currentPoint.
x(), 2.0 ) + std::pow( point.y() - currentPoint.
y(), 2.0 );
199 if ( currentDist < minDist )
201 minDist = currentDist;
202 currentPlotMarkerIndex = i;
205 return currentPlotMarkerIndex;
209void QgsCurveEditorWidget::plotMouseRelease( QPointF )
213void QgsCurveEditorWidget::plotMouseMove( QPointF point )
215 if ( mCurrentPlotMarkerIndex < 0 )
218 QList<QgsPointXY> cp = mCurve.controlPoints();
219 bool removePoint =
false;
220 if ( mCurrentPlotMarkerIndex == 0 )
222 point.setX( std::min( point.x(), cp.at( 1 ).x() - 0.01 ) );
226 removePoint = point.x() <= cp.at( mCurrentPlotMarkerIndex - 1 ).x();
228 if ( mCurrentPlotMarkerIndex == cp.count() - 1 )
230 point.setX( std::max( point.x(), cp.at( mCurrentPlotMarkerIndex - 1 ).x() + 0.01 ) );
235 removePoint = removePoint || point.x() >= cp.at( mCurrentPlotMarkerIndex + 1 ).x();
240 cp.removeAt( mCurrentPlotMarkerIndex );
241 mCurrentPlotMarkerIndex = -1;
245 cp[mCurrentPlotMarkerIndex] = QgsPointXY( point.x(), point.y() );
247 mCurve.setControlPoints( cp );
252void QgsCurveEditorWidget::addPlotMarker(
double x,
double y,
bool isSelected )
254 const QColor borderColor( 0, 0, 0 );
256 const QColor brushColor = isSelected ? borderColor : QColor( 255, 255, 255, 0 );
258 QwtPlotMarker *marker =
new QwtPlotMarker();
259 marker->setSymbol(
new QwtSymbol( QwtSymbol::Ellipse, QBrush( brushColor ), QPen( borderColor, isSelected ? 2 : 1 ), QSize( 8, 8 ) ) );
260 marker->setValue( x, y );
261 marker->attach( mPlot );
262 marker->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
266void QgsCurveEditorWidget::updateHistogram()
272 const QBrush histoBrush( QColor( 0, 0, 0, 70 ) );
274 delete mPlotHistogram;
275 mPlotHistogram = createPlotHistogram( histoBrush );
276 QVector<QwtIntervalSample> dataHisto;
279 QList<double> edges = mHistogram->binEdges( bins );
280 const QList<int> counts = mHistogram->counts( bins );
283 const double max = *std::max_element( counts.constBegin(), counts.constEnd() );
288 std::transform( edges.begin(), edges.end(), edges.begin(), [
this](
double d ) ->
double { return ( d - mMinValueRange ) / ( mMaxValueRange - mMinValueRange ); } );
291 for (
int bin = 0; bin < bins; ++bin )
293 const double binValue = counts.at( bin ) / max;
295 const double upperEdge = edges.at( bin + 1 );
297 dataHisto << QwtIntervalSample( binValue, edges.at( bin ), upperEdge );
300 mPlotHistogram->setSamples( dataHisto );
301 mPlotHistogram->attach( mPlot );
305void QgsCurveEditorWidget::updatePlot()
308 const auto constMMarkers = mMarkers;
309 for ( QwtPlotMarker *marker : constMMarkers )
316 QPolygonF curvePoints;
320 const auto constControlPoints = mCurve.controlPoints();
321 for (
const QgsPointXY &point : constControlPoints )
324 addPlotMarker( point.x(), point.y(), mCurrentPlotMarkerIndex == i );
330 for (
double p = 0; p <= 1.0; p += 0.01 )
334 std::sort( x.begin(), x.end() );
335 const QVector<double> y = mCurve.y( x );
337 for (
int j = 0; j < x.count(); ++j )
339 curvePoints << QPointF( x.at( j ), y.at( j ) );
342 mPlotCurve->setSamples( curvePoints );
346QwtPlotHistogram *QgsCurveEditorWidget::createPlotHistogram(
const QBrush &brush,
const QPen &pen )
const
348 QwtPlotHistogram *histogram =
new QwtPlotHistogram( QString() );
349 histogram->setBrush( brush );
350 if ( pen != Qt::NoPen )
352 histogram->setPen( pen );
354 else if ( brush.color().lightness() > 200 )
357 p.setColor( brush.color().darker( 150 ) );
359 p.setCosmetic(
true );
360 histogram->setPen( p );
364 histogram->setPen( QPen( Qt::NoPen ) );
371QgsCurveEditorPlotEventFilter::QgsCurveEditorPlotEventFilter( QwtPlot *plot )
375 mPlot->canvas()->installEventFilter(
this );
378bool QgsCurveEditorPlotEventFilter::eventFilter( QObject *
object, QEvent *event )
380 if ( !mPlot->isEnabled() )
381 return QObject::eventFilter(
object, event );
383 switch ( event->type() )
385 case QEvent::MouseButtonPress:
387 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
388 if ( mouseEvent->button() == Qt::LeftButton )
390 emit mousePress( mapPoint( mouseEvent->pos() ) );
394 case QEvent::MouseMove:
396 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
397 if ( mouseEvent->buttons() & Qt::LeftButton )
400 emit mouseMove( mapPoint( mouseEvent->pos() ) );
404 case QEvent::MouseButtonRelease:
406 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
407 if ( mouseEvent->button() == Qt::LeftButton )
409 emit mouseRelease( mapPoint( mouseEvent->pos() ) );
417 return QObject::eventFilter(
object, event );
420QPointF QgsCurveEditorPlotEventFilter::mapPoint( QPointF point )
const
425 return QPointF( mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.x() ), mPlot->canvasMap( QwtPlot::yLeft ).invTransform( point.y() ) );
Represents a vector layer which manages a vector based dataset.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).