VisualizationGraphRenderingDelegate.cpp
102 lines
| 3.5 KiB
| text/x-c
|
CppLexer
|
r480 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | ||
#include "Visualization/qcustomplot.h" | ||||
|
r490 | #include <Common/DateUtils.h> | ||
|
r482 | namespace { | ||
|
r483 | const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz"); | ||
|
r576 | const auto TOOLTIP_FORMAT = QStringLiteral("key: %1\nvalue: %2"); | ||
|
r483 | |||
|
r576 | /// Tooltip display rectangle (the tooltip is hidden when the mouse leaves this rectangle) | ||
const auto TOOLTIP_RECT = QRect{10, 10, 10, 10}; | ||||
/// Timeout after which the tooltip is displayed | ||||
const auto TOOLTIP_TIMEOUT = 500; | ||||
|
r482 | |||
|
r483 | /// Formats a data value according to the axis on which it is present | ||
QString formatValue(double value, const QCPAxis &axis) | ||||
{ | ||||
// If the axis is a time axis, formats the value as a date | ||||
|
r490 | if (auto axisTicker = qSharedPointerDynamicCast<QCPAxisTickerDateTime>(axis.ticker())) { | ||
return DateUtils::dateTime(value, axisTicker->dateTimeSpec()).toString(DATETIME_FORMAT); | ||||
} | ||||
else { | ||||
return QString::number(value); | ||||
} | ||||
|
r483 | } | ||
|
r485 | |||
void initPointTracerStyle(QCPItemTracer &tracer) noexcept | ||||
{ | ||||
tracer.setInterpolating(false); | ||||
|
r577 | tracer.setStyle(QCPItemTracer::tsCircle); | ||
tracer.setSize(3); | ||||
|
r485 | tracer.setPen(QPen(Qt::black)); | ||
tracer.setBrush(Qt::black); | ||||
} | ||||
|
r482 | } // namespace | ||
|
r480 | struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate { | ||
|
r482 | explicit VisualizationGraphRenderingDelegatePrivate(QCustomPlot &plot) | ||
|
r576 | : m_Plot{plot}, m_PointTracer{new QCPItemTracer{&plot}}, m_TracerTimer{} | ||
|
r482 | { | ||
|
r485 | initPointTracerStyle(*m_PointTracer); | ||
|
r482 | |||
|
r576 | m_TracerTimer.setInterval(TOOLTIP_TIMEOUT); | ||
|
r482 | m_TracerTimer.setSingleShot(true); | ||
} | ||||
|
r480 | |||
QCustomPlot &m_Plot; | ||||
|
r482 | QCPItemTracer *m_PointTracer; | ||
QTimer m_TracerTimer; | ||||
|
r480 | }; | ||
VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate(QCustomPlot &plot) | ||||
: impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(plot)} | ||||
{ | ||||
} | ||||
|
r481 | |||
void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept | ||||
{ | ||||
|
r482 | // Cancels pending refresh | ||
impl->m_TracerTimer.disconnect(); | ||||
|
r576 | // Reinits tracers | ||
impl->m_PointTracer->setGraph(nullptr); | ||||
impl->m_PointTracer->setVisible(false); | ||||
impl->m_Plot.replot(); | ||||
// Gets the graph under the mouse position | ||||
auto eventPos = event->pos(); | ||||
if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) { | ||||
auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x()); | ||||
auto graphData = graph->data(); | ||||
// Gets the closest data point to the mouse | ||||
auto graphDataIt = graphData->findBegin(mouseKey); | ||||
if (graphDataIt != graphData->constEnd()) { | ||||
auto key = formatValue(graphDataIt->key, *graph->keyAxis()); | ||||
auto value = formatValue(graphDataIt->value, *graph->valueAxis()); | ||||
// Displays point tracer | ||||
impl->m_PointTracer->setGraph(graph); | ||||
impl->m_PointTracer->setGraphKey(graphDataIt->key); | ||||
impl->m_PointTracer->setLayer( | ||||
impl->m_Plot.layer("main")); // Tracer is set on top of the plot's main layer | ||||
impl->m_PointTracer->setVisible(true); | ||||
|
r484 | impl->m_Plot.replot(); | ||
|
r482 | |||
|
r576 | // Starts timer to show tooltip after timeout | ||
auto showTooltip = [ tooltip = TOOLTIP_FORMAT.arg(key, value), eventPos, this ]() | ||||
{ | ||||
QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos), tooltip, &impl->m_Plot, | ||||
TOOLTIP_RECT); | ||||
}; | ||||
QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip); | ||||
impl->m_TracerTimer.start(); | ||||
} | ||||
} | ||||
|
r481 | } | ||