QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 { return mLayer; }
100 void setLayer( const QgsVectorLayer *layer ) { mLayer = const_cast<QgsVectorLayer *>( layer ); }
101
102 QString expression() const { return mExpression; }
103 void setExpression( const QString &expression ) { mExpression = expression; }
104
105 signals:
106
110 void calculatedHistogram();
111
112 private:
113 QPointer<const QgsVectorLayer> mLayer = nullptr;
114 QString mExpression;
115 QgsHistogram mHistogram;
116 QgsFeedback *mFeedback = nullptr;
117 QMutex mFeedbackMutex;
118 bool mWasCanceled = false;
119};
120
122
123#endif
124
130class GUI_EXPORT QgsCurveEditorWidget : public QWidget
131{
132 Q_OBJECT
133
134 public:
138 QgsCurveEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QgsCurveTransform &curve = QgsCurveTransform() );
139
140 ~QgsCurveEditorWidget() override;
141
146 QgsCurveTransform curve() const { return mCurve; }
147
152 void setCurve( const QgsCurveTransform &curve );
153
161 void setHistogramSource( const QgsVectorLayer *layer, const QString &expression );
162
168 double minHistogramValueRange() const { return mMinValueRange; }
169
175 double maxHistogramValueRange() const { return mMaxValueRange; }
176
177 public slots:
178
184 void setMinHistogramValueRange( double minValueRange );
185
191 void setMaxHistogramValueRange( double maxValueRange );
192
193 signals:
194
196 void changed();
197
198 protected:
199 void keyPressEvent( QKeyEvent *event ) override;
200
201 private slots:
202
203 void plotMousePress( QPointF point );
204 void plotMouseRelease( QPointF point );
205 void plotMouseMove( QPointF point );
206
207 private:
208 QgsCurveTransform mCurve;
209
210 QwtPlot *mPlot = nullptr;
211
212 QwtPlotCurve *mPlotCurve = nullptr;
213
214 QList<QwtPlotMarker *> mMarkers;
215 QgsCurveEditorPlotEventFilter *mPlotFilter = nullptr;
216 int mCurrentPlotMarkerIndex = -1;
218 std::unique_ptr<QgsHistogramValuesGatherer> mGatherer;
219 std::unique_ptr<QgsHistogram> mHistogram;
220 double mMinValueRange = 0.0;
221 double mMaxValueRange = 1.0;
222
223 QwtPlotHistogram *mPlotHistogram = nullptr;
224
225 void updatePlot();
226 void addPlotMarker( double x, double y, bool isSelected = false );
227 void updateHistogram();
228
229 int findNearestControlPoint( QPointF point ) const;
230
231 QwtPlotHistogram *createPlotHistogram( const QBrush &brush, const QPen &pen = Qt::NoPen ) const;
232};
233
234
235#ifndef SIP_RUN
236//
237// NOTE:
238// For private only, not part of stable api or exposed to Python bindings
239//
241class GUI_EXPORT QgsCurveEditorPlotEventFilter : public QObject
242{
243 Q_OBJECT
244
245 public:
246 QgsCurveEditorPlotEventFilter( QwtPlot *plot );
247
248 bool eventFilter( QObject *object, QEvent *event ) override;
249
250 signals:
251
252 void mousePress( QPointF );
253 void mouseRelease( QPointF );
254 void mouseMove( QPointF );
255
256 private:
257 QwtPlot *mPlot = nullptr;
258 QPointF mapPoint( QPointF point ) const;
259};
261#endif
262
263#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:52
#define SIP_SKIP
Definition qgis_sip.h:133
QPointF QwtDoublePoint