您的位置:首页 > 其它

Qwt源码解读之标尺相关类——QwtScaleDiv

2014-07-11 18:40 399 查看
先看一看Qwt文档的说明

A class representing a scale division.

A Qwt scale is defined by its
boundaries and 3 list for the positions of the major, medium and minor ticks.

The upperLimit() might be smaller than the lowerLimit() to indicate inverted scales.上限可以小于下限,这说明标尺是逆序的

Scale divisions can be calculated from a QwtScaleEngine.

See Also

QwtScaleEngine::divideScale(), QwtPlot::setAxisScaleDiv(), QwtAbstractSlider::setScaleDiv()

类定义

class QWT_EXPORT QwtScaleDiv
{
public:
//! Scale tick types
enum TickType
{
//! No ticks
NoTick = -1,

//! Minor ticks
MinorTick,

//! Medium ticks
MediumTick,

//! Major ticks
MajorTick,

//! Number of valid tick types
NTickTypes
};

explicit QwtScaleDiv( double lowerBound = 0.0,
double upperBound = 0.0 );//构造函数

explicit QwtScaleDiv( const QwtInterval &, QList<double>[NTickTypes] );//构造函数

explicit QwtScaleDiv( double lowerBound, double upperBound,
QList<double>[NTickTypes] );//构造函数

explicit QwtScaleDiv( double lowerBound, double upperBound,
const QList<double> &minorTicks, const QList<double> &mediumTicks,
const QList<double> &majorTicks );//构造函数

bool operator==( const QwtScaleDiv & ) const;
bool operator!=( const QwtScaleDiv & ) const;

void setInterval( double lowerBound, double upperBound );
void setInterval( const QwtInterval & );
QwtInterval interval() const;

void setLowerBound( double );
double lowerBound() const;

void setUpperBound( double );
double upperBound() const;

double range() const;

bool contains( double value ) const;

void setTicks( int tickType, const QList<double> & );
QList<double> ticks( int tickType ) const;

bool isEmpty() const;
bool isIncreasing() const;

void invert();
QwtScaleDiv inverted() const;

QwtScaleDiv bounded( double lowerBound, double upperBound ) const;

private:
double d_lowerBound;//下边界
double d_upperBound;//上边界,注意:下边界有可能大于上边界(此时刻度划分是逆序的)。
QList<double> d_ticks[NTickTypes];
};

一些成员函数

/*!
Change the interval

\param lowerBound First boundary
\param upperBound Second boundary

\note lowerBound might be greater than upperBound for inverted scales
*/
void QwtScaleDiv::setInterval( double lowerBound, double upperBound )
{
d_lowerBound = lowerBound;
d_upperBound = upperBound;
}

/*!
Change the interval

\param interval Interval
*/
void QwtScaleDiv::setInterval( const QwtInterval &interval )
{
d_lowerBound = interval.minValue();
d_upperBound = interval.maxValue();
}


设置刻度划分的上下界。注意,下边界也可能大于上边界。

/*!
\return lowerBound -> upperBound
*/
QwtInterval QwtScaleDiv::interval() const
{
return QwtInterval( d_lowerBound, d_upperBound );
}
返回刻度划分的区间。

/*!
\return upperBound() - lowerBound()
*/
double QwtScaleDiv::range() const
{
return d_upperBound - d_lowerBound;
}
返回刻度划分的区间宽度。注意,如果d_lowerBound>d_upperBound,则返回的是负值。

//! Check if the scale division is empty( lowerBound() == upperBound() )
bool QwtScaleDiv::isEmpty() const
{
return ( d_lowerBound == d_upperBound );
}
如果上下边界相同,显然这个刻度划分是空的。

//! Check if the scale division is increasing( lowerBound() <= upperBound() )
bool QwtScaleDiv::isIncreasing() const
{
return d_lowerBound <= d_upperBound;
}
检查这个刻度划分是否递增的。

/*!
Return if a value is between lowerBound() and upperBound()

\param value Value
\return true/false
*/
bool QwtScaleDiv::contains( double value ) const
{
const double min = qMin( d_lowerBound, d_upperBound );
const double max = qMax( d_lowerBound, d_upperBound );

return value >= min && value <= max;
}
检查值value是否在这个刻度划分的范围之内。

/*!
Invert the scale division
\sa inverted()
*/
void QwtScaleDiv::invert()
{
qSwap( d_lowerBound, d_upperBound );

for ( int i = 0; i < NTickTypes; i++ )
{
QList<double>& ticks = d_ticks[i];

const int size = ticks.count();
const int size2 = size / 2;

for ( int j = 0; j < size2; j++ )
qSwap( ticks[j], ticks[size - 1 - j] );
}
}
反转刻度划分。

/*!
\return A scale division with inverted boundaries and ticks
\sa invert()
*/
QwtScaleDiv QwtScaleDiv::inverted() const
{
QwtScaleDiv other = *this;
other.invert();

return other;
}
返回一个刻度划分反转的新QwtScaleDiv对象。

/*!
Return a scale division with an interval [lowerBound, upperBound]
where all ticks outside this interval are removed

\param lowerBound Lower bound
\param upperBound Upper bound

\return Scale division with all ticks inside of the given interval

\note lowerBound might be greater than upperBound for inverted scales
*/
QwtScaleDiv QwtScaleDiv::bounded(
double lowerBound, double upperBound ) const
{
const double min = qMin( lowerBound, upperBound );
const double max = qMax( lowerBound, upperBound );

QwtScaleDiv sd;
sd.setInterval( lowerBound, upperBound );

for ( int tickType = 0; tickType < QwtScaleDiv::NTickTypes; tickType++ )
{
const QList<double> &ticks = d_ticks[ tickType ];

QList<double> boundedTicks;
for ( int i = 0; i < ticks.size(); i++ )
{
const double tick = ticks[i];
if ( tick >= min && tick <= max )
boundedTicks += tick;
}

sd.setTicks( tickType, boundedTicks );
}

return sd;

}
返回一个新的刻度划分,它的上下边界及其ticks由参数lowerBound和upperBound限制。

/*!
Assign ticks

\param type MinorTick, MediumTick or MajorTick
\param ticks Values of the tick positions
*/
void QwtScaleDiv::setTicks( int type, const QList<double> &ticks )
{
if ( type >= 0 && type < NTickTypes )// 赋值之前,首先对type的合法性进行检查
d_ticks[type] = ticks;
}
给MinorTick、MediumTick、MajorTick赋值。

/*!
Return a list of ticks

\param type MinorTick, MediumTick or MajorTick
\return Tick list
*/
QList<double> QwtScaleDiv::ticks( int type ) const
{
if ( type >= 0 && type < NTickTypes )// 首先对type的合法性进行检查
return d_ticks[type];

return QList<double>();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: