QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
125 QPointer< const QgsVectorLayer > mLayer = nullptr;
126 QString mExpression;
127 QgsHistogram mHistogram;
128 QgsFeedback *mFeedback = nullptr;
129 QMutex mFeedbackMutex;
130 bool mWasCanceled = false;
131};
132
134
135#endif
136
142class GUI_EXPORT QgsCurveEditorWidget : public QWidget
143{
144 Q_OBJECT
145
146 public:
147
151 QgsCurveEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QgsCurveTransform &curve = QgsCurveTransform() );
152
153 ~QgsCurveEditorWidget() override;
154
159 QgsCurveTransform curve() const { return mCurve; }
160
165 void setCurve( const QgsCurveTransform &curve );
166
174 void setHistogramSource( const QgsVectorLayer *layer, const QString &expression );
175
181 double minHistogramValueRange() const { return mMinValueRange; }
182
188 double maxHistogramValueRange() const { return mMaxValueRange; }
189
190 public slots:
191
197 void setMinHistogramValueRange( double minValueRange );
198
204 void setMaxHistogramValueRange( double maxValueRange );
205
206 signals:
207
209 void changed();
210
211 protected:
212
213 void keyPressEvent( QKeyEvent *event ) override;
214
215 private slots:
216
217 void plotMousePress( QPointF point );
218 void plotMouseRelease( QPointF point );
219 void plotMouseMove( QPointF point );
220
221 private:
222
223 QgsCurveTransform mCurve;
224
225 QwtPlot *mPlot = nullptr;
226
227 QwtPlotCurve *mPlotCurve = nullptr;
228
229 QList< QwtPlotMarker * > mMarkers;
230 QgsCurveEditorPlotEventFilter *mPlotFilter = nullptr;
231 int mCurrentPlotMarkerIndex = -1;
233 std::unique_ptr< QgsHistogramValuesGatherer > mGatherer;
234 std::unique_ptr< QgsHistogram > mHistogram;
235 double mMinValueRange = 0.0;
236 double mMaxValueRange = 1.0;
237
238 QwtPlotHistogram *mPlotHistogram = nullptr;
239
240 void updatePlot();
241 void addPlotMarker( double x, double y, bool isSelected = false );
242 void updateHistogram();
243
244 int findNearestControlPoint( QPointF point ) const;
245
246 QwtPlotHistogram *createPlotHistogram( const QBrush &brush, const QPen &pen = Qt::NoPen ) const;
247
248};
249
250
251
252
253#ifndef SIP_RUN
254//
255// NOTE:
256// For private only, not part of stable api or exposed to Python bindings
257//
259class GUI_EXPORT QgsCurveEditorPlotEventFilter: public QObject
260{
261 Q_OBJECT
262
263 public:
264
265 QgsCurveEditorPlotEventFilter( QwtPlot *plot );
266
267 bool eventFilter( QObject *object, QEvent *event ) override;
268
269 signals:
270
271 void mousePress( QPointF );
272 void mouseRelease( QPointF );
273 void mouseMove( QPointF );
274
275 private:
276
277 QwtPlot *mPlot = nullptr;
278 QPointF mapPoint( QPointF point ) const;
279};
281#endif
282
283#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.
Definition: qgshistogram.h:37
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