Cabana: align the charts properly (#26116)

old-commit-hash: 60586e0d5835dbb3928321700c9d4d99cdeec216
This commit is contained in:
Dean Lee
2022-10-18 05:39:18 +08:00
committed by GitHub
parent 3bffc8d9d9
commit e78b3ceee9
2 changed files with 20 additions and 1 deletions
+19 -1
View File
@@ -2,6 +2,7 @@
#include <QGraphicsLayout>
#include <QRubberBand>
#include <QTimer>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
@@ -183,7 +184,6 @@ ChartView::ChartView(const QString &id, const Signal *sig, QWidget *parent)
font.setBold(true);
chart->setTitleFont(font);
chart->setMargins({0, 0, 0, 0});
chart->layout()->setContentsMargins(0, 0, 0, 0);
track_line = new QGraphicsLineItem(chart);
track_line->setPen(QPen(Qt::gray, 1, Qt::DashLine));
@@ -202,14 +202,32 @@ ChartView::ChartView(const QString &id, const Signal *sig, QWidget *parent)
rubber->setPalette(pal);
}
QTimer *timer = new QTimer(this);
timer->setInterval(100);
timer->setSingleShot(true);
timer->callOnTimeout(this, &ChartView::adjustChartMargins);
QObject::connect(can, &CANMessages::updated, this, &ChartView::updateState);
QObject::connect(can, &CANMessages::rangeChanged, this, &ChartView::rangeChanged);
QObject::connect(can, &CANMessages::eventsMerged, this, &ChartView::updateSeries);
QObject::connect(dynamic_cast<QValueAxis *>(chart->axisX()), &QValueAxis::rangeChanged, can, &CANMessages::setRange);
QObject::connect(chart, &QChart::plotAreaChanged, [=](const QRectF &plotArea) {
// use a singleshot timer to avoid recursion call.
timer->start();
});
updateSeries();
}
void ChartView::adjustChartMargins() {
// TODO: Remove hardcoded aligned_pos
const int aligned_pos = 60;
if (chart()->plotArea().left() != aligned_pos) {
const float left_margin = chart()->margins().left() + aligned_pos - chart()->plotArea().left();
chart()->setMargins(QMargins(left_margin, 0, 0, 0));
}
}
void ChartWidget::setHeight(int height) {
chart_view->setFixedHeight(height);
}
+1
View File
@@ -27,6 +27,7 @@ private:
void mouseMoveEvent(QMouseEvent *ev) override;
void enterEvent(QEvent *event) override;
void leaveEvent(QEvent *event) override;
void adjustChartMargins();
void rangeChanged(qreal min, qreal max);
void updateAxisY();