- more refactor, updated .hgignore

Mon, 04 Aug 2014 13:43:54 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 04 Aug 2014 13:43:54 +0300
changeset 860
a496e72af069
parent 859
ebc7a186699c
child 861
83426c5fa732

- more refactor, updated .hgignore

.hgignore file | annotate | diff | comparison | revisions
src/actions.cc file | annotate | diff | comparison | revisions
src/actionsEdit.cc file | annotate | diff | comparison | revisions
src/configDialog.cc file | annotate | diff | comparison | revisions
src/ldObject.h file | annotate | diff | comparison | revisions
src/miscallenous.cc file | annotate | diff | comparison | revisions
--- a/.hgignore	Mon Aug 04 03:35:25 2014 +0300
+++ b/.hgignore	Mon Aug 04 13:43:54 2014 +0300
@@ -14,3 +14,5 @@
 .kdev_include_paths
 *.cfg
 .kdev4
+.*~
+*.orig
--- a/src/actions.cc	Mon Aug 04 03:35:25 2014 +0300
+++ b/src/actions.cc	Mon Aug 04 13:43:54 2014 +0300
@@ -807,19 +807,15 @@
 
 	// Determine the BFC winding type used in the main document - it is to
 	// be carried over to the subfile.
-	for (LDObjectPtr obj : getCurrentDocument()->objects())
+	LDIterate<LDBFC> (getCurrentDocument()->objects(), [&] (LDBFCPtr const& bfc)
 	{
-		if (obj->type() != OBJ_BFC)
-			continue;
-
-		BFCStatement a = obj.staticCast<LDBFC>()->statement();
-
-		if (eq (a, BFCStatement::CertifyCCW, BFCStatement::CertifyCW, BFCStatement::NoCertify))
+		if (eq (bfc->statement(), BFCStatement::CertifyCCW, BFCStatement::CertifyCW, 
+			BFCStatement::NoCertify))
 		{
-			bfctype = a;
-			break;
+			bfctype = bfc->statement();
+			Break();
 		}
-	}
+	});
 
 	// Get the body of the document in LDraw code
 	for (LDObjectPtr obj : selection())
--- a/src/actionsEdit.cc	Mon Aug 04 03:35:25 2014 +0300
+++ b/src/actionsEdit.cc	Mon Aug 04 13:43:54 2014 +0300
@@ -121,16 +121,14 @@
 {
 	LDObjectList sel = selection();
 
-	for (LDObjectPtr obj : sel)
+	LDIterate<LDSubfile> (selection(), [&](LDSubfilePtr const& ref)
 	{
 		// Get the index of the subfile so we know where to insert the
 		// inlined contents.
-		long idx = obj->lineNumber();
+		long idx = ref->lineNumber();
 
-		if (idx == -1 or obj->type() != OBJ_Subfile)
-			continue;
-
-		LDObjectList objs = obj.staticCast<LDSubfile>()->inlineContents (deep, false);
+		assert (idx != -1);
+		LDObjectList objs = ref->inlineContents (deep, false);
 
 		// Merge in the inlined objects
 		for (LDObjectPtr inlineobj : objs)
@@ -143,8 +141,8 @@
 		}
 
 		// Delete the subfile now as it's been inlined.
-		obj->destroy();
-	}
+		ref->destroy();
+	});
 
 	g_win->refresh();
 }
@@ -165,25 +163,22 @@
 {
 	int num = 0;
 
-	for (LDObjectPtr obj : selection())
+	LDIterate<LDQuad> (selection(), [&](LDQuadPtr const& quad)
 	{
-		if (obj->type() != OBJ_Quad)
-			continue;
-
 		// Find the index of this quad
-		long index = obj->lineNumber();
+		long index = quad->lineNumber();
 
 		if (index == -1)
 			return;
 
-		QList<LDTrianglePtr> triangles = obj.staticCast<LDQuad>()->splitToTriangles();
+		QList<LDTrianglePtr> triangles = quad->splitToTriangles();
 
 		// Replace the quad with the first triangle and add the second triangle
 		// after the first one.
 		getCurrentDocument()->setObject (index, triangles[0]);
 		getCurrentDocument()->insertObj (index + 1, triangles[1]);
 		num++;
-	}
+	});
 
 	print ("%1 quadrilaterals split", num);
 	refresh();
@@ -666,17 +661,13 @@
 //
 void MainWindow::slot_actionDemote()
 {
-	LDObjectList sel = selection();
 	int num = 0;
 
-	for (LDObjectPtr obj : sel)
+	LDIterate<LDCondLine> (selection(), [&](LDCondLinePtr const& cnd)
 	{
-		if (obj->type() != OBJ_CondLine)
-			continue;
-
-		obj.staticCast<LDCondLine>()->toEdgeLine();
+		cnd->toEdgeLine();
 		++num;
-	}
+	});
 
 	print (tr ("Demoted %1 conditional lines"), num);
 	refresh();
