mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-02-27 09:33:56 +08:00
DriveStats: switch between MILES and KM in real time,based on setting. (#21234)
* update distance unit * remove main_layout_ from header * cleanup * add title in add_stats_layouts
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "selfdrive/ui/qt/widgets/drive_stats.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QJsonDocument>
|
||||
#include <QGridLayout>
|
||||
#include <QJsonObject>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
@@ -10,17 +10,63 @@
|
||||
|
||||
const double MILE_TO_KM = 1.60934;
|
||||
|
||||
static QLayout* build_stat_layout(QLabel** metric, const QString& name) {
|
||||
QVBoxLayout* layout = new QVBoxLayout;
|
||||
layout->setMargin(0);
|
||||
*metric = new QLabel("0");
|
||||
(*metric)->setStyleSheet("font-size: 80px; font-weight: 600;");
|
||||
layout->addWidget(*metric, 0, Qt::AlignLeft);
|
||||
namespace {
|
||||
|
||||
QLabel* numberLabel() {
|
||||
QLabel* label = new QLabel("0");
|
||||
label->setStyleSheet("font-size: 80px; font-weight: 600;");
|
||||
return label;
|
||||
}
|
||||
|
||||
QLabel* unitLabel(const QString& name) {
|
||||
QLabel* label = new QLabel(name);
|
||||
label->setStyleSheet("font-size: 45px; font-weight: 500;");
|
||||
layout->addWidget(label, 0, Qt::AlignLeft);
|
||||
return layout;
|
||||
return label;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DriveStats::DriveStats(QWidget* parent) : QWidget(parent) {
|
||||
metric_ = Params().getBool("IsMetric");
|
||||
|
||||
QGridLayout* main_layout = new QGridLayout(this);
|
||||
main_layout->setMargin(0);
|
||||
|
||||
auto add_stats_layouts = [=](const QString &title, StatsLabels& labels) {
|
||||
int row = main_layout->rowCount();
|
||||
main_layout->addWidget(new QLabel(title), row++, 0, 1, 3);
|
||||
|
||||
main_layout->addWidget(labels.routes = numberLabel(), row, 0, Qt::AlignLeft);
|
||||
main_layout->addWidget(labels.distance = numberLabel(), row, 1, Qt::AlignLeft);
|
||||
main_layout->addWidget(labels.hours = numberLabel(), row, 2, Qt::AlignLeft);
|
||||
|
||||
main_layout->addWidget(unitLabel("DRIVES"), row + 1, 0, Qt::AlignLeft);
|
||||
main_layout->addWidget(labels.distance_unit = unitLabel(getDistanceUnit()), row + 1, 1, Qt::AlignLeft);
|
||||
main_layout->addWidget(unitLabel("HOURS"), row + 1, 2, Qt::AlignLeft);
|
||||
};
|
||||
|
||||
add_stats_layouts("ALL TIME", all_);
|
||||
add_stats_layouts("PAST WEEK", week_);
|
||||
|
||||
QString dongleId = QString::fromStdString(Params().get("DongleId"));
|
||||
QString url = "https://api.commadotai.com/v1.1/devices/" + dongleId + "/stats";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_DriveStats", 30);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &DriveStats::parseResponse);
|
||||
|
||||
setStyleSheet(R"(QLabel {font-size: 48px; font-weight: 500;})");
|
||||
}
|
||||
|
||||
void DriveStats::updateStats() {
|
||||
auto update = [=](const QJsonObject& obj, StatsLabels& labels) {
|
||||
labels.routes->setText(QString::number((int)obj["routes"].toDouble()));
|
||||
labels.distance->setText(QString::number(int(obj["distance"].toDouble() * (metric_ ? MILE_TO_KM : 1))));
|
||||
labels.distance_unit->setText(getDistanceUnit());
|
||||
labels.hours->setText(QString::number((int)(obj["minutes"].toDouble() / 60)));
|
||||
};
|
||||
|
||||
QJsonObject json = stats_.object();
|
||||
update(json["all"].toObject(), all_);
|
||||
update(json["week"].toObject(), week_);
|
||||
}
|
||||
|
||||
void DriveStats::parseResponse(const QString& response) {
|
||||
@@ -29,41 +75,14 @@ void DriveStats::parseResponse(const QString& response) {
|
||||
qDebug() << "JSON Parse failed on getting past drives statistics";
|
||||
return;
|
||||
}
|
||||
|
||||
auto update = [](const QJsonObject &obj, StatsLabels& labels, bool metric) {
|
||||
labels.routes->setText(QString::number((int)obj["routes"].toDouble()));
|
||||
labels.distance->setText(QString::number(int(obj["distance"].toDouble() * (metric ? MILE_TO_KM : 1))));
|
||||
labels.hours->setText(QString::number((int)(obj["minutes"].toDouble() / 60)));
|
||||
};
|
||||
|
||||
QJsonObject json = doc.object();
|
||||
update(json["all"].toObject(), all_, metric);
|
||||
update(json["week"].toObject(), week_, metric);
|
||||
stats_ = doc;
|
||||
updateStats();
|
||||
}
|
||||
|
||||
DriveStats::DriveStats(QWidget* parent) : QWidget(parent) {
|
||||
metric = Params().getBool("IsMetric");
|
||||
QString distance_unit = metric ? "KM" : "MILES";
|
||||
|
||||
auto add_stats_layouts = [&](QGridLayout* gl, StatsLabels& labels, int row, const QString &distance_unit) {
|
||||
gl->addLayout(build_stat_layout(&labels.routes, "DRIVES"), row, 0, 3, 1);
|
||||
gl->addLayout(build_stat_layout(&labels.distance, distance_unit), row, 1, 3, 1);
|
||||
gl->addLayout(build_stat_layout(&labels.hours, "HOURS"), row, 2, 3, 1);
|
||||
};
|
||||
|
||||
QGridLayout* gl = new QGridLayout(this);
|
||||
gl->setMargin(0);
|
||||
|
||||
gl->addWidget(new QLabel("ALL TIME"), 0, 0, 1, 3);
|
||||
add_stats_layouts(gl, all_, 1, distance_unit);
|
||||
|
||||
gl->addWidget(new QLabel("PAST WEEK"), 6, 0, 1, 3);
|
||||
add_stats_layouts(gl, week_, 7, distance_unit);
|
||||
|
||||
QString dongleId = QString::fromStdString(Params().get("DongleId"));
|
||||
QString url = "https://api.commadotai.com/v1.1/devices/" + dongleId + "/stats";
|
||||
RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_DriveStats", 30);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &DriveStats::parseResponse);
|
||||
|
||||
setStyleSheet(R"(QLabel {font-size: 48px; font-weight: 500;})");
|
||||
void DriveStats::showEvent(QShowEvent* event) {
|
||||
bool metric = Params().getBool("IsMetric");
|
||||
if (metric_ != metric) {
|
||||
metric_ = metric;
|
||||
updateStats();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QLabel>
|
||||
|
||||
class DriveStats : public QWidget {
|
||||
@@ -9,9 +10,14 @@ public:
|
||||
explicit DriveStats(QWidget* parent = 0);
|
||||
|
||||
private:
|
||||
bool metric;
|
||||
void showEvent(QShowEvent *event) override;
|
||||
void updateStats();
|
||||
inline QString getDistanceUnit() const { return metric_ ? "KM" : "MILES"; }
|
||||
|
||||
bool metric_;
|
||||
QJsonDocument stats_;
|
||||
struct StatsLabels {
|
||||
QLabel *routes, *distance, *hours;
|
||||
QLabel *routes, *distance, *distance_unit, *hours;
|
||||
} all_, week_;
|
||||
|
||||
private slots:
|
||||
|
||||
Reference in New Issue
Block a user