src/primitives.cpp

changeset 1217
314e12e23c3a
parent 1215
77a0270352a3
child 1222
34def2630300
--- a/src/primitives.cpp	Thu Jan 04 19:40:52 2018 +0200
+++ b/src/primitives.cpp	Thu Jan 04 19:44:26 2018 +0200
@@ -58,9 +58,9 @@
 void LoadPrimitives()
 {
 	// Try to load prims.cfg
-	QFile conf (getPrimitivesCfgPath());
+	QFile conf(getPrimitivesCfgPath());
 
-	if (not conf.open (QIODevice::ReadOnly))
+	if (not conf.open(QIODevice::ReadOnly))
 	{
 		// No prims.cfg, build it
 		PrimitiveScanner::start();
@@ -71,38 +71,38 @@
 		{
 			QString line = conf.readLine();
 
-			if (line.endsWith ("\n"))
-				line.chop (1);
+			if (line.endsWith("\n"))
+				line.chop(1);
 
-			if (line.endsWith ("\r"))
-				line.chop (1);
+			if (line.endsWith("\r"))
+				line.chop(1);
 
-			int space = line.indexOf (" ");
+			int space = line.indexOf(" ");
 
 			if (space == -1)
 				continue;
 
 			Primitive info;
-			info.name = line.left (space);
-			info.title = line.mid (space + 1);
+			info.name = line.left(space);
+			info.title = line.mid(space + 1);
 			g_primitives << info;
 		}
 
 		PrimitiveCategory::populateCategories();
-		print ("%1 primitives loaded.\n", g_primitives.size());
+		print("%1 primitives loaded.\n", g_primitives.size());
 	}
 }
 
 // =============================================================================
 //
