reworked much of keyframeview for new architecture

This commit is contained in:
itsmattkc
2021-12-13 14:12:30 -08:00
parent 090fbb09d0
commit 7d82d56f31
25 changed files with 852 additions and 754 deletions
+58 -96
View File
@@ -47,22 +47,6 @@ CurveView::CurveView(QWidget *parent) :
connect(scene(), &QGraphicsScene::selectionChanged, this, &CurveView::SelectionChanged);
}
CurveView::~CurveView()
{
// Quick way to avoid segfault when QGraphicsScene::selectionChanged is emitted after other members have been destroyed
Clear();
}
void CurveView::Clear()
{
KeyframeViewBase::Clear();
foreach (QGraphicsLineItem* line, lines_) {
delete line;
}
lines_.clear();
}
void CurveView::ConnectInput(const NodeKeyframeTrackReference& ref)
{
if (connected_inputs_.contains(ref)) {
@@ -71,7 +55,9 @@ void CurveView::ConnectInput(const NodeKeyframeTrackReference& ref)
}
// Add keyframes from track
AddKeyframesOfTrack(ref);
KeyframeViewInputConnection *track_con = AddKeyframesOfTrack(ref);
track_con->SetBrush(keyframe_colors_.value(ref));
track_connections_.insert(ref, track_con);
// Append to the list
connected_inputs_.append(ref);
@@ -85,7 +71,7 @@ void CurveView::DisconnectInput(const NodeKeyframeTrackReference& ref)
}
// Remove keyframes belonging to this element and track
RemoveKeyframesOfTrack(ref);
RemoveKeyframesOfTrack(track_connections_.take(ref));
// Remove from the list
connected_inputs_.removeOne(ref);
@@ -95,24 +81,16 @@ void CurveView::SelectKeyframesOfInput(const NodeKeyframeTrackReference& ref)
{
DeselectAll();
for (auto it=item_map().cbegin(); it!=item_map().cend(); it++) {
if (it.key()->key_track_ref() == ref) {
it.value()->setSelected(true);
foreach (KeyframeViewInputConnection *con, track_connections_) {
foreach (NodeKeyframe *key, con->GetKeyframes()) {
SelectKeyframe(key);
}
}
}
void CurveView::ZoomToFitInput(const NodeKeyframeTrackReference& ref)
{
QList<NodeKeyframe*> keys;
for (auto it=item_map().cbegin(); it!=item_map().cend(); it++) {
if (it.key()->key_track_ref() == ref) {
keys.append(it.key());
}
}
ZoomToFitInternal(keys);
ZoomToFitInternal(track_connections_.value(ref)->GetKeyframes());
}
void CurveView::SetKeyframeTrackColor(const NodeKeyframeTrackReference &ref, const QColor &color)
@@ -121,11 +99,7 @@ void CurveView::SetKeyframeTrackColor(const NodeKeyframeTrackReference &ref, con
keyframe_colors_.insert(ref, color);
// Update all keyframes
for (auto it=item_map().cbegin(); it!=item_map().cend(); it++) {
if (it.key()->key_track_ref() == ref) {
it.value()->SetOverrideBrush(color);
}
}
track_connections_.value(ref)->SetBrush(color);
}
void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
@@ -196,7 +170,7 @@ void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
QPainterPath path;
// Draw straight line leading to first keyframe
QPointF first_key_pos = item_map().value(track.first())->pos();
QPointF first_key_pos = GetKeyframePosition(track.first());
path.moveTo(QPointF(scene_bottom_left.x(), first_key_pos.y()));
path.lineTo(first_key_pos);
@@ -205,24 +179,24 @@ void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
NodeKeyframe* before = track.at(i-1);
NodeKeyframe* after = track.at(i);
KeyframeViewItem* before_item = item_map().value(before);
KeyframeViewItem* after_item = item_map().value(after);
QPointF before_pos = GetKeyframePosition(before);
QPointF after_pos = GetKeyframePosition(after);
if (before->type() == NodeKeyframe::kHold) {
// Draw a hold keyframe (basically a right angle)
path.lineTo(after_item->pos().x(), before_item->pos().y());
path.lineTo(after_item->pos().x(), after_item->pos().y());
path.lineTo(after_pos.x(), before_pos.y());
path.lineTo(after_pos.x(), after_pos.y());
} else if (before->type() == NodeKeyframe::kBezier && after->type() == NodeKeyframe::kBezier) {
// Draw a cubic bezier
// Cubic beziers have two control points, so we can just use both
QPointF before_control_point = before_item->pos() + ScalePoint(before->valid_bezier_control_out());
QPointF after_control_point = after_item->pos() + ScalePoint(after->valid_bezier_control_in());
QPointF before_control_point = before_pos + ScalePoint(before->valid_bezier_control_out());
QPointF after_control_point = after_pos + ScalePoint(after->valid_bezier_control_in());
path.cubicTo(before_control_point, after_control_point, after_item->pos());
path.cubicTo(before_control_point, after_control_point, after_pos);
} else if (before->type() == NodeKeyframe::kBezier || after->type() == NodeKeyframe::kBezier) {
// Draw a quadratic bezier
@@ -232,10 +206,10 @@ void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
QPointF control_point;
if (before->type() == NodeKeyframe::kBezier) {
key_anchor = before_item->pos();
key_anchor = before_pos;
control_point = before->valid_bezier_control_out();
} else {
key_anchor = after_item->pos();
key_anchor = after_pos;
control_point = after->valid_bezier_control_in();
}
@@ -243,18 +217,18 @@ void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
control_point = key_anchor + ScalePoint(control_point);
// Create the path from both keyframes
path.quadTo(control_point, after_item->pos());
path.quadTo(control_point, after_pos);
} else {
// Linear to linear
path.lineTo(after_item->pos());
path.lineTo(after_pos);
}
}
// Draw straight line leading from end keyframe
QPointF last_key_pos = item_map().value(track.last())->pos();
QPointF last_key_pos = GetKeyframePosition(track.last());
path.lineTo(QPointF(scene_top_right.x(), last_key_pos.y()));
painter->drawPath(path);
@@ -263,7 +237,7 @@ void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
}
// Draw bezier control point lines
if (!bezier_control_points_.isEmpty()) {
/*if (!bezier_control_points_.isEmpty()) {
painter->setPen(QPen(palette().text().color(), 1));
QVector<QLineF> bezier_lines;
@@ -274,13 +248,7 @@ void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
bezier_lines.append(QLineF(par->pos(), par->pos() + item->pos()));
}
painter->drawLines(bezier_lines);
}
}
void CurveView::KeyframeAboutToBeRemoved(NodeKeyframe *key)
{
disconnect(key, &NodeKeyframe::ValueChanged, this, &CurveView::KeyframeValueChanged);
disconnect(key, &NodeKeyframe::TypeChanged, this, &CurveView::KeyframeTypeChanged);
}*/
}
void CurveView::ScaleChangedEvent(const double& scale)
@@ -296,13 +264,11 @@ void CurveView::VerticalScaleChangedEvent(double scale)
{
Q_UNUSED(scale)
for (auto iterator=item_map().begin();iterator!=item_map().end();iterator++) {
SetItemYFromKeyframeValue(iterator.value()->key(), iterator.value());
}
foreach (BezierControlPointItem* item, bezier_control_points_) {
item->SetYScale(scale);
}
viewport()->update();
}
void CurveView::ContextMenuEvent(Menu &m)
@@ -324,7 +290,7 @@ void CurveView::SceneRectUpdateEvent(QRectF &r)
r.setBottom(r.bottom() + this->height());
}
void CurveView::ZoomToFitInternal(const QList<NodeKeyframe *> &keys)
void CurveView::ZoomToFitInternal(const QVector<NodeKeyframe *> &keys)
{
if (keys.isEmpty()) {
// Prevent scaling to DBL_MIN/DBL_MAX
@@ -373,53 +339,58 @@ qreal CurveView::GetItemYFromKeyframeValue(double value)
return -value * GetYScale();
}
void CurveView::SetItemYFromKeyframeValue(NodeKeyframe *key, KeyframeViewItem *item)
{
item->SetOverrideY(GetItemYFromKeyframeValue(key));
}
QPointF CurveView::ScalePoint(const QPointF &point)
{
// Flips Y coordinate because curves are drawn bottom to top
return QPointF(point.x() * GetScale(), - point.y() * GetYScale());
}
void CurveView::CreateBezierControlPoints(KeyframeViewItem* item)
void CurveView::CreateBezierControlPoints(NodeKeyframe* item)
{
BezierControlPointItem* bezier_in_pt = new BezierControlPointItem(item->key(), NodeKeyframe::kInHandle, item);
qDebug() << "STUB!";
/*BezierControlPointItem* bezier_in_pt = new BezierControlPointItem(item, NodeKeyframe::kInHandle, item);
bezier_in_pt->SetXScale(GetScale());
bezier_in_pt->SetYScale(GetYScale());
bezier_control_points_.append(bezier_in_pt);
connect(bezier_in_pt, &QObject::destroyed, this, &CurveView::BezierControlPointDestroyed, Qt::DirectConnection);
BezierControlPointItem* bezier_out_pt = new BezierControlPointItem(item->key(), NodeKeyframe::kOutHandle, item);
BezierControlPointItem* bezier_out_pt = new BezierControlPointItem(item, NodeKeyframe::kOutHandle, item);
bezier_out_pt->SetXScale(GetScale());
bezier_out_pt->SetYScale(GetYScale());
bezier_control_points_.append(bezier_out_pt);
connect(bezier_out_pt, &QObject::destroyed, this, &CurveView::BezierControlPointDestroyed, Qt::DirectConnection);
connect(bezier_out_pt, &QObject::destroyed, this, &CurveView::BezierControlPointDestroyed, Qt::DirectConnection);*/
}
QPointF CurveView::GetKeyframePosition(NodeKeyframe *key)
{
return QPointF(GetKeyframeSceneX(key), GetItemYFromKeyframeValue(key));
}
void CurveView::KeyframeValueChanged()
{
NodeKeyframe* key = static_cast<NodeKeyframe*>(sender());
qDebug() << "STUB!";
/*NodeKeyframe* key = static_cast<NodeKeyframe*>(sender());
KeyframeViewItem* item = item_map().value(key);
SetItemYFromKeyframeValue(key, item);
SetItemYFromKeyframeValue(key, item);*/
}
void CurveView::KeyframeTypeChanged()
{
NodeKeyframe* key = static_cast<NodeKeyframe*>(sender());
qDebug() << "STUB!";
/*NodeKeyframe* key = static_cast<NodeKeyframe*>(sender());
KeyframeViewItem* item = item_map().value(key);
if (item->isSelected()) {
item->setSelected(false);
item->setSelected(true);
}
}*/
}
void CurveView::SelectionChanged()
{
qDebug() << "STUB!";
/*
// Clear current bezier handles
while (!bezier_control_points_.isEmpty()) {
delete bezier_control_points_.first();
@@ -434,6 +405,7 @@ void CurveView::SelectionChanged()
CreateBezierControlPoints(this_item);
}
}
*/
}
void CurveView::BezierControlPointDestroyed()
@@ -444,20 +416,22 @@ void CurveView::BezierControlPointDestroyed()
void CurveView::ZoomToFit()
{
ZoomToFitInternal(item_map().keys());
QVector<NodeKeyframe*> keys;
foreach (KeyframeViewInputConnection *con, track_connections_) {
foreach (NodeKeyframe *k, con->GetKeyframes()) {
if (!keys.contains(k)) {
keys.append(k);
}
}
}
ZoomToFitInternal(keys);
}
void CurveView::ZoomToFitSelected()
{
QList<NodeKeyframe*> selected_keys;
for (auto it=item_map().cbegin(); it!=item_map().cend(); it++) {
if (it.value()->isSelected()) {
selected_keys.append(it.key());
}
}
ZoomToFitInternal(selected_keys);
ZoomToFitInternal(GetSelectedKeyframes());
}
void CurveView::ResetZoom()
@@ -466,16 +440,4 @@ void CurveView::ResetZoom()
SetYScale(1.0);
}
KeyframeViewItem* CurveView::AddKeyframe(NodeKeyframe* key)
{
KeyframeViewItem* item = super::AddKeyframe(key);
SetItemYFromKeyframeValue(key, item);
item->SetOverrideBrush(keyframe_colors_.value(key->key_track_ref()));
connect(key, &NodeKeyframe::ValueChanged, this, &CurveView::KeyframeValueChanged);
connect(key, &NodeKeyframe::TypeChanged, this, &CurveView::KeyframeTypeChanged);
return item;
}
}
+5 -13
View File
@@ -24,7 +24,6 @@
#include "beziercontrolpointitem.h"
#include "node/keyframe.h"
#include "widget/keyframeview/keyframeview.h"
#include "widget/keyframeview/keyframeviewitem.h"
namespace olive {
@@ -34,10 +33,6 @@ class CurveView : public KeyframeViewBase
public:
CurveView(QWidget* parent = nullptr);
virtual ~CurveView() override;
virtual void Clear() override;
void ConnectInput(const NodeKeyframeTrackReference &ref);
void DisconnectInput(const NodeKeyframeTrackReference &ref);
@@ -49,8 +44,6 @@ public:
void SetKeyframeTrackColor(const NodeKeyframeTrackReference& ref, const QColor& color);
public slots:
virtual KeyframeViewItem* AddKeyframe(NodeKeyframe* key) override;
void ZoomToFit();
void ZoomToFitSelected();
@@ -60,8 +53,6 @@ public slots:
protected:
virtual void drawBackground(QPainter* painter, const QRectF& rect) override;
virtual void KeyframeAboutToBeRemoved(NodeKeyframe *key) override;
virtual void ScaleChangedEvent(const double &scale) override;
virtual void VerticalScaleChangedEvent(double scale) override;
@@ -71,20 +62,21 @@ protected:
virtual void SceneRectUpdateEvent(QRectF &r) override;
private:
void ZoomToFitInternal(const QList<NodeKeyframe *> &keys);
void ZoomToFitInternal(const QVector<NodeKeyframe *> &keys);
qreal GetItemYFromKeyframeValue(NodeKeyframe* key);
qreal GetItemYFromKeyframeValue(double value);
void SetItemYFromKeyframeValue(NodeKeyframe* key, KeyframeViewItem* item);
QPointF ScalePoint(const QPointF& point);
void AdjustLines();
void CreateBezierControlPoints(KeyframeViewItem *item);
void CreateBezierControlPoints(NodeKeyframe *item);
QPointF GetKeyframePosition(NodeKeyframe *key);
QHash<NodeKeyframeTrackReference, QColor> keyframe_colors_;
QHash<NodeKeyframeTrackReference, KeyframeViewInputConnection*> track_connections_;
int text_padding_;
+8 -35
View File
@@ -113,12 +113,6 @@ CurveWidget::CurveWidget(QWidget *parent) :
SetScale(120.0);
}
CurveWidget::~CurveWidget()
{
// Quick way to avoid segfault when QGraphicsScene::selectionChanged is emitted after other members have been destroyed
view_->Clear();
}
const double &CurveWidget::GetVerticalScale()
{
return view_->GetYScale();
@@ -223,15 +217,6 @@ void CurveWidget::ConnectNode(Node *node, bool connect)
ConnectInput(node, input, connect);
}
}
// Connect add/remove signals
if (connect) {
QObject::connect(node, &Node::KeyframeAdded, this, &CurveWidget::AddKeyframe);
QObject::connect(node, &Node::KeyframeRemoved, this, &CurveWidget::RemoveKeyframe);
} else {
QObject::disconnect(node, &Node::KeyframeAdded, this, &CurveWidget::AddKeyframe);
QObject::disconnect(node, &Node::KeyframeRemoved, this, &CurveWidget::RemoveKeyframe);
}
}
void CurveWidget::ConnectInput(Node *node, const QString &input, bool connect)
@@ -287,20 +272,20 @@ void CurveWidget::ConnectInput(Node *node, const QString &input, bool connect)
void CurveWidget::SelectionChanged()
{
QList<QGraphicsItem*> selected = view_->scene()->selectedItems();
const QVector<NodeKeyframe*> &selected = view_->GetSelectedKeyframes();
SetKeyframeButtonChecked(false);
SetKeyframeButtonEnabled(!selected.isEmpty());
if (!selected.isEmpty()) {
bool all_same_type = true;
NodeKeyframe::Type type = static_cast<KeyframeViewItem*>(selected.first())->key()->type();
NodeKeyframe::Type type = selected.first()->type();
for (int i=1;i<selected.size();i++) {
KeyframeViewItem* prev_item = static_cast<KeyframeViewItem*>(selected.at(i-1));
KeyframeViewItem* this_item = static_cast<KeyframeViewItem*>(selected.at(i));
NodeKeyframe* prev_item = selected.at(i-1);
NodeKeyframe* this_item = selected.at(i);
if (prev_item->key()->type() != this_item->key()->type()) {
if (prev_item->type() != this_item->type()) {
all_same_type = false;
break;
}
@@ -323,7 +308,7 @@ void CurveWidget::KeyframeTypeButtonTriggered(bool checked)
}
// Get selected items and do nothing if there are none
QList<QGraphicsItem*> selected = view_->scene()->selectedItems();
const QVector<NodeKeyframe*> &selected = view_->GetSelectedKeyframes();
if (selected.isEmpty()) {
return;
}
@@ -345,10 +330,8 @@ void CurveWidget::KeyframeTypeButtonTriggered(bool checked)
MultiUndoCommand* command = new MultiUndoCommand();
foreach (QGraphicsItem* item, selected) {
KeyframeViewItem* key_item = static_cast<KeyframeViewItem*>(item);
command->add_child(new KeyframeSetTypeCommand(key_item->key(), new_type));
foreach (NodeKeyframe* item, selected) {
command->add_child(new KeyframeSetTypeCommand(item, new_type));
}
Core::instance()->undo_stack()->push(command);
@@ -368,16 +351,6 @@ void CurveWidget::InputEnabledChanged(const NodeKeyframeTrackReference& ref, boo
}
}
void CurveWidget::AddKeyframe(NodeKeyframe *key)
{
view_->AddKeyframe(key);
}
void CurveWidget::RemoveKeyframe(NodeKeyframe *key)
{
view_->RemoveKeyframe(key);
}
void CurveWidget::InputSelectionChanged(const NodeKeyframeTrackReference& ref)
{
key_control_->SetInput(ref.input());
-6
View File
@@ -40,8 +40,6 @@ class CurveWidget : public TimeBasedWidget, public TimeTargetObject
public:
CurveWidget(QWidget* parent = nullptr);
virtual ~CurveWidget() override;
const double& GetVerticalScale();
void SetVerticalScale(const double& vscale);
@@ -107,10 +105,6 @@ private slots:
void InputEnabledChanged(const NodeKeyframeTrackReference &ref, bool e);
void AddKeyframe(NodeKeyframe* key);
void RemoveKeyframe(NodeKeyframe* key);
void InputSelectionChanged(const NodeKeyframeTrackReference& ref);
void InputDoubleClicked(const NodeKeyframeTrackReference& ref);