From 72f6a3b56dc9cefa1bee0716f300f96d873f2016 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Fri, 28 Oct 2022 05:48:40 +0800 Subject: [PATCH] Cabana: change the chart theme in setting (#26220) * change the chart theme in setting * change title colors * cleanup old-commit-hash: ba570b963f18d96ac181882b1f4e1678b992f665 --- tools/cabana/chartswidget.cc | 27 ++++++++++++++++++++------- tools/cabana/chartswidget.h | 5 ++++- tools/cabana/settings.cc | 8 ++++++++ tools/cabana/settings.h | 3 +++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/tools/cabana/chartswidget.cc b/tools/cabana/chartswidget.cc index c7846de069..3f1dd890c3 100644 --- a/tools/cabana/chartswidget.cc +++ b/tools/cabana/chartswidget.cc @@ -200,33 +200,31 @@ ChartWidget::ChartWidget(const QString &id, const Signal *sig, QWidget *parent) main_layout->setSpacing(0); main_layout->setContentsMargins(0, 0, 0, 0); - QWidget *header = new QWidget(this); - header->setStyleSheet("background-color:white"); + header = new QWidget(this); QGridLayout *header_layout = new QGridLayout(header); header_layout->setContentsMargins(11, 11, 11, 0); msg_name_label = new QLabel(this); msg_name_label->setTextFormat(Qt::RichText); header_layout->addWidget(msg_name_label, 0, 0, Qt::AlignLeft); sig_name_label = new QLabel(this); - sig_name_label->setStyleSheet("font-weight:bold"); header_layout->addWidget(sig_name_label, 0, 1, Qt::AlignCenter); //, 0, Qt::AlignCenter); - QPushButton *remove_btn = new QPushButton("✖", this); + remove_btn = new QPushButton("✖", this); remove_btn->setFixedSize(20, 20); remove_btn->setToolTip(tr("Remove chart")); header_layout->addWidget(remove_btn, 0, 2, Qt::AlignRight); main_layout->addWidget(header); chart_view = new ChartView(id, sig, this); - chart_view->setFixedHeight(settings.chart_height); main_layout->addWidget(chart_view); main_layout->addStretch(); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); updateTitle(); + updateFromSettings(); QObject::connect(remove_btn, &QPushButton::clicked, [=]() { emit remove(id, sig); }); - QObject::connect(&settings, &Settings::changed, [this]() { chart_view->setFixedHeight(settings.chart_height); }); + QObject::connect(&settings, &Settings::changed, this, &ChartWidget::updateFromSettings); } void ChartWidget::updateTitle() { @@ -234,12 +232,22 @@ void ChartWidget::updateTitle() { sig_name_label->setText(signal->name.c_str()); } +void ChartWidget::updateFromSettings() { + header->setStyleSheet(settings.chart_theme == 0 ? "background-color:white" : "background-color:#23242c"); + QString color_style = settings.chart_theme == 0 ? "color:black" : "color:white"; + sig_name_label->setStyleSheet("font-weight:bold;" + color_style); + msg_name_label->setStyleSheet(color_style); + remove_btn->setStyleSheet(color_style); + chart_view->updateFromSettings(); +} + // ChartView ChartView::ChartView(const QString &id, const Signal *sig, QWidget *parent) : id(id), signal(sig), QChartView(nullptr, parent) { QLineSeries *series = new QLineSeries(); QChart *chart = new QChart(); + chart->setBackgroundRoundness(0); chart->addSeries(series); chart->createDefaultAxes(); chart->legend()->hide(); @@ -254,7 +262,6 @@ ChartView::ChartView(const QString &id, const Signal *sig, QWidget *parent) value_text->setBrush(Qt::gray); line_marker = new QGraphicsLineItem(chart); line_marker->setZValue(chart->zValue() + 10); - line_marker->setPen(QPen(Qt::black, 2)); setChart(chart); @@ -277,6 +284,12 @@ ChartView::ChartView(const QString &id, const Signal *sig, QWidget *parent) }); } +void ChartView::updateFromSettings() { + setFixedHeight(settings.chart_height); + chart()->setTheme(settings.chart_theme == 0 ? QChart::ChartThemeLight : QChart::QChart::ChartThemeDark); + line_marker->setPen(QPen(settings.chart_theme == 0 ? Qt::black : Qt::white, 2)); +} + void ChartView::setRange(double min, double max, bool force_update) { auto axis_x = dynamic_cast(chart()->axisX()); if (force_update || (min != axis_x->min() || max != axis_x->max())) { diff --git a/tools/cabana/chartswidget.h b/tools/cabana/chartswidget.h index a7c6f4bbd3..897ed62c2f 100644 --- a/tools/cabana/chartswidget.h +++ b/tools/cabana/chartswidget.h @@ -23,6 +23,7 @@ public: void updateSeries(const std::pair &range); void setRange(double min, double max, bool force_update = false); void updateLineMarker(double current_sec); + void updateFromSettings(); signals: void zoomIn(double min, double max); @@ -34,7 +35,6 @@ private: void enterEvent(QEvent *event) override; void leaveEvent(QEvent *event) override; void adjustChartMargins(); - void updateAxisY(); QGraphicsLineItem *track_line; @@ -51,6 +51,7 @@ Q_OBJECT public: ChartWidget(const QString &id, const Signal *sig, QWidget *parent); void updateTitle(); + void updateFromSettings(); signals: void remove(const QString &msg_id, const Signal *sig); @@ -58,8 +59,10 @@ signals: public: QString id; const Signal *signal; + QWidget *header; QLabel *msg_name_label; QLabel *sig_name_label; + QPushButton *remove_btn; ChartView *chart_view = nullptr; }; diff --git a/tools/cabana/settings.cc b/tools/cabana/settings.cc index 6e9d7f17cc..93bc05f20b 100644 --- a/tools/cabana/settings.cc +++ b/tools/cabana/settings.cc @@ -17,6 +17,7 @@ void Settings::save() { s.setValue("log_size", can_msg_log_size); s.setValue("cached_segment", cached_segment_limit); s.setValue("chart_height", chart_height); + s.setValue("chart_theme", chart_theme); s.setValue("max_chart_x_range", max_chart_x_range); emit changed(); } @@ -27,6 +28,7 @@ void Settings::load() { can_msg_log_size = s.value("log_size", 100).toInt(); cached_segment_limit = s.value("cached_segment", 3).toInt(); chart_height = s.value("chart_height", 200).toInt(); + chart_theme = s.value("chart_theme", 0).toInt(); max_chart_x_range = s.value("max_chart_x_range", 3 * 60).toInt(); } @@ -67,6 +69,11 @@ SettingsDlg::SettingsDlg(QWidget *parent) : QDialog(parent) { chart_height->setValue(settings.chart_height); form_layout->addRow(tr("Chart's height"), chart_height); + chart_theme = new QComboBox(); + chart_theme->addItems({"Light", "Dark"}); + chart_theme->setCurrentIndex(settings.chart_theme == 1 ? 1 : 0); + form_layout->addRow(tr("Chart theme"), chart_theme); + main_layout->addLayout(form_layout); auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); @@ -82,6 +89,7 @@ void SettingsDlg::save() { settings.can_msg_log_size = log_size->value(); settings.cached_segment_limit = cached_segment->value(); settings.chart_height = chart_height->value(); + settings.chart_theme = chart_theme->currentIndex(); settings.max_chart_x_range = max_chart_x_range->value() * 60; settings.save(); accept(); diff --git a/tools/cabana/settings.h b/tools/cabana/settings.h index fa97c49140..88eeebc722 100644 --- a/tools/cabana/settings.h +++ b/tools/cabana/settings.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -15,6 +16,7 @@ public: int can_msg_log_size = 100; int cached_segment_limit = 3; int chart_height = 200; + int chart_theme = 0; int max_chart_x_range = 3 * 60; // 3 minutes signals: @@ -31,6 +33,7 @@ public: QSpinBox *log_size ; QSpinBox *cached_segment; QSpinBox *chart_height; + QComboBox *chart_theme; QSpinBox *max_chart_x_range; };