地面站终端 App
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.

441 lines
11 KiB

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 "qwt_dial_needle.h"
#include "qwt_global.h"
15 years ago
#include "qwt_math.h"
#include "qwt_painter.h"
#include <qapplication.h>
#include <qpainter.h>
15 years ago
#if QT_VERSION < 0x040601
#define qFastSin(x) qSin(x)
#define qFastCos(x) qCos(x)
15 years ago
#endif
static void qwtDrawStyle1Needle( QPainter *painter,
const QPalette &palette, QPalette::ColorGroup colorGroup,
double length )
15 years ago
{
const double r[] = { 0.4, 0.3, 1, 0.8, 1, 0.3, 0.4 };
const double a[] = { -45, -20, -15, 0, 15, 20, 45 };
15 years ago
QPainterPath path;
for ( int i = 0; i < 7; i++ )
{
const double angle = a[i] / 180.0 * M_PI;
const double radius = r[i] * length;
15 years ago
const double x = radius * qFastCos( angle );
const double y = radius * qFastSin( angle );
15 years ago
path.lineTo( x, -y );
}
15 years ago
painter->setPen( Qt::NoPen );
painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
painter->drawPath( path );
15 years ago
}
static void qwtDrawStyle2Needle( QPainter *painter,
const QPalette &palette, QPalette::ColorGroup colorGroup, double length )
15 years ago
{
const double ratioX = 0.7;
const double ratioY = 0.3;
15 years ago
QPainterPath path1;
path1.lineTo( ratioX * length, 0.0 );
path1.lineTo( length, ratioY * length );
15 years ago
QPainterPath path2;
path2.lineTo( ratioX * length, 0.0 );
path2.lineTo( length, -ratioY * length );
15 years ago
painter->setPen( Qt::NoPen );
15 years ago
painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
painter->drawPath( path1 );
15 years ago
painter->setBrush( palette.brush( colorGroup, QPalette::Dark ) );
painter->drawPath( path2 );
}
15 years ago
static void qwtDrawShadedPointer( QPainter *painter,
const QColor &lightColor, const QColor &darkColor,
double length, double width )
{
const double peak = qMax( length / 10.0, 5.0 );
15 years ago
const double knobWidth = width + 8;
QRectF knobRect( 0, 0, knobWidth, knobWidth );
knobRect.moveCenter( QPointF(0, 0) );
15 years ago
QPainterPath path1;
path1.lineTo( 0.0, 0.5 * width );
path1.lineTo( length - peak, 0.5 * width );
path1.lineTo( length, 0.0 );
path1.lineTo( 0.0, 0.0 );
15 years ago
QPainterPath arcPath1;
arcPath1.arcTo( knobRect, 0.0, -90.0 );
15 years ago
path1 = path1.united( arcPath1 );
15 years ago
QPainterPath path2;
path2.lineTo( 0.0, -0.5 * width );
path2.lineTo( length - peak, -0.5 * width );
path2.lineTo( length, 0.0 );
path2.lineTo( 0.0, 0.0 );
15 years ago
QPainterPath arcPath2;
arcPath2.arcTo( knobRect, 0.0, 90.0 );
15 years ago
path2 = path2.united( arcPath2 );
15 years ago
painter->setPen( Qt::NoPen );
15 years ago
painter->setBrush( lightColor );
painter->drawPath( path1 );
painter->setBrush( darkColor );
painter->drawPath( path2 );
15 years ago
}
static void qwtDrawArrowNeedle( QPainter *painter,
const QPalette &palette, QPalette::ColorGroup colorGroup,
double length, double width )
15 years ago
{
if ( width <= 0 )
width = qMax( length * 0.06, 9.0 );
15 years ago
const double peak = qMax( 2.0, 0.4 * width );
15 years ago
QPainterPath path;
path.moveTo( 0.0, 0.5 * width );
path.lineTo( length - peak, 0.3 * width );
path.lineTo( length, 0.0 );
path.lineTo( length - peak, -0.3 * width );
path.lineTo( 0.0, -0.5 * width );
15 years ago
QRectF br = path.boundingRect();
15 years ago
QPalette pal( palette.color( QPalette::Mid ) );
QColor c1 = pal.color( QPalette::Light );
QColor c2 = pal.color( QPalette::Dark );
15 years ago
QLinearGradient gradient( br.topLeft(), br.bottomLeft() );
gradient.setColorAt( 0.0, c1 );
gradient.setColorAt( 0.5, c1 );
gradient.setColorAt( 0.5001, c2 );
gradient.setColorAt( 1.0, c2 );
15 years ago
QPen pen( gradient, 1 );
pen.setJoinStyle( Qt::MiterJoin );
15 years ago
painter->setPen( pen );
painter->setBrush( palette.brush( colorGroup, QPalette::Mid ) );
painter->drawPath( path );
}
15 years ago
static void qwtDrawTriangleNeedle( QPainter *painter,
const QPalette &palette, QPalette::ColorGroup colorGroup,
double length )
{
const double width = qRound( length / 3.0 );
15 years ago
QPainterPath path[4];
15 years ago
path[0].lineTo( length, 0.0 );
path[0].lineTo( 0.0, width / 2 );
15 years ago
path[1].lineTo( length, 0.0 );
path[1].lineTo( 0.0, -width / 2 );
15 years ago
path[2].lineTo( -length, 0.0 );
path[2].lineTo( 0.0, width / 2 );
15 years ago
path[3].lineTo( -length, 0.0 );
path[3].lineTo( 0.0, -width / 2 );
15 years ago
const int colorOffset = 10;
const QColor darkColor = palette.color( colorGroup, QPalette::Dark );
const QColor lightColor = palette.color( colorGroup, QPalette::Light );
15 years ago
QColor color[4];
color[0] = darkColor.light( 100 + colorOffset );
color[1] = darkColor.dark( 100 + colorOffset );
color[2] = lightColor.light( 100 + colorOffset );
color[3] = lightColor.dark( 100 + colorOffset );
15 years ago
painter->setPen( Qt::NoPen );
15 years ago
for ( int i = 0; i < 4; i++ )
{
painter->setBrush( color[i] );
painter->drawPath( path[i] );
15 years ago
}
}
//! Constructor
QwtDialNeedle::QwtDialNeedle():
d_palette( QApplication::palette() )
{
}
15 years ago
//! Destructor
QwtDialNeedle::~QwtDialNeedle()
{
15 years ago
}
/*!
Sets the palette for the needle.
15 years ago
\param palette New Palette
15 years ago
*/
void QwtDialNeedle::setPalette( const QPalette &palette )
15 years ago
{
d_palette = palette;
15 years ago
}
/*!
\return the palette of the needle.
15 years ago
*/
const QPalette &QwtDialNeedle::palette() const
15 years ago
{
return d_palette;
15 years ago
}
/*!
Draw the needle
15 years ago
\param painter Painter
\param center Center of the dial, start position for the needle
15 years ago
\param length Length of the needle
\param direction Direction of the needle, in degrees counter clockwise
\param colorGroup Color group, used for painting
15 years ago
*/
void QwtDialNeedle::draw( QPainter *painter,
const QPointF &center, double length, double direction,
QPalette::ColorGroup colorGroup ) const
15 years ago
{
painter->save();
painter->translate( center );
painter->rotate( -direction );
15 years ago
drawNeedle( painter, length, colorGroup );
15 years ago
painter->restore();
}
//! Draw the knob
void QwtDialNeedle::drawKnob( QPainter *painter,
double width, const QBrush &brush, bool sunken ) const
15 years ago
{
QPalette palette( brush.color() );
15 years ago
QColor c1 = palette.color( QPalette::Light );
QColor c2 = palette.color( QPalette::Dark );
15 years ago
if ( sunken )
qSwap( c1, c2 );
15 years ago
QRectF rect( 0.0, 0.0, width, width );
rect.moveCenter( painter->combinedTransform().map( QPointF() ) );
15 years ago
QLinearGradient gradient( rect.topLeft(), rect.bottomRight() );
gradient.setColorAt( 0.0, c1 );
gradient.setColorAt( 0.3, c1 );
gradient.setColorAt( 0.7, c2 );
gradient.setColorAt( 1.0, c2 );
15 years ago
painter->save();
15 years ago
painter->resetTransform();
15 years ago
painter->setPen( QPen( gradient, 1 ) );
painter->setBrush( brush );
painter->drawEllipse( rect );
15 years ago
painter->restore();
}
/*!
Constructor
15 years ago
\param style Style
\param hasKnob With/Without knob
\param mid Middle color
\param base Base color
15 years ago
*/
QwtDialSimpleNeedle::QwtDialSimpleNeedle( Style style, bool hasKnob,
const QColor &mid, const QColor &base ):
d_style( style ),
d_hasKnob( hasKnob ),
d_width( -1 )
15 years ago
{
QPalette palette;
palette.setColor( QPalette::Mid, mid );
palette.setColor( QPalette::Base, base );
15 years ago
setPalette( palette );
15 years ago
}
/*!
Set the width of the needle
\param width Width
\sa width()
*/
void QwtDialSimpleNeedle::setWidth( double width )
{
d_width = width;
}
15 years ago
/*!
\return the width of the needle
\sa setWidth()
15 years ago
*/
double QwtDialSimpleNeedle::width() const
15 years ago
{
return d_width;
15 years ago
}
/*!
Draw the needle
15 years ago
\param painter Painter
\param length Length of the needle
\param colorGroup Color group, used for painting
15 years ago
*/
void QwtDialSimpleNeedle::drawNeedle( QPainter *painter,
double length, QPalette::ColorGroup colorGroup ) const
15 years ago
{
double knobWidth = 0.0;
double width = d_width;
15 years ago
if ( d_style == Arrow )
{
if ( width <= 0.0 )
width = qMax(length * 0.06, 6.0);
15 years ago
qwtDrawArrowNeedle( painter,
palette(), colorGroup, length, width );
15 years ago
knobWidth = qMin( width * 2.0, 0.2 * length );
}
else
{
if ( width <= 0.0 )
width = 5.0;
QPen pen ( palette().brush( colorGroup, QPalette::Mid ), width );
pen.setCapStyle( Qt::FlatCap );
painter->setPen( pen );
painter->drawLine( QPointF( 0.0, 0.0 ), QPointF( length, 0.0 ) );
knobWidth = qMax( width * 3.0, 5.0 );
15 years ago
}
if ( d_hasKnob && knobWidth > 0.0 )
{
drawKnob( painter, knobWidth,
palette().brush( colorGroup, QPalette::Base ), false );
}
15 years ago
}
//! Constructor
QwtCompassMagnetNeedle::QwtCompassMagnetNeedle( Style style,
const QColor &light, const QColor &dark ):
d_style( style )
15 years ago
{
QPalette palette;
palette.setColor( QPalette::Light, light );
palette.setColor( QPalette::Dark, dark );
palette.setColor( QPalette::Base, Qt::gray );
15 years ago
setPalette( palette );
}
15 years ago
/*!
Draw the needle
15 years ago
\param painter Painter
\param length Length of the needle
\param colorGroup Color group, used for painting
*/
void QwtCompassMagnetNeedle::drawNeedle( QPainter *painter,
double length, QPalette::ColorGroup colorGroup ) const
{
if ( d_style == ThinStyle )
{
const double width = qMax( length / 6.0, 3.0 );
const int colorOffset = 10;
const QColor light = palette().color( colorGroup, QPalette::Light );
const QColor dark = palette().color( colorGroup, QPalette::Dark );
qwtDrawShadedPointer( painter,
dark.light( 100 + colorOffset ),
dark.dark( 100 + colorOffset ),
length, width );
painter->rotate( 180.0 );
qwtDrawShadedPointer( painter,
light.light( 100 + colorOffset ),
light.dark( 100 + colorOffset ),
length, width );
const QBrush baseBrush = palette().brush( colorGroup, QPalette::Base );
drawKnob( painter, width, baseBrush, true );
}
else
{
qwtDrawTriangleNeedle( painter, palette(), colorGroup, length );
}
}
15 years ago
/*!
Constructor
15 years ago
\param style Arrow style
\param light Light color
\param dark Dark color
*/
QwtCompassWindArrow::QwtCompassWindArrow( Style style,
const QColor &light, const QColor &dark ):
d_style( style )
{
QPalette palette;
palette.setColor( QPalette::Light, light );
palette.setColor( QPalette::Dark, dark );
15 years ago
setPalette( palette );
}
15 years ago
/*!
Draw the needle
15 years ago
\param painter Painter
\param length Length of the needle
\param colorGroup Color group, used for painting
*/
void QwtCompassWindArrow::drawNeedle( QPainter *painter,
double length, QPalette::ColorGroup colorGroup ) const
{
if ( d_style == Style1 )
qwtDrawStyle1Needle( painter, palette(), colorGroup, length );
else
qwtDrawStyle2Needle( painter, palette(), colorGroup, length );
15 years ago
}