-static void GetRecursiveFilenames (QDir dir, QList<QString>& fnames)
+static void GetRecursiveFilenames(QDir dir, QList<QString>& fnames)
 {
-	QFileInfoList flist = dir.entryInfoList (QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
+	QFileInfoList flist = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
 
 	for (const QFileInfo& info : flist)
 	{
 		if (info.isDir())
-			GetRecursiveFilenames (QDir (info.absoluteFilePath()), fnames);
+			GetRecursiveFilenames(QDir(info.absoluteFilePath()), fnames);
 		else
 			fnames << info.absoluteFilePath();
 	}
@@ -110,16 +110,16 @@
 
 // =============================================================================
 //
-PrimitiveScanner::PrimitiveScanner (QObject* parent) :
-	QObject (parent),
-	m_i (0)
+PrimitiveScanner::PrimitiveScanner(QObject* parent) :
+	QObject(parent),
+	m_i(0)
 {
 	g_activeScanner = this;
 	QDir dir = LDPaths::primitivesDir();
 	m_baselen = dir.absolutePath().length();
-	GetRecursiveFilenames (dir, m_files);
-	emit starting (m_files.size());
-	print ("Scanning primitives...");
+	GetRecursiveFilenames(dir, m_files);
+	emit starting(m_files.size());
+	print("Scanning primitives...");
 }
 
 // =============================================================================
@@ -133,30 +133,30 @@
 //
 void PrimitiveScanner::work()
 {
-	int j = qMin (m_i + 100, m_files.size());
+	int j = qMin(m_i + 100, m_files.size());
 
 	for (; m_i < j; ++m_i)
 	{
 		QString fname = m_files[m_i];
-		QFile f (fname);
+		QFile f(fname);
 
-		if (not f.open (QIODevice::ReadOnly))
+		if (not f.open(QIODevice::ReadOnly))
 			continue;
 
 		Primitive info;
-		info.name = fname.mid (m_baselen + 1);  // make full path relative
-		info.name.replace ('/', '\\');  // use DOS backslashes, they're expected
+		info.name = fname.mid(m_baselen + 1);  // make full path relative
+		info.name.replace('/', '\\');  // use DOS backslashes, they're expected
 		info.category = nullptr;
 		QByteArray titledata = f.readLine();
 
 		if (titledata != QByteArray())
-			info.title = QString::fromUtf8 (titledata);
+			info.title = QString::fromUtf8(titledata);
 
 		info.title = info.title.simplified();
 
-		if (Q_LIKELY (info.title[0] == '0'))
+		if (Q_LIKELY(info.title[0] == '0'))
 		{
-			info.title.remove (0, 1);  // remove 0
+			info.title.remove(0, 1);  // remove 0
 			info.title = info.title.simplified();
 		}
 
@@ -167,22 +167,22 @@
 	{
 		// Done with primitives, now save to a config file
 		QString path = getPrimitivesCfgPath();
-		QFile conf (path);
+		QFile conf(path);
 
-		if (not conf.open (QIODevice::WriteOnly | QIODevice::Text))
-			Critical (format ("Couldn't write primitive list %1: %2",
+		if (not conf.open(QIODevice::WriteOnly | QIODevice::Text))
+			Critical(format("Couldn't write primitive list %1: %2",
 				path, conf.errorString()));
 		else
 		{
 			for (Primitive& info : m_prims)
-				fprint (conf, "%1 %2\r\n", info.name, info.title);
+				fprint(conf, "%1 %2\r\n", info.name, info.title);
 
 			conf.close();
 		}
 
 		g_primitives = m_prims;
 		PrimitiveCategory::populateCategories();
-		print ("%1 primitives scanned", g_primitives.size());
+		print("%1 primitives scanned", g_primitives.size());
 		g_activeScanner = nullptr;
 		emit workDone();
 		deleteLater();
@@ -190,8 +190,8 @@
 	else
 	{
 		// Defer to event loop, pick up the work later
-		emit update (m_i);
-		QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection);
+		emit update(m_i);
+		QMetaObject::invokeMethod(this, "work", Qt::QueuedConnection);
 	}
 }
 
@@ -209,9 +209,9 @@
 
 // =============================================================================
 //
-PrimitiveCategory::PrimitiveCategory (QString name, QObject* parent) :
-	QObject (parent),
-	m_name (name) {}
+PrimitiveCategory::PrimitiveCategory(QString name, QObject* parent) :
+	QObject(parent),
+	m_name(name) {}
 
 // =============================================================================
 //
@@ -233,18 +233,18 @@
 		{
 			for (RegexEntry& entry : cat->regexes)
 			{
-				switch (entry.type)
+				switch(entry.type)
 				{
 					case EFilenameRegex:
 					{
 						// f-regex, check against filename
-						matched = entry.regex.exactMatch (prim.name);
+						matched = entry.regex.exactMatch(prim.name);
 					} break;
 
 					case ETitleRegex:
 					{
 						// t-regex, check against title
-						matched = entry.regex.exactMatch (prim.title);
+						matched = entry.regex.exactMatch(prim.title);
 					} break;
 				}
 
@@ -270,7 +270,7 @@
 
 	// Sort the categories. Note that we do this here because we need the existing
 	// order for regex matching.
-	qSort (g_PrimitiveCategories.begin(), g_PrimitiveCategories.end(),
+	qSort(g_PrimitiveCategories.begin(), g_PrimitiveCategories.end(),
 		[](PrimitiveCategory* const& a, PrimitiveCategory* const& b) -> bool
 		{
 			return a->name() < b->name();
@@ -286,11 +286,11 @@
 
 	g_PrimitiveCategories.clear();
 	QString path = ":/data/primitive-categories.cfg";
-	QFile f (path);
+	QFile f(path);
 
-	if (not f.open (QIODevice::ReadOnly))
+	if (not f.open(QIODevice::ReadOnly))
 	{
-		Critical (format (QObject::tr ("Failed to open primitive categories: %1"), f.errorString()));
+		Critical(format(QObject::tr("Failed to open primitive categories: %1"), f.errorString()));
 		return;
 	}
 
@@ -301,22 +301,22 @@
 		QString line = f.readLine();
 		int colon;
 
-		if (line.endsWith ("\n"))
-			line.chop (1);
+		if (line.endsWith("\n"))
+			line.chop(1);
 
 		if (line.length() == 0 or line[0] == '#')
 			continue;
 
-		if ((colon = line.indexOf (":")) == -1)
+		if ((colon = line.indexOf(":")) == -1)
 		{
 			if (cat and cat->isValidToInclude())
 				g_PrimitiveCategories << cat;
 
-			cat = new PrimitiveCategory (line);
+			cat = new PrimitiveCategory(line);
 		}
 		else if (cat)
 		{
-			QString cmd = line.left (colon);
+			QString cmd = line.left(colon);
 			RegexType type = EFilenameRegex;
 
 			if (cmd == "f")
@@ -325,16 +325,16 @@
 				type = ETitleRegex;
 			else
 			{
-				print (tr ("Warning: unknown command \"%1\" on line \"%2\""), cmd, line);
+				print(tr("Warning: unknown command \"%1\" on line \"%2\""), cmd, line);
 				continue;
 			}
 
-			QRegExp regex (line.mid (colon + 1));
+			QRegExp regex(line.mid(colon + 1));
 			RegexEntry entry = { regex, type };
 			cat->regexes << entry;
 		}
 		else
-			print ("Warning: Rules given before the first category name");
+			print("Warning: Rules given before the first category name");
 	}
 
 	if (cat->isValidToInclude())
@@ -343,7 +343,7 @@
 	// Add a category for unmatched primitives.
 	// Note: if this function is called the second time, g_unmatched has been
 	// deleted at the beginning of the function and is dangling at this point.
-	g_unmatched = new PrimitiveCategory (tr ("Other"));
+	g_unmatched = new PrimitiveCategory(tr("Other"));
 	g_PrimitiveCategories << g_unmatched;
 	f.close();
 }
@@ -354,7 +354,7 @@
 {
 	if (regexes.isEmpty())
 	{
-		print (tr ("Warning: category \"%1\" left without patterns"), name());
+		print(tr("Warning: category \"%1\" left without patterns"), name());
 		deleteLater();
 		return false;
 	}
@@ -376,35 +376,35 @@
 
 // =============================================================================
 //
-static double GetRadialPoint (int i, int divs, double (*func) (double))
+static double GetRadialPoint(int i, int divs, double(*func)(double))
 {
-	return (*func) ((i * 2 * Pi) / divs);
+	return (*func)((i * 2 * Pi) / divs);
 }
 
 // =============================================================================
 //
-void MakeCircle (int segs, int divs, double radius, QList<QLineF>& lines)
+void MakeCircle(int segs, int divs, double radius, QList<QLineF>& lines)
 {
 	for (int i = 0; i < segs; ++i)
 	{
-		double x0 = radius * GetRadialPoint (i, divs, cos),
-			x1 = radius * GetRadialPoint (i + 1, divs, cos),
-			z0 = radius * GetRadialPoint (i, divs, sin),
-			z1 = radius * GetRadialPoint (i + 1, divs, sin);
+		double x0 = radius * GetRadialPoint(i, divs, cos),
+			x1 = radius * GetRadialPoint(i + 1, divs, cos),
+			z0 = radius * GetRadialPoint(i, divs, sin),
+			z1 = radius * GetRadialPoint(i + 1, divs, sin);
 
-		lines << QLineF (QPointF (x0, z0), QPointF (x1, z1));
+		lines << QLineF(QPointF(x0, z0), QPointF(x1, z1));
 	}
 }
 
 // =============================================================================
 //
-LDObjectList MakePrimitive (PrimitiveType type, int segs, int divs, int num)
+LDObjectList MakePrimitive(PrimitiveType type, int segs, int divs, int num)
 {
 	LDObjectList objs;
 	QList<int> condLineSegs;
 	QList<QLineF> circle;
 
-	MakeCircle (segs, divs, 1, circle);
+	MakeCircle(segs, divs, 1, circle);
 
 	for (int i = 0; i < segs; ++i)
 	{
@@ -413,17 +413,17 @@
 				   z0 = circle[i].y1(),
 				   z1 = circle[i].y2();
 
-		switch (type)
+		switch(type)
 		{
 			case Circle:
 			{
-				Vertex v0 (x0, 0.0f, z0),
-				  v1 (x1, 0.0f, z1);
+				Vertex v0(x0, 0.0f, z0),
+				  v1(x1, 0.0f, z1);
 
-				LDLine* line (LDSpawn<LDLine>());
-				line->setVertex (0, v0);
-				line->setVertex (1, v1);
-				line->setColor (EdgeColor);
+				LDLine* line(LDSpawn<LDLine>());
+				line->setVertex(0, v0);
+				line->setVertex(1, v1);
+				line->setColor(EdgeColor);
 				objs << line;
 			} break;
 
@@ -446,10 +446,10 @@
 				}
 				else
 				{
-					x2 = x1 * (num + 1);
-					x3 = x0 * (num + 1);
-					z2 = z1 * (num + 1);
-					z3 = z0 * (num + 1);
+					x2 = x1 *(num + 1);
+					x3 = x0 *(num + 1);
+					z2 = z1 *(num + 1);
+					z3 = z0 *(num + 1);
 
 					x0 *= num;
 					x1 *= num;
@@ -465,13 +465,13 @@
 					}
 				}
 
-				Vertex v0 (x0, y0, z0),
-					   v1 (x1, y1, z1),
-					   v2 (x2, y2, z2),
-					   v3 (x3, y3, z3);
+				Vertex v0(x0, y0, z0),
+					   v1(x1, y1, z1),
+					   v2(x2, y2, z2),
+					   v3(x3, y3, z3);
 
-				LDQuad* quad (LDSpawn<LDQuad> (v0, v1, v2, v3));
-				quad->setColor (MainColor);
+				LDQuad* quad(LDSpawn<LDQuad>(v0, v1, v2, v3));
+				quad->setColor(MainColor);
 
 				if (type == Cylinder)
 					quad->invert();
@@ -495,17 +495,17 @@
 					z2 = (z0 >= 0.0f) ? 1.0f : -1.0f;
 				}
 
-				Vertex v0 (x0, 0.0f, z0),
-					   v1 (x1, 0.0f, z1),
-					   v2 (x2, 0.0f, z2);
+				Vertex v0(x0, 0.0f, z0),
+					   v1(x1, 0.0f, z1),
+					   v2(x2, 0.0f, z2);
 
 				// Disc negatives need to go the other way around, otherwise
 				// they'll end up upside-down.
-				LDTriangle* seg (LDSpawn<LDTriangle>());
-				seg->setColor (MainColor);
-				seg->setVertex (type == Disc ? 0 : 2, v0);
-				seg->setVertex (1, v1);
-				seg->setVertex (type == Disc ? 2 : 0, v2);
+				LDTriangle* seg(LDSpawn<LDTriangle>());
+				seg->setColor(MainColor);
+				seg->setVertex(type == Disc ? 0 : 2, v0);
+				seg->setVertex(1, v1);
+				seg->setVertex(type == Disc ? 2 : 0, v2);
 				objs << seg;
 			} break;
 		}
@@ -518,29 +518,29 @@
 
 	for (int i : condLineSegs)
 	{
-		Vertex v0 (GetRadialPoint (i, divs, cos), 0.0f, GetRadialPoint (i, divs, sin)),
+		Vertex v0(GetRadialPoint(i, divs, cos), 0.0f, GetRadialPoint(i, divs, sin)),
 		  v1,
-		  v2 (GetRadialPoint (i + 1, divs, cos), 0.0f, GetRadialPoint (i + 1, divs, sin)),
-		  v3 (GetRadialPoint (i - 1, divs, cos), 0.0f, GetRadialPoint (i - 1, divs, sin));
+		  v2(GetRadialPoint(i + 1, divs, cos), 0.0f, GetRadialPoint(i + 1, divs, sin)),
+		  v3(GetRadialPoint(i - 1, divs, cos), 0.0f, GetRadialPoint(i - 1, divs, sin));
 
 		if (type == Cylinder)
 		{
-			v1 = Vertex (v0[X], 1.0f, v0[Z]);
+			v1 = Vertex(v0[X], 1.0f, v0[Z]);
 		}
 		else if (type == Cone)
 		{
-			v1 = Vertex (v0[X] * (num + 1), 0.0f, v0[Z] * (num + 1));
-			v0.setX (v0.x() * num);
-			v0.setY (1.0);
-			v0.setZ (v0.z() * num);
+			v1 = Vertex(v0[X] *(num + 1), 0.0f, v0[Z] *(num + 1));
+			v0.setX(v0.x() * num);
+			v0.setY(1.0);
+			v0.setZ(v0.z() * num);
 		}
 
 		LDCondLine* line = (LDSpawn<LDCondLine>());
-		line->setColor (EdgeColor);
-		line->setVertex (0, v0);
-		line->setVertex (1, v1);
-		line->setVertex (2, v2);
-		line->setVertex (3, v3);
+		line->setColor(EdgeColor);
+		line->setVertex(0, v0);
+		line->setVertex(1, v1);
+		line->setVertex(2, v2);
+		line->setVertex(3, v3);
 		objs << line;
 	}
 
@@ -549,7 +549,7 @@
 
 // =============================================================================
 //
-static QString PrimitiveTypeName (PrimitiveType type)
+static QString PrimitiveTypeName(PrimitiveType type)
 {
 	// Not translated as primitives are in English.
 	return type == Circle   ? "Circle" :
@@ -561,13 +561,13 @@
 
 // =============================================================================
 //
-QString MakeRadialFileName (PrimitiveType type, int segs, int divs, int num)
+QString MakeRadialFileName(PrimitiveType type, int segs, int divs, int num)
 {
 	int numer = segs,
 			denom = divs;
 
 	// Simplify the fractional part, but the denominator must be at least 4.
-	Simplify (numer, denom);
+	Simplify(numer, denom);
 
 	if (denom < 4)
 	{
@@ -577,15 +577,15 @@
 	}
 
 	// Compose some general information: prefix, fraction, root, ring number
-	QString prefix = (divs == LowResolution) ? "" : format ("%1/", divs);
-	QString frac = format ("%1-%2", numer, denom);
+	QString prefix = (divs == LowResolution) ? "" : format("%1/", divs);
+	QString frac = format("%1-%2", numer, denom);
 	QString root = g_radialNameRoots[type];
-	QString numstr = (type == Ring or type == Cone) ? format ("%1", num) : "";
+	QString numstr = (type == Ring or type == Cone) ? format("%1", num) : "";
 
-	// Truncate the root if necessary (7-16rin4.dat for instance).
+	// Truncate the root if necessary(7-16rin4.dat for instance).
 	// However, always keep the root at least 2 characters.
 	int extra = (frac.length() + numstr.length() + root.length()) - 8;
-	root.chop (qBound (0, extra, 2));
+	root.chop(qBound(0, extra, 2));
 
 	// Stick them all together and return the result.
 	return prefix + frac + root + numstr + ".dat";
@@ -593,15 +593,15 @@
 
 // =============================================================================
 //
-LDDocument* GeneratePrimitive (PrimitiveType type, int segs, int divs, int num)
+LDDocument* GeneratePrimitive(PrimitiveType type, int segs, int divs, int num)
 {
 	// Make the description
-	QString frac = QString::number ((float) segs / divs);
-	QString name = MakeRadialFileName (type, segs, divs, num);
+	QString frac = QString::number((float) segs / divs);
+	QString name = MakeRadialFileName(type, segs, divs, num);
 	QString descr;
 
 	// Ensure that there's decimals, even if they're 0.
-	if (frac.indexOf (".") == -1)
+	if (frac.indexOf(".") == -1)
 		frac += ".0";
 
 	if (type == Ring or type == Cone)
@@ -610,17 +610,17 @@
 			(num < 10) ? "  " :
 			(num < 100) ? " "  : "";
 
-		descr = format ("%1 %2%3 x %4", PrimitiveTypeName (type), spacing, num, frac);
+		descr = format("%1 %2%3 x %4", PrimitiveTypeName(type), spacing, num, frac);
 	}
 	else
-		descr = format ("%1 %2", PrimitiveTypeName (type), frac);
+		descr = format("%1 %2", PrimitiveTypeName(type), frac);
 
 	// Prepend "Hi-Res" if 48/ primitive.
 	if (divs == HighResolution)
-		descr.insert (0, "Hi-Res ");
+		descr.insert(0, "Hi-Res ");
 
 	LDDocument* document = g_win->newDocument();
-	document->setDefaultName (name);
+	document->setDefaultName(name);
 
 	QString author = APPNAME;
 	QString license = "";
@@ -628,50 +628,50 @@
 	if (not config.defaultName().isEmpty())
 	{
 		license = PreferredLicenseText();
-		author = format ("%1 [%2]", config.defaultName(), config.defaultUser());
+		author = format("%1 [%2]", config.defaultName(), config.defaultUser());
 	}
 
 	LDObjectList objs;
 
-	objs << LDSpawn<LDComment> (descr)
-		 << LDSpawn<LDComment> (format ("Name: %1", name))
-		 << LDSpawn<LDComment> (format ("Author: %1", author))
-		 << LDSpawn<LDComment> (format ("!LDRAW_ORG Unofficial_%1Primitive",
+	objs << LDSpawn<LDComment>(descr)
+		 << LDSpawn<LDComment>(format("Name: %1", name))
+		 << LDSpawn<LDComment>(format("Author: %1", author))
+		 << LDSpawn<LDComment>(format("!LDRAW_ORG Unofficial_%1Primitive",
 									  divs == HighResolution ?  "48_" : ""))
-		 << LDSpawn<LDComment> (license)
+		 << LDSpawn<LDComment>(license)
 		 << LDSpawn<LDEmpty>()
-		 << LDSpawn<LDBfc> (BfcStatement::CertifyCCW)
+		 << LDSpawn<LDBfc>(BfcStatement::CertifyCCW)
 		 << LDSpawn<LDEmpty>();
 
 	document->openForEditing();
-	document->history()->setIgnoring (false);
-	document->addObjects (objs);
-	document->addObjects (MakePrimitive (type, segs, divs, num));
+	document->history()->setIgnoring(false);
+	document->addObjects(objs);
+	document->addObjects(MakePrimitive(type, segs, divs, num));
 	document->addHistoryStep();
 	return document;
 }
 
 // =============================================================================
 //
-LDDocument* GetPrimitive (PrimitiveType type, int segs, int divs, int num)
+LDDocument* GetPrimitive(PrimitiveType type, int segs, int divs, int num)
 {
-	QString name = MakeRadialFileName (type, segs, divs, num);
-	LDDocument* f = g_win->documents()->getDocumentByName (name);
+	QString name = MakeRadialFileName(type, segs, divs, num);
+	LDDocument* f = g_win->documents()->getDocumentByName(name);
 
 	if (f)
 		return f;
 
-	return GeneratePrimitive (type, segs, divs, num);
+	return GeneratePrimitive(type, segs, divs, num);
 }
 
 // =============================================================================
 //
-PrimitivePrompt::PrimitivePrompt (QWidget* parent, Qt::WindowFlags f) :
-	QDialog (parent, f)
+PrimitivePrompt::PrimitivePrompt(QWidget* parent, Qt::WindowFlags f) :
+	QDialog(parent, f)
 {
 	ui = new Ui_MakePrimUI;
-	ui->setupUi (this);
-	connect (ui->cb_hires, SIGNAL (toggled (bool)), this, SLOT (hiResToggled (bool)));
+	ui->setupUi(this);
+	connect(ui->cb_hires, SIGNAL(toggled(bool)), this, SLOT(hiResToggled(bool)));
 }
 
 // =============================================================================
@@ -683,12 +683,12 @@
 
 // =============================================================================
 //
-void PrimitivePrompt::hiResToggled (bool on)
+void PrimitivePrompt::hiResToggled(bool on)
 {
-	ui->sb_segs->setMaximum (on ? HighResolution : LowResolution);
+	ui->sb_segs->setMaximum(on ? HighResolution : LowResolution);
 
 	// If the current value is 16 and we switch to hi-res, default the
 	// spinbox to 48.
 	if (on and ui->sb_segs->value() == LowResolution)
-		ui->sb_segs->setValue (HighResolution);
+		ui->sb_segs->setValue(HighResolution);
 }

mercurial