@@ -702,8 +693,11 @@
 	int colnum = 0;
 	LDColor color;
 
-	for (colnum = 0; colnum < numLDConfigColors() and ((color = LDColor::fromIndex (colnum)) == null or isColorUsed (color)); colnum++)
-		;
+	for (colnum = 0;
+		 colnum < numLDConfigColors() and
+			((color = LDColor::fromIndex (colnum)) == null or
+			isColorUsed (color));
+		colnum++) {}
 
 	if (colnum >= numLDConfigColors())
 	{
@@ -794,7 +788,7 @@
 
 	for (LDObjectPtr obj : selection())
 	{
-		if (obj->type() != OBJ_Line and obj->type() != OBJ_CondLine)
+		if (not eq (obj->type(), OBJ_Line, OBJ_CondLine))
 			continue;
 
 		QVector<LDObjectPtr> newsegs;
@@ -803,8 +797,18 @@
 		{
 			LDObjectPtr segment;
 			Vertex v0, v1;
-			v0.apply ([&](Axis ax, double& a) { a = (obj->vertex (0)[ax] + (((obj->vertex (1)[ax] - obj->vertex (0)[ax]) * i) / segments)); });
-			v1.apply ([&](Axis ax, double& a) { a = (obj->vertex (0)[ax] + (((obj->vertex (1)[ax] - obj->vertex (0)[ax]) * (i + 1)) / segments)); });
+
+			v0.apply ([&](Axis ax, double& a)
+			{
+				double len = obj->vertex (1)[ax] - obj->vertex (0)[ax];
+				a = (obj->vertex (0)[ax] + ((len * i) / segments));
+			});
+
+			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));
+			});
 
 			if (obj->type() == OBJ_Line)
 				segment = spawn<LDLine> (v0, v1);
--- a/src/configDialog.cc	Mon Aug 04 03:35:25 2014 +0300
+++ b/src/configDialog.cc	Mon Aug 04 13:43:54 2014 +0300
@@ -245,12 +245,12 @@
 void ConfigDialog::_applyToWidgetOptions (std::function<void (QWidget*, AbstractConfigEntry*)> func)
 {
 	// Apply configuration
-	for (QWidget* wdg : findChildren<QWidget*>())
+	for (QWidget* widget : findChildren<QWidget*>())
 	{
-		if (not wdg->objectName().startsWith ("config"))
+		if (not widget->objectName().startsWith ("config"))
 			continue;
 
-		QString confname (wdg->objectName().mid (strlen ("config")));
+		QString confname (widget->objectName().mid (strlen ("config")));
 		AbstractConfigEntry* conf (Config::FindByName (confname));
 
 		if (conf == null)
@@ -259,7 +259,7 @@
 			continue;
 		}
 
-		func (wdg, conf);
+		func (widget, conf);
 	}
 }
 
@@ -268,7 +268,7 @@
 //
 void ConfigDialog::applySettings()
 {
-	_applyToWidgetOptions ([&](QWidget* wdg, AbstractConfigEntry* conf)
+	_applyToWidgetOptions ([&](QWidget* widget, AbstractConfigEntry* conf)
 	{
 		QVariant value (conf->toVariant());
 		QLineEdit* le;
@@ -278,23 +278,22 @@
 		QCheckBox* checkbox;
 		QPushButton* button;
 
-		if ((le = qobject_cast<QLineEdit*> (wdg)) != null)
+		if ((le = qobject_cast<QLineEdit*> (widget)) != null)
 			value = le->text();
-		elif ((spinbox = qobject_cast<QSpinBox*> (wdg)) != null)
+		elif ((spinbox = qobject_cast<QSpinBox*> (widget)) != null)
 			value = spinbox->value();
-		elif ((doublespinbox = qobject_cast<QDoubleSpinBox*> (wdg)) != null)
+		elif ((doublespinbox = qobject_cast<QDoubleSpinBox*> (widget)) != null)
 			value = doublespinbox->value();
-		elif ((slider = qobject_cast<QSlider*> (wdg)) != null)
+		elif ((slider = qobject_cast<QSlider*> (widget)) != null)
 			value = slider->value();
-		elif ((checkbox = qobject_cast<QCheckBox*> (wdg)) != null)
+		elif ((checkbox = qobject_cast<QCheckBox*> (widget)) != null)
 			value = checkbox->isChecked();
-		elif ((button = qobject_cast<QPushButton*> (wdg)) != null)
+		elif ((button = qobject_cast<QPushButton*> (widget)) != null)
 			value = _buttonColors[button];
 		else
-			print ("Unknown widget of type %1\n", wdg->metaObject()->className());
+			print ("Unknown widget of type %1\n", widget->metaObject()->className());
 
 		conf->loadFromVariant (value);
-		print ("Value of %1: %2\n", conf->name(), conf->toVariant().toString());
 	});
 
 	// Rebuild the quick color toolbar
@@ -423,17 +422,15 @@
 	if (not ColorSelector::selectColor (val, defval, this))
 		return;
 
-	if (entry)
+	if (entry != null)
 	{
 		entry->setColor (val);
 	}
 	else
 	{
 		LDQuickColor entry (val, null);
-
 		item = getSelectedQuickColor();
 		int idx = (item) ? getItemRow (item, quickColorItems) + 1 : quickColorItems.size();
-
 		quickColors.insert (idx, entry);
 		entry = quickColors[idx];
 	}
@@ -471,10 +468,7 @@
 	if (dest < 0 or dest >= quickColorItems.size())
 		return; // destination out of bounds
 
-	LDQuickColor tmp = quickColors[dest];
-	quickColors[dest] = quickColors[idx];
-	quickColors[idx] = tmp;
-
+	qSwap (quickColors[dest], quickColors[idx]);
 	updateQuickColorList (&quickColors[dest]);
 }
 
@@ -510,18 +504,13 @@
 		return;
 	}
 
