Skip to content

Commit

Permalink
qt/util: new function scaledPixmap (commaai#23103)
Browse files Browse the repository at this point in the history
* new function scaledPixmap

* rename to loadpixmap
  • Loading branch information
deanlee authored Dec 15, 2021
1 parent 13b4ff5 commit 7568716
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 12 deletions.
3 changes: 1 addition & 2 deletions selfdrive/ui/qt/maps/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,9 @@ void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruct
fn += "turn_straight";
}

QPixmap pix(fn + ICON_SUFFIX);
auto icon = new QLabel;
int wh = active ? 125 : 75;
icon->setPixmap(pix.scaled(wh, wh, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
icon->setPixmap(loadPixmap(fn + ICON_SUFFIX, {wh, wh}, Qt::IgnoreAspectRatio));
icon->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
lane_layout->addWidget(icon);
}
Expand Down
4 changes: 2 additions & 2 deletions selfdrive/ui/qt/offroad/driverview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void DriverViewWindow::mouseReleaseEvent(QMouseEvent* e) {
}

DriverViewScene::DriverViewScene(QWidget* parent) : sm({"driverState"}), QWidget(parent) {
face_img = QImage("../assets/img_driver_face.png").scaled(FACE_IMG_SIZE, FACE_IMG_SIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
face_img = loadPixmap("../assets/img_driver_face.png", {FACE_IMG_SIZE, FACE_IMG_SIZE});
}

void DriverViewScene::showEvent(QShowEvent* event) {
Expand Down Expand Up @@ -97,5 +97,5 @@ void DriverViewScene::paintEvent(QPaintEvent* event) {
const int img_x = is_rhd ? rect2.right() - FACE_IMG_SIZE - img_offset : rect2.left() + img_offset;
const int img_y = rect2.bottom() - FACE_IMG_SIZE - img_offset;
p.setOpacity(face_detected ? 1.0 : 0.3);
p.drawImage(img_x, img_y, face_img);
p.drawPixmap(img_x, img_y, face_img);
}
2 changes: 1 addition & 1 deletion selfdrive/ui/qt/offroad/driverview.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public slots:
private:
Params params;
SubMaster sm;
QImage face_img;
QPixmap face_img;
bool is_rhd = false;
bool frame_updated = false;
};
Expand Down
8 changes: 4 additions & 4 deletions selfdrive/ui/qt/sidebar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void Sidebar::drawMetric(QPainter &p, const QString &label, QColor c, int y) {
}

Sidebar::Sidebar(QWidget *parent) : QFrame(parent) {
home_img = QImage("../assets/images/button_home.png").scaled(180, 180, Qt::KeepAspectRatio, Qt::SmoothTransformation);
settings_img = QImage("../assets/images/button_settings.png").scaled(settings_btn.width(), settings_btn.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
home_img = loadPixmap("../assets/images/button_home.png", {180, 180});
settings_img = loadPixmap("../assets/images/button_settings.png", settings_btn.size(), Qt::IgnoreAspectRatio);

connect(this, &Sidebar::valueChanged, [=] { update(); });

Expand Down Expand Up @@ -90,9 +90,9 @@ void Sidebar::paintEvent(QPaintEvent *event) {

// static imgs
p.setOpacity(0.65);
p.drawImage(settings_btn.x(), settings_btn.y(), settings_img);
p.drawPixmap(settings_btn.x(), settings_btn.y(), settings_img);
p.setOpacity(1.0);
p.drawImage(60, 1080 - 180 - 40, home_img);
p.drawPixmap(60, 1080 - 180 - 40, home_img);

// network
int x = 58;
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/ui/qt/sidebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public slots:
void mouseReleaseEvent(QMouseEvent *event) override;
void drawMetric(QPainter &p, const QString &label, QColor c, int y);

QImage home_img, settings_img;
QPixmap home_img, settings_img;
const QMap<cereal::DeviceState::NetworkType, QString> network_type = {
{cereal::DeviceState::NetworkType::NONE, "--"},
{cereal::DeviceState::NetworkType::WIFI, "Wi-Fi"},
Expand Down
4 changes: 2 additions & 2 deletions selfdrive/ui/qt/spinner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ TrackWidget::TrackWidget(QWidget *parent) : QWidget(parent) {
setFixedSize(spinner_size);

// pre-compute all the track imgs. make this a gif instead?
QPixmap comma_img = QPixmap("../assets/img_spinner_comma.png").scaled(spinner_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap track_img = QPixmap("../assets/img_spinner_track.png").scaled(spinner_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap comma_img = loadPixmap("../assets/img_spinner_comma.png", spinner_size);
QPixmap track_img = loadPixmap("../assets/img_spinner_track.png", spinner_size);

QTransform transform(1, 0, 0, 1, width() / 2, height() / 2);
QPixmap pm(spinner_size);
Expand Down
8 changes: 8 additions & 0 deletions selfdrive/ui/qt/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ QWidget* topWidget (QWidget* widget) {
while (widget->parentWidget() != nullptr) widget=widget->parentWidget();
return widget;
}

QPixmap loadPixmap(const QString &fileName, const QSize &size, Qt::AspectRatioMode aspectRatioMode) {
if (size.isEmpty()) {
return QPixmap(fileName);
} else {
return QPixmap(fileName).scaled(size, aspectRatioMode, Qt::SmoothTransformation);
}
}
2 changes: 2 additions & 0 deletions selfdrive/ui/qt/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QDateTime>
#include <QLayout>
#include <QPainter>
#include <QPixmap>
#include <QSurfaceFormat>
#include <QWidget>

Expand All @@ -20,3 +21,4 @@ QString timeAgo(const QDateTime &date);
void swagLogMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
void initApp();
QWidget* topWidget (QWidget* widget);
QPixmap loadPixmap(const QString &fileName, const QSize &size = {}, Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);

0 comments on commit 7568716

Please sign in to comment.