diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-03-06 11:52:10 +0100 |
---|---|---|
committer | Jari-Pekka Nurmi <jpnurmi@theqtcompany.com> | 2015-03-11 01:06:44 +0200 |
commit | a3921bcf01275425350c9c153fe92086a34d1403 (patch) | |
tree | 1c5c4dd36e9d19a36281bde723c7bf989bc70298 /src/controls/qquickstyle.cpp | |
parent | e9398b7465c7df38876d0c6528ea5c42bbd8bf6a (diff) |
Implement Style as an attached property
Change-Id: I006ee566647e31d1a14919d164d7dd68539aae10
Reviewed-by: Jari-Pekka Nurmi <jpnurmi@theqtcompany.com>
Diffstat (limited to 'src/controls/qquickstyle.cpp')
-rw-r--r-- | src/controls/qquickstyle.cpp | 204 |
1 files changed, 0 insertions, 204 deletions
diff --git a/src/controls/qquickstyle.cpp b/src/controls/qquickstyle.cpp deleted file mode 100644 index 8b3612c6..00000000 --- a/src/controls/qquickstyle.cpp +++ /dev/null @@ -1,204 +0,0 @@ -/**************************************************************************** -** -** 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 "qquickstyle_p_p.h" -#include "qquickstylable_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> -#include <QtQuick/qquickitem.h> -#include <QtQuick/qquickwindow.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 -*/ - -QQuickStyle *QQuickStylePrivate::resolve(QQuickItem *item, QQuickStyle *res) -{ - QQuickStylable *stylable = qobject_cast<QQuickStylable *>(item); - Q_ASSERT(stylable); - - if (!res && stylable->hasStyle()) - return stylable->style(); - - // lookup parent style - if (!res) { - QQuickItem *parent = item->parentItem(); - while (!res && parent) { - QQuickStylable *stylable = qobject_cast<QQuickStylable *>(parent); - if (stylable && stylable->hasStyle()) - res = stylable->style(); - parent = parent->parentItem(); - } - } - - // fallback to window or global style - if (!res) { - QQuickStylable *window = qobject_cast<QQuickStylable *>(item->window()); - if (window) - res = window->style(); - if (!res) - res = QQuickStyle::instance(qmlEngine(item)); - } - - return res; -} - -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 |