-	print ("Color of %1 is %2\n", button, _buttonColors[button].name());
-	QColor col = QColorDialog::getColor (_buttonColors[button]);
+	QColor color = QColorDialog::getColor (_buttonColors[button]);
 
-	if (col.isValid())
+	if (color.isValid())
 	{
-		int r = col.red();
-		int g = col.green();
-		int b = col.blue();
-
-		QString colname;
-		colname.sprintf ("#%.2X%.2X%.2X", r, g, b);
-		setButtonBackground (button, colname);
+		QString colorname;
+		colorname.sprintf ("#%.2X%.2X%.2X", color.red(), color.green(), color.blue());
+		setButtonBackground (button, colorname);
 	}
 }
 
@@ -534,7 +523,6 @@
 	button->setAutoFillBackground (true);
 	button->setStyleSheet (format ("background-color: %1", value));
 	_buttonColors[button] = QColor (value);
-	print ("Color of %1 set to %2\n", button, value);
 }
 
 //
--- a/src/ldObject.h	Mon Aug 04 03:35:25 2014 +0300
+++ b/src/ldObject.h	Mon Aug 04 13:43:54 2014 +0300
@@ -25,14 +25,16 @@
 
 #define LDOBJ(T)												\
 public:															\
+	static constexpr LDObjectType SubclassType = OBJ_##T;		\
+	LD##T (LDObjectPtr* selfptr);								\
+																\
 	virtual LDObjectType type() const override					\
 	{															\
 		return OBJ_##T;											\
 	}															\
+																\
 	virtual QString asText() const override;					\
 	virtual void invert() override;								\
-																\
-	LD##T (LDObjectPtr* selfptr);								\
 
 #define LDOBJ_NAME(N)          public: virtual QString typeName() const override { return #N; }
 #define LDOBJ_VERTICES(V)      public: virtual int numVertices() const override { return V; }
@@ -594,3 +596,35 @@
 static const int HighResolution = 48;
 
 QString PreferredLicenseText();
+
+template<typename T>
+inline void DynamicExecute (LDObjectPtr obj, std::function<void (QSharedPointer<T> const&)> func)
+{
+	static_assert (std::is_base_of<LDObject, T>::value,
+		"DynamicExecute may only be used with LDObject-derivatives");
+
+	if (obj->type() == T::SubclassType)
+		func (obj.staticCast<T>());
+}
+
+struct LDIterationBreakage {};
+
+template<typename T>
+inline void LDIterate (LDObjectList const& objs,
+	std::function<void (QSharedPointer<T> const&)> func)
+{
+	static_assert (std::is_base_of<LDObject, T>::value,
+		"LDIterate may only be used with LDObject-derivatives");
+
+	try
+	{
+		for (LDObjectPtr const& obj : objs)
+			DynamicExecute<T> (obj, func);
+	}
+	catch (LDIterationBreakage) {}
+}
+
+inline void Break()
+{
+	throw LDIterationBreakage();
+}
--- a/src/miscallenous.cc	Mon Aug 04 03:35:25 2014 +0300
+++ b/src/miscallenous.cc	Mon Aug 04 13:43:54 2014 +0300
@@ -255,7 +255,7 @@
 //
 void RoundToDecimals (double& a, int decimals)
 {
-	assert (decimals >= 0 and decimals < (signed) (sizeof E10 / sizeof *E10));
+	assert (decimals >= 0 and decimals < countof (E10));
 	a = round (a * E10[decimals]) / E10[decimals];
 }
 

mercurial