Commit 10b07330 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Fix track insert/delete

parent de70a8e8
......@@ -66,7 +66,7 @@ bool constructTimelineFromMelt(const std::shared_ptr<TimelineItemModel> &timelin
case tractor_type: {
// that is a double track
int tid;
ok = timeline->requestTrackInsertion(-1, tid, undo, redo);
ok = timeline->requestTrackInsertion(-1, tid, QString(), false, undo, redo);
Mlt::Tractor local_tractor(*track);
ok = ok && constructTrackFromMelt(timeline, tid, local_tractor, undo, redo);
break;
......@@ -75,19 +75,11 @@ bool constructTimelineFromMelt(const std::shared_ptr<TimelineItemModel> &timelin
// that is a single track
qDebug() << "Adding track: " << track->get("id");
int tid;
ok = timeline->requestTrackInsertion(-1, tid, undo, redo);
timeline->setTrackProperty(tid, "kdenlive:trackheight", QString::number(KdenliveSettings::trackheight()));
Mlt::Playlist local_playlist(*track);
ok = ok && constructTrackFromMelt(timeline, tid, local_playlist, undo, redo);
QString trackName = local_playlist.get("kdenlive:track_name");
if (!trackName.isEmpty()) {
timeline->setTrackProperty(tid, QStringLiteral("kdenlive:track_name"), trackName.toUtf8().constData());
}
const QString trackName = local_playlist.get("kdenlive:track_name");
int audioTrack = local_playlist.get_int("kdenlive:audio_track");
if (audioTrack == 1) {
// This is an audio track
timeline->setTrackProperty(tid, QStringLiteral("kdenlive:audio_track"), QStringLiteral("1"));
}
ok = timeline->requestTrackInsertion(-1, tid, trackName, audioTrack == 1, undo, redo);
ok = ok && constructTrackFromMelt(timeline, tid, local_playlist, undo, redo);
break;
}
default:
......@@ -95,7 +87,6 @@ bool constructTimelineFromMelt(const std::shared_ptr<TimelineItemModel> &timelin
}
}
// build internal track compositing
timeline->buildTrackCompositing();
......
......@@ -876,7 +876,7 @@ bool TimelineModel::requestClipUngroup(int id, Fun &undo, Fun &redo, bool tempor
return m_groups->ungroupItem(id, undo, redo, temporarySelection);
}
bool TimelineModel::requestTrackInsertion(int position, int &id)
bool TimelineModel::requestTrackInsertion(int position, int &id, const QString &trackName, bool audioTrack)
{
#ifdef LOGGING
m_logFile << "timeline->requestTrackInsertion(" << position << ", dummy_id ); " << std::endl;
......@@ -884,14 +884,14 @@ bool TimelineModel::requestTrackInsertion(int position, int &id)
QWriteLocker locker(&m_lock);
Fun undo = []() { return true; };
Fun redo = []() { return true; };
bool result = requestTrackInsertion(position, id, undo, redo);
bool result = requestTrackInsertion(position, id, trackName, audioTrack, undo, redo);
if (result) {
PUSH_UNDO(undo, redo, i18n("Insert Track"));
}
return result;
}
bool TimelineModel::requestTrackInsertion(int position, int &id, Fun &undo, Fun &redo)
bool TimelineModel::requestTrackInsertion(int position, int &id, const QString &trackName, bool audioTrack, Fun &undo, Fun &redo)
{
// TODO: make sure we disable overlayTrack before inserting a track
if (position == -1) {
......@@ -902,8 +902,8 @@ bool TimelineModel::requestTrackInsertion(int position, int &id, Fun &undo, Fun
}
int trackId = TimelineModel::getNextId();
id = trackId;
Fun local_undo = deregisterTrack_lambda(trackId);
TrackModel::construct(shared_from_this(), trackId, position);
Fun local_undo = deregisterTrack_lambda(trackId, true);
TrackModel::construct(shared_from_this(), trackId, position, trackName, audioTrack);
auto track = getTrackById(trackId);
Fun local_redo = [track, position, this]() {
// We capture a shared_ptr to the track, which means that as long as this undo object lives, the track object is not deleted. To insert it back it is
......@@ -1021,14 +1021,14 @@ Fun TimelineModel::deregisterTrack_lambda(int id, bool updateView)
qDebug() << "DEREGISTER TRACK" << id;
auto it = m_iteratorTable[id]; // iterator to the element
int index = getTrackPosition(id); // compute index in list
if (updateView) {
QModelIndex root;
_resetView();
}
m_tractor->remove_track(static_cast<int>(index + 1)); // melt operation, add 1 to account for black background track
// send update to the model
m_allTracks.erase(it); // actual deletion of object
m_iteratorTable.erase(id); // clean table
if (updateView) {
QModelIndex root;
_resetView();
}
return true;
};
}
......
......@@ -389,9 +389,9 @@ public:
@param Requested position (order). If set to -1, the track is inserted last.
@param id is a return parameter that holds the id of the resulting track (-1 on failure)
*/
bool requestTrackInsertion(int pos, int &id);
bool requestTrackInsertion(int pos, int &id, const QString &trackName = QString(), bool audioTrack = false);
/* Same function, but accumulates undo and redo*/
bool requestTrackInsertion(int pos, int &id, Fun &undo, Fun &redo);
bool requestTrackInsertion(int pos, int &id, const QString &trackName, bool audioTrack, Fun &undo, Fun &redo);
/* @brief Delete track with given id
This also deletes all the clips contained in the track.
......
......@@ -24,12 +24,13 @@
#include "compositionmodel.hpp"
#include "snapmodel.hpp"
#include "timelinemodel.hpp"
#include "kdenlivesettings.h"
#include <QDebug>
#include <QModelIndex>
#include <mlt++/MltProfile.h>
#include <mlt++/MltTransition.h>
TrackModel::TrackModel(const std::weak_ptr<TimelineModel> &parent, int id)
TrackModel::TrackModel(const std::weak_ptr<TimelineModel> &parent, int id, const QString &trackName, bool audioTrack)
: m_parent(parent)
, m_id(id == -1 ? TimelineModel::getNextId() : id)
, m_lock(QReadWriteLock::Recursive)
......@@ -40,6 +41,16 @@ TrackModel::TrackModel(const std::weak_ptr<TimelineModel> &parent, int id)
m_playlists[1].set_profile(*ptr->getProfile());
m_track->insert_track(m_playlists[0], 0);
m_track->insert_track(m_playlists[1], 1);
if (!trackName.isEmpty()) {
m_track->set("kdenlive:track_name", trackName.toUtf8().constData());
}
if (audioTrack) {
m_track->set("kdenlive:audio_track", 1);
for (int i = 0; i < 2; i++) {
m_playlists[i].set("hide", 1);
}
}
m_track->set("kdenlive:trackheight", KdenliveSettings::trackheight());
} else {
qDebug() << "Error : construction of track failed because parent timeline is not available anymore";
Q_ASSERT(false);
......@@ -66,9 +77,9 @@ TrackModel::~TrackModel()
m_track->remove_track(0);
}
int TrackModel::construct(const std::weak_ptr<TimelineModel> &parent, int id, int pos)
int TrackModel::construct(const std::weak_ptr<TimelineModel> &parent, int id, int pos, const QString &trackName, bool audioTrack)
{
std::shared_ptr<TrackModel> track(new TrackModel(parent, id));
std::shared_ptr<TrackModel> track(new TrackModel(parent, id, trackName, audioTrack));
id = track->m_id;
if (auto ptr = parent.lock()) {
ptr->registerTrack(std::move(track), pos);
......
......@@ -55,7 +55,7 @@ public:
private:
/* This constructor is private, call the static construct instead */
TrackModel(const std::weak_ptr<TimelineModel> &parent, int id = -1);
TrackModel(const std::weak_ptr<TimelineModel> &parent, int id = -1, const QString &trackName = QString(), bool audioTrack = false);
TrackModel(const std::weak_ptr<TimelineModel> &parent, Mlt::Tractor mltTrack, int id = -1);
public:
......@@ -64,7 +64,7 @@ public:
@param id Requested id of the track. Automatic if id = -1
@param pos is the optional position of the track. If left to -1, it will be added at the end
*/
static int construct(const std::weak_ptr<TimelineModel> &parent, int id = -1, int pos = -1);
static int construct(const std::weak_ptr<TimelineModel> &parent, int id = -1, int pos = -1, const QString &trackName = QString(), bool audioTrack = false);
/* @brief returns the number of clips */
int getClipsCount();
......
......@@ -23,7 +23,7 @@
#include <QIcon>
TrackDialog::TrackDialog(std::shared_ptr<TimelineItemModel> model, int trackIndex, QWidget *parent) :
TrackDialog::TrackDialog(std::shared_ptr<TimelineItemModel> model, int trackIndex, QWidget *parent, bool deleteMode) :
QDialog(parent)
{
//setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
......@@ -45,11 +45,20 @@ TrackDialog::TrackDialog(std::shared_ptr<TimelineItemModel> model, int trackInde
comboTracks->setCurrentIndex(ix);
}
trackIndex--;
QString proposedName = i18n("Video %1", trackIndex);
while (existingTrackNames.contains(proposedName)) {
proposedName = i18n("Video %1", ++trackIndex);
if (deleteMode) {
track_name->setVisible(false);
video_track->setVisible(false);
audio_track->setVisible(false);
name_label->setVisible(false);
before_select->setVisible(false);
label->setText(i18n("Delete Track"));
} else {
QString proposedName = i18n("Video %1", trackIndex);
while (existingTrackNames.contains(proposedName)) {
proposedName = i18n("Video %1", ++trackIndex);
}
track_name->setText(proposedName);
}
track_name->setText(proposedName);
}
int TrackDialog::selectedTrack() const
......
......@@ -28,7 +28,7 @@ class TrackDialog : public QDialog, public Ui::AddTrack_UI
Q_OBJECT
public:
explicit TrackDialog(std::shared_ptr<TimelineItemModel> model, int trackIndex = -1, QWidget *parent = nullptr);
explicit TrackDialog(std::shared_ptr<TimelineItemModel> model, int trackIndex = -1, QWidget *parent = nullptr, bool deleteMode = false);
/** @brief: returns the selected track's trackId
*/
int selectedTrack() const;
......
......@@ -330,12 +330,10 @@ Rectangle {
id: headerMenu
MenuItem {
text: i18n('Add Track')
shortcut: 'Ctrl+U'
onTriggered: timeline.addTrack(tracksRepeater.itemAt(currentTrack).trackId);
}
MenuItem {
text: i18n('Delete Track')
//shortcut: 'Ctrl+U'
onTriggered: timeline.deleteTrack(tracksRepeater.itemAt(currentTrack).trackId);
}
}
......
......@@ -340,38 +340,21 @@ bool TimelineController::showWaveforms() const
void TimelineController::addTrack(int tid)
{
qDebug() << "Adding track: " << tid<< " = " << m_model->getTrackMltIndex(tid);
QPointer<TrackDialog> d = new TrackDialog(m_model, m_model->getTrackMltIndex(tid), qApp->activeWindow());
if (d->exec() == QDialog::Accepted) {
int mltIndex = d->selectedTrack();
int tid;
qDebug()<<"// INSERT TRACK: "<<mltIndex;
m_model->requestTrackInsertion(mltIndex, tid);
m_model->setTrackProperty(tid, "kdenlive:trackheight", QString::number(KdenliveSettings::trackheight()));
m_model->setTrackProperty(tid, QStringLiteral("kdenlive:track_name"), d->trackName().toUtf8().constData());
if (d->addAudioTrack()) {
m_model->setTrackProperty(tid, QStringLiteral("kdenlive:audio_track"), QStringLiteral("1"));
}
}
/*d->comboTracks->setCurrentIndex(m_timeline->visibleTracksCount() - ix);
d->label->setText(i18n("Insert track"));
QStringList existingTrackNames = m_timeline->getTrackNames();
int i = 1;
QString proposedName = i18n("Video %1", i);
while (existingTrackNames.contains(proposedName)) {
proposedName = i18n("Video %1", ++i);
m_model->requestTrackInsertion(mltIndex, tid, d->trackName(), d->addAudioTrack());
}
d->track_name->setText(proposedName);
d->setWindowTitle(i18n("Insert New Track"));*/
//info.trackName = d->track_name->text();
//auto *addTrack = new AddTrackCommand(this, ix, info, true);
//m_commandStack->push(addTrack);
}
void TimelineController::deleteTrack(int tid)
{
qDebug() << "Deleting track: " << tid;
QPointer<TrackDialog> d = new TrackDialog(m_model, m_model->getTrackMltIndex(tid), qApp->activeWindow(), true);
if (d->exec() == QDialog::Accepted) {
int mltIndex = d->selectedTrack();
m_model->requestTrackDeletion(mltIndex);
}
}
void TimelineController::gotoNextSnap()
......
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