Qwt source code interpretation of ruler-related classes

Source: Internet
Author: User

It is very convenient and beautiful to add a ruler to qwt. In addition to general ruler, there is also a circular ruler. Here we will first learn about the class of the ruler. There are several classes related to the ruler:

1. qwtscalemap: scale value ing Class.

The qwtscalemap class is used to provide ing between the scale coordinate system and the drawing device coordinate system. Its main functions are implemented based on the qwtscaletransformation class.

2. qwtscalediv: scale classification.

The qwtscalediv class represents the scale division. The scale division of a scale includes its start value and three Scale lists (mainly scale list, secondary scale list, and minimum scale list ). In most cases, the scale division is automatically calculated by qwtscaleengine (or its derived class.

3. qwtscaledraw: class for drawing a ruler.

The qwtscaledraw class inherits from the abstract base class qwtabstractscaledraw, which is used to draw common scales. A scale is also composed of multiple parts (as shown in the following figure), which are identified by the variable qwtabstractscaledraw: scalecomponent.

1) along the backbone ruler.
2) ticks dial.
3) Labels value tag.

4. qwtscalewidget: ruler component.

The qwtscalewidget class is a component that is rendered or used to draw a ruler. It inherits from qwidget. In actual development, this class is used a lot.

Code Analysis:

1. qwtscaletransformation class

In addition to constructors and habits, the qwtscaletransformation class provides the following interfaces: conversion between the scale coordinate value and the drawing device coordinate value.

    virtual double xForm( double s, double s1, double s2,        double p1, double p2 ) const;    virtual double invXForm( double p, double p1, double p2,        double s1, double s2 ) const;

Since the copy constructor and copy assignment operator functions are disabled, the clone function is also provided:

//! Create a clone of the transformationQwtScaleTransformation *QwtScaleTransformation::copy() const{    return new QwtScaleTransformation( d_type );}

2. qwtscalemap class
Here we will take a look at the copy constructor and copy assignment operator functions of qwtscalemap to help you understand the copy () function of the qwtscaletransformation class above.

//! Copy constructorQwtScaleMap::QwtScaleMap( const QwtScaleMap& other ):    d_s1( other.d_s1 ),    d_s2( other.d_s2 ),    d_p1( other.d_p1 ),    d_p2( other.d_p2 ),    d_cnv( other.d_cnv ){    d_transformation = other.d_transformation->copy();}//! Assignment operatorQwtScaleMap &QwtScaleMap::operator=( const QwtScaleMap & other ){    d_s1 = other.d_s1;    d_s2 = other.d_s2;    d_p1 = other.d_p1;    d_p2 = other.d_p2;    d_cnv = other.d_cnv;    delete d_transformation;    d_transformation = other.d_transformation->copy();    return *this;}//! Copy constructorQwtScaleMap::QwtScaleMap( const QwtScaleMap& other ):    d_s1( other.d_s1 ),    d_s2( other.d_s2 ),    d_p1( other.d_p1 ),    d_p2( other.d_p2 ),    d_cnv( other.d_cnv ){    d_transformation = other.d_transformation->copy();}//! Assignment operatorQwtScaleMap &QwtScaleMap::operator=( const QwtScaleMap & other ){    d_s1 = other.d_s1;    d_s2 = other.d_s2;    d_p1 = other.d_p1;    d_p2 = other.d_p2;    d_cnv = other.d_cnv;    delete d_transformation;    d_transformation = other.d_transformation->copy();    return *this;}

Then, the parameter setting interface of qwtscalemap and the function that completes value conversion between coordinate systems:

    void setPaintInterval( double p1, double p2 );    void setScaleInterval( double s1, double s2 );    double transform( double s ) const;    double invTransform( double p ) const;

3. qwtscalediv class
The qwtscalediv class has three constructors. The default constructor without parameters creates an "invalid" scale division instance. The constructor with two parameters can understand the basic attribute features of qwtscalediv.

/*!  Construct QwtScaleDiv instance.  \param interval Interval  \param ticks List of major, medium and minor ticks*/QwtScaleDiv::QwtScaleDiv( const QwtInterval &interval,        QList<double> ticks[NTickTypes] ):    d_lowerBound( interval.minValue() ),    d_upperBound( interval.maxValue() ),    d_isValid( true ){    for ( int i = 0; i < NTickTypes; i++ )        d_ticks[i] = ticks[i];}

Let's take a look at its implementation of the function interfaces that are worth learning:

