Commit 496b29c1 authored by Nicolas Carion's avatar Nicolas Carion

[Timeline2][Model] Fix qml for clip resize

parent 52a69185
......@@ -390,12 +390,12 @@ bool TimelineModel::requestClipMove(int cid, int tid, int position, bool test_on
int gid = m_groups->getRootId(cid);
int delta_track = tid - m_allClips[cid]->getCurrentTrackId();
int delta_pos = position - m_allClips[cid]->getPosition();
//TODO fix call to send test_only
return requestGroupMove(gid, delta_track, delta_pos);
}
std::function<bool (void)> undo = [](){return true;};
std::function<bool (void)> redo = [](){return true;};
bool res = requestClipMove(cid, tid, position, test_only, undo, redo);
qDebug()<<"clip move in model"<<cid<<tid<<position<<res<<test_only;
if (res && !test_only) {
PUSH_UNDO(undo, redo, i18n("Move clip"));
}
......@@ -462,19 +462,13 @@ bool TimelineModel::requestGroupMove(int gid, int delta_track, int delta_pos, bo
return true;
}
bool TimelineModel::trimClip(int cid, int delta, bool right, bool ripple)
{
return requestClipResize(cid, m_allClips[cid]->getPlaytime() - delta, right);
}
bool TimelineModel::requestClipResize(int cid, int size, bool right)
bool TimelineModel::requestClipResize(int cid, int size, bool right, bool test_only)
{
std::function<bool (void)> undo = [](){return true;};
std::function<bool (void)> redo = [](){return true;};
bool result = m_allClips[cid]->requestResize(size, right, undo, redo);
//qDebug()<<"clip resize in model"<<cid<<size<<right<<result;
if (result) {
Fun undo = [](){return true;};
Fun redo = [](){return true;};
Fun update_model = [cid, right, this]() {
if (getClipTrackId(cid) != -1) {
QModelIndex modelIndex = makeClipIndexFromID(cid);
if (right) {
......@@ -483,11 +477,24 @@ bool TimelineModel::requestClipResize(int cid, int size, bool right)
emit dataChanged(modelIndex, modelIndex, {StartRole,DurationRole});
}
}
PUSH_UNDO(undo, redo, i18n("Resize clip"));
return true;
};
bool result = m_allClips[cid]->requestResize(size, right, undo, redo);
if (result) {
PUSH_LAMBDA(update_model, undo);
PUSH_LAMBDA(update_model, redo);
update_model();
if (!test_only) {
PUSH_UNDO(undo, redo, i18n("Resize clip"));
}
}
return result;
}
bool TimelineModel::requestClipTrim(int cid, int delta, bool right, bool ripple, bool test_only)
{
return requestClipResize(cid, m_allClips[cid]->getPlaytime() - delta, right, test_only);
}
bool TimelineModel::requestGroupClips(const std::unordered_set<int>& ids)
{
......
......@@ -150,7 +150,7 @@ public:
@param cid is the ID of the clip
@param tid is the ID of the target track
@param position is the position where we want to move
@param test_only if set to false, the undo is not created and no signal is sent to qml
@param test_only if set to true, the undo is not created and no signal is sent to qml
*/
bool requestClipMove(int cid, int tid, int position, bool test_only = false);
protected:
......@@ -158,7 +158,6 @@ protected:
bool requestClipMove(int cid, int tid, int position, bool test_only, Fun &undo, Fun &redo);
public:
bool trimClip(int cid, int delta, bool right, bool ripple = false);
/* @brief Request clip insertion at given position.
This action is undoable
......@@ -184,8 +183,19 @@ public:
@param cid is the ID of the clip
@param size is the new size of the clip
@param right is true if we change the right side of the clip, false otherwise
@param test_only if set to true, the undo is not created and no signal is sent to qml
*/
bool requestClipResize(int cid, int size, bool right);
bool requestClipResize(int cid, int size, bool right, bool test_only = false);
/* @brief Similar to requestClipResize but takes a delta instead of absolute size
Returns true on success. If it fails, nothing is modified.
@param cid is the ID of the clip
@param delta is the delta to be applied to the length of the clip
@param right is true if we change the right side of the clip, false otherwise
@param ripple TODO document this
@param test_only if set to true, the undo is not created and no signal is sent to qml
*/
bool requestClipTrim(int cid, int delta, bool right, bool ripple = false, bool test_only = false);
/* @brief Group together a set of ids
Returns true on success. If it fails, nothing is modified.
......
......@@ -40,7 +40,13 @@ using Fun = std::function<bool (void)>;
return operation() && v; \
};
/* @brief this macro executes an operation after a given lambda
*/
#define PUSH_LAMBDA(operation, lambda) \
lambda = [lambda, operation]() { \
bool v = lambda(); \
return operation() && v; \
};
#include <QUndoCommand>
......
......@@ -44,6 +44,8 @@ Rectangle {
property int clipId //Id of the clip in the model
property int originalTrackId: trackId
property int originalX: x
property int originalDuration: clipDuration
property int lastValidDuration: clipDuration
property int draggedX: x
property bool selected: false
property string hash: ''
......@@ -54,9 +56,9 @@ Rectangle {
signal dragged(var clip, var mouse)
signal dropped(var clip)
signal draggedToTrack(var clip, int direction)
signal trimmingIn(var clip, real delta, var mouse)
signal trimmingIn(var clip, real newDuration, var mouse)
signal trimmedIn(var clip)
signal trimmingOut(var clip, real delta, var mouse)
signal trimmingOut(var clip, real newDuration, var mouse)
signal trimmedOut(var clip)
onModelStartChanged: {
......@@ -483,6 +485,7 @@ Rectangle {
root.stopScrolling = true
startX = mapToItem(null, x, y).x
originalX = 0 // reusing originalX to accumulate delta for bubble help
clipRoot.originalDuration = clipDuration
parent.anchors.left = undefined
}
onReleased: {
......@@ -495,12 +498,8 @@ Rectangle {
if (mouse.buttons === Qt.LeftButton) {
var newX = mapToItem(null, x, y).x
var delta = Math.round((newX - startX) / timeScale)
if (Math.abs(delta) > 0) {
if (clipDuration + originalX + delta > 0)
originalX += delta
clipRoot.trimmingIn(clipRoot, delta, mouse)
startX = newX
}
var newDuration = clipRoot.clipDuration - delta
clipRoot.trimmingIn(clipRoot, newDuration, mouse)
}
}
onEntered: parent.opacity = 0.5
......@@ -526,12 +525,10 @@ Rectangle {
cursorShape: Qt.SizeHorCursor
drag.target: parent
drag.axis: Drag.XAxis
property int duration
onPressed: {
root.stopScrolling = true
duration = clipDuration
originalX = 0 // reusing originalX to accumulate delta for bubble help
clipRoot.originalDuration = clipDuration
parent.anchors.right = undefined
}
onReleased: {
......@@ -542,13 +539,7 @@ Rectangle {
onPositionChanged: {
if (mouse.buttons === Qt.LeftButton) {
var newDuration = Math.round((parent.x + parent.width) / timeScale)
var delta = duration - newDuration
if (Math.abs(delta) > 0) {
if (clipDuration - originalX - delta > 0)
originalX += delta
clipRoot.trimmingOut(clipRoot, delta, mouse)
duration = newDuration
}
clipRoot.trimmingOut(clipRoot, newDuration, mouse)
}
}
onEntered: parent.opacity = 0.5
......
......@@ -124,54 +124,42 @@ Column{
clip.draggedX = clip.x
}
onTrimmingIn: {
var originalDelta = delta
if (!(mouse.modifiers & Qt.AltModifier) && timeline.snap && !timeline.ripple)
delta = Logic.snapTrimIn(clip, delta)
if (delta != 0) {
if (timeline.trimClip(clip.clipId, delta, false)) {
// Show amount trimmed as a time in a "bubble" help.
var s = timeline.timecode(Math.abs(clip.originalX))
s = '%1%2 = %3'.arg((clip.originalX < 0)? '-' : (clip.originalX > 0)? '+' : '')
.arg(s.substring(3))
.arg(timeline.timecode(clipDuration))
bubbleHelp.show(clip.x, trackRoot.y + trackRoot.height, s)
} else {
clip.originalX -= originalDelta
}
//if (!(mouse.modifiers & Qt.AltModifier) && timeline.snap && !timeline.ripple)
// delta = Logic.snapTrimIn(clip, delta)
if (timeline.resizeClip(clip.clipId, newDuration, false, true)) {
clip.lastValidDuration = newDuration
// Show amount trimmed as a time in a "bubble" help.
var delta = newDuration - clip.originalDuration
var s = timeline.timecode(Math.abs(delta))
s = '%1%2 = %3'.arg((delta < 0)? '+' : (delta > 0)? '-' : '')
.arg(s.substring(3))
.arg(timeline.timecode(clipDuration))
bubbleHelp.show(clip.x + clip.width, trackRoot.y + trackRoot.height, s)
}
}
onTrimmedIn: {
multitrack.notifyClipIn(trackRoot.DelegateModel.itemsIndex, clip.DelegateModel.itemsIndex)
// Notify out point of clip A changed when trimming to add a transition.
if (clip.DelegateModel.itemsIndex > 1 && repeater.itemAt(clip.DelegateModel.itemsIndex - 1).isTransition)
multitrack.notifyClipOut(trackRoot.DelegateModel.itemsIndex, clip.DelegateModel.itemsIndex - 2)
bubbleHelp.hide()
timeline.commitTrimCommand()
timeline.resizeClip(clip.clipId, clip.originalDuration, false, true)
timeline.resizeClip(clip.clipId, clip.lastValidDuration, false, false)
}
onTrimmingOut: {
var originalDelta = delta
if (!(mouse.modifiers & Qt.AltModifier) && timeline.snap && !timeline.ripple)
delta = Logic.snapTrimOut(clip, delta)
if (delta != 0) {
if (timeline.trimClip(clip.clipId, delta, true)) {
// Show amount trimmed as a time in a "bubble" help.
var s = timeline.timecode(Math.abs(clip.originalX))
s = '%1%2 = %3'.arg((clip.originalX < 0)? '+' : (clip.originalX > 0)? '-' : '')
.arg(s.substring(3))
.arg(timeline.timecode(clipDuration))
bubbleHelp.show(clip.x + clip.width, trackRoot.y + trackRoot.height, s)
} else {
clip.originalX -= originalDelta
}
// if (!(mouse.modifiers & Qt.AltModifier) && timeline.snap && !timeline.ripple)
// delta = Logic.snapTrimOut(clip, delta)
if (timeline.resizeClip(clip.clipId, newDuration, true, true)) {
clip.lastValidDuration = newDuration
// Show amount trimmed as a time in a "bubble" help.
var delta = newDuration - clip.originalDuration
var s = timeline.timecode(Math.abs(delta))
s = '%1%2 = %3'.arg((delta < 0)? '+' : (delta > 0)? '-' : '')
.arg(s.substring(3))
.arg(timeline.timecode(clipDuration))
bubbleHelp.show(clip.x + clip.width, trackRoot.y + trackRoot.height, s)
}
}
onTrimmedOut: {
multitrack.notifyClipOut(trackRoot.DelegateModel.itemsIndex, clip.DelegateModel.itemsIndex)
// Notify in point of clip B changed when trimming to add a transition.
if (clip.DelegateModel.itemsIndex + 2 < repeater.count && repeater.itemAt(clip.DelegateModel.itemsIndex + 1).isTransition)
multitrack.notifyClipIn(trackRoot.DelegateModel.itemsIndex, clip.DelegateModel.itemsIndex + 2)
bubbleHelp.hide()
timeline.commitTrimCommand()
timeline.resizeClip(clip.clipId, clip.originalDuration, true, true)
timeline.resizeClip(clip.clipId, clip.lastValidDuration, true, false)
}
Component.onCompleted: {
......
......@@ -145,9 +145,14 @@ bool TimelineWidget::allowMoveClip(int toTrack, int clipIndex, int position)
return m_model->requestClipMove(clipIndex, toTrack, position, true);
}
bool TimelineWidget::trimClip(int clipIndex, int delta, bool right)
bool TimelineWidget::trimClip(int clipIndex, int delta, bool right, bool test_only)
{
return m_model->trimClip(clipIndex, delta, right);
return m_model->requestClipTrim(clipIndex, delta, right, false, test_only);
}
bool TimelineWidget::resizeClip(int clipIndex, int duration, bool right, bool test_only)
{
return m_model->requestClipResize(clipIndex, duration, right, test_only);
}
void TimelineWidget::insertClip(int track, int position, QString data)
......
......@@ -49,7 +49,8 @@ public:
Q_INVOKABLE void setScaleFactor(double scale);
Q_INVOKABLE bool moveClip(int toTrack, int clipIndex, int position, bool test_only = false);
Q_INVOKABLE bool allowMoveClip(int toTrack, int clipIndex, int position);
Q_INVOKABLE bool trimClip(int clipIndex, int delta, bool right);
Q_INVOKABLE bool trimClip(int clipIndex, int delta, bool right, bool test_only = false);
Q_INVOKABLE bool resizeClip(int clipIndex, int duration, bool right, bool test_only = false);
Q_INVOKABLE int duration() const;
Q_INVOKABLE int position() const { return m_position; }
Q_INVOKABLE void setPosition(int);
......
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