QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgscurveeditorwidget.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscurveeditorwidget.h
3 ----------------------
4 begin : February 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSCURVEEDITORWIDGET_H
17#define QGSCURVEEDITORWIDGET_H
18
19#include <QWidget>
20#include "qgis_sip.h"
21#include <QThread>
22#include <QMutex>
23#include <QPen>
24#include <QPointer>
25#include <qwt_global.h>
26#include "qgis_gui.h"
28#include "qgshistogram.h"
29#include "qgsvectorlayer.h"
30
31class QwtPlot;
32class QwtPlotCurve;
33class QwtPlotMarker;
34class QwtPlotHistogram;
35class HistogramItem;
36class QgsCurveEditorPlotEventFilter;
37
38// fix for qwt5/qwt6 QwtDoublePoint vs. QPointF
39typedef QPointF QwtDoublePoint SIP_SKIP;
40
41#ifndef SIP_RUN
42
43// just internal guff - definitely not for exposing to public API!
45
51class QgsHistogramValuesGatherer : public QThread
52{
53 Q_OBJECT
54
55 public:
56 QgsHistogramValuesGatherer() = default;
57
58 void run() override
59 {
60 mWasCanceled = false;
61 if ( mExpression.isEmpty() || !mLayer )
62 {
63 mHistogram.setValues( QList<double>() );
64 return;
65 }
66
67 // allow responsive cancellation
68 mFeedback = new QgsFeedback();
69
70 mHistogram.setValues( mLayer, mExpression, mFeedback );
71
72 // be overly cautious - it's *possible* stop() might be called between deleting mFeedback and nulling it
73 mFeedbackMutex.lock();
74 delete mFeedback;
75 mFeedback = nullptr;
76 mFeedbackMutex.unlock();
77
78 emit calculatedHistogram();
79 }
80
82 void stop()
83 {
84 // be cautious, in case gatherer stops naturally just as we are canceling it and mFeedback gets deleted
85 mFeedbackMutex.lock();
86 if ( mFeedback )
87 mFeedback->cancel();
88 mFeedbackMutex.unlock();
89
90 mWasCanceled = true;
91 }
92
94 bool wasCanceled() const { return mWasCanceled; }
95
96 const QgsHistogram &histogram() const { return mHistogram; }
97
98 const QgsVectorLayer *layer() const
99 {
100 return mLayer;
101 }
102 void setLayer( const QgsVectorLayer *layer )
103 {
104 mLayer = const_cast<QgsVectorLayer *>( layer );
105 }
106
107 QString expression() const
108 {
109 return mExpression;
110 }
111 void setExpression( const QString &expression )
112 {
113 mExpression = expression;
114 }
115
116 signals:
117
121 void calculatedHistogram();
122
123 private:
124 QPointer<const QgsVectorLayer> mLayer = nullptr;
125 QString mExpression;
126 QgsHistogram mHistogram;
127 QgsFeedback *mFeedback = nullptr;
128 QMutex mFeedbackMutex;
129 bool mWasCanceled = false;
130};
131
133
134#endif
135
141class GUI_EXPORT QgsCurveEditorWidget : public QWidget
142{
143 Q_OBJECT
144
145 public:
149 QgsCurveEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QgsCurveTransform &curve = QgsCurveTransform() );
150
151 ~QgsCurveEditorWidget() override;
152
157 QgsCurveTransform curve() const { return mCurve; }
158
163 void setCurve( const QgsCurveTransform &curve );
164
172 void setHistogramSource( const QgsVectorLayer *layer, const QString &expression );
173
179 double minHistogramValueRange() const { return mMinValueRange; }
180
186 double maxHistogramValueRange() const { return mMaxValueRange; }
187
188 public slots:
189
195 void setMinHistogramValueRange( double minValueRange );
196
202 void setMaxHistogramValueRange( double maxValueRange );
203
204 signals:
205
207 void changed();
208
209 protected:
210 void keyPressEvent( QKeyEvent *event ) override;
211
212 private slots:
213
214 void plotMousePress( QPointF point );
215 void plotMouseRelease( QPointF point );
216 void plotMouseMove( QPointF point );
217
218 private:
219 QgsCurveTransform mCurve;
220
221 QwtPlot *mPlot = nullptr;
222
223 QwtPlotCurve *mPlotCurve = nullptr;
224
225 QList<QwtPlotMarker *> mMarkers;
226 QgsCurveEditorPlotEventFilter *mPlotFilter = nullptr;
227 int mCurrentPlotMarkerIndex = -1;
229 std::unique_ptr<QgsHistogramValuesGatherer> mGatherer;
230 std::unique_ptr<QgsHistogram> mHistogram;
231 double mMinValueRange = 0.0;
232 double mMaxValueRange = 1.0;
233
234 QwtPlotHistogram *mPlotHistogram = nullptr;
235
236 void updatePlot();
237 void addPlotMarker( double x, double y, bool isSelected = false );
238 void updateHistogram();
239
240 int findNearestControlPoint( QPointF point ) const;
241
242 QwtPlotHistogram *createPlotHistogram( const QBrush &brush, const QPen &pen = Qt::NoPen ) const;
243};
244
245
246#ifndef SIP_RUN
247//
248// NOTE:
249// For private only, not part of stable api or exposed to Python bindings
250//
252class GUI_EXPORT QgsCurveEditorPlotEventFilter : public QObject
253{
254 Q_OBJECT
255
256 public:
257 QgsCurveEditorPlotEventFilter( QwtPlot *plot );
258
259 bool eventFilter( QObject *object, QEvent *event ) override;
260
261 signals:
262
263 void mousePress( QPointF );
264 void mouseRelease( QPointF );
265 void mouseMove( QPointF );
266
267 private:
268 QwtPlot *mPlot = nullptr;
269 QPointF mapPoint( QPointF point ) const;
270};
272#endif
273
274#endif // QGSCURVEEDITORWIDGET_H
A widget for manipulating QgsCurveTransform curves.
QgsCurveTransform curve() const
Returns a curve representing the current curve from the widget.
void changed()
Emitted when the widget curve changes.
double maxHistogramValueRange() const
Returns the maximum expected value for the range of values shown in the histogram.
double minHistogramValueRange() const
Returns the minimum expected value for the range of values shown in the histogram.
Handles scaling of input values to output values by using a curve created from smoothly joining a num...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Calculator for a numeric histogram from a list of values.
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:126
QPointF QwtDoublePoint