#ifndef PLOTWIDGET_H #define PLOTWIDGET_H #include #include #include #include #include #include class PlotWidget : public QWidget { Q_OBJECT public: explicit PlotWidget(QWidget *parent = nullptr); void addDataPoint(const QVector &values); void setAllData(const QVector> &channels); void clear(); void setChannelVisible(int channel, bool visible); bool isChannelVisible(int channel) const; void setDisplayPointCount(int count); int displayPointCount() const; void setYRange(double min, double max); void autoRangeY(); void setTitle(const QString &title); int totalPoints() const; protected: void paintEvent(QPaintEvent *event) override; void wheelEvent(QWheelEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override; void resizeEvent(QResizeEvent *event) override; private: void drawBackground(QPainter &painter); void drawGrid(QPainter &painter); void drawCurves(QPainter &painter); void drawAxes(QPainter &painter); void drawLegend(QPainter &painter); QRectF plotArea() const; QPointF dataToWidget(double x, double y) const; static QList defaultColors(); // 数据 QVector> m_data; // 16个通道 QVector m_channelVisible; int m_totalPoints; // 视图参数 int m_displayPoints; double m_yMin, m_yMax; bool m_autoYRange; bool m_autoScroll; // 鼠标交互 bool m_dragging; QPoint m_dragStart; double m_dragYMinStart, m_dragYMaxStart; // 边距 int m_marginLeft; int m_marginRight; int m_marginTop; int m_marginBottom; // 颜色 QList m_colors; QColor m_bgColor; QColor m_gridColor; QColor m_gridColorMinor; QColor m_textColor; QColor m_axisColor; // 标题 QString m_title; // 刷新定时器 QTimer *m_refreshTimer; bool m_dirty; }; #endif // PLOTWIDGET_H