BFC INVERTNEXT objects are no longer individual objects

Fri, 12 Jan 2018 00:55:31 +0200

author
Santeri Piippo
date
Fri, 12 Jan 2018 00:55:31 +0200
changeset 1236
861bf8ebb8ec
parent 1235
b2e44b23b42a
child 1237
40bb00d82e2b

BFC INVERTNEXT objects are no longer individual objects

src/documentloader.cpp file | annotate | diff | comparison | revisions
src/lddocument.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
--- a/src/documentloader.cpp	Fri Jan 12 00:22:35 2018 +0200
+++ b/src/documentloader.cpp	Fri Jan 12 00:55:31 2018 +0200
@@ -105,13 +105,21 @@
 	// Parse up to 200 lines per iteration
 	int max = i + 200;
 
+	bool invertNext = false;
+
 	for (; i < max and i < (int) countof(m_lines); ++i)
 	{
-		QString line = m_lines[i];
+		QString line = m_lines[i].trimmed();
 
 		// Trim the trailing newline
 		while (line.endsWith ("\n") or line.endsWith ("\r"))
-			line.chop (1);
+			line.chop(1);
+
+		if (line == "0 BFC INVERTNEXT")
+		{
+			invertNext = true;
+			continue;
+		}
 
 		LDObject* obj = _model->addFromString(line);
 
@@ -121,6 +129,11 @@
 			emit parseErrorMessage(format(tr("Couldn't parse line #%1: %2"), progress() + 1, static_cast<LDError*> (obj)->reason()));
 			++m_warningCount;
 		}
+
+		if (invertNext and obj->type() == LDObjectType::SubfileReference)
+			obj->setInverted(true);
+
+		invertNext = false;
 	}
 
 	m_progress = i;
--- a/src/lddocument.cpp	Fri Jan 12 00:22:35 2018 +0200
+++ b/src/lddocument.cpp	Fri Jan 12 00:55:31 2018 +0200
@@ -229,17 +229,13 @@
 
 	QByteArray data;
 
-	if (sizeptr)
-		*sizeptr = 0;
-
 	// File is open, now save the model to it. Note that LDraw requires files to have DOS line endings.
 	for (LDObject* obj : objects())
 	{
-		QByteArray subdata ((obj->asText() + "\r\n").toUtf8());
-		data.append (subdata);
+		if (obj->isInverted())
+			data += "0 BFC INVERTNEXT\r\n";
 
-		if (sizeptr)
-			*sizeptr += countof(subdata);
+		data += (obj->asText() + "\r\n").toUtf8();
 	}
 
 	QFile f (path);
@@ -247,6 +243,9 @@
 	if (not f.open (QIODevice::WriteOnly))
 		return false;
 
+	if (sizeptr)
+		*sizeptr = data.size();
+
 	f.write (data);
 	f.close();
 
@@ -444,12 +443,6 @@
 	{
 		m_selection.insert(obj);
 		emit objectModified(obj);
-
-		// If this object is inverted with INVERTNEXT, select the INVERTNEXT as well.
-		LDBfc* invertnext;
-
-		if (obj->previousIsInvertnext(invertnext))
-			addToSelection(invertnext);
 	}
 }
 
@@ -461,12 +454,6 @@
 	{
 		m_selection.remove(obj);
 		emit objectModified(obj);
-
-		// If this object is inverted with INVERTNEXT, deselect the INVERTNEXT as well.
-		LDBfc* invertnext;
-
-		if (obj->previousIsInvertnext(invertnext))
-			removeFromSelection(invertnext);
 	}
 }
 
--- a/src/linetypes/modelobject.cpp	Fri Jan 12 00:22:35 2018 +0200
+++ b/src/linetypes/modelobject.cpp	Fri Jan 12 00:55:31 2018 +0200
@@ -302,23 +302,6 @@
 
 // =============================================================================
 //
-// Is the previous object INVERTNEXT?
-//
-bool LDObject::previousIsInvertnext (LDBfc*& ptr)
-{
-	LDObject* prev = previous();
-
-	if (prev and prev->type() == LDObjectType::Bfc and static_cast<LDBfc*> (prev)->statement() == BfcStatement::InvertNext)
-	{
-		ptr = static_cast<LDBfc*> (prev);
-		return true;
-	}
-
-	return false;
-}
-
-// =============================================================================
-//
 // Moves this object using the given vertex as a movement List
 //
 void LDObject::move (Vertex vect)
@@ -428,22 +411,7 @@
 	}
 
 	// Subfile is not flat. Resort to invertnext.
-	int idx = lineNumber();
-
-	if (idx > 0)
-	{
-		LDBfc* bfc = dynamic_cast<LDBfc*> (previous());
-
-		if (bfc and bfc->statement() == BfcStatement::InvertNext)
-		{
-			// This is prefixed with an invertnext, thus remove it.
-			this->model()->remove(bfc);
-			return;
-		}
-	}
-
-	// Not inverted, thus prefix it with a new invertnext.
-	this->model()->emplaceAt<LDBfc>(idx, BfcStatement::InvertNext);
+	setInverted(not this->isInverted());
 }
 
 // =============================================================================
@@ -737,3 +705,13 @@
 {
 	return statementToString();
 }
+
+bool LDObject::isInverted() const
+{
+	return m_hasInvertNext;
+}
+
+void LDObject::setInverted(bool value)
+{
+	changeProperty(&m_hasInvertNext, value);
+}
--- a/src/linetypes/modelobject.h	Fri Jan 12 00:22:35 2018 +0200
+++ b/src/linetypes/modelobject.h	Fri Jan 12 00:55:31 2018 +0200
@@ -78,12 +78,13 @@
 	virtual int numVertices() const;
 	virtual QString objectListText() const;
 	LDObject* previous() const;
-	bool previousIsInvertnext(class LDBfc*& ptr);
 	QColor randomColor() const;
 	void setColor (LDColor color);
 	void setHidden (bool value);
 	void setVertex (int i, const Vertex& vert);
 	void swap (LDObject* other);
+	bool isInverted() const;
+	void setInverted(bool value);
 	virtual int triangleCount() const;
 	virtual LDObjectType type() const = 0;
 	virtual QString typeName() const = 0;
@@ -104,6 +105,7 @@
 	void changeProperty(T* property, const T& value);
 
 private:
+	bool m_hasInvertNext = false;
 	bool m_isHidden;
 	bool m_isSelected;
 	Model* _model;
--- a/src/mainwindow.cpp	Fri Jan 12 00:22:35 2018 +0200
+++ b/src/mainwindow.cpp	Fri Jan 12 00:55:31 2018 +0200
@@ -356,6 +356,9 @@
 		QListWidgetItem* item = new QListWidgetItem {obj->objectListText()};
 		item->setIcon (getIcon (obj->typeName()));
 
+		if (obj->isInverted())
+			item->setText("↺ " + item->text());
+
 		// Use italic font if hidden
 		if (obj->isHidden())
 		{

mercurial