QmlMaterial 0.1.0
Loading...
Searching...
No Matches
theme.hpp
1#pragma once
2
3#include <functional>
4
5#include <QtCore/QObject>
6#include <QtCore/QTimer>
7#include <QtQml/QQmlEngine>
8#include <QtGui/QColor>
9#include <QtQuickControls2/QQuickAttachedPropertyPropagator>
10
11#include "qml_material/token/color.hpp"
12#include "qml_material/util/page_context.hpp"
13
14#define ATTACH_PROPERTY(Type, Name) \
15private: \
16 Q_PROPERTY(Type Name READ Name WRITE set_##Name RESET reset_##Name NOTIFY Name##Changed FINAL) \
17public: \
18 Type Name() const; \
19 void set_##Name(Type); \
20 void reset_##Name(); \
21 AttachProp<Type>& get_##Name(); \
22 Q_SIGNAL void Name##Changed(); \
23 \
24private: \
25 AttachProp<Type> m_##Name { &Self::Name##Changed };
26
27namespace qml_material
28{
29
30class ThemeSize : public QObject {
31 Q_OBJECT
32 QML_ELEMENT
33public:
34 ThemeSize(QObject* parent = nullptr);
35 ~ThemeSize();
36 Q_PROPERTY(
37 qint32 windowClass READ windowClass WRITE setWindowClass NOTIFY windowClassChanged FINAL)
38 Q_PROPERTY(bool isCompact READ isCompact NOTIFY windowClassChanged FINAL)
39 Q_PROPERTY(bool isMedium READ isMedium NOTIFY windowClassChanged FINAL)
40 Q_PROPERTY(bool isExpanded READ isExpanded NOTIFY windowClassChanged FINAL)
41 Q_PROPERTY(bool isLarge READ isLarge NOTIFY windowClassChanged FINAL)
42 Q_PROPERTY(bool isExtraLarge READ isExtraLarge NOTIFY windowClassChanged FINAL)
43
44 Q_PROPERTY(qint32 duration READ duration WRITE setDuration NOTIFY durationChanged FINAL)
45 Q_PROPERTY(qint32 width READ width WRITE setWidth NOTIFY widthChanged FINAL)
46
47 auto isCompact() const -> bool;
48 auto isMedium() const -> bool;
49 auto isExpanded() const -> bool;
50 auto isLarge() const -> bool;
51 auto isExtraLarge() const -> bool;
52
53 auto width() const -> qint32;
54 void setWidth(qint32 w);
55 Q_SIGNAL void widthChanged(qint32);
56
57 auto duration() const -> qint32;
58 void setDuration(qint32 w);
59 Q_SIGNAL void durationChanged(qint32);
60
61 auto windowClass() const -> qint32;
62 void setWindowClass(qint32);
63 Q_SIGNAL void windowClassChanged(qint32);
64
65 Q_PROPERTY(qint32 verticalPadding READ verticalPadding NOTIFY verticalPaddingChanged FINAL)
66 auto verticalPadding() const -> qint32;
67 Q_SIGNAL void verticalPaddingChanged();
68
69private:
70 qint32 m_window_class;
71 qint32 m_width;
72 qint32 m_duration;
73 QTimer m_width_timer;
74};
75
76class Theme : public QQuickAttachedPropertyPropagator {
77 Q_OBJECT
78
79 QML_NAMED_ELEMENT(MProp)
80 QML_UNCREATABLE("")
81 QML_ATTACHED(Theme)
82
83public:
84 using Self = Theme;
85
86 template<typename V>
87 struct AttachProp {
88 using SigFunc = void (Theme::*)();
89 using ReadFunc = V (Theme::* const)();
90 using WriteFunc = void (Theme::*)(V);
91 using GetFunc = AttachProp<V>& (Theme::* const)();
92
93 std::optional<V> value;
94 bool explicited;
95 SigFunc sig_func;
96
97 AttachProp(SigFunc s): value(), explicited(false), sig_func(s) {}
98 };
99
100 ATTACH_PROPERTY(QColor, textColor)
101 ATTACH_PROPERTY(QColor, backgroundColor)
102 ATTACH_PROPERTY(int, elevation)
103 ATTACH_PROPERTY(qml_material::MdColorMgr*, color)
104 ATTACH_PROPERTY(qml_material::ThemeSize*, size)
105 ATTACH_PROPERTY(qml_material::PageContext*, page)
106
107public:
108 Theme(QObject* parent);
109 ~Theme();
110
111 static Theme* qmlAttachedProperties(QObject* object);
112
113protected:
114 void attachedParentChange(QQuickAttachedPropertyPropagator* newParent,
115 QQuickAttachedPropertyPropagator* oldParent) override;
116};
117} // namespace qml_material
118
119#undef ATTACH_PROPERTY
Material Design Color Manager class.
Definition color.hpp:26