mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-02-22 20:03:53 +08:00
ui/map: reuse the Qlabels and remove call to clearLayout (#28788)
* dont clearlayout * replace show with setVisible * remove signals&connections, call updateDistance in updateInstructions * refactor updateDistance to getDistance
This commit is contained in:
@@ -32,8 +32,6 @@ MapWindow::MapWindow(const QMapboxGLSettings &settings) : m_settings(settings),
|
||||
|
||||
// Instructions
|
||||
map_instructions = new MapInstructions(this);
|
||||
QObject::connect(this, &MapWindow::instructionsChanged, map_instructions, &MapInstructions::updateInstructions);
|
||||
QObject::connect(this, &MapWindow::distanceChanged, map_instructions, &MapInstructions::updateDistance);
|
||||
map_instructions->setVisible(false);
|
||||
|
||||
map_eta = new MapETA(this);
|
||||
@@ -207,8 +205,7 @@ void MapWindow::updateState(const UIState &s) {
|
||||
|
||||
if (locationd_valid) {
|
||||
m_map->setPitch(MAX_PITCH); // TODO: smooth pitching based on maneuver distance
|
||||
emit distanceChanged(i.getManeuverDistance()); // TODO: combine with instructionsChanged
|
||||
emit instructionsChanged(i);
|
||||
map_instructions->updateInstructions(i);
|
||||
}
|
||||
} else {
|
||||
clearRoute();
|
||||
@@ -416,33 +413,16 @@ MapInstructions::MapInstructions(QWidget * parent) : QWidget(parent) {
|
||||
setPalette(pal);
|
||||
}
|
||||
|
||||
void MapInstructions::updateDistance(float d) {
|
||||
QString MapInstructions::getDistance(float d) {
|
||||
d = std::max(d, 0.0f);
|
||||
QString distance_str;
|
||||
|
||||
if (uiState()->scene.is_metric) {
|
||||
if (d > 500) {
|
||||
distance_str.setNum(d / 1000, 'f', 1);
|
||||
distance_str += tr(" km");
|
||||
} else {
|
||||
distance_str.setNum(50 * int(d / 50));
|
||||
distance_str += tr(" m");
|
||||
}
|
||||
return (d > 500) ? QString::number(d / 1000, 'f', 1) + tr(" km")
|
||||
: QString::number(50 * int(d / 50)) + tr(" m");
|
||||
} else {
|
||||
float miles = d * METER_TO_MILE;
|
||||
float feet = d * METER_TO_FOOT;
|
||||
|
||||
if (feet > 500) {
|
||||
distance_str.setNum(miles, 'f', 1);
|
||||
distance_str += tr(" mi");
|
||||
} else {
|
||||
distance_str.setNum(50 * int(feet / 50));
|
||||
distance_str += tr(" ft");
|
||||
}
|
||||
return (feet > 500) ? QString::number(d * METER_TO_MILE, 'f', 1) + tr(" mi")
|
||||
: QString::number(50 * int(feet / 50)) + tr(" ft");
|
||||
}
|
||||
|
||||
distance->setAlignment(Qt::AlignLeft);
|
||||
distance->setText(distance_str);
|
||||
}
|
||||
|
||||
void MapInstructions::showError(QString error_text) {
|
||||
@@ -464,6 +444,8 @@ void MapInstructions::noError() {
|
||||
}
|
||||
|
||||
void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruction) {
|
||||
setUpdatesEnabled(false);
|
||||
|
||||
// Show instruction text
|
||||
QString primary_str = QString::fromStdString(instruction.getManeuverPrimaryText());
|
||||
QString secondary_str = QString::fromStdString(instruction.getManeuverSecondaryText());
|
||||
@@ -471,6 +453,8 @@ void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruct
|
||||
primary->setText(primary_str);
|
||||
secondary->setVisible(secondary_str.length() > 0);
|
||||
secondary->setText(secondary_str);
|
||||
distance->setAlignment(Qt::AlignLeft);
|
||||
distance->setText(getDistance(instruction.getManeuverDistance()));
|
||||
|
||||
// Show arrow with direction
|
||||
QString type = QString::fromStdString(instruction.getManeuverType());
|
||||
@@ -502,15 +486,13 @@ void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruct
|
||||
}
|
||||
|
||||
// Show lanes
|
||||
bool has_lanes = false;
|
||||
clearLayout(lane_layout);
|
||||
for (auto const &lane: instruction.getLanes()) {
|
||||
has_lanes = true;
|
||||
bool active = lane.getActive();
|
||||
auto lanes = instruction.getLanes();
|
||||
for (int i = 0; i < lanes.size(); ++i) {
|
||||
bool active = lanes[i].getActive();
|
||||
|
||||
// TODO: only use active direction if active
|
||||
bool left = false, straight = false, right = false;
|
||||
for (auto const &direction: lane.getDirections()) {
|
||||
for (auto const &direction: lanes[i].getDirections()) {
|
||||
left |= direction == cereal::NavInstruction::Direction::LEFT;
|
||||
right |= direction == cereal::NavInstruction::Direction::RIGHT;
|
||||
straight |= direction == cereal::NavInstruction::Direction::STRAIGHT;
|
||||
@@ -530,13 +512,20 @@ void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruct
|
||||
fn += "_inactive";
|
||||
}
|
||||
|
||||
auto icon = new QLabel;
|
||||
icon->setPixmap(loadPixmap(fn + ICON_SUFFIX, {125, 125}, Qt::IgnoreAspectRatio));
|
||||
icon->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
|
||||
lane_layout->addWidget(icon);
|
||||
QLabel *label = (i < lane_labels.size()) ? lane_labels[i] : lane_labels.emplace_back(new QLabel);
|
||||
if (!label->parentWidget()) {
|
||||
lane_layout->addWidget(label);
|
||||
}
|
||||
label->setPixmap(loadPixmap(fn + ICON_SUFFIX, {125, 125}, Qt::IgnoreAspectRatio));
|
||||
label->setVisible(true);
|
||||
}
|
||||
lane_widget->setVisible(has_lanes);
|
||||
|
||||
for (int i = lanes.size(); i < lane_labels.size(); ++i) {
|
||||
lane_labels[i]->setVisible(false);
|
||||
}
|
||||
lane_widget->setVisible(lanes.size() > 0);
|
||||
|
||||
setUpdatesEnabled(true);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,15 +35,14 @@ private:
|
||||
QHBoxLayout *lane_layout;
|
||||
bool error = false;
|
||||
bool is_rhd = false;
|
||||
std::vector<QLabel *> lane_labels;
|
||||
|
||||
public:
|
||||
MapInstructions(QWidget * parent=nullptr);
|
||||
void showError(QString error);
|
||||
void noError();
|
||||
void hideIfNoError();
|
||||
|
||||
public slots:
|
||||
void updateDistance(float d);
|
||||
QString getDistance(float d);
|
||||
void updateInstructions(cereal::NavInstruction::Reader instruction);
|
||||
};
|
||||
|
||||
@@ -122,9 +121,6 @@ public slots:
|
||||
void offroadTransition(bool offroad);
|
||||
|
||||
signals:
|
||||
void distanceChanged(float distance);
|
||||
void instructionsChanged(cereal::NavInstruction::Reader instruction);
|
||||
|
||||
void requestVisible(bool visible);
|
||||
void openSettings();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user