QmlMaterial 0.1.0
Loading...
Searching...
No Matches
theme.hpp
1#pragma once
2
3#include <functional>
4
5#include <QtCore/QObject>
6#include <QtQml/QQmlEngine>
7#include <QtGui/QColor>
8#include <QtQuickControls2/QQuickAttachedPropertyPropagator>
9
10#include "qml_material/token/color.hpp"
11#include "qml_material/util/page_context.hpp"
12
13#define ATTACH_PROPERTY(_type_, _name_) \
14private: \
15 Q_PROPERTY(_type_ _name_ READ _name_ WRITE set_##_name_ RESET reset_##_name_ NOTIFY \
16 _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
40 auto isCompact() const -> bool;
41 auto windowClass() const -> qint32;
42 void setWindowClass(qint32);
43 Q_SIGNAL void windowClassChanged();
44
45 Q_PROPERTY(qint32 verticalPadding READ verticalPadding NOTIFY verticalPaddingChanged FINAL)
46 auto verticalPadding() const -> qint32;
47 Q_SIGNAL void verticalPaddingChanged();
48
49private:
50 qint32 m_window_class;
51};
52
53class Theme : public QQuickAttachedPropertyPropagator {
54 Q_OBJECT
55
56 QML_NAMED_ELEMENT(MProp)
57 QML_UNCREATABLE("")
58 QML_ATTACHED(Theme)
59
60public:
61 using Self = Theme;
62
63 template<typename V>
64 struct AttachProp {
65 using SigFunc = void (Theme::*)();
66 using ReadFunc = V (Theme::*const)();
67 using WriteFunc = void (Theme::*)(V);
68 using GetFunc = AttachProp<V>& (Theme::*const)();
69
70 std::optional<V> value;
71 bool explicited;
72 SigFunc sig_func;
73
74 AttachProp(SigFunc s): value(), explicited(false), sig_func(s) {}
75 };
76
77 ATTACH_PROPERTY(QColor, textColor)
78 ATTACH_PROPERTY(QColor, backgroundColor)
79 ATTACH_PROPERTY(int, elevation)
80 ATTACH_PROPERTY(qml_material::MdColorMgr*, color)
81 ATTACH_PROPERTY(qml_material::ThemeSize*, size)
82 ATTACH_PROPERTY(qml_material::PageContext*, page)
83
84public:
85 Theme(QObject* parent);
86 ~Theme();
87
88 static Theme* qmlAttachedProperties(QObject* object);
89
90protected:
91 void attachedParentChange(QQuickAttachedPropertyPropagator* newParent,
92 QQuickAttachedPropertyPropagator* oldParent) override;
93};
94} // namespace qml_material
95
96#undef ATTACH_PROPERTY
Material Design Color Manager class.
Definition color.hpp:26