mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-03 20:42:09 +08:00
cabana: align y axis correctly (#26837)
old-commit-hash: f15a5c9ad697e41eb273875577855ec5edede7f4
This commit is contained in:
@@ -156,6 +156,7 @@ void ChartsWidget::showChart(const QString &id, const Signal *sig, bool show, bo
|
||||
QObject::connect(chart, &ChartView::zoomReset, this, &ChartsWidget::zoomReset);
|
||||
QObject::connect(chart, &ChartView::seriesRemoved, this, &ChartsWidget::seriesChanged);
|
||||
QObject::connect(chart, &ChartView::seriesAdded, this, &ChartsWidget::seriesChanged);
|
||||
QObject::connect(chart, &ChartView::axisYUpdated, this, &ChartsWidget::alignCharts);
|
||||
charts_layout->insertWidget(0, chart);
|
||||
charts.push_back(chart);
|
||||
}
|
||||
@@ -171,6 +172,7 @@ void ChartsWidget::removeChart(ChartView *chart) {
|
||||
charts.removeOne(chart);
|
||||
chart->deleteLater();
|
||||
updateToolBar();
|
||||
alignCharts();
|
||||
emit seriesChanged();
|
||||
}
|
||||
|
||||
@@ -183,6 +185,16 @@ void ChartsWidget::removeAll() {
|
||||
emit seriesChanged();
|
||||
}
|
||||
|
||||
void ChartsWidget::alignCharts() {
|
||||
int plot_left = 0;
|
||||
for (auto c : charts) {
|
||||
plot_left = qMax((qreal)plot_left, c->getYAsixLabelWidth());
|
||||
}
|
||||
for (auto c : charts) {
|
||||
c->setPlotAreaLeftPosition(plot_left);
|
||||
}
|
||||
}
|
||||
|
||||
bool ChartsWidget::eventFilter(QObject *obj, QEvent *event) {
|
||||
if (obj != this && event->type() == QEvent::Close) {
|
||||
emit dock_btn->triggered();
|
||||
@@ -202,7 +214,6 @@ ChartView::ChartView(QWidget *parent) : QChartView(nullptr, parent) {
|
||||
chart->addAxis(axis_y, Qt::AlignLeft);
|
||||
chart->legend()->setShowToolTips(true);
|
||||
chart->layout()->setContentsMargins(0, 0, 0, 0);
|
||||
chart->setMargins(QMargins(20, 11, 11, 11));
|
||||
|
||||
QToolButton *remove_btn = new QToolButton();
|
||||
remove_btn->setText("X");
|
||||
@@ -234,6 +245,19 @@ ChartView::ChartView(QWidget *parent) : QChartView(nullptr, parent) {
|
||||
QObject::connect(manage_btn, &QToolButton::clicked, this, &ChartView::manageSeries);
|
||||
}
|
||||
|
||||
qreal ChartView::getYAsixLabelWidth() const {
|
||||
QFontMetrics fm(axis_y->labelsFont());
|
||||
int n = qMax(int(-qFloor(std::log10((axis_y->max() - axis_y->min()) / (axis_y->tickCount() - 1)))), 0) + 1;
|
||||
return qMax(fm.width(QString::number(axis_y->min(), 'f', n)), fm.width(QString::number(axis_y->max(), 'f', n))) + 20;
|
||||
}
|
||||
|
||||
void ChartView::setPlotAreaLeftPosition(int pos) {
|
||||
if (std::ceil(chart()->plotArea().left()) != pos) {
|
||||
const float left_margin = chart()->margins().left() + pos - chart()->plotArea().left();
|
||||
chart()->setMargins(QMargins(left_margin, 11, 11, 11));
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::addSeries(const QString &msg_id, const Signal *sig) {
|
||||
QLineSeries *series = new QLineSeries(this);
|
||||
series->setUseOpenGL(true);
|
||||
@@ -361,16 +385,6 @@ void ChartView::setDisplayRange(double min, double max) {
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::adjustChartMargins() {
|
||||
// TODO: Remove hardcoded aligned_pos
|
||||
const int aligned_pos = 60;
|
||||
if ((int)chart()->plotArea().left() != aligned_pos) {
|
||||
const float left_margin = chart()->margins().left() + aligned_pos - chart()->plotArea().left();
|
||||
chart()->setMargins(QMargins(left_margin, 11, 11, 11));
|
||||
scene()->invalidate({}, QGraphicsScene::ForegroundLayer);
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::updateSeries(const Signal *sig) {
|
||||
auto events = can->events();
|
||||
if (!events || sigs.isEmpty()) return;
|
||||
@@ -436,8 +450,7 @@ void ChartView::updateAxisY() {
|
||||
double range = max_y - min_y;
|
||||
applyNiceNumbers(min_y - range * 0.05, max_y + range * 0.05);
|
||||
}
|
||||
|
||||
adjustChartMargins();
|
||||
QTimer::singleShot(0, this, &ChartView::axisYUpdated);
|
||||
}
|
||||
|
||||
void ChartView::applyNiceNumbers(qreal min, qreal max) {
|
||||
|
||||
@@ -26,6 +26,8 @@ public:
|
||||
void updateSeries(const Signal *sig = nullptr);
|
||||
void setEventsRange(const std::pair<double, double> &range);
|
||||
void setDisplayRange(double min, double max);
|
||||
void setPlotAreaLeftPosition(int pos);
|
||||
qreal getYAsixLabelWidth() const;
|
||||
|
||||
struct SigItem {
|
||||
QString msg_id;
|
||||
@@ -44,6 +46,7 @@ signals:
|
||||
void zoomIn(double min, double max);
|
||||
void zoomReset();
|
||||
void remove();
|
||||
void axisYUpdated();
|
||||
|
||||
private slots:
|
||||
void msgRemoved(uint32_t address);
|
||||
@@ -58,7 +61,6 @@ private:
|
||||
void mouseMoveEvent(QMouseEvent *ev) override;
|
||||
void leaveEvent(QEvent *event) override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
void adjustChartMargins();
|
||||
void updateAxisY();
|
||||
void updateTitle();
|
||||
void updateFromSettings();
|
||||
@@ -89,6 +91,7 @@ signals:
|
||||
void seriesChanged();
|
||||
|
||||
private:
|
||||
void alignCharts();
|
||||
void removeChart(ChartView *chart);
|
||||
void eventsMerged();
|
||||
void updateState();
|
||||
|
||||
Reference in New Issue
Block a user