Debian Codegraph @

utils/fgqcanvas/canvasitem.cpp

raw symbols SCIP
1 //
2 // Copyright (C) 2017 James Turner [email protected]
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 #include "canvasitem.h"
19
20 #include <QMatrix4x4>
21 #include <QSGClipNode>
22
23 class LocalTransform : public QQuickTransform
24 {
25 Q_OBJECT
26 public:
27 LocalTransform(QObject *parent) : QQuickTransform(parent) {}
28
29 void setTransform(const QMatrix4x4 &t) {
30 transform = t;
31 update();
32 }
33 void applyTo(QMatrix4x4 *matrix) const override
34 {
35 *matrix *= transform;
36 }
37 private:
38 QMatrix4x4 transform;
39 };
40
41 CanvasItem::CanvasItem(QQuickItem* pr)
42 : QQuickItem(pr)
43 , m_localTransform(new LocalTransform(this))
44 {
45 setFlag(ItemHasContents);
46 m_localTransform->prependToItem(this);
47 }
48
49 void CanvasItem::setTransform(const QMatrix4x4 &mat)
50 {
51 m_localTransform->setTransform(mat);
52 }
53
54 void CanvasItem::setClip(const QRectF &clip, ReferenceFrame rf)
55 {
56 if (m_hasClip && (clip == m_clipRect) && (rf == m_clipReferenceFrame)) {
57 return;
58 }
59
60 m_hasClip = true;
61 m_clipRect = clip;
62 m_clipReferenceFrame = rf;
63 update();
64 }
65
66 void CanvasItem::setClipReferenceFrameItem(QQuickItem *refItem)
67 {
68 m_clipReferenceFrameItem = refItem;
69 }
70
71 void CanvasItem::clearClip()
72 {
73 m_hasClip = false;
74 update();
75 }
76
77 QSGNode *CanvasItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *d)
78 {
79 QSGNode* realOldNode = oldNode;
80 QSGClipNode* oldClip = nullptr;
81
82 if (oldNode && (oldNode->type() == QSGNode::ClipNodeType)) {
83 Q_ASSERT(oldNode->childCount() == 1);
84 realOldNode = oldNode->childAtIndex(0);
85 oldClip = static_cast<QSGClipNode*>(oldNode);
86 }
87
88 QSGNode* contentNode = updateRealPaintNode(realOldNode, d);
89 if (!contentNode) {
90 return nullptr;
91 }
92
93 QSGNode* clipNode = updateClipNode(oldClip, contentNode);
94 return clipNode ? clipNode : contentNode;
95 }
96
97 QSGNode *CanvasItem::updateRealPaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *d)
98 {
99 if (oldNode) {
100 return oldNode;
101 }
102
103 return new QSGNode();
104 }
105
106 QRectF checkRectangularClip(QPointF* vertices)
107 {
108 // order is TL / BL / TR / BR to match updateRectGeometry
109 const double top = vertices[0].y();
110 const double left = vertices[0].x();
111 const double bottom = vertices[1].y();
112 const double right = vertices[2].x();
113
114 if (vertices[1].x() != left) return {};
115 if (vertices[2].y() != top) return {};
116 if ((vertices[3].x() != right) || (vertices[3].y() != bottom))
117 return {};
118
119 return QRectF(vertices[0], vertices[3]);
120 }
121
122 QSGClipNode* CanvasItem::updateClipNode(QSGClipNode* oldClipNode, QSGNode* contentNode)
123 {
124 Q_ASSERT(contentNode);
125 if (!m_hasClip) {
126 return nullptr;
127 }
128
129 QSGGeometry* clipGeometry = nullptr;
130 QSGClipNode* clipNode = oldClipNode;
131
132 if (!clipNode) {
133 clipNode = new QSGClipNode();
134 clipGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4);
135 clipGeometry->setDrawingMode(GL_TRIANGLE_STRIP);
136 clipNode->setGeometry(clipGeometry);
137 clipNode->setFlag(QSGNode::OwnsGeometry);
138 clipNode->appendChildNode(contentNode);
139 } else {
140 if (clipNode->childCount() == 1) {
141 const auto existingChild = clipNode->childAtIndex(0);
142 if (existingChild == contentNode) {
143 qInfo() << "optimise for this case!";
144 }
145 }
146
147 clipNode->removeAllChildNodes();
148 clipNode->appendChildNode(contentNode);
149 clipGeometry = clipNode->geometry();
150 Q_ASSERT(clipGeometry);
151 }
152
153 QPointF clipVertices[4],
154 inVertices[4] = {m_clipRect.topLeft(), m_clipRect.bottomLeft(),
155 m_clipRect.topRight(), m_clipRect.bottomRight()};
156 QRectF rectClip;
157
158 switch (m_clipReferenceFrame) {
159 case ReferenceFrame::GLOBAL:
160 case ReferenceFrame::PARENT:
161 Q_ASSERT(m_clipReferenceFrameItem);
162 for (int i=0; i<4; ++i) {
163 clipVertices[i] = mapFromItem(m_clipReferenceFrameItem, inVertices[i]);
164 }
165 rectClip = checkRectangularClip(clipVertices);
166 break;
167
168 case ReferenceFrame::LOCAL:
169 // local ref-frame clip is always rectangular
170 rectClip = m_clipRect;
171 for (int i=0; i<4; ++i) {
172 clipVertices[i] = inVertices[i];
173 }
174 break;
175 }
176
177 clipNode->setIsRectangular(!rectClip.isNull());
178 qInfo() << "\nobj:" << objectName();
179 if (!rectClip.isNull()) {
180 qInfo() << "have rectangular clip for:" << m_clipRect << (int) m_clipReferenceFrame << rectClip;
181 clipNode->setClipRect(rectClip);
182 } else {
183 qInfo() << "haved rotated clip" << m_clipRect << (int) m_clipReferenceFrame;
184 qInfo() << "final local clip points:" << clipVertices[0] << clipVertices[1]
185 << clipVertices[2] << clipVertices[3];
186 }
187
188 QSGGeometry::Point2D *v = clipGeometry->vertexDataAsPoint2D();
189 for (int i=0; i<4; ++i) {
190 v[i].x = clipVertices[i].x();
191 v[i].y = clipVertices[i].y();
192 }
193 clipGeometry->markVertexDataDirty();
194 clipNode->markDirty(QSGNode::DirtyGeometry);
195 return clipNode;
196 }
197
198 #include "canvasitem.moc"