Cabana: auto theme, detect from system. (#26563)

* auto theme

* cleanup

* get font color from ForegroundRole

* fix label color

* add padding for header

* smaller warning icon

* fix bg of binary view

* hightlight after init
old-commit-hash: 4662f1e0be
This commit is contained in:
Dean Lee 2022-11-23 06:23:49 +08:00 committed by GitHub
parent 4f7bfc430a
commit 4a67971c61
11 changed files with 41 additions and 32 deletions

View File

@ -1,6 +1,5 @@
#include "tools/cabana/binaryview.h"
#include <QApplication>
#include <QFontDatabase>
#include <QHeaderView>
#include <QMouseEvent>
@ -74,14 +73,18 @@ void BinaryView::mousePressEvent(QMouseEvent *event) {
event->accept();
}
void BinaryView::mouseMoveEvent(QMouseEvent *event) {
if (auto index = indexAt(event->pos()); index.isValid()) {
void BinaryView::highlightPosition(const QPoint &pos) {
if (auto index = indexAt(viewport()->mapFromGlobal(pos)); index.isValid()) {
auto item = (BinaryViewModel::Item *)index.internalPointer();
const Signal *sig = item->sigs.isEmpty() ? nullptr : item->sigs.back();
highlight(sig);
sig ? QToolTip::showText(event->globalPos(), sig->name.c_str(), this, rect())
sig ? QToolTip::showText(pos, sig->name.c_str(), this, rect())
: QToolTip::hideText();
}
}
void BinaryView::mouseMoveEvent(QMouseEvent *event) {
highlightPosition(event->globalPos());
QTableView::mouseMoveEvent(event);
}
@ -116,6 +119,7 @@ void BinaryView::setMessage(const QString &message_id) {
anchor_index = QModelIndex();
resize_sig = nullptr;
hovered_sig = nullptr;
highlightPosition(QCursor::pos());
updateState();
}
@ -232,19 +236,18 @@ void BinaryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
BinaryView *bin_view = (BinaryView *)parent();
painter->save();
// background
if (option.state & QStyle::State_Selected) {
if (index.column() == 8) {
painter->setFont(hex_font);
} else if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, selection_color);
} else if (!bin_view->selectionModel()->hasSelection() || !item->sigs.contains(bin_view->resize_sig)) {
painter->setPen(QApplication::style()->standardPalette().color(QPalette::BrightText));
} else if (!item->sigs.isEmpty() && (!bin_view->selectionModel()->hasSelection() || !item->sigs.contains(bin_view->resize_sig))) {
painter->fillRect(option.rect, item->bg_color);
painter->setPen(item->sigs.contains(bin_view->hovered_sig)
? QApplication::style()->standardPalette().color(QPalette::BrightText)
: Qt::black);
}
// text
if (index.column() == 8) { // hex column
painter->setFont(hex_font);
} else if (option.state & QStyle::State_Selected || (!bin_view->resize_sig && item->sigs.contains(bin_view->hovered_sig))) {
painter->setPen(Qt::white);
}
painter->drawText(option.rect, Qt::AlignCenter, item->val);
if (item->is_msb || item->is_lsb) {
painter->setFont(small_font);

View File

@ -1,5 +1,6 @@
#pragma once
#include <QApplication>
#include <QList>
#include <QSet>
#include <QStyledItemDelegate>
@ -41,7 +42,7 @@ public:
}
struct Item {
QColor bg_color = QColor(Qt::white);
QColor bg_color = QApplication::style()->standardPalette().color(QPalette::Base);
bool is_msb = false;
bool is_lsb = false;
QString val = "0";
@ -79,6 +80,7 @@ private:
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void leaveEvent(QEvent *event) override;
void highlightPosition(const QPoint &pt);
QModelIndex anchor_index;
BinaryViewModel *model;

View File

@ -1,6 +1,5 @@
#include <QApplication>
#include <QCommandLineParser>
#include <QStyleFactory>
#include "selfdrive/ui/qt/util.h"
#include "tools/cabana/mainwin.h"
@ -8,7 +7,6 @@
int main(int argc, char *argv[]) {
initApp(argc, argv);
QApplication app(argc, argv);
app.setStyle(QStyleFactory::create("Fusion"));
QCommandLineParser cmd_parser;
cmd_parser.addHelpOption();

View File

@ -40,6 +40,8 @@ ChartsWidget::ChartsWidget(QWidget *parent) : QWidget(parent) {
main_layout->addWidget(charts_scroll);
use_dark_theme = palette().color(QPalette::WindowText).value() > palette().color(QPalette::Background).value();
QObject::connect(dbc(), &DBCManager::DBCFileChanged, this, &ChartsWidget::removeAll);
QObject::connect(can, &CANMessages::eventsMerged, this, &ChartsWidget::eventsMerged);
QObject::connect(can, &CANMessages::updated, this, &ChartsWidget::updateState);
@ -125,6 +127,7 @@ void ChartsWidget::showChart(const QString &id, const Signal *sig, bool show, bo
ChartView *chart = merge && charts.size() > 0 ? charts.back() : nullptr;
if (!chart) {
chart = new ChartView(this);
chart->chart()->setTheme(use_dark_theme ? QChart::QChart::ChartThemeDark : QChart::ChartThemeLight);
chart->setEventsRange(display_range);
auto range = is_zoomed ? zoomed_range : display_range;
chart->setDisplayRange(range.first, range.second);
@ -288,7 +291,6 @@ void ChartView::updateTitle() {
void ChartView::updateFromSettings() {
setFixedHeight(settings.chart_height);
chart()->setTheme(settings.chart_theme == 0 ? QChart::ChartThemeLight : QChart::QChart::ChartThemeDark);
}
void ChartView::setEventsRange(const std::pair<double, double> &range) {

View File

@ -112,4 +112,5 @@ private:
std::pair<double, double> event_range;
std::pair<double, double> display_range;
std::pair<double, double> zoomed_range;
bool use_dark_theme = false;
};

View File

@ -25,7 +25,6 @@ DetailWidget::DetailWidget(ChartsWidget *charts, QWidget *parent) : charts(chart
// tabbar
tabbar = new QTabBar(this);
tabbar->setTabsClosable(true);
tabbar->setDrawBase(false);
tabbar->setUsesScrollButtons(true);
tabbar->setAutoHide(true);
tabbar->setContextMenuPolicy(Qt::CustomContextMenu);
@ -57,7 +56,7 @@ DetailWidget::DetailWidget(ChartsWidget *charts, QWidget *parent) : charts(chart
QHBoxLayout *warning_hlayout = new QHBoxLayout(warning_widget);
warning_hlayout->setContentsMargins(0, 0, 0, 0);
QLabel *warning_icon = new QLabel(this);
warning_icon->setPixmap(style()->standardPixmap(QStyle::SP_MessageBoxWarning));
warning_icon->setPixmap(style()->standardPixmap(QStyle::SP_MessageBoxWarning).scaledToWidth(24, Qt::SmoothTransformation));
warning_hlayout->addWidget(warning_icon, 0, Qt::AlignTop);
warning_label = new QLabel(this);
warning_hlayout->addWidget(warning_label, 1, Qt::AlignLeft);

View File

@ -1,6 +1,7 @@
#include "tools/cabana/historylog.h"
#include <QFontDatabase>
#include <QPainter>
// HistoryLogModel
@ -44,6 +45,8 @@ QVariant HistoryLogModel::headerData(int section, Qt::Orientation orientation, i
return has_signal ? QString::fromStdString(get_signal(dbc_msg, section - 1).name).replace('_', ' ') : "Data";
} else if (role == Qt::BackgroundRole && section > 0 && has_signal) {
return QBrush(QColor(getColor(section - 1)));
} else if (role == Qt::ForegroundRole && section > 0 && has_signal) {
return QBrush(Qt::black);
}
}
return {};
@ -73,7 +76,18 @@ void HistoryLogModel::updateState() {
QSize HeaderView::sectionSizeFromContents(int logicalIndex) const {
const QString text = model()->headerData(logicalIndex, this->orientation(), Qt::DisplayRole).toString();
const QRect rect = fontMetrics().boundingRect(QRect(0, 0, sectionSize(logicalIndex), 1000), defaultAlignment(), text);
return rect.size() + QSize{10, 5};
return rect.size() + QSize{10, 6};
}
void HeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const {
auto bg_role = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
if (bg_role.isValid()) {
QPen pen(model()->headerData(logicalIndex, Qt::Horizontal, Qt::ForegroundRole).value<QBrush>(), 1);
painter->setPen(pen);
painter->fillRect(rect, bg_role.value<QBrush>());
}
QString text = model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString();
painter->drawText(rect.adjusted(5, 3, 5, 3), defaultAlignment(), text);
}
// HistoryLog
@ -88,7 +102,6 @@ HistoryLog::HistoryLog(QWidget *parent) : QTableView(parent) {
verticalHeader()->setVisible(false);
setFrameShape(QFrame::NoFrame);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
setStyleSheet("QTableView::item { border:0px; padding-left:5px; padding-right:5px; }");
}
int HistoryLog::sizeHintForColumn(int column) const {

View File

@ -10,6 +10,7 @@ class HeaderView : public QHeaderView {
public:
HeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent) {}
QSize sectionSizeFromContents(int logicalIndex) const override;
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
};
class HistoryLogModel : public QAbstractTableModel {

View File

@ -18,7 +18,6 @@ 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);
s.setValue("last_dir", last_dir);
s.setValue("splitter_state", splitter_state);
@ -30,7 +29,6 @@ void Settings::load() {
can_msg_log_size = s.value("log_size", 50).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();
last_dir = s.value("last_dir", QDir::homePath()).toString();
splitter_state = s.value("splitter_state").toByteArray();
@ -72,11 +70,6 @@ SettingsDlg::SettingsDlg(QWidget *parent) : QDialog(parent) {
chart_height->setValue(settings.chart_height);
form_layout->addRow(tr("Chart 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);
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
form_layout->addRow(buttonBox);
@ -90,7 +83,6 @@ 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();

View File

@ -17,7 +17,6 @@ public:
int can_msg_log_size = 50;
int cached_segment_limit = 3;
int chart_height = 200;
int chart_theme = 0;
int max_chart_x_range = 3 * 60; // 3 minutes
QString last_dir;
QByteArray splitter_state;
@ -36,7 +35,6 @@ public:
QSpinBox *log_size ;
QSpinBox *cached_segment;
QSpinBox *chart_height;
QComboBox *chart_theme;
QSpinBox *max_chart_x_range;
};

View File

@ -137,7 +137,7 @@ void SignalEdit::setSignal(const QString &message_id, const Signal *signal) {
updateForm(msg_id == message_id && form->isVisible());
msg_id = message_id;
color_label->setText(QString::number(form_idx + 1));
color_label->setStyleSheet(QString("background-color:%1").arg(getColor(form_idx)));
color_label->setStyleSheet(QString("color:black; background-color:%2").arg(getColor(form_idx)));
title->setText(sig->name.c_str());
show();
}