aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/componentcore/theme.cpp
blob: 74c53841bdf9fa21512bdf6dc49e41ec0e572584 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "theme.h"
#include "qmldesignericonprovider.h"

#include <qmldesignerplugin.h>

#include <coreplugin/icore.h>

#include <utils/stylehelper.h>

#include <QApplication>
#include <QRegExp>
#include <QScreen>
#include <QPointer>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlProperty>
#include <qqml.h>

static Q_LOGGING_CATEGORY(themeLog, "qtc.qmldesigner.theme", QtWarningMsg)

namespace QmlDesigner {

Theme::Theme(Utils::Theme *originTheme, QObject *parent)
    : Utils::Theme(originTheme, parent)
    , m_constants(nullptr)
{
    QString constantsPath = Core::ICore::resourcePath() +
            QStringLiteral("/qmldesigner/propertyEditorQmlSources/imports/StudioTheme/InternalConstants.qml");

    QQmlEngine* engine = new QQmlEngine(this);
    QQmlComponent component(engine, QUrl::fromLocalFile(constantsPath));

    if (component.status() == QQmlComponent::Ready) {
        m_constants = component.create();
    }
    else if (component.status() == QQmlComponent::Error ) {
        qCWarning(themeLog) << "Couldn't load" << constantsPath
                            << "due to the following error(s):";
        for (const QQmlError &error : component.errors())
            qCWarning(themeLog) << error.toString();
    }
    else {
        qCWarning(themeLog) << "Couldn't load" << constantsPath
                            << "the status of the QQmlComponent is" << component.status();
    }
}

QColor Theme::evaluateColorAtThemeInstance(const QString &themeColorName)
{
    const QMetaObject &m = *metaObject();
    const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Color"));
    for (int i = 0, total = e.keyCount(); i < total; ++i) {
        if (QString::fromLatin1(e.key(i)) == themeColorName)
            return color(static_cast<Utils::Theme::Color>(i)).name();
    }

    qWarning() << Q_FUNC_INFO << "error while evaluating" << themeColorName;
    return {};
}

Theme *Theme::instance()
{
    static QPointer<Theme> qmldesignerTheme =
        new Theme(Utils::creatorTheme(), QmlDesigner::QmlDesignerPlugin::instance());
    return qmldesignerTheme;
}

QString Theme::replaceCssColors(const QString &input)
{
    QRegExp rx("creatorTheme\\.(\\w+)");

    int pos = 0;
    QString output = input;

    while ((pos = rx.indexIn(input, pos)) != -1) {
        const QString themeColorName = rx.cap(1);

        if (themeColorName == "smallFontPixelSize") {
            output.replace("creatorTheme." + themeColorName, QString::number(instance()->smallFontPixelSize()) + "px");
        } else if (themeColorName == "captionFontPixelSize") {
            output.replace("creatorTheme." + themeColorName, QString::number(instance()->captionFontPixelSize()) + "px");
        } else {
            const QColor color = instance()->evaluateColorAtThemeInstance(themeColorName);
            output.replace("creatorTheme." + rx.cap(1), color.name());
        }
        pos += rx.matchedLength();
    }

    return output;
}

void Theme::setupTheme(QQmlEngine *engine)
{
    static const int typeIndex = qmlRegisterSingletonType<Utils::Theme>("QtQuickDesignerTheme", 1, 0,
        "Theme", [](QQmlEngine *, QJSEngine *) {
            return qobject_cast<QObject*>(new Theme(Utils::creatorTheme(), nullptr));
    });
    Q_UNUSED(typeIndex)

    engine->addImageProvider(QLatin1String("icons"), new QmlDesignerIconProvider());
}

QColor Theme::getColor(Theme::Color role)
{

    return instance()->color(role);

}

int Theme::smallFontPixelSize() const
{
    if (highPixelDensity())
        return 13;
    return 9;
}

int Theme::captionFontPixelSize() const
{
    if (highPixelDensity())
        return 14;
    return 11;
}

bool Theme::highPixelDensity() const
{
    return qApp->primaryScreen()->logicalDotsPerInch() > 100;
}

QPixmap Theme::getPixmap(const QString &id)
{
    return QmlDesignerIconProvider::getPixmap(id);
}

QString Theme::getIconUnicode(Theme::Icon i)
{
    if (!instance()->m_constants)
        return QString();

    const QMetaObject *m = instance()->metaObject();
    const char *enumName = "Icon";
    int enumIndex = m->indexOfEnumerator(enumName);

    if (enumIndex == -1) {
        qCWarning(themeLog) << "Couldn't find enum" << enumName;
        return QString();
    }

    QMetaEnum e = m->enumerator(enumIndex);

    return instance()->m_constants->property(e.valueToKey(i)).toString();
}

QColor Theme::qmlDesignerBackgroundColorDarker() const
{
    return getColor(QmlDesigner_BackgroundColorDarker);
}

QColor Theme::qmlDesignerBackgroundColorDarkAlternate() const
{
    return getColor(QmlDesigner_BackgroundColorDarkAlternate);
}

QColor Theme::qmlDesignerTabLight() const
{
    return getColor(QmlDesigner_TabLight);
}

QColor Theme::qmlDesignerTabDark() const
{
    return getColor(QmlDesigner_TabDark);
}

QColor Theme::qmlDesignerButtonColor() const
{
    return getColor(QmlDesigner_ButtonColor);
}

QColor Theme::qmlDesignerBorderColor() const
{
    return getColor(QmlDesigner_BorderColor);
}

} // namespace QmlDesigner