Loading...
Searching...
No Matches
qwt_interval_symbol.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_interval_symbol.h"
#include "qwt_painter.h"
#include "qwt_math.h"
#include <qpainter.h>
#include <qmath.h>
class QwtIntervalSymbol::PrivateData
{
public:
PrivateData()
: style( QwtIntervalSymbol::NoSymbol )
, width( 6 )
{
}
bool operator==( const PrivateData& other ) const
{
return ( style == other.style )
&& ( width == other.width )
&& ( brush == other.brush )
&& ( pen == other.pen );
}
QwtIntervalSymbol::Style style;
int width;
QPen pen;
QBrush brush;
};
QwtIntervalSymbol::QwtIntervalSymbol( Style style )
{
m_data = new PrivateData();
m_data->style = style;
}
QwtIntervalSymbol::QwtIntervalSymbol( const QwtIntervalSymbol& other )
{
m_data = new PrivateData();
*m_data = *other.m_data;
}
QwtIntervalSymbol::~QwtIntervalSymbol()
{
delete m_data;
}
QwtIntervalSymbol& QwtIntervalSymbol::operator=(
const QwtIntervalSymbol& other )
{
*m_data = *other.m_data;
return *this;
}
bool QwtIntervalSymbol::operator==(
const QwtIntervalSymbol& other ) const
{
return *m_data == *other.m_data;
}
bool QwtIntervalSymbol::operator!=(
const QwtIntervalSymbol& other ) const
{
return !( *m_data == *other.m_data );
}
void QwtIntervalSymbol::setStyle( Style style )
{
m_data->style = style;
}
QwtIntervalSymbol::Style QwtIntervalSymbol::style() const
{
return m_data->style;
}
void QwtIntervalSymbol::setWidth( int width )
{
m_data->width = width;
}
int QwtIntervalSymbol::width() const
{
return m_data->width;
}
void QwtIntervalSymbol::setBrush( const QBrush& brush )
{
m_data->brush = brush;
}
const QBrush& QwtIntervalSymbol::brush() const
{
return m_data->brush;
}
void QwtIntervalSymbol::setPen( const QColor& color,
qreal width, Qt::PenStyle style )
{
setPen( QPen( color, width, style ) );
}
void QwtIntervalSymbol::setPen( const QPen& pen )
{
m_data->pen = pen;
}
const QPen& QwtIntervalSymbol::pen() const
{
return m_data->pen;
}
void QwtIntervalSymbol::draw( QPainter* painter, Qt::Orientation orientation,
const QPointF& from, const QPointF& to ) const
{
const qreal pw = QwtPainter::effectivePenWidth( painter->pen() );
QPointF p1 = from;
QPointF p2 = to;
if ( QwtPainter::roundingAlignment( painter ) )
{
p1 = p1.toPoint();
p2 = p2.toPoint();
}
switch ( m_data->style )
{
case QwtIntervalSymbol::Bar:
{
QwtPainter::drawLine( painter, p1, p2 );
if ( m_data->width > pw )
{
if ( ( orientation == Qt::Horizontal )
&& ( p1.y() == p2.y() ) )
{
const double sw = m_data->width;
const double y = p1.y() - sw / 2;
QwtPainter::drawLine( painter,
p1.x(), y, p1.x(), y + sw );
QwtPainter::drawLine( painter,
p2.x(), y, p2.x(), y + sw );
}
else if ( ( orientation == Qt::Vertical )
&& ( p1.x() == p2.x() ) )
{
const double sw = m_data->width;
const double x = p1.x() - sw / 2;
QwtPainter::drawLine( painter,
x, p1.y(), x + sw, p1.y() );
QwtPainter::drawLine( painter,
x, p2.y(), x + sw, p2.y() );
}
else
{
const double sw = m_data->width;
const double dx = p2.x() - p1.x();
const double dy = p2.y() - p1.y();
const double angle = std::atan2( dy, dx ) + M_PI_2;
double dw2 = sw / 2.0;
const double cx = qFastCos( angle ) * dw2;
const double sy = qFastSin( angle ) * dw2;
QwtPainter::drawLine( painter,
p1.x() - cx, p1.y() - sy,
p1.x() + cx, p1.y() + sy );
QwtPainter::drawLine( painter,
p2.x() - cx, p2.y() - sy,
p2.x() + cx, p2.y() + sy );
}
}
break;
}
case QwtIntervalSymbol::Box:
{
if ( m_data->width <= pw )
{
QwtPainter::drawLine( painter, p1, p2 );
}
else
{
if ( ( orientation == Qt::Horizontal )
&& ( p1.y() == p2.y() ) )
{
const double sw = m_data->width;
const double y = p1.y() - m_data->width / 2;
QwtPainter::drawRect( painter,
p1.x(), y, p2.x() - p1.x(), sw );
}
else if ( ( orientation == Qt::Vertical )
&& ( p1.x() == p2.x() ) )
{
const double sw = m_data->width;
const double x = p1.x() - m_data->width / 2;
QwtPainter::drawRect( painter,
x, p1.y(), sw, p2.y() - p1.y() );
}
else
{
const double sw = m_data->width;
const double dx = p2.x() - p1.x();
const double dy = p2.y() - p1.y();
const double angle = std::atan2( dy, dx ) + M_PI_2;
double dw2 = sw / 2.0;
const double cx = qFastCos( angle ) * dw2;
const double sy = qFastSin( angle ) * dw2;
QPolygonF polygon;
polygon += QPointF( p1.x() - cx, p1.y() - sy );
polygon += QPointF( p1.x() + cx, p1.y() + sy );
polygon += QPointF( p2.x() + cx, p2.y() + sy );
polygon += QPointF( p2.x() - cx, p2.y() - sy );
QwtPainter::drawPolygon( painter, polygon );
}
}
break;
}
default:;
}
}
QwtIntervalSymbol
A drawing primitive for displaying an interval like an error bar.
Definition qwt_interval_symbol.h:28
QwtIntervalSymbol::width
int width() const
Definition qwt_interval_symbol.cpp:124
QwtIntervalSymbol::setStyle
void setStyle(Style)
Definition qwt_interval_symbol.cpp:94
QwtIntervalSymbol::draw
virtual void draw(QPainter *, Qt::Orientation, const QPointF &from, const QPointF &to) const
Definition qwt_interval_symbol.cpp:200
QwtIntervalSymbol::setBrush
void setBrush(const QBrush &)
Assign a brush.
Definition qwt_interval_symbol.cpp:137
QwtIntervalSymbol::pen
const QPen & pen() const
Definition qwt_interval_symbol.cpp:185
QwtIntervalSymbol::QwtIntervalSymbol
QwtIntervalSymbol(Style=NoSymbol)
Definition qwt_interval_symbol.cpp:47
QwtIntervalSymbol::operator=
QwtIntervalSymbol & operator=(const QwtIntervalSymbol &)
Assignment operator.
Definition qwt_interval_symbol.cpp:67
QwtIntervalSymbol::operator==
bool operator==(const QwtIntervalSymbol &) const
Compare two symbols.
Definition qwt_interval_symbol.cpp:75
QwtIntervalSymbol::Style
Style
Symbol style.
Definition qwt_interval_symbol.h:32
QwtIntervalSymbol::Bar
@ Bar
Definition qwt_interval_symbol.h:40
QwtIntervalSymbol::NoSymbol
@ NoSymbol
No Style. The symbol cannot be drawn.
Definition qwt_interval_symbol.h:34
QwtIntervalSymbol::Box
@ Box
Definition qwt_interval_symbol.h:47
QwtIntervalSymbol::style
Style style() const
Definition qwt_interval_symbol.cpp:103
QwtIntervalSymbol::setPen
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition qwt_interval_symbol.cpp:164
QwtIntervalSymbol::operator!=
bool operator!=(const QwtIntervalSymbol &) const
Compare two symbols.
Definition qwt_interval_symbol.cpp:82
QwtIntervalSymbol::brush
const QBrush & brush() const
Definition qwt_interval_symbol.cpp:146
QwtIntervalSymbol::~QwtIntervalSymbol
virtual ~QwtIntervalSymbol()
Destructor.
Definition qwt_interval_symbol.cpp:61
QwtIntervalSymbol::setWidth
void setWidth(int)
Definition qwt_interval_symbol.cpp:115
QwtPainter::drawPolygon
static void drawPolygon(QPainter *, const QPolygonF &)
Wrapper for QPainter::drawPolygon()
Definition qwt_painter.cpp:551
QwtPainter::effectivePenWidth
static qreal effectivePenWidth(const QPen &)
Definition qwt_painter.h:201
QwtPainter::drawRect
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
Definition qwt_painter.cpp:337
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