##// END OF EJS Templates
modify dateTime method to range method
modify dateTime method to range method

File last commit:

r494:933865558c87
r535:624470ecacf4
Show More
VisualizationGraphRenderingDelegate.cpp
123 lines | 4.3 KiB | text/x-c | CppLexer
/ gui / src / Visualization / VisualizationGraphRenderingDelegate.cpp
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r480 #include "Visualization/VisualizationGraphRenderingDelegate.h"
#include "Visualization/qcustomplot.h"
Alexandre Leroux
Uses the same time spec (UTC) as axis for tooltips on graph
r490 #include <Common/DateUtils.h>
Alexandre Leroux
Inits tracers and timer...
r482 namespace {
Alexandre Leroux
Show tracers method (1)...
r483 const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz");
const auto TEXT_TRACER_FORMAT = QStringLiteral("key: %1\nvalue: %2");
Alexandre Leroux
Inits tracers and timer...
r482 /// Timeout after which a tracer is displayed
const auto TRACER_TIMEOUT = 500;
Alexandre Leroux
Show tracers method (1)...
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
Alexandre Leroux
Uses the same time spec (UTC) as axis for tooltips on graph
r490 if (auto axisTicker = qSharedPointerDynamicCast<QCPAxisTickerDateTime>(axis.ticker())) {
return DateUtils::dateTime(value, axisTicker->dateTimeSpec()).toString(DATETIME_FORMAT);
}
else {
return QString::number(value);
}
Alexandre Leroux
Show tracers method (1)...
r483 }
Alexandre Leroux
Sets tracers' styles
r485
void initPointTracerStyle(QCPItemTracer &tracer) noexcept
{
tracer.setInterpolating(false);
tracer.setStyle(QCPItemTracer::tsPlus);
tracer.setPen(QPen(Qt::black));
tracer.setBrush(Qt::black);
tracer.setSize(10);
}
void initTextTracerStyle(QCPItemText &tracer) noexcept
{
tracer.setPen(QPen{Qt::gray});
tracer.setBrush(Qt::white);
tracer.setPadding(QMargins{6, 6, 6, 6});
tracer.setPositionAlignment(Qt::AlignTop | Qt::AlignLeft);
tracer.setTextAlignment(Qt::AlignLeft);
}
Alexandre Leroux
Inits tracers and timer...
r482 } // namespace
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r480 struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate {
Alexandre Leroux
Inits tracers and timer...
r482 explicit VisualizationGraphRenderingDelegatePrivate(QCustomPlot &plot)
: m_Plot{plot},
m_PointTracer{new QCPItemTracer{&plot}},
m_TextTracer{new QCPItemText{&plot}},
m_TracerTimer{}
{
Alexandre Leroux
Sets tracers' styles
r485 initPointTracerStyle(*m_PointTracer);
initTextTracerStyle(*m_TextTracer);
Alexandre Leroux
Inits tracers and timer...
r482
m_TracerTimer.setInterval(TRACER_TIMEOUT);
m_TracerTimer.setSingleShot(true);
}
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r480
QCustomPlot &m_Plot;
Alexandre Leroux
Inits tracers and timer...
r482 QCPItemTracer *m_PointTracer;
QCPItemText *m_TextTracer;
QTimer m_TracerTimer;
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r480 };
VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate(QCustomPlot &plot)
: impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(plot)}
{
}
Alexandre Leroux
Creates method that will display a tooltip and a tracer with data point information after a while
r481
void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept
{
Alexandre Leroux
Inits tracers and timer...
r482 // Cancels pending refresh
impl->m_TracerTimer.disconnect();
auto showTracers = [ eventPos = event->pos(), this ]()
{
Alexandre Leroux
Show tracers method (2)...
r484 // Lambda function to display a tracer
auto displayTracer = [this](auto &tracer) {
// Tracer is set on top of the plot's main layer
tracer.setLayer(impl->m_Plot.layer("main"));
tracer.setVisible(true);
impl->m_Plot.replot();
};
Alexandre Leroux
Show tracers method (1)...
r483 // Reinits tracers
impl->m_PointTracer->setGraph(nullptr);
impl->m_PointTracer->setVisible(false);
impl->m_TextTracer->setVisible(false);
impl->m_Plot.replot();
// Gets the graph under the mouse position
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());
impl->m_TextTracer->setText(TEXT_TRACER_FORMAT.arg(key, value));
Alexandre Leroux
Show tracers method (2)...
r484
// Displays point tracer
impl->m_PointTracer->setGraph(graph);
Alexandre Leroux
Fixes tooltip imprecision
r494 impl->m_PointTracer->setGraphKey(graphDataIt->key);
Alexandre Leroux
Show tracers method (2)...
r484 displayTracer(*impl->m_PointTracer);
// Displays text tracer
auto tracerPosition = impl->m_TextTracer->position;
tracerPosition->setAxes(graph->keyAxis(), graph->valueAxis());
tracerPosition->setCoords(impl->m_PointTracer->position->key(),
impl->m_PointTracer->position->value());
displayTracer(*impl->m_TextTracer);
}
Alexandre Leroux
Show tracers method (1)...
r483 }
Alexandre Leroux
Inits tracers and timer...
r482 };
// Starts the timer to display tracers at timeout
QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTracers);
impl->m_TracerTimer.start();
Alexandre Leroux
Creates method that will display a tooltip and a tracer with data point information after a while
r481 }