Commit 56573c12 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Fix crash when deleting folders / clips that were parents

CCBUG: 346413
parent 46c92cee
......@@ -439,7 +439,10 @@ void Bin::deleteClip(const QString &id)
emit openClip(NULL);
}
ProjectClip *clip = m_rootFolder->clip(id);
if (!clip) return;
if (!clip) {
qWarning()<<"Cannot bin find clip to delete: "<<id;
return;
}
m_jobManager->discardJobs(id);
AbstractProjectItem *parent = clip->parent();
parent->removeChild(clip);
......@@ -474,31 +477,71 @@ void Bin::slotDeleteClip()
ProjectSubClip *sub;
QString subId;
QPoint zone;
// check folders, remove child folders if there is any
QList <ProjectFolder*> topFolders;
foreach (const QModelIndex &ix, indexes) {
if (!ix.isValid() || ix.column() != 0) continue;
AbstractProjectItem *item = static_cast<AbstractProjectItem*>(m_proxyModel->mapToSource(ix).internalPointer());
if (!item) continue;
AbstractProjectItem::PROJECTITEMTYPE type = item->itemType();
switch (type) {
case AbstractProjectItem::ClipItem:
clipIds << item->clipId();
break;
case AbstractProjectItem::FolderItem:
foldersIds << item->clipId();
break;
case AbstractProjectItem::SubClipItem:
subId = item->clipId();
if (!item) continue;
if (item->itemType() == AbstractProjectItem::SubClipItem) {
QString subId = item->clipId();
sub = static_cast<ProjectSubClip*>(item);
zone = sub->zone();
subId.append(":" + QString::number(zone.x()) + ":" + QString::number(zone.y()));
subClipIds << subId;
break;
default:
break;
}
continue;
}
if (item->itemType() != AbstractProjectItem::FolderItem) continue;
ProjectFolder *current = static_cast<ProjectFolder*>(item);
if (topFolders.isEmpty()) {
topFolders << current;
continue;
}
// parse all folders to check for children
bool isChild = false;
foreach (ProjectFolder *f, topFolders) {
if (f->folder(current->clipId())) {
// Current is a child, no need to take it into account
isChild = true;
break;
}
}
if (isChild) continue;
QList <ProjectFolder*> childFolders;
// parse all folders to check for children
foreach (ProjectFolder *f, topFolders) {
if (current->folder(f->clipId())) {
childFolders << f;
}
}
if (!childFolders.isEmpty()) {
// children are in the list, remove from
foreach (ProjectFolder *f, childFolders) {
topFolders.removeAll(f);
}
}
topFolders << current;
}
foreach (const ProjectFolder *f, topFolders) {
foldersIds << f->clipId();
}
QList <ProjectFolder*> topClips;
// Check if clips are in already selected folders
foreach (const QModelIndex &ix, indexes) {
if (!ix.isValid() || ix.column() != 0) continue;
AbstractProjectItem *item = static_cast<AbstractProjectItem*>(m_proxyModel->mapToSource(ix).internalPointer());
if (!item || item->itemType() != AbstractProjectItem::ClipItem) continue;
ProjectClip *current = static_cast<ProjectClip*>(item);
bool isChild = false;
foreach (const ProjectFolder *f, topFolders) {
if (current->hasParent(f->clipId())) {
isChild = true;
break;
}
}
if (!isChild) clipIds << current->clipId();
}
// For some reason, we get duplicates, which is not expected
//ids.removeDuplicates();
m_doc->clipManager()->deleteProjectItems(clipIds, foldersIds, subClipIds);
}
......@@ -804,9 +847,6 @@ void Bin::removeFolder(const QString &id, QUndoCommand *deleteCommand)
break;
}
}
foreach(const QString &folderId, folderIds) {
removeFolder(folderId, deleteCommand);
}
m_doc->clipManager()->deleteProjectItems(clipIds, folderIds, QStringList(), deleteCommand);
}
new AddBinFolderCommand(this, folder->clipId(), folder->name(), parent->clipId(), true, deleteCommand);
......@@ -1233,7 +1273,6 @@ void Bin::showClipProperties(ProjectClip *clip)
m_collapser->collapse();
return;
}
if (clip) qDebug()<<"+ + +SHOWING CLP PROPS: "<<clip->clipType();
if (clip && clip->clipType() == SlideShow) {
// Cleanup widget for new content
foreach (QWidget * w, m_propertiesPanel->findChildren<ClipPropertiesController*>()) {
......
......@@ -139,6 +139,18 @@ ClipType ProjectClip::clipType() const
return m_type;
}
bool ProjectClip::hasParent(const QString &id) const
{
AbstractProjectItem *par = parent();
while (par) {
if (par->clipId() == id) {
return true;
}
par = par->parent();
}
return false;
}
ProjectClip* ProjectClip::clip(const QString &id)
{
if (id == m_id) {
......
......@@ -83,6 +83,9 @@ public:
/** @brief Returns the clip type as defined in definitions.h */
ClipType clipType() const;
/** @brief Check if clip has a parent folder with id id */
bool hasParent(const QString &id) const;
ClipPropertiesController *buildProperties(QWidget *parent);
QPoint zone() const;
......
......@@ -68,7 +68,6 @@ ProjectClip* ProjectFolder::clip(const QString &id)
return NULL;
}
QString ProjectFolder::getToolTip() const
{
return QString(i18np("%1 clip", "%1 clips", size()));
......
......@@ -1096,6 +1096,11 @@ bool KdenliveDoc::addClipInfo(QDomElement elem, QDomElement orig, const QString
void KdenliveDoc::deleteClip(const QString &clipId)
{
ClipController *controller = pCore->binController()->getController(clipId);
if (!controller) {
// Clip doesn't exist, something is wrong
qWarning()<<"// Document error deleting clip: "<<clipId;
return;
}
ClipType type = controller->clipType();
QString url = controller->clipUrl().toLocalFile();
if (type != Color && type != SlideShow && !url.isEmpty()) {
......
......@@ -239,7 +239,6 @@ void ClipManager::deleteProjectItems(QStringList clipIds, QStringList folderIds,
}
}
// remove clips and folders from bin
qDebug()<<" + ++ +DELETING CLIPs: "<<clipIds;
slotDeleteClips(clipIds, folderIds, subClipIds, deleteCommand, execute);
}
}
......@@ -248,10 +247,10 @@ void ClipManager::slotDeleteClips(QStringList clipIds, QStringList folderIds, QS
{
for (int i = 0; i < clipIds.size(); ++i) {
QString xml = pCore->binController()->xmlFromId(clipIds.at(i));
QDomDocument doc;
doc.setContent(xml);
if (!xml.isNull()) {
new AddClipCommand(m_doc, doc.documentElement(), clipIds.at(i), false, deleteCommand);
if (!xml.isEmpty()) {
QDomDocument doc;
doc.setContent(xml);
new AddClipCommand(m_doc, doc.documentElement(), clipIds.at(i), false, deleteCommand);
}
}
for (int i = 0; i < folderIds.size(); ++i) {
......@@ -262,7 +261,6 @@ void ClipManager::slotDeleteClips(QStringList clipIds, QStringList folderIds, QS
}
if (execute) {
qDebug()<<" // / /EXECUTE DELETION";
m_doc->commandStack()->push(deleteCommand);
}
}
......
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