/**************************************************************************** ** ** 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 #include #include #include #include #include #include #include #include #include #include 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(i)); } qWarning() << Q_FUNC_INFO << "error while evaluating" << themeColorName; return {}; } Theme *Theme::instance() { static QPointer qmldesignerTheme = new Theme(Utils::creatorTheme(), QmlDesigner::QmlDesignerPlugin::instance()); return qmldesignerTheme; } QString Theme::replaceCssColors(const QString &input) { const QRegularExpression rx("creatorTheme\\.(\\w+)"); int pos = 0; QString output = input; QRegularExpressionMatchIterator it = rx.globalMatch(input); while (it.hasNext()) { const QRegularExpressionMatch match = it.next(); const QString themeColorName = match.captured(1); const QRegularExpression replaceExp("creatorTheme\\." + themeColorName + "(\\s|;|\\n)"); if (themeColorName == "smallFontPixelSize") { output.replace(replaceExp, QString::number(instance()->smallFontPixelSize()) + "px" + "\\1"); } else if (themeColorName == "captionFontPixelSize") { output.replace(replaceExp, QString::number(instance()->captionFontPixelSize()) + "px" + "\\1"); } else { const QColor color = instance()->evaluateColorAtThemeInstance(themeColorName); // Create rgba(r, g, b, a) const QString rgbaStr = QString("rgba(%1, %2, %3, %4)") .arg(color.red()).arg(color.green()).arg(color.blue()) .arg(color.alpha()); output.replace(replaceExp, rgbaStr + "\\1"); } pos += match.capturedLength(); } return output; } void Theme::setupTheme(QQmlEngine *engine) { static const int typeIndex = qmlRegisterSingletonType("QtQuickDesignerTheme", 1, 0, "Theme", [](QQmlEngine *, QJSEngine *) { return qobject_cast(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