QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsgraduatedhistogramwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgraduatedhistogramwidget.cpp
3 -------------------------------
4 begin : May 2015
5 copyright : (C) 2015 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "moc_qgsgraduatedhistogramwidget.cpp"
22#include "qgsapplication.h"
23#include "qgsvectorlayer.h"
25
26#include <QSettings>
27#include <QObject>
28#include <QMouseEvent>
29
30// QWT Charting widget
31#include <qwt_global.h>
32#include <qwt_plot_canvas.h>
33#include <qwt_plot.h>
34#include <qwt_plot_curve.h>
35#include <qwt_plot_grid.h>
36#include <qwt_plot_marker.h>
37#include <qwt_plot_picker.h>
38#include <qwt_picker_machine.h>
39#include <qwt_plot_layout.h>
40#include <qwt_plot_renderer.h>
41#include <qwt_plot_histogram.h>
42#include <qwt_scale_map.h>
43
44
46 : QgsHistogramWidget( parent )
47{
48 //clear x axis title to make more room for graph
49 setXAxisTitle( QString() );
50
51 mFilter = new QgsGraduatedHistogramEventFilter( mPlot );
52
53 connect( mFilter, &QgsGraduatedHistogramEventFilter::mousePress, this, &QgsGraduatedHistogramWidget::mousePress );
54 connect( mFilter, &QgsGraduatedHistogramEventFilter::mouseRelease, this, &QgsGraduatedHistogramWidget::mouseRelease );
55
56 mHistoPicker = new QwtPlotPicker( mPlot->canvas() );
57 mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
58 mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
59 mHistoPicker->setStateMachine( new QwtPickerDragPointMachine );
60}
61
63{
64 mRenderer = renderer;
65}
66
68{
69 if ( !mRenderer )
70 return;
71
72 bool pickerEnabled = false;
73 if ( mRenderer->rangesOverlap() )
74 {
75 setToolTip( tr( "Ranges are overlapping and can't be edited by the histogram" ) );
77 }
78 else if ( mRenderer->rangesHaveGaps() )
79 {
80 setToolTip( tr( "Ranges have gaps and can't be edited by the histogram" ) );
82 }
83 else if ( mRenderer->ranges().isEmpty() )
84 {
85 setToolTip( QString() );
87 }
88 else
89 {
90 setToolTip( QString() );
91 setGraduatedRanges( mRenderer->ranges() );
92 pickerEnabled = true;
93 }
95
96 // histo picker
97 mHistoPicker->setEnabled( pickerEnabled );
98 mFilter->blockSignals( !pickerEnabled );
99}
100
101void QgsGraduatedHistogramWidget::mousePress( double value )
102{
103 mPressedValue = value;
104
105 int closestRangeIndex = 0;
106 int minPixelDistance = 9999;
107 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
108
109 if ( minPixelDistance <= 6 )
110 {
111 //moving a break, so hide the break line
112 mRangeMarkers.at( closestRangeIndex )->hide();
113 mPlot->replot();
114 }
115}
116
117void QgsGraduatedHistogramWidget::mouseRelease( double value )
118{
119 int closestRangeIndex = 0;
120 int minPixelDistance = 9999;
121 findClosestRange( mPressedValue, closestRangeIndex, minPixelDistance );
122
123 if ( minPixelDistance <= 6 )
124 {
125 //do a sanity check - don't allow users to drag a break line
126 //into the middle of a different range. Doing so causes overlap
127 //of the ranges
128
129 //new value needs to be within range covered by closestRangeIndex or
130 //closestRangeIndex + 1
131 if ( value <= mRenderer->ranges().at( closestRangeIndex ).lowerValue() || value >= mRenderer->ranges().at( closestRangeIndex + 1 ).upperValue() )
132 {
133 refresh();
134 return;
135 }
136
137 mRenderer->updateRangeUpperValue( closestRangeIndex, value );
138 mRenderer->updateRangeLowerValue( closestRangeIndex + 1, value );
139 emit rangesModified( false );
140 }
141 else
142 {
143 //if distance from markers is too big, add a break
144 mRenderer->addBreak( value );
145 // to fix the deprecated call to reset() in QgsGraduatedSymbolRendererWidget::refreshRanges,
146 // this class should work on the model in the widget rather than adding break via the renderer.
147 emit rangesModified( true );
148 }
149
150 refresh();
151}
152
153void QgsGraduatedHistogramWidget::findClosestRange( double value, int &closestRangeIndex, int &pixelDistance ) const
154{
155 const QgsRangeList &ranges = mRenderer->ranges();
156
157 double minDistance = std::numeric_limits<double>::max();
158 const int pressedPixel = mPlot->canvasMap( QwtPlot::xBottom ).transform( value );
159 for ( int i = 0; i < ranges.count() - 1; ++i )
160 {
161 if ( std::fabs( mPressedValue - ranges.at( i ).upperValue() ) < minDistance )
162 {
163 closestRangeIndex = i;
164 minDistance = std::fabs( mPressedValue - ranges.at( i ).upperValue() );
165 pixelDistance = std::fabs( pressedPixel - mPlot->canvasMap( QwtPlot::xBottom ).transform( ranges.at( i ).upperValue() ) );
166 }
167 }
168}
169
171
172QgsGraduatedHistogramEventFilter::QgsGraduatedHistogramEventFilter( QwtPlot *plot )
173 : QObject( plot )
174 , mPlot( plot )
175{
176 mPlot->canvas()->installEventFilter( this );
177}
178
179bool QgsGraduatedHistogramEventFilter::eventFilter( QObject *object, QEvent *event )
180{
181 if ( !mPlot->isEnabled() )
182 return QObject::eventFilter( object, event );
183
184 switch ( event->type() )
185 {
186 case QEvent::MouseButtonPress:
187 {
188 const QMouseEvent *mouseEvent = static_cast<QMouseEvent *>( event );
189 if ( mouseEvent->button() == Qt::LeftButton )
190 {
191 emit mousePress( posToValue( mouseEvent->pos() ) );
192 }
193 break;
194 }
195 case QEvent::MouseButtonRelease:
196 {
197 const QMouseEvent *mouseEvent = static_cast<QMouseEvent *>( event );
198 if ( mouseEvent->button() == Qt::LeftButton )
199 {
200 emit mouseRelease( posToValue( mouseEvent->pos() ) );
201 }
202 break;
203 }
204 default:
205 break;
206 }
207
208 return QObject::eventFilter( object, event );
209}
210
211double QgsGraduatedHistogramEventFilter::posToValue( QPointF point ) const
212{
213 if ( !mPlot )
214 return -99999999;
215
216 return mPlot->canvasMap( QwtPlot::xBottom ).invTransform( point.x() );
217}
void rangesModified(bool rangesAdded)
Emitted when the user modifies the graduated ranges using the histogram widget.
QgsGraduatedHistogramWidget(QWidget *parent=nullptr)
QgsGraduatedHistogramWidget constructor.
void setRenderer(QgsGraduatedSymbolRenderer *renderer)
Sets the QgsGraduatedSymbolRenderer renderer associated with the histogram.
void drawHistogram() override
Updates and redraws the histogram.
A vector feature renderer which uses numeric attributes to classify features into different ranges.
void addBreak(double breakValue, bool updateSymbols=true)
Add a breakpoint by splitting existing classes so that the specified value becomes a break between tw...
bool rangesOverlap() const
Tests whether classes assigned to the renderer have ranges which overlap.
bool updateRangeUpperValue(int rangeIndex, double value)
bool updateRangeLowerValue(int rangeIndex, double value)
const QgsRangeList & ranges() const
Returns a list of all ranges used in the classification.
bool rangesHaveGaps() const
Tests whether classes assigned to the renderer have gaps between the ranges.
Graphical histogram for displaying distributions of field values.
QList< QwtPlotMarker * > mRangeMarkers
void refresh()
Redraws the histogram.
void setXAxisTitle(const QString &title)
Sets the title for the histogram's x-axis.
virtual void drawHistogram()
Updates and redraws the histogram.
void setGraduatedRanges(const QgsRangeList &ranges)
Sets the graduated ranges associated with the histogram.
QList< QgsRendererRange > QgsRangeList