diff options
author | Henry Hu <henry.hu.sh@gmail.com> | 2016-05-12 19:11:10 -0400 |
---|---|---|
committer | Henry Hu <henry.hu.sh@gmail.com> | 2016-05-12 19:11:10 -0400 |
commit | 40c939afe1f7ff30531096fba621c6c5860cefe4 (patch) | |
tree | 615d804841d7ed895f8c8acadd18a6d5be5ac519 /src-qt5/core/lumina-desktop/LDesktopBackground.cpp | |
parent | add 2 options for background image: fit and full (diff) | |
download | lumina-40c939afe1f7ff30531096fba621c6c5860cefe4.tar.gz lumina-40c939afe1f7ff30531096fba621c6c5860cefe4.tar.bz2 lumina-40c939afe1f7ff30531096fba621c6c5860cefe4.zip |
Move background processing into LDesktopBackground class
Generate a QPixmap and use it during paintEvent
Diffstat (limited to 'src-qt5/core/lumina-desktop/LDesktopBackground.cpp')
-rw-r--r-- | src-qt5/core/lumina-desktop/LDesktopBackground.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src-qt5/core/lumina-desktop/LDesktopBackground.cpp b/src-qt5/core/lumina-desktop/LDesktopBackground.cpp new file mode 100644 index 00000000..bdb6dbab --- /dev/null +++ b/src-qt5/core/lumina-desktop/LDesktopBackground.cpp @@ -0,0 +1,78 @@ +//=========================================== +// Lumina-DE source code +// Copyright (c) 2016, Henry Hu +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include "LDesktopBackground.h" + +#include <QPainter> + +void LDesktopBackground::paintEvent(QPaintEvent *pe) { + if (bgPixmap != NULL) { + QPainter painter(this); + painter.setBrush(*bgPixmap); + painter.drawRect(0, 0, width(), height()); + } +} + +void LDesktopBackground::setBackground(const QString& bgFile, const QString& format) { + if (bgPixmap != NULL) delete bgPixmap; + bgPixmap = new QPixmap(size()); + + if (bgFile.startsWith("rgb(")) { + QStringList colors = bgFile.section(")",0,0).section("(",1,1).split(","); + QColor color = QColor(colors[0].toInt(), colors[1].toInt(), colors[2].toInt()); + bgPixmap->fill(color); + } else { + bgPixmap->fill(Qt::black); + + // Load the background file and scale + QPixmap bgImage(bgFile); + if (format == "stretch" || format == "full" || format == "fit") { + Qt::AspectRatioMode mode; + if (format == "stretch") { + mode = Qt::IgnoreAspectRatio; + } else if (format == "full") { + mode = Qt::KeepAspectRatioByExpanding; + } else { + mode = Qt::KeepAspectRatio; + } + bgImage = bgImage.scaled(size(), mode); + } + + // Calculate the offset + int dx = 0, dy = 0; + int drawWidth = bgImage.width(), drawHeight = bgImage.height(); + if (format == "fit" || format == "center" || format == "full") { + dx = (width() - bgImage.width()) / 2; + dy = (height() - bgImage.height()) / 2; + } else if (format == "tile") { + drawWidth = width(); + drawHeight = height(); + } else { + if (format.endsWith("right")) { + dx = width() - bgImage.width(); + } + if (format.startsWith("bottom")) { + dy = height() - bgImage.height(); + } + } + + // Draw the background image + QPainter painter(bgPixmap); + painter.setBrush(bgImage); + painter.setBrushOrigin(dx, dy); + painter.drawRect(dx, dy, drawWidth, drawHeight); + } + update(); + show(); +} + +LDesktopBackground::LDesktopBackground() : QWidget() { + bgPixmap = NULL; +} + +LDesktopBackground::~LDesktopBackground() { + if (bgPixmap != NULL) delete bgPixmap; +} |