Loading...
Searching...
No Matches
qwt_compass.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.h"
#include "qwt_compass_rose.h"
#include "qwt_text.h"
#include <qevent.h>
#include <qmap.h>
class QwtCompassScaleDraw::PrivateData
{
public:
QMap< double, QString > labelMap;
};
QwtCompassScaleDraw::QwtCompassScaleDraw()
{
m_data = new PrivateData;
enableComponent( QwtAbstractScaleDraw::Backbone, false );
enableComponent( QwtAbstractScaleDraw::Ticks, false );
QMap< double, QString >& map = m_data->labelMap;
map.insert( 0.0, QString::fromLatin1( "N" ) );
map.insert( 45.0, QString::fromLatin1( "NE" ) );
map.insert( 90.0, QString::fromLatin1( "E" ) );
map.insert( 135.0, QString::fromLatin1( "SE" ) );
map.insert( 180.0, QString::fromLatin1( "S" ) );
map.insert( 225.0, QString::fromLatin1( "SW" ) );
map.insert( 270.0, QString::fromLatin1( "W" ) );
map.insert( 315.0, QString::fromLatin1( "NW" ) );
#if 0
map.insert( 22.5, QString::fromLatin1( "NNE" ) );
map.insert( 67.5, QString::fromLatin1( "NEE" ) );
map.insert( 112.5, QString::fromLatin1( "SEE" ) );
map.insert( 157.5, QString::fromLatin1( "SSE" ) );
map.insert( 202.5, QString::fromLatin1( "SSW" ) );
map.insert( 247.5, QString::fromLatin1( "SWW" ) );
map.insert( 292.5, QString::fromLatin1( "NWW" ) );
map.insert( 337.5, QString::fromLatin1( "NNW" ) );
#endif
}
QwtCompassScaleDraw::QwtCompassScaleDraw( const QMap< double, QString >& map )
{
m_data = new PrivateData;
m_data->labelMap = map;
enableComponent( QwtAbstractScaleDraw::Backbone, false );
enableComponent( QwtAbstractScaleDraw::Ticks, false );
}
QwtCompassScaleDraw::~QwtCompassScaleDraw()
{
delete m_data;
}
void QwtCompassScaleDraw::setLabelMap( const QMap< double, QString >& map )
{
m_data->labelMap = map;
}
QMap< double, QString > QwtCompassScaleDraw::labelMap() const
{
return m_data->labelMap;
}
QwtText QwtCompassScaleDraw::label( double value ) const
{
if ( qFuzzyCompare( value + 1.0, 1.0 ) )
value = 0.0;
if ( value < 0.0 )
value += 360.0;
QMap< double, QString >::const_iterator it =
m_data->labelMap.constFind( value );
if ( it != m_data->labelMap.constEnd() )
return *it;
return QwtText();
}
class QwtCompass::PrivateData
{
public:
PrivateData():
rose( NULL )
{
}
~PrivateData()
{
delete rose;
}
QwtCompassRose* rose;
};
QwtCompass::QwtCompass( QWidget* parent )
: QwtDial( parent )
{
m_data = new PrivateData;
setScaleDraw( new QwtCompassScaleDraw() );
setOrigin( 270.0 );
setWrapping( true );
setScaleMaxMajor( 36 );
setScaleMaxMinor( 10 );
setScale( 0.0, 360.0 ); // degrees as default
setTotalSteps( 360 );
}
QwtCompass::~QwtCompass()
{
delete m_data;
}
void QwtCompass::drawScaleContents( QPainter* painter,
const QPointF& center, double radius ) const
{
QPalette::ColorGroup cg;
if ( isEnabled() )
cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
else
cg = QPalette::Disabled;
double north = origin();
if ( isValid() )
{
if ( mode() == RotateScale )
north -= value();
}
const int margin = 4;
drawRose( painter, center, radius - margin, 360.0 - north, cg );
}
void QwtCompass::drawRose( QPainter* painter, const QPointF& center,
double radius, double north, QPalette::ColorGroup cg ) const
{
if ( m_data->rose )
m_data->rose->draw( painter, center, radius, north, cg );
}
void QwtCompass::setRose( QwtCompassRose* rose )
{
if ( rose != m_data->rose )
{
if ( m_data->rose )
delete m_data->rose;
m_data->rose = rose;
update();
}
}
const QwtCompassRose* QwtCompass::rose() const
{
return m_data->rose;
}
QwtCompassRose* QwtCompass::rose()
{
return m_data->rose;
}
void QwtCompass::keyPressEvent( QKeyEvent* kev )
{
if ( isReadOnly() )
return;
#if 0
if ( kev->key() == Key_5 )
{
invalidate(); // signal ???
return;
}
#endif
double newValue = value();
if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
{
if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
return;
switch ( kev->key() )
{
case Qt::Key_6:
newValue = 180.0 * 0.0;
break;
case Qt::Key_3:
newValue = 180.0 * 0.25;
break;
case Qt::Key_2:
newValue = 180.0 * 0.5;
break;
case Qt::Key_1:
newValue = 180.0 * 0.75;
break;
case Qt::Key_4:
newValue = 180.0 * 1.0;
break;
case Qt::Key_7:
newValue = 180.0 * 1.25;
break;
case Qt::Key_8:
newValue = 180.0 * 1.5;
break;
case Qt::Key_9:
newValue = 180.0 * 1.75;
break;
}
newValue -= origin();
setValue( newValue );
}
else
{
QwtDial::keyPressEvent( kev );
}
}
#include "moc_qwt_compass.cpp"
QMap< double, QString >
QwtAbstractScaleDraw::Ticks
@ Ticks
Ticks.
Definition qwt_abstract_scale_draw.h:45
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition qwt_abstract_scale_draw.h:42
QwtAbstractScaleDraw::enableComponent
void enableComponent(ScaleComponent, bool enable=true)
Definition qwt_abstract_scale_draw.cpp:79
QwtAbstractScale::setScaleMaxMajor
void setScaleMaxMajor(int ticks)
Set the maximum number of major tick intervals.
Definition qwt_abstract_scale.cpp:189
QwtAbstractScale::setScaleMaxMinor
void setScaleMaxMinor(int ticks)
Set the maximum number of minor tick intervals.
Definition qwt_abstract_scale.cpp:219
QwtAbstractScale::setScale
void setScale(double lowerBound, double upperBound)
Specify a scale.
Definition qwt_abstract_scale.cpp:130
QwtAbstractSlider::keyPressEvent
virtual void keyPressEvent(QKeyEvent *) override
Definition qwt_abstract_slider.cpp:370
QwtAbstractSlider::setTotalSteps
void setTotalSteps(uint)
Set the number of steps.
Definition qwt_abstract_slider.cpp:474
QwtAbstractSlider::setValue
void setValue(double value)
Definition qwt_abstract_slider.cpp:570
QwtAbstractSlider::isReadOnly
bool isReadOnly() const
Definition qwt_abstract_slider.cpp:159
QwtAbstractSlider::isValid
bool isValid() const
Definition qwt_abstract_slider.cpp:125
QwtAbstractSlider::value
double value() const
Returns the current value.
Definition qwt_abstract_slider.cpp:587
QwtAbstractSlider::setWrapping
void setWrapping(bool)
Definition qwt_abstract_slider.cpp:599
QwtCompass::QwtCompass
QwtCompass(QWidget *parent=NULL)
Constructor.
Definition qwt_compass.cpp:158
QwtCompass::setRose
void setRose(QwtCompassRose *rose)
Definition qwt_compass.cpp:232
QwtCompass::keyPressEvent
virtual void keyPressEvent(QKeyEvent *) override
Definition qwt_compass.cpp:271
QwtCompass::drawRose
virtual void drawRose(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup) const
Definition qwt_compass.cpp:218
QwtCompass::drawScaleContents
virtual void drawScaleContents(QPainter *, const QPointF &center, double radius) const override
Definition qwt_compass.cpp:189
QwtCompass::~QwtCompass
virtual ~QwtCompass()
Destructor.
Definition qwt_compass.cpp:176
QwtCompass::rose
const QwtCompassRose * rose() const
Definition qwt_compass.cpp:248
QwtCompassRose
Abstract base class for a compass rose.
Definition qwt_compass_rose.h:22
QwtCompassRose::draw
virtual void draw(QPainter *painter, const QPointF &center, double radius, double north, QPalette::ColorGroup colorGroup=QPalette::Active) const =0
QwtCompassScaleDraw
A special scale draw made for QwtCompass.
Definition qwt_compass.h:33
QwtCompassScaleDraw::label
virtual QwtText label(double value) const override
Definition qwt_compass.cpp:116
QwtCompassScaleDraw::setLabelMap
void setLabelMap(const QMap< double, QString > &map)
Set a map, mapping values to labels.
Definition qwt_compass.cpp:90
QwtCompassScaleDraw::~QwtCompassScaleDraw
virtual ~QwtCompassScaleDraw()
Destructor.
Definition qwt_compass.cpp:73
QwtCompassScaleDraw::labelMap
QMap< double, QString > labelMap() const
Definition qwt_compass.cpp:99
QwtCompassScaleDraw::QwtCompassScaleDraw
QwtCompassScaleDraw()
Constructor.
Definition qwt_compass.cpp:28
QwtDial
QwtDial class provides a rounded range control.
Definition qwt_dial.h:51
QwtDial::setScaleDraw
void setScaleDraw(QwtRoundScaleDraw *)
Definition qwt_dial.cpp:574
QwtDial::setOrigin
virtual void setOrigin(double)
Change the origin.
Definition qwt_dial.cpp:660
QwtDial::origin
double origin() const
Definition qwt_dial.cpp:674
QwtDial::RotateScale
@ RotateScale
The needle is fixed, the scales are rotating.
Definition qwt_dial.h:92
QwtDial::RotateNeedle
@ RotateNeedle
The needle is rotating.
Definition qwt_dial.h:89
QwtDial::mode
Mode mode() const
Definition qwt_dial.cpp:293
QwtText
A class representing a text.
Definition qwt_text.h:52