diff options
Diffstat (limited to 'src/controls/qquickstyle.cpp')
-rw-r--r-- | src/controls/qquickstyle.cpp | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/src/controls/qquickstyle.cpp b/src/controls/qquickstyle.cpp new file mode 100644 index 00000000..e52e8b3a --- /dev/null +++ b/src/controls/qquickstyle.cpp @@ -0,0 +1,168 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyle_p.h" +#include <QtCore/qfile.h> +#include <QtCore/qjsondocument.h> +#include <QtCore/qjsonobject.h> +#include <QtCore/qjsonvalue.h> +#include <QtCore/qdebug.h> +#include <QtQml/qqmlengine.h> + +QT_BEGIN_NAMESPACE + +/*! + \qmltype Style + \inherits QtObject + \instantiates QQuickStyle + \inqmlmodule QtQuick.Controls + \ingroup utilities + \brief A style interface. + + TODO +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::accentColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::backgroundColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::focusColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::frameColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::pressColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::selectedTextColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::selectionColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::shadowColor +*/ + +/*! + \qmlproperty color QtQuickControls2::Style::textColor +*/ + +/*! + \qmlproperty int QtQuickControls2::Style::padding +*/ + +/*! + \qmlproperty int QtQuickControls2::Style::roundness +*/ + +/*! + \qmlproperty int QtQuickControls2::Style::spacing +*/ + +/*! + \qmlproperty real QtQuickControls2::Style::disabledOpacity +*/ + +static QColor readColorValue(const QJsonValue &value, const QColor &defaultValue) +{ + if (value.isString()) + return QColor(value.toString()); + return QColor::fromRgba(value.toInt(defaultValue.rgba())); +} + +static double readNumberValue(const QJsonValue &value, double defaultValue) +{ + return value.toDouble(defaultValue); +} + +QQuickStyle::QQuickStyle(QObject *parent) : QObject(parent) +{ + QJsonDocument doc; + + QFile file(QStringLiteral(":/qtquickcontrols/style.json")); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + qDebug() << file.error(); + qWarning() << "QQuickStyle: failed to open ':/qtquickcontrols/style.json': " << qPrintable(file.errorString()); + } else { + QJsonParseError error; + doc = QJsonDocument::fromJson(file.readAll(), &error); + if (error.error != QJsonParseError::NoError) + qWarning() << "QQuickStyle: failed to parse ':/qtquickcontrols/style.json': " << qPrintable(error.errorString()); + } + + QJsonObject style = doc.object(); + accentColor = readColorValue(style.value(QStringLiteral("accentColor")), QColor("#7bc258")); + backgroundColor = readColorValue(style.value(QStringLiteral("backgroundColor")), QColor("#ffffff")); + baseColor = readColorValue(style.value(QStringLiteral("baseColor")), QColor("#eeeeee")); + focusColor = readColorValue(style.value(QStringLiteral("focusColor")), QColor("#45a7d7")); + frameColor = readColorValue(style.value(QStringLiteral("frameColor")), QColor("#bdbebf")); + pressColor = readColorValue(style.value(QStringLiteral("pressColor")), QColor("#33333333")); + selectedTextColor = readColorValue(style.value(QStringLiteral("selectedTextColor")), QColor("#ffffff")); + selectionColor = readColorValue(style.value(QStringLiteral("selectionColor")), QColor("#45a7d7")); + shadowColor = readColorValue(style.value(QStringLiteral("shadowColor")), QColor("#28282a")); + textColor = readColorValue(style.value(QStringLiteral("textColor")), QColor("#26282a")); + padding = readNumberValue(style.value(QStringLiteral("padding")), 6); + roundness = readNumberValue(style.value(QStringLiteral("roundness")), 3); + spacing = readNumberValue(style.value(QStringLiteral("spacing")), 6); + disabledOpacity = readNumberValue(style.value(QStringLiteral("disabledOpacity")), 0.3); +} + +QQuickStyle *QQuickStyle::instance(QQmlEngine *engine) +{ + static QHash<QQmlEngine *, QQuickStyle *> styles; + QHash<QQmlEngine *, QQuickStyle *>::iterator it = styles.find(engine); + if (it == styles.end()) + it = styles.insert(engine, new QQuickStyle(engine)); + return it.value(); +} + +QQuickStyle *QQuickStyle::qmlAttachedProperties(QObject *object) +{ + return instance(qmlEngine(object)); +} + +QT_END_NAMESPACE |