QmlMaterial 0.1.0
Loading...
Searching...
No Matches
scroll_viewport.hpp
1#pragma once
2
3#include "qml_material/scrollable/flickable.hpp"
4#include <QtQuick/private/qquickflickable_p.h>
5#include <type_traits>
6
7namespace qml_material
8{
9
10class ScrollViewport {
11public:
12 explicit ScrollViewport(QObject* object)
13 : m_owned(qobject_cast<Flickable*>(object)), m_qt(qobject_cast<QQuickFlickable*>(object)) {}
14
15 QQuickItem* item() const {
16 return m_owned ? static_cast<QQuickItem*>(m_owned.data()) : m_qt.data();
17 }
18 QObject* visibleArea() const {
19 if (m_owned) return m_owned->visibleArea();
20 return m_qt ? m_qt->property("visibleArea").value<QObject*>() : nullptr;
21 }
22 bool moving(bool horizontal) const {
23 if (m_owned)
24 return horizontal ? m_owned->isMovingHorizontally() : m_owned->isMovingVertically();
25 return m_qt && (horizontal ? m_qt->isMovingHorizontally() : m_qt->isMovingVertically());
26 }
27 qreal offset(bool horizontal) const {
28 if (m_owned) return horizontal ? m_owned->contentX() : m_owned->contentY();
29 return m_qt ? (horizontal ? m_qt->contentX() : m_qt->contentY()) : 0;
30 }
31 bool pixelAligned() const {
32 return m_owned ? m_owned->pixelAligned() : m_qt && m_qt->pixelAligned();
33 }
34 void setOffset(bool horizontal, qreal position) {
35 if (m_owned) {
36 if (horizontal)
37 m_owned->setContentX(position);
38 else
39 m_owned->setContentY(position);
40 } else if (m_qt) {
41 if (horizontal)
42 m_qt->setContentX(position);
43 else
44 m_qt->setContentY(position);
45 }
46 }
47 template<typename F>
48 void observeMoving(QObject* receiver, F callback) {
49 auto connectOwner = [receiver, callback](auto* owner) {
50 using Owner = std::remove_pointer_t<decltype(owner)>;
51 QObject::connect(owner, &Owner::movingHorizontallyChanged, receiver, [callback] {
52 callback(true);
53 });
54 QObject::connect(owner, &Owner::movingVerticallyChanged, receiver, [callback] {
55 callback(false);
56 });
57 };
58 if (m_owned)
59 connectOwner(m_owned.data());
60 else if (m_qt)
61 connectOwner(m_qt.data());
62 }
63
64private:
65 QPointer<Flickable> m_owned;
66 QPointer<QQuickFlickable> m_qt;
67};
68
69} // namespace qml_material