curve/timeline: improved user interface behavior
* Implements "auto-fit" setting for curve view and sets it on open. * Improves scroll zooming on all TimelineViewBase derivatives. * Improves code sharing for better maintenance.
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "curveview.h"
|
||||
|
||||
#include <QScrollBar>
|
||||
#include <QMouseEvent>
|
||||
#include <QtMath>
|
||||
|
||||
@@ -251,42 +252,22 @@ void CurveView::VerticalScaleChangedEvent(double scale)
|
||||
|
||||
void CurveView::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
if (WheelEventIsAZoomEvent(event)) {
|
||||
if (!event->angleDelta().isNull()) {
|
||||
bool only_vertical = false;
|
||||
bool only_horizontal = false;
|
||||
|
||||
if (event->modifiers() & Qt::ShiftModifier) {
|
||||
if (event->modifiers() & Qt::AltModifier) {
|
||||
only_horizontal = true;
|
||||
} else {
|
||||
only_vertical = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (event->angleDelta().x() + event->angleDelta().y() > 0) {
|
||||
if (!only_vertical) {
|
||||
emit ScaleChanged(GetScale() * 1.1);
|
||||
}
|
||||
|
||||
if (!only_horizontal) {
|
||||
SetYScale(GetYScale() * 1.1);
|
||||
}
|
||||
} else {
|
||||
if (!only_vertical) {
|
||||
emit ScaleChanged(GetScale() * 0.9);
|
||||
}
|
||||
|
||||
if (!only_horizontal) {
|
||||
SetYScale(GetYScale() *0.9);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!HandleZoomFromScroll(event)) {
|
||||
KeyframeViewBase::wheelEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void CurveView::ContextMenuEvent(Menu &m)
|
||||
{
|
||||
m.addSeparator();
|
||||
|
||||
// View settings
|
||||
QAction* zoom_fit_action = m.addAction(tr("Zoom to Fit"));
|
||||
connect(zoom_fit_action, &QAction::triggered, this, &CurveView::ZoomToFit);
|
||||
|
||||
//QAction* reset_zoom_action = m.addAction(tr("Reset Zoom"));
|
||||
}
|
||||
|
||||
QList<NodeKeyframe *> CurveView::GetKeyframesSortedByTime(int track)
|
||||
{
|
||||
QList<NodeKeyframe *> sorted;
|
||||
@@ -320,7 +301,12 @@ QList<NodeKeyframe *> CurveView::GetKeyframesSortedByTime(int track)
|
||||
|
||||
qreal CurveView::GetItemYFromKeyframeValue(NodeKeyframe *key)
|
||||
{
|
||||
return -key->value().toDouble() * GetYScale();
|
||||
return GetItemYFromKeyframeValue(key->value().toDouble());
|
||||
}
|
||||
|
||||
qreal CurveView::GetItemYFromKeyframeValue(double value)
|
||||
{
|
||||
return -value * GetYScale();
|
||||
}
|
||||
|
||||
void CurveView::SetItemYFromKeyframeValue(NodeKeyframe *key, KeyframeViewItem *item)
|
||||
@@ -402,6 +388,40 @@ void CurveView::BezierControlPointDestroyed()
|
||||
bezier_control_points_.removeOne(item);
|
||||
}
|
||||
|
||||
void CurveView::ZoomToFit()
|
||||
{
|
||||
if (item_map().isEmpty()) {
|
||||
// Prevent scaling to DBL_MIN/DBL_MAX
|
||||
return;
|
||||
}
|
||||
|
||||
QMap<NodeKeyframe *, KeyframeViewItem *>::const_iterator i;
|
||||
|
||||
rational min_time = RATIONAL_MAX;
|
||||
rational max_time = RATIONAL_MIN;
|
||||
|
||||
double min_val = DBL_MAX;
|
||||
double max_val = DBL_MIN;
|
||||
|
||||
for (i=item_map().constBegin(); i!=item_map().constEnd(); i++) {
|
||||
min_time = qMin(i.key()->time(), min_time);
|
||||
max_time = qMax(i.key()->time(), max_time);
|
||||
|
||||
min_val = qMin(i.key()->value().toDouble(), min_val);
|
||||
max_val = qMax(i.key()->value().toDouble(), max_val);
|
||||
}
|
||||
|
||||
double time_range = max_time.toDouble() - min_time.toDouble();
|
||||
double new_x_scale = CalculateScaleFromDimensions(this->width(), time_range);
|
||||
double new_y_scale = CalculateScaleFromDimensions(this->height(), max_val - min_val);
|
||||
|
||||
emit ScaleChanged(new_x_scale);
|
||||
SetYScale(new_y_scale);
|
||||
|
||||
horizontalScrollBar()->setValue(TimeToScene(min_time) - CalculatePaddingFromDimensionScale(this->width()));
|
||||
verticalScrollBar()->setValue(GetItemYFromKeyframeValue(max_val) - CalculatePaddingFromDimensionScale(this->height()));
|
||||
}
|
||||
|
||||
void CurveView::AddKeyframe(NodeKeyframePtr key)
|
||||
{
|
||||
KeyframeViewItem* item = AddKeyframeInternal(key);
|
||||
|
||||
Reference in New Issue
Block a user