From 3daa82e5a1c74cbdff1bca57057fa75d1a0b109c Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 7 Feb 2023 10:58:24 +0800 Subject: [PATCH] cabana: paint points in drawForeground instead of switching opengl mode (#27239) old-commit-hash: ae99a6d15d363ac01f970f266dd7bc7a13ae7366 --- tools/cabana/chartswidget.cc | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tools/cabana/chartswidget.cc b/tools/cabana/chartswidget.cc index d482b71cb3..a5abb8dc64 100644 --- a/tools/cabana/chartswidget.cc +++ b/tools/cabana/chartswidget.cc @@ -503,7 +503,7 @@ void ChartView::updateSeriesPoints() { // Show points when zoomed in enough for (auto &s : sigs) { auto begin = std::lower_bound(s.vals.begin(), s.vals.end(), axis_x->min(), [](auto &p, double x) { return p.x() < x; }); - auto end = std::lower_bound(s.vals.begin(), s.vals.end(), axis_x->max(), [](auto &p, double x) { return p.x() < x; }); + auto end = std::lower_bound(begin, s.vals.end(), axis_x->max(), [](auto &p, double x) { return p.x() < x; }); int num_points = std::max(end - begin, 1); int pixels_per_point = width() / num_points; @@ -512,21 +512,6 @@ void ChartView::updateSeriesPoints() { ((QScatterSeries *)s.series)->setMarkerSize(std::clamp(pixels_per_point / 3, 1, 8)); } else { s.series->setPointsVisible(pixels_per_point > 20); - - // TODO: On MacOS QChartWidget doesn't work with the OpenGL settings that CameraWidget needs. -#ifndef __APPLE - // OpenGL mode lacks certain features (such as showing points), only use when drawing many points - bool use_opengl = pixels_per_point < 1; - s.series->setUseOpenGL(use_opengl); - - // Qt doesn't properly apply device pixel ratio in OpenGL mode - QApplication *application = static_cast(QApplication::instance()); - float scale = use_opengl ? application->devicePixelRatio() : 1.0; - - QPen pen = s.series->pen(); - pen.setWidth(2.0 * scale); - s.series->setPen(pen); -#endif } } } @@ -592,7 +577,7 @@ void ChartView::updateAxisY() { if (!s.series->isVisible()) continue; auto first = std::lower_bound(s.vals.begin(), s.vals.end(), axis_x->min(), [](auto &p, double x) { return p.x() < x; }); - auto last = std::lower_bound(s.vals.begin(), s.vals.end(), axis_x->max(), [](auto &p, double x) { return p.x() < x; }); + auto last = std::lower_bound(first, s.vals.end(), axis_x->max(), [](auto &p, double x) { return p.x() < x; }); for (auto it = first; it != last; ++it) { if (it->y() < min) min = it->y(); if (it->y() > max) max = it->y(); @@ -783,6 +768,19 @@ void ChartView::drawForeground(QPainter *painter, const QRectF &rect) { } } } + + // paint points. OpenGL mode lacks certain features (such as showing points) + painter->setPen(Qt::NoPen); + for (auto &s : sigs) { + if (s.series->useOpenGL() && s.series->isVisible() && s.series->pointsVisible()) { + auto first = std::lower_bound(s.vals.begin(), s.vals.end(), axis_x->min(), [](auto &p, double x) { return p.x() < x; }); + auto last = std::lower_bound(first, s.vals.end(), axis_x->max(), [](auto &p, double x) { return p.x() < x; }); + for (auto it = first; it != last; ++it) { + painter->setBrush(s.series->color()); + painter->drawEllipse(chart()->mapToPosition(*it), 4, 4); + } + } + } } QXYSeries *ChartView::createSeries(QAbstractSeries::SeriesType type) {