From 40c939afe1f7ff30531096fba621c6c5860cefe4 Mon Sep 17 00:00:00 2001 From: Henry Hu Date: Thu, 12 May 2016 19:11:10 -0400 Subject: Move background processing into LDesktopBackground class Generate a QPixmap and use it during paintEvent --- src-qt5/core/lumina-desktop/LDesktopBackground.cpp | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src-qt5/core/lumina-desktop/LDesktopBackground.cpp (limited to 'src-qt5/core/lumina-desktop/LDesktopBackground.cpp') 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 + +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; +} -- cgit