diff options
author | Simon Rettberg | 2017-12-16 00:11:27 +0100 |
---|---|---|
committer | Simon Rettberg | 2017-12-16 00:11:27 +0100 |
commit | 47f7040eb6f266c4fceec127b1501af54b5cdb79 (patch) | |
tree | 1a418886c86c5f0340cd86bd6459ecfe88b4dd7f /src | |
parent | Add support for distro logo (diff) | |
download | slxgreeter-47f7040eb6f266c4fceec127b1501af54b5cdb79.tar.gz slxgreeter-47f7040eb6f266c4fceec127b1501af54b5cdb79.tar.xz slxgreeter-47f7040eb6f266c4fceec127b1501af54b5cdb79.zip |
Fix image scaling by using proper existing methods instead of broken copy & paste stuff
Diffstat (limited to 'src')
-rw-r--r-- | src/mainwindow.cpp | 76 |
1 files changed, 21 insertions, 55 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 75a1dcb..d58b7f6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -73,18 +73,13 @@ MainWindow::MainWindow(bool primary, int screen, const QRect &screenRect, QWidge QSvgWidget *banner = new QSvgWidget(_settings.bannerImagePath(), this); qWarning() << banner->sizeHint(); if (banner->renderer()->isValid()) { - int bw, bh; - QSize sh = banner->sizeHint(); - if (sh.height() < spaceY) { - banner->setFixedSize(sh); - bw = sh.width(); - bh = sh.height(); - } else { - bh = spaceY - 20; - bw = sh.width() * bh / sh.height(); - banner->setFixedSize(bw, bh); - } - banner->move((screenRect.width() - bw) / 2, (spaceY - bh) / 2); + QSize sh; + int offs = 0; + do { + offs += 20; + sh = banner->sizeHint().scaled(screenRect.width() - offs, spaceY - offs, Qt::KeepAspectRatio); + } while (offs < 200 && sh.width() > screenRect.width() / 2 && sh.height() > spaceY / 2); + banner->setGeometry((screenRect.width() - sh.width()) / 2, (spaceY - sh.height()) / 2, sh.width(), sh.height()); } } int ls = (spaceY > 500 ? 500 : spaceY); @@ -113,26 +108,13 @@ QSize MainWindow::createLogo(const QRect &max) QSvgWidget *img = new QSvgWidget(path, this); if (!img->renderer()->isValid()) return QSize(0, 0); - QSize sh = img->sizeHint(); - int w = sh.width(), h = sh.height(); - // This requires that the given rect is square - if (w > h) { - if (w != max.width()) { - h = h * max.width() / w; - w = max.width(); - } - } else { - if (h != max.height()) { - w = w * max.height() / h; - h = max.height(); - } - } - QSize size(w, h); - QRect c(max); - c.adjust(0, max.height() - h, -(max.width() - w), 0); - c.adjust(10, 10, -10, -10); + QSize virtualSize = img->sizeHint().scaled(max.width(), max.height(), Qt::KeepAspectRatio); + QSize realSize = img->sizeHint().scaled(max.width() - 20, max.height() - 20, Qt::KeepAspectRatio); + if (virtualSize.width() == 0 || virtualSize.height() == 0) + return QSize(0, 0); + QRect c(10, max.bottom() - realSize.height() - 10, realSize.width(), realSize.height()); img->setGeometry(c); - return size; + return virtualSize; } QSize MainWindow::createDistro(const QRect &max) @@ -140,36 +122,20 @@ QSize MainWindow::createDistro(const QRect &max) QImage img(QString("/etc/distro.png")); if (img.isNull()) return QSize(0, 0); - int w = img.width(), h = img.height(); - if (w+20 > max.width() || h+20 > max.height()) { + QSize realSize = img.size(); + QSize virtualSize = realSize.scaled(realSize.width() + 20, realSize.height() + 20, Qt::IgnoreAspectRatio); + if (virtualSize.width() > max.width() || virtualSize.height() > max.height()) { // This requires that the given rect is square - QRect cmp(max); - cmp.adjust(0, 0, -20, -20); - if (w > h) { - if (w != cmp.width()) { - h = h * cmp.width() / w; - w = cmp.width(); - } - } else { - if (h != cmp.height()) { - w = w * cmp.height() / h; - h = cmp.height(); - } - } - img = img.scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation); - cmp.adjust(0, 0, 20, 20); + img = img.scaled(max.width() - 20, max.height() - 20, Qt::KeepAspectRatio, Qt::SmoothTransformation); + realSize = img.size(); + virtualSize = realSize.scaled(realSize.width() + 20, realSize.height() + 20, Qt::IgnoreAspectRatio); } - w += 20; - h += 20; QPixmap pixmap(QPixmap::fromImage(img)); QLabel *lbl = new QLabel(this); lbl->setPixmap(pixmap); - QSize size(w, h); - QRect c(max); - c.adjust(max.width() - w, max.height() - h, 0, 0); - c.adjust(10, 10, -10, -10); + QRect c(max.right() - realSize.width() - 10, max.bottom() - realSize.height() - 10, realSize.width(), realSize.height()); lbl->setGeometry(c); - return size; + return virtualSize; } void MainWindow::createLogWindow(const QRect& geom) |