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

720 lines
18 KiB

15 years ago
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
15 years ago
* 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_plot.h"
#include "qwt_math.h"
#include "qwt_scale_widget.h"
#include "qwt_scale_div.h"
#include "qwt_scale_engine.h"
class QwtPlot::AxisData
{
public:
bool isEnabled;
bool doAutoScale;
double minValue;
double maxValue;
double stepSize;
int maxMajor;
int maxMinor;
bool isValid;
15 years ago
QwtScaleDiv scaleDiv;
QwtScaleEngine *scaleEngine;
QwtScaleWidget *scaleWidget;
};
//! Initialize axes
void QwtPlot::initAxesData()
{
int axisId;
for ( axisId = 0; axisId < axisCnt; axisId++ )
15 years ago
d_axisData[axisId] = new AxisData;
d_axisData[yLeft]->scaleWidget =
new QwtScaleWidget( QwtScaleDraw::LeftScale, this );
d_axisData[yRight]->scaleWidget =
new QwtScaleWidget( QwtScaleDraw::RightScale, this );
d_axisData[xTop]->scaleWidget =
new QwtScaleWidget( QwtScaleDraw::TopScale, this );
d_axisData[xBottom]->scaleWidget =
new QwtScaleWidget( QwtScaleDraw::BottomScale, this );
15 years ago
d_axisData[yLeft]->scaleWidget->setObjectName( "QwtPlotAxisYLeft" );
d_axisData[yRight]->scaleWidget->setObjectName( "QwtPlotAxisYRight" );
d_axisData[xTop]->scaleWidget->setObjectName( "QwtPlotAxisXTop" );
d_axisData[xBottom]->scaleWidget->setObjectName( "QwtPlotAxisXBottom" );
15 years ago
#if 1
// better find the font sizes from the application font
QFont fscl( fontInfo().family(), 10 );
QFont fttl( fontInfo().family(), 12, QFont::Bold );
#endif
15 years ago
for ( axisId = 0; axisId < axisCnt; axisId++ )
{
15 years ago
AxisData &d = *d_axisData[axisId];
d.scaleEngine = new QwtLinearScaleEngine;
d.scaleWidget->setTransformation(
d.scaleEngine->transformation() );
d.scaleWidget->setFont( fscl );
d.scaleWidget->setMargin( 2 );
15 years ago
QwtText text = d.scaleWidget->title();
text.setFont( fttl );
d.scaleWidget->setTitle( text );
15 years ago
d.doAutoScale = true;
d.minValue = 0.0;
d.maxValue = 1000.0;
d.stepSize = 0.0;
d.maxMinor = 5;
d.maxMajor = 8;
d.isValid = false;
15 years ago
}
d_axisData[yLeft]->isEnabled = true;
d_axisData[yRight]->isEnabled = false;
d_axisData[xBottom]->isEnabled = true;
d_axisData[xTop]->isEnabled = false;
}
void QwtPlot::deleteAxesData()
{
for ( int axisId = 0; axisId < axisCnt; axisId++ )
{
15 years ago
delete d_axisData[axisId]->scaleEngine;
delete d_axisData[axisId];
d_axisData[axisId] = NULL;
}
}
/*!
\return Scale widget of the specified axis, or NULL if axisId is invalid.
\param axisId Axis index
15 years ago
*/
const QwtScaleWidget *QwtPlot::axisWidget( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->scaleWidget;
return NULL;
}
/*!
\return Scale widget of the specified axis, or NULL if axisId is invalid.
\param axisId Axis index
15 years ago
*/
QwtScaleWidget *QwtPlot::axisWidget( int axisId )
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->scaleWidget;
return NULL;
}
/*!
Change the scale engine for an axis
15 years ago
\param axisId Axis index
15 years ago
\param scaleEngine Scale engine
\sa axisScaleEngine()
*/
void QwtPlot::setAxisScaleEngine( int axisId, QwtScaleEngine *scaleEngine )
15 years ago
{
if ( axisValid( axisId ) && scaleEngine != NULL )
{
15 years ago
AxisData &d = *d_axisData[axisId];
delete d.scaleEngine;
d.scaleEngine = scaleEngine;
d_axisData[axisId]->scaleWidget->setTransformation(
scaleEngine->transformation() );
d.isValid = false;
15 years ago
autoRefresh();
}
}
/*!
\param axisId Axis index
\return Scale engine for a specific axis
*/
QwtScaleEngine *QwtPlot::axisScaleEngine( int axisId )
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->scaleEngine;
else
return NULL;
}
/*!
\param axisId Axis index
\return Scale engine for a specific axis
*/
const QwtScaleEngine *QwtPlot::axisScaleEngine( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->scaleEngine;
else
return NULL;
}
/*!
\return \c True, if autoscaling is enabled
\param axisId Axis index
15 years ago
*/
bool QwtPlot::axisAutoScale( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->doAutoScale;
else
return false;
15 years ago
}
/*!
\return \c True, if a specified axis is enabled
\param axisId Axis index
15 years ago
*/
bool QwtPlot::axisEnabled( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->isEnabled;
else
return false;
}
/*!
\return The font of the scale labels for a specified axis
\param axisId Axis index
15 years ago
*/
QFont QwtPlot::axisFont( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
return axisWidget( axisId )->font();
15 years ago
else
return QFont();
15 years ago
}
/*!
\return The maximum number of major ticks for a specified axis
\param axisId Axis index
\sa setAxisMaxMajor(), QwtScaleEngine::divideScale()
15 years ago
*/
int QwtPlot::axisMaxMajor( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->maxMajor;
else
return 0;
}
/*!
\return the maximum number of minor ticks for a specified axis
\param axisId Axis index
\sa setAxisMaxMinor(), QwtScaleEngine::divideScale()
15 years ago
*/
int QwtPlot::axisMaxMinor( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
15 years ago
return d_axisData[axisId]->maxMinor;
else
return 0;
}
/*!
\brief Return the scale division of a specified axis
axisScaleDiv(axisId).lowerBound(), axisScaleDiv(axisId).upperBound()
15 years ago
are the current limits of the axis scale.
\param axisId Axis index
\return Scale division
15 years ago
\sa QwtScaleDiv, setAxisScaleDiv(), QwtScaleEngine::divideScale()
15 years ago
*/
const QwtScaleDiv &QwtPlot::axisScaleDiv( int axisId ) const
15 years ago
{
return d_axisData[axisId]->scaleDiv;
15 years ago
}
/*!
\brief Return the scale draw of a specified axis
15 years ago
\param axisId Axis index
\return Specified scaleDraw for axis, or NULL if axis is invalid.
15 years ago
*/
const QwtScaleDraw *QwtPlot::axisScaleDraw( int axisId ) const
15 years ago
{
if ( !axisValid( axisId ) )
15 years ago
return NULL;
return axisWidget( axisId )->scaleDraw();
15 years ago
}
/*!
\brief Return the scale draw of a specified axis
\param axisId Axis index
\return Specified scaleDraw for axis, or NULL if axis is invalid.
15 years ago
*/
QwtScaleDraw *QwtPlot::axisScaleDraw( int axisId )
15 years ago
{
if ( !axisValid( axisId ) )
15 years ago
return NULL;
return axisWidget( axisId )->scaleDraw();
15 years ago
}
/*!
\brief Return the step size parameter that has been set in setAxisScale.
This doesn't need to be the step size of the current scale.
\param axisId Axis index
\return step size parameter value
\sa setAxisScale(), QwtScaleEngine::divideScale()
15 years ago
*/
double QwtPlot::axisStepSize( int axisId ) const
15 years ago
{
if ( !axisValid( axisId ) )
return 0;
15 years ago
return d_axisData[axisId]->stepSize;
15 years ago
}
/*!
\brief Return the current interval of the specified axis
15 years ago
This is only a convenience function for axisScaleDiv( axisId )->interval();
\param axisId Axis index
\return Scale interval
15 years ago
\sa QwtScaleDiv, axisScaleDiv()
*/
QwtInterval QwtPlot::axisInterval( int axisId ) const
15 years ago
{
if ( !axisValid( axisId ) )
return QwtInterval();
15 years ago
return d_axisData[axisId]->scaleDiv.interval();
15 years ago
}
/*!
\return Title of a specified axis
\param axisId Axis index
15 years ago
*/
QwtText QwtPlot::axisTitle( int axisId ) const
15 years ago
{
if ( axisValid( axisId ) )
return axisWidget( axisId )->title();
15 years ago
else
return QwtText();
}
/*!
\brief Enable or disable a specified axis
When an axis is disabled, this only means that it is not
visible on the screen. Curves, markers and can be attached
to disabled axes, and transformation of screen coordinates
into values works as normal.
Only xBottom and yLeft are enabled by default.
\param axisId Axis index
15 years ago
\param tf \c true (enabled) or \c false (disabled)
*/
void QwtPlot::enableAxis( int axisId, bool tf )
15 years ago
{
if ( axisValid( axisId ) && tf != d_axisData[axisId]->isEnabled )
{
15 years ago
d_axisData[axisId]->isEnabled = tf;
updateLayout();
}
}
/*!
Transform the x or y coordinate of a position in the
drawing region into a value.
\param axisId Axis index
15 years ago
\param pos position
\return Position as axis coordinate
15 years ago
\warning The position can be an x or a y coordinate,
depending on the specified axis.
*/
double QwtPlot::invTransform( int axisId, int pos ) const
15 years ago
{
if ( axisValid( axisId ) )
return( canvasMap( axisId ).invTransform( pos ) );
15 years ago
else
return 0.0;
15 years ago
}
/*!
\brief Transform a value into a coordinate in the plotting region
\param axisId Axis index
15 years ago
\param value value
\return X or Y coordinate in the plotting region corresponding
15 years ago
to the value.
*/
double QwtPlot::transform( int axisId, double value ) const
15 years ago
{
if ( axisValid( axisId ) )
return( canvasMap( axisId ).transform( value ) );
15 years ago
else
return 0.0;
15 years ago
}
/*!
\brief Change the font of an axis
\param axisId Axis index
\param font Font
15 years ago
\warning This function changes the font of the tick labels,
not of the axis title.
*/
void QwtPlot::setAxisFont( int axisId, const QFont &font )
15 years ago
{
if ( axisValid( axisId ) )
axisWidget( axisId )->setFont( font );
15 years ago
}
/*!
\brief Enable autoscaling for a specified axis
This member function is used to switch back to autoscaling mode
after a fixed scale has been set. Autoscaling is enabled by default.
\param axisId Axis index
\param on On/Off
\sa setAxisScale(), setAxisScaleDiv(), updateAxes()
\note The autoscaling flag has no effect until updateAxes() is executed
( called by replot() ).
15 years ago
*/
void QwtPlot::setAxisAutoScale( int axisId, bool on )
15 years ago
{
if ( axisValid( axisId ) && ( d_axisData[axisId]->doAutoScale != on ) )
{
d_axisData[axisId]->doAutoScale = on;
15 years ago
autoRefresh();
}
}
/*!
\brief Disable autoscaling and specify a fixed scale for a selected axis.
In updateAxes() the scale engine calculates a scale division from the
specified parameters, that will be assigned to the scale widget. So
updates of the scale widget usually happen delayed with the next replot.
\param axisId Axis index
\param min Minimum of the scale
\param max Maximum of the scale
15 years ago
\param stepSize Major step size. If <code>step == 0</code>, the step size is
calculated automatically using the maxMajor setting.
\sa setAxisMaxMajor(), setAxisAutoScale(), axisStepSize(), QwtScaleEngine::divideScale()
15 years ago
*/
void QwtPlot::setAxisScale( int axisId, double min, double max, double stepSize )
15 years ago
{
if ( axisValid( axisId ) )
{
15 years ago
AxisData &d = *d_axisData[axisId];
d.doAutoScale = false;
d.isValid = false;
15 years ago
d.minValue = min;
d.maxValue = max;
d.stepSize = stepSize;
15 years ago
autoRefresh();
}
}
/*!
\brief Disable autoscaling and specify a fixed scale for a selected axis.
The scale division will be stored locally only until the next call
of updateAxes(). So updates of the scale widget usually happen delayed with
the next replot.
\param axisId Axis index
15 years ago
\param scaleDiv Scale division
15 years ago
\sa setAxisScale(), setAxisAutoScale()
*/
void QwtPlot::setAxisScaleDiv( int axisId, const QwtScaleDiv &scaleDiv )
15 years ago
{
if ( axisValid( axisId ) )
{
15 years ago
AxisData &d = *d_axisData[axisId];
d.doAutoScale = false;
d.scaleDiv = scaleDiv;
d.isValid = true;
15 years ago
autoRefresh();
}
}
/*!
\brief Set a scale draw
\param axisId Axis index
\param scaleDraw Object responsible for drawing scales.
15 years ago
By passing scaleDraw it is possible to extend QwtScaleDraw
functionality and let it take place in QwtPlot. Please note
that scaleDraw has to be created with new and will be deleted
by the corresponding QwtScale member ( like a child object ).
\sa QwtScaleDraw, QwtScaleWidget
\warning The attributes of scaleDraw will be overwritten by those of the
previous QwtScaleDraw.
15 years ago
*/
void QwtPlot::setAxisScaleDraw( int axisId, QwtScaleDraw *scaleDraw )
15 years ago
{
if ( axisValid( axisId ) )
{
axisWidget( axisId )->setScaleDraw( scaleDraw );
15 years ago
autoRefresh();
}
}
/*!
Change the alignment of the tick labels
\param axisId Axis index
\param alignment Or'd Qt::AlignmentFlags see <qnamespace.h>
15 years ago
\sa QwtScaleDraw::setLabelAlignment()
*/
void QwtPlot::setAxisLabelAlignment( int axisId, Qt::Alignment alignment )
15 years ago
{
if ( axisValid( axisId ) )
axisWidget( axisId )->setLabelAlignment( alignment );
15 years ago
}
/*!
Rotate all tick labels
\param axisId Axis index
15 years ago
\param rotation Angle in degrees. When changing the label rotation,
the label alignment might be adjusted too.
\sa QwtScaleDraw::setLabelRotation(), setAxisLabelAlignment()
15 years ago
*/
void QwtPlot::setAxisLabelRotation( int axisId, double rotation )
15 years ago
{
if ( axisValid( axisId ) )
axisWidget( axisId )->setLabelRotation( rotation );
15 years ago
}
/*!
Set the maximum number of minor scale intervals for a specified axis
\param axisId Axis index
\param maxMinor Maximum number of minor steps
15 years ago
\sa axisMaxMinor()
*/
void QwtPlot::setAxisMaxMinor( int axisId, int maxMinor )
15 years ago
{
if ( axisValid( axisId ) )
{
maxMinor = qBound( 0, maxMinor, 100 );
15 years ago
AxisData &d = *d_axisData[axisId];
if ( maxMinor != d.maxMinor )
{
15 years ago
d.maxMinor = maxMinor;
d.isValid = false;
15 years ago
autoRefresh();
}
}
}
/*!
Set the maximum number of major scale intervals for a specified axis
\param axisId Axis index
\param maxMajor Maximum number of major steps
15 years ago
\sa axisMaxMajor()
*/
void QwtPlot::setAxisMaxMajor( int axisId, int maxMajor )
15 years ago
{
if ( axisValid( axisId ) )
{
maxMajor = qBound( 1, maxMajor, 10000 );
15 years ago
AxisData &d = *d_axisData[axisId];
if ( maxMajor != d.maxMajor )
{
15 years ago
d.maxMajor = maxMajor;
d.isValid = false;
15 years ago
autoRefresh();
}
}
}
/*!
\brief Change the title of a specified axis
\param axisId Axis index
15 years ago
\param title axis title
*/
void QwtPlot::setAxisTitle( int axisId, const QString &title )
15 years ago
{
if ( axisValid( axisId ) )
axisWidget( axisId )->setTitle( title );
15 years ago
}
/*!
\brief Change the title of a specified axis
\param axisId Axis index
\param title Axis title
15 years ago
*/
void QwtPlot::setAxisTitle( int axisId, const QwtText &title )
15 years ago
{
if ( axisValid( axisId ) )
axisWidget( axisId )->setTitle( title );
15 years ago
}
/*!
\brief Rebuild the axes scales
In case of autoscaling the boundaries of a scale are calculated
from the bounding rectangles of all plot items, having the
QwtPlotItem::AutoScale flag enabled ( QwtScaleEngine::autoScale() ).
Then a scale division is calculated ( QwtScaleEngine::didvideScale() )
and assigned to scale widget.
When the scale boundaries have been assigned with setAxisScale() a
scale division is calculated ( QwtScaleEngine::didvideScale() )
for this interval and assigned to the scale widget.
When the scale has been set explicitly by setAxisScaleDiv() the
locally stored scale division gets assigned to the scale widget.
The scale widget indicates modifications by emitting a
QwtScaleWidget::scaleDivChanged() signal.
updateAxes() is usually called by replot().
\sa setAxisAutoScale(), setAxisScale(), setAxisScaleDiv(), replot()
QwtPlotItem::boundingRect()
*/
void QwtPlot::updateAxes()
15 years ago
{
// Find bounding interval of the item data
// for all axes, where autoscaling is enabled
QwtInterval intv[axisCnt];
15 years ago
const QwtPlotItemList& itmList = itemList();
QwtPlotItemIterator it;
for ( it = itmList.begin(); it != itmList.end(); ++it )
{
15 years ago
const QwtPlotItem *item = *it;
if ( !item->testItemAttribute( QwtPlotItem::AutoScale ) )
continue;
if ( !item->isVisible() )
15 years ago
continue;
if ( axisAutoScale( item->xAxis() ) || axisAutoScale( item->yAxis() ) )
{
const QRectF rect = item->boundingRect();
if ( rect.width() >= 0.0 )
intv[item->xAxis()] |= QwtInterval( rect.left(), rect.right() );
if ( rect.height() >= 0.0 )
intv[item->yAxis()] |= QwtInterval( rect.top(), rect.bottom() );
15 years ago
}
}
// Adjust scales
for ( int axisId = 0; axisId < axisCnt; axisId++ )
{
15 years ago
AxisData &d = *d_axisData[axisId];
double minValue = d.minValue;
double maxValue = d.maxValue;
double stepSize = d.stepSize;
if ( d.doAutoScale && intv[axisId].isValid() )
{
d.isValid = false;
15 years ago
minValue = intv[axisId].minValue();
maxValue = intv[axisId].maxValue();
d.scaleEngine->autoScale( d.maxMajor,
minValue, maxValue, stepSize );
15 years ago
}
if ( !d.isValid )
{
15 years ago
d.scaleDiv = d.scaleEngine->divideScale(
minValue, maxValue,
d.maxMajor, d.maxMinor, stepSize );
d.isValid = true;
15 years ago
}
QwtScaleWidget *scaleWidget = axisWidget( axisId );
scaleWidget->setScaleDiv( d.scaleDiv );
15 years ago
int startDist, endDist;
scaleWidget->getBorderDistHint( startDist, endDist );
scaleWidget->setBorderDist( startDist, endDist );
15 years ago
}
for ( it = itmList.begin(); it != itmList.end(); ++it )
{
15 years ago
QwtPlotItem *item = *it;
if ( item->testItemInterest( QwtPlotItem::ScaleInterest ) )
{
item->updateScaleDiv( axisScaleDiv( item->xAxis() ),
axisScaleDiv( item->yAxis() ) );
}
15 years ago
}
}