QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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:
150 QgsCurveEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QgsCurveTransform &curve = QgsCurveTransform() );
151
152 ~QgsCurveEditorWidget() override;
153
158 QgsCurveTransform curve() const { return mCurve; }
159
164 void setCurve( const QgsCurveTransform &curve );
165
173 void setHistogramSource( const QgsVectorLayer *layer, const QString &expression );
174
180 double minHistogramValueRange() const { return mMinValueRange; }
181
187 double maxHistogramValueRange() const { return mMaxValueRange; }
188
189 public slots:
190
196 void setMinHistogramValueRange( double minValueRange );
197
203 void setMaxHistogramValueRange( double maxValueRange );
204
205 signals:
206
208 void changed();
209
210 protected:
211 void keyPressEvent( QKeyEvent *event ) override;
212
213 private slots:
214
215 void plotMousePress( QPointF point );
216 void plotMouseRelease( QPointF point );
217 void plotMouseMove( QPointF point );
218
219 private:
220 QgsCurveTransform mCurve;
221
222 QwtPlot *mPlot = nullptr;
223
224 QwtPlotCurve *mPlotCurve = nullptr;
225
226 QList<QwtPlotMarker *> mMarkers;
227 QgsCurveEditorPlotEventFilter *mPlotFilter = nullptr;
228 int mCurrentPlotMarkerIndex = -1;
230 std::unique_ptr<QgsHistogramValuesGatherer> mGatherer;
231 std::unique_ptr<QgsHistogram> mHistogram;
232 double mMinValueRange = 0.0;
233 double mMaxValueRange = 1.0;
234
235 QwtPlotHistogram *mPlotHistogram = nullptr;
236
237 void updatePlot();
238 void addPlotMarker( double x, double y, bool isSelected = false );
239 void updateHistogram();
240
241 int findNearestControlPoint( QPointF point ) const;
242
243 QwtPlotHistogram *createPlotHistogram( const QBrush &brush, const QPen &pen = Qt::NoPen ) const;
244};
245
246
247#ifndef SIP_RUN
248//
249// NOTE:
250// For private only, not part of stable api or exposed to Python bindings
251//
253class GUI_EXPORT QgsCurveEditorPlotEventFilter : public QObject
254{
255 Q_OBJECT
256
257 public:
258 QgsCurveEditorPlotEventFilter( QwtPlot *plot );
259
260 bool eventFilter( QObject *object, QEvent *event ) override;
261
262 signals:
263
264 void mousePress( QPointF );
265 void mouseRelease( QPointF );
266 void mouseMove( QPointF );
267
268 private:
269 QwtPlot *mPlot = nullptr;
270 QPointF mapPoint( QPointF point ) const;
271};
273#endif
274
275#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