snap clips on timeline move

parent 169f3ef4
......@@ -231,6 +231,10 @@ int TimelineModel::suggestClipMove(int cid, int tid, int position)
if (currentPos == position || currentTrack != tid) {
return position;
}
int snapped = requestBestSnapPos(position, m_allClips[cid]->getPlaytime());
if (snapped >= 0) {
position = snapped;
}
qDebug() << "Starting suggestion "<<cid << position << currentPos;
//we check if move is possible
Fun undo = [](){return true;};
......@@ -754,10 +758,14 @@ void TimelineModel::setUndoStack(std::weak_ptr<DocUndoStack> undo_stack)
m_undoStack = undo_stack;
}
int TimelineModel::requestBestSnapPos(int pos, int length)
int TimelineModel::requestBestSnapPos(int pos, int length, const std::vector<int> pts)
{
if (pts.size() > 0) {
m_snaps->ignore(pts);
}
int snapped_start = m_snaps->getClosestPoint(pos);
int snapped_end = m_snaps->getClosestPoint(pos + length);
m_snaps->unIgnore();
int startDiff = qAbs(pos - snapped_start);
int endDiff = qAbs(pos + length - snapped_end);
if (startDiff < endDiff && snapped_start >= 0) {
......
......@@ -259,9 +259,10 @@ public:
/* @brief Requests the best snapped position for a clip
@param pos is the clip's requested position
@param length is the clip's duration
@param pts snap points to ignore (for example currently moved clip)
@returns best snap position or -1 if no snap point is near
*/
int requestBestSnapPos(int pos, int length);
int requestBestSnapPos(int pos, int length, const std::vector<int> pts = std::vector<int>());
/* @brief Requests the next snapped point
@param pos is the current position
......
......@@ -94,11 +94,11 @@ Fun TrackModel::requestClipInsertion_lambda(int cid, int position, bool updateVi
//update clip position and track
clip->setPosition(position);
clip->setCurrentTrackId(getId());
int new_in = clip->getPosition();
int new_out = new_in + clip->getPlaytime();
ptr->m_snaps->addPoint(new_in);
ptr->m_snaps->addPoint(new_out);
if (updateView) {
int new_in = clip->getPosition();
int new_out = new_in + clip->getPlaytime();
ptr->m_snaps->addPoint(new_in);
ptr->m_snaps->addPoint(new_out);
int clip_index = getRowfromClip(clip->getId());
ptr->_beginInsertRows(ptr->makeTrackIndexFromID(getId()), clip_index, clip_index);
ptr->_endInsertRows();
......@@ -184,8 +184,10 @@ Fun TrackModel::requestClipDeletion_lambda(int cid, bool updateView)
m_allClips.erase(cid);
delete prod;
if (auto ptr = m_parent.lock()) {
ptr->m_snaps->removePoint(old_in);
ptr->m_snaps->removePoint(old_out + 1);
if (updateView) {
ptr->m_snaps->removePoint(old_in);
ptr->m_snaps->removePoint(old_out + 1);
}
qDebug() << "update" <<updateView;
}
return true;
......
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