2020-09-02 13:42:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of the Qt Design Tooling
|
|
|
|
|
**
|
|
|
|
|
** 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 "curveeditorview.h"
|
|
|
|
|
#include "curveeditor.h"
|
|
|
|
|
#include "curveeditormodel.h"
|
|
|
|
|
#include "curvesegment.h"
|
2020-10-26 15:26:17 +01:00
|
|
|
#include "treeitem.h"
|
2020-09-02 13:42:32 +02:00
|
|
|
|
|
|
|
|
#include <bindingproperty.h>
|
|
|
|
|
#include <easingcurve.h>
|
|
|
|
|
#include <nodeabstractproperty.h>
|
|
|
|
|
#include <variantproperty.h>
|
|
|
|
|
#include <qmlstate.h>
|
|
|
|
|
#include <qmltimeline.h>
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
|
|
|
|
CurveEditorView::CurveEditorView(QObject *parent)
|
|
|
|
|
: AbstractView(parent)
|
|
|
|
|
, m_block(false)
|
2020-10-26 15:26:17 +01:00
|
|
|
, m_model(new CurveEditorModel())
|
|
|
|
|
, m_editor(new CurveEditor(m_model))
|
2020-09-02 13:42:32 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
2020-10-26 15:26:17 +01:00
|
|
|
connect(m_model, &CurveEditorModel::commitCurrentFrame, this, &CurveEditorView::commitCurrentFrame);
|
|
|
|
|
connect(m_model, &CurveEditorModel::commitStartFrame, this, &CurveEditorView::commitStartFrame);
|
|
|
|
|
connect(m_model, &CurveEditorModel::commitEndFrame, this, &CurveEditorView::commitEndFrame);
|
|
|
|
|
connect(m_model, &CurveEditorModel::curveChanged, this, &CurveEditorView::commitKeyframes);
|
2021-09-28 17:54:31 +02:00
|
|
|
|
|
|
|
|
connect(m_editor, &CurveEditor::viewEnabledChanged, this, [this](bool enabled){
|
|
|
|
|
setEnabled(enabled);
|
|
|
|
|
if (enabled)
|
|
|
|
|
init();
|
|
|
|
|
});
|
|
|
|
|
setEnabled(false);
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurveEditorView::~CurveEditorView() {}
|
|
|
|
|
|
|
|
|
|
bool CurveEditorView::hasWidget() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WidgetInfo CurveEditorView::widgetInfo()
|
|
|
|
|
{
|
|
|
|
|
return createWidgetInfo(
|
2022-02-23 14:10:09 +01:00
|
|
|
m_editor, nullptr, "CurveEditorId", WidgetInfo::BottomPane, 0, tr("Curve Editor"));
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::modelAttached(Model *model)
|
|
|
|
|
{
|
|
|
|
|
AbstractView::modelAttached(model);
|
|
|
|
|
|
2021-09-28 17:54:31 +02:00
|
|
|
if (isEnabled())
|
|
|
|
|
init();
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::modelAboutToBeDetached(Model *model)
|
|
|
|
|
{
|
|
|
|
|
AbstractView::modelAboutToBeDetached(model);
|
|
|
|
|
m_model->reset({});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool dirtyfiesView(const ModelNode &node)
|
|
|
|
|
{
|
|
|
|
|
return QmlTimeline::isValidQmlTimeline(node)
|
|
|
|
|
|| QmlTimelineKeyframeGroup::isValidQmlTimelineKeyframeGroup(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::nodeRemoved(const ModelNode &removedNode,
|
|
|
|
|
const NodeAbstractProperty &parentProperty,
|
|
|
|
|
PropertyChangeFlags propertyChange)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(removedNode);
|
|
|
|
|
Q_UNUSED(propertyChange);
|
|
|
|
|
|
|
|
|
|
if (!parentProperty.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ModelNode parent = parentProperty.parentModelNode();
|
|
|
|
|
if (dirtyfiesView(parent))
|
|
|
|
|
updateKeyframes();
|
2021-04-20 15:11:14 +02:00
|
|
|
|
|
|
|
|
if (!activeTimeline().isValid())
|
|
|
|
|
m_model->reset({});
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::nodeReparented(const ModelNode &node,
|
|
|
|
|
const NodeAbstractProperty &newPropertyParent,
|
|
|
|
|
const NodeAbstractProperty &oldPropertyParent,
|
|
|
|
|
PropertyChangeFlags propertyChange)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(node);
|
|
|
|
|
Q_UNUSED(newPropertyParent);
|
|
|
|
|
Q_UNUSED(oldPropertyParent);
|
|
|
|
|
Q_UNUSED(propertyChange);
|
|
|
|
|
|
|
|
|
|
ModelNode parent = newPropertyParent.parentModelNode();
|
|
|
|
|
if (newPropertyParent.isValid() && dirtyfiesView(parent))
|
|
|
|
|
updateKeyframes();
|
|
|
|
|
else if (QmlTimelineKeyframeGroup::checkKeyframesType(node))
|
|
|
|
|
updateKeyframes();
|
2021-03-24 15:31:31 +01:00
|
|
|
else if (newPropertyParent.isValid() && !oldPropertyParent.isValid()) {
|
|
|
|
|
if (activeTimeline().hasKeyframeGroupForTarget(node)) {
|
|
|
|
|
updateKeyframes();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
void CurveEditorView::auxiliaryDataChanged(const ModelNode &node,
|
|
|
|
|
const PropertyName &name,
|
|
|
|
|
const QVariant &data)
|
|
|
|
|
{
|
|
|
|
|
if (name == "locked") {
|
|
|
|
|
if (auto *item = m_model->find(node.id())) {
|
|
|
|
|
QSignalBlocker blocker(m_model);
|
|
|
|
|
m_model->setLocked(item, data.toBool());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 13:42:32 +02:00
|
|
|
void CurveEditorView::instancePropertyChanged(const QList<QPair<ModelNode, PropertyName>> &propertyList)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(propertyList);
|
|
|
|
|
|
|
|
|
|
for (const auto &pair : propertyList) {
|
|
|
|
|
if (!QmlTimeline::isValidQmlTimeline(pair.first))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (pair.second == "startFrame")
|
|
|
|
|
updateStartFrame(pair.first);
|
|
|
|
|
else if (pair.second == "endFrame")
|
|
|
|
|
updateEndFrame(pair.first);
|
|
|
|
|
else if (pair.second == "currentFrame")
|
|
|
|
|
updateCurrentFrame(pair.first);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::variantPropertiesChanged(const QList<VariantProperty> &propertyList,
|
|
|
|
|
PropertyChangeFlags propertyChange)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(propertyList);
|
|
|
|
|
Q_UNUSED(propertyChange);
|
|
|
|
|
|
|
|
|
|
for (const auto &property : propertyList) {
|
|
|
|
|
if ((property.name() == "frame" || property.name() == "value")
|
|
|
|
|
&& property.parentModelNode().type() == "QtQuick.Timeline.Keyframe"
|
|
|
|
|
&& property.parentModelNode().isValid()
|
|
|
|
|
&& property.parentModelNode().hasParentProperty()) {
|
|
|
|
|
const ModelNode framesNode = property.parentModelNode().parentProperty().parentModelNode();
|
|
|
|
|
if (QmlTimelineKeyframeGroup::isValidQmlTimelineKeyframeGroup(framesNode))
|
|
|
|
|
updateKeyframes();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::bindingPropertiesChanged(const QList<BindingProperty> &propertyList,
|
|
|
|
|
PropertyChangeFlags propertyChange)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(propertyList);
|
|
|
|
|
Q_UNUSED(propertyChange);
|
|
|
|
|
|
|
|
|
|
for (const auto &property : propertyList) {
|
|
|
|
|
if (property.name() == "easing.bezierCurve") {
|
|
|
|
|
updateKeyframes();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::propertiesRemoved(const QList<AbstractProperty> &propertyList)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(propertyList);
|
|
|
|
|
|
|
|
|
|
for (const auto &property : propertyList) {
|
|
|
|
|
if (property.name() == "keyframes" && property.parentModelNode().isValid()) {
|
|
|
|
|
ModelNode parent = property.parentModelNode();
|
|
|
|
|
if (dirtyfiesView(parent))
|
|
|
|
|
updateKeyframes();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlTimeline CurveEditorView::activeTimeline() const
|
|
|
|
|
{
|
|
|
|
|
QmlModelState state = currentState();
|
|
|
|
|
if (state.isBaseState()) {
|
|
|
|
|
for (const ModelNode &node : allModelNodesOfType("QtQuick.Timeline.Timeline")) {
|
|
|
|
|
if (QmlTimeline::isValidQmlTimeline(node)) {
|
|
|
|
|
if (node.hasVariantProperty("enabled")
|
|
|
|
|
&& node.variantProperty("enabled").value().toBool())
|
|
|
|
|
return QmlTimeline(node);
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const ModelNode &node : allModelNodesOfType("QtQuick.Timeline.Timeline")) {
|
|
|
|
|
if (QmlTimeline::isValidQmlTimeline(node) && state.affectsModelNode(node)) {
|
|
|
|
|
QmlPropertyChanges propertyChanges(state.propertyChanges(node));
|
|
|
|
|
if (!propertyChanges.isValid())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (node.hasVariantProperty("enabled") && node.variantProperty("enabled").value().toBool())
|
|
|
|
|
return QmlTimeline(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::updateKeyframes()
|
|
|
|
|
{
|
|
|
|
|
if (m_block)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QmlTimeline timeline = activeTimeline();
|
|
|
|
|
if (timeline.isValid())
|
|
|
|
|
m_model->setTimeline(timeline);
|
|
|
|
|
else
|
|
|
|
|
m_model->reset({});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::updateCurrentFrame(const ModelNode &node)
|
|
|
|
|
{
|
|
|
|
|
if (m_editor->dragging())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QmlTimeline timeline(node);
|
|
|
|
|
if (timeline.isValid())
|
|
|
|
|
m_model->setCurrentFrame(static_cast<int>(std::round(timeline.currentKeyframe())));
|
|
|
|
|
else
|
|
|
|
|
m_model->setCurrentFrame(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::updateStartFrame(const ModelNode &node)
|
|
|
|
|
{
|
|
|
|
|
QmlTimeline timeline(node);
|
|
|
|
|
if (timeline.isValid())
|
|
|
|
|
m_model->setMinimumTime(static_cast<int>(std::round(timeline.startKeyframe())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::updateEndFrame(const ModelNode &node)
|
|
|
|
|
{
|
|
|
|
|
QmlTimeline timeline(node);
|
|
|
|
|
if (timeline.isValid())
|
|
|
|
|
m_model->setMaximumTime(static_cast<int>(std::round(timeline.endKeyframe())));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
ModelNode getTargetNode(PropertyTreeItem *item, const QmlTimeline &timeline)
|
2020-09-02 13:42:32 +02:00
|
|
|
{
|
2020-10-26 15:26:17 +01:00
|
|
|
if (const NodeTreeItem *nodeItem = item->parentNodeTreeItem()) {
|
2020-09-02 13:42:32 +02:00
|
|
|
QString targetId = nodeItem->name();
|
|
|
|
|
if (timeline.isValid()) {
|
|
|
|
|
for (auto &&target : timeline.allTargets()) {
|
2020-10-16 16:45:44 +02:00
|
|
|
if (target.isValid() && target.displayName() == targetId)
|
2020-09-02 13:42:32 +02:00
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ModelNode();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
QmlTimelineKeyframeGroup timelineKeyframeGroup(QmlTimeline &timeline, PropertyTreeItem *item)
|
2020-09-02 13:42:32 +02:00
|
|
|
{
|
2020-10-26 15:26:17 +01:00
|
|
|
ModelNode node = getTargetNode(item, timeline);
|
2020-09-02 13:42:32 +02:00
|
|
|
if (node.isValid())
|
|
|
|
|
return timeline.keyframeGroup(node, item->name().toLatin1());
|
|
|
|
|
|
|
|
|
|
return QmlTimelineKeyframeGroup();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
void attachEasingCurve(const QmlTimelineKeyframeGroup &group, double frame, const QEasingCurve &curve)
|
2020-09-02 13:42:32 +02:00
|
|
|
{
|
|
|
|
|
ModelNode frameNode = group.keyframe(frame);
|
|
|
|
|
if (frameNode.isValid()) {
|
|
|
|
|
auto expression = EasingCurve(curve).toString();
|
|
|
|
|
frameNode.bindingProperty("easing.bezierCurve").setExpression(expression);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
void commitAuxiliaryData(ModelNode &node, TreeItem *item)
|
2020-09-02 13:42:32 +02:00
|
|
|
{
|
2020-10-26 15:26:17 +01:00
|
|
|
if (node.isValid()) {
|
|
|
|
|
if (item->locked())
|
|
|
|
|
node.setAuxiliaryData("locked", true);
|
|
|
|
|
else
|
|
|
|
|
node.removeAuxiliaryData("locked");
|
|
|
|
|
|
|
|
|
|
if (item->pinned())
|
|
|
|
|
node.setAuxiliaryData("pinned", true);
|
|
|
|
|
else
|
|
|
|
|
node.removeAuxiliaryData("pinned");
|
|
|
|
|
|
|
|
|
|
if (auto *pitem = item->asPropertyItem()) {
|
|
|
|
|
if (pitem->hasUnified())
|
|
|
|
|
node.setAuxiliaryData("unified", pitem->unifyString());
|
2020-09-02 13:42:32 +02:00
|
|
|
else
|
2020-10-26 15:26:17 +01:00
|
|
|
node.removeAuxiliaryData("unified");
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
2020-10-26 15:26:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-02 13:42:32 +02:00
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
void CurveEditorView::commitKeyframes(TreeItem *item)
|
|
|
|
|
{
|
|
|
|
|
if (auto *nitem = item->asNodeItem()) {
|
|
|
|
|
ModelNode node = modelNodeForId(nitem->name());
|
|
|
|
|
commitAuxiliaryData(node, item);
|
|
|
|
|
|
|
|
|
|
} else if (auto *pitem = item->asPropertyItem()) {
|
|
|
|
|
QmlTimeline currentTimeline = activeTimeline();
|
|
|
|
|
QmlTimelineKeyframeGroup group = timelineKeyframeGroup(currentTimeline, pitem);
|
|
|
|
|
|
|
|
|
|
if (group.isValid()) {
|
|
|
|
|
ModelNode groupNode = group.modelNode();
|
|
|
|
|
commitAuxiliaryData(groupNode, item);
|
|
|
|
|
|
|
|
|
|
auto replaceKeyframes = [&group, pitem, this]() {
|
|
|
|
|
m_block = true;
|
|
|
|
|
for (auto frame : group.keyframes())
|
|
|
|
|
frame.destroy();
|
|
|
|
|
|
|
|
|
|
Keyframe previous;
|
|
|
|
|
for (auto &&frame : pitem->curve().keyframes()) {
|
|
|
|
|
QPointF pos = frame.position();
|
|
|
|
|
group.setValue(QVariant(pos.y()), pos.x());
|
|
|
|
|
|
|
|
|
|
if (previous.isValid()) {
|
2021-03-02 12:26:40 +01:00
|
|
|
if (frame.interpolation() == Keyframe::Interpolation::Bezier ||
|
|
|
|
|
frame.interpolation() == Keyframe::Interpolation::Step ) {
|
2020-10-26 15:26:17 +01:00
|
|
|
CurveSegment segment(previous, frame);
|
|
|
|
|
if (segment.isValid())
|
|
|
|
|
attachEasingCurve(group, pos.x(), segment.easingCurve());
|
|
|
|
|
} else if (frame.interpolation() == Keyframe::Interpolation::Easing) {
|
|
|
|
|
QVariant data = frame.data();
|
|
|
|
|
if (data.type() == static_cast<int>(QMetaType::QEasingCurve))
|
|
|
|
|
attachEasingCurve(group, pos.x(), data.value<QEasingCurve>());
|
|
|
|
|
}
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
previous = frame;
|
|
|
|
|
}
|
|
|
|
|
m_block = false;
|
|
|
|
|
};
|
2020-09-02 13:42:32 +02:00
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
executeInTransaction("CurveEditor::commitKeyframes", replaceKeyframes);
|
|
|
|
|
}
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::commitCurrentFrame(int frame)
|
|
|
|
|
{
|
|
|
|
|
QmlTimeline timeline = activeTimeline();
|
2021-03-17 14:56:01 +01:00
|
|
|
if (timeline.isValid())
|
|
|
|
|
timeline.modelNode().setAuxiliaryData("currentFrame@NodeInstance", frame);
|
2020-09-02 13:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::commitStartFrame(int frame)
|
|
|
|
|
{
|
|
|
|
|
QmlTimeline timeline = activeTimeline();
|
|
|
|
|
if (timeline.isValid())
|
|
|
|
|
timeline.modelNode().variantProperty("startFrame").setValue(frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CurveEditorView::commitEndFrame(int frame)
|
|
|
|
|
{
|
|
|
|
|
QmlTimeline timeline = activeTimeline();
|
|
|
|
|
if (timeline.isValid())
|
|
|
|
|
timeline.modelNode().variantProperty("endFrame").setValue(frame);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 17:54:31 +02:00
|
|
|
void CurveEditorView::init()
|
|
|
|
|
{
|
|
|
|
|
QmlTimeline timeline = activeTimeline();
|
|
|
|
|
if (timeline.isValid()) {
|
|
|
|
|
m_model->setTimeline(timeline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 13:42:32 +02:00
|
|
|
} // namespace QmlDesigner
|