qwt_plot_grid.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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 
25 {
26 public:
28  xEnabled( true ),
29  yEnabled( true ),
30  xMinEnabled( false ),
31  yMinEnabled( false )
32  {
33  }
34 
35  bool xEnabled;
36  bool yEnabled;
39 
42 
43  QPen majorPen;
44  QPen minorPen;
45 };
46 
49  QwtPlotItem( QwtText( "Grid" ) )
50 {
51  d_data = new PrivateData;
52 
54  setZ( 10.0 );
55 }
56 
59 {
60  delete d_data;
61 }
62 
64 int QwtPlotGrid::rtti() const
65 {
67 }
68 
76 void QwtPlotGrid::enableX( bool on )
77 {
78  if ( d_data->xEnabled != on )
79  {
80  d_data->xEnabled = on;
81 
82  legendChanged();
83  itemChanged();
84  }
85 }
86 
92 void QwtPlotGrid::enableY( bool on )
93 {
94  if ( d_data->yEnabled != on )
95  {
96  d_data->yEnabled = on;
97 
98  legendChanged();
99  itemChanged();
100  }
101 }
102 
108 void QwtPlotGrid::enableXMin( bool on )
109 {
110  if ( d_data->xMinEnabled != on )
111  {
112  d_data->xMinEnabled = on;
113 
114  legendChanged();
115  itemChanged();
116  }
117 }
118 
124 void QwtPlotGrid::enableYMin( bool on )
125 {
126  if ( d_data->yMinEnabled != on )
127  {
128  d_data->yMinEnabled = on;
129 
130  legendChanged();
131  itemChanged();
132  }
133 }
134 
140 void QwtPlotGrid::setXDiv( const QwtScaleDiv &scaleDiv )
141 {
142  if ( d_data->xScaleDiv != scaleDiv )
143  {
144  d_data->xScaleDiv = scaleDiv;
145  itemChanged();
146  }
147 }
148 
154 void QwtPlotGrid::setYDiv( const QwtScaleDiv &scaleDiv )
155 {
156  if ( d_data->yScaleDiv != scaleDiv )
157  {
158  d_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 ( d_data->majorPen != pen || d_data->minorPen != pen )
190  {
191  d_data->majorPen = pen;
192  d_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 ( d_data->majorPen != pen )
226  {
227  d_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 ( d_data->minorPen != pen )
261  {
262  d_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 = d_data->minorPen;
288  minorPen.setCapStyle( Qt::FlatCap );
289 
290  painter->setPen( minorPen );
291 
292  if ( d_data->xEnabled && d_data->xMinEnabled )
293  {
294  drawLines( painter, canvasRect, Qt::Vertical, xMap,
296  drawLines( painter, canvasRect, Qt::Vertical, xMap,
298  }
299 
300  if ( d_data->yEnabled && d_data->yMinEnabled )
301  {
302  drawLines( painter, canvasRect, Qt::Horizontal, yMap,
304  drawLines( painter, canvasRect, Qt::Horizontal, yMap,
306  }
307 
308  // draw major grid lines
309  QPen majorPen = d_data->majorPen;
310  majorPen.setCapStyle( Qt::FlatCap );
311 
312  painter->setPen( majorPen );
313 
314  if ( d_data->xEnabled )
315  {
316  drawLines( painter, canvasRect, Qt::Vertical, xMap,
318  }
319 
320  if ( d_data->yEnabled )
321  {
322  drawLines( painter, canvasRect, Qt::Horizontal, yMap,
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 d_data->majorPen;
370 }
371 
376 const QPen &QwtPlotGrid::minorPen() const
377 {
378  return d_data->minorPen;
379 }
380 
386 {
387  return d_data->xEnabled;
388 }
389 
395 {
396  return d_data->xMinEnabled;
397 }
398 
404 {
405  return d_data->yEnabled;
406 }
407 
413 {
414  return d_data->yMinEnabled;
415 }
416 
417 
420 {
421  return d_data->xScaleDiv;
422 }
423 
426 {
427  return d_data->yScaleDiv;
428 }
429 
439  const QwtScaleDiv& yScaleDiv )
440 {
441  setXDiv( xScaleDiv );
442  setYDiv( yScaleDiv );
443 }
virtual void legendChanged()
bool yEnabled() const
enum MQTTPropertyCodes value
void setMajorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
void enableX(bool)
Enable or disable vertical grid lines.
const QPen & minorPen() const
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:152
bool xEnabled() const
void enableYMin(bool)
Enable or disable minor horizontal grid lines.
void setXDiv(const QwtScaleDiv &)
A class representing a scale division.
Definition: qwt_scale_div.h:33
virtual int rtti() const QWT_OVERRIDE
void drawLines(QPainter *, const QRectF &, Qt::Orientation, const QwtScaleMap &, const QList< double > &) const
void enableXMin(bool)
Enable or disable minor vertical grid lines.
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
void enableY(bool)
Enable or disable horizontal grid lines.
PrivateData * d_data
Definition: qwt_plot_grid.h:86
virtual ~QwtPlotGrid()
Destructor.
A class representing a text.
Definition: qwt_text.h:51
void setZ(double z)
Set the z value.
void setMinorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
const QPen & majorPen() const
A scale map.
Definition: qwt_scale_map.h:26
virtual void updateScaleDiv(const QwtScaleDiv &xScaleDiv, const QwtScaleDiv &yScaleDiv) QWT_OVERRIDE
virtual void itemChanged()
bool xMinEnabled() const
QwtPlotGrid()
Enables major grid, disables minor grid.
bool yMinEnabled() const
static bool qwtFuzzyGreaterOrEqual(double d1, double d2)
const QwtScaleDiv & xScaleDiv() const
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const QWT_OVERRIDE
Draw the grid.
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:65
double transform(double s) const
void setItemInterest(ItemInterest, bool on=true)
void setYDiv(const QwtScaleDiv &)
QList< double > ticks(int tickType) const
static bool roundingAlignment()
Definition: qwt_painter.h:181
const QwtScaleDiv & yScaleDiv() const


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10