aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qmljs/qmljsicons.cpp
blob: b6ffe72c5b56a10ef81a38bea62170e5ad183758 (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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmljsicons.h"

#include <cplusplus/Icons.h>
#include <utils/appinfo.h>
#include <qmljs/parser/qmljsast_p.h>

#include <QDir>
#include <QHash>
#include <QIcon>
#include <QLoggingCategory>
#include <QPair>
#include <QStringView>

using namespace QmlJS;
using namespace QmlJS::AST;

enum {
    debug = false
};

static Q_LOGGING_CATEGORY(iconsLog, "qtc.qmljs.icons", QtWarningMsg)

namespace QmlJS::Icons {
namespace Internal {
class IconsStorage
{
public:
    using FullTypeName = std::pair<QString /*packageName*/, QString /*typeName*/>;

    explicit IconsStorage();

    QIcon query(QStringView typeName) const;

private:
    const Utils::FilePath m_path = Utils::appInfo().resources / QLatin1String("qmlicons");
    QHash<FullTypeName, QIcon> m_iconsMap;
    QList<QString> m_packageNames;
};

IconsStorage::IconsStorage()
{
    const QString iconsPath = m_path.toFSPathString();
    if (debug)
        qCDebug(iconsLog) << "parsing" << iconsPath;
    QDir topDir(iconsPath);
    const QFileInfoList dirs = topDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
    for (const QFileInfo &subDirInfo : dirs) {
        if (debug)
            qCDebug(iconsLog) << "parsing" << subDirInfo.absoluteFilePath();
        const QString packageName = subDirInfo.fileName();
        m_packageNames.append(packageName);
        QDir subDir(subDirInfo.absoluteFilePath() + QLatin1String("/16x16"));
        const QFileInfoList files = subDir.entryInfoList(QDir::Files);
        for (const QFileInfo &iconFile : files) {
            QIcon icon(iconFile.absoluteFilePath());
            if (icon.isNull()) {
                if (debug)
                    qCDebug(iconsLog) << "skipping" << iconFile.absoluteFilePath();
                continue;
            }
            if (debug)
                qCDebug(iconsLog) << "adding" << packageName << iconFile.baseName()
                                  << "icon to database";
            FullTypeName iconId(packageName, iconFile.baseName());
            m_iconsMap.insert(iconId, icon);
        }
    }
}

QIcon IconsStorage::query(QStringView typeName) const
{
    for (const QString &packageName : m_packageNames) {
        const FullTypeName fullTypeName(packageName, typeName.toString());
        if (debug)
            qCDebug(iconsLog) << "icon for" << packageName << typeName << "requested"
                              << m_iconsMap.contains(fullTypeName);
        if (m_iconsMap.contains(fullTypeName)) {
            return m_iconsMap.value(fullTypeName);
        }
    }
    return QIcon();
}
} // namespace Internal

Provider::Provider()
    : m_stockPtr(std::make_unique<const Internal::IconsStorage>())
{
}

Provider &Provider::instance()
{
    static Provider iconsProvider;
    return iconsProvider;
}

QIcon Provider::icon(QStringView typeName) const
{
    return m_stockPtr->query(typeName);
}

QIcon objectDefinitionIcon()
{
    return Utils::CodeModelIcon::iconForType(Utils::CodeModelIcon::Class);
}

QIcon scriptBindingIcon()
{
    return Utils::CodeModelIcon::iconForType(Utils::CodeModelIcon::VarPublic);
}

QIcon publicMemberIcon()
{
    return Utils::CodeModelIcon::iconForType(Utils::CodeModelIcon::FuncPublic);
}

QIcon functionDeclarationIcon()
{
    return Utils::CodeModelIcon::iconForType(Utils::CodeModelIcon::FuncPublic);
}

QIcon enumMemberIcon()
{
    return Utils::CodeModelIcon::iconForType(Utils::CodeModelIcon::Enum);
}
} // namespace QmlJS::Icons