Loading...
Searching...
No Matches
qwt_plot_zoneitem.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_zoneitem.h"
#include "qwt_painter.h"
#include "qwt_scale_map.h"
#include "qwt_text.h"
#include "qwt_interval.h"
#include <qpainter.h>
class QwtPlotZoneItem::PrivateData
{
public:
PrivateData()
: orientation( Qt::Vertical )
, pen( Qt::NoPen )
{
QColor c( Qt::darkGray );
c.setAlpha( 100 );
brush = QBrush( c );
}
Qt::Orientation orientation;
QPen pen;
QBrush brush;
QwtInterval interval;
};
QwtPlotZoneItem::QwtPlotZoneItem()
: QwtPlotItem( QwtText( "Zone" ) )
{
m_data = new PrivateData;
setItemAttribute( QwtPlotItem::AutoScale, false );
setItemAttribute( QwtPlotItem::Legend, false );
setZ( 5 );
}
QwtPlotZoneItem::~QwtPlotZoneItem()
{
delete m_data;
}
int QwtPlotZoneItem::rtti() const
{
return QwtPlotItem::Rtti_PlotZone;
}
void QwtPlotZoneItem::setPen( const QColor& color, qreal width, Qt::PenStyle style )
{
setPen( QPen( color, width, style ) );
}
void QwtPlotZoneItem::setPen( const QPen& pen )
{
if ( m_data->pen != pen )
{
m_data->pen = pen;
itemChanged();
}
}
const QPen& QwtPlotZoneItem::pen() const
{
return m_data->pen;
}
void QwtPlotZoneItem::setBrush( const QBrush& brush )
{
if ( m_data->brush != brush )
{
m_data->brush = brush;
itemChanged();
}
}
const QBrush& QwtPlotZoneItem::brush() const
{
return m_data->brush;
}
void QwtPlotZoneItem::setOrientation( Qt::Orientation orientation )
{
if ( m_data->orientation != orientation )
{
m_data->orientation = orientation;
itemChanged();
}
}
Qt::Orientation QwtPlotZoneItem::orientation() const
{
return m_data->orientation;
}
void QwtPlotZoneItem::setInterval( double min, double max )
{
setInterval( QwtInterval( min, max ) );
}
void QwtPlotZoneItem::setInterval( const QwtInterval& interval )
{
if ( m_data->interval != interval )
{
m_data->interval = interval;
itemChanged();
}
}
QwtInterval QwtPlotZoneItem::interval() const
{
return m_data->interval;
}
void QwtPlotZoneItem::draw( QPainter* painter,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QRectF& canvasRect ) const
{
if ( !m_data->interval.isValid() )
return;
QPen pen = m_data->pen;
pen.setCapStyle( Qt::FlatCap );
const bool doAlign = QwtPainter::roundingAlignment( painter );
if ( m_data->orientation == Qt::Horizontal )
{
double y1 = yMap.transform( m_data->interval.minValue() );
double y2 = yMap.transform( m_data->interval.maxValue() );
if ( doAlign )
{
y1 = qRound( y1 );
y2 = qRound( y2 );
}
QRectF r( canvasRect.left(), y1, canvasRect.width(), y2 - y1 );
r = r.normalized();
if ( ( m_data->brush.style() != Qt::NoBrush ) && ( y1 != y2 ) )
{
QwtPainter::fillRect( painter, r, m_data->brush );
}
if ( m_data->pen.style() != Qt::NoPen )
{
painter->setPen( m_data->pen );
QwtPainter::drawLine( painter, r.left(), r.top(), r.right(), r.top() );
QwtPainter::drawLine( painter, r.left(), r.bottom(), r.right(), r.bottom() );
}
}
else
{
double x1 = xMap.transform( m_data->interval.minValue() );
double x2 = xMap.transform( m_data->interval.maxValue() );
if ( doAlign )
{
x1 = qRound( x1 );
x2 = qRound( x2 );
}
QRectF r( x1, canvasRect.top(), x2 - x1, canvasRect.height() );
r = r.normalized();
if ( ( m_data->brush.style() != Qt::NoBrush ) && ( x1 != x2 ) )
{
QwtPainter::fillRect( painter, r, m_data->brush );
}
if ( m_data->pen.style() != Qt::NoPen )
{
painter->setPen( m_data->pen );
QwtPainter::drawLine( painter, r.left(), r.top(), r.left(), r.bottom() );
QwtPainter::drawLine( painter, r.right(), r.top(), r.right(), r.bottom() );
}
}
}
QRectF QwtPlotZoneItem::boundingRect() const
{
QRectF br = QwtPlotItem::boundingRect();
const QwtInterval& intv = m_data->interval;
if ( intv.isValid() )
{
if ( m_data->orientation == Qt::Horizontal )
{
br.setTop( intv.minValue() );
br.setBottom( intv.maxValue() );
}
else
{
br.setLeft( intv.minValue() );
br.setRight( intv.maxValue() );
}
}
return br;
}
QwtInterval
A class representing an interval.
Definition qwt_interval.h:23
QwtInterval::minValue
double minValue() const
Definition qwt_interval.h:192
QwtInterval::maxValue
double maxValue() const
Definition qwt_interval.h:198
QwtInterval::isValid
bool isValid() const
Definition qwt_interval.h:210
QwtPainter::fillRect
static void fillRect(QPainter *, const QRectF &, const QBrush &)
Wrapper for QPainter::fillRect()
Definition qwt_painter.cpp:372
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
QwtPlotItem
Base class for items on the plot canvas.
Definition qwt_plot_item.h:67
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition qwt_plot_item.cpp:228
QwtPlotItem::Rtti_PlotZone
@ Rtti_PlotZone
For QwtPlotZoneItem.
Definition qwt_plot_item.h:126
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition qwt_plot_item.cpp:481
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition qwt_plot_item.h:150
QwtPlotItem::AutoScale
@ AutoScale
Definition qwt_plot_item.h:157
QwtPlotItem::boundingRect
virtual QRectF boundingRect() const
Definition qwt_plot_item.cpp:568
QwtPlotZoneItem::brush
const QBrush & brush() const
Definition qwt_plot_zoneitem.cpp:138
QwtPlotZoneItem::setOrientation
void setOrientation(Qt::Orientation)
Set the orientation of the zone.
Definition qwt_plot_zoneitem.cpp:152
QwtPlotZoneItem::pen
const QPen & pen() const
Definition qwt_plot_zoneitem.cpp:112
QwtPlotZoneItem::setBrush
void setBrush(const QBrush &)
Assign a brush.
Definition qwt_plot_zoneitem.cpp:125
QwtPlotZoneItem::boundingRect
virtual QRectF boundingRect() const override
Definition qwt_plot_zoneitem.cpp:297
QwtPlotZoneItem::interval
QwtInterval interval() const
Definition qwt_plot_zoneitem.cpp:209
QwtPlotZoneItem::~QwtPlotZoneItem
virtual ~QwtPlotZoneItem()
Destructor.
Definition qwt_plot_zoneitem.cpp:62
QwtPlotZoneItem::setPen
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition qwt_plot_zoneitem.cpp:86
QwtPlotZoneItem::QwtPlotZoneItem
QwtPlotZoneItem()
Constructor.
Definition qwt_plot_zoneitem.cpp:50
QwtPlotZoneItem::setInterval
void setInterval(double min, double max)
Definition qwt_plot_zoneitem.cpp:181
QwtPlotZoneItem::rtti
virtual int rtti() const override
Definition qwt_plot_zoneitem.cpp:68
QwtPlotZoneItem::orientation
Qt::Orientation orientation() const
Definition qwt_plot_zoneitem.cpp:165
QwtPlotZoneItem::draw
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &canvasRect) const override
Definition qwt_plot_zoneitem.cpp:223
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