qwt_compass_rose.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_compass_rose.h"
#include "qwt_point_polar.h"
#include <qpainter.h>
#include <qpainterpath.h>
static QPointF qwtIntersection(
QPointF p11, QPointF p12, QPointF p21, QPointF p22 )
{
const QLineF line1( p11, p12 );
const QLineF line2( p21, p22 );
QPointF pos;
#if QT_VERSION >= 0x050e00
if ( line1.intersects( line2, &pos ) == QLineF::NoIntersection )
#else
if ( line1.intersect( line2, &pos ) == QLineF::NoIntersection )
#endif
return QPointF();
return pos;
}
QwtCompassRose::QwtCompassRose()
{
}
QwtCompassRose::~QwtCompassRose()
{
}
void QwtCompassRose::setPalette( const QPalette& p )
{
m_palette = p;
}
const QPalette& QwtCompassRose::palette() const
{
return m_palette;
}
class QwtSimpleCompassRose::PrivateData
{
public:
PrivateData()
: width( 0.2 )
, numThorns( 8 )
, numThornLevels( -1 )
, shrinkFactor( 0.9 )
{
}
double width;
int numThorns;
int numThornLevels;
double shrinkFactor;
};
QwtSimpleCompassRose::QwtSimpleCompassRose(
int numThorns, int numThornLevels )
{
m_data = new PrivateData();
m_data->numThorns = numThorns;
m_data->numThornLevels = numThornLevels;
const QColor dark( 128, 128, 255 );
const QColor light( 192, 255, 255 );
QPalette palette;
palette.setColor( QPalette::Dark, dark );
palette.setColor( QPalette::Light, light );
setPalette( palette );
}
QwtSimpleCompassRose::~QwtSimpleCompassRose()
{
delete m_data;
}
void QwtSimpleCompassRose::setShrinkFactor( double factor )
{
m_data->shrinkFactor = factor;
}
double QwtSimpleCompassRose::shrinkFactor() const
{
return m_data->shrinkFactor;
}
void QwtSimpleCompassRose::draw( QPainter* painter, const QPointF& center,
double radius, double north, QPalette::ColorGroup cg ) const
{
QPalette pal = palette();
pal.setCurrentColorGroup( cg );
drawRose( painter, pal, center, radius, north, m_data->width,
m_data->numThorns, m_data->numThornLevels, m_data->shrinkFactor );
}
void QwtSimpleCompassRose::drawRose(
QPainter* painter,
const QPalette& palette,
const QPointF& center, double radius, double north, double width,
int numThorns, int numThornLevels, double shrinkFactor )
{
if ( numThorns < 4 )
numThorns = 4;
if ( numThorns % 4 )
numThorns += 4 - numThorns % 4;
if ( numThornLevels <= 0 )
numThornLevels = numThorns / 4;
if ( shrinkFactor >= 1.0 )
shrinkFactor = 1.0;
if ( shrinkFactor <= 0.5 )
shrinkFactor = 0.5;
painter->save();
painter->setPen( Qt::NoPen );
for ( int j = 1; j <= numThornLevels; j++ )
{
double step = std::pow( 2.0, j ) * M_PI / numThorns;
if ( step > M_PI_2 )
break;
double r = radius;
for ( int k = 0; k < 3; k++ )
{
if ( j + k < numThornLevels )
r *= shrinkFactor;
}
double leafWidth = r * width;
if ( 2.0 * M_PI / step > 32 )
leafWidth = 16;
const double origin = qwtRadians( north );
for ( double angle = origin;
angle < 2.0 * M_PI + origin; angle += step )
{
const QPointF p = qwtPolar2Pos( center, r, angle );
const QPointF p1 = qwtPolar2Pos( center, leafWidth, angle + M_PI_2 );
const QPointF p2 = qwtPolar2Pos( center, leafWidth, angle - M_PI_2 );
const QPointF p3 = qwtPolar2Pos( center, r, angle + step / 2.0 );
const QPointF p4 = qwtPolar2Pos( center, r, angle - step / 2.0 );
QPainterPath darkPath;
darkPath.moveTo( center );
darkPath.lineTo( p );
darkPath.lineTo( qwtIntersection( center, p3, p1, p ) );
painter->setBrush( palette.brush( QPalette::Dark ) );
painter->drawPath( darkPath );
QPainterPath lightPath;
lightPath.moveTo( center );
lightPath.lineTo( p );
lightPath.lineTo( qwtIntersection( center, p4, p2, p ) );
painter->setBrush( palette.brush( QPalette::Light ) );
painter->drawPath( lightPath );
}
}
painter->restore();
}
void QwtSimpleCompassRose::setWidth( double width )
{
m_data->width = width;
if ( m_data->width < 0.03 )
m_data->width = 0.03;
if ( m_data->width > 0.4 )
m_data->width = 0.4;
}
double QwtSimpleCompassRose::width() const
{
return m_data->width;
}
void QwtSimpleCompassRose::setNumThorns( int numThorns )
{
if ( numThorns < 4 )
numThorns = 4;
if ( numThorns % 4 )
numThorns += 4 - numThorns % 4;
m_data->numThorns = numThorns;
}
int QwtSimpleCompassRose::numThorns() const
{
return m_data->numThorns;
}
void QwtSimpleCompassRose::setNumThornLevels( int numThornLevels )
{
m_data->numThornLevels = numThornLevels;
}
int QwtSimpleCompassRose::numThornLevels() const
{
return m_data->numThornLevels;
}
QwtCompassRose::~QwtCompassRose
virtual ~QwtCompassRose()
Destructor.
Definition: qwt_compass_rose.cpp:39
QwtCompassRose::QwtCompassRose
QwtCompassRose()
Constructor.
Definition: qwt_compass_rose.cpp:34
QwtCompassRose::setPalette
virtual void setPalette(const QPalette &)
Assign a palette.
Definition: qwt_compass_rose.cpp:44
QwtCompassRose::palette
const QPalette & palette() const
Definition: qwt_compass_rose.cpp:50
QwtSimpleCompassRose::setWidth
void setWidth(double)
Definition: qwt_compass_rose.cpp:232
QwtSimpleCompassRose::~QwtSimpleCompassRose
virtual ~QwtSimpleCompassRose()
Destructor.
Definition: qwt_compass_rose.cpp:96
QwtSimpleCompassRose::numThorns
int numThorns() const
Definition: qwt_compass_rose.cpp:273
QwtSimpleCompassRose::width
double width() const
Definition: qwt_compass_rose.cpp:246
QwtSimpleCompassRose::setNumThornLevels
void setNumThornLevels(int)
Definition: qwt_compass_rose.cpp:284
QwtSimpleCompassRose::shrinkFactor
double shrinkFactor() const
Definition: qwt_compass_rose.cpp:117
QwtSimpleCompassRose::QwtSimpleCompassRose
QwtSimpleCompassRose(int numThorns=8, int numThornLevels=-1)
Definition: qwt_compass_rose.cpp:78
QwtSimpleCompassRose::numThornLevels
int numThornLevels() const
Definition: qwt_compass_rose.cpp:293
QwtSimpleCompassRose::draw
virtual void draw(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup=QPalette::Active) const override
Definition: qwt_compass_rose.cpp:131
QwtSimpleCompassRose::setNumThorns
void setNumThorns(int)
Definition: qwt_compass_rose.cpp:258
QwtSimpleCompassRose::drawRose
static void drawRose(QPainter *, const QPalette &, const QPointF &center, double radius, double north, double width, int numThorns, int numThornLevels, double shrinkFactor)
Definition: qwt_compass_rose.cpp:154
QwtSimpleCompassRose::setShrinkFactor
void setShrinkFactor(double factor)
Definition: qwt_compass_rose.cpp:108