QmlMaterial 0.1.0
Loading...
Searching...
No Matches
scroll_indicator.hpp
1#pragma once
2
3#include "qml_material/control/control.hpp"
4#include <memory>
5
6namespace qml_material
7{
8
9class ScrollIndicatorAttached;
10class ScrollViewport;
11
13class QML_MATERIAL_API ScrollIndicator : public Control {
14 Q_OBJECT
15 QML_NAMED_ELEMENT(ScrollIndicator)
16 QML_ATTACHED(ScrollIndicatorAttached)
17 Q_PROPERTY(qreal size READ size WRITE setSize NOTIFY sizeChanged FINAL)
18 Q_PROPERTY(qreal position READ position WRITE setPosition NOTIFY positionChanged FINAL)
19 Q_PROPERTY(
20 qreal minimumSize READ minimumSize WRITE setMinimumSize NOTIFY minimumSizeChanged FINAL)
21 Q_PROPERTY(qreal visualSize READ visualSize NOTIFY visualSizeChanged FINAL)
22 Q_PROPERTY(qreal visualPosition READ visualPosition NOTIFY visualPositionChanged FINAL)
23 Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged FINAL)
24 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY
25 orientationChanged FINAL)
26 Q_PROPERTY(bool horizontal READ horizontal NOTIFY orientationChanged FINAL)
27 Q_PROPERTY(bool vertical READ vertical NOTIFY orientationChanged FINAL)
28public:
29 explicit ScrollIndicator(QQuickItem* parent = nullptr);
30 static ScrollIndicatorAttached* qmlAttachedProperties(QObject*);
31 qreal size() const { return m_size; }
32 qreal position() const { return m_position; }
33 qreal minimumSize() const { return m_minimum_size; }
34 qreal visualSize() const;
35 qreal visualPosition() const;
36 bool active() const { return m_active; }
37 Qt::Orientation orientation() const { return m_orientation; }
38 bool horizontal() const { return m_orientation == Qt::Horizontal; }
39 bool vertical() const { return m_orientation == Qt::Vertical; }
40 Q_SLOT void setSize(qreal);
41 Q_SLOT void setPosition(qreal);
42 void setMinimumSize(qreal);
43 void setActive(bool);
44 void setOrientation(Qt::Orientation);
45 Q_SIGNAL void sizeChanged();
46 Q_SIGNAL void positionChanged();
47 Q_SIGNAL void minimumSizeChanged();
48 Q_SIGNAL void visualSizeChanged();
49 Q_SIGNAL void visualPositionChanged();
50 Q_SIGNAL void activeChanged();
51 Q_SIGNAL void orientationChanged();
52
53protected:
54 QRectF contentRect() const override;
55 virtual void setMoving(bool value) { setActive(value); }
56 virtual void disconnectFlickable() {}
57
58private:
59 friend class ScrollIndicatorAttached;
60 QPointF visualArea() const;
61 void updateVisualArea(const QPointF& old);
62 qreal m_size = 0;
63 qreal m_position = 0;
64 qreal m_minimum_size = 0;
65 bool m_active = false;
66 Qt::Orientation m_orientation = Qt::Vertical;
67 QPointer<ScrollIndicatorAttached> m_attachment;
68};
69
70class QML_MATERIAL_API ScrollIndicatorAttached : public QObject {
71 Q_OBJECT
72 Q_PROPERTY(ScrollIndicator* horizontal READ horizontal WRITE setHorizontal NOTIFY
73 horizontalChanged FINAL)
74 Q_PROPERTY(
75 ScrollIndicator* vertical READ vertical WRITE setVertical NOTIFY verticalChanged FINAL)
76public:
77 explicit ScrollIndicatorAttached(QObject* parent);
78 ScrollIndicatorAttached(QObject* target, QObject* parent, bool bidirectional);
79 ~ScrollIndicatorAttached() override;
80 ScrollIndicator* horizontal() const { return m_horizontal.item; }
81 ScrollIndicator* vertical() const { return m_vertical.item; }
82 void setHorizontal(ScrollIndicator*);
83 void setVertical(ScrollIndicator*);
84 Q_SIGNAL void horizontalChanged();
85 Q_SIGNAL void verticalChanged();
86
87private:
88 struct Axis {
89 QPointer<ScrollIndicator> item;
90 QList<QMetaObject::Connection> connections;
91 qreal edge = 0;
92 bool layingOut = false;
93 bool syncing = false;
94 bool syncPending = false;
95 };
96 void attach(Axis&, ScrollIndicator*, bool horizontal);
97 void detach(Axis&);
98 void layout(Axis&, bool horizontal, bool force = false);
99 void activate(Axis&, bool horizontal);
100 void sync(Axis&, bool horizontal);
101 void scroll(Axis&, bool horizontal);
102 Q_SLOT void syncHorizontal();
103 Q_SLOT void syncVertical();
104 std::unique_ptr<ScrollViewport> m_viewport;
105 QPointer<QQuickItem> m_flickable;
106 bool m_bidirectional = false;
107 Axis m_horizontal, m_vertical;
108};
109
110} // namespace qml_material
111
112QML_DECLARE_TYPEINFO(qml_material::ScrollIndicator, QML_HAS_ATTACHED_PROPERTIES)