Qwt源码解读之QwtPlotItem类(一)

QwtPlotItem类是画布上所有图元的基类。QwtPlotItem是一个抽象基类,所有的实例图元必须实现YourPlotItem::draw()方法(纯虚函数)。具体的看看Qwt文档:

Base class for items on the plot canvas.

A plot item is "something", that can be painted on the plot canvas, or only affects the scales of the plot widget. They can be categorized as:

  • Representator
    A "Representator" is an item that represents some sort of data on the plot canvas. The different representator classes are organized according to the characteristics of the data:

Depending on the QwtPlotItem::ItemAttribute flags, an item is included into autoscaling or has an entry on the legnd.

Before misusing the existing item classes it might be better to implement a new type of plot item ( don't implement a watermark as spectrogram ). Deriving a new type of QwtPlotItemprimarily means to implement the YourPlotItem::draw() method.

See also:
The cpuplot example shows the implementation of additional  plot items.

QwtPlotItem继承自QwtLegendItemManager类,其目的是为了绑定QwtPlotItem到QwtLegend。即QwtLegendItemManager类主要是被当作接口使用。【Abstract API to bind plot items to the legend.】 继承关系示例图如下:


QwtLegendItemManager类代码分析:

/*!
  \brief Abstract API to bind plot items to the legend
*/

class QWT_EXPORT QwtLegendItemManager
{
public:
    //! Constructor
    QwtLegendItemManager()
    {
    }

    //! Destructor
    virtual ~QwtLegendItemManager()
    {
    }

    /*!
      Update the widget that represents the item on the legend
      \param legend Legend
      \sa legendItem()
     */
    virtual void updateLegend( QwtLegend *legend ) const = 0;

    /*!
      Allocate the widget that represents the item on the legend
      \return Allocated widget
      \sa updateLegend() QwtLegend()
     */

    virtual QWidget *legendItem() const = 0;

    /*!
      QwtLegendItem can display an icon-identifier followed
      by a text. The icon helps to identify a plot item on
      the plot canvas and depends on the type of information,
      that is displayed.

      The default implementation paints nothing.
     */
    virtual void drawLegendIdentifier( QPainter *, const QRectF & ) const
    {
    }
};

QwtPlotItem类代码分析:

数据:

class QwtPlotItem::PrivateData
{
public:
    PrivateData():
        plot( NULL ),
        isVisible( true ),
        attributes( 0 ),
        renderHints( 0 ),
        z( 0.0 ),
        xAxis( QwtPlot::xBottom ),
        yAxis( QwtPlot::yLeft )
    {
    }

    mutable QwtPlot *plot;

    bool isVisible;
    QwtPlotItem::ItemAttributes attributes;
    QwtPlotItem::RenderHints renderHints;
    double z;

    int xAxis;
    int yAxis;

    QwtText title;
};
1、关于mutable:mutable关键词的作用是可以在常函数中修改被mutable修饰的成员变量的值。但从代码的实现来看,没见过这种需求的必要?

2、QwtPlotItem仍然是一个抽象基类,因此只定义了很少的必须的共有属性。

3、这种共性的提取是在设计一个继承体系时需要重点考虑和验证的,也是在编码过程中重构需要时时关心的地方。

实现:

1、返回运行时类型信息:

virtual int rtti() const; // 运行时类型信息

2、设置图元的叠加(绘制)顺序:

/*!
   \brief Set the z value

   Plot items are painted in increasing z-order.

   \param z Z-value
   \sa z(), QwtPlotDict::itemList()
*/
void QwtPlotItem::setZ( double z )
{
    if ( d_data->z != z )
    {
        if ( d_data->plot ) // update the z order
            d_data->plot->attachItem( this, false );

        d_data->z = z;

        if ( d_data->plot )
            d_data->plot->attachItem( this, true ); // 重新排列Items的顺序

        itemChanged();
    }
}
3、需要在派生类重新实现的几个虚函数:

    virtual void itemChanged(); // 更新legend并会调用QwtPlot的自动更新功能

    /*!
      \brief Draw the item

      \param painter Painter
      \param xMap Maps x-values into pixel coordinates.
      \param yMap Maps y-values into pixel coordinates.
      \param canvasRect Contents rect of the canvas in painter coordinates
    */
    virtual void draw( QPainter *painter,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect ) const = 0; // 纯虚函数,绘制Item,会在各派生类中实现

    virtual QRectF boundingRect() const; // Item 的外围边界框,默认实现返回一个无效的矩形,会在各派生类中重新实现

    virtual void updateLegend( QwtLegend *legend ) const; // 更新(还不存在的话则创建)QwtLegendItem, 并绘制相应的标识图示(drawLegendIdentifier())
    virtual void updateScaleDiv( 
        const QwtScaleDiv&, const QwtScaleDiv& ); // 该接口的默认的实现是:什么都不做。只是对某些特定的派生类需要重新实现(like QwtPlotGrid())

    virtual QWidget *legendItem() const; // Allocate the widget that represents the item on the legend
4、禁用拷贝和赋值:

private:
    // Disabled copy constructor and operator=
    QwtPlotItem( const QwtPlotItem & );
    QwtPlotItem &operator=( const QwtPlotItem & );



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值