Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_plot_grid.cpp
1/******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_plot_grid.h"
11#include "qwt_painter.h"
12#include "qwt_text.h"
13#include "qwt_scale_map.h"
14#include "qwt_scale_div.h"
15
16#include <qpainter.h>
17#include <qpen.h>
18
19 static inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
20{
21 return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
22}
23
24 class QwtPlotGrid::PrivateData
25{
26 public:
27 PrivateData()
28 : xEnabled( true )
29 , yEnabled( true )
30 , xMinEnabled( false )
31 , yMinEnabled( false )
32 {
33 }
34
35 bool xEnabled;
36 bool yEnabled;
37 bool xMinEnabled;
38 bool yMinEnabled;
39
40 QwtScaleDiv xScaleDiv;
41 QwtScaleDiv yScaleDiv;
42
43 QPen majorPen;
44 QPen minorPen;
45};
46
48 QwtPlotGrid::QwtPlotGrid()
49 : QwtPlotItem( QwtText( "Grid" ) )
50{
51 m_data = new PrivateData;
52
53 setItemInterest( QwtPlotItem::ScaleInterest, true );
54 setZ( 10.0 );
55}
56
58 QwtPlotGrid::~QwtPlotGrid()
59{
60 delete m_data;
61}
62
64 int QwtPlotGrid::rtti() const
65{
66 return QwtPlotItem::Rtti_PlotGrid;
67}
68
76 void QwtPlotGrid::enableX( bool on )
77{
78 if ( m_data->xEnabled != on )
79 {
80 m_data->xEnabled = on;
81
82 legendChanged();
83 itemChanged();
84 }
85}
86
92 void QwtPlotGrid::enableY( bool on )
93{
94 if ( m_data->yEnabled != on )
95 {
96 m_data->yEnabled = on;
97
98 legendChanged();
99 itemChanged();
100 }
101}
102
108 void QwtPlotGrid::enableXMin( bool on )
109{
110 if ( m_data->xMinEnabled != on )
111 {
112 m_data->xMinEnabled = on;
113
114 legendChanged();
115 itemChanged();
116 }
117}
118
124 void QwtPlotGrid::enableYMin( bool on )
125{
126 if ( m_data->yMinEnabled != on )
127 {
128 m_data->yMinEnabled = on;
129
130 legendChanged();
131 itemChanged();
132 }
133}
134
140 void QwtPlotGrid::setXDiv( const QwtScaleDiv& scaleDiv )
141{
142 if ( m_data->xScaleDiv != scaleDiv )
143 {
144 m_data->xScaleDiv = scaleDiv;
145 itemChanged();
146 }
147}
148
154 void QwtPlotGrid::setYDiv( const QwtScaleDiv& scaleDiv )
155{
156 if ( m_data->yScaleDiv != scaleDiv )
157 {
158 m_data->yScaleDiv = scaleDiv;
159 itemChanged();
160 }
161}
162
176 void QwtPlotGrid::setPen( const QColor& color, qreal width, Qt::PenStyle style )
177{
178 setPen( QPen( color, width, style ) );
179}
180
187 void QwtPlotGrid::setPen( const QPen& pen )
188{
189 if ( m_data->majorPen != pen || m_data->minorPen != pen )
190 {
191 m_data->majorPen = pen;
192 m_data->minorPen = pen;
193
194 legendChanged();
195 itemChanged();
196 }
197}
198
212 void QwtPlotGrid::setMajorPen( const QColor& color, qreal width, Qt::PenStyle style )
213{
214 setMajorPen( QPen( color, width, style ) );
215}
216
223 void QwtPlotGrid::setMajorPen( const QPen& pen )
224{
225 if ( m_data->majorPen != pen )
226 {
227 m_data->majorPen = pen;
228
229 legendChanged();
230 itemChanged();
231 }
232}
233
247 void QwtPlotGrid::setMinorPen( const QColor& color, qreal width, Qt::PenStyle style )
248{
249 setMinorPen( QPen( color, width, style ) );
250}
251
258 void QwtPlotGrid::setMinorPen( const QPen& pen )
259{
260 if ( m_data->minorPen != pen )
261 {
262 m_data->minorPen = pen;
263
264 legendChanged();
265 itemChanged();
266 }
267}
268
282 void QwtPlotGrid::draw( QPainter* painter,
283 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
284 const QRectF& canvasRect ) const
285{
286 // draw minor grid lines
287 QPen minorPen = m_data->minorPen;
288 minorPen.setCapStyle( Qt::FlatCap );
289
290 painter->setPen( minorPen );
291
292 if ( m_data->xEnabled && m_data->xMinEnabled )
293 {
294 drawLines( painter, canvasRect, Qt::Vertical, xMap,
295 m_data->xScaleDiv.ticks( QwtScaleDiv::MinorTick ) );
296 drawLines( painter, canvasRect, Qt::Vertical, xMap,
297 m_data->xScaleDiv.ticks( QwtScaleDiv::MediumTick ) );
298 }
299
300 if ( m_data->yEnabled && m_data->yMinEnabled )
301 {
302 drawLines( painter, canvasRect, Qt::Horizontal, yMap,
303 m_data->yScaleDiv.ticks( QwtScaleDiv::MinorTick ) );
304 drawLines( painter, canvasRect, Qt::Horizontal, yMap,
305 m_data->yScaleDiv.ticks( QwtScaleDiv::MediumTick ) );
306 }
307
308 // draw major grid lines
309 QPen majorPen = m_data->majorPen;
310 majorPen.setCapStyle( Qt::FlatCap );
311
312 painter->setPen( majorPen );
313
314 if ( m_data->xEnabled )
315 {
316 drawLines( painter, canvasRect, Qt::Vertical, xMap,
317 m_data->xScaleDiv.ticks( QwtScaleDiv::MajorTick ) );
318 }
319
320 if ( m_data->yEnabled )
321 {
322 drawLines( painter, canvasRect, Qt::Horizontal, yMap,
323 m_data->yScaleDiv.ticks( QwtScaleDiv::MajorTick ) );
324 }
325}
326
327 void QwtPlotGrid::drawLines( QPainter* painter, const QRectF& canvasRect,
328 Qt::Orientation orientation, const QwtScaleMap& scaleMap,
329 const QList< double >& values ) const
330{
331 const double x1 = canvasRect.left();
332 const double x2 = canvasRect.right() - 1.0;
333 const double y1 = canvasRect.top();
334 const double y2 = canvasRect.bottom() - 1.0;
335
336 const bool doAlign = QwtPainter::roundingAlignment( painter );
337
338 for ( int i = 0; i < values.count(); i++ )
339 {
340 double value = scaleMap.transform( values[i] );
341 if ( doAlign )
342 value = qRound( value );
343
344 if ( orientation == Qt::Horizontal )
345 {
346 if ( qwtFuzzyGreaterOrEqual( value, y1 ) &&
347 qwtFuzzyGreaterOrEqual( y2, value ) )
348 {
349 QwtPainter::drawLine( painter, x1, value, x2, value );
350 }
351 }
352 else
353 {
354 if ( qwtFuzzyGreaterOrEqual( value, x1 ) &&
355 qwtFuzzyGreaterOrEqual( x2, value ) )
356 {
357 QwtPainter::drawLine( painter, value, y1, value, y2 );
358 }
359 }
360 }
361}
362
367 const QPen& QwtPlotGrid::majorPen() const
368{
369 return m_data->majorPen;
370}
371
376 const QPen& QwtPlotGrid::minorPen() const
377{
378 return m_data->minorPen;
379}
380
385 bool QwtPlotGrid::xEnabled() const
386{
387 return m_data->xEnabled;
388}
389
394 bool QwtPlotGrid::xMinEnabled() const
395{
396 return m_data->xMinEnabled;
397}
398
403 bool QwtPlotGrid::yEnabled() const
404{
405 return m_data->yEnabled;
406}
407
412 bool QwtPlotGrid::yMinEnabled() const
413{
414 return m_data->yMinEnabled;
415}
416
417
419 const QwtScaleDiv& QwtPlotGrid::xScaleDiv() const
420{
421 return m_data->xScaleDiv;
422}
423
425 const QwtScaleDiv& QwtPlotGrid::yScaleDiv() const
426{
427 return m_data->yScaleDiv;
428}
429
438 void QwtPlotGrid::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
439 const QwtScaleDiv& yScaleDiv )
440{
441 setXDiv( xScaleDiv );
442 setYDiv( yScaleDiv );
443}
QList< double >
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition qwt_painter.h:183
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition qwt_painter.h:154
QwtPlotGrid::yMinEnabled
bool yMinEnabled() const
Definition qwt_plot_grid.cpp:412
QwtPlotGrid::setMajorPen
void setMajorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition qwt_plot_grid.cpp:212
QwtPlotGrid::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const override
Draw the grid.
Definition qwt_plot_grid.cpp:282
QwtPlotGrid::yScaleDiv
const QwtScaleDiv & yScaleDiv() const
Definition qwt_plot_grid.cpp:425
QwtPlotGrid::QwtPlotGrid
QwtPlotGrid()
Enables major grid, disables minor grid.
Definition qwt_plot_grid.cpp:48
QwtPlotGrid::enableY
void enableY(bool)
Enable or disable horizontal grid lines.
Definition qwt_plot_grid.cpp:92
QwtPlotGrid::minorPen
const QPen & minorPen() const
Definition qwt_plot_grid.cpp:376
QwtPlotGrid::enableX
void enableX(bool)
Enable or disable vertical grid lines.
Definition qwt_plot_grid.cpp:76
QwtPlotGrid::yEnabled
bool yEnabled() const
Definition qwt_plot_grid.cpp:403
QwtPlotGrid::majorPen
const QPen & majorPen() const
Definition qwt_plot_grid.cpp:367
QwtPlotGrid::setXDiv
void setXDiv(const QwtScaleDiv &)
Definition qwt_plot_grid.cpp:140
QwtPlotGrid::updateScaleDiv
virtual void updateScaleDiv(const QwtScaleDiv &xScaleDiv, const QwtScaleDiv &yScaleDiv) override
Definition qwt_plot_grid.cpp:438
QwtPlotGrid::setPen
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition qwt_plot_grid.cpp:176
QwtPlotGrid::enableXMin
void enableXMin(bool)
Enable or disable minor vertical grid lines.
Definition qwt_plot_grid.cpp:108
QwtPlotGrid::rtti
virtual int rtti() const override
Definition qwt_plot_grid.cpp:64
QwtPlotGrid::xScaleDiv
const QwtScaleDiv & xScaleDiv() const
Definition qwt_plot_grid.cpp:419
QwtPlotGrid::xEnabled
bool xEnabled() const
Definition qwt_plot_grid.cpp:385
QwtPlotGrid::enableYMin
void enableYMin(bool)
Enable or disable minor horizontal grid lines.
Definition qwt_plot_grid.cpp:124
QwtPlotGrid::xMinEnabled
bool xMinEnabled() const
Definition qwt_plot_grid.cpp:394
QwtPlotGrid::setYDiv
void setYDiv(const QwtScaleDiv &)
Definition qwt_plot_grid.cpp:154
QwtPlotGrid::setMinorPen
void setMinorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition qwt_plot_grid.cpp:247
QwtPlotGrid::~QwtPlotGrid
virtual ~QwtPlotGrid()
Destructor.
Definition qwt_plot_grid.cpp:58
QwtPlotItem
Base class for items on the plot canvas.
Definition qwt_plot_item.h:67
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition qwt_plot_item.cpp:491
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition qwt_plot_item.cpp:165
QwtPlotItem::Rtti_PlotGrid
@ Rtti_PlotGrid
For QwtPlotGrid.
Definition qwt_plot_item.h:81
QwtPlotItem::setItemInterest
void setItemInterest(ItemInterest, bool on=true)
Definition qwt_plot_item.cpp:279
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition qwt_plot_item.cpp:481
QwtPlotItem::ScaleInterest
@ ScaleInterest
Definition qwt_plot_item.h:185
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition qwt_scale_div.cpp:309
QwtScaleDiv::MediumTick
@ MediumTick
Medium ticks.
Definition qwt_scale_div.h:46
QwtScaleDiv::MinorTick
@ MinorTick
Minor ticks.
Definition qwt_scale_div.h:43
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition qwt_scale_div.h:49
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtText
A class representing a text.
Definition qwt_text.h:52