File indexing completed on 2024-05-12 16:01:59

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Saurabh Kumar <saurabhk660@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "StoryboardItem.h"
0008 
0009 #include <QDomElement>
0010 #include <QDomDocument>
0011 
0012 #include "kis_pointer_utils.h"
0013 
0014 StoryboardItem::StoryboardItem()
0015     : m_childData()
0016 {}
0017 
0018 StoryboardItem::StoryboardItem(const StoryboardItem& other)
0019     : QEnableSharedFromThis<StoryboardItem>()
0020     , m_childData()
0021 {
0022     cloneChildrenFrom(other);
0023 }
0024 
0025 StoryboardItem::~StoryboardItem()
0026 {
0027     m_childData.clear();
0028 }
0029 
0030 void StoryboardItem::appendChild(QVariant data)
0031 {
0032     QSharedPointer<StoryboardChild> child = toQShared( new StoryboardChild(data) );
0033     child->setParent(sharedFromThis());
0034     m_childData.append(child);
0035 }
0036 
0037 void StoryboardItem::cloneChildrenFrom(const StoryboardItem& other)
0038 {
0039     m_childData.clear();
0040     for (int i = 0; i < other.m_childData.count(); i++) {
0041         QSharedPointer<StoryboardChild> child = toQShared( new StoryboardChild(*other.m_childData.at(i)));
0042         child->setParent(sharedFromThis());
0043         m_childData.append(child);
0044     }
0045 }
0046 
0047 void StoryboardItem::insertChild(int row, QVariant data)
0048 {
0049     QSharedPointer<StoryboardChild> child = toQShared( new StoryboardChild(data) );
0050     child->setParent(sharedFromThis());
0051     m_childData.insert(row, child);
0052 }
0053 
0054 void StoryboardItem::removeChild(int row)
0055 {
0056     m_childData.removeAt(row);
0057 }
0058 
0059 void StoryboardItem::moveChild(int from, int to)
0060 {
0061     m_childData.move(from, to);
0062 }
0063 
0064 int StoryboardItem::childCount() const
0065 {
0066     return m_childData.count();
0067 }
0068 
0069 QSharedPointer<StoryboardChild> StoryboardItem::child(int row) const
0070 {
0071     if (row < 0 || row >= m_childData.size()) {
0072         return nullptr;
0073     }
0074     return m_childData.at(row);
0075 }
0076 
0077 QDomElement StoryboardItem::toXML(QDomDocument doc)
0078 {
0079     QDomElement itemElement = doc.createElement("storyboarditem");
0080 
0081     int frame = qvariant_cast<ThumbnailData>(child(FrameNumber)->data()).frameNum.toInt();
0082     itemElement.setAttribute("frame", frame);
0083     itemElement.setAttribute("item-name", child(ItemName)->data().toString());
0084     itemElement.setAttribute("duration-second", child(DurationSecond)->data().toInt());
0085     itemElement.setAttribute("duration-frame", child(DurationFrame)->data().toInt());
0086 
0087     for (int i = Comments; i < childCount(); i++) {
0088         CommentBox comment = qvariant_cast<CommentBox>(child(i)->data());
0089         QDomElement commentElement = doc.createElement("comment");
0090 
0091         commentElement.setAttribute("content", comment.content.toString());
0092         commentElement.setAttribute("scroll-value", comment.scrollValue.toInt());
0093 
0094         itemElement.appendChild(commentElement);
0095     }
0096 
0097     return itemElement;
0098 }
0099 
0100 void StoryboardItem::loadXML(const QDomElement &itemNode)
0101 {
0102     ThumbnailData thumbnail;
0103     thumbnail.frameNum = itemNode.attribute("frame").toInt();
0104     appendChild(QVariant::fromValue<ThumbnailData>(thumbnail));
0105     appendChild(itemNode.attribute("item-name"));
0106     appendChild(itemNode.attribute("duration-second").toInt());
0107     appendChild(itemNode.attribute("duration-frame").toInt());
0108 
0109     for (QDomElement commentNode = itemNode.firstChildElement(); !commentNode.isNull(); commentNode = commentNode.nextSiblingElement()) {
0110         if (commentNode.nodeName().toUpper() != "COMMENT") continue;
0111 
0112         CommentBox comment;
0113         if (commentNode.hasAttribute("content")) {
0114             comment.content = commentNode.attribute("content");
0115         }
0116         if (commentNode.hasAttribute("scroll-value")) {
0117             comment.scrollValue = commentNode.attribute("scroll-value");
0118         }
0119         appendChild(QVariant::fromValue<CommentBox>(comment));
0120     }
0121 }
0122 
0123 StoryboardItemList StoryboardItem::cloneStoryboardItemList(const StoryboardItemList &list)
0124 {
0125     StoryboardItemList clonedList;
0126     for (auto i = 0; i < list.count(); i++) {
0127         StoryboardItemSP item = toQShared( new StoryboardItem(*list.at(i)) );
0128         item->cloneChildrenFrom(*list.at(i));
0129         clonedList.append(item);
0130     }
0131     return clonedList;
0132 }