QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 "qgis_gui.h"
20#include "qgis_sip.h"
21#include "qgshistogram.h"
23#include "qgsvectorlayer.h"
24
25#include <QMutex>
26#include <QPen>
27#include <QPointer>
28#include <QThread>
29#include <QWidget>
30#include <qwt_global.h>
31
32class QwtPlot;
33class QwtPlotCurve;
34class QwtPlotMarker;
35class QwtPlotHistogram;
36class HistogramItem;
37class QgsCurveEditorPlotEventFilter;
38
39// fix for qwt5/qwt6 QwtDoublePoint vs. QPointF
40typedef QPointF QwtDoublePoint SIP_SKIP;
41
42#ifndef SIP_RUN
43
44// just internal guff - definitely not for exposing to public API!
46
52class QgsHistogramValuesGatherer : public QThread
53{
54 Q_OBJECT
55
56 public:
57 QgsHistogramValuesGatherer() = default;
58
59 void run() override
60 {
61 mWasCanceled = false;
62 if ( mExpression.isEmpty() || !mLayer )
63 {
64 mHistogram.setValues( QList<double>() );
65 return;
66 }
67
68 // allow responsive cancellation
69 mFeedback = new QgsFeedback();
70
71 mHistogram.setValues( mLayer, mExpression, mFeedback );
72
73 // be overly cautious - it's *possible* stop() might be called between deleting mFeedback and nulling it
74 mFeedbackMutex.lock();
75 delete mFeedback;
76 mFeedback = nullptr;
77 mFeedbackMutex.unlock();
78
79 emit calculatedHistogram();
80 }
81
83 void stop()
84 {
85 // be cautious, in case gatherer stops naturally just as we are canceling it and mFeedback gets deleted
86 mFeedbackMutex.lock();
87 if ( mFeedback )
88 mFeedback->cancel();
89 mFeedbackMutex.unlock();
90
91 mWasCanceled = true;
92 }
93
95 bool wasCanceled() const { return mWasCanceled; }
96
97 const QgsHistogram &histogram() const { return mHistogram; }
98
99 const QgsVectorLayer *layer() const
100 {
101 return mLayer;
102 }
103 void setLayer( const QgsVectorLayer *layer )
104 {
105 mLayer = const_cast<QgsVectorLayer *>( layer );
106 }
107
108 QString expression() const
109 {
110 return mExpression;
111 }
112 void setExpression( const QString &expression )
113 {
114 mExpression = expression;
115 }
116
117 signals:
118
122 void calculatedHistogram();
123
124 private:
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 void keyPressEvent( QKeyEvent *event ) override;
213
214 private slots:
215
216 void plotMousePress( QPointF point );
217 void plotMouseRelease( QPointF point );
218 void plotMouseMove( QPointF point );
219
220 private:
221 QgsCurveTransform mCurve;
222
223 QwtPlot *mPlot = nullptr;
224
225 QwtPlotCurve *mPlotCurve = nullptr;
226
227 QList<QwtPlotMarker *> mMarkers;
228 QgsCurveEditorPlotEventFilter *mPlotFilter = nullptr;
229 int mCurrentPlotMarkerIndex = -1;
231 std::unique_ptr<QgsHistogramValuesGatherer> mGatherer;
232 std::unique_ptr<QgsHistogram> mHistogram;
233 double mMinValueRange = 0.0;
234 double mMaxValueRange = 1.0;
235
236 QwtPlotHistogram *mPlotHistogram = nullptr;
237
238 void updatePlot();
239 void addPlotMarker( double x, double y, bool isSelected = false );
240 void updateHistogram();
241
242 int findNearestControlPoint( QPointF point ) const;
243
244 QwtPlotHistogram *createPlotHistogram( const QBrush &brush, const QPen &pen = Qt::NoPen ) const;
245};
246
247
248#ifndef SIP_RUN
249//
250// NOTE:
251// For private only, not part of stable api or exposed to Python bindings
252//
254class GUI_EXPORT QgsCurveEditorPlotEventFilter : public QObject
255{
256 Q_OBJECT
257
258 public:
259 QgsCurveEditorPlotEventFilter( QwtPlot *plot );
260
261 bool eventFilter( QObject *object, QEvent *event ) override;
262
263 signals:
264
265 void mousePress( QPointF );
266 void mouseRelease( QPointF );
267 void mouseMove( QPointF );
268
269 private:
270 QwtPlot *mPlot = nullptr;
271 QPointF mapPoint( QPointF point ) const;
272};
274#endif
275
276#endif // QGSCURVEEDITORWIDGET_H
QgsCurveTransform curve() const
Returns a curve representing the current curve from the widget.
void changed()
Emitted when the widget curve changes.
void keyPressEvent(QKeyEvent *event) override
double maxHistogramValueRange() const
Returns the maximum expected value for the range of values shown in the histogram.
QgsCurveEditorWidget(QWidget *parent=nullptr, const QgsCurveTransform &curve=QgsCurveTransform())
Constructor for QgsCurveEditorWidget.
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 dataset.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:134
QPointF QwtDoublePoint