qt/util: new function scaledPixmap (#23103)

* new function scaledPixmap

* rename to loadpixmap
pull/23227/head
Dean Lee 2021-12-15 14:58:47 +08:00 committed by GitHub
parent 13b4ff504d
commit 75687169eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 21 additions and 12 deletions

View File

@ -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);
}

View File

@ -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) {
@ -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);
}

View File

@ -24,7 +24,7 @@ protected:
private:
Params params;
SubMaster sm;
QImage face_img;
QPixmap face_img;
bool is_rhd = false;
bool frame_updated = false;
};

View File

@ -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(); });
@ -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;

View File

@ -32,7 +32,7 @@ protected:
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"},

View File

@ -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);

View File

@ -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);
}
}

View File

@ -5,6 +5,7 @@
#include <QDateTime>
#include <QLayout>
#include <QPainter>
#include <QPixmap>
#include <QSurfaceFormat>
#include <QWidget>
@ -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);