summaryrefslogtreecommitdiffstats
path: root/src/designer/src/lib/shared/iconloader.cpp
blob: ecb97ba2b66d741dd408ffdb32394e0e6fbb6605 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "iconloader_p.h"

#include <QtCore/qfile.h>
#include <QtCore/qoperatingsystemversion.h>
#include <QtGui/qicon.h>
#include <QtGui/qpixmap.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

namespace qdesigner_internal {

QDESIGNER_SHARED_EXPORT QIcon createIconSet(QIcon::ThemeIcon themeIcon,
                                            QLatin1StringView name)
{
    return QOperatingSystemVersion::currentType() != QOperatingSystemVersion::MacOS
           && QIcon::hasThemeIcon(themeIcon)
        ? QIcon::fromTheme(themeIcon) : createIconSet(name);
}

template <class StringView>
static inline QIcon createIconSetHelper(StringView name)
{
    constexpr QLatin1StringView prefixes[] = {
        ":/qt-project.org/formeditor/images/"_L1,
#ifdef Q_OS_MACOS
        ":/qt-project.org/formeditor/images/mac/"_L1,
#else
        ":/qt-project.org/formeditor/images/win/"_L1,
#endif
        ":/qt-project.org/formeditor/images/designer_"_L1
    };

    for (QLatin1StringView prefix : prefixes) {
        const QString f = prefix + name;
        if (QFile::exists(f))
            return QIcon(f);
    }

    return {};
}

QDESIGNER_SHARED_EXPORT QIcon createIconSet(QStringView name)
{
    return createIconSetHelper(name);
}

QDESIGNER_SHARED_EXPORT QIcon createIconSet(QLatin1StringView name)
{
    return createIconSetHelper(name);
}

QDESIGNER_SHARED_EXPORT QIcon emptyIcon()
{
    return QIcon(u":/qt-project.org/formeditor/images/emptyicon.png"_s);
}

static QIcon buildIcon(const QString &prefix, const int *sizes, size_t sizeCount)
{
    QIcon result;
    for (size_t i = 0; i < sizeCount; ++i) {
        const QString size = QString::number(sizes[i]);
        const QPixmap pixmap(prefix + size + 'x'_L1 + size + ".png"_L1);
        Q_ASSERT(!pixmap.size().isEmpty());
        result.addPixmap(pixmap);
    }
    return result;
}

QDESIGNER_SHARED_EXPORT QIcon qtLogoIcon()
{
    static const int sizes[] = {16, 24, 32, 64, 128};
    static const QIcon result =
        buildIcon(u":/qt-project.org/formeditor/images/qtlogo"_s,
                  sizes, sizeof(sizes) / sizeof(sizes[0]));
    return result;
}

} // namespace qdesigner_internal

QT_END_NAMESPACE