You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
275 lines
0 B
275 lines
0 B
15 years ago
|
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
||
|
* 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 <math.h>
|
||
|
#include <qpainter.h>
|
||
|
#include "qwt_math.h"
|
||
|
#include "qwt_painter.h"
|
||
|
#include "qwt_compass_rose.h"
|
||
|
|
||
|
static QPoint cutPoint(QPoint p11, QPoint p12, QPoint p21, QPoint p22)
|
||
|
{
|
||
|
double dx1 = p12.x() - p11.x();
|
||
|
double dy1 = p12.y() - p11.y();
|
||
|
double dx2 = p22.x() - p21.x();
|
||
|
double dy2 = p22.y() - p21.y();
|
||
|
|
||
|
if ( dx1 == 0.0 && dx2 == 0.0 )
|
||
|
return QPoint();
|
||
|
|
||
14 years ago
|
if ( dx1 == 0.0 ) {
|
||
15 years ago
|
const double m = dy2 / dx2;
|
||
|
const double t = p21.y() - m * p21.x();
|
||
|
return QPoint(p11.x(), qRound(m * p11.x() + t));
|
||
|
}
|
||
|
|
||
14 years ago
|
if ( dx2 == 0 ) {
|
||
15 years ago
|
const double m = dy1 / dx1;
|
||
|
const double t = p11.y() - m * p11.x();
|
||
|
return QPoint(p21.x(), qRound(m * p21.x() + t));
|
||
|
}
|
||
|
|
||
|
const double m1 = dy1 / dx1;
|
||
|
const double t1 = p11.y() - m1 * p11.x();
|
||
|
|
||
|
const double m2 = dy2 / dx2;
|
||
|
const double t2 = p21.y() - m2 * p21.x();
|
||
|
|
||
|
if ( m1 == m2 )
|
||
|
return QPoint();
|
||
|
|
||
|
const double x = ( t2 - t1 ) / ( m1 - m2 );
|
||
|
const double y = t1 + m1 * x;
|
||
|
|
||
|
return QPoint(qRound(x), qRound(y));
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
Constructor
|
||
|
|
||
|
\param numThorns Number of thorns
|
||
|
\param numThornLevels Number of thorn levels
|
||
|
*/
|
||
|
QwtSimpleCompassRose::QwtSimpleCompassRose(int numThorns, int numThornLevels):
|
||
|
d_width(0.2),
|
||
|
d_numThorns(numThorns),
|
||
|
d_numThornLevels(numThornLevels),
|
||
|
d_shrinkFactor(0.9)
|
||
|
{
|
||
|
const QColor dark(128,128,255);
|
||
|
const QColor light(192,255,255);
|
||
14 years ago
|
|
||
15 years ago
|
QPalette palette;
|
||
14 years ago
|
for ( int i = 0; i < QPalette::NColorGroups; i++ ) {
|
||
15 years ago
|
#if QT_VERSION < 0x040000
|
||
|
palette.setColor((QPalette::ColorGroup)i,
|
||
14 years ago
|
QColorGroup::Dark, dark);
|
||
15 years ago
|
palette.setColor((QPalette::ColorGroup)i,
|
||
14 years ago
|
QColorGroup::Light, light);
|
||
15 years ago
|
#else
|
||
|
palette.setColor((QPalette::ColorGroup)i,
|
||
14 years ago
|
QPalette::Dark, dark);
|
||
15 years ago
|
palette.setColor((QPalette::ColorGroup)i,
|
||
14 years ago
|
QPalette::Light, light);
|
||
15 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
setPalette(palette);
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
Draw the rose
|
||
|
|
||
|
\param painter Painter
|
||
|
\param center Center point
|
||
|
\param radius Radius of the rose
|
||
|
\param north Position
|
||
|
\param cg Color group
|
||
|
*/
|
||
14 years ago
|
void QwtSimpleCompassRose::draw(QPainter *painter, const QPoint ¢er,
|
||
|
int radius, double north, QPalette::ColorGroup cg) const
|
||
15 years ago
|
{
|
||
|
#if QT_VERSION < 0x040000
|
||
|
QColorGroup colorGroup;
|
||
14 years ago
|
switch(cg) {
|
||
|
case QPalette::Disabled:
|
||
|
colorGroup = palette().disabled();
|
||
|
case QPalette::Inactive:
|
||
|
colorGroup = palette().inactive();
|
||
|
default:
|
||
|
colorGroup = palette().active();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
drawRose(painter, colorGroup, center, radius, north, d_width,
|
||
|
d_numThorns, d_numThornLevels, d_shrinkFactor);
|
||
15 years ago
|
#else
|
||
|
QPalette pal = palette();
|
||
|
pal.setCurrentColorGroup(cg);
|
||
14 years ago
|
drawRose(painter, pal, center, radius, north, d_width,
|
||
|
d_numThorns, d_numThornLevels, d_shrinkFactor);
|
||
15 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
Draw the rose
|
||
|
|
||
|
\param painter Painter
|
||
|
\param palette Palette
|
||
|
\param center Center of the rose
|
||
|
\param radius Radius of the rose
|
||
|
\param north Position pointing to north
|
||
|
\param width Width of the rose
|
||
|
\param numThorns Number of thorns
|
||
|
\param numThornLevels Number of thorn levels
|
||
|
\param shrinkFactor Factor to shrink the thorns with each level
|
||
|
*/
|
||
|
void QwtSimpleCompassRose::drawRose(
|
||
14 years ago
|
QPainter *painter,
|
||
15 years ago
|
#if QT_VERSION < 0x040000
|
||
|
const QColorGroup &cg,
|
||
|
#else
|
||
|
const QPalette &palette,
|
||
|
#endif
|
||
|
const QPoint ¢er, int 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);
|
||
|
|
||
14 years ago
|
for ( int j = 1; j <= numThornLevels; j++ ) {
|
||
15 years ago
|
double step = pow(2.0, j) * M_PI / (double)numThorns;
|
||
|
if ( step > M_PI_2 )
|
||
|
break;
|
||
|
|
||
|
double r = radius;
|
||
14 years ago
|
for ( int k = 0; k < 3; k++ ) {
|
||
15 years ago
|
if ( j + k < numThornLevels )
|
||
|
r *= shrinkFactor;
|
||
|
}
|
||
|
|
||
|
double leafWidth = r * width;
|
||
|
if ( 2.0 * M_PI / step > 32 )
|
||
|
leafWidth = 16;
|
||
|
|
||
|
const double origin = north / 180.0 * M_PI;
|
||
14 years ago
|
for ( double angle = origin;
|
||
|
angle < 2.0 * M_PI + origin; angle += step) {
|
||
15 years ago
|
const QPoint p = qwtPolar2Pos(center, r, angle);
|
||
|
QPoint p1 = qwtPolar2Pos(center, leafWidth, angle + M_PI_2);
|
||
|
QPoint p2 = qwtPolar2Pos(center, leafWidth, angle - M_PI_2);
|
||
|
|
||
|
QwtPolygon pa(3);
|
||
|
pa.setPoint(0, center);
|
||
|
pa.setPoint(1, p);
|
||
|
|
||
|
QPoint p3 = qwtPolar2Pos(center, r, angle + step / 2.0);
|
||
|
p1 = cutPoint(center, p3, p1, p);
|
||
|
pa.setPoint(2, p1);
|
||
|
#if QT_VERSION < 0x040000
|
||
|
painter->setBrush(cg.brush(QColorGroup::Dark));
|
||
|
#else
|
||
|
painter->setBrush(palette.brush(QPalette::Dark));
|
||
|
#endif
|
||
|
painter->drawPolygon(pa);
|
||
|
|
||
|
QPoint p4 = qwtPolar2Pos(center, r, angle - step / 2.0);
|
||
|
p2 = cutPoint(center, p4, p2, p);
|
||
|
|
||
|
pa.setPoint(2, p2);
|
||
|
#if QT_VERSION < 0x040000
|
||
|
painter->setBrush(cg.brush(QColorGroup::Light));
|
||
|
#else
|
||
|
painter->setBrush(palette.brush(QPalette::Light));
|
||
|
#endif
|
||
|
painter->drawPolygon(pa);
|
||
|
}
|
||
|
}
|
||
|
painter->restore();
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
Set the width of the rose heads. Lower value make thinner heads.
|
||
|
The range is limited from 0.03 to 0.4.
|
||
|
|
||
|
\param width Width
|
||
|
*/
|
||
|
|
||
14 years ago
|
void QwtSimpleCompassRose::setWidth(double width)
|
||
15 years ago
|
{
|
||
14 years ago
|
d_width = width;
|
||
|
if (d_width < 0.03)
|
||
15 years ago
|
d_width = 0.03;
|
||
|
|
||
14 years ago
|
if (d_width > 0.4)
|
||
15 years ago
|
d_width = 0.4;
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
Set the number of thorns on one level
|
||
14 years ago
|
The number is aligned to a multiple of 4, with a minimum of 4
|
||
15 years ago
|
|
||
|
\param numThorns Number of thorns
|
||
|
\sa numThorns(), setNumThornLevels()
|
||
|
*/
|
||
14 years ago
|
void QwtSimpleCompassRose::setNumThorns(int numThorns)
|
||
15 years ago
|
{
|
||
|
if ( numThorns < 4 )
|
||
|
numThorns = 4;
|
||
|
|
||
|
if ( numThorns % 4 )
|
||
|
numThorns += 4 - numThorns % 4;
|
||
|
|
||
|
d_numThorns = numThorns;
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\return Number of thorns
|
||
|
\sa setNumThorns(), setNumThornLevels()
|
||
|
*/
|
||
|
int QwtSimpleCompassRose::numThorns() const
|
||
|
{
|
||
14 years ago
|
return d_numThorns;
|
||
15 years ago
|
}
|
||
|
|
||
|
/*!
|
||
|
Set the of thorns levels
|
||
|
|
||
|
\param numThornLevels Number of thorns levels
|
||
|
\sa setNumThorns(), numThornLevels()
|
||
|
*/
|
||
14 years ago
|
void QwtSimpleCompassRose::setNumThornLevels(int numThornLevels)
|
||
15 years ago
|
{
|
||
|
d_numThornLevels = numThornLevels;
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\return Number of thorn levels
|
||
|
\sa setNumThorns(), setNumThornLevels()
|
||
|
*/
|
||
|
int QwtSimpleCompassRose::numThornLevels() const
|
||
|
{
|
||
|
return d_numThornLevels;
|
||
|
}
|