src/toolsets/algorithmtoolset.cpp

changeset 1217
314e12e23c3a
parent 1215
77a0270352a3
child 1218
e0b59d183f96
--- a/src/toolsets/algorithmtoolset.cpp	Thu Jan 04 19:40:52 2018 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Thu Jan 04 19:44:26 2018 +0200
@@ -39,12 +39,12 @@
 #include "ui_addhistoryline.h"
 #include "algorithmtoolset.h"
 
-ConfigOption (int RoundPositionPrecision = 3)
-ConfigOption (int RoundMatrixPrecision = 4)
-ConfigOption (int SplitLinesSegments = 5)
+ConfigOption(int RoundPositionPrecision = 3)
+ConfigOption(int RoundMatrixPrecision = 4)
+ConfigOption(int SplitLinesSegments = 5)
 
-AlgorithmToolset::AlgorithmToolset (MainWindow* parent) :
-	Toolset (parent)
+AlgorithmToolset::AlgorithmToolset(MainWindow* parent) :
+	Toolset(parent)
 {
 }
 
@@ -52,7 +52,7 @@
 {
 	int num = 0;
 
-	for (LDObjectIterator<LDQuad> it (selectedObjects()); it.isValid(); ++it)
+	for (LDObjectIterator<LDQuad> it(selectedObjects()); it.isValid(); ++it)
 	{
 		// Find the index of this quad
 		int index = it->lineNumber();
@@ -64,12 +64,12 @@
 
 		// Replace the quad with the first triangle and add the second triangle
 		// after the first one.
-		currentDocument()->setObject (index, triangles[0]);
-		currentDocument()->insertObj (index + 1, triangles[1]);
+		currentDocument()->setObject(index, triangles[0]);
+		currentDocument()->insertObj(index + 1, triangles[1]);
 		num++;
 	}
 
-	print ("%1 quadrilaterals split", num);
+	print("%1 quadrilaterals split", num);
 }
 
 void AlgorithmToolset::editRaw()
@@ -81,11 +81,11 @@
 	QDialog* dlg = new QDialog;
 	Ui::EditRawUI ui;
 
-	ui.setupUi (dlg);
-	ui.code->setText (obj->asText());
+	ui.setupUi(dlg);
+	ui.code->setText(obj->asText());
 
 	if (obj->type() == OBJ_Error)
-		ui.errorDescription->setText (static_cast<LDError*> (obj)->reason());
+		ui.errorDescription->setText(static_cast<LDError*>(obj)->reason());
 	else
 	{
 		ui.errorDescription->hide();
@@ -96,8 +96,8 @@
 		return;
 
 	// Reinterpret it from the text of the input field
-	LDObject* newobj = ParseLine (ui.code->text());
-	obj->replace (newobj);
+	LDObject* newobj = ParseLine(ui.code->text());
+	obj->replace(newobj);
 }
 
 void AlgorithmToolset::makeBorders()
@@ -116,87 +116,87 @@
 
 		if (type == OBJ_Quad)
 		{
-			LDQuad* quad = static_cast<LDQuad*> (obj);
-			lines[0] = LDSpawn<LDLine> (quad->vertex (0), quad->vertex (1));
-			lines[1] = LDSpawn<LDLine> (quad->vertex (1), quad->vertex (2));
-			lines[2] = LDSpawn<LDLine> (quad->vertex (2), quad->vertex (3));
-			lines[3] = LDSpawn<LDLine> (quad->vertex (3), quad->vertex (0));
+			LDQuad* quad = static_cast<LDQuad*>(obj);
+			lines[0] = LDSpawn<LDLine>(quad->vertex(0), quad->vertex(1));
+			lines[1] = LDSpawn<LDLine>(quad->vertex(1), quad->vertex(2));
+			lines[2] = LDSpawn<LDLine>(quad->vertex(2), quad->vertex(3));
+			lines[3] = LDSpawn<LDLine>(quad->vertex(3), quad->vertex(0));
 		}
 		else
 		{
-			LDTriangle* tri = static_cast<LDTriangle*> (obj);
-			lines[0] = LDSpawn<LDLine> (tri->vertex (0), tri->vertex (1));
-			lines[1] = LDSpawn<LDLine> (tri->vertex (1), tri->vertex (2));
-			lines[2] = LDSpawn<LDLine> (tri->vertex (2), tri->vertex (0));
+			LDTriangle* tri = static_cast<LDTriangle*>(obj);
+			lines[0] = LDSpawn<LDLine>(tri->vertex(0), tri->vertex(1));
+			lines[1] = LDSpawn<LDLine>(tri->vertex(1), tri->vertex(2));
+			lines[2] = LDSpawn<LDLine>(tri->vertex(2), tri->vertex(0));
 			lines[3] = nullptr;
 		}
 
