1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "timelinerenderstate_p.h"
27 #include <utils/qtcassert.h>
28 
29 namespace Timeline {
30 
TimelineRenderState(qint64 start,qint64 end,float scale,int numPasses)31 TimelineRenderState::TimelineRenderState(qint64 start, qint64 end, float scale, int numPasses) :
32     d_ptr(new TimelineRenderStatePrivate)
33 {
34     Q_D(TimelineRenderState);
35     d->expandedRowRoot = new QSGNode;
36     d->collapsedRowRoot = new QSGNode;
37     d->expandedOverlayRoot = new QSGNode;
38     d->collapsedOverlayRoot = new QSGNode;
39     d->start = start;
40     d->end = end;
41     d->scale = scale;
42     d->passes.resize(numPasses);
43 
44     d->expandedRowRoot->setFlag(QSGNode::OwnedByParent, false);
45     d->collapsedRowRoot->setFlag(QSGNode::OwnedByParent, false);
46     d->expandedOverlayRoot->setFlag(QSGNode::OwnedByParent, false);
47     d->collapsedOverlayRoot->setFlag(QSGNode::OwnedByParent, false);
48 }
49 
~TimelineRenderState()50 TimelineRenderState::~TimelineRenderState()
51 {
52     Q_D(TimelineRenderState);
53     delete d->expandedRowRoot;
54     delete d->collapsedRowRoot;
55     delete d->expandedOverlayRoot;
56     delete d->collapsedOverlayRoot;
57     qDeleteAll(d->passes);
58     delete d;
59 }
60 
start() const61 qint64 TimelineRenderState::start() const
62 {
63     Q_D(const TimelineRenderState);
64     return d->start;
65 }
66 
end() const67 qint64 TimelineRenderState::end() const
68 {
69     Q_D(const TimelineRenderState);
70     return d->end;
71 }
72 
scale() const73 float TimelineRenderState::scale() const
74 {
75     Q_D(const TimelineRenderState);
76     return d->scale;
77 }
78 
expandedRowRoot() const79 const QSGNode *TimelineRenderState::expandedRowRoot() const
80 {
81     Q_D(const TimelineRenderState);
82     return d->expandedRowRoot;
83 }
84 
collapsedRowRoot() const85 const QSGNode *TimelineRenderState::collapsedRowRoot() const
86 {
87     Q_D(const TimelineRenderState);
88     return d->collapsedRowRoot;
89 }
90 
expandedOverlayRoot() const91 const QSGNode *TimelineRenderState::expandedOverlayRoot() const
92 {
93     Q_D(const TimelineRenderState);
94     return d->expandedOverlayRoot;
95 }
96 
collapsedOverlayRoot() const97 const QSGNode *TimelineRenderState::collapsedOverlayRoot() const
98 {
99     Q_D(const TimelineRenderState);
100     return d->collapsedOverlayRoot;
101 }
102 
expandedRowRoot()103 QSGNode *TimelineRenderState::expandedRowRoot()
104 {
105     Q_D(TimelineRenderState);
106     return d->expandedRowRoot;
107 }
108 
collapsedRowRoot()109 QSGNode *TimelineRenderState::collapsedRowRoot()
110 {
111     Q_D(TimelineRenderState);
112     return d->collapsedRowRoot;
113 }
114 
expandedOverlayRoot()115 QSGNode *TimelineRenderState::expandedOverlayRoot()
116 {
117     Q_D(TimelineRenderState);
118     return d->expandedOverlayRoot;
119 }
120 
collapsedOverlayRoot()121 QSGNode *TimelineRenderState::collapsedOverlayRoot()
122 {
123     Q_D(TimelineRenderState);
124     return d->collapsedOverlayRoot;
125 }
126 
isEmpty() const127 bool TimelineRenderState::isEmpty() const
128 {
129     Q_D(const TimelineRenderState);
130     return d->collapsedRowRoot->childCount() == 0 && d->expandedRowRoot->childCount() == 0 &&
131             d->collapsedOverlayRoot->childCount() == 0 && d->expandedOverlayRoot->childCount() == 0;
132 }
133 
assembleNodeTree(const TimelineModel * model,int defaultRowHeight,int defaultRowOffset)134 void TimelineRenderState::assembleNodeTree(const TimelineModel *model, int defaultRowHeight,
135                                            int defaultRowOffset)
136 {
137     Q_D(TimelineRenderState);
138     QTC_ASSERT(isEmpty(), return);
139 
140     for (int pass = 0; pass < d->passes.length(); ++pass) {
141         const TimelineRenderPass::State *passState = d->passes[pass];
142         if (!passState)
143             continue;
144         if (passState->expandedOverlay())
145             d->expandedOverlayRoot->appendChildNode(passState->expandedOverlay());
146         if (passState->collapsedOverlay())
147             d->collapsedOverlayRoot->appendChildNode(passState->collapsedOverlay());
148     }
149 
150     int row = 0;
151     for (int i = 0; i < model->expandedRowCount(); ++i) {
152         QSGTransformNode *rowNode = new QSGTransformNode;
153         for (int pass = 0; pass < d->passes.length(); ++pass) {
154             const TimelineRenderPass::State *passState = d->passes[pass];
155             if (!passState)
156                 continue;
157             const QVector<QSGNode *> &rows = passState->expandedRows();
158             if (rows.length() > row) {
159                 QSGNode *rowChildNode = rows[row];
160                 if (rowChildNode)
161                     rowNode->appendChildNode(rowChildNode);
162             }
163         }
164         d->expandedRowRoot->appendChildNode(rowNode);
165         ++row;
166     }
167 
168     for (int row = 0; row < model->collapsedRowCount(); ++row) {
169         QSGTransformNode *rowNode = new QSGTransformNode;
170         QMatrix4x4 matrix;
171         matrix.translate(0, row * defaultRowOffset, 0);
172         matrix.scale(1.0, static_cast<float>(defaultRowHeight) /
173                      static_cast<float>(TimelineModel::defaultRowHeight()), 1.0);
174         rowNode->setMatrix(matrix);
175         for (int pass = 0; pass < d->passes.length(); ++pass) {
176             const TimelineRenderPass::State *passState = d->passes[pass];
177             if (!passState)
178                 continue;
179             const QVector<QSGNode *> &rows = passState->collapsedRows();
180             if (rows.length() > row) {
181                 QSGNode *rowChildNode = rows[row];
182                 if (rowChildNode)
183                     rowNode->appendChildNode(rowChildNode);
184             }
185         }
186         d->collapsedRowRoot->appendChildNode(rowNode);
187     }
188 
189     updateExpandedRowHeights(model, defaultRowHeight, defaultRowOffset);
190 }
191 
updateExpandedRowHeights(const TimelineModel * model,int defaultRowHeight,int defaultRowOffset)192 void TimelineRenderState::updateExpandedRowHeights(const TimelineModel *model, int defaultRowHeight,
193                                                    int defaultRowOffset)
194 {
195     Q_D(TimelineRenderState);
196     int row = 0;
197     qreal offset = 0;
198     for (QSGNode *rowNode = d->expandedRowRoot->firstChild(); rowNode != nullptr;
199          rowNode = rowNode->nextSibling()) {
200         qreal rowHeight = model->expandedRowHeight(row++);
201         QMatrix4x4 matrix;
202         matrix.translate(0, offset, 0);
203         matrix.scale(1, rowHeight / defaultRowHeight, 1);
204         offset += defaultRowOffset * rowHeight / defaultRowHeight;
205         static_cast<QSGTransformNode *>(rowNode)->setMatrix(matrix);
206     }
207 }
208 
finalize(QSGNode * oldNode,bool expanded,const QMatrix4x4 & transform)209 QSGTransformNode *TimelineRenderState::finalize(QSGNode *oldNode, bool expanded,
210                                                 const QMatrix4x4 &transform)
211 {
212     Q_D(TimelineRenderState);
213     QSGNode *rowNode = expanded ? d->expandedRowRoot : d->collapsedRowRoot;
214     QSGNode *overlayNode = expanded ?d->expandedOverlayRoot : d->collapsedOverlayRoot;
215 
216     QSGTransformNode *node = oldNode ? static_cast<QSGTransformNode *>(oldNode) :
217                                             new QSGTransformNode;
218     node->setMatrix(transform);
219 
220     if (node->firstChild() != rowNode || node->lastChild() != overlayNode) {
221         node->removeAllChildNodes();
222         node->appendChildNode(rowNode);
223         node->appendChildNode(overlayNode);
224     }
225     return node;
226 }
227 
passState(int i)228 TimelineRenderPass::State *TimelineRenderState::passState(int i)
229 {
230     Q_D(TimelineRenderState);
231     return d->passes[i];
232 }
233 
passState(int i) const234 const TimelineRenderPass::State *TimelineRenderState::passState(int i) const
235 {
236     Q_D(const TimelineRenderState);
237     return d->passes[i];
238 }
239 
setPassState(int i,TimelineRenderPass::State * state)240 void TimelineRenderState::setPassState(int i, TimelineRenderPass::State *state)
241 {
242     Q_D(TimelineRenderState);
243     d->passes[i] = state;
244 }
245 
246 } // namespace Timeline
247