Loading...
Searching...
No Matches
qwt_plot_grid.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot_grid.h"
#include "qwt_painter.h"
#include "qwt_text.h"
#include "qwt_scale_map.h"
#include "qwt_scale_div.h"
#include <qpainter.h>
#include <qpen.h>
static inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
{
return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
}
class QwtPlotGrid::PrivateData
{
public:
PrivateData()
: xEnabled( true )
, yEnabled( true )
, xMinEnabled( false )
, yMinEnabled( false )
{
}
bool xEnabled;
bool yEnabled;
bool xMinEnabled;
bool yMinEnabled;
QwtScaleDiv xScaleDiv;
QwtScaleDiv yScaleDiv;
QPen majorPen;
QPen minorPen;
};
QwtPlotGrid::QwtPlotGrid()
: QwtPlotItem( QwtText( "Grid" ) )
{
m_data = new PrivateData;
setItemInterest( QwtPlotItem::ScaleInterest, true );
setZ( 10.0 );
}
QwtPlotGrid::~QwtPlotGrid()
{
delete m_data;
}
int QwtPlotGrid::rtti() const
{
return QwtPlotItem::Rtti_PlotGrid;
}
void QwtPlotGrid::enableX( bool on )
{
if ( m_data->xEnabled != on )
{
m_data->xEnabled = on;
legendChanged();
itemChanged();
}
}
void QwtPlotGrid::enableY( bool on )
{
if ( m_data->yEnabled != on )
{
m_data->yEnabled = on;
legendChanged();
itemChanged();
}
}
void QwtPlotGrid::enableXMin( bool on )
{
if ( m_data->xMinEnabled != on )
{
m_data->xMinEnabled = on;
legendChanged();
itemChanged();
}
}
void QwtPlotGrid::enableYMin( bool on )
{
if ( m_data->yMinEnabled != on )
{
m_data->yMinEnabled = on;
legendChanged();
itemChanged();
}
}
void QwtPlotGrid::setXDiv( const QwtScaleDiv& scaleDiv )
{
if ( m_data->xScaleDiv != scaleDiv )
{
m_data->xScaleDiv = scaleDiv;
itemChanged();
}
}
void QwtPlotGrid::setYDiv( const QwtScaleDiv& scaleDiv )
{
if ( m_data->yScaleDiv != scaleDiv )
{
m_data->yScaleDiv = scaleDiv;
itemChanged();
}
}
void QwtPlotGrid::setPen( const QColor& color, qreal width, Qt::PenStyle style )
{
setPen( QPen( color, width, style ) );
}
void QwtPlotGrid::setPen( const QPen& pen )
{
if ( m_data->majorPen != pen || m_data->minorPen != pen )
{
m_data->majorPen = pen;
m_data->minorPen = pen;
legendChanged();
itemChanged();
}
}
void QwtPlotGrid::setMajorPen( const QColor& color, qreal width, Qt::PenStyle style )
{
setMajorPen( QPen( color, width, style ) );
}
void QwtPlotGrid::setMajorPen( const QPen& pen )
{
if ( m_data->majorPen != pen )
{
m_data->majorPen = pen;
legendChanged();
itemChanged();
}
}
void QwtPlotGrid::setMinorPen( const QColor& color, qreal width, Qt::PenStyle style )
{
setMinorPen( QPen( color, width, style ) );
}
void QwtPlotGrid::setMinorPen( const QPen& pen )
{
if ( m_data->minorPen != pen )
{
m_data->minorPen = pen;
legendChanged();
itemChanged();
}
}
void QwtPlotGrid::draw( QPainter* painter,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QRectF& canvasRect ) const
{
// draw minor grid lines
QPen minorPen = m_data->minorPen;
minorPen.setCapStyle( Qt::FlatCap );
painter->setPen( minorPen );
if ( m_data->xEnabled && m_data->xMinEnabled )
{
drawLines( painter, canvasRect, Qt::Vertical, xMap,
m_data->xScaleDiv.ticks( QwtScaleDiv::MinorTick ) );
drawLines( painter, canvasRect, Qt::Vertical, xMap,
m_data->xScaleDiv.ticks( QwtScaleDiv::MediumTick ) );
}
if ( m_data->yEnabled && m_data->yMinEnabled )
{
drawLines( painter, canvasRect, Qt::Horizontal, yMap,
m_data->yScaleDiv.ticks( QwtScaleDiv::MinorTick ) );
drawLines( painter, canvasRect, Qt::Horizontal, yMap,
m_data->yScaleDiv.ticks( QwtScaleDiv::MediumTick ) );
}
// draw major grid lines
QPen majorPen = m_data->majorPen;
majorPen.setCapStyle( Qt::FlatCap );
painter->setPen( majorPen );
if ( m_data->xEnabled )
{
drawLines( painter, canvasRect, Qt::Vertical, xMap,
m_data->xScaleDiv.ticks( QwtScaleDiv::MajorTick ) );
}
if ( m_data->yEnabled )
{
drawLines( painter, canvasRect, Qt::Horizontal, yMap,
m_data->yScaleDiv.ticks( QwtScaleDiv::MajorTick ) );
}
}
void QwtPlotGrid::drawLines( QPainter* painter, const QRectF& canvasRect,
Qt::Orientation orientation, const QwtScaleMap& scaleMap,
const QList< double >& values ) const
{
const double x1 = canvasRect.left();
const double x2 = canvasRect.right() - 1.0;
const double y1 = canvasRect.top();
const double y2 = canvasRect.bottom() - 1.0;
const bool doAlign = QwtPainter::roundingAlignment( painter );
for ( int i = 0; i < values.count(); i++ )
{
double value = scaleMap.transform( values[i] );
if ( doAlign )
value = qRound( value );
if ( orientation == Qt::Horizontal )
{
if ( qwtFuzzyGreaterOrEqual( value, y1 ) &&
qwtFuzzyGreaterOrEqual( y2, value ) )
{
QwtPainter::drawLine( painter, x1, value, x2, value );
}
}
else
{
if ( qwtFuzzyGreaterOrEqual( value, x1 ) &&
qwtFuzzyGreaterOrEqual( x2, value ) )
{
QwtPainter::drawLine( painter, value, y1, value, y2 );
}
}
}
}
const QPen& QwtPlotGrid::majorPen() const
{
return m_data->majorPen;
}
const QPen& QwtPlotGrid::minorPen() const
{
return m_data->minorPen;
}
bool QwtPlotGrid::xEnabled() const
{
return m_data->xEnabled;
}
bool QwtPlotGrid::xMinEnabled() const
{
return m_data->xMinEnabled;
}
bool QwtPlotGrid::yEnabled() const
{
return m_data->yEnabled;
}
bool QwtPlotGrid::yMinEnabled() const
{
return m_data->yMinEnabled;
}
const QwtScaleDiv& QwtPlotGrid::xScaleDiv() const
{
return m_data->xScaleDiv;
}
const QwtScaleDiv& QwtPlotGrid::yScaleDiv() const
{
return m_data->yScaleDiv;
}
void QwtPlotGrid::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
const QwtScaleDiv& yScaleDiv )
{
setXDiv( xScaleDiv );
setYDiv( yScaleDiv );
}
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