📄 qwt_curve.cpp
字号:
/* -*- 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 *****************************************************************************/#include <qpainter.h>#include "qwt_global.h"#include "qwt_curve.h"#include "qwt_data.h"#include "qwt_dimap.h"#include "qwt_double_rect.h"#include "qwt_math.h"#include "qwt_painter.h"/*! \brief Initialize data members*/void QwtCurve::init(const QString &title){ d_pen = QPen(Qt::black, 1); d_ref = 0.0; d_splineSize = 250; d_options = Auto; d_title = title; d_style = Lines; d_data = new QwtDoublePointData(QwtArray<QwtDoublePoint>());}/*! \brief Copy the contents of a curve into another curve*/void QwtCurve::copy(const QwtCurve &c){ d_ref = c.d_ref; d_sym = c.d_sym; d_pen = c.d_pen; d_title = c.d_title; d_style = c.d_style; d_splineSize = c.d_splineSize; d_options = c.d_options; delete d_data; d_data = c.d_data->copy();}//! DtorQwtCurve::~QwtCurve(){ delete d_data;}/*! \brief Copy Constructor*/QwtCurve::QwtCurve(const QwtCurve &c){ init(c.d_title); copy(c);}/*! \brief Copy Assignment*/const QwtCurve& QwtCurve::operator=(const QwtCurve &c){ if (this != &c) { copy(c); curveChanged(); } return *this;}/*! \brief Set the curve's drawing style Valid styles are: <dl> <dt>QwtCurve::NoCurve</dt> <dd>Don't draw a curve. Note: This doesn't affect the symbol. </dd> <dt>QwtCurve::Lines</dt> <dd>Connect the points with straight lines.</dd> <dt>QwtCurve::Sticks</dt> <dd>Draw vertical sticks from a baseline which is defined by setBaseline().</dd> <dt>QwtCurve::Steps</dt> <dd>Connect the points with a step function. The step function is drawn from the left to the right or vice versa, depending on the 'Inverted' option.</dd> <dt>QwtCurves::Dots</dt> <dd>Draw dots at the locations of the data points. Note: This is different from a dotted line (see setPen()).</dd> <dt>QwtCurve::Spline</dt> <dd>Interpolate the points with a spline. The spline type can be specified with setOptions(), the size of the spline (= number of interpolated points) can be specified with setSplineSize().</dd> <dt>QwtCurve::UserCurve ...</dt> <dd>Styles >= QwtCurve::UserCurve are reserved for derived classes of QwtCurve that overload QwtCurve::draw() with additional application specific curve types.</dd> </dl> \sa QwtCurve::style()*/void QwtCurve::setStyle(int style, int options){ d_options = options; d_style = style; curveChanged();}/*! \fn CurveStyle QwtCurve::style() const \brief Return the current style \sa QwtCurve::setStyle*/int QwtCurve::style() const { return d_style; }/*! \brief Assign a symbol \param s symbol \sa QwtSymbol*/void QwtCurve::setSymbol(const QwtSymbol &s ){ d_sym = s; curveChanged();}/*! \brief Return the current symbol \sa QwtCurve::setSymbol*/const QwtSymbol &QwtCurve::symbol() const { return d_sym; }/*! \brief Assign a pen \param p New pen*/void QwtCurve::setPen(const QPen &p){ if ( p != d_pen ) { d_pen = p; curveChanged(); }}/*! \brief Return the pen used to draw the lines \sa QwtCurve::setPen*/const QPen& QwtCurve::pen() const { return d_pen; }/*! \brief Assign a brush. In case of brush.style() != QBrush::NoBrush and style() != QwtCurve::Sticks the area between the curve and the baseline will be filled. In case !brush.color().isValid() the area will be filled by pen.color(). The fill algorithm simply connects the first and the last curve point to the baseline. So the curve data has to be sorted (ascending or descending). \param brush New brush \sa QwtCurve::brush, QwtCurve::setBaseline, QwtCurve::baseline*/void QwtCurve::setBrush(const QBrush &brush){ if ( brush != d_brush ) { d_brush = brush; curveChanged(); }}/*! \brief Return the brush used to fill the area between lines and the baseline \sa QwtCurve::setBrush, QwtCurve::setBaseline, QwtCurve::baseline*/const QBrush& QwtCurve::brush() const { return d_brush;}/*! \brief Set data by copying x- and y-values from specified memory blocks Contrary to \b QwtPlot::setCurveRawData, this function makes a 'deep copy' of the data. \param xData pointer to x values \param yData pointer to y values \param size size of xData and yData \sa QwData::setData.*/void QwtCurve::setData(const double *xData, const double *yData, int size){ delete d_data; d_data = new QwtArrayData(xData, yData, size); curveChanged();}/*! \brief Initialize data with x- and y-arrays (explicitly shared) \param xData x data \param yData y data \sa QwtData::setData.*/void QwtCurve::setData(const QwtArray<double> &xData, const QwtArray<double> &yData){ delete d_data; d_data = new QwtArrayData(xData, yData); curveChanged();}/*! Initialize data with an array of points (explicitly shared). \param data Data \sa QwtDoublePointData::setData.*/void QwtCurve::setData(const QwtArray<QwtDoublePoint> &data){ delete d_data; d_data = new QwtDoublePointData(data); curveChanged();}/*! Initialize data with a pointer to QwtData. \param data Data \sa QwtData::copy.*/void QwtCurve::setData(const QwtData &data){ delete d_data; d_data = data.copy(); curveChanged();}/*! \brief Initialize the data by pointing to memory blocks which are not managed by QwtCurve. setRawData is provided for efficiency. It is important to keep the pointers during the lifetime of the underlying QwtCPointerData class. \param xData pointer to x data \param yData pointer to y data \param size size of x and y \sa QwtCPointerData::setData.*/void QwtCurve::setRawData(const double *xData, const double *yData, int size){ delete d_data; d_data = new QwtCPointerData(xData, yData, size); curveChanged();}/*! \brief Assign a title to a curve \param title new title*/void QwtCurve::setTitle(const QString &title){ d_title = title; curveChanged();}/*! \brief Return the title. \sa QwtCurve::setTitle*/const QString &QwtCurve::title() const { return d_title; }/*! Returns the bounding rectangle of the curve data. If there is no bounding rect, like for empty data the rectangle is invalid: QwtDoubleRect.isValid() == FALSE*/QwtDoubleRect QwtCurve::boundingRect() const{ if ( d_data == NULL ) return QwtDoubleRect(1.0, -1.0, 1.0, -1.0); // invalid return d_data->boundingRect();}/*! \brief Ctor \param title title of the curve */QwtCurve::QwtCurve(const QString &title){ init(title);}/*! \brief Checks if a range of indices is valid and corrects it if necessary \param i1 Index 1 \param i2 Index 2*/int QwtCurve::verifyRange(int &i1, int &i2){ int size = dataSize(); if (size < 1) return 0; i1 = qwtLim(i1, 0, size-1); i2 = qwtLim(i2, 0, size-1); qwtSort(i1, i2, i1, i2); return (i2 - i1 + 1);}/*! \brief Draw an intervall of the curve \param painter Painter \param xMap maps x-values into pixel coordinates. \param yMap maps y-values into pixel coordinates. \param from index of the first point to be painted \param to index of the last point to be painted. If to < 0 the curve will be painted to its last point. \sa QwtCurve::drawCurve, QwtCurve::drawDots, QwtCurve::drawLines, QwtCurve::drawSpline, QwtCurve::drawSteps, QwtCurve::drawSticks*/void QwtCurve::draw(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to){ if ( !painter || dataSize() <= 0 ) return; if (to < 0) to = dataSize() - 1; if ( verifyRange(from, to) > 0 ) { painter->save(); painter->setPen(d_pen); QBrush b = d_brush; if ( b.style() != Qt::NoBrush && !b.color().isValid() ) b.setColor(d_pen.color()); painter->setBrush(b); drawCurve(painter, d_style, xMap, yMap, from, to); painter->restore(); if (d_sym.style() != QwtSymbol::None) { painter->save(); drawSymbols(painter, d_sym, xMap, yMap, from, to); painter->restore(); } }}/*! \brief Draw the line part (without symbols) of a curve interval. \param painter Painter \param style curve style, see QwtCurve::CurveStyle \param xMap x map \param yMap y map \param from index of the first point to be painted \param to index of the last point to be painted \sa QwtCurve::draw, QwtCurve::drawDots, QwtCurve::drawLines, QwtCurve::drawSpline, QwtCurve::drawSteps, QwtCurve::drawSticks*/void QwtCurve::drawCurve(QPainter *painter, int style, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to){ switch (style) { case NoCurve: break; case Lines: drawLines(painter, xMap, yMap, from, to); break; case Sticks: drawSticks(painter, xMap, yMap, from, to); break; case Steps: drawSteps(painter, xMap, yMap, from, to); break; case Spline: if ( from > 0 || to < dataSize() - 1 ) drawLines(painter, xMap, yMap, from, to); else drawSpline(painter, xMap, yMap); break; case Dots: drawDots(painter, xMap, yMap, from, to); break; default: break; }}/*! \brief Draw lines \param painter Painter \param xMap x map \param yMap y map \param from index of the first point to be painted \param to index of the last point to be painted \sa QwtCurve::draw, QwtCurve::drawLines, QwtCurve::drawDots, QwtCurve::drawSpline, QwtCurve::drawSteps, QwtCurve::drawSticks*/void QwtCurve::drawLines(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, int from, int to){ QPointArray polyline(to - from + 1); for (int i = from; i <= to; i++) { int xi = xMap.transform(x(i)); int yi = yMap.transform(y(i)); polyline.setPoint(i - from, xi, yi); } QwtPainter::drawPolyline(painter, polyline); if ( painter->brush().style() != Qt::NoBrush ) { closePolyline(xMap, yMap, polyline); painter->setPen(QPen(QPen::NoPen)); QwtPainter::drawPolygon(painter, polyline); }}/*! \brief Draw sticks \param painter Painter
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -