From bba4474600ef3a091396c4e30acfb38f28e2b2fa Mon Sep 17 00:00:00 2001 From: Andrei Popa Date: Wed, 8 May 2024 17:32:00 +0300 Subject: [PATCH] backup --- gui/include/gui/widgets/plotinfo2.h | 44 ++++++++++ gui/include/gui/widgets/plotinfowidgets.h | 71 ++++++++++++++++ gui/src/plotwidget.cpp | 12 +++ gui/src/widgets/plotinfo2.cpp | 73 +++++++++++++++++ gui/src/widgets/plotinfowidgets.cpp | 99 +++++++++++++++++++++++ 5 files changed, 299 insertions(+) create mode 100644 gui/include/gui/widgets/plotinfo2.h create mode 100644 gui/include/gui/widgets/plotinfowidgets.h create mode 100644 gui/src/widgets/plotinfo2.cpp create mode 100644 gui/src/widgets/plotinfowidgets.cpp diff --git a/gui/include/gui/widgets/plotinfo2.h b/gui/include/gui/widgets/plotinfo2.h new file mode 100644 index 0000000000..75a3ba4e77 --- /dev/null +++ b/gui/include/gui/widgets/plotinfo2.h @@ -0,0 +1,44 @@ +#ifndef PLOTINFO_H +#define PLOTINFO_H + +#include "hoverwidget.h" +#include +#include + +class QLabel; +namespace scopy { + +enum InfoPosition +{ + IP_LEFT, + IP_RIGHT +}; + +class SCOPY_GUI_EXPORT PlotInfo : public QWidget +{ + Q_OBJECT +public: + PlotInfo(QWidget *parent = nullptr); + virtual ~PlotInfo(); + + void addCustomInfo(QWidget *info, InfoPosition pos); + QLabel *addLabelInfo(InfoPosition pos); + +Q_SIGNALS: + +protected: + void initLayouts(); + +private: + QWidget *m_parent; + QWidget *m_leftInfo; + HoverWidget *m_leftHover; + QWidget *m_rightInfo; + HoverWidget *m_rightHover; + int m_margin; + int m_spacing; +}; + +} // namespace scopy + +#endif // PLOTINFO_H diff --git a/gui/include/gui/widgets/plotinfowidgets.h b/gui/include/gui/widgets/plotinfowidgets.h new file mode 100644 index 0000000000..4e3a931074 --- /dev/null +++ b/gui/include/gui/widgets/plotinfowidgets.h @@ -0,0 +1,71 @@ +#ifndef PLOTINFOWIDGETS_H +#define PLOTINFOWIDGETS_H + +#include +#include +#include +#include +#include + +namespace scopy { + +class SCOPY_GUI_EXPORT HDivInfo : public QLabel +{ + Q_OBJECT +public: + HDivInfo(PlotWidget *plot, QWidget *parent = nullptr); + virtual ~HDivInfo(); + +public Q_SLOTS: + void update(double val, bool zoomed = false); + +private: + MetricPrefixFormatter *m_mpf; + PlotWidget *m_plot; +}; + +class SCOPY_GUI_EXPORT TimeSamplingInfo : public QLabel +{ + Q_OBJECT +public: + TimeSamplingInfo(QWidget *parent = nullptr); + virtual ~TimeSamplingInfo(); + +public Q_SLOTS: + void update(PlotSamplingInfo info); + +private: + MetricPrefixFormatter *m_mpf; +}; + +class SCOPY_GUI_EXPORT FPSInfo : public QLabel +{ + Q_OBJECT +public: + FPSInfo(PlotWidget *plot, QWidget *parent = nullptr); + virtual ~FPSInfo(); + +public Q_SLOTS: + void update(qint64 timestamp); + +private: + PlotWidget *m_plot; + QList *m_replotTimes; + qint64 m_lastTimeStamp; + int m_avgSize; +}; + +class SCOPY_GUI_EXPORT TimestampInfo : public QLabel +{ + Q_OBJECT +public: + TimestampInfo(PlotWidget *plot, QWidget *parent = nullptr); + virtual ~TimestampInfo(); + +private: + PlotWidget *m_plot; +}; + +} // namespace scopy + +#endif // PLOTINFOWIDGETS_H diff --git a/gui/src/plotwidget.cpp b/gui/src/plotwidget.cpp index 76cefe2557..c3826dc98b 100644 --- a/gui/src/plotwidget.cpp +++ b/gui/src/plotwidget.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include @@ -78,6 +80,16 @@ PlotWidget::PlotWidget(QWidget *parent) setupNavigator(); m_plot->canvas()->installEventFilter(this); + + auto info = new PlotInfo(m_plot->canvas()); + + info->addCustomInfo(new FPSInfo(this), InfoPosition::IP_LEFT); + info->addCustomInfo(new FPSInfo(this), InfoPosition::IP_RIGHT); + info->addCustomInfo(new FPSInfo(this), InfoPosition::IP_LEFT); + info->addCustomInfo(new TimestampInfo(this), InfoPosition::IP_RIGHT); + info->addCustomInfo(new TimestampInfo(this), InfoPosition::IP_RIGHT); + info->addCustomInfo(new HDivInfo(this), InfoPosition::IP_LEFT); + info->addCustomInfo(new TimeSamplingInfo(this), InfoPosition::IP_RIGHT); } void PlotWidget::setupNavigator() diff --git a/gui/src/widgets/plotinfo2.cpp b/gui/src/widgets/plotinfo2.cpp new file mode 100644 index 0000000000..1bf54c1b36 --- /dev/null +++ b/gui/src/widgets/plotinfo2.cpp @@ -0,0 +1,73 @@ +#include "plotinfo2.h" + +#include +#include + +using namespace scopy; + +PlotInfo::PlotInfo(QWidget *parent) + : QWidget(parent) + , m_parent(parent) + , m_margin(6) + , m_spacing(6) + , m_leftInfo(new QWidget()) + , m_rightInfo(new QWidget()) +{ + initLayouts(); +} + +PlotInfo::~PlotInfo() {} + +void PlotInfo::addCustomInfo(QWidget *info, InfoPosition pos) +{ + switch(pos) { + case InfoPosition::IP_LEFT: + m_leftInfo->layout()->addWidget(info); + info->setParent(m_leftInfo); + break; + + case InfoPosition::IP_RIGHT: + m_rightInfo->layout()->addWidget(info); + info->setParent(m_rightInfo); + break; + } + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); +} + +QLabel *PlotInfo::addLabelInfo(InfoPosition pos) +{ + QLabel *label = new QLabel(); + StyleHelper::TimePlotSamplingInfo(label); + addCustomInfo(label, pos); + + return label; +} + +void PlotInfo::initLayouts() +{ + // left info + m_leftInfo->setAttribute(Qt::WA_TransparentForMouseEvents); + QVBoxLayout *leftLayout = new QVBoxLayout(m_leftInfo); + leftLayout->setSpacing(m_spacing); + leftLayout->setMargin(m_margin); + leftLayout->setAlignment(Qt::AlignLeft); + + m_leftHover = new HoverWidget(m_leftInfo, m_parent, m_parent); + m_leftHover->setAnchorPos(HoverPosition::HP_TOPLEFT); + m_leftHover->setContentPos(HoverPosition::HP_BOTTOMRIGHT); + m_leftHover->setAttribute(Qt::WA_TransparentForMouseEvents); + m_leftHover->show(); + + // right info + m_rightInfo->setAttribute(Qt::WA_TransparentForMouseEvents); + QVBoxLayout *rightLayout = new QVBoxLayout(m_rightInfo); + rightLayout->setSpacing(m_spacing); + rightLayout->setMargin(m_margin); + rightLayout->setAlignment(Qt::AlignRight); + + m_rightHover = new HoverWidget(m_rightInfo, m_parent, m_parent); + m_rightHover->setAnchorPos(HoverPosition::HP_TOPRIGHT); + m_rightHover->setContentPos(HoverPosition::HP_BOTTOMLEFT); + m_rightHover->setAttribute(Qt::WA_TransparentForMouseEvents); + m_rightHover->show(); +} diff --git a/gui/src/widgets/plotinfowidgets.cpp b/gui/src/widgets/plotinfowidgets.cpp new file mode 100644 index 0000000000..eb4bb8670e --- /dev/null +++ b/gui/src/widgets/plotinfowidgets.cpp @@ -0,0 +1,99 @@ +#include "plotinfowidgets.h" +#include "plotaxis.h" +#include "plotnavigator.hpp" +#include "plotwidget.h" +#include +#include + +using namespace scopy; + +HDivInfo::HDivInfo(PlotWidget *plot, QWidget *parent) + : m_mpf(new MetricPrefixFormatter(this)) + , m_plot(plot) +{ + StyleHelper::TimePlotHDivInfo(this); + m_mpf->setTrimZeroes(true); + connect(m_plot->navigator(), &PlotNavigator::rectChanged, [=]() { + PlotAxis *xAxis = m_plot->xAxis(); + double currMin, currMax, axisMax, axisMin, divs; + bool zoomed; + + axisMax = xAxis->max(); + axisMin = xAxis->min(); + currMax = xAxis->visibleMax(); + currMin = xAxis->visibleMin(); + zoomed = axisMax != currMax || axisMin != currMin; + divs = xAxis->divs(); + update(abs(currMax - currMin) / divs, zoomed); + }); +} + +HDivInfo::~HDivInfo() {} + +void HDivInfo::update(double val, bool zoomed) +{ + setText(m_mpf->format(val, "s", 2) + "/div" + (zoomed ? " (zoomed)" : "")); +} + +TimeSamplingInfo::TimeSamplingInfo(QWidget *parent) + : m_mpf(new MetricPrefixFormatter(this)) +{ + StyleHelper::TimePlotSamplingInfo(this); + m_mpf->setTrimZeroes(true); +} + +TimeSamplingInfo::~TimeSamplingInfo() {} + +void TimeSamplingInfo::update(PlotSamplingInfo info) +{ + QString text; + text = QString("%1").arg(m_mpf->format(info.plotSize, "samples", 2)); + //.arg(m_mpf->format(binfo.bufferSizes, "samples", 2)); + // if(info.sampleRate != 1.0) + text += QString(" at %2").arg(m_mpf->format(info.sampleRate, "sps", 2)); + + setText(text); +} + +FPSInfo::FPSInfo(PlotWidget *plot, QWidget *parent) + : m_plot(plot) + , m_replotTimes(new QList()) + , m_lastTimeStamp(0) + , m_avgSize(10) +{ + StyleHelper::TimePlotSamplingInfo(this); + connect(m_plot, &PlotWidget::newData, this, [=]() { update(QDateTime::currentMSecsSinceEpoch()); }); +} + +FPSInfo::~FPSInfo() {} + +void FPSInfo::update(qint64 timestamp) +{ + if(m_lastTimeStamp == 0) { + m_lastTimeStamp = timestamp; + return; + } + + m_replotTimes->append(timestamp - m_lastTimeStamp); + if(m_replotTimes->size() > m_avgSize) { + m_replotTimes->removeAt(0); + } + + qint64 avg = 0; + for(qint64 time : *m_replotTimes) { + avg += time; + } + avg /= m_replotTimes->size(); + m_lastTimeStamp = timestamp; + + setText(QString(QString::number(1000. / avg, 'g', 3) + " FPS")); +} + +TimestampInfo::TimestampInfo(PlotWidget *plot, QWidget *parent) +{ + StyleHelper::TimePlotSamplingInfo(this); + connect(plot, &PlotWidget::newData, this, + [=]() { setText(QDateTime::currentDateTime().time().toString("hh:mm:ss.zzz")); }); +} + +TimestampInfo::~TimestampInfo() {}