Automated with clang-tidy readability-identifier-naming (config added to .clang-tidy) plus scripted passes, per the updated rules now documented in CONTRIBUTING.md: - types (class/struct/enum/alias/template params): PascalCase - functions, variables, members: snake_case (incl. rational -> Rational) - private/protected members: trailing underscore; static member variables likewise (instance_, available_themes_) - constants and enum values: snake_case (kLinear -> k_linear, F32P -> f32p); ALL_CAPS reserved for macros - macros: OAK_ prefix (OLIVE_ADD_TEST/OLIVE_ASSERT/OLIVE_CONFIG -> OAK_ADD_TEST/OAK_ASSERT/OAK_CONFIG, GL_PREAMBLE -> OAK_GL_PREAMBLE, include guards -> OAK_*) - file names: all lowercase (Current/Plugin/OliveHost/OliveClip/ OlivePluginInstance -> current/plugin/olivehost/oliveclip/ oliveplugininstance) - getters share the member name sans underscore, setters set_foo() - Qt and third-party (OpenFX) virtual overrides and framework callbacks keep their original names (exempt in .clang-tidy) Manual follow-ups required where automation could not reach: - string-based QMetaObject/SIGNAL/SLOT references updated to renamed methods (AddTask, CreatedFile, DeleteSpecificFile, moveSelectionUp, ...) - macro bodies referencing renamed methods (OLIVE_CONFIG, NODE_DEFAULT_DESTRUCTOR, MANAGEDDISPLAYWIDGET_*) - self-shadowing locals renamed where signals/methods became same-named (size_changed, worker_count, selected_items, import param, filters) - third_party OFX member/namespace usages restored (OFX::Host::*, _created, _clipPrefsDirty, createInstance, clearPersistentMessage) - STL protocol aliases restored (const_iterator) with .clang-tidy ignore rules; qHash overloads restored Full build and test suite pass: ctest 4/4, ~1960 gtest cases green.
710 lines
20 KiB
C++
710 lines
20 KiB
C++
/***
|
|
|
|
Olive - Non-Linear Video Editor
|
|
Copyright (C) 2022 Olive Team
|
|
Modifications Copyright (C) 2025 mikesolar
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
#include "curveview.h"
|
|
|
|
#include <cfloat>
|
|
#include <QHash>
|
|
#include <QMouseEvent>
|
|
#include <QPainterPath>
|
|
#include <QScrollBar>
|
|
#include <QtMath>
|
|
|
|
#include "common/decibel.h"
|
|
#include "common/qtutils.h"
|
|
#include "node/nodeundo.h"
|
|
#include "widget/keyframeview/keyframeviewundo.h"
|
|
|
|
namespace olive
|
|
{
|
|
|
|
#define super KeyframeView
|
|
|
|
CurveView::CurveView(QWidget *parent)
|
|
: KeyframeView(parent)
|
|
, dragging_bezier_pt_(nullptr)
|
|
{
|
|
setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
|
set_y_axis_enabled(true);
|
|
set_auto_select_siblings(false);
|
|
|
|
text_padding_ =
|
|
QtUtils::q_font_metrics_width(fontMetrics(), QStringLiteral("i"));
|
|
|
|
minimum_grid_space_ =
|
|
QtUtils::q_font_metrics_width(fontMetrics(), QStringLiteral("00000"));
|
|
}
|
|
|
|
void CurveView::connect_input(const NodeKeyframeTrackReference &ref)
|
|
{
|
|
if (connected_inputs_.contains(ref)) {
|
|
// Input wasn't connected, do nothing
|
|
return;
|
|
}
|
|
|
|
// Add keyframes from track
|
|
KeyframeViewInputConnection *track_con = add_keyframes_of_track(ref);
|
|
track_con->set_brush(keyframe_colors_.value(ref));
|
|
track_connections_.insert(ref, track_con);
|
|
|
|
// Signal to CurveWidget to update its bezier/linear/hold buttons if a key type changes
|
|
connect(track_con, &KeyframeViewInputConnection::type_changed, this,
|
|
&CurveView::selection_changed);
|
|
|
|
// Append to the list
|
|
connected_inputs_.append(ref);
|
|
}
|
|
|
|
void CurveView::disconnect_input(const NodeKeyframeTrackReference &ref)
|
|
{
|
|
if (!connected_inputs_.contains(ref)) {
|
|
// Input wasn't connected, do nothing
|
|
return;
|
|
}
|
|
|
|
// Remove keyframes belonging to this element and track
|
|
remove_keyframes_of_track(track_connections_.take(ref));
|
|
|
|
// Remove from the list
|
|
connected_inputs_.removeOne(ref);
|
|
}
|
|
|
|
void CurveView::select_keyframes_of_input(const NodeKeyframeTrackReference &ref)
|
|
{
|
|
deselect_all();
|
|
|
|
if (KeyframeViewInputConnection *con = track_connections_.value(ref)) {
|
|
foreach (NodeKeyframe *key, con->get_keyframes()) {
|
|
select_keyframe(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
void CurveView::set_keyframe_track_color(const NodeKeyframeTrackReference &ref,
|
|
const QColor &color)
|
|
{
|
|
// Insert color into hashmap
|
|
keyframe_colors_.insert(ref, color);
|
|
|
|
if (KeyframeViewInputConnection *con = track_connections_.value(ref)) {
|
|
// Update all keyframes
|
|
con->set_brush(color);
|
|
}
|
|
}
|
|
|
|
void CurveView::drawBackground(QPainter *painter, const QRectF &rect)
|
|
{
|
|
if (timebase().isNull()) {
|
|
return;
|
|
}
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
QVector<QLine> lines;
|
|
|
|
double x_interval = timebase().flipped().to_double();
|
|
double y_interval = 100.0;
|
|
|
|
int x_grid_interval, y_grid_interval;
|
|
|
|
painter->setPen(QPen(palette().window().color(), 1));
|
|
|
|
do {
|
|
x_grid_interval = qRound(x_interval * get_scale() * timebase_dbl());
|
|
x_interval *= 2.0;
|
|
} while (x_grid_interval < minimum_grid_space_);
|
|
|
|
do {
|
|
y_grid_interval = qRound(y_interval * get_y_scale());
|
|
y_interval *= 2.0;
|
|
} while (y_grid_interval < minimum_grid_space_);
|
|
|
|
int x_start = qCeil(rect.left() / x_grid_interval) * x_grid_interval;
|
|
int y_start = qCeil(rect.top() / y_grid_interval) * y_grid_interval;
|
|
|
|
QPointF scene_bottom_left = mapToScene(QPoint(0, qRound(rect.height())));
|
|
QPointF scene_top_right = mapToScene(QPoint(qRound(rect.width()), 0));
|
|
|
|
// Add vertical lines
|
|
for (int i = x_start; i < rect.right(); i += x_grid_interval) {
|
|
int value =
|
|
qRound(static_cast<double>(i) / get_scale() / timebase_dbl());
|
|
painter->drawText(i + text_padding_,
|
|
qRound(scene_bottom_left.y()) - text_padding_,
|
|
QString::number(value));
|
|
lines.append(QLine(i, qRound(rect.top()), i, qRound(rect.bottom())));
|
|
}
|
|
|
|
// Add horizontal lines
|
|
for (int i = y_start; i < rect.bottom(); i += y_grid_interval) {
|
|
int value = qRound(static_cast<double>(i) / get_y_scale());
|
|
painter->drawText(qRound(scene_bottom_left.x()) + text_padding_,
|
|
i - text_padding_, QString::number(-value));
|
|
lines.append(QLine(qRound(rect.left()), i, qRound(rect.right()), i));
|
|
}
|
|
|
|
// Draw grid
|
|
painter->drawLines(lines);
|
|
|
|
// Draw keyframe lines
|
|
foreach (const NodeKeyframeTrackReference &ref, connected_inputs_) {
|
|
Node *node = ref.input().node();
|
|
const QString &input = ref.input().input();
|
|
|
|
if (node->is_input_keyframing(input, ref.input().element())) {
|
|
const QVector<NodeKeyframeTrack> &tracks =
|
|
node->get_keyframe_tracks(ref.input());
|
|
|
|
const NodeKeyframeTrack &track = tracks.at(ref.track());
|
|
|
|
if (!track.isEmpty()) {
|
|
painter->setPen(QPen(keyframe_colors_.value(ref),
|
|
qMax(1, fontMetrics().height() / 4)));
|
|
|
|
// Create a path
|
|
QPainterPath path;
|
|
|
|
// Draw straight line leading to first keyframe
|
|
QPointF first_key_pos = get_keyframe_position(track.first());
|
|
path.moveTo(QPointF(scene_bottom_left.x(), first_key_pos.y()));
|
|
path.lineTo(first_key_pos);
|
|
|
|
// Draw lines between each keyframe
|
|
for (int i = 1; i < track.size(); i++) {
|
|
NodeKeyframe *before = track.at(i - 1);
|
|
NodeKeyframe *after = track.at(i);
|
|
|
|
QPointF before_pos = get_keyframe_position(before);
|
|
QPointF after_pos = get_keyframe_position(after);
|
|
|
|
if (before->type() == NodeKeyframe::k_hold) {
|
|
// Draw a hold keyframe (basically a right angle)
|
|
path.lineTo(after_pos.x(), before_pos.y());
|
|
path.lineTo(after_pos.x(), after_pos.y());
|
|
|
|
} else if (before->type() == NodeKeyframe::k_bezier &&
|
|
after->type() == NodeKeyframe::k_bezier) {
|
|
// Draw a cubic bezier
|
|
|
|
// Cubic beziers have two control points, so we can just use both
|
|
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_pos);
|
|
|
|
} else if (before->type() == NodeKeyframe::k_bezier ||
|
|
after->type() == NodeKeyframe::k_bezier) {
|
|
// Draw a quadratic bezier
|
|
|
|
// Quadratic beziers have a single control point, we just have to determine which it is
|
|
QPointF key_anchor;
|
|
QPointF control_point;
|
|
|
|
if (before->type() == NodeKeyframe::k_bezier) {
|
|
key_anchor = before_pos;
|
|
control_point = before->valid_bezier_control_out();
|
|
} else {
|
|
key_anchor = after_pos;
|
|
control_point = after->valid_bezier_control_in();
|
|
}
|
|
|
|
// Scale control point
|
|
control_point = key_anchor + ScalePoint(control_point);
|
|
|
|
// Create the path from both keyframes
|
|
path.quadTo(control_point, after_pos);
|
|
|
|
} else {
|
|
// Linear to linear
|
|
path.lineTo(after_pos);
|
|
}
|
|
}
|
|
|
|
// Draw straight line leading from end keyframe
|
|
QPointF last_key_pos = get_keyframe_position(track.last());
|
|
path.lineTo(QPointF(scene_top_right.x(), last_key_pos.y()));
|
|
|
|
painter->drawPath(path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void CurveView::drawForeground(QPainter *painter, const QRectF &rect)
|
|
{
|
|
bezier_pts_.clear();
|
|
|
|
super::drawForeground(painter, rect);
|
|
}
|
|
|
|
void CurveView::ContextMenuEvent(Menu &m)
|
|
{
|
|
// View settings
|
|
QAction *zoom_fit_action = m.addAction(tr("Zoom to Fit"));
|
|
connect(zoom_fit_action, &QAction::triggered, this, &CurveView::zoom_to_fit);
|
|
|
|
QAction *zoom_fit_selected_action = m.addAction(tr("Zoom to Fit Selected"));
|
|
connect(zoom_fit_selected_action, &QAction::triggered, this,
|
|
&CurveView::zoom_to_fit_selected);
|
|
|
|
QAction *reset_zoom_action = m.addAction(tr("Reset Zoom"));
|
|
connect(reset_zoom_action, &QAction::triggered, this,
|
|
&CurveView::reset_zoom);
|
|
}
|
|
|
|
void CurveView::SceneRectUpdateEvent(QRectF &r)
|
|
{
|
|
double min_val, max_val;
|
|
bool got_val = false;
|
|
|
|
foreach (KeyframeViewInputConnection *con, track_connections_) {
|
|
foreach (NodeKeyframe *key, con->get_keyframes()) {
|
|
qreal key_y = get_item_y_from_keyframe_value(key);
|
|
|
|
if (got_val) {
|
|
min_val = qMin(key_y, min_val);
|
|
max_val = qMax(key_y, max_val);
|
|
} else {
|
|
min_val = key_y;
|
|
max_val = key_y;
|
|
got_val = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (got_val) {
|
|
r.setTop(min_val - this->height());
|
|
r.setBottom(max_val + this->height());
|
|
}
|
|
}
|
|
|
|
qreal CurveView::get_keyframe_scene_y(KeyframeViewInputConnection *track,
|
|
NodeKeyframe *key)
|
|
{
|
|
return get_item_y_from_keyframe_value(key);
|
|
}
|
|
|
|
void CurveView::draw_keyframe(QPainter *painter, NodeKeyframe *key,
|
|
KeyframeViewInputConnection *track,
|
|
const QRectF &key_rect)
|
|
{
|
|
if (is_keyframe_selected(key) && key->type() == NodeKeyframe::k_bezier) {
|
|
// Draw bezier control points if keyframe is selected
|
|
int control_point_size = QtUtils::q_font_metrics_width(fontMetrics(), "o");
|
|
int half_sz = control_point_size / 2;
|
|
QRectF control_point_rect(-half_sz, -half_sz, control_point_size,
|
|
control_point_size);
|
|
|
|
painter->setPen(palette().text().color());
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
QRectF cp_in = control_point_rect.translated(
|
|
key_rect.center() + ScalePoint(key->bezier_control_in()));
|
|
QRectF cp_out = control_point_rect.translated(
|
|
key_rect.center() + ScalePoint(key->bezier_control_out()));
|
|
|
|
painter->drawLine(key_rect.center(), cp_in.center());
|
|
painter->drawLine(key_rect.center(), cp_out.center());
|
|
|
|
painter->drawEllipse(cp_in);
|
|
painter->drawEllipse(cp_out);
|
|
|
|
bezier_pts_.append({ cp_in, key, NodeKeyframe::k_in_handle });
|
|
bezier_pts_.append({ cp_out, key, NodeKeyframe::k_out_handle });
|
|
}
|
|
|
|
super::draw_keyframe(painter, key, track, key_rect);
|
|
}
|
|
|
|
bool CurveView::first_chance_mouse_press(QMouseEvent *event)
|
|
{
|
|
dragging_bezier_pt_ = nullptr;
|
|
QPointF scene_pt = mapToScene(event->pos());
|
|
foreach (const BezierPoint &b, bezier_pts_) {
|
|
if (b.rect.contains(scene_pt)) {
|
|
dragging_bezier_pt_ = &b;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (dragging_bezier_pt_) {
|
|
NodeKeyframe *key = dragging_bezier_pt_->keyframe;
|
|
dragging_bezier_point_start_ =
|
|
(dragging_bezier_pt_->type == NodeKeyframe::k_in_handle) ?
|
|
key->bezier_control_in() :
|
|
key->bezier_control_out();
|
|
dragging_bezier_point_opposing_start_ =
|
|
(dragging_bezier_pt_->type == NodeKeyframe::k_in_handle) ?
|
|
key->bezier_control_out() :
|
|
key->bezier_control_in();
|
|
|
|
drag_start_ = mapToScene(event->pos());
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void CurveView::first_chance_mouse_move(QMouseEvent *event)
|
|
{
|
|
// Calculate cursor difference and scale it
|
|
QPointF scene_pos = mapToScene(event->pos());
|
|
QPointF mouse_diff_scaled = get_scaled_cursor_pos(scene_pos - drag_start_);
|
|
|
|
if (event->modifiers() & Qt::ShiftModifier) {
|
|
// If holding shift, only move one axis
|
|
mouse_diff_scaled.setY(0);
|
|
}
|
|
|
|
// Flip the mouse Y because bezier control points are drawn bottom to top, not top to bottom
|
|
mouse_diff_scaled.setY(-mouse_diff_scaled.y());
|
|
|
|
QPointF new_bezier_pos = generate_bezier_control_position(
|
|
dragging_bezier_pt_->type, dragging_bezier_point_start_,
|
|
mouse_diff_scaled);
|
|
|
|
// If the user is NOT holding control, we set the other handle to the exact negative of this handle
|
|
QPointF new_opposing_pos;
|
|
NodeKeyframe::BezierType opposing_type =
|
|
NodeKeyframe::get_opposing_bezier_type(dragging_bezier_pt_->type);
|
|
|
|
if (!(event->modifiers() & Qt::ControlModifier)) {
|
|
new_opposing_pos = generate_bezier_control_position(
|
|
opposing_type, dragging_bezier_point_opposing_start_,
|
|
-mouse_diff_scaled);
|
|
} else {
|
|
new_opposing_pos = dragging_bezier_point_opposing_start_;
|
|
}
|
|
|
|
dragging_bezier_pt_->keyframe->set_bezier_control(dragging_bezier_pt_->type,
|
|
new_bezier_pos);
|
|
|
|
dragging_bezier_pt_->keyframe->set_bezier_control(opposing_type,
|
|
new_opposing_pos);
|
|
|
|
redraw();
|
|
}
|
|
|
|
void CurveView::first_chance_mouse_release(QMouseEvent *event)
|
|
{
|
|
MultiUndoCommand *command = new MultiUndoCommand();
|
|
|
|
// Create undo command with the current bezier point and the old one
|
|
command->add_child(new KeyframeSetBezierControlPoint(
|
|
dragging_bezier_pt_->keyframe, dragging_bezier_pt_->type,
|
|
dragging_bezier_pt_->keyframe->bezier_control(dragging_bezier_pt_->type),
|
|
dragging_bezier_point_start_));
|
|
|
|
if (!(event->modifiers() & Qt::ControlModifier)) {
|
|
auto opposing_type =
|
|
NodeKeyframe::get_opposing_bezier_type(dragging_bezier_pt_->type);
|
|
|
|
command->add_child(new KeyframeSetBezierControlPoint(
|
|
dragging_bezier_pt_->keyframe, opposing_type,
|
|
dragging_bezier_pt_->keyframe->bezier_control(opposing_type),
|
|
dragging_bezier_point_opposing_start_));
|
|
}
|
|
|
|
dragging_bezier_pt_ = nullptr;
|
|
|
|
Core::instance()->undo_stack()->push(
|
|
command, tr("Moved Keyframe Bezier Control Point"));
|
|
}
|
|
|
|
void CurveView::keyframe_drag_start(QMouseEvent *event)
|
|
{
|
|
drag_keyframe_values_.resize(get_selected_keyframes().size());
|
|
for (size_t i = 0; i < get_selected_keyframes().size(); i++) {
|
|
NodeKeyframe *key = get_selected_keyframes().at(i);
|
|
drag_keyframe_values_[i] = key->value();
|
|
}
|
|
|
|
drag_start_ = mapToScene(event->pos());
|
|
}
|
|
|
|
void CurveView::keyframe_drag_move(QMouseEvent *event, QString &tip)
|
|
{
|
|
if (event->modifiers() & Qt::ShiftModifier) {
|
|
// Lock to X axis only and set original values on all keys
|
|
for (size_t i = 0; i < get_selected_keyframes().size(); i++) {
|
|
NodeKeyframe *key = get_selected_keyframes().at(i);
|
|
key->set_value(drag_keyframe_values_.at(i));
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Calculate cursor difference
|
|
double scaled_diff =
|
|
(mapToScene(event->pos()).y() - drag_start_.y()) / get_y_scale();
|
|
|
|
// Validate movement - ensure no keyframe goes above its max point or below its min point
|
|
for (size_t i = 0; i < get_selected_keyframes().size(); i++) {
|
|
NodeKeyframe *key = get_selected_keyframes().at(i);
|
|
|
|
FloatSlider::DisplayType display = get_float_display_type_from_keyframe(key);
|
|
Node *node = key->parent();
|
|
double original_val = FloatSlider::transform_value_to_display(
|
|
drag_keyframe_values_.at(i).toDouble(), display);
|
|
const QString &input = key->input();
|
|
double new_val = FloatSlider::transform_display_to_value(
|
|
original_val - scaled_diff, display);
|
|
double limited = new_val;
|
|
|
|
if (node->has_input_property(input, QStringLiteral("min"))) {
|
|
limited = qMax(
|
|
limited,
|
|
node->get_input_property(input, QStringLiteral("min")).toDouble());
|
|
}
|
|
|
|
if (node->has_input_property(input, QStringLiteral("max"))) {
|
|
limited = qMin(
|
|
limited,
|
|
node->get_input_property(input, QStringLiteral("max")).toDouble());
|
|
}
|
|
|
|
if (limited != new_val) {
|
|
scaled_diff = original_val - limited;
|
|
}
|
|
}
|
|
|
|
// Set values
|
|
for (size_t i = 0; i < get_selected_keyframes().size(); i++) {
|
|
NodeKeyframe *key = get_selected_keyframes().at(i);
|
|
FloatSlider::DisplayType display = get_float_display_type_from_keyframe(key);
|
|
key->set_value(FloatSlider::transform_display_to_value(
|
|
FloatSlider::transform_value_to_display(
|
|
drag_keyframe_values_.at(i).toDouble(), display) -
|
|
scaled_diff,
|
|
display));
|
|
}
|
|
|
|
NodeKeyframe *tip_item = get_selected_keyframes().front();
|
|
|
|
bool ok;
|
|
double num_value = tip_item->value().toDouble(&ok);
|
|
|
|
if (ok) {
|
|
tip = QStringLiteral("%1\n");
|
|
tip.append(FloatSlider::value_to_string(
|
|
num_value + get_offset_from_keyframe(tip_item),
|
|
get_float_display_type_from_keyframe(tip_item), 2, true));
|
|
}
|
|
}
|
|
|
|
void CurveView::keyframe_drag_release(QMouseEvent *event,
|
|
MultiUndoCommand *command)
|
|
{
|
|
for (size_t i = 0; i < get_selected_keyframes().size(); i++) {
|
|
NodeKeyframe *k = get_selected_keyframes().at(i);
|
|
if (!qFuzzyCompare(k->value().toDouble(),
|
|
drag_keyframe_values_.at(i).toDouble())) {
|
|
command->add_child(new NodeParamSetKeyframeValueCommand(
|
|
k, k->value(), drag_keyframe_values_.at(i)));
|
|
}
|
|
}
|
|
}
|
|
|
|
QPointF
|
|
CurveView::generate_bezier_control_position(const NodeKeyframe::BezierType mode,
|
|
const QPointF &start_point,
|
|
const QPointF &scaled_cursor_diff)
|
|
{
|
|
QPointF new_bezier_pos = start_point;
|
|
|
|
new_bezier_pos += scaled_cursor_diff;
|
|
|
|
// LIMIT bezier handles from overlapping each other
|
|
if (mode == NodeKeyframe::k_in_handle) {
|
|
if (new_bezier_pos.x() > 0) {
|
|
new_bezier_pos.setX(0);
|
|
}
|
|
} else {
|
|
if (new_bezier_pos.x() < 0) {
|
|
new_bezier_pos.setX(0);
|
|
}
|
|
}
|
|
|
|
return new_bezier_pos;
|
|
}
|
|
|
|
QPointF CurveView::get_scaled_cursor_pos(const QPointF &cursor_pos)
|
|
{
|
|
return QPointF(cursor_pos.x() / get_scale(), cursor_pos.y() / get_y_scale());
|
|
}
|
|
|
|
void CurveView::zoom_to_fit_internal(bool selected_only)
|
|
{
|
|
bool got_val = false;
|
|
|
|
Rational min_time, max_time;
|
|
double min_val, max_val;
|
|
|
|
foreach (KeyframeViewInputConnection *con, track_connections_) {
|
|
foreach (NodeKeyframe *key, con->get_keyframes()) {
|
|
if (!selected_only || is_keyframe_selected(key)) {
|
|
Rational transformed_time =
|
|
get_adjusted_time(key->parent(), get_time_target(), key->time(),
|
|
Node::k_transform_towards_output);
|
|
|
|
qreal key_y = get_unscaled_item_y_from_keyframe_value(key);
|
|
|
|
if (got_val) {
|
|
min_time = qMin(transformed_time, min_time);
|
|
max_time = qMax(transformed_time, max_time);
|
|
|
|
min_val = qMin(key_y, min_val);
|
|
max_val = qMax(key_y, max_val);
|
|
} else {
|
|
min_time = transformed_time;
|
|
max_time = transformed_time;
|
|
|
|
min_val = key_y;
|
|
max_val = key_y;
|
|
|
|
got_val = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Prevent scaling if no keyframes were found
|
|
if (got_val) {
|
|
QRectF desired(QPointF(min_time.to_double(), min_val),
|
|
QPointF(max_time.to_double(), max_val));
|
|
|
|
const double scale_divider = 0.5;
|
|
double scale_half_divider = scale_divider * 0.5;
|
|
|
|
double new_x_scale =
|
|
viewport()->width() / desired.width() * scale_divider;
|
|
double new_y_scale;
|
|
|
|
if (qFuzzyIsNull(desired.height())) {
|
|
// Catch divide by zero
|
|
new_y_scale = 1.0;
|
|
scale_half_divider = 0.5;
|
|
} else {
|
|
// Use height as normal
|
|
new_y_scale =
|
|
viewport()->height() / desired.height() * scale_divider;
|
|
}
|
|
|
|
emit scale_changed(new_x_scale);
|
|
set_y_scale(new_y_scale);
|
|
|
|
update_scene_rect();
|
|
|
|
int sb_x = desired.left() * new_x_scale -
|
|
viewport()->width() * scale_half_divider;
|
|
QMetaObject::invokeMethod(horizontalScrollBar(), "setValue",
|
|
Qt::QueuedConnection, Q_ARG(int, sb_x));
|
|
|
|
int sb_y = desired.top() * new_y_scale -
|
|
viewport()->height() * scale_half_divider;
|
|
QMetaObject::invokeMethod(verticalScrollBar(), "setValue",
|
|
Qt::QueuedConnection, Q_ARG(int, sb_y));
|
|
}
|
|
}
|
|
|
|
qreal CurveView::get_item_y_from_keyframe_value(NodeKeyframe *key)
|
|
{
|
|
return get_unscaled_item_y_from_keyframe_value(key) * get_y_scale();
|
|
}
|
|
|
|
qreal CurveView::get_unscaled_item_y_from_keyframe_value(NodeKeyframe *key)
|
|
{
|
|
double val = key->value().toDouble();
|
|
|
|
val = FloatSlider::transform_value_to_display(
|
|
val, get_float_display_type_from_keyframe(key));
|
|
|
|
val += get_offset_from_keyframe(key);
|
|
|
|
return -val;
|
|
}
|
|
|
|
QPointF CurveView::ScalePoint(const QPointF &point)
|
|
{
|
|
// Flips Y coordinate because curves are drawn bottom to top
|
|
return QPointF(point.x() * get_scale(), -point.y() * get_y_scale());
|
|
}
|
|
|
|
FloatSlider::DisplayType
|
|
CurveView::get_float_display_type_from_keyframe(NodeKeyframe *key)
|
|
{
|
|
Node *node = key->parent();
|
|
const QString &input = key->input();
|
|
if (node->has_input_property(input, QStringLiteral("view"))) {
|
|
// Try to get view from input (which will be normal if unset)
|
|
return static_cast<FloatSlider::DisplayType>(
|
|
node->get_input_property(input, QStringLiteral("view")).toInt());
|
|
}
|
|
|
|
// Fallback to normal
|
|
return FloatSlider::k_normal;
|
|
}
|
|
|
|
double CurveView::get_offset_from_keyframe(NodeKeyframe *key)
|
|
{
|
|
Node *node = key->parent();
|
|
const QString &input = key->input();
|
|
if (node->has_input_property(input, QStringLiteral("offset"))) {
|
|
QVariant v = node->get_input_property(input, QStringLiteral("offset"));
|
|
|
|
// NOTE: Implement getting correct offset for the track based on the data type
|
|
QVector<QVariant> track_vals =
|
|
NodeValue::split_normal_value_into_track_values(
|
|
node->get_input_data_type(input), v);
|
|
|
|
return track_vals.at(key->track()).toDouble();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
QPointF CurveView::get_keyframe_position(NodeKeyframe *key)
|
|
{
|
|
return QPointF(get_keyframe_scene_x(key), get_item_y_from_keyframe_value(key));
|
|
}
|
|
|
|
void CurveView::zoom_to_fit()
|
|
{
|
|
zoom_to_fit_internal(false);
|
|
}
|
|
|
|
void CurveView::zoom_to_fit_selected()
|
|
{
|
|
zoom_to_fit_internal(true);
|
|
}
|
|
|
|
void CurveView::reset_zoom()
|
|
{
|
|
emit scale_changed(1.0);
|
|
set_y_scale(1.0);
|
|
}
|
|
|
|
}
|