aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/style/Style.h
blob: 995abbeb01fdafeac3e9584158f8174db7f24f8e (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
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Neptune 3 UI.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite 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 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** 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.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/
#pragma once

#include "StyleData.h"

#include <QJSValue>
#include <QQmlEngine>

#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
#  include <QtQuickControls2/private/qquickstyle_p.h>
class QQuickAttachedPropertyPropagator : public QQuickAttachedObject
{
protected:
    void initialize() { QQuickAttachedObject::init(); }
};

#else
#  include <QtQuickControls2/QQuickAttachedPropertyPropagator>
#endif

class Style : public QQuickAttachedPropertyPropagator
{
    Q_OBJECT
    Q_PROPERTY(Theme theme READ theme WRITE setTheme NOTIFY themeChanged FINAL)
    Q_PROPERTY(bool supportsMultipleThemes READ supportsMultipleThemes CONSTANT FINAL)

    // naming scheme use by other qt control styles
    // TODO: update controls to use colors from the design spec group or update the specs if needed
    Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY themeChanged FINAL)
    Q_PROPERTY(QColor buttonColor READ buttonColor NOTIFY themeChanged FINAL)
    Q_PROPERTY(QColor highlightedButtonColor READ highlightedButtonColor NOTIFY themeChanged FINAL)

    // naming scheme used in the design specs
    Q_PROPERTY(QColor accentColor READ accentColor WRITE setAccentColor NOTIFY accentColorChanged FINAL)
    Q_PROPERTY(QColor mainColor READ mainColor NOTIFY themeChanged FINAL)
    Q_PROPERTY(QColor offMainColor READ offMainColor NOTIFY themeChanged FINAL)
    Q_PROPERTY(QColor accentDetailColor READ accentDetailColor NOTIFY themeChanged FINAL)
    Q_PROPERTY(QColor contrastColor READ contrastColor NOTIFY themeChanged FINAL)
    Q_PROPERTY(QColor clusterMarksColor READ clusterMarksColor NOTIFY themeChanged FINAL)

    Q_PROPERTY(qreal opacityLow READ opacityLow NOTIFY themeChanged FINAL)
    Q_PROPERTY(qreal opacityMedium READ opacityMedium NOTIFY themeChanged FINAL)
    Q_PROPERTY(qreal opacityHigh READ opacityHigh NOTIFY themeChanged FINAL)
    Q_PROPERTY(qreal defaultDisabledOpacity READ defaultDisabledOpacity NOTIFY themeChanged FINAL)

    Q_PROPERTY(QString fontFamily READ fontFamily NOTIFY themeChanged FINAL)

    Q_PROPERTY(QString name READ name CONSTANT FINAL)

    /*
        Returns the file path for the given image name in the current style and theme

        Do not specify any file sufix. Ie, call Style.image("foobar"), not Style.image("foobar.png")
     */
    // This is a property instead of a Q_INVOKABLE so that it gets reevaluated by QML bindings when
    // its NOTIFY signal is emitted (in that case, when the theme changes). We need that as its results
    // are theme-dependent.
    Q_PROPERTY(QJSValue image READ image NOTIFY themeChanged)


public:

    // Not really public API. This is a helper method for the image property (which is a javascript Function,
    // ie. a callable object). Q_INVOKABLE as it's called from the javascript side (the QJSValue Function in
    // the image property)
    Q_INVOKABLE QString imageHelper(const QString &value);

    enum Theme { Light=StyleData::Light, Dark=StyleData::Dark };
    Q_ENUM(Theme)

    explicit Style(QObject *parent = nullptr);
    virtual ~Style();

    static Style *qmlAttachedProperties(QObject *object);

    Theme theme() const { return (Theme) m_theme; }
    void setTheme(Theme);

    bool supportsMultipleThemes() const;

    QColor accentColor() const { return m_accentColor; }
    void setAccentColor(const QColor &accent);

    QColor backgroundColor() const;
    QColor buttonColor() const;
    QColor highlightedButtonColor() const;
    QColor mainColor() const;
    QColor offMainColor() const;
    QColor accentDetailColor() const;
    QColor contrastColor() const;
    QColor clusterMarksColor() const;

    qreal opacityHigh() const;
    qreal opacityMedium() const;
    qreal opacityLow() const;
    qreal defaultDisabledOpacity() const;

    QString fontFamily() const;

    QString name() const;

    QJSValue image();

protected:
    void initialize();
    void attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent) override;

signals:
    void accentColorChanged();
    void themeChanged();

private:
    void inheritStyle(const Style& inheritedStyle);
    void propagateAccentColor();
    void propagateTheme();

    enum StyleData::Theme m_theme;
    QColor m_accentColor;

    mutable QJSValue m_image;
};

QML_DECLARE_TYPEINFO(Style, QML_HAS_ATTACHED_PROPERTIES)