aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/timelineeditor/timelineactions.cpp
blob: c9271e490c9471c3763996902d6e1d8ffe4e7439 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "timelineactions.h"

#include "timelineutils.h"
#include "timelineview.h"

#include <bindingproperty.h>
#include <designdocument.h>
#include <designdocumentview.h>
#include <nodelistproperty.h>
#include <nodemetainfo.h>
#include <rewritertransaction.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <variantproperty.h>
#include <qmldesignerplugin.h>
#include <qmlobjectnode.h>
#include <qmltimelinekeyframegroup.h>

namespace QmlDesigner {

TimelineActions::TimelineActions() = default;

void TimelineActions::deleteAllKeyframesForTarget(const ModelNode &targetNode,
                                                  const QmlTimeline &timeline)
{
    targetNode.view()->executeInTransaction("TimelineActions::deleteAllKeyframesForTarget", [=](){
        if (timeline.isValid()) {
            for (auto frames : timeline.keyframeGroupsForTarget(targetNode))
                frames.destroy();
        }
    });
}

void TimelineActions::insertAllKeyframesForTarget(const ModelNode &targetNode,
                                                  const QmlTimeline &timeline)
{
    targetNode.view()->executeInTransaction("TimelineActions::insertAllKeyframesForTarget", [=](){
        auto object = QmlObjectNode(targetNode);
        if (timeline.isValid() && object.isValid()) {
            for (auto frames : timeline.keyframeGroupsForTarget(targetNode)) {
                QVariant value = object.instanceValue(frames.propertyName());
                frames.setValue(value, timeline.currentKeyframe());
            }
        }

    });
}

void TimelineActions::copyAllKeyframesForTarget(const ModelNode &targetNode,
                                                const QmlTimeline &timeline)
{
    DesignDocumentView::copyModelNodes(Utils::transform(timeline.keyframeGroupsForTarget(targetNode),
                                                        &QmlTimelineKeyframeGroup::modelNode));
}

void TimelineActions::pasteKeyframesToTarget(const ModelNode &targetNode,
                                             const QmlTimeline &timeline)
{
    if (timeline.isValid()) {
        QScopedPointer<Model> pasteModel(DesignDocumentView::pasteToModel());

        if (!pasteModel)
            return;

        DesignDocumentView view;
        pasteModel->attachView(&view);

        if (!view.rootModelNode().isValid())
            return;

        ModelNode rootNode = view.rootModelNode();

        //Sanity check
        if (!QmlTimelineKeyframeGroup::checkKeyframesType(rootNode)) {
            for (const ModelNode &node : rootNode.directSubModelNodes())
                if (!QmlTimelineKeyframeGroup::checkKeyframesType(node))
                    return;
        }

        pasteModel->detachView(&view);

        targetNode.view()->model()->attachView(&view);
        view.executeInTransaction("TimelineActions::pasteKeyframesToTarget", [=, &view](){

            ModelNode nonConstTargetNode = targetNode;
            nonConstTargetNode.validId();

            if (QmlTimelineKeyframeGroup::checkKeyframesType(rootNode)) {
                /* Single selection */

                ModelNode newNode = view.insertModel(rootNode);
                QmlTimelineKeyframeGroup frames(newNode);
                frames.setTarget(targetNode);

                timeline.modelNode().defaultNodeListProperty().reparentHere(newNode);

            } else {
                /* Multi selection */
                for (const ModelNode &node : rootNode.directSubModelNodes()) {
                    ModelNode newNode = view.insertModel(node);
                    QmlTimelineKeyframeGroup frames(newNode);
                    frames.setTarget(targetNode);
                    timeline.modelNode().defaultNodeListProperty().reparentHere(newNode);
                }
            }
        });
    }
}

void TimelineActions::copyKeyframes(const QList<ModelNode> &keyframes)
{
    QList<ModelNode> nodes;
    for (const auto &node : keyframes) {
        NodeAbstractProperty pp = node.parentProperty();
        QTC_ASSERT(pp.isValid(), return );

        ModelNode parentModelNode = pp.parentModelNode();
        for (const auto &property : parentModelNode.properties()) {
            auto name = property.name();
            if (property.isBindingProperty()) {
                BindingProperty bp = property.toBindingProperty();
                ModelNode bpNode = bp.resolveToModelNode();
                if (bpNode.isValid())
                    node.setAuxiliaryData(name, bpNode.id());
            } else if (property.isVariantProperty()) {
                VariantProperty vp = property.toVariantProperty();
                node.setAuxiliaryData(name, vp.value());
            }
        }

        nodes << node;
    }

    DesignDocumentView::copyModelNodes(nodes);
}

bool isKeyframe(const ModelNode &node)
{
    return node.isValid() && node.metaInfo().isValid()
           && node.metaInfo().isSubclassOf("QtQuick.Timeline.Keyframe");
}

QVariant getValue(const ModelNode &node)
{
    if (node.isValid())
        return node.variantProperty("value").value();

    return QVariant();
}

qreal getTime(const ModelNode &node)
{
    Q_ASSERT(node.isValid());
    Q_ASSERT(node.hasProperty("frame"));

    return node.variantProperty("frame").value().toReal();
}

QmlTimelineKeyframeGroup getFrameGroup(const ModelNode &node,
                                       AbstractView *timelineView,
                                       const QmlTimeline &timeline)
{
    QVariant targetId = node.auxiliaryData("target");
    QVariant property = node.auxiliaryData("property");

    if (targetId.isValid() && property.isValid()) {
        ModelNode targetNode = timelineView->modelNodeForId(targetId.toString());
        if (targetNode.isValid()) {
            for (QmlTimelineKeyframeGroup frameGrp : timeline.keyframeGroupsForTarget(targetNode)) {
                if (frameGrp.propertyName() == property.toByteArray())
                    return frameGrp;
            }
        }
    }
    return QmlTimelineKeyframeGroup();
}

void pasteKeyframe(const qreal expectedTime,
                   const ModelNode &keyframe,
                   AbstractView *timelineView,
                   const QmlTimeline &timeline)
{
    QmlTimelineKeyframeGroup group = getFrameGroup(keyframe, timelineView, timeline);
    if (group.isValid()) {
        const qreal clampedTime = TimelineUtils::clamp(expectedTime,
                                                       timeline.startKeyframe(),
                                                       timeline.endKeyframe());

        // Create a new frame ...
        group.setValue(getValue(keyframe), clampedTime);

        // ... look it up by time ...
        for (const ModelNode &key : group.keyframePositions()) {
            qreal time = key.variantProperty("frame").value().toReal();
            if (qFuzzyCompare(clampedTime, time)) {
                // ... and transfer the properties.
                for (const auto &property : keyframe.properties()) {
                    if (property.name() == "frame" || property.name() == "value")
                        continue;

                    if (property.isVariantProperty()) {
                        auto vp = property.toVariantProperty();
                        key.variantProperty(vp.name()).setValue(vp.value());
                    } else if (property.isBindingProperty()) {
                        auto bp = property.toBindingProperty();
                        key.bindingProperty(bp.name()).setExpression(bp.expression());
                    }
                }
            }
        }
    }
}

std::vector<std::tuple<ModelNode, qreal>> getFramesRelative(const ModelNode &parent)
{
    auto byTime = [](const ModelNode &lhs, const ModelNode &rhs) {
        return getTime(lhs) < getTime(rhs);
    };

    std::vector<std::tuple<ModelNode, qreal>> result;

    QList<ModelNode> sortedByTime;
    QList<ModelNode> subs(parent.directSubModelNodes());

    std::copy_if(subs.begin(), subs.end(), std::back_inserter(sortedByTime), &isKeyframe);
    std::sort(sortedByTime.begin(), sortedByTime.end(), byTime);

    if (!sortedByTime.empty()) {
        qreal firstTime = getTime(sortedByTime.first());
        for (const ModelNode &keyframe : sortedByTime)
            result.emplace_back(keyframe, getTime(keyframe) - firstTime);
    }

    return result;
}

void TimelineActions::pasteKeyframes(AbstractView *timelineView, const QmlTimeline &timeline)
{
    QScopedPointer<Model> pasteModel(DesignDocumentView::pasteToModel());

    if (!pasteModel)
        return;

    DesignDocumentView view;
    pasteModel->attachView(&view);

    if (!view.rootModelNode().isValid())
        return;

    const qreal currentTime = timeline.currentKeyframe();

    ModelNode rootNode = view.rootModelNode();

    timelineView->executeInTransaction("TimelineActions::pasteKeyframes", [=](){
        if (isKeyframe(rootNode))
            pasteKeyframe(currentTime, rootNode, timelineView, timeline);
        else
            for (auto frame : getFramesRelative(rootNode))
                pasteKeyframe(currentTime + std::get<1>(frame),
                              std::get<0>(frame),
                              timelineView,
                              timeline);

    });
}

bool TimelineActions::clipboardContainsKeyframes()
{
    QScopedPointer<Model> pasteModel(DesignDocumentView::pasteToModel());

    if (!pasteModel)
        return false;

    DesignDocumentView view;
    pasteModel->attachView(&view);

    if (!view.rootModelNode().isValid())
        return false;

    ModelNode rootNode = view.rootModelNode();

    if (!rootNode.hasAnySubModelNodes())
        return false;

    //Sanity check
    if (!QmlTimelineKeyframeGroup::checkKeyframesType(rootNode)) {
        for (const ModelNode &node : rootNode.directSubModelNodes())
            if (!QmlTimelineKeyframeGroup::checkKeyframesType(node))
                return false;
    }

    return true;
}

} // namespace QmlDesigner