Commit 2cd6cca5 authored by Nicolas Carion's avatar Nicolas Carion

[Timeline2][Model] Track deletion mechanism

parent 45c365b0
......@@ -22,6 +22,7 @@
#include "timelinemodel.hpp"
#include "trackmodel.hpp"
#include "clipmodel.hpp"
#include <mlt++/MltTractor.h>
......@@ -34,6 +35,7 @@ TimelineModel::TimelineModel() :
void TimelineModel::registerTrack(std::unique_ptr<TrackModel>&& track, int pos)
{
int id = track->getId();
if (pos == -1) {
pos = m_allTracks.size();
}
......@@ -42,9 +44,20 @@ void TimelineModel::registerTrack(std::unique_ptr<TrackModel>&& track, int pos)
Q_ASSERT(error == 0); //we might need better error handling...
// we now insert in the list
auto it = m_allTracks.begin();
std::advance(it, pos);
m_allTracks.insert(it, std::move(track));
auto posIt = m_allTracks.begin();
std::advance(posIt, pos);
auto it = m_allTracks.insert(posIt, std::move(track));
//it now contains the iterator to the inserted element, we store it
Q_ASSERT(m_iteratorTable.count(id) == 0); //check that id is not used (shouldn't happen)
m_iteratorTable[id] = it;
}
void TimelineModel::deregisterTrack(int id)
{
auto it = m_iteratorTable[id]; //iterator to the element
m_iteratorTable.erase(id);
int index = std::distance(m_allTracks.begin(), it);
m_tractor.remove_track(index);
m_allTracks.erase(it);
}
......@@ -20,6 +20,7 @@
***************************************************************************/
#include <memory>
#include <unordered_map>
#include <QVector>
#include <mlt++/MltTractor.h>
......@@ -38,11 +39,16 @@ protected:
@param pos indicates the number of the track we are adding. If this is -1, then we add at the end.
*/
void registerTrack(std::unique_ptr<TrackModel>&& track, int pos = -1);
/* @brief Deregister and destruct the track with given id.
*/
void deregisterTrack(int id);
private:
Mlt::Tractor m_tractor;
QVector<int> m_snapPoints; // this will be modified from a lot of different places, we will probably need a mutex
// QList<std::unique_ptr<TrackModel>> m_allTracks;
std::list<std::unique_ptr<TrackModel>> m_allTracks;
std::unordered_map<int, std::list<std::unique_ptr<TrackModel>>::iterator> m_iteratorTable; //this logs the iterator associated which each track id. This allows easy access of a track based on its id.
};
......@@ -24,8 +24,12 @@
#include "clipmodel.hpp"
#include <QDebug>
int TrackModel::next_id = 0;
TrackModel::TrackModel(std::weak_ptr<TimelineModel> parent) :
m_parent(parent)
, m_id(TrackModel::next_id++)
{
}
......@@ -38,3 +42,19 @@ void TrackModel::construct(std::weak_ptr<TimelineModel> parent)
Q_ASSERT(false);
}
}
void TrackModel::destruct()
{
if (auto ptr = m_parent.lock()) {
// ptr->deregisterTrack(m_id);
} else {
qDebug() << "Error : destruction of track failed because parent timeline is not available anymore";
Q_ASSERT(false);
}
}
int TrackModel::getId() const
{
return m_id;
}
......@@ -35,17 +35,19 @@ class TrackModel
public:
TrackModel() = delete;
friend class TimelineModel;
private:
/* This constructor is private, call the static construct instead */
TrackModel(std::weak_ptr<TimelineModel> parent);
public:
/* @brief Creates a track, which reference itself to the parent
/* @brief Creates a track, which references itself to the parent
*/
static void construct(std::weak_ptr<TimelineModel> parent);
/* @brief The destructor. It notifies the parent of the destruction
/* @brief The destructor. It asks the parent to be deleted
*/
~TrackModel();
void destruct();
/* Perform a resize operation on a clip. Returns true if the operation succeeded*/
bool requestClipResize(QSharedPointer<ClipModel> caller, int newSize);
......@@ -60,14 +62,22 @@ public:
*/
operator Mlt::Producer&(){ return m_playlist;}
protected:
/*@brief Returns the (unique) construction id of the track*/
int getId() const;
public slots:
/*Delete the current track and all its associated clips */
void slotDelete();
private:
std::weak_ptr<TimelineModel> m_parent;
int m_id; //this is the creation id of the track, used for book-keeping
Mlt::Playlist m_playlist;
std::vector<std::unique_ptr<ClipModel>> m_allClips;
static int next_id; //next valid id to assign
};
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