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
19static inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
20{
21 return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
22}
23
24class 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
49 : QwtPlotItem( QwtText( "Grid" ) )
50{
51 m_data = new PrivateData;
52
54 setZ( 10.0 );
55}
56
59{
60 delete m_data;
61}
62
65{
67}
68
76void QwtPlotGrid::enableX( bool on )
77{
78 if ( m_data->xEnabled != on )
79 {
80 m_data->xEnabled = on;
81
84 }
85}
86
92void QwtPlotGrid::enableY( bool on )
93{
94 if ( m_data->yEnabled != on )
95 {
96 m_data->yEnabled = on;
97
100 }
101}
102
109{
110 if ( m_data->xMinEnabled != on )
111 {
112 m_data->xMinEnabled = on;
113
115 itemChanged();
116 }
117}
118
125{
126 if ( m_data->yMinEnabled != on )
127 {
128 m_data->yMinEnabled = on;
129
131 itemChanged();
132 }
133}
134
140void QwtPlotGrid::setXDiv( const QwtScaleDiv& scaleDiv )
141{
142 if ( m_data->xScaleDiv != scaleDiv )
143 {
144 m_data->xScaleDiv = scaleDiv;
145 itemChanged();
146 }
147}
148
154void QwtPlotGrid::setYDiv( const QwtScaleDiv& scaleDiv )
155{
156 if ( m_data->yScaleDiv != scaleDiv )
157 {
158 m_data->yScaleDiv = scaleDiv;
159 itemChanged();
160 }
161}
162
176void QwtPlotGrid::setPen( const QColor& color, qreal width, Qt::PenStyle style )
177{
178 setPen( QPen( color, width, style ) );
179}
180
187void 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
195 itemChanged();
196 }
197}
198
212void QwtPlotGrid::setMajorPen( const QColor& color, qreal width, Qt::PenStyle style )
213{
214 setMajorPen( QPen( color, width, style ) );
215}
216
223void QwtPlotGrid::setMajorPen( const QPen& pen )
224{
225 if ( m_data->majorPen != pen )
226 {
227 m_data->majorPen = pen;
228
230 itemChanged();
231 }
232}
233
247void QwtPlotGrid::setMinorPen( const QColor& color, qreal width, Qt::PenStyle style )
248{
249 setMinorPen( QPen( color, width, style ) );
250}
251
258void QwtPlotGrid::setMinorPen( const QPen& pen )
259{
260 if ( m_data->minorPen != pen )
261 {
262 m_data->minorPen = pen;
263
265 itemChanged();
266 }
267}
268
282void 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
327void 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
367const QPen& QwtPlotGrid::majorPen() const
368{
369 return m_data->majorPen;
370}
371
376const QPen& QwtPlotGrid::minorPen() const
377{
378 return m_data->minorPen;
379}
380
386{
387 return m_data->xEnabled;
388}
389
395{
396 return m_data->xMinEnabled;
397}
398
404{
405 return m_data->yEnabled;
406}
407
413{
414 return m_data->yMinEnabled;
415}
416
417
420{
421 return m_data->xScaleDiv;
422}
423
426{
427 return m_data->yScaleDiv;
428}
429
439 const QwtScaleDiv& yScaleDiv )
440{
443}
static bool roundingAlignment()
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
bool yMinEnabled() const
void setMajorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const override
Draw the grid.
const QwtScaleDiv & yScaleDiv() const
QwtPlotGrid()
Enables major grid, disables minor grid.
void enableY(bool)
Enable or disable horizontal grid lines.
const QPen & minorPen() const
void enableX(bool)
Enable or disable vertical grid lines.
bool yEnabled() const
const QPen & majorPen() const
void setXDiv(const QwtScaleDiv &)
virtual void updateScaleDiv(const QwtScaleDiv &xScaleDiv, const QwtScaleDiv &yScaleDiv) override
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
void enableXMin(bool)
Enable or disable minor vertical grid lines.
virtual int rtti() const override
const QwtScaleDiv & xScaleDiv() const
bool xEnabled() const
void enableYMin(bool)
Enable or disable minor horizontal grid lines.
bool xMinEnabled() const
void setYDiv(const QwtScaleDiv &)
void setMinorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
virtual ~QwtPlotGrid()
Destructor.
Base class for items on the plot canvas.
virtual void legendChanged()
void setZ(double z)
Set the z value.
@ Rtti_PlotGrid
For QwtPlotGrid.
void setItemInterest(ItemInterest, bool on=true)
virtual void itemChanged()
A class representing a scale division.
QList< double > ticks(int tickType) const
@ MediumTick
Medium ticks.
@ MinorTick
Minor ticks.
@ MajorTick
Major ticks.
A scale map.
double transform(double s) const
A class representing a text.
Definition qwt_text.h:52