Welcome to mirror list, hosted at ThFree Co, Russian Federation.

iconutils.cpp « gui « src - github.com/nextcloud/desktop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40ad051ce15aa0f5c3fd2f138bd6850787d3903e (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) by Oleksandr Zolotov <alex@nextcloud.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#include "iconutils.h"

#include <theme.h>

#include <QFile>
#include <QLoggingCategory>
#include <QPainter>
#include <QPixmapCache>
#include <QSvgRenderer>

namespace {
QString findSvgFilePath(const QString &fileName, const QStringList &possibleColors)
{
    QString result;
    result = QString{OCC::Theme::themePrefix} + fileName;
    if (QFile::exists(result)) {
        return result;
    } else {
        for (const auto &color : possibleColors) {
            result = QString{OCC::Theme::themePrefix} + color + QStringLiteral("/") + fileName;

            if (QFile::exists(result)) {
                return result;
            }
        }
        result.clear();
    }

    return result;
}
}

namespace OCC {
namespace Ui {
namespace IconUtils {
Q_LOGGING_CATEGORY(lcIconUtils, "nextcloud.gui.iconutils", QtInfoMsg)
QPixmap pixmapForBackground(const QString &fileName, const QColor &backgroundColor)
{
    Q_ASSERT(!fileName.isEmpty());

    const auto pixmapColor = backgroundColor.isValid() && !Theme::isDarkColor(backgroundColor)
        ? QColorConstants::Svg::black
        : QColorConstants::Svg::white;
    ;
    return createSvgPixmapWithCustomColorCached(fileName, pixmapColor);
}

QImage createSvgImageWithCustomColor(const QString &fileName, const QColor &customColor, QSize *originalSize, const QSize &requestedSize)
{
    Q_ASSERT(!fileName.isEmpty());
    Q_ASSERT(customColor.isValid());

    QImage result{};

    if (fileName.isEmpty() || !customColor.isValid()) {
        qWarning(lcIconUtils) << "invalid fileName or customColor";
        return result;
    }

    // some icons are present in white or black only, so, we need to check both when needed
    const auto iconBaseColors = QStringList{QStringLiteral("black"), QStringLiteral("white")};

    // check if there is an existing image matching the custom color
    {
        const auto customColorName = [&customColor]() {
            auto result = customColor.name();
            if (result.startsWith(QStringLiteral("#"))) {
                if (result == QStringLiteral("#000000")) {
                    result = QStringLiteral("black");
                }
                if (result == QStringLiteral("#ffffff")) {
                    result = QStringLiteral("white");
                }
            }
            return result;
        }();

        if (iconBaseColors.contains(customColorName)) {
            result = QImage{QString{OCC::Theme::themePrefix} + customColorName + QStringLiteral("/") + fileName};
            if (!result.isNull()) {
                return result;
            }
        }
    }
    
    // find the first matching svg file
    const auto sourceSvg = findSvgFilePath(fileName, iconBaseColors);

    Q_ASSERT(!sourceSvg.isEmpty());
    if (sourceSvg.isEmpty()) {
        qWarning(lcIconUtils) << "Failed to find base SVG file for" << fileName;
        return result;
    }

    result = drawSvgWithCustomFillColor(sourceSvg, customColor, originalSize, requestedSize);

    Q_ASSERT(!result.isNull());
    if (result.isNull()) {
        qWarning(lcIconUtils) << "Failed to load pixmap for" << fileName;
    }

    return result;
}

QPixmap createSvgPixmapWithCustomColorCached(const QString &fileName, const QColor &customColor, QSize *originalSize, const QSize &requestedSize)
{
    QPixmap cachedPixmap;

    const auto customColorName = customColor.name();

    const QString cacheKey = fileName + QStringLiteral(",") + customColorName;

    // check for existing QPixmap in cache
    if (QPixmapCache::find(cacheKey, &cachedPixmap)) {
        if (originalSize) {
            *originalSize = {};
        }
        return cachedPixmap;
    }

    cachedPixmap = QPixmap::fromImage(createSvgImageWithCustomColor(fileName, customColor, originalSize, requestedSize));

    if (!cachedPixmap.isNull()) {
        QPixmapCache::insert(cacheKey, cachedPixmap);
    }

    return cachedPixmap;
}

QImage drawSvgWithCustomFillColor(
    const QString &sourceSvgPath, const QColor &fillColor, QSize *originalSize, const QSize &requestedSize)
{
    QSvgRenderer svgRenderer;

    if (!svgRenderer.load(sourceSvgPath)) {
        qCWarning(lcIconUtils) << "Could no load initial SVG image";
        return {};
    }

    const auto reqSize = requestedSize.isValid() ? requestedSize : svgRenderer.defaultSize();

    if (originalSize) {
        *originalSize = svgRenderer.defaultSize();
    }

    // render source image
    QImage svgImage(reqSize, QImage::Format_ARGB32);
    {
        QPainter svgImagePainter(&svgImage);
        svgImage.fill(Qt::GlobalColor::transparent);
        svgRenderer.render(&svgImagePainter);
    }

    // draw target image with custom fillColor
    QImage image(reqSize, QImage::Format_ARGB32);
    image.fill(QColor(fillColor));
    {
        QPainter imagePainter(&image);
        imagePainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        imagePainter.drawImage(0, 0, svgImage);
    }

    return image;
}
}
}
}