29#include "moc_qgscurveeditorwidget.cpp"
31using namespace Qt::StringLiterals;
34#include <qwt_global.h>
35#include <qwt_plot_canvas.h>
37#include <qwt_plot_curve.h>
38#include <qwt_plot_grid.h>
39#include <qwt_plot_marker.h>
40#include <qwt_plot_picker.h>
41#include <qwt_picker_machine.h>
42#include <qwt_plot_layout.h>
43#include <qwt_symbol.h>
44#include <qwt_legend.h>
45#include <qwt_scale_div.h>
46#include <qwt_scale_map.h>
48#include <qwt_plot_renderer.h>
49#include <qwt_plot_histogram.h>
55 mPlot =
new QwtPlot();
56 mPlot->setMinimumSize( QSize( 0, 100 ) );
57 mPlot->setAxisScale( QwtPlot::yLeft, 0, 1 );
58 mPlot->setAxisScale( QwtPlot::yRight, 0, 1 );
59 mPlot->setAxisScale( QwtPlot::xBottom, 0, 1 );
60 mPlot->setAxisScale( QwtPlot::xTop, 0, 1 );
62 QVBoxLayout *vlayout =
new QVBoxLayout();
63 vlayout->addWidget( mPlot );
67 mPlot->setFrameStyle( QFrame::NoFrame );
68 QFrame *plotCanvasFrame =
dynamic_cast<QFrame *
>( mPlot->canvas() );
69 if ( plotCanvasFrame )
70 plotCanvasFrame->setFrameStyle( QFrame::NoFrame );
72 mPlot->enableAxis( QwtPlot::yLeft,
false );
73 mPlot->enableAxis( QwtPlot::xBottom,
false );
76 QwtPlotGrid *grid =
new QwtPlotGrid();
77 const QwtScaleDiv gridDiv( 0.0, 1.0, QList<double>(), QList<double>(), QList<double>() << 0.2 << 0.4 << 0.6 << 0.8 );
78 grid->setXDiv( gridDiv );
79 grid->setYDiv( gridDiv );
80 grid->setPen( QPen( QColor( 0, 0, 0, 50 ) ) );
81 grid->attach( mPlot );
83 mPlotCurve =
new QwtPlotCurve();
84 mPlotCurve->setTitle( u
"Curve"_s );
85 mPlotCurve->setPen( QPen( QColor( 30, 30, 30 ), 0.0 ) ), mPlotCurve->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
86 mPlotCurve->attach( mPlot );
88 mPlotFilter =
new QgsCurveEditorPlotEventFilter( mPlot );
89 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mousePress,
this, &QgsCurveEditorWidget::plotMousePress );
90 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mouseRelease,
this, &QgsCurveEditorWidget::plotMouseRelease );
91 connect( mPlotFilter, &QgsCurveEditorPlotEventFilter::mouseMove,
this, &QgsCurveEditorWidget::plotMouseMove );
93 mPlotCurve->setVisible(
true );
99 if ( mGatherer && mGatherer->isRunning() )
101 connect( mGatherer.get(), &QgsHistogramValuesGatherer::finished, mGatherer.get(), &QgsHistogramValuesGatherer::deleteLater );
103 ( void ) mGatherer.release();
118 mGatherer = std::make_unique<QgsHistogramValuesGatherer>();
119 connect( mGatherer.get(), &QgsHistogramValuesGatherer::calculatedHistogram,
this, [
this] {
120 mHistogram = std::make_unique<QgsHistogram>( mGatherer->histogram() );
125 const bool changed = mGatherer->layer() != layer || mGatherer->expression() != expression;
128 mGatherer->setExpression( expression );
129 mGatherer->setLayer( layer );
131 if ( mGatherer->isRunning() )
135 while ( mGatherer->isRunning() )
137 QCoreApplication::processEvents();
150 mMinValueRange = minValueRange;
156 mMaxValueRange = maxValueRange;
162 if ( event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace )
164 QList<QgsPointXY> cp = mCurve.controlPoints();
165 if ( mCurrentPlotMarkerIndex > 0 && mCurrentPlotMarkerIndex < cp.count() - 1 )
167 cp.removeAt( mCurrentPlotMarkerIndex );
168 mCurve.setControlPoints( cp );
175void QgsCurveEditorWidget::plotMousePress( QPointF point )
177 mCurrentPlotMarkerIndex = findNearestControlPoint( point );
178 if ( mCurrentPlotMarkerIndex < 0 )
182 mCurrentPlotMarkerIndex = findNearestControlPoint( point );
189int QgsCurveEditorWidget::findNearestControlPoint( QPointF point )
const
191 double minDist = 3.0 / mPlot->width();
192 int currentPlotMarkerIndex = -1;
194 const QList<QgsPointXY> controlPoints = mCurve.controlPoints();
196 for (
int i = 0; i < controlPoints.count(); ++i )
198 const QgsPointXY currentPoint = controlPoints.at( i );
200 currentDist = std::pow( point.x() - currentPoint.
x(), 2.0 ) + std::pow( point.y() - currentPoint.
y(), 2.0 );
201 if ( currentDist < minDist )
203 minDist = currentDist;
204 currentPlotMarkerIndex = i;
207 return currentPlotMarkerIndex;
211void QgsCurveEditorWidget::plotMouseRelease( QPointF )
214void QgsCurveEditorWidget::plotMouseMove( QPointF point )
216 if ( mCurrentPlotMarkerIndex < 0 )
219 QList<QgsPointXY> cp = mCurve.controlPoints();
220 bool removePoint =
false;
221 if ( mCurrentPlotMarkerIndex == 0 )
223 point.setX( std::min( point.x(), cp.at( 1 ).x() - 0.01 ) );
227 removePoint = point.x() <= cp.at( mCurrentPlotMarkerIndex - 1 ).x();
229 if ( mCurrentPlotMarkerIndex == cp.count() - 1 )
231 point.setX( std::max( point.x(), cp.at( mCurrentPlotMarkerIndex - 1 ).x() + 0.01 ) );
236 removePoint = removePoint || point.x() >= cp.at( mCurrentPlotMarkerIndex + 1 ).x();
241 cp.removeAt( mCurrentPlotMarkerIndex );
242 mCurrentPlotMarkerIndex = -1;
246 cp[mCurrentPlotMarkerIndex] = QgsPointXY( point.x(), point.y() );
248 mCurve.setControlPoints( cp );
253void QgsCurveEditorWidget::addPlotMarker(
double x,
double y,
bool isSelected )
255 const QColor borderColor( 0, 0, 0 );
257 const QColor brushColor = isSelected ? borderColor : QColor( 255, 255, 255, 0 );
259 QwtPlotMarker *marker =
new QwtPlotMarker();
260 marker->setSymbol(
new QwtSymbol( QwtSymbol::Ellipse, QBrush( brushColor ), QPen( borderColor, isSelected ? 2 : 1 ), QSize( 8, 8 ) ) );
261 marker->setValue( x, y );
262 marker->attach( mPlot );
263 marker->setRenderHint( QwtPlotItem::RenderAntialiased,
true );
267void QgsCurveEditorWidget::updateHistogram()
273 const QBrush histoBrush( QColor( 0, 0, 0, 70 ) );
275 delete mPlotHistogram;
276 mPlotHistogram = createPlotHistogram( histoBrush );
277 QVector<QwtIntervalSample> dataHisto;
280 QList<double> edges = mHistogram->binEdges( bins );
281 const QList<int> counts = mHistogram->counts( bins );
284 const double max = *std::max_element( counts.constBegin(), counts.constEnd() );
289 std::transform( edges.begin(), edges.end(), edges.begin(), [
this](
double d ) ->
double { return ( d - mMinValueRange ) / ( mMaxValueRange - mMinValueRange ); } );
292 for (
int bin = 0; bin < bins; ++bin )
294 const double binValue = counts.at( bin ) / max;
296 const double upperEdge = edges.at( bin + 1 );
298 dataHisto << QwtIntervalSample( binValue, edges.at( bin ), upperEdge );
301 mPlotHistogram->setSamples( dataHisto );
302 mPlotHistogram->attach( mPlot );
306void QgsCurveEditorWidget::updatePlot()
309 const auto constMMarkers = mMarkers;
310 for ( QwtPlotMarker *marker : constMMarkers )
317 QPolygonF curvePoints;
321 const auto constControlPoints = mCurve.controlPoints();
322 for (
const QgsPointXY &point : constControlPoints )
325 addPlotMarker( point.x(), point.y(), mCurrentPlotMarkerIndex == i );
331 for (
double p = 0; p <= 1.0; p += 0.01 )
335 std::sort( x.begin(), x.end() );
336 const QVector<double> y = mCurve.y( x );
338 for (
int j = 0; j < x.count(); ++j )
340 curvePoints << QPointF( x.at( j ), y.at( j ) );
343 mPlotCurve->setSamples( curvePoints );
347QwtPlotHistogram *QgsCurveEditorWidget::createPlotHistogram(
const QBrush &brush,
const QPen &pen )
const
349 QwtPlotHistogram *histogram =
new QwtPlotHistogram( QString() );
350 histogram->setBrush( brush );
351 if ( pen != Qt::NoPen )
353 histogram->setPen( pen );
355 else if ( brush.color().lightness() > 200 )
358 p.setColor( brush.color().darker( 150 ) );
360 p.setCosmetic(
true );
361 histogram->setPen( p );
365 histogram->setPen( QPen( Qt::NoPen ) );
372QgsCurveEditorPlotEventFilter::QgsCurveEditorPlotEventFilter( QwtPlot *plot )
376 mPlot->canvas()->installEventFilter(
this );
379bool QgsCurveEditorPlotEventFilter::eventFilter( QObject *
object, QEvent *event )
381 if ( !mPlot->isEnabled() )
382 return QObject::eventFilter(
object, event );
384 switch ( event->type() )
386 case QEvent::MouseButtonPress:
388 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
389 if ( mouseEvent->button() == Qt::LeftButton )
391 emit mousePress( mapPoint( mouseEvent->pos() ) );
395 case QEvent::MouseMove:
397 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
398 if ( mouseEvent->buttons() & Qt::LeftButton )
401 emit mouseMove( mapPoint( mouseEvent->pos() ) );
405 case QEvent::MouseButtonRelease:
407 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
408 if ( mouseEvent->button() == Qt::LeftButton )
410 emit mouseRelease( mapPoint( mouseEvent->pos() ) );
418 return QObject::eventFilter(
object, event );
421QPointF QgsCurveEditorPlotEventFilter::mapPoint( QPointF point )
const
426 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).