VisualizationGraphRenderingDelegate.cpp
349 lines
| 12.0 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r480 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | ||
Alexandre Leroux
|
r916 | #include "Visualization/AxisRenderingUtils.h" | ||
Alexandre Leroux
|
r1002 | #include "Visualization/ColorScaleEditor.h" | ||
Alexandre Leroux
|
r919 | #include "Visualization/PlottablesRenderingUtils.h" | ||
Alexandre Leroux
|
r1009 | #include "Visualization/SqpColorScale.h" | ||
Alexandre Leroux
|
r725 | #include "Visualization/VisualizationGraphWidget.h" | ||
Alexandre Leroux
|
r480 | #include "Visualization/qcustomplot.h" | ||
Alexandre Leroux
|
r490 | #include <Common/DateUtils.h> | ||
Alexandre Leroux
|
r726 | #include <SqpApplication.h> | ||
r1420 | namespace | |||
{ | ||||
Alexandre Leroux
|
r482 | |||
Alexandre Leroux
|
r729 | /// Name of the axes layer in QCustomPlot | ||
const auto AXES_LAYER = QStringLiteral("axes"); | ||||
/// Icon used to show x-axis properties | ||||
const auto HIDE_AXIS_ICON_PATH = QStringLiteral(":/icones/down.png"); | ||||
Alexandre Leroux
|
r726 | /// Name of the overlay layer in QCustomPlot | ||
const auto OVERLAY_LAYER = QStringLiteral("overlay"); | ||||
Alexandre Leroux
|
r729 | /// Pixmap used to show x-axis properties | ||
const auto SHOW_AXIS_ICON_PATH = QStringLiteral(":/icones/up.png"); | ||||
Alexandre Leroux
|
r1024 | /// Tooltip format for graphs | ||
const auto GRAPH_TOOLTIP_FORMAT = QStringLiteral("key: %1\nvalue: %2"); | ||||
Alexandre Leroux
|
r1027 | /// Tooltip format for colormaps | ||
const auto COLORMAP_TOOLTIP_FORMAT = QStringLiteral("x: %1\ny: %2\nvalue: %3"); | ||||
Alexandre Leroux
|
r483 | |||
Alexandre Leroux
|
r578 | /// Offset used to shift the tooltip of the mouse | ||
r1420 | const auto TOOLTIP_OFFSET = QPoint { 20, 20 }; | |||
Alexandre Leroux
|
r578 | |||
Alexandre Leroux
|
r576 | /// Tooltip display rectangle (the tooltip is hidden when the mouse leaves this rectangle) | ||
r1420 | const auto TOOLTIP_RECT = QRect { 10, 10, 10, 10 }; | |||
Alexandre Leroux
|
r576 | |||
/// Timeout after which the tooltip is displayed | ||||
const auto TOOLTIP_TIMEOUT = 500; | ||||
Alexandre Leroux
|
r482 | |||
r1420 | void initPointTracerStyle(QCPItemTracer& tracer) noexcept | |||
Alexandre Leroux
|
r485 | { | ||
tracer.setInterpolating(false); | ||||
Alexandre Leroux
|
r577 | tracer.setStyle(QCPItemTracer::tsCircle); | ||
tracer.setSize(3); | ||||
Alexandre Leroux
|
r485 | tracer.setPen(QPen(Qt::black)); | ||
tracer.setBrush(Qt::black); | ||||
r1044 | tracer.setSelectable(false); | |||
Alexandre Leroux
|
r485 | } | ||
r1420 | QPixmap pixmap(const QString& iconPath) noexcept | |||
Alexandre Leroux
|
r729 | { | ||
r1420 | return QIcon { iconPath }.pixmap(QSize { 16, 16 }); | |||
Alexandre Leroux
|
r729 | } | ||
r1420 | void initClosePixmapStyle(QCPItemPixmap& pixmap) noexcept | |||
Alexandre Leroux
|
r726 | { | ||
// Icon | ||||
pixmap.setPixmap( | ||||
r1420 | sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton).pixmap(QSize { 16, 16 })); | |||
Alexandre Leroux
|
r726 | |||
// Position | ||||
pixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); | ||||
pixmap.topLeft->setCoords(1, 0); | ||||
pixmap.setClipToAxisRect(false); | ||||
// Can be selected | ||||
pixmap.setSelectable(true); | ||||
} | ||||
r1420 | void initXAxisPixmapStyle(QCPItemPixmap& itemPixmap) noexcept | |||
Alexandre Leroux
|
r729 | { | ||
// Icon | ||||
itemPixmap.setPixmap(pixmap(HIDE_AXIS_ICON_PATH)); | ||||
// Position | ||||
itemPixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); | ||||
itemPixmap.topLeft->setCoords(0, 1); | ||||
itemPixmap.setClipToAxisRect(false); | ||||
// Can be selected | ||||
itemPixmap.setSelectable(true); | ||||
} | ||||
r1420 | void initTitleTextStyle(QCPItemText& text) noexcept | |||
Alexandre Leroux
|
r726 | { | ||
// Font and background styles | ||||
text.setColor(Qt::gray); | ||||
text.setBrush(Qt::white); | ||||
// Position | ||||
text.setPositionAlignment(Qt::AlignTop | Qt::AlignLeft); | ||||
text.position->setType(QCPItemPosition::ptAxisRectRatio); | ||||
text.position->setCoords(0.5, 0); | ||||
r1044 | text.setSelectable(false); | |||
Alexandre Leroux
|
r726 | } | ||
Alexandre Leroux
|
r1026 | /** | ||
* Returns the cell index (x or y) of a colormap according to the coordinate passed in parameter. | ||||
* This method handles the fact that a colormap axis can be logarithmic or linear. | ||||
* @param colormap the colormap for which to calculate the index | ||||
* @param coord the coord to convert to cell index | ||||
* @param xCoord calculates the x index if true, calculates y index if false | ||||
* @return the cell index | ||||
*/ | ||||
r1420 | int colorMapCellIndex(const QCPColorMap& colormap, double coord, bool xCoord) | |||
Alexandre Leroux
|
r1026 | { | ||
// Determines the axis of the colormap according to xCoord, and whether it is logarithmic or not | ||||
auto isLogarithmic = (xCoord ? colormap.keyAxis() : colormap.valueAxis())->scaleType() | ||||
r1420 | == QCPAxis::stLogarithmic; | |||
Alexandre Leroux
|
r1026 | |||
r1420 | if (isLogarithmic) | |||
{ | ||||
Alexandre Leroux
|
r1026 | // For a logarithmic axis we can't use the conversion method of colormap, so we calculate | ||
// the index manually based on the position of the coordinate on the axis | ||||
// Gets the axis range and the number of values between range bounds to calculate the step | ||||
// between each value of the range | ||||
auto range = xCoord ? colormap.data()->keyRange() : colormap.data()->valueRange(); | ||||
auto nbValues = (xCoord ? colormap.data()->keySize() : colormap.data()->valueSize()) - 1; | ||||
auto valueStep | ||||
= (std::log10(range.upper) - std::log10(range.lower)) / static_cast<double>(nbValues); | ||||
// According to the coord position, calculates the closest index in the range | ||||
return std::round((std::log10(coord) - std::log10(range.lower)) / valueStep); | ||||
} | ||||
r1420 | else | |||
{ | ||||
Alexandre Leroux
|
r1026 | // For a linear axis, we use the conversion method of colormap | ||
int index; | ||||
r1420 | if (xCoord) | |||
{ | ||||
Alexandre Leroux
|
r1026 | colormap.data()->coordToCell(coord, 0., &index, nullptr); | ||
} | ||||
r1420 | else | |||
{ | ||||
Alexandre Leroux
|
r1026 | colormap.data()->coordToCell(0., coord, nullptr, &index); | ||
} | ||||
return index; | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r482 | } // namespace | ||
r1420 | struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate | |||
{ | ||||
explicit VisualizationGraphRenderingDelegatePrivate(VisualizationGraphWidget& graphWidget) | ||||
: m_Plot { graphWidget.plot() } | ||||
, m_PointTracer { new QCPItemTracer { &m_Plot } } | ||||
, m_TracerTimer {} | ||||
, m_ClosePixmap { new QCPItemPixmap { &m_Plot } } | ||||
, m_TitleText { new QCPItemText { &m_Plot } } | ||||
, m_XAxisPixmap { new QCPItemPixmap { &m_Plot } } | ||||
, m_ShowXAxis { true } | ||||
, m_XAxisLabel {} | ||||
, m_ColorScale { SqpColorScale { m_Plot } } | ||||
Alexandre Leroux
|
r482 | { | ||
Alexandre Leroux
|
r485 | initPointTracerStyle(*m_PointTracer); | ||
Alexandre Leroux
|
r482 | |||
Alexandre Leroux
|
r576 | m_TracerTimer.setInterval(TOOLTIP_TIMEOUT); | ||
Alexandre Leroux
|
r482 | m_TracerTimer.setSingleShot(true); | ||
Alexandre Leroux
|
r726 | |||
// Inits "close button" in plot overlay | ||||
m_ClosePixmap->setLayer(OVERLAY_LAYER); | ||||
initClosePixmapStyle(*m_ClosePixmap); | ||||
Alexandre Leroux
|
r727 | |||
// Connects pixmap selection to graph widget closing | ||||
r1420 | QObject::connect( | |||
&m_Plot, &QCustomPlot::itemClick, [&graphWidget, this](auto item, auto mouseEvent) { | ||||
if (item == m_ClosePixmap) | ||||
{ | ||||
graphWidget.close(); | ||||
} | ||||
}); | ||||
Alexandre Leroux
|
r727 | |||
Alexandre Leroux
|
r726 | // Inits graph name in plot overlay | ||
m_TitleText->setLayer(OVERLAY_LAYER); | ||||
m_TitleText->setText(graphWidget.name()); | ||||
initTitleTextStyle(*m_TitleText); | ||||
Alexandre Leroux
|
r729 | |||
// Inits "show x-axis button" in plot overlay | ||||
m_XAxisPixmap->setLayer(OVERLAY_LAYER); | ||||
initXAxisPixmapStyle(*m_XAxisPixmap); | ||||
// Connects pixmap selection to graph x-axis showing/hiding | ||||
r1054 | QObject::connect(&m_Plot, &QCustomPlot::itemClick, [this](auto item, auto mouseEvent) { | |||
r1420 | if (m_XAxisPixmap == item) | |||
{ | ||||
Alexandre Leroux
|
r729 | // Changes the selection state and refreshes the x-axis | ||
m_ShowXAxis = !m_ShowXAxis; | ||||
r1054 | this->updateXAxisState(); | |||
Alexandre Leroux
|
r729 | m_Plot.layer(AXES_LAYER)->replot(); | ||
// Deselects the x-axis pixmap and updates icon | ||||
m_XAxisPixmap->setPixmap( | ||||
pixmap(m_ShowXAxis ? HIDE_AXIS_ICON_PATH : SHOW_AXIS_ICON_PATH)); | ||||
m_Plot.layer(OVERLAY_LAYER)->replot(); | ||||
} | ||||
}); | ||||
} | ||||
/// Updates state of x-axis according to the current selection of x-axis pixmap | ||||
/// @remarks the method doesn't call plot refresh | ||||
void updateXAxisState() noexcept | ||||
{ | ||||
m_Plot.xAxis->setTickLabels(m_ShowXAxis); | ||||
r1420 | m_Plot.xAxis->setLabel(m_ShowXAxis ? m_XAxisLabel : QString {}); | |||
Alexandre Leroux
|
r482 | } | ||
Alexandre Leroux
|
r480 | |||
r1420 | QCustomPlot& m_Plot; | |||
QCPItemTracer* m_PointTracer; | ||||
Alexandre Leroux
|
r482 | QTimer m_TracerTimer; | ||
r1420 | QCPItemPixmap* m_ClosePixmap; /// Graph's close button | |||
QCPItemText* m_TitleText; /// Graph's title | ||||
QCPItemPixmap* m_XAxisPixmap; | ||||
Alexandre Leroux
|
r729 | bool m_ShowXAxis; /// X-axis properties are shown or hidden | ||
QString m_XAxisLabel; | ||||
Alexandre Leroux
|
r1009 | SqpColorScale m_ColorScale; /// Color scale used for some types of graphs (as spectrograms) | ||
Alexandre Leroux
|
r480 | }; | ||
Alexandre Leroux
|
r725 | VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate( | ||
r1420 | VisualizationGraphWidget& graphWidget) | |||
: impl { spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(graphWidget) } | ||||
Alexandre Leroux
|
r480 | { | ||
} | ||||
Alexandre Leroux
|
r481 | |||
r1420 | void VisualizationGraphRenderingDelegate::onMouseDoubleClick(QMouseEvent* event) noexcept | |||
Alexandre Leroux
|
r1002 | { | ||
// Opens color scale editor if color scale is double clicked | ||||
r1420 | auto colorScale = static_cast<QCPColorScale*>(impl->m_Plot.layoutElementAt(event->pos())); | |||
if (impl->m_ColorScale.m_Scale == colorScale) | ||||
{ | ||||
if (ColorScaleEditor { impl->m_ColorScale }.exec() == QDialog::Accepted) | ||||
{ | ||||
Alexandre Leroux
|
r1013 | impl->m_Plot.replot(); | ||
} | ||||
Alexandre Leroux
|
r1002 | } | ||
} | ||||
r1420 | void VisualizationGraphRenderingDelegate::updateTooltip(QMouseEvent* event) noexcept | |||
Alexandre Leroux
|
r481 | { | ||
Alexandre Leroux
|
r482 | // Cancels pending refresh | ||
impl->m_TracerTimer.disconnect(); | ||||
Alexandre Leroux
|
r576 | // Reinits tracers | ||
impl->m_PointTracer->setGraph(nullptr); | ||||
impl->m_PointTracer->setVisible(false); | ||||
impl->m_Plot.replot(); | ||||
r1420 | QString tooltip {}; | |||
Alexandre Leroux
|
r1024 | |||
Alexandre Leroux
|
r576 | // Gets the graph under the mouse position | ||
auto eventPos = event->pos(); | ||||
r1420 | if (auto graph = qobject_cast<QCPGraph*>(impl->m_Plot.plottableAt(eventPos))) | |||
{ | ||||
Alexandre Leroux
|
r576 | auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x()); | ||
auto graphData = graph->data(); | ||||
// Gets the closest data point to the mouse | ||||
auto graphDataIt = graphData->findBegin(mouseKey); | ||||
r1420 | if (graphDataIt != graphData->constEnd()) | |||
{ | ||||
Alexandre Leroux
|
r1024 | // Sets tooltip | ||
Alexandre Leroux
|
r576 | auto key = formatValue(graphDataIt->key, *graph->keyAxis()); | ||
auto value = formatValue(graphDataIt->value, *graph->valueAxis()); | ||||
Alexandre Leroux
|
r1024 | tooltip = GRAPH_TOOLTIP_FORMAT.arg(key, value); | ||
Alexandre Leroux
|
r576 | |||
// 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
|
r484 | impl->m_Plot.replot(); | ||
Alexandre Leroux
|
r1024 | } | ||
} | ||||
r1420 | else if (auto colorMap = qobject_cast<QCPColorMap*>(impl->m_Plot.plottableAt(eventPos))) | |||
{ | ||||
Alexandre Leroux
|
r1025 | // Gets x and y coords | ||
auto x = colorMap->keyAxis()->pixelToCoord(eventPos.x()); | ||||
auto y = colorMap->valueAxis()->pixelToCoord(eventPos.y()); | ||||
Alexandre Leroux
|
r1026 | |||
// Calculates x and y cell indexes, and retrieves the underlying value | ||||
auto xCellIndex = colorMapCellIndex(*colorMap, x, true); | ||||
auto yCellIndex = colorMapCellIndex(*colorMap, y, false); | ||||
auto value = colorMap->data()->cell(xCellIndex, yCellIndex); | ||||
Alexandre Leroux
|
r1027 | // Sets tooltips | ||
tooltip = COLORMAP_TOOLTIP_FORMAT.arg(formatValue(x, *colorMap->keyAxis()), | ||||
r1420 | formatValue(y, *colorMap->valueAxis()), | |||
formatValue(value, *colorMap->colorScale()->axis())); | ||||
Alexandre Leroux
|
r1024 | } | ||
Alexandre Leroux
|
r482 | |||
r1420 | if (!tooltip.isEmpty()) | |||
{ | ||||
Alexandre Leroux
|
r1024 | // Starts timer to show tooltip after timeout | ||
auto showTooltip = [tooltip, eventPos, this]() { | ||||
QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip, | ||||
r1420 | &impl->m_Plot, TOOLTIP_RECT); | |||
Alexandre Leroux
|
r1024 | }; | ||
Alexandre Leroux
|
r576 | |||
Alexandre Leroux
|
r1024 | QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip); | ||
impl->m_TracerTimer.start(); | ||||
Alexandre Leroux
|
r576 | } | ||
Alexandre Leroux
|
r481 | } | ||
Alexandre Leroux
|
r728 | |||
Alexandre Leroux
|
r1020 | void VisualizationGraphRenderingDelegate::onPlotUpdated() noexcept | ||
{ | ||||
// Updates color scale bounds | ||||
impl->m_ColorScale.updateDataRange(); | ||||
impl->m_Plot.replot(); | ||||
} | ||||
r1420 | void VisualizationGraphRenderingDelegate::setAxesUnits(Variable2& variable) noexcept | |||
Alexandre Leroux
|
r729 | { | ||
Alexandre Leroux
|
r1283 | auto axisHelper = IAxisHelperFactory::create(variable); | ||
axisHelper->setUnits(impl->m_Plot, impl->m_ColorScale); | ||||
// Stores x-axis label to be able to retrieve it when x-axis pixmap is unselected | ||||
impl->m_XAxisLabel = impl->m_Plot.xAxis->label(); | ||||
Alexandre Leroux
|
r729 | |||
// Updates x-axis state | ||||
impl->updateXAxisState(); | ||||
impl->m_Plot.layer(AXES_LAYER)->replot(); | ||||
} | ||||
r1420 | void VisualizationGraphRenderingDelegate::setGraphProperties( | |||
Variable2& variable, PlottablesMap& plottables) noexcept | ||||
Alexandre Leroux
|
r919 | { | ||
Alexandre Leroux
|
r1283 | // Axes' properties | ||
auto axisHelper = IAxisHelperFactory::create(variable); | ||||
axisHelper->setProperties(impl->m_Plot, impl->m_ColorScale); | ||||
// Plottables' properties | ||||
auto plottablesHelper = IPlottablesHelperFactory::create(variable); | ||||
Alexandre Leroux
|
r919 | plottablesHelper->setProperties(plottables); | ||
} | ||||
Alexandre Leroux
|
r728 | void VisualizationGraphRenderingDelegate::showGraphOverlay(bool show) noexcept | ||
{ | ||||
auto overlay = impl->m_Plot.layer(OVERLAY_LAYER); | ||||
overlay->setVisible(show); | ||||
overlay->replot(); | ||||
} | ||||