-		for (int i = 0; i < countof (lines); ++i)
+		for (int i = 0; i < countof(lines); ++i)
 		{
 			if (lines[i] == nullptr)
 				continue;
 
 			long idx = obj->lineNumber() + i + 1;
-			currentDocument()->insertObj (idx, lines[i]);
+			currentDocument()->insertObj(idx, lines[i]);
 			++num;
 		}
 	}
 
-	print (tr ("Added %1 border lines"), num);
+	print(tr("Added %1 border lines"), num);
 }
 
 void AlgorithmToolset::roundCoordinates()
 {
-	setlocale (LC_ALL, "C");
+	setlocale(LC_ALL, "C");
 	int num = 0;
 
 	for (LDObject* obj : selectedObjects())
 	{
-		LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj);
+		LDMatrixObject* mo = dynamic_cast<LDMatrixObject*>(obj);
 
 		if (mo)
 		{
 			Vertex v = mo->position();
 			Matrix t = mo->transform();
 
-			v.apply ([&](Axis, double& a)
+			v.apply([&](Axis, double& a)
 			{
-				RoundToDecimals (a, config.roundPositionPrecision());
+				RoundToDecimals(a, config.roundPositionPrecision());
 			});
 
-			ApplyToMatrix (t, [&](int, double& a)
+			ApplyToMatrix(t, [&](int, double& a)
 			{
-				RoundToDecimals (a, config.roundMatrixPrecision());
+				RoundToDecimals(a, config.roundMatrixPrecision());
 			});
 
-			mo->setPosition (v);
-			mo->setTransform (t);
+			mo->setPosition(v);
+			mo->setTransform(t);
 			num += 12;
 		}
 		else
 		{
 			for (int i = 0; i < obj->numVertices(); ++i)
 			{
-				Vertex v = obj->vertex (i);
-				v.apply ([&](Axis, double& a)
+				Vertex v = obj->vertex(i);
+				v.apply([&](Axis, double& a)
 				{
-					RoundToDecimals (a, config.roundPositionPrecision());
+					RoundToDecimals(a, config.roundPositionPrecision());
 				});
-				obj->setVertex (i, v);
+				obj->setVertex(i, v);
 				num += 3;
 			}
 		}
 	}
 
-	print (tr ("Rounded %1 values"), num);
+	print(tr("Rounded %1 values"), num);
 	m_window->refreshObjectList();
 }
 
 void AlgorithmToolset::replaceCoordinates()
 {
-	QDialog* dlg = new QDialog (m_window);
+	QDialog* dlg = new QDialog(m_window);
 	Ui::ReplaceCoordsUI ui;
-	ui.setupUi (dlg);
+	ui.setupUi(dlg);
 
 	if (not dlg->exec())
 		return;
@@ -217,11 +217,11 @@
 	{
 		for (int i = 0; i < obj->numVertices(); ++i)
 		{
-			Vertex v = obj->vertex (i);
+			Vertex v = obj->vertex(i);
 
-			v.apply ([&](Axis ax, double& coord)
+			v.apply([&](Axis ax, double& coord)
 			{
-				if (not sel.contains (ax) or
+				if (not sel.contains(ax) or
 					(not any and coord != search))
 				{
 					return;
@@ -234,18 +234,18 @@
 				num++;
 			});
 
-			obj->setVertex (i, v);
+			obj->setVertex(i, v);
 		}
 	}
 
-	print (tr ("Altered %1 values"), num);
+	print(tr("Altered %1 values"), num);
 }
 
 void AlgorithmToolset::flip()
 {
 	QDialog* dlg = new QDialog;
 	Ui::FlipUI ui;
-	ui.setupUi (dlg);
+	ui.setupUi(dlg);
 
 	if (not dlg->exec())
 		return;
@@ -258,7 +258,7 @@
 
 	for (LDObject* obj : selectedObjects())
 	{
-        LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj);
+        LDMatrixObject* mo = dynamic_cast<LDMatrixObject*>(obj);
 
 		if (mo)
 		{
@@ -283,24 +283,24 @@
             }
             mo->setTransform(transformationMatrix);
             Vertex position = mo->position();
-			position.apply ([&](Axis ax, double& a)
+			position.apply([&](Axis ax, double& a)
 			{
-				if (sel.contains (ax))
+				if (sel.contains(ax))
 					a = -a;
 			});
             mo->setPosition(position);
         }
 		for (int i = 0; i < obj->numVertices(); ++i)
 		{
-			Vertex v = obj->vertex (i);
+			Vertex v = obj->vertex(i);
 
-			v.apply ([&](Axis ax, double& a)
+			v.apply([&](Axis ax, double& a)
 			{
-				if (sel.contains (ax))
+				if (sel.contains(ax))
 					a = -a;
 			});
 
-			obj->setVertex (i, v);
+			obj->setVertex(i, v);
 		}
 		if (not mo and sel.size() % 2 == 1)
             obj->invert();
@@ -311,16 +311,16 @@
 {
 	int num = 0;
 
-	for (LDObjectIterator<LDCondLine> it (selectedObjects()); it.isValid(); ++it)
+	for (LDObjectIterator<LDCondLine> it(selectedObjects()); it.isValid(); ++it)
 	{
 		it->toEdgeLine();
 		++num;
 	}
 
-	print (tr ("Converted %1 conditional lines"), num);
+	print(tr("Converted %1 conditional lines"), num);
 }
 
-bool AlgorithmToolset::isColorUsed (LDColor color)
+bool AlgorithmToolset::isColorUsed(LDColor color)
 {
 	for (LDObject* obj : currentDocument()->objects())
 	{
@@ -337,13 +337,13 @@
 
 	for (color = 0; color.isLDConfigColor(); ++color)
 	{
-		if (color.isValid() and not isColorUsed (color))
+		if (color.isValid() and not isColorUsed(color))
 			break;
 	}
 
 	if (not color.isLDConfigColor())
 	{
-		print (tr ("Cannot auto-color: all colors are in use!"));
+		print(tr("Cannot auto-color: all colors are in use!"));
 		return;
 	}
 
@@ -352,10 +352,10 @@
 		if (not obj->isColored())
 			continue;
 
-		obj->setColor (color);
+		obj->setColor(color);
 	}
 
-	print (tr ("Auto-colored: new color is [%1] %2"), color.index(), color.name());
+	print(tr("Auto-colored: new color is [%1] %2"), color.index(), color.name());
 }
 
 void AlgorithmToolset::addHistoryLine()
@@ -366,28 +366,28 @@
 
 	QDialog* dlg = new QDialog;
 	Ui_AddHistoryLine* ui = new Ui_AddHistoryLine;
-	ui->setupUi (dlg);
-	ui->m_username->setText (config.defaultUser());
-	ui->m_date->setDate (QDate::currentDate());
+	ui->setupUi(dlg);
+	ui->m_username->setText(config.defaultUser());
+	ui->m_date->setDate(QDate::currentDate());
 	ui->m_comment->setFocus();
 
 	if (not dlg->exec())
 		return;
 
 	// Create the comment object based on input
-	LDComment* comment = new LDComment (format ("!HISTORY %1 [%2] %3",
-		ui->m_date->date().toString ("yyyy-MM-dd"),
+	LDComment* comment = new LDComment(format("!HISTORY %1 [%2] %3",
+		ui->m_date->date().toString("yyyy-MM-dd"),
 		ui->m_username->text(),
 		ui->m_comment->text()));
 
 	// Find a spot to place the new comment
-	for (obj = currentDocument()->getObject (0);
+	for (obj = currentDocument()->getObject(0);
 		obj and obj->next() and not obj->next()->isScemantic();
 		obj = obj->next())
 	{
-		LDComment* comment = dynamic_cast<LDComment*> (obj);
+		LDComment* comment = dynamic_cast<LDComment*>(obj);
 
-		if (comment and comment->text().startsWith ("!HISTORY "))
+		if (comment and comment->text().startsWith("!HISTORY "))
 			ishistory = true;
 
 		if (prevIsHistory and not ishistory)
@@ -397,12 +397,12 @@
 	}
 
 	int idx = obj ? obj->lineNumber() : 0;
-	currentDocument()->insertObj (idx++, comment);
+	currentDocument()->insertObj(idx++, comment);
 
 	// If we're adding a history line right before a scemantic object, pad it
 	// an empty line
 	if (obj and obj->next() and obj->next()->isScemantic())
-		currentDocument()->insertObj (idx, new LDEmpty);
+		currentDocument()->insertObj(idx, new LDEmpty);
 
 	m_window->buildObjectList();
 	delete ui;
@@ -411,17 +411,17 @@
 void AlgorithmToolset::splitLines()
 {
 	bool ok;
-	int segments = QInputDialog::getInt (m_window, APPNAME, "Amount of segments:",
+	int segments = QInputDialog::getInt(m_window, APPNAME, "Amount of segments:",
 		config.splitLinesSegments(), 0, std::numeric_limits<int>::max(), 1, &ok);
 
 	if (not ok)
 		return;
 
-	config.setSplitLinesSegments (segments);
+	config.setSplitLinesSegments(segments);
 
 	for (LDObject* obj : selectedObjects())
 	{
-		if (not isOneOf (obj->type(), OBJ_Line, OBJ_CondLine))
+		if (not isOneOf(obj->type(), OBJ_Line, OBJ_CondLine))
 			continue;
 
 		QVector<LDObject*> newsegs;
@@ -431,22 +431,22 @@
 			LDObject* segment;
 			Vertex v0, v1;
 
-			v0.apply ([&](Axis ax, double& a)
+			v0.apply([&](Axis ax, double& a)
 			{
-				double len = obj->vertex (1)[ax] - obj->vertex (0)[ax];
-				a = (obj->vertex (0)[ax] + ((len * i) / segments));
+				double len = obj->vertex(1)[ax] - obj->vertex(0)[ax];
+				a = (obj->vertex(0)[ax] +((len * i) / segments));
 			});
 
-			v1.apply ([&](Axis ax, double& a)
+			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));
+				double len = obj->vertex(1)[ax] - obj->vertex(0)[ax];
+				a = (obj->vertex(0)[ax] +((len *(i + 1)) / segments));
 			});
 
 			if (obj->type() == OBJ_Line)
-				segment = LDSpawn<LDLine> (v0, v1);
+				segment = LDSpawn<LDLine>(v0, v1);
 			else
-				segment = LDSpawn<LDCondLine> (v0, v1, obj->vertex (2), obj->vertex (3));
+				segment = LDSpawn<LDCondLine>(v0, v1, obj->vertex(2), obj->vertex(3));
 
 			newsegs << segment;
 		}
@@ -454,7 +454,7 @@
 		int ln = obj->lineNumber();
 
 		for (LDObject* seg : newsegs)
-			currentDocument()->insertObj (ln++, seg);
+			currentDocument()->insertObj(ln++, seg);
 
 		obj->destroy();
 	}
@@ -468,31 +468,31 @@
 	if (selectedObjects().size() == 0)
 		return;
 
-	QString			parentpath (currentDocument()->fullPath());
+	QString			parentpath(currentDocument()->fullPath());
 
 	// BFC type of the new subfile - it shall inherit the BFC type of the parent document
-	BfcStatement	bfctype (BfcStatement::NoCertify);
+	BfcStatement	bfctype(BfcStatement::NoCertify);
 
 	// Dirname of the new subfile
-	QString			subdirname (Dirname (parentpath));
+	QString			subdirname(Dirname(parentpath));
 
 	// Title of the new subfile
 	QString			subtitle;
 
 	// Comment containing the title of the parent document
-	LDComment*	titleobj = dynamic_cast<LDComment*> (currentDocument()->getObject (0));
+	LDComment*	titleobj = dynamic_cast<LDComment*>(currentDocument()->getObject(0));
 
 	// License text for the subfile
-	QString			license (PreferredLicenseText());
+	QString			license(PreferredLicenseText());
 
-	// LDraw code body of the new subfile (i.e. code of the selection)
+	// LDraw code body of the new subfile(i.e. code of the selection)
 	QStringList		code;
 
 	// Full path of the subfile to be
 	QString			fullsubname;
 
 	// Where to insert the subfile reference?
-	int				refidx (selectedObjects()[0]->lineNumber());
+	int				refidx(selectedObjects()[0]->lineNumber());
 
 	// Determine title of subfile
 	if (titleobj)
@@ -501,24 +501,24 @@
 		subtitle = "~subfile";
 
 	// Remove duplicate tildes
-	while (subtitle.startsWith ("~~"))
-		subtitle.remove (0, 1);
+	while (subtitle.startsWith("~~"))
+		subtitle.remove(0, 1);
 
 	// If this the parent document isn't already in s/, we need to stuff it into
 	// a subdirectory named s/. Ensure it exists!
-	QString topdirname = Basename (Dirname (currentDocument()->fullPath()));
+	QString topdirname = Basename(Dirname(currentDocument()->fullPath()));
 
 	if (topdirname != "s")
 	{
 		QString desiredPath = subdirname + "/s";
-		QString title = tr ("Create subfile directory?");
-		QString text = format (tr ("The directory <b>%1</b> is suggested for "
+		QString title = tr("Create subfile directory?");
+		QString text = format(tr("The directory <b>%1</b> is suggested for "
 			"subfiles. This directory does not exist, create it?"), desiredPath);
 
-		if (QDir (desiredPath).exists() or Confirm (title, text))
+		if (QDir(desiredPath).exists() or Confirm(title, text))
 		{
 			subdirname = desiredPath;
-			QDir().mkpath (subdirname);
+			QDir().mkpath(subdirname);
 		}
 		else
 			return;
@@ -528,14 +528,14 @@
 	if (not parentpath.isEmpty())
 	{
 		// Chop existing '.dat' suffix
-		if (parentpath.endsWith (".dat"))
-			parentpath.chop (4);
+		if (parentpath.endsWith(".dat"))
+			parentpath.chop(4);
 
 		// Remove the s?? suffix if it's there, otherwise we'll get filenames
 		// like s01s01.dat when subfiling subfiles.
-		QRegExp subfilesuffix ("s[0-9][0-9]$");
-		if (subfilesuffix.indexIn (parentpath) != -1)
-			parentpath.chop (subfilesuffix.matchedLength());
+		QRegExp subfilesuffix("s[0-9][0-9]$");
+		if (subfilesuffix.indexIn(parentpath) != -1)
+			parentpath.chop(subfilesuffix.matchedLength());
 
 		int subidx = 1;
 		QString digits;
@@ -544,21 +544,21 @@
 		// until we find a name which isn't already taken.
 		do
 		{
-			digits.setNum (subidx++);
+			digits.setNum(subidx++);
 
 			// pad it with a zero
 			if (digits.length() == 1)
-				digits.prepend ("0");
+				digits.prepend("0");
 
-			fullsubname = subdirname + "/" + Basename (parentpath) + "s" + digits + ".dat";
-		} while (m_documents->findDocumentByName ("s\\" + Basename (fullsubname)) or QFile (fullsubname).exists());
+			fullsubname = subdirname + "/" + Basename(parentpath) + "s" + digits + ".dat";
+		} while (m_documents->findDocumentByName("s\\" + Basename(fullsubname)) or QFile(fullsubname).exists());
 	}
 
 	// Determine the BFC winding type used in the main document - it is to
 	// be carried over to the subfile.
-	for (LDObjectIterator<LDBfc> it (currentDocument()); it.isValid(); ++it)
+	for (LDObjectIterator<LDBfc> it(currentDocument()); it.isValid(); ++it)
 	{
-		if (isOneOf (it->statement(), BfcStatement::CertifyCCW, BfcStatement::CertifyCW, BfcStatement::NoCertify))
+		if (isOneOf(it->statement(), BfcStatement::CertifyCCW, BfcStatement::CertifyCW, BfcStatement::NoCertify))
 		{
 			bfctype = it->statement();
 			break;
@@ -572,33 +572,33 @@
 	// Create the new subfile document
 	LDDocument* doc = m_window->newDocument();
 	doc->openForEditing();
-	doc->setFullPath (fullsubname);
-	doc->setName (LDDocument::shortenName (fullsubname));
+	doc->setFullPath(fullsubname);
+	doc->setName(LDDocument::shortenName(fullsubname));
 
 	LDObjectList objs;
-	objs << LDSpawn<LDComment> (subtitle);
-	objs << LDSpawn<LDComment> ("Name: "); // This gets filled in when the subfile is saved
-	objs << LDSpawn<LDComment> (format ("Author: %1 [%2]", config.defaultName(), config.defaultUser()));
-	objs << LDSpawn<LDComment> ("!LDRAW_ORG Unofficial_Subpart");
+	objs << LDSpawn<LDComment>(subtitle);
+	objs << LDSpawn<LDComment>("Name: "); // This gets filled in when the subfile is saved
+	objs << LDSpawn<LDComment>(format("Author: %1 [%2]", config.defaultName(), config.defaultUser()));
+	objs << LDSpawn<LDComment>("!LDRAW_ORG Unofficial_Subpart");
 
 	if (not license.isEmpty())
-		objs << LDSpawn<LDComment> (license);
+		objs << LDSpawn<LDComment>(license);
 
 	objs << LDSpawn<LDEmpty>();
-	objs << LDSpawn<LDBfc> (bfctype);
+	objs << LDSpawn<LDBfc>(bfctype);
 	objs << LDSpawn<LDEmpty>();
 
-	doc->addObjects (objs);
+	doc->addObjects(objs);
 
 	// Add the actual subfile code to the new document
 	for (QString line : code)
 	{
-		LDObject* obj = ParseLine (line);
-		doc->addObject (obj);
+		LDObject* obj = ParseLine(line);
+		doc->addObject(obj);
 	}
 
 	// Try save it
-	if (m_window->save (doc, true))
+	if (m_window->save(doc, true))
 	{
 		// Save was successful. Delete the original selection now from the
 		// main document.
@@ -607,11 +607,11 @@
 
 		// Add a reference to the new subfile to where the selection was
 		LDSubfileReference* ref = LDSpawn<LDSubfileReference>();
-		ref->setColor (MainColor);
-		ref->setFileInfo (doc);
-		ref->setPosition (Origin);
-		ref->setTransform (IdentityMatrix);
-		currentDocument()->insertObj (refidx, ref);
+		ref->setColor(MainColor);
+		ref->setFileInfo(doc);
+		ref->setPosition(Origin);
+		ref->setTransform(IdentityMatrix);
+		currentDocument()->insertObj(refidx, ref);
 
 		// Refresh stuff
 		m_window->updateDocumentList();

mercurial