diff -r 639a900999bc -r fdc285e5952f src/ldDocument.cc --- a/src/ldDocument.cc Thu Apr 17 21:51:59 2014 +0300 +++ b/src/ldDocument.cc Fri Apr 18 18:46:25 2014 +0300 @@ -124,6 +124,7 @@ // ============================================================================= // LDDocument::LDDocument() : + m_flags (0), m_gldata (new LDGLData) { setImplicit (true); @@ -141,7 +142,7 @@ // Remove this file from the list of files. This MUST be done FIRST, otherwise // a ton of other functions will think this file is still valid when it is not! g_loadedFiles.removeOne (this); - + m_flags |= DOCF_IsBeingDestroyed; m_history->setIgnoring (true); // Clear everything from the model @@ -471,7 +472,7 @@ // ============================================================================= // -LDDocument* openDocument (QString path, bool search) +LDDocument* openDocument (QString path, bool search, bool implicit) { // Convert the file name to lowercase since some parts contain uppercase // file names. I'll assume here that the library will always use lowercase @@ -497,6 +498,7 @@ return null; LDDocument* load = new LDDocument; + load->setImplicit (implicit); load->setFullPath (fullpath); load->setName (LDDocument::shortenName (load->fullPath())); dprint ("name: %1 (%2)", load->name(), load->fullPath()); @@ -671,7 +673,7 @@ return; } - LDDocument* file = openDocument (path, false); + LDDocument* file = openDocument (path, false, false); if (not file) { @@ -1024,7 +1026,7 @@ // If it's not loaded, try open it if (not doc) - doc = openDocument (filename, true); + doc = openDocument (filename, true, true); return doc; } @@ -1066,9 +1068,7 @@ { history()->add (new AddHistory (objects().size(), obj)); m_objects << obj; - - if (obj->type() == LDObject::EVertex) - m_vertices << obj; + addKnownVerticesOf (obj); #ifdef DEBUG if (not isImplicit()) @@ -1097,6 +1097,7 @@ m_objects.insert (pos, obj); obj->setDocument (this); g_win->R()->compileObject (obj); + addKnownVerticesOf (obj); #ifdef DEBUG if (not isImplicit()) @@ -1106,14 +1107,61 @@ // ============================================================================= // +void LDDocument::addKnownVerticesOf (LDObject* obj) +{ + if (isImplicit()) + return; + + if (obj->type() == LDObject::ESubfile) + { + for (LDObject* sub : static_cast (obj)->inlineContents (true, false)) + { + addKnownVerticesOf (sub); + sub->destroy(); + } + } + else + { + for (int i = 0; i < obj->vertices(); ++i) + addKnownVertexReference (obj->vertex (i)); + } +} + +// ============================================================================= +// +void LDDocument::removeKnownVerticesOf (LDObject* obj) +{ + if (isImplicit()) + return; + + if (obj->type() == LDObject::ESubfile) + { + for (LDObject* sub : static_cast (obj)->inlineContents (true, false)) + { + removeKnownVerticesOf (sub); + sub->destroy(); + } + } + else + { + for (int i = 0; i < obj->vertices(); ++i) + removeKnownVertexReference (obj->vertex (i)); + } +} + +// ============================================================================= +// void LDDocument::forgetObject (LDObject* obj) { int idx = obj->lineNumber(); obj->unselect(); assert (m_objects[idx] == obj); - if (not history()->isIgnoring()) + if (not isImplicit() && not (flags() & DOCF_IsBeingDestroyed)) + { history()->add (new DelHistory (idx, obj)); + removeKnownVerticesOf (obj); + } m_objects.removeAt (idx); obj->setDocument (null); @@ -1121,6 +1169,44 @@ // ============================================================================= // +void LDDocument::vertexChanged (const Vertex& a, const Vertex& b) +{ + removeKnownVertexReference (a); + addKnownVertexReference (b); +} + +// ============================================================================= +// +void LDDocument::addKnownVertexReference (const Vertex& a) +{ + if (isImplicit()) + return; + + auto it = m_vertices.find (a); + + if (it == m_vertices.end()) + m_vertices[a] = 1; + else + ++it.value(); +} + +// ============================================================================= +// +void LDDocument::removeKnownVertexReference (const Vertex& a) +{ + if (isImplicit()) + return; + + auto it = m_vertices.find (a); + assert (it != m_vertices.end()); + + // If there's no more references to a given vertex, it is to be removed. + if (--it.value() == 0) + m_vertices.erase (it); +} + +// ============================================================================= +// bool safeToCloseAll() { for (LDDocument* f : g_loadedFiles) @@ -1144,9 +1230,11 @@ *m_history << new EditHistory (idx, oldcode, newcode); } + removeKnownVerticesOf (m_objects[idx]); m_objects[idx]->unselect(); m_objects[idx]->setDocument (null); obj->setDocument (this); + addKnownVerticesOf (obj); g_win->R()->compileObject (obj); m_objects[idx] = obj; } @@ -1354,8 +1442,8 @@ delete g_logoedStud; delete g_logoedStud2; - g_logoedStud = openDocument ("stud-logo.dat", true); - g_logoedStud2 = openDocument ("stud2-logo.dat", true); + g_logoedStud = openDocument ("stud-logo.dat", true, true); + g_logoedStud2 = openDocument ("stud2-logo.dat", true, true); print (LDDocument::tr ("Logoed studs loaded.\n")); }