cleaned up LDColor constructors

Sun, 10 Jun 2018 16:04:38 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 10 Jun 2018 16:04:38 +0300
changeset 1397
5d5c11af0268
parent 1396
3442a59ab05c
child 1398
1c70d3447d20

cleaned up LDColor constructors

src/colors.cpp file | annotate | diff | comparison | revisions
src/colors.h file | annotate | diff | comparison | revisions
src/dialogs/colorselector.cpp file | annotate | diff | comparison | revisions
src/glcompiler.cpp file | annotate | diff | comparison | revisions
src/guiutilities.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.cpp file | annotate | diff | comparison | revisions
src/parser.cpp file | annotate | diff | comparison | revisions
src/toolsets/extprogramtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/colors.cpp	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/colors.cpp	Sun Jun 10 16:04:38 2018 +0300
@@ -22,7 +22,7 @@
 #include "main.h"
 
 ColorData* LDColor::colorData = nullptr;
-const LDColor LDColor::nullColor = -1;
+const LDColor LDColor::nullColor {-1};
 
 /*
  * Initializes the color information module.
@@ -47,12 +47,6 @@
     m_index {index} {}
 
 /*
- * Constructs a direct color.
- */
-LDColor::LDColor(QColor color, bool transparent) :
-    m_index {directColorIndex(color, transparent)} {}
-
-/*
  * Returns whether or not the color is valid.
  */
 bool LDColor::isValid() const
@@ -180,13 +174,13 @@
 /*
  * Returns the LDraw color index for a direct color.
  */
