File indexing completed on 2024-04-14 04:46:14

0001 /*
0002 SPDX-FileCopyrightText: 2012 Till Theato <root@ttill.de>
0003 SPDX-FileCopyrightText: 2014 Jean-Baptiste Mardelle <jb@kdenlive.org>
0004 This file is part of Kdenlive. See www.kdenlive.org.
0005 
0006 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "projectfolder.h"
0010 #include "bin.h"
0011 #include "core.h"
0012 #include "projectclip.h"
0013 #include "projectitemmodel.h"
0014 
0015 #include <KLocalizedString>
0016 #include <QDomElement>
0017 #include <utility>
0018 ProjectFolder::ProjectFolder(const QString &id, const QString &name, const std::shared_ptr<ProjectItemModel> &model)
0019     : AbstractProjectItem(AbstractProjectItem::FolderItem, id, model)
0020 {
0021     m_name = name;
0022     m_clipStatus = FileStatus::StatusReady;
0023 }
0024 
0025 std::shared_ptr<ProjectFolder> ProjectFolder::construct(const QString &id, const QString &name, std::shared_ptr<ProjectItemModel> model)
0026 {
0027     std::shared_ptr<ProjectFolder> self(new ProjectFolder(id, name, std::move(model)));
0028 
0029     baseFinishConstruct(self);
0030     return self;
0031 }
0032 
0033 ProjectFolder::ProjectFolder(const std::shared_ptr<ProjectItemModel> &model)
0034     : AbstractProjectItem(AbstractProjectItem::FolderItem, QString::number(-1), model, true)
0035 {
0036     m_name = QStringLiteral("root");
0037 }
0038 
0039 std::shared_ptr<ProjectFolder> ProjectFolder::construct(std::shared_ptr<ProjectItemModel> model)
0040 {
0041     std::shared_ptr<ProjectFolder> self(new ProjectFolder(std::move(model)));
0042 
0043     baseFinishConstruct(self);
0044     return self;
0045 }
0046 
0047 ProjectFolder::~ProjectFolder() = default;
0048 
0049 std::shared_ptr<ProjectClip> ProjectFolder::clip(const QString &id)
0050 {
0051     for (int i = 0; i < childCount(); ++i) {
0052         std::shared_ptr<ProjectClip> clip = std::static_pointer_cast<ProjectClip>(child(i))->clip(id);
0053         if (clip) {
0054             return clip;
0055         }
0056     }
0057     return std::shared_ptr<ProjectClip>();
0058 }
0059 
0060 QList<std::shared_ptr<ProjectClip>> ProjectFolder::childClips()
0061 {
0062     QList<std::shared_ptr<ProjectClip>> allChildren;
0063     for (int i = 0; i < childCount(); ++i) {
0064         std::shared_ptr<AbstractProjectItem> childItem = std::static_pointer_cast<AbstractProjectItem>(child(i));
0065         if (childItem->itemType() == ClipItem) {
0066             allChildren << std::static_pointer_cast<ProjectClip>(childItem);
0067         } else if (childItem->itemType() == FolderItem) {
0068             allChildren << std::static_pointer_cast<ProjectFolder>(childItem)->childClips();
0069         }
0070     }
0071     return allChildren;
0072 }
0073 
0074 QString ProjectFolder::childByHash(const QString &hash)
0075 {
0076     QList<std::shared_ptr<ProjectClip>> allChildren;
0077     for (int i = 0; i < childCount(); ++i) {
0078         std::shared_ptr<AbstractProjectItem> childItem = std::static_pointer_cast<AbstractProjectItem>(child(i));
0079         if (childItem->itemType() == ClipItem) {
0080             allChildren << std::static_pointer_cast<ProjectClip>(childItem);
0081         } else if (childItem->itemType() == FolderItem) {
0082             allChildren << std::static_pointer_cast<ProjectFolder>(childItem)->childClips();
0083         }
0084     }
0085     for (auto &clip : allChildren) {
0086         if (clip->statusReady() && clip->hash() == hash) {
0087             return clip->clipId();
0088         }
0089     }
0090     return QString();
0091 }
0092 
0093 bool ProjectFolder::hasChildClips() const
0094 {
0095     for (int i = 0; i < childCount(); ++i) {
0096         std::shared_ptr<AbstractProjectItem> childItem = std::static_pointer_cast<AbstractProjectItem>(child(i));
0097         if (childItem->itemType() == ClipItem) {
0098             return true;
0099         }
0100         if (childItem->itemType() == FolderItem) {
0101             bool hasChildren = std::static_pointer_cast<ProjectFolder>(childItem)->hasChildClips();
0102             if (hasChildren) {
0103                 return true;
0104             }
0105         }
0106     }
0107     return false;
0108 }
0109 
0110 QString ProjectFolder::getToolTip() const
0111 {
0112     return i18np("%1 clip", "%1 clips", childCount());
0113 }
0114 
0115 std::shared_ptr<ProjectFolder> ProjectFolder::folder(const QString &id)
0116 {
0117     if (m_binId == id) {
0118         return std::static_pointer_cast<ProjectFolder>(shared_from_this());
0119     }
0120     for (int i = 0; i < childCount(); ++i) {
0121         std::shared_ptr<ProjectFolder> folderItem = std::static_pointer_cast<ProjectFolder>(child(i))->folder(id);
0122         if (folderItem) {
0123             return folderItem;
0124         }
0125     }
0126     return std::shared_ptr<ProjectFolder>();
0127 }
0128 
0129 std::shared_ptr<ProjectClip> ProjectFolder::clipAt(int index)
0130 {
0131     if (childCount() == 0) {
0132         return std::shared_ptr<ProjectClip>();
0133     }
0134     for (int i = 0; i < childCount(); ++i) {
0135         std::shared_ptr<ProjectClip> clip = std::static_pointer_cast<AbstractProjectItem>(child(i))->clipAt(index);
0136         if (clip) {
0137             return clip;
0138         }
0139     }
0140     return std::shared_ptr<ProjectClip>();
0141 }
0142 
0143 void ProjectFolder::setBinEffectsEnabled(bool enabled)
0144 {
0145     for (int i = 0; i < childCount(); ++i) {
0146         std::shared_ptr<AbstractProjectItem> item = std::static_pointer_cast<AbstractProjectItem>(child(i));
0147         item->setBinEffectsEnabled(enabled);
0148     }
0149 }
0150 
0151 QDomElement ProjectFolder::toXml(QDomDocument &document, bool, bool)
0152 {
0153     QDomElement folder = document.createElement(QStringLiteral("folder"));
0154     folder.setAttribute(QStringLiteral("name"), name());
0155     for (int i = 0; i < childCount(); ++i) {
0156         folder.appendChild(std::static_pointer_cast<AbstractProjectItem>(child(i))->toXml(document));
0157     }
0158     return folder;
0159 }
0160 
0161 bool ProjectFolder::rename(const QString &name, int column)
0162 {
0163     Q_UNUSED(column)
0164     if (m_name == name) {
0165         return false;
0166     }
0167     // Rename folder
0168     if (auto ptr = m_model.lock()) {
0169         auto self = std::static_pointer_cast<ProjectFolder>(shared_from_this());
0170         return std::static_pointer_cast<ProjectItemModel>(ptr)->requestRenameFolder(self, name);
0171     }
0172     qDebug() << "ERROR: Impossible to rename folder because model is not available";
0173     Q_ASSERT(false);
0174     return false;
0175 }
0176 
0177 ClipType::ProducerType ProjectFolder::clipType() const
0178 {
0179     return ClipType::Unknown;
0180 }
0181 
0182 bool ProjectFolder::hasAudioAndVideo() const
0183 {
0184     return false;
0185 }