qwt_plot_grid.cpp
Go to the documentation of this file.
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 
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  m_data = new PrivateData;
52 
54  setZ( 10.0 );
55 }
56 
59 {
60  delete m_data;
61 }
62 
64 int QwtPlotGrid::rtti() const
65 {
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,
296  drawLines( painter, canvasRect, Qt::Vertical, xMap,
298  }
299 
300  if ( m_data->yEnabled && m_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 = 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,
318  }
319 
320  if ( m_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 m_data->majorPen;
370 }
371 
376 const 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 {
441  setXDiv( xScaleDiv );
442  setYDiv( yScaleDiv );
443 }
color
color
Definition: color.h:16
QwtPlotGrid::minorPen
const QPen & minorPen() const
Definition: qwt_plot_grid.cpp:376
QwtPlotItem::setItemInterest
void setItemInterest(ItemInterest, bool on=true)
Definition: qwt_plot_item.cpp:279
qwt_scale_div.h
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtScaleDiv::MediumTick
@ MediumTick
Medium ticks.
Definition: qwt_scale_div.h:46
QwtPlotGrid::enableXMin
void enableXMin(bool)
Enable or disable minor vertical grid lines.
Definition: qwt_plot_grid.cpp:108
QwtPlotGrid::enableX
void enableX(bool)
Enable or disable vertical grid lines.
Definition: qwt_plot_grid.cpp:76
QwtPlotGrid::setXDiv
void setXDiv(const QwtScaleDiv &)
Definition: qwt_plot_grid.cpp:140
QList< double >
QwtPlotGrid::enableYMin
void enableYMin(bool)
Enable or disable minor horizontal grid lines.
Definition: qwt_plot_grid.cpp:124
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPlotGrid::PrivateData::PrivateData
PrivateData()
Definition: qwt_plot_grid.cpp:27
QwtPlotGrid::enableY
void enableY(bool)
Enable or disable horizontal grid lines.
Definition: qwt_plot_grid.cpp:92
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition: qwt_scale_div.cpp:309
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition: qwt_scale_div.h:49
qwtFuzzyGreaterOrEqual
static bool qwtFuzzyGreaterOrEqual(double d1, double d2)
Definition: qwt_plot_grid.cpp:19
QwtPlotGrid::setPen
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_plot_grid.cpp:176
QwtPlotGrid::~QwtPlotGrid
virtual ~QwtPlotGrid()
Destructor.
Definition: qwt_plot_grid.cpp:58
qwt_scale_map.h
QwtPlotGrid::rtti
virtual int rtti() const QWT_OVERRIDE
Definition: qwt_plot_grid.cpp:64
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtPlotGrid::updateScaleDiv
virtual void updateScaleDiv(const QwtScaleDiv &xScaleDiv, const QwtScaleDiv &yScaleDiv) QWT_OVERRIDE
Definition: qwt_plot_grid.cpp:438
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154
QwtPlotGrid::xMinEnabled
bool xMinEnabled() const
Definition: qwt_plot_grid.cpp:394
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:66
QwtPlotItem::Rtti_PlotGrid
@ Rtti_PlotGrid
For QwtPlotGrid.
Definition: qwt_plot_item.h:81
QwtPlotGrid::PrivateData::majorPen
QPen majorPen
Definition: qwt_plot_grid.cpp:43
QwtPlotGrid::yMinEnabled
bool yMinEnabled() const
Definition: qwt_plot_grid.cpp:412
QwtPlotGrid::PrivateData::xMinEnabled
bool xMinEnabled
Definition: qwt_plot_grid.cpp:37
QwtPlotGrid::setMinorPen
void setMinorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_plot_grid.cpp:247
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtPlotGrid::PrivateData::xEnabled
bool xEnabled
Definition: qwt_plot_grid.cpp:35
qwt_painter.h
QwtPlotGrid::xScaleDiv
const QwtScaleDiv & xScaleDiv() const
Definition: qwt_plot_grid.cpp:419
QwtPlotGrid::PrivateData::minorPen
QPen minorPen
Definition: qwt_plot_grid.cpp:44
QwtPlotGrid::setYDiv
void setYDiv(const QwtScaleDiv &)
Definition: qwt_plot_grid.cpp:154
QwtPlotGrid::QwtPlotGrid
QwtPlotGrid()
Enables major grid, disables minor grid.
Definition: qwt_plot_grid.cpp:48
QwtPlotGrid::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const QWT_OVERRIDE
Draw the grid.
Definition: qwt_plot_grid.cpp:282
QwtPlotGrid::PrivateData::xScaleDiv
QwtScaleDiv xScaleDiv
Definition: qwt_plot_grid.cpp:40
QwtPlotGrid::PrivateData::yScaleDiv
QwtScaleDiv yScaleDiv
Definition: qwt_plot_grid.cpp:41
QwtPlotGrid::PrivateData::yMinEnabled
bool yMinEnabled
Definition: qwt_plot_grid.cpp:38
QwtScaleDiv::MinorTick
@ MinorTick
Minor ticks.
Definition: qwt_scale_div.h:43
QwtPlotGrid::PrivateData
Definition: qwt_plot_grid.cpp:24
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:33
qwt_text.h
QwtPlotGrid::drawLines
void drawLines(QPainter *, const QRectF &, Qt::Orientation, const QwtScaleMap &, const QList< double > &) const
Definition: qwt_plot_grid.cpp:327
QwtPlotGrid::setMajorPen
void setMajorPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_plot_grid.cpp:212
QwtPlotItem::ScaleInterest
@ ScaleInterest
Definition: qwt_plot_item.h:185
QwtPlotGrid::yEnabled
bool yEnabled() const
Definition: qwt_plot_grid.cpp:403
qwt_plot_grid.h
QwtPlotGrid::m_data
PrivateData * m_data
Definition: qwt_plot_grid.h:86
QwtPlotGrid::yScaleDiv
const QwtScaleDiv & yScaleDiv() const
Definition: qwt_plot_grid.cpp:425
QwtPlotGrid::xEnabled
bool xEnabled() const
Definition: qwt_plot_grid.cpp:385
QwtPlotGrid::PrivateData::yEnabled
bool yEnabled
Definition: qwt_plot_grid.cpp:36
QwtPlotGrid::majorPen
const QPen & majorPen() const
Definition: qwt_plot_grid.cpp:367


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45