cabana: improve LogSlider (#27673)

* improve logslider

* fix code indentation
pull/27700/head
Dean Lee 2 years ago committed by GitHub
parent 8bd4c0f510
commit 583c4a031f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      tools/cabana/util.cc
  2. 23
      tools/cabana/util.h

@ -4,10 +4,8 @@
#include <QFontDatabase> #include <QFontDatabase>
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
#include <QDebug>
#include <limits>
#include <cmath> #include <cmath>
#include <limits>
#include "selfdrive/ui/qt/util.h" #include "selfdrive/ui/qt/util.h"
@ -42,7 +40,7 @@ void ChangeTracker::compute(const QByteArray &dat, double ts, uint32_t freq) {
} }
// Track bit level changes // Track bit level changes
for (int bit = 0; bit < 8; bit++){ for (int bit = 0; bit < 8; bit++) {
if ((cur ^ last) & (1 << bit)) { if ((cur ^ last) & (1 << bit)) {
bit_change_counts[i][bit] += 1; bit_change_counts[i][bit] += 1;
} }
@ -67,7 +65,6 @@ void ChangeTracker::clear() {
colors.clear(); colors.clear();
} }
// SegmentTree // SegmentTree
void SegmentTree::build(const QVector<QPointF> &arr) { void SegmentTree::build(const QVector<QPointF> &arr) {
@ -116,7 +113,7 @@ void MessageBytesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
int h_margin = option.widget->style()->pixelMetric(QStyle::PM_FocusFrameHMargin); int h_margin = option.widget->style()->pixelMetric(QStyle::PM_FocusFrameHMargin);
QRect rc{option.rect.left() + h_margin, option.rect.top() + v_margin, byte_width, option.rect.height() - 2 * v_margin}; QRect rc{option.rect.left() + h_margin, option.rect.top() + v_margin, byte_width, option.rect.height() - 2 * v_margin};
auto color_role = option.state & QStyle::State_Selected ? QPalette::HighlightedText: QPalette::Text; auto color_role = option.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text;
painter->setPen(option.palette.color(color_role)); painter->setPen(option.palette.color(color_role));
painter->setFont(fixed_font); painter->setFont(fixed_font);
for (int i = 0; i < byte_list.size(); ++i) { for (int i = 0; i < byte_list.size(); ++i) {
@ -139,7 +136,7 @@ QColor getColor(const cabana::Signal *sig) {
return QColor::fromHsvF(h, s, v); return QColor::fromHsvF(h, s, v);
} }
NameValidator::NameValidator(QObject *parent) : QRegExpValidator(QRegExp("^(\\w+)"), parent) { } NameValidator::NameValidator(QObject *parent) : QRegExpValidator(QRegExp("^(\\w+)"), parent) {}
QValidator::State NameValidator::validate(QString &input, int &pos) const { QValidator::State NameValidator::validate(QString &input, int &pos) const {
input.replace(' ', '_'); input.replace(' ', '_');
@ -175,7 +172,6 @@ QToolButton *toolButton(const QString &icon, const QString &tooltip) {
return btn; return btn;
}; };
QString toHex(uint8_t byte) { QString toHex(uint8_t byte) {
static std::array<QString, 256> hex = []() { static std::array<QString, 256> hex = []() {
std::array<QString, 256> ret; std::array<QString, 256> ret;
@ -193,4 +189,4 @@ int num_decimals(double num) {
} else { } else {
return split[1].size(); return split[1].size();
} }
} }

@ -36,17 +36,26 @@ class LogSlider : public QSlider {
public: public:
LogSlider(double factor, Qt::Orientation orientation, QWidget *parent = nullptr) : factor(factor), QSlider(orientation, parent) {}; LogSlider(double factor, Qt::Orientation orientation, QWidget *parent = nullptr) : factor(factor), QSlider(orientation, parent) {};
void setRange(double min, double max) { QSlider::setRange(logScale(min), logScale(max)); } void setRange(double min, double max) {
int value() const { return invLogScale(QSlider::value()); } log_min = factor * std::log10(min);
void setValue(int value) { QSlider::setValue(logScale(value)); } log_max = factor * std::log10(max);
QSlider::setRange(min, max);
setValue(QSlider::value());
}
int value() const {
double v = log_min + (log_max - log_min) * ((QSlider::value() - minimum()) / double(maximum() - minimum()));
return std::lround(std::pow(10, v / factor));
}
void setValue(int v) {
double log_v = std::clamp(factor * std::log10(v), log_min, log_max);
v = minimum() + (maximum() - minimum()) * ((log_v - log_min) / (log_max - log_min));
QSlider::setValue(v);
}
private: private:
double factor; double factor, log_min = 0, log_max = 1;
int logScale(int value) const { return factor * std::log10(value); }
int invLogScale(int value) const { return std::pow(10, value / factor); }
}; };
enum { enum {
ColorsRole = Qt::UserRole + 1, ColorsRole = Qt::UserRole + 1,
BytesRole = Qt::UserRole + 2 BytesRole = Qt::UserRole + 2

Loading…
Cancel
Save