Commit cdd5b632 authored by Nicolas Carion's avatar Nicolas Carion

[Timeline2] Fix composition resize

parent 98799e85
......@@ -37,6 +37,7 @@ int CompositionModel::construct(std::weak_ptr<TimelineModel> parent, const QStri
{
auto xml = TransitionsRepository::get()->getXml(transitionId);
Mlt::Transition *transition = TransitionsRepository::get()->getTransition(transitionId);
transition->set_in_and_out(0, 0);
std::shared_ptr<CompositionModel> composition(new CompositionModel(parent, transition, id, xml, transitionId));
id = composition->m_id;
if (auto ptr = parent.lock()) {
......@@ -55,6 +56,7 @@ bool CompositionModel::requestResize(int size, bool right, Fun& undo, Fun& redo)
return false;
}
int delta = getPlaytime() - size;
qDebug() << "compo request resize "<<size<<right <<delta;
int in = getIn();
int out = getOut();
int old_in = in, old_out = out;
......@@ -70,17 +72,17 @@ bool CompositionModel::requestResize(int size, bool right, Fun& undo, Fun& redo)
std::function<bool (void)> track_operation = [](){return true;};
std::function<bool (void)> track_reverse = [](){return true;};
/*if (m_currentTrackId != -1) {
if (m_currentTrackId != -1) {
if (auto ptr = m_parent.lock()) {
track_operation = ptr->requestCompositionResize_lambda(m_id, in, out, right);
track_operation = ptr->getTrackById(m_currentTrackId)->requestCompositionResize_lambda(m_id, in, out);
} else {
qDebug() << "Error : Moving clip failed because parent timeline is not available anymore";
qDebug() << "Error : Moving composition failed because parent timeline is not available anymore";
Q_ASSERT(false);
}
}
auto operation = [this, in, out, track_operation]() {
Fun operation = [in, out, track_operation, this]() {
if (track_operation()) {
service()->set_in_and_out(in, out);
setInOut(in, out);
return true;
}
return false;
......@@ -89,19 +91,19 @@ bool CompositionModel::requestResize(int size, bool right, Fun& undo, Fun& redo)
// Now, we are in the state in which the timeline should be when we try to revert current action. So we can build the reverse action from here
auto ptr = m_parent.lock();
if (m_currentTrackId != -1 && ptr) {
track_reverse = ptr->requestCompositionResize_lambda(m_id, old_in, old_out, right);
track_reverse = ptr->getTrackById(m_currentTrackId)->requestCompositionResize_lambda(m_id, old_in, old_out);
}
auto reverse = [this, old_in, old_out, track_reverse]() {
Fun reverse = [old_in, old_out, track_reverse, this]() {
if (track_reverse()) {
service()->set_in_and_out(old_in, old_out);
setInOut(old_in, old_out);
return true;
}
return false;
};
UPDATE_UNDO_REDO(operation, reverse, undo, redo);
return true;
}*/
return true;
}
return false;
}
......@@ -112,5 +114,6 @@ Mlt::Transition* CompositionModel::service() const
int CompositionModel::getPlaytime() const
{
return service()->get_length();
qDebug() << "composition length"<<getIn()<<getOut();
return getOut() - getIn() + 1;
}
......@@ -624,9 +624,9 @@ bool TimelineModel::requestItemResize(int itemId, int size, bool right, bool log
};
bool result = false;
if (isClip(itemId)) {
m_allClips[itemId]->requestResize(size, right, undo, redo);
result = m_allClips[itemId]->requestResize(size, right, undo, redo);
} else {
m_allCompositions[itemId]->requestResize(size, right, undo, redo);
result = m_allCompositions[itemId]->requestResize(size, right, undo, redo);
}
if (result) {
PUSH_LAMBDA(update_model, undo);
......
......@@ -462,6 +462,41 @@ bool TrackModel::checkConsistency()
}
}
}
// We now check compositions positions
if (m_allCompositions.size() != m_compoPos.size()) {
qDebug() << "Error: the number of compositions position doesn't match number of compositions";
return false;
}
for (const auto& compo : m_allCompositions) {
int pos = compo.second->getPosition();
if (m_compoPos.count(pos) == 0) {
qDebug() << "Error: the position of composition "<<compo.first<<" is not properly stored";
return false;
}
if (m_compoPos[pos] != compo.first) {
qDebug() << "Error: found composition"<<m_compoPos[pos]<<"instead of "<<compo.first<<"at position"<<pos;
return false;
}
}
for (auto it = m_compoPos.begin(); it != m_compoPos.end(); ++it) {
int compoId = it->second;
int cur_in = m_allCompositions[compoId]->getPosition();
Q_ASSERT(cur_in == it->first);
int cur_out = cur_in + m_allCompositions[compoId]->getPlaytime() - 1;
++it;
if (it != m_compoPos.end()) {
int next_compoId = it->second;
int next_in = m_allCompositions[next_compoId]->getPosition();
int next_out = next_in + m_allCompositions[next_compoId]->getPlaytime() - 1;
if (next_in <= cur_out) {
qDebug()<<"Error: found collision between composition "
<< compoId << "[ "<<cur_in<<", "<<cur_out<<"] and "
<<next_compoId << "[ "<<next_in<<", "<<next_out<<"]" ;
return false;
}
}
--it;
}
return true;
}
......@@ -508,6 +543,7 @@ int TrackModel::getBlankEnd(int position)
Fun TrackModel::requestCompositionResize_lambda(int compoId, int in, int out)
{
qDebug() << "compo resize "<<compoId<< in<< out;
int compo_position = m_allCompositions[compoId]->getPosition();
Q_ASSERT(m_compoPos.count(compo_position) > 0);
Q_ASSERT(m_compoPos[compo_position] == compoId);
......@@ -619,9 +655,6 @@ int TrackModel::getCompositionsCount() const
Fun TrackModel::requestCompositionInsertion_lambda(int compoId, int position, bool updateView)
{
// By default, insertion occurs in topmost track
//we create the function that has to be executed after the melt order. This is essentially book-keeping
bool intersecting = true;
if (auto ptr = m_parent.lock()) {
intersecting = hasIntersectingComposition(position, position + ptr->getCompositionPlaytime(compoId) - 1);
......
......@@ -47,6 +47,7 @@ public:
friend class TimelineModel;
friend class TimelineItemModel;
friend class ClipModel;
friend class CompositionModel;
private:
/* This constructor is private, call the static construct instead */
TrackModel(std::weak_ptr<TimelineModel> parent, int id = -1);
......
......@@ -67,8 +67,12 @@ TEST_CASE("Composition manipulation", "[CompositionModel]")
std::shared_ptr<TimelineItemModel> timeline = TimelineItemModel::construct(new Mlt::Profile(), undoStack);
int tid1 = TrackModel::construct(timeline);
int cid2 = CompositionModel::construct(timeline, aCompo);
int tid2 = TrackModel::construct(timeline);
int tid3 = TrackModel::construct(timeline);
int cid1 = CompositionModel::construct(timeline, aCompo);
REQUIRE(timeline->getCompositionPlaytime(cid1) == 1);
REQUIRE(timeline->getCompositionPlaytime(cid2) == 1);
SECTION("Insert a composition in a track and change track") {
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
......@@ -76,76 +80,84 @@ TEST_CASE("Composition manipulation", "[CompositionModel]")
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 0);
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 0);
int cid1 = CompositionModel::construct(timeline, aCompo);
REQUIRE(timeline->getCompositionPlaytime(cid1) == 1);
REQUIRE(timeline->getCompositionPlaytime(cid2) == 1);
REQUIRE(timeline->getCompositionTrackId(cid1) == -1);
REQUIRE(timeline->getCompositionPosition(cid1) == -1);
int pos = 10;
//real insert
// // REQUIRE(timeline->allowClipMove(cid1, tid1, pos));
REQUIRE(timeline->requestCompositionMove(cid1, tid1, pos));
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
REQUIRE(timeline->getTrackById(tid2)->checkConsistency());
REQUIRE(timeline->getCompositionTrackId(cid1) == tid1);
REQUIRE(timeline->getCompositionPosition(cid1) == pos);
REQUIRE(timeline->getCompositionPlaytime(cid1) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 0);
}
/*
pos = 1;
//real
// // REQUIRE(timeline->allowClipMove(cid1, tid2, pos));
REQUIRE(timeline->requestClipMove(cid1, tid2, pos));
REQUIRE(timeline->requestCompositionMove(cid1, tid2, pos));
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
REQUIRE(timeline->getTrackById(tid2)->checkConsistency());
REQUIRE(timeline->getClipTrackId(cid1) == tid2);
REQUIRE(timeline->getClipPosition(cid1) == pos);
REQUIRE(timeline->getTrackClipsCount(tid2) == 1);
REQUIRE(timeline->getTrackClipsCount(tid1) == 0);
REQUIRE(timeline->getCompositionTrackId(cid1) == tid2);
REQUIRE(timeline->getCompositionPosition(cid1) == pos);
REQUIRE(timeline->getCompositionPlaytime(cid1) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 0);
REQUIRE(timeline->requestItemResize(cid1, 10, true));
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
REQUIRE(timeline->getTrackById(tid2)->checkConsistency());
REQUIRE(timeline->getCompositionTrackId(cid1) == tid2);
REQUIRE(timeline->getCompositionPosition(cid1) == pos);
REQUIRE(timeline->getCompositionPlaytime(cid1) == 10);
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 0);
REQUIRE(timeline->requestItemResize(cid2, 10, true));
REQUIRE(timeline->getCompositionPlaytime(cid2) == 10);
// Check conflicts
int pos2 = producer->get_playtime();
// // REQUIRE(timeline->allowClipMove(cid2, tid1, pos2));
REQUIRE(timeline->requestClipMove(cid2, tid1, pos2));
int pos2 = timeline->getCompositionPlaytime(cid1);
REQUIRE(timeline->requestCompositionMove(cid2, tid1, pos2));
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
REQUIRE(timeline->getTrackById(tid2)->checkConsistency());
REQUIRE(timeline->getClipTrackId(cid2) == tid1);
REQUIRE(timeline->getClipPosition(cid2) == pos2);
REQUIRE(timeline->getTrackClipsCount(tid2) == 1);
REQUIRE(timeline->getTrackClipsCount(tid1) == 1);
REQUIRE(timeline->getCompositionTrackId(cid2) == tid1);
REQUIRE(timeline->getCompositionPosition(cid2) == pos2);
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 1);
// // REQUIRE_FALSE(timeline->allowClipMove(cid1, tid1, pos2 + 2));
REQUIRE_FALSE(timeline->requestClipMove(cid1, tid1, pos2 + 2));
REQUIRE_FALSE(timeline->requestCompositionMove(cid1, tid1, pos2 + 2));
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
REQUIRE(timeline->getTrackById(tid2)->checkConsistency());
REQUIRE(timeline->getTrackClipsCount(tid2) == 1);
REQUIRE(timeline->getTrackClipsCount(tid1) == 1);
REQUIRE(timeline->getClipTrackId(cid1) == tid2);
REQUIRE(timeline->getClipPosition(cid1) == pos);
REQUIRE(timeline->getClipTrackId(cid2) == tid1);
REQUIRE(timeline->getClipPosition(cid2) == pos2);
// // REQUIRE_FALSE(timeline->allowClipMove(cid1, tid1, pos2 - 2));
REQUIRE_FALSE(timeline->requestClipMove(cid1, tid1, pos2 - 2));
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 1);
REQUIRE(timeline->getCompositionTrackId(cid1) == tid2);
REQUIRE(timeline->getCompositionPosition(cid1) == pos);
REQUIRE(timeline->getCompositionTrackId(cid2) == tid1);
REQUIRE(timeline->getCompositionPosition(cid2) == pos2);
REQUIRE_FALSE(timeline->requestCompositionMove(cid1, tid1, pos2 - 2));
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
REQUIRE(timeline->getTrackById(tid2)->checkConsistency());
REQUIRE(timeline->getTrackClipsCount(tid2) == 1);
REQUIRE(timeline->getTrackClipsCount(tid1) == 1);
REQUIRE(timeline->getClipTrackId(cid1) == tid2);
REQUIRE(timeline->getClipPosition(cid1) == pos);
REQUIRE(timeline->getClipTrackId(cid2) == tid1);
REQUIRE(timeline->getClipPosition(cid2) == pos2);
// // REQUIRE(timeline->allowClipMove(cid1, tid1, 0));
REQUIRE(timeline->requestClipMove(cid1, tid1, 0));
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 1);
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 1);
REQUIRE(timeline->getCompositionTrackId(cid1) == tid2);
REQUIRE(timeline->getCompositionPosition(cid1) == pos);
REQUIRE(timeline->getCompositionTrackId(cid2) == tid1);
REQUIRE(timeline->getCompositionPosition(cid2) == pos2);
REQUIRE(timeline->requestCompositionMove(cid1, tid1, 0));
REQUIRE(timeline->getTrackById(tid1)->checkConsistency());
REQUIRE(timeline->getTrackById(tid2)->checkConsistency());
REQUIRE(timeline->getTrackClipsCount(tid2) == 0);
REQUIRE(timeline->getTrackClipsCount(tid1) == 2);
REQUIRE(timeline->getClipTrackId(cid1) == tid1);
REQUIRE(timeline->getClipPosition(cid1) == 0);
REQUIRE(timeline->getClipTrackId(cid2) == tid1);
REQUIRE(timeline->getClipPosition(cid2) == pos2);
REQUIRE(timeline->getTrackCompositionsCount(tid2) == 0);
REQUIRE(timeline->getTrackCompositionsCount(tid1) == 2);
REQUIRE(timeline->getCompositionTrackId(cid1) == tid1);
REQUIRE(timeline->getCompositionPosition(cid1) == 0);
REQUIRE(timeline->getCompositionTrackId(cid2) == tid1);
REQUIRE(timeline->getCompositionPosition(cid2) == pos2);
}
*/
}
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