⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qwt_plot.cpp

📁 软件无线电的平台
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- ***************************** * 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 *****************************************************************************/// vim: expandtab#include <qlabel.h>#include <qpainter.h>#include <qfocusdata.h>#include "qwt_plot.h"#include "qwt_plot_layout.h"#include "qwt_plot_dict.h"#include "qwt_rect.h"#include "qwt_scale.h"#include "qwt_scale.h"#include "qwt_legend.h"#include "qwt_dyngrid_layout.h"#include "qwt_plot_canvas.h"#include "qwt_math.h"#include "qwt_paint_buffer.h"/*!  \brief Constructor  \param parent Parent widget  \param name Widget name */QwtPlot::QwtPlot(QWidget *parent, const char *name) :    QFrame(parent, name, WRepaintNoErase|WResizeNoErase){    initPlot();}/*!  \brief Constructor  \param title Title text  \param parent Parent widget  \param name Widget name */QwtPlot::QwtPlot(const QString &title, QWidget *parent, const char *name) :    QFrame(parent, name, WRepaintNoErase|WResizeNoErase){    initPlot(title);}//! DestructorQwtPlot::~QwtPlot(){    delete d_layout;    delete d_curves;    delete d_markers;    delete d_grid;}/*!  \brief Initializes a QwtPlot instance  \param title Title text */void QwtPlot::initPlot(const QString &title){    d_layout = new QwtPlotLayout;    d_curves = new QwtCurveDict;    d_markers = new QwtMarkerDict;    d_autoReplot = FALSE;    d_lblTitle = new QLabel(title, this);    d_lblTitle->setFont(QFont(fontInfo().family(), 14, QFont::Bold));    d_lblTitle->setAlignment(AlignCenter|WordBreak|ExpandTabs);    d_legend = new QwtLegend(this);    d_autoLegend = FALSE;    d_scale[yLeft] = new QwtScale(QwtScale::Left, this, "yLeft");    d_scale[yRight] = new QwtScale(QwtScale::Right, this, "yRight");    d_scale[xTop] = new QwtScale(QwtScale::Top, this, "xTop");    d_scale[xBottom] = new QwtScale(QwtScale::Bottom, this, "xBottom");    initAxes();    d_grid = new QwtPlotGrid(this);    d_grid->setPen(QPen(black, 0, DotLine));    d_grid->enableXMin(FALSE);    d_grid->enableYMin(FALSE);    d_grid->setAxis(xBottom, yLeft);    d_canvas = new QwtPlotCanvas(this);    d_canvas->setFrameStyle(QFrame::Panel|QFrame::Sunken);    d_canvas->setLineWidth(2);    d_canvas->setMidLineWidth(0);#ifndef QWT_NO_COMPAT    connect(d_canvas, SIGNAL(mousePressed(const QMouseEvent &)),        this, SIGNAL(plotMousePressed(const QMouseEvent &)));    connect(d_canvas, SIGNAL(mouseMoved(const QMouseEvent &)),        this, SIGNAL(plotMouseMoved(const QMouseEvent &)));    connect(d_canvas, SIGNAL(mouseReleased(const QMouseEvent &)),        this, SIGNAL(plotMouseReleased(const QMouseEvent &)));#endif    updateTabOrder();}//! Initialize axesvoid QwtPlot::initAxes(){    int axis;    QFont fscl(fontInfo().family(), 10);    QFont fttl(fontInfo().family(), 12, QFont::Bold);    for(axis = 0; axis < axisCnt; axis++)    {        d_scale[axis]->setFont(fscl);        d_scale[axis]->setTitleFont(fttl);        d_scale[axis]->setBaselineDist(2);    }    d_axisEnabled[yLeft] = TRUE;    d_axisEnabled[yRight] = FALSE;    d_axisEnabled[xBottom] = TRUE;    d_axisEnabled[xTop] = FALSE;    for (axis=0; axis < axisCnt; axis++)    {        d_as[axis].adjust(0.0,1000.0,TRUE);        d_scale[axis]->setScaleDiv(d_as[axis].scaleDiv());    }}/*!  \brief Adds handling of QEvent::LayoutHint*/bool QwtPlot::event(QEvent *e){    bool ok = QFrame::event(e);    switch(e->type())    {#if 0        case QEvent::ChildInserted:        case QEvent::ChildRemoved:#endif        case QEvent::LayoutHint:            updateLayout();            break;        default:;    }    return ok;}/*!  \brief Replots the plot if QwtPlot::autoReplot() is \c TRUE.*/void QwtPlot::autoRefresh(){    if (d_autoReplot)        replot();}/*!  \brief Set or reset the autoReplot option  If the autoReplot option is set, the plot will be  updated implicitly by manipulating member functions.  Since this may be time-consuming, it is recommended  to leave this option switched off and call replot()  explicitly if necessary.  The autoReplot option is set to FALSE by default, which  means that the user has to call replot() in order to make  changes visible.  \param tf \c TRUE or \c FALSE. Defaults to \c TRUE.  \sa replot()*/void QwtPlot::setAutoReplot(bool tf){    d_autoReplot = tf;}/*!    \return TRUE if the autoReplot option is set.*/bool QwtPlot::autoReplot() const{    return d_autoReplot; }/*!  \brief Change the plot's title  \param t new title*/void QwtPlot::setTitle(const QString &t){    d_lblTitle->setText(t);}/*!  \return the plot's title*/QString QwtPlot::title() const{    return d_lblTitle->text();}/*!  \brief Change the title font  \param f new title font*/void QwtPlot::setTitleFont(const QFont &f){    d_lblTitle->setFont(f);}/*!  \return the plot's title font*/QFont QwtPlot::titleFont() const{    return d_lblTitle->font();}/*!  \return the plot's layout*/QwtPlotLayout *QwtPlot::plotLayout(){    return d_layout;}/*!  \return the plot's layout*/const QwtPlotLayout *QwtPlot::plotLayout() const{    return d_layout;}/*!  \return the plot's titel label.*/QLabel *QwtPlot::titleLabel(){    return d_lblTitle;}/*!  \return the plot's titel label.*/const QLabel *QwtPlot::titleLabel() const{    return d_lblTitle;}/*!  \return the plot's legend  \sa insertLegendItem(), updateLegendItem(), printLegendItem()*/QwtLegend *QwtPlot::legend(){     return d_legend;}   /*!  \return the plot's legend  \sa insertLegendItem(), updateLegendItem(), printLegendItem()*/const QwtLegend *QwtPlot::legend() const{     return d_legend;}   /*!  \return the plot's canvas*/QwtPlotCanvas *QwtPlot::canvas(){     return d_canvas;}   /*!  \return the plot's canvas*/const QwtPlotCanvas *QwtPlot::canvas() const{     return d_canvas;}//! Return MinimumExpanding/MinimumExpandingQSizePolicy QwtPlot::sizePolicy() const{    QSizePolicy sp;    sp.setHorData( QSizePolicy::MinimumExpanding );    sp.setVerData( QSizePolicy::MinimumExpanding );    return sp;}/*!    Return sizeHint  \sa QwtPlot::minimumSizeHint()*/QSize QwtPlot::sizeHint() const{    int dw = 0;    int dh = 0;    for ( int axis = 0; axis < axisCnt; axis++ )    {        if ( d_axisEnabled[axis] )        {               const int niceDist = 40;            const QwtScale *scale = d_scale[axis];            const int majCnt = scale->scaleDraw()->scaleDiv().majCnt();            if ( axis == yLeft || axis == yRight )            {                int hDiff = (majCnt - 1) * niceDist                     - scale->minimumSizeHint().height();                if ( hDiff > dh )                    dh = hDiff;            }            else            {                int wDiff = (majCnt - 1) * niceDist                     - scale->minimumSizeHint().width();                if ( wDiff > dw )                    dw = wDiff;            }        }    }    return minimumSizeHint() + QSize(dw, dh);}/*!  \brief Return a minimum size hint*/QSize QwtPlot::minimumSizeHint() const{    QSize hint = d_layout->minimumSizeHint(this);    hint += QSize(2 * frameWidth(), 2 * frameWidth());    return hint;}//! Resize and update internal layoutvoid QwtPlot::resizeEvent(QResizeEvent *e){    QFrame::resizeEvent(e);    updateLayout();}/*!  \brief Redraw the plot  If the autoReplot option is not set (which is the default)  or if any curves are attached to raw data, the plot has to  be refreshed explicitly in order to make changes visible.  \sa setAutoReplot()*/void QwtPlot::replot(){    bool doAutoReplot = autoReplot();    setAutoReplot(FALSE);    updateAxes();    d_canvas->drawCanvas();    setAutoReplot(doAutoReplot);}/*!  \brief Adjust plot content to its current size.  \sa QwtPlot::resizeEvent*/void QwtPlot::updateLayout(){    d_layout->activate(this, contentsRect());    //    // resize and show the visible widgets    //    if (!d_lblTitle->text().isEmpty())    {        d_lblTitle->setGeometry(d_layout->titleRect());        if (!d_lblTitle->isVisible())            d_lblTitle->show();    }    else        d_lblTitle->hide();    for (int axis = 0; axis < axisCnt; axis++ )    {        if (d_axisEnabled[axis])        {            d_scale[axis]->setGeometry(d_layout->scaleRect(axis));            if ( axis == xBottom || axis == xTop )            {                QRegion r(d_layout->scaleRect(axis));                if ( d_axisEnabled[yLeft] )                    r = r.subtract(QRegion(d_layout->scaleRect(yLeft)));                if ( d_axisEnabled[yRight] )                    r = r.subtract(QRegion(d_layout->scaleRect(yRight)));                r.translate(-d_layout->scaleRect(axis).x(),                     -d_layout->scaleRect(axis).y());                d_scale[axis]->setMask(r);            }            if (!d_scale[axis]->isVisible())                d_scale[axis]->show();        }        else            d_scale[axis]->hide();    }    if (d_legend->itemCount() > 0)    {        d_legend->setGeometry(d_layout->legendRect());        d_legend->show();    }    else        d_legend->hide();    d_canvas->setGeometry(d_layout->canvasRect());}//! Rebuild the scales and mapsvoid QwtPlot::updateAxes(){    int i;    bool resetDone[axisCnt];    for (i = 0; i < axisCnt; i++)        resetDone[i] = FALSE;    //    //  Adjust autoscalers    //    QwtPlotCurveIterator itc = curveIterator();    for (const QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc )    {        const int xAxis = c->xAxis();        const int yAxis = c->yAxis();        if ( d_as[xAxis].autoScale() || d_as[yAxis].autoScale() )        {            const QwtDoubleRect rect = c->boundingRect();            if ( rect.isValid() )            {                if ( d_as[xAxis].autoScale() )                {                    if ( !resetDone[xAxis] )                    {                        d_as[xAxis].reset();                        resetDone[xAxis] = TRUE;                    }                    d_as[xAxis].adjust(rect.x1(), rect.x2());                }                if ( d_as[yAxis].autoScale() )                {                    if ( !resetDone[yAxis] )                    {                        d_as[yAxis].reset();                        resetDone[yAxis] = TRUE;                    }                    d_as[yAxis].adjust(rect.y1(), rect.y2());                }            }        }    }    //    // Adjust scales    //    for (i = 0; i < axisCnt; i++)    {        d_scale[i]->setScaleDiv(d_as[i].scaleDiv());        int startDist, endDist;        d_scale[i]->minBorderDist(startDist, endDist);        d_scale[i]->setBorderDist(startDist, endDist);    }    d_grid->setXDiv(d_as[d_grid->xAxis()].scaleDiv());    d_grid->setYDiv(d_as[d_grid->yAxis()].scaleDiv());}//! Update the focus tab ordervoid QwtPlot::updateTabOrder(){    // Depending on the position of the legend the     // tab order will be changed that the canvas is    // next to the last legend item, or directly before    // the first one. The following code seems much too    // complicated but is there a better implementation ?    if ( d_canvas->focusPolicy() == NoFocus || focusData() == NULL )        return;    // move the cursor to the canvas    for ( int i = 0; i < focusData()->count(); i++ )    {        if ( focusData()->next() == d_canvas )            break;    }    const bool canvasFirst = d_layout->legendPos() == Qwt::Bottom ||        d_layout->legendPos() == Qwt::Right;    for ( int j = 0; j < focusData()->count(); j++ )    {        QWidget *w = canvasFirst ? focusData()->next() : focusData()->prev();        if ( w->focusPolicy() != NoFocus             && w->parent() && w->parent() == d_legend->contentsWidget() )        {            if ( canvasFirst )            {                do // go back to last non legend item                {                    w = focusData()->prev(); // before the first legend item                } while ( w->focusPolicy() == NoFocus );            }            if ( w != d_canvas )                setTabOrder(w, d_canvas);            break;        }    }}//! drawContents// \sa QFrame::drawContentsvoid QwtPlot::drawContents( QPainter * ){    // We must erase the region that is not    // occupied by our children    QRegion cr( contentsRect() );    cr = cr.subtract( childrenRegion() );    erase( cr );}/*!   Redraw the canvas.  \param painter Painter used for drawing  \warning drawCanvas calls drawCanvasItems what is also used           for printing. Applications that like to add individual           plot items better overload QwtPlot::drawCanvasItems

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -