##// END OF EJS Templates
Merge branch 'feature/ImprovePerf' into develop
Merge branch 'feature/ImprovePerf' into develop

File last commit:

r543:4cbf5bf3dd44
r615:4575ac2a0a6a merge
Show More
VisualizationGraphRenderingDelegate.cpp
105 lines | 3.6 KiB | text/x-c | CppLexer
/ gui / src / Visualization / VisualizationGraphRenderingDelegate.cpp
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r442 #include "Visualization/VisualizationGraphRenderingDelegate.h"
#include "Visualization/qcustomplot.h"
Alexandre Leroux
Uses the same time spec (UTC) as axis for tooltips on graph
r452 #include <Common/DateUtils.h>
Alexandre Leroux
Inits tracers and timer...
r444 namespace {
Alexandre Leroux
Show tracers method (1)...
r445 const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz");
Alexandre Leroux
Uses Qt tooltips instead of QCPTextItem...
r541 const auto TOOLTIP_FORMAT = QStringLiteral("key: %1\nvalue: %2");
Alexandre Leroux
Show tracers method (1)...
r445
Alexandre Leroux
Shifts tooltip to mouse
r543 /// Offset used to shift the tooltip of the mouse
const auto TOOLTIP_OFFSET = QPoint{20, 20};
Alexandre Leroux
Uses Qt tooltips instead of QCPTextItem...
r541 /// 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;
Alexandre Leroux
Inits tracers and timer...
r444
Alexandre Leroux
Show tracers method (1)...
r445 /// 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
r452 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)...
r445 }
Alexandre Leroux
Sets tracers' styles
r447
void initPointTracerStyle(QCPItemTracer &tracer) noexcept
{
tracer.setInterpolating(false);
Alexandre Leroux
Replaces "Plus" cursor by "Circle" cursor
r542 tracer.setStyle(QCPItemTracer::tsCircle);
tracer.setSize(3);
Alexandre Leroux
Sets tracers' styles
r447 tracer.setPen(QPen(Qt::black));
tracer.setBrush(Qt::black);
}
Alexandre Leroux
Inits tracers and timer...
r444 } // namespace
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r442 struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate {
Alexandre Leroux
Inits tracers and timer...
r444 explicit VisualizationGraphRenderingDelegatePrivate(QCustomPlot &plot)
Alexandre Leroux
Uses Qt tooltips instead of QCPTextItem...
r541 : m_Plot{plot}, m_PointTracer{new QCPItemTracer{&plot}}, m_TracerTimer{}
Alexandre Leroux
Inits tracers and timer...
r444 {
Alexandre Leroux
Sets tracers' styles
r447 initPointTracerStyle(*m_PointTracer);
Alexandre Leroux
Inits tracers and timer...
r444
Alexandre Leroux
Uses Qt tooltips instead of QCPTextItem...
r541 m_TracerTimer.setInterval(TOOLTIP_TIMEOUT);
Alexandre Leroux
Inits tracers and timer...
r444 m_TracerTimer.setSingleShot(true);
}
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r442
QCustomPlot &m_Plot;
Alexandre Leroux
Inits tracers and timer...
r444 QCPItemTracer *m_PointTracer;
QTimer m_TracerTimer;
Alexandre Leroux
Creates a delegate offering methods for rendering a graph
r442 };
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
r443
void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept
{
Alexandre Leroux
Inits tracers and timer...
r444 // Cancels pending refresh
impl->m_TracerTimer.disconnect();
Alexandre Leroux
Uses Qt tooltips instead of QCPTextItem...
r541 // 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);
Alexandre Leroux
Show tracers method (2)...
r446 impl->m_Plot.replot();
Alexandre Leroux
Inits tracers and timer...
r444
Alexandre Leroux
Uses Qt tooltips instead of QCPTextItem...
r541 // Starts timer to show tooltip after timeout
auto showTooltip = [ tooltip = TOOLTIP_FORMAT.arg(key, value), eventPos, this ]()
{
Alexandre Leroux
Shifts tooltip to mouse
r543 QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip,
&impl->m_Plot, TOOLTIP_RECT);
Alexandre Leroux
Uses Qt tooltips instead of QCPTextItem...
r541 };
QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip);
impl->m_TracerTimer.start();
}
}
Alexandre Leroux
Creates method that will display a tooltip and a tracer with data point information after a while
r443 }