Commit 491d80de authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Ensure keyframes cannot be dragged outside item range

CCBUG: 404463
parent b1d2bb45
......@@ -40,7 +40,7 @@ KeyframeView::KeyframeView(std::shared_ptr<KeyframeModelList> model, int duratio
{
setMouseTracking(true);
setMinimumSize(QSize(150, 20));
setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum));
setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
QPalette p = palette();
KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window);
......@@ -48,8 +48,8 @@ KeyframeView::KeyframeView(std::shared_ptr<KeyframeModelList> model, int duratio
m_colKeyframe = scheme.foreground(KColorScheme::NormalText).color();
m_size = QFontInfo(font()).pixelSize() * 1.8;
m_lineHeight = m_size / 2;
setMinimumHeight(m_size);
setMaximumHeight(m_size);
m_offset = m_lineHeight;
setFixedHeight(m_size);
connect(m_model.get(), &KeyframeModelList::modelChanged, this, &KeyframeView::slotModelChanged);
}
......@@ -127,7 +127,7 @@ void KeyframeView::setDuration(int dur)
void KeyframeView::slotGoToNext()
{
if (m_position == m_duration) {
if (m_position == m_duration - 1) {
return;
}
......@@ -157,14 +157,14 @@ void KeyframeView::slotGoToPrev()
emit seekToPos(qMax(0, (int)prev.first.frames(pCore->getCurrentFps()) - offset));
} else {
// no keyframe after current position
emit seekToPos(m_duration);
emit seekToPos(m_duration - 1);
}
}
void KeyframeView::mousePressEvent(QMouseEvent *event)
{
int offset = pCore->getItemIn(m_model->getOwnerId());
int pos = event->x() / m_scale;
int pos = (event->x() - m_offset) / m_scale;
if (event->y() < m_lineHeight && event->button() == Qt::LeftButton) {
bool ok;
GenTime position(pos + offset, pCore->getCurrentFps());
......@@ -187,7 +187,7 @@ void KeyframeView::mousePressEvent(QMouseEvent *event)
void KeyframeView::mouseMoveEvent(QMouseEvent *event)
{
int offset = pCore->getItemIn(m_model->getOwnerId());
int pos = qBound(0, (int)(event->x() / m_scale), m_duration);
int pos = qBound(0, (int)((event->x() - m_offset) / m_scale), m_duration - 1);
GenTime position(pos + offset, pCore->getCurrentFps());
if ((event->buttons() & Qt::LeftButton) != 0u) {
if (m_currentKeyframe == pos) {
......@@ -238,7 +238,7 @@ void KeyframeView::mouseReleaseEvent(QMouseEvent *event)
void KeyframeView::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && event->y() < m_lineHeight) {
int pos = qBound(0, (int)(event->x() / m_scale), m_duration);
int pos = qBound(0, (int)((event->x() - m_offset) / m_scale), m_duration - 1);
int offset = pCore->getItemIn(m_model->getOwnerId());
GenTime position(pos + offset, pCore->getCurrentFps());
bool ok;
......@@ -272,7 +272,7 @@ void KeyframeView::wheelEvent(QWheelEvent *event)
return;
}
int change = event->delta() > 0 ? -1 : 1;
int pos = qBound(0, m_position + change, m_duration);
int pos = qBound(0, m_position + change, m_duration - 1);
emit seekToPos(pos);
}
......@@ -281,7 +281,7 @@ void KeyframeView::paintEvent(QPaintEvent *event)
Q_UNUSED(event)
QStylePainter p(this);
m_scale = width() / (double)(m_duration);
m_scale = (width() - 2 * m_offset) / (double)(m_duration - 1);
// p.translate(0, m_lineHeight);
int headOffset = m_lineHeight / 1.5;
int offset = pCore->getItemIn(m_model->getOwnerId());
......@@ -296,7 +296,7 @@ void KeyframeView::paintEvent(QPaintEvent *event)
} else {
p.setBrush(m_colKeyframe);
}
double scaledPos = pos * m_scale;
double scaledPos = m_offset + (pos * m_scale);
p.drawLine(QPointF(scaledPos, headOffset), QPointF(scaledPos, m_lineHeight + headOffset / 2.0));
switch (keyframe.second.first) {
case KeyframeType::Linear: {
......@@ -321,17 +321,20 @@ void KeyframeView::paintEvent(QPaintEvent *event)
* Time-"line"
*/
p.setPen(m_colKeyframe);
p.drawLine(0, m_lineHeight + (headOffset / 2), (m_duration - 1) * m_scale, m_lineHeight + (headOffset / 2));
p.drawLine(m_offset, m_lineHeight + (headOffset / 2), width() - m_offset, m_lineHeight + (headOffset / 2));
/*
* current position
*/
if (m_position >= 0) {
if (m_position >= 0 && m_position < m_duration) {
QPolygon pa(3);
int cursorwidth = (m_size - (m_lineHeight + headOffset / 2)) / 2 + 1;
QPolygonF position = QPolygonF() << QPointF(-cursorwidth, m_size) << QPointF(cursorwidth, m_size) << QPointF(0, m_lineHeight + (headOffset / 2) + 1);
position.translate(m_position * m_scale, 0);
position.translate(m_offset + (m_position * m_scale), 0);
p.setBrush(m_colKeyframe);
p.drawPolygon(position);
}
p.setOpacity(0.5);
p.drawLine(m_offset, m_lineHeight, m_offset, m_lineHeight + headOffset );
p.drawLine(width() - m_offset, m_lineHeight, width() - m_offset, m_lineHeight + headOffset );
}
......@@ -73,6 +73,7 @@ private:
int m_currentKeyframeOriginal;
int m_hoverKeyframe;
int m_lineHeight;
int m_offset;
double m_scale;
int m_size;
......
......@@ -40,6 +40,7 @@ Rectangle
onKfrCountChanged: {
keyframecanvas.requestPaint()
}
Keys.onShortcutOverride: {
if (event.key == Qt.Key_Left) {
if (event.modifiers & Qt.AltModifier) {
......@@ -61,7 +62,10 @@ Rectangle
} else {
var oldFrame = activeFrame
activeFrame += 1
timeline.updateEffectKeyframe(masterObject.clipId, oldFrame, activeFrame)
activeFrame = Math.min(activeFrame, parent.width / timeScale)
if (activeFrame > oldFrame) {
timeline.updateEffectKeyframe(masterObject.clipId, oldFrame, activeFrame)
}
}
event.accepted = true
}
......@@ -78,8 +82,9 @@ Rectangle
var newVal = Math.max(keyframes.itemAt(activeIndex).value / parent.height - .05, 0)
kfrModel.updateKeyframe(activeFrame, newVal)
event.accepted = true
} else {
event.accepted = false
}
event.accepted = false
}
Repeater {
id: keyframes
......@@ -93,6 +98,7 @@ Rectangle
property int value: parent.height * model.normalizedValue
property int tmpVal : keyframeVal.y + root.baseUnit / 2
property int tmpPos : x + keyframeVal.x + root.baseUnit / 2
property int dragPos : -1
anchors.bottom: parent.bottom
onFrameTypeChanged: {
keyframecanvas.requestPaint()
......@@ -115,6 +121,7 @@ Rectangle
drag.axis: Drag.XAxis
onReleased: {
root.stopScrolling = false
dragPos = -1
var newPos = Math.round(parent.x / timeScale) + inPoint
if (frame != inPoint && newPos != frame) {
if (mouse.modifiers & Qt.ShiftModifier) {
......@@ -127,10 +134,22 @@ Rectangle
}
}
onPositionChanged: {
if (mouse.buttons === Qt.LeftButton && frame != inPoint) {
var newPos = Math.round(parent.x / timeScale)
parent.x = newPos * timeScale
keyframecanvas.requestPaint()
if (mouse.buttons === Qt.LeftButton) {
if (frame == inPoint) {
parent.x = inPoint * timeScale
return
}
var newPos = Math.min(Math.round(parent.x / timeScale), Math.round(keyframeContainer.width / timeScale) - 1)
if (newPos < 1) {
newPos = 1
}
if (newPos != dragPos) {
dragPos = newPos
parent.x = newPos * timeScale
keyframecanvas.requestPaint()
} else {
parent.x = dragPos * timeScale
}
}
}
}
......@@ -192,8 +211,17 @@ Rectangle
if (frame == inPoint) {
parent.x = - root.baseUnit / 2
} else {
var newPos = Math.round(parent.x / timeScale)
parent.x = newPos * timeScale
var newPos = Math.min(Math.round(parent.x / timeScale), Math.round(keyframeContainer.width / timeScale) - frame + inPoint - 1)
if (frame + newPos <= inPoint) {
newPos = inPoint + 1 - frame
}
if (newPos != dragPos) {
dragPos = newPos
parent.x = newPos * timeScale - root.baseUnit / 2
keyframecanvas.requestPaint()
} else {
parent.x = dragPos * timeScale - root.baseUnit / 2
}
}
keyframecanvas.requestPaint()
}
......
......@@ -72,6 +72,7 @@ void TransitionStackView::setModel(const std::shared_ptr<AssetParameterModel> &m
});
initKeyframeView(true);
pCore->getMonitor(m_model->monitorId)->slotShowEffectScene(needsMonitorEffectScene());
m_lay->addStretch(10);
slotRefresh();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment