aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/lumina-desktop/LDesktopBackground.cpp
blob: dadbd84863a0b35d18692589f6d4f33b96fd0a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 *) {
    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;
}
bgstack15