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
|
// SPDX-License-Identifier: GPL-2.0
// Helper functions to render the stats. Includes
// QSGNode template jugglery to overcome API flaws.
#ifndef STATSHELPER_H
#define STATSHELPER_H
#include <memory>
#include <QPointF>
#include <QSGNode>
// Round positions to integer values to avoid ugly artifacts
QPointF roundPos(const QPointF &p);
// A stupid pointer class that initializes to null and can be copy
// assigned. This is for historical reasons: unique_ptrs to ChartItems
// were replaced by plain pointers. Instead of nulling the plain pointers
// in the constructors, use this. Ultimately, we might think about making
// this thing smarter, once removal of individual ChartItems is implemented.
template <typename T>
class ChartItemPtr {
friend class StatsView; // Only the stats view can create these pointers
T *ptr;
ChartItemPtr(T *ptr) : ptr(ptr)
{
}
public:
ChartItemPtr() : ptr(nullptr)
{
}
ChartItemPtr(const ChartItemPtr &p) : ptr(p.ptr)
{
}
void reset()
{
ptr = nullptr;
}
ChartItemPtr &operator=(const ChartItemPtr &p)
{
ptr = p.ptr;
return *this;
}
operator bool() const
{
return !!ptr;
}
bool operator!() const
{
return !ptr;
}
T &operator*() const
{
return *ptr;
}
T *operator->() const
{
return ptr;
}
};
// In general, we want chart items to be hideable. For example to show/hide
// labels on demand. Very sadly, the QSG API is absolutely terrible with
// respect to temporarily disabling. Instead of simply having a flag,
// a QSGNode is queried using the "isSubtreeBlocked()" virtual function(!).
//
// Not only is this a slow operation performed on every single node, it
// also is often not possible to override this function: For improved
// performance, the documentation recommends to create QSG nodes via
// QQuickWindow. This provides nodes optimized for the actual hardware.
// However, this obviously means that these nodes cannot be derived from!
//
// In that case, there are two possibilities: Add a proxy node with an
// overridden "isSubtreeBlocked()" function or remove the node from the
// scene. The former was chosen here, because it is less complex.
//
// The following slightly cryptic templates are used to unify the two
// cases: The QSGNode is generated by our own code or the QSGNode is
// obtained from QQuickWindow.
//
// The "HideableQSGNode<Node>" template augments the QSGNode "Node"
// by a "setVisible()" function and overrides "isSubtreeBlocked()"
//
// The "QSGProxyNode<Node>" template is a QSGNode with a single
// child of type "Node".
//
// Thus, if the node can be created, use:
// HideableQSGNode<NodeTypeThatCanBeCreated> node
// and if the node can only be obtained from QQuickWindow, use:
// HideableQSGNode<QSGProxyNode<NodeThatCantBeCreated>> node
// The latter should obviously be typedef-ed.
//
// Yes, that's all horrible, but if nothing else it teaches us about
// composition.
template <typename Node>
class HideableQSGNode : public Node {
bool hidden;
bool isSubtreeBlocked() const override final;
public:
template<class... Args>
HideableQSGNode(bool visible, Args&&... args);
void setVisible(bool visible);
};
template <typename Node>
class QSGProxyNode : public QSGNode {
public:
std::unique_ptr<Node> node;
QSGProxyNode(Node *node);
};
// Implementation detail of templates - move to serparate header file
template <typename Node>
QSGProxyNode<Node>::QSGProxyNode(Node *node) : node(node)
{
appendChildNode(node);
}
template <typename Node>
bool HideableQSGNode<Node>::isSubtreeBlocked() const
{
return hidden;
}
template <typename Node>
template<class... Args>
HideableQSGNode<Node>::HideableQSGNode(bool visible, Args&&... args) :
Node(std::forward<Args>(args)...),
hidden(!visible)
{
}
template <typename Node>
void HideableQSGNode<Node>::setVisible(bool visible)
{
hidden = !visible;
Node::markDirty(QSGNode::DirtySubtreeBlocked);
}
#endif
|