project: remove constant root node

This commit is contained in:
itsmattkc
2023-02-28 17:01:46 -08:00
parent b88b738071
commit b091ed8592
8 changed files with 36 additions and 33 deletions
+25 -20
View File
@@ -42,22 +42,19 @@ const QString Project::kCachePathKey = QStringLiteral("customcachepath");
const QString Project::kColorConfigFilename = QStringLiteral("colorconfigfilename");
const QString Project::kDefaultInputColorSpaceKey = QStringLiteral("defaultinputcolorspace");
const QString Project::kColorReferenceSpace = QStringLiteral("colorreferencespace");
const QString Project::kRootKey = QStringLiteral("root");
const QString Project::kItemMimeType = QStringLiteral("application/x-oliveprojectitemdata");
Project::Project() :
root_(nullptr),
is_modified_(false),
autorecovery_saved_(true)
{
// Generate UUID for this project
RegenerateUuid();
// Folder root for project
root_ = new Folder();
root_->setParent(this);
root_->SetLabel(tr("Root"));
AddDefaultNode(root_);
// Initialize color manager
color_manager_ = new ColorManager(this);
color_manager_->Init();
}
@@ -67,6 +64,16 @@ Project::~Project()
Clear();
}
void Project::Initialize()
{
if (!root_) {
root_ = new Folder();
root_->setParent(this);
root_->SetLabel(tr("Root"));
settings_.insert(kRootKey, QString::number(reinterpret_cast<quintptr>(root_)));
}
}
void Project::Clear()
{
// By deleting the last nodes first, we assume that nodes that are most important are deleted last
@@ -93,15 +100,12 @@ SerializedData Project::Load(QXmlStreamReader *reader)
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("node")) {
bool is_root = false;
QString id;
{
XMLAttributeLoop(reader, attr) {
if (attr.name() == QStringLiteral("id")) {
id = attr.value().toString();
} else if (attr.name() == QStringLiteral("root") && attr.value() == QStringLiteral("1")) {
is_root = true;
}
}
}
@@ -110,13 +114,7 @@ SerializedData Project::Load(QXmlStreamReader *reader)
qWarning() << "Failed to load node with empty ID";
reader->skipCurrentElement();
} else {
Node* node;
if (is_root) {
node = this->root();
} else {
node = NodeFactory::CreateFromID(id);
}
Node* node = NodeFactory::CreateFromID(id);
if (!node) {
qWarning() << "Failed to find node with ID" << id;
@@ -149,6 +147,17 @@ SerializedData Project::Load(QXmlStreamReader *reader)
}
}
// Resolve root if applicable
QString root = GetSetting(kRootKey);
if (!root.isEmpty()) {
quintptr r = root.toULongLong();
if (Node *n = data.node_ptrs.value(r)) {
Q_ASSERT(!root_);
root_ = dynamic_cast<Folder*>(n);
SetSetting(kRootKey, QString::number(reinterpret_cast<quintptr>(root_)));
}
}
return data;
}
@@ -164,10 +173,6 @@ void Project::Save(QXmlStreamWriter *writer) const
foreach (Node* node, this->nodes()) {
writer->writeStartElement(QStringLiteral("node"));
if (node == this->root()) {
writer->writeAttribute(QStringLiteral("root"), QStringLiteral("1"));
}
node->Save(writer);
writer->writeEndElement(); // node