Fix new copy paste (fix crash and allow compositions)

parent 2b06bd1b
......@@ -249,3 +249,24 @@ int CompositionModel::getIn() const
{
return getPosition();
}
QDomElement CompositionModel::toXml(QDomDocument &document)
{
QDomElement container = document.createElement(QStringLiteral("composition"));
container.setAttribute(QStringLiteral("id"), m_id);
container.setAttribute(QStringLiteral("composition"), m_assetId);
container.setAttribute(QStringLiteral("in"), getIn());
container.setAttribute(QStringLiteral("out"), getOut());
container.setAttribute(QStringLiteral("position"), getPosition());
container.setAttribute(QStringLiteral("track"), getCurrentTrackId());
container.setAttribute(QStringLiteral("a_track"), getATrack());
QScopedPointer<Mlt::Properties> props(properties());
for (int i = 0; i < props->count(); i++) {
QString name = props->get_name(i);
if (name.startsWith(QLatin1Char('_'))) {
continue;
}
Xml::setXmlProperty(container, name, props->get(i));
}
return container;
}
......@@ -91,6 +91,9 @@ public:
const QString &displayName() const;
Mlt::Properties *properties();
/* @brief Returns an XML representation of the clip with its effects */
QDomElement toXml(QDomDocument &document);
protected:
Mlt::Transition *service() const override;
void setInOut(int in, int out) override;
......
......@@ -908,6 +908,11 @@ int TimelineFunctions::getOffsetTrackId(std::shared_ptr<TimelineItemModel> timel
while (offset != 0) {
masterTrackMltIndex += offset > 0 ? 1 : -1;
qDebug()<<"#### TESTING TRACK: "<<masterTrackMltIndex;
if (masterTrackMltIndex < 0) {
masterTrackMltIndex = 0;
} else if (masterTrackMltIndex > (int)timeline->m_allTracks.size() ) {
masterTrackMltIndex = timeline->m_allTracks.size();
}
int trackId = timeline->getTrackIndexFromPosition(masterTrackMltIndex - 1);
if (timeline->isAudioTrack(trackId) == isAudio) {
offset += offset > 0 ? -1 : 1;
......
......@@ -513,7 +513,7 @@ void TimelineController::copyItem()
}
std::unordered_set<int> selection = m_model->getGroupElements(id);
for (int j : selection) {
if (m_model->isClip(j) && !extraClips.contains(j)) {
if (!extraClips.contains(j)) {
extraClips << j;
}
}
......@@ -529,11 +529,13 @@ void TimelineController::copyItem()
QDomElement container = m_copiedItems.createElement(QStringLiteral("list"));
m_copiedItems.appendChild(container);
for (int id : extraClips) {
if (offset == -1 || m_model->getItemPosition(id) < offset) {
offset = m_model->getItemPosition(id);
}
if (m_model->isClip(id)) {
if (offset == -1 || m_model->getClipPosition(id) < offset) {
offset = m_model->getClipPosition(id);
}
container.appendChild(m_model->m_allClips[id]->toXml(m_copiedItems));
} else if (m_model->isComposition(id)) {
container.appendChild(m_model->m_allCompositions[id]->toXml(m_copiedItems));
}
}
container.setAttribute(QStringLiteral("offset"), offset);
......@@ -569,6 +571,7 @@ bool TimelineController::pasteItem(int clipId, int tid, int position)
position = timelinePosition();
}
QDomNodeList clips = m_copiedItems.documentElement().elementsByTagName(QStringLiteral("clip"));
QDomNodeList compositions = m_copiedItems.documentElement().elementsByTagName(QStringLiteral("composition"));
int offset = m_copiedItems.documentElement().attribute(QStringLiteral("offset")).toInt();
int masterTrack = m_copiedItems.documentElement().attribute(QStringLiteral("masterTrack")).toInt();
int trackOffset = TimelineFunctions::getTrackOffset(m_model, masterTrack, tid);
......@@ -588,6 +591,21 @@ bool TimelineController::pasteItem(int clipId, int tid, int position)
}
tracksMap.insert(trackId, TimelineFunctions::getOffsetTrackId(m_model, trackId, trackOffset, masterIsAudio));
}
for (int i = 0; i < compositions.count(); i++) {
QDomElement prod = compositions.at(i).toElement();
int trackId = prod.attribute(QStringLiteral("track")).toInt();
if (!tracksMap.contains(trackId)) {
tracksMap.insert(trackId, TimelineFunctions::getOffsetTrackId(m_model, trackId, trackOffset, masterIsAudio));
}
int atrackId = prod.attribute(QStringLiteral("a_track")).toInt();
if (atrackId == 0) {
continue;
}
atrackId = m_model->getTrackIndexFromPosition(atrackId);
if (!tracksMap.contains(atrackId)) {
tracksMap.insert(atrackId, TimelineFunctions::getOffsetTrackId(m_model, atrackId, trackOffset, masterIsAudio));
}
}
std::function<bool(void)> undo = []() { return true; };
std::function<bool(void)> redo = []() { return true; };
......@@ -599,7 +617,6 @@ bool TimelineController::pasteItem(int clipId, int tid, int position)
int in = prod.attribute(QStringLiteral("in")).toInt();
int out = prod.attribute(QStringLiteral("out")).toInt();
int trackId = prod.attribute(QStringLiteral("track")).toInt();
int mltTrackIndex = m_model->getTrackMltIndex(trackId);
int pos = prod.attribute(QStringLiteral("position")).toInt() - offset;
int newId;
res = m_model->requestClipCreation(originalId, newId, m_model->getTrackById_const(trackId)->trackType(), undo, redo);
......@@ -612,6 +629,27 @@ bool TimelineController::pasteItem(int clipId, int tid, int position)
destStack->fromXml(prod.firstChildElement(QStringLiteral("effects")), undo, redo);
}
}
// Compositions
for (int i = 0; res && i < compositions.count(); i++) {
QDomElement prod = compositions.at(i).toElement();
QString originalId = prod.attribute(QStringLiteral("composition"));
int in = prod.attribute(QStringLiteral("in")).toInt();
int out = prod.attribute(QStringLiteral("out")).toInt();
int trackId = prod.attribute(QStringLiteral("track")).toInt();
int aTrackId = prod.attribute(QStringLiteral("a_track")).toInt();
if (aTrackId > 0) {
aTrackId = tracksMap.value(m_model->getTrackIndexFromPosition(aTrackId));
}
int pos = prod.attribute(QStringLiteral("position")).toInt() - offset;
int newId;
Mlt::Properties transProps;
QDomNodeList props = prod.elementsByTagName(QStringLiteral("property"));
for (int j = 0; j < props.count(); j++) {
transProps.set(props.at(j).toElement().attribute(QStringLiteral("name")).toUtf8().constData(), props.at(j).toElement().text().toUtf8().constData());
}
res = m_model->requestCompositionInsertion(originalId, tracksMap.value(trackId), aTrackId, position + pos, out - in, &transProps, newId, undo, redo);
}
if (!res) {
undo();
return false;
......
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