/*! Return a list of ticks \ Param type minortick, mediumtick or majortick */const qlist <double> & qwtscalediv: ticks (INT type) const {If (type> = 0 | type <nticktypes) // error prevention judgment, OK! Return d_ticks [type]; static qlist <double> noticks; return noticks ;}

Static qlist <double> noticks; // defines a local static variable, which can be initialized only once. If ticks () is called in a loop and the input parameter is incorrect, the efficiency can be greatly improved.

4. qwtabstractscaledraw class
Data:

Class metadata: privatedata {public: privatedata (): spacing (4.0), penwidth (0), minextent (0.0) {components = qwtabstractscaledraw: backbone | qwtabstractscaledraw :: ticks | labels: labels; ticklength [qwtscalediv: minortick] = 4.0; ticklength [qwtscalediv: mediumtick] = 6.0; ticklength [qwtscalediv: majortick] = 8.0 ;} scalecomponents; // The qwtscalemap map; qwtscalediv scldiv; double spacing; double ticklength [qwtscalediv: nticktypes]; // The length of the dial line int penwidth; double minextent; qmap <double, qwttext> labelcache; // tag cache };

Implementation:
The qwtabstractscaledraw class uses the template method mode [template method]. The draw () method defines the basic framework for drawing a ruler:

/*! \ Brief draw the scale \ Param painter the painter \ Param palette, text color is used for the labels, foreground color for ticks and backbone */void qwtabstractscaledraw :: draw (qpainter * painter, const qpalette & palette) const {painter-> Save (); qpen pen = painter-> pen (); pen. setwidth (d_data-> penwidth); pen. setcosmetic (false); painter-> setpen (PEN); If (hascomponent (qwtabstractscaledraw: labels) {painter-> Save (); painter-> setpen (palette. color (qpalette: Text); // ignore pen style const qlist <double> & majorticks = d_data-> scldiv. ticks (qwtscalediv: majortick); For (INT I = 0; I <majorticks. count (); I ++) {const Double V = majorticks [I]; If (d_data-> scldiv. contains (V) drawlabel (painter, majorticks [I]); // draw labels, pure virtual functions, and delay to sub-classes} painter-> restore ();} if (hascomponent (qwtabstractscaledraw: ticks) {painter-> Save (); qpen pen = painter-> pen (); pen. setcolor (palette. color (qpalette: windowtext); pen. setcapstyle (QT: flatcap); painter-> setpen (PEN); For (INT ticktype = qwtscalediv: minortick; ticktype <qwtscalediv: nticktypes; ticktype ++) {const qlist <double> & ticks = d_data-> scldiv. ticks (ticktype); For (INT I = 0; I <ticks. count (); I ++) {const Double V = ticks [I]; If (d_data-> scldiv. contains (V) drawtick (painter, V, d_data-> ticklength [ticktype]); // draw the scale, pure virtual function, the specific implementation delay is in the subclass} painter-> restore ();} If (hascomponent (qwtabstractscaledraw: backbone) {painter-> Save (); qpen pen = painter-> pen (); pen. setcolor (palette. color (qpalette: windowtext); pen. setcapstyle (QT: flatcap); painter-> setpen (PEN); drawbackbone (painter); // draw the bottom line of the ruler, pure virtual function, the specific implementation is delayed to Painter-> restore ();} painter-> restore ();} in the subclass ();}

Parameter Setting interface:

    void setScaleDiv( const QwtScaleDiv &s );    const QwtScaleDiv& scaleDiv() const;    void setTransformation( QwtScaleTransformation * );    const QwtScaleMap &scaleMap() const;    QwtScaleMap &scaleMap();

Of course, you can also set the width of the pen, the length of the dial, and other attributes to display scales of different styles.
5. qwtscaledraw class
It mainly implements four pure virtual functions of qwtabstractscaledraw, as well as the movement of the ruler's position (origin), label rotation, scale alignment, and other functions.
6. qwtscalewidget class
A good interface naming example code:

Void getminborderdist (Int & START, Int & End) const; // two values void setminborderdist (INT start, int end) are returned through reference );

Set coordinate conversion and scale Division:

    void setScaleDiv( QwtScaleTransformation *, const QwtScaleDiv &sd );    void setScaleDraw( QwtScaleDraw * );    const QwtScaleDraw *scaleDraw() const;    QwtScaleDraw *scaleDraw();

Usage of color labels:

    void setColorMap( const QwtInterval &, QwtColorMap * );    QwtInterval colorBarInterval() const;    const QwtColorMap *colorMap() const;

7. Finally, let's look at the "static tool class" qwtscalearithmetic used by the ruler:

/*!  \brief Arithmetic including a tolerance*/class QWT_EXPORT QwtScaleArithmetic{public:    static double ceilEps( double value, double intervalSize );    static double floorEps( double value, double intervalSize );    static double divideEps( double interval, double steps );    static double ceil125( double x );    static double floor125( double x );};

In this way, the static function defined by classification is much better than the global function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.