-qint32 LDColor::directColorIndex(QColor color, bool transparent)
+LDColor LDColor::directColor(QColor color, bool transparent)
 {
 	qint32 index = transparent ? 0x03000000 : 0x02000000;
 	index |= color.red() << 16;
 	index |= color.green() << 8;
 	index |= color.blue();
-	return index;
+	return LDColor {index};
 }
 
 /*
@@ -212,12 +206,12 @@
 ColorData::ColorData()
 {
 	// Initialize main and edge colors, they're special like that.
-	m_data[MainColor].faceColor = "#AAAAAA";
-	m_data[MainColor].edgeColor = Qt::black;
-	m_data[MainColor].name = "Main color";
-	m_data[EdgeColor].faceColor = Qt::black;
-	m_data[EdgeColor].edgeColor = Qt::black;
-	m_data[EdgeColor].name = "Edge color";
+	m_data[MainColor.index()].faceColor = "#AAAAAA";
+	m_data[MainColor.index()].edgeColor = Qt::black;
+	m_data[MainColor.index()].name = "Main color";
+	m_data[EdgeColor.index()].faceColor = Qt::black;
+	m_data[EdgeColor.index()].edgeColor = Qt::black;
+	m_data[EdgeColor.index()].name = "Edge color";
 }
 
 /*
--- a/src/colors.h	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/colors.h	Sun Jun 10 16:04:38 2018 +0300
@@ -55,8 +55,7 @@
 {
 public:
 	LDColor();
-	LDColor(qint32 index);
-	LDColor(QColor color, bool transparent = false);
+	explicit LDColor(qint32 index);
 	LDColor(const LDColor& other) = default;
 
 	bool isLDConfigColor() const;
@@ -70,15 +69,15 @@
 	QString indexString() const;
 
 	static const LDColor nullColor;
-	static qint32 directColorIndex(QColor color, bool transparent = false);
+	static LDColor directColor(QColor color, bool transparent = false);
 	static void initColors();
 
 	LDColor& operator=(qint32 index) { m_index = index; return *this; }
 	LDColor& operator=(const LDColor &other) = default;
-	LDColor operator++() { return ++m_index; }
-	LDColor operator++(int) { return m_index++; }
-	LDColor operator--() { return --m_index; }
-	LDColor operator--(int) { return m_index--; }
+	LDColor operator++() { return LDColor {++m_index}; }
+	LDColor operator++(int) { return LDColor {m_index++}; }
+	LDColor operator--() { return LDColor {--m_index}; }
+	LDColor operator--(int) { return LDColor {m_index--}; }
 	bool operator==(LDColor other) const { return index() == other.index(); }
 	bool operator!=(LDColor other) const { return index() != other.index(); }
 	bool operator<(LDColor other) const { return index() < other.index(); }
@@ -117,8 +116,5 @@
 
 int luma(const QColor& col);
 
-enum
-{
-	MainColor = 16,
-	EdgeColor = 24,
-};
+static const LDColor MainColor {16};
+static const LDColor EdgeColor {24};
--- a/src/dialogs/colorselector.cpp	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/dialogs/colorselector.cpp	Sun Jun 10 16:04:38 2018 +0300
@@ -119,11 +119,11 @@
  */
 void ColorSelector::chooseDirectColor()
 {
-	QColor defaultColor = selectedColor() != -1 ? selectedColor().faceColor() : Qt::white;
+	QColor defaultColor = selectedColor().isValid() ? selectedColor().faceColor() : Qt::white;
 	QColor newColor = QColorDialog::getColor(defaultColor);
 
 	if (newColor.isValid())
-		setSelectedColor({newColor, ui.transparentDirectColor->isChecked()});
+		setSelectedColor(LDColor::directColor(newColor, ui.transparentDirectColor->isChecked()));
 }
 
 /*
@@ -132,7 +132,7 @@
 void ColorSelector::transparentCheckboxClicked()
 {
 	if (selectedColor().isDirect())
-		setSelectedColor({selectedColor().faceColor(), ui.transparentDirectColor->isChecked()});
+		setSelectedColor(LDColor::directColor(selectedColor().faceColor(), ui.transparentDirectColor->isChecked()));
 }
 
 /*
--- a/src/glcompiler.cpp	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/glcompiler.cpp	Sun Jun 10 16:04:38 2018 +0300
@@ -179,7 +179,7 @@
 
 	case VboSubclass::RegularColors:
 		// For normal colors, use the polygon's color.
-		if (polygon.color == MainColor)
+		if (LDColor {polygon.color} == MainColor)
 		{
 			// If it's the main color, use the polygon owner's color.
 			if (polygonOwner->color() == MainColor)
@@ -192,7 +192,7 @@
 				color = polygonOwner->color().faceColor();
 			}
 		}
-		else if (polygon.color == EdgeColor)
+		else if (LDColor {polygon.color} == EdgeColor)
 		{
 			// Edge color is black, unless we have a dark background, in which case lines need to be bright.
 			color = luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white;
--- a/src/guiutilities.cpp	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/guiutilities.cpp	Sun Jun 10 16:04:38 2018 +0300
@@ -144,7 +144,7 @@
 		}
 		else
 		{
-			LDColor color = colorName.toInt();
+			LDColor color {colorName.toInt()};
 
 			if (color.isValid())
 				colors.append(ColorToolbarItem {color});
--- a/src/linetypes/modelobject.cpp	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/linetypes/modelobject.cpp	Sun Jun 10 16:04:38 2018 +0300
@@ -464,7 +464,7 @@
 	for (LDPolygon& poly : polygons)
 	{
 		LDEdgeLine* line = model.emplace<LDEdgeLine>(poly.vertices[0], poly.vertices[1]);
-		line->setColor (poly.color);
+		line->setColor(LDColor {poly.color});
 	}
 }
 
--- a/src/parser.cpp	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/parser.cpp	Sun Jun 10 16:04:38 2018 +0300
@@ -363,7 +363,7 @@
 						CheckTokenCount (tokens, 16);
 						CheckTokenNumbers (tokens, 3, 15);
 						LDBezierCurve* obj = model.emplaceAt<LDBezierCurve>(position);
-						obj->setColor(tokens[3].toInt(nullptr, 0));
+						obj->setColor(LDColor {tokens[3].toInt(nullptr, 0)});
 
 						for (int i = 0; i < 4; ++i)
 							obj->setVertex (i, parseVertex (tokens, 4 + (i * 3)));
@@ -409,7 +409,7 @@
 					obj = model.emplaceAt<LDSubfileReference>(position, referenceName, transform, displacement);
 				}
 
-				obj->setColor (tokens[1].toInt(nullptr, 0));
+				obj->setColor(LDColor {tokens[1].toInt(nullptr, 0)});
 				return obj;
 			}
 
@@ -420,7 +420,7 @@
 
 				// Line
 				LDEdgeLine* obj = model.emplaceAt<LDEdgeLine>(position);
-				obj->setColor (tokens[1].toInt(nullptr, 0));
+				obj->setColor(LDColor {tokens[1].toInt(nullptr, 0)});
 
 				for (int i = 0; i < 2; ++i)
 					obj->setVertex (i, parseVertex (tokens, 2 + (i * 3)));   // 2 - 7
@@ -435,7 +435,7 @@
 
 				// Triangle
 				LDTriangle* obj = model.emplaceAt<LDTriangle>(position);
-				obj->setColor (tokens[1].toInt(nullptr, 0));
+				obj->setColor(LDColor {tokens[1].toInt(nullptr, 0)});
 
 				for (int i = 0; i < 3; ++i)
 					obj->setVertex (i, parseVertex (tokens, 2 + (i * 3)));   // 2 - 10
@@ -457,7 +457,7 @@
 				else
 					obj = model.emplaceAt<LDConditionalEdge>(position);
 
-				obj->setColor (tokens[1].toInt(nullptr, 0));
+				obj->setColor(LDColor {tokens[1].toInt(nullptr, 0)});
 
 				for (int i = 0; i < 4; ++i)
 					obj->setVertex (i, parseVertex (tokens, 2 + (i * 3)));   // 2 - 13
--- a/src/toolsets/extprogramtoolset.cpp	Sun Jun 10 15:53:21 2018 +0300
+++ b/src/toolsets/extprogramtoolset.cpp	Sun Jun 10 16:04:38 2018 +0300
@@ -540,8 +540,8 @@
 	if (not dlg->exec())
 		return;
 
-	LDColor in1Col = ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt();
-	LDColor in2Col = ui.cmb_col2->itemData (ui.cmb_col2->currentIndex()).toInt();
+	LDColor in1Col {ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt()};
+	LDColor in2Col {ui.cmb_col2->itemData (ui.cmb_col2->currentIndex()).toInt()};
 	QTemporaryFile in1dat, in2dat, outdat;
 	QString in1DATName, in2DATName, outDATName;
 

mercurial