# HG changeset patch # User Teemu Piippo # Date 1485697776 -7200 # Node ID c72e3115a297bc00973f665868a058f242637cb6 # Parent 952d6b3e7d11fc0d392e1683ff4bbb4591a0349b Removed uses of LDSpawn in the algorithm toolset diff -r 952d6b3e7d11 -r c72e3115a297 src/ldObject.cpp --- a/src/ldObject.cpp Sun Jan 29 15:39:35 2017 +0200 +++ b/src/ldObject.cpp Sun Jan 29 15:49:36 2017 +0200 @@ -793,9 +793,9 @@ m_fileReferenced = value; } -LDComment::LDComment (QString text, LDDocument* document) : - LDObject (document), - m_text (text) {} +LDComment::LDComment (QString text, Model* model) : + LDObject {model}, + m_text {text} {} QString LDComment::text() const { @@ -807,9 +807,9 @@ changeProperty (this, &m_text, value); } -LDBfc::LDBfc (const BfcStatement type, LDDocument* document) : - LDObject (document), - m_statement (type) {} +LDBfc::LDBfc (const BfcStatement type, Model* model) : + LDObject {model}, + m_statement {type} {} BfcStatement LDBfc::statement() const { diff -r 952d6b3e7d11 -r c72e3115a297 src/ldObject.h --- a/src/ldObject.h Sun Jan 29 15:39:35 2017 +0200 +++ b/src/ldObject.h Sun Jan 29 15:49:36 2017 +0200 @@ -248,7 +248,7 @@ LDOBJ_NO_MATRIX public: - LDComment (QString text, LDDocument* model = nullptr); + LDComment (QString text, Model* model = nullptr); QString text() const; void setText (QString value); @@ -287,7 +287,7 @@ LDOBJ_NO_MATRIX public: - LDBfc (const BfcStatement type, LDDocument* model = nullptr); + LDBfc (const BfcStatement type, Model* model = nullptr); BfcStatement statement() const; void setStatement (BfcStatement value); diff -r 952d6b3e7d11 -r c72e3115a297 src/toolsets/algorithmtoolset.cpp --- a/src/toolsets/algorithmtoolset.cpp Sun Jan 29 15:39:35 2017 +0200 +++ b/src/toolsets/algorithmtoolset.cpp Sun Jan 29 15:49:36 2017 +0200 @@ -116,46 +116,38 @@ void AlgorithmToolset::makeBorders() { - int num = 0; + int count = 0; - for (LDObject* obj : selectedObjects()) + for (LDObject* object : selectedObjects()) { - const LDObjectType type = obj->type(); + const LDObjectType type = object->type(); if (type != OBJ_Quad and type != OBJ_Triangle) continue; - LDLine* lines[4]; + Model lines; if (type == OBJ_Quad) { - LDQuad* quad = static_cast (obj); - lines[0] = LDSpawn (quad->vertex (0), quad->vertex (1)); - lines[1] = LDSpawn (quad->vertex (1), quad->vertex (2)); - lines[2] = LDSpawn (quad->vertex (2), quad->vertex (3)); - lines[3] = LDSpawn (quad->vertex (3), quad->vertex (0)); + LDQuad* quad = static_cast(object); + lines.emplace(quad->vertex (0), quad->vertex (1)); + lines.emplace(quad->vertex (1), quad->vertex (2)); + lines.emplace(quad->vertex (2), quad->vertex (3)); + lines.emplace(quad->vertex (3), quad->vertex (0)); } else { - LDTriangle* tri = static_cast (obj); - lines[0] = LDSpawn (tri->vertex (0), tri->vertex (1)); - lines[1] = LDSpawn (tri->vertex (1), tri->vertex (2)); - lines[2] = LDSpawn (tri->vertex (2), tri->vertex (0)); - lines[3] = nullptr; + LDTriangle* triangle = static_cast(object); + lines.emplace(triangle->vertex (0), triangle->vertex (1)); + lines.emplace(triangle->vertex (1), triangle->vertex (2)); + lines.emplace(triangle->vertex (2), triangle->vertex (0)); } - for (int i = 0; i < countof (lines); ++i) - { - if (lines[i] == nullptr) - continue; - - long idx = obj->lineNumber() + i + 1; - currentDocument()->insertObject (idx, lines[i]); - ++num; - } + count += countof(lines.objects()); + currentDocument()->merge(lines, object->lineNumber() + 1); } - print (tr ("Added %1 border lines"), num); + print(tr("Added %1 border lines"), count); } void AlgorithmToolset::roundCoordinates() @@ -390,52 +382,45 @@ void AlgorithmToolset::splitLines() { bool ok; - int segments = QInputDialog::getInt (m_window, APPNAME, "Amount of segments:", + int numSegments = QInputDialog::getInt (m_window, APPNAME, "Amount of segments:", m_config->splitLinesSegments(), 0, std::numeric_limits::max(), 1, &ok); if (not ok) return; - m_config->setSplitLinesSegments (segments); + m_config->setSplitLinesSegments (numSegments); for (LDObject* obj : selectedObjects()) { if (not isOneOf (obj->type(), OBJ_Line, OBJ_CondLine)) continue; - QVector newsegs; + Model segments; - for (int i = 0; i < segments; ++i) + for (int i = 0; i < numSegments; ++i) { - LDObject* segment; - Vertex v0, v1; + Vertex v0; + Vertex v1; v0.apply ([&](Axis ax, double& a) { double len = obj->vertex (1)[ax] - obj->vertex (0)[ax]; - a = (obj->vertex (0)[ax] + ((len * i) / segments)); + a = (obj->vertex (0)[ax] + ((len * i) / numSegments)); }); v1.apply ([&](Axis ax, double& a) { double len = obj->vertex (1)[ax] - obj->vertex (0)[ax]; - a = (obj->vertex (0)[ax] + ((len * (i + 1)) / segments)); + a = (obj->vertex (0)[ax] + ((len * (i + 1)) / numSegments)); }); if (obj->type() == OBJ_Line) - segment = LDSpawn (v0, v1); + segments.emplace(v0, v1); else - segment = LDSpawn (v0, v1, obj->vertex (2), obj->vertex (3)); - - newsegs << segment; + segments.emplace(v0, v1, obj->vertex (2), obj->vertex (3)); } - int ln = obj->lineNumber(); - - for (LDObject* seg : newsegs) - currentDocument()->insertObject (ln++, seg); - - currentDocument()->remove(obj); + currentDocument()->replace(obj, segments); } m_window->buildObjectList(); @@ -472,7 +457,7 @@ // Where to insert the subfile reference? // TODO: the selection really should be sorted by position... - int refidx = (*selectedObjects().begin())->lineNumber(); + int referencePosition = (*selectedObjects().begin())->lineNumber(); // Determine title of subfile if (titleobj) @@ -545,53 +530,40 @@ } } - // Get the body of the document in LDraw code - for (LDObject* obj : selectedObjects()) - code << obj->asText(); + // Create the new subfile document + LDDocument* subfile = m_window->newDocument(); + subfile->openForEditing(); + subfile->setFullPath(fullsubname); + subfile->setName(LDDocument::shortenName(fullsubname)); - // Create the new subfile document - LDDocument* doc = m_window->newDocument(); - doc->openForEditing(); - doc->setFullPath (fullsubname); - doc->setName (LDDocument::shortenName (fullsubname)); - - LDObjectList objs; - objs << LDSpawn (subtitle); - objs << LDSpawn ("Name: "); // This gets filled in when the subfile is saved - objs << LDSpawn (format ("Author: %1 [%2]", m_config->defaultName(), m_config->defaultUser())); - objs << LDSpawn ("!LDRAW_ORG Unofficial_Subpart"); + Model header; + header.emplace(subtitle); + header.emplace("Name: "); // This gets filled in when the subfile is saved + header.emplace(format("Author: %1 [%2]", m_config->defaultName(), m_config->defaultUser())); + header.emplace("!LDRAW_ORG Unofficial_Subpart"); if (not license.isEmpty()) - objs << LDSpawn (license); - - objs << LDSpawn(); - objs << LDSpawn (bfctype); - objs << LDSpawn(); + header.emplace(license); - doc->addObjects (objs); + header.emplace(); + header.emplace(bfctype); + header.emplace(); + subfile->merge(header); - // Add the actual subfile code to the new document - for (QString line : code) - { - LDObject* obj = ParseLine (line); - doc->addObject (obj); - } + // Copy the body over to the new document + for (LDObject* object : selectedObjects()) + subfile->addObject(object->createCopy()); // Try save it - if (m_window->save (doc, true)) + if (m_window->save(subfile, true)) { // Save was successful. Delete the original selection now from the // main document. - for (LDObject* object : selectedObjects()) + for (LDObject* object : selectedObjects().toList()) currentDocument()->remove(object); // Add a reference to the new subfile to where the selection was - LDSubfileReference* ref = LDSpawn(); - ref->setColor (MainColor); - ref->setFileInfo (doc); - ref->setPosition (Origin); - ref->setTransformationMatrix (Matrix::identity); - currentDocument()->insertObject (refidx, ref); + currentDocument()->emplaceAt(referencePosition, subfile, Matrix::identity, Origin); // Refresh stuff m_window->updateDocumentList(); @@ -600,6 +572,6 @@ else { // Failed to save. - doc->close(); + subfile->close(); } } \ No newline at end of file