used the new CircularSectionEditor in CircularPrimitiveEditor

Thu, 21 Jun 2018 19:21:49 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 21 Jun 2018 19:21:49 +0300
changeset 1418
503d4e7e27c9
parent 1417
ed39bfca7a67
child 1419
f7c53002a990

used the new CircularSectionEditor in CircularPrimitiveEditor

src/basics.cpp file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/dialogs/circularprimitiveeditor.cpp file | annotate | diff | comparison | revisions
src/dialogs/circularprimitiveeditor.ui file | annotate | diff | comparison | revisions
src/linetypes/circularprimitive.cpp file | annotate | diff | comparison | revisions
src/linetypes/circularprimitive.h file | annotate | diff | comparison | revisions
--- a/src/basics.cpp	Thu Jun 21 18:46:03 2018 +0300
+++ b/src/basics.cpp	Thu Jun 21 19:21:49 2018 +0300
@@ -259,3 +259,13 @@
 
 	return sum;
 }
+
+bool operator==(const CircularSection& one, const CircularSection& other)
+{
+	return one.segments == other.segments and one.divisions == other.divisions;
+}
+
+bool operator!=(const CircularSection& one, const CircularSection& other)
+{
+	return not (one == other);
+}
--- a/src/basics.h	Thu Jun 21 18:46:03 2018 +0300
+++ b/src/basics.h	Thu Jun 21 19:21:49 2018 +0300
@@ -65,6 +65,10 @@
 	int divisions = 16;
 };
 
+Q_DECLARE_METATYPE(CircularSection)
+bool operator==(const CircularSection& one, const CircularSection& other);
+bool operator!=(const CircularSection& one, const CircularSection& other);
+
 /*
  * Special operator definition that implements the XOR operator for windings.
  * However, if either winding is NoWinding, then this function returns NoWinding.
--- a/src/dialogs/circularprimitiveeditor.cpp	Thu Jun 21 18:46:03 2018 +0300
+++ b/src/dialogs/circularprimitiveeditor.cpp	Thu Jun 21 19:21:49 2018 +0300
@@ -85,21 +85,12 @@
 
 	// Connect various widgets so that changing them changes the primitive object.
 	connect(
-		ui.segments,
-		qOverload<int>(&QSpinBox::valueChanged),
-		[&](int newSegments)
+		ui.section,
+		&CircularSectionEditor::sectionChanged,
+		[&](const CircularSection& newSection)
 		{
 			if (this->primitive)
-				this->primitive->setSegments(newSegments);
-		}
-	);
-	connect(
-		ui.divisions,
-		&QComboBox::currentTextChanged,
-		[&](const QString& newDivisions)
-		{
-			if (this->primitive)
-				this->primitive->setDivisions(newDivisions.toInt());
+				this->primitive->setSection(newSection);
 		}
 	);
 	connect(
@@ -176,11 +167,7 @@
 		}
 
 		// Set the values of the form.
-		withSignalsBlocked(ui.segments, [&](){ ui.segments->setValue(primitive->segments()); });
-		withSignalsBlocked(ui.divisions, [&]()
-		{
-			ui.divisions->setCurrentText(QString::number(primitive->divisions()));
-		});
+		withSignalsBlocked(ui.section, [&](){ ui.section->setSection(primitive->section()); });
 		withSignalsBlocked(ui.color, [&](){ ui.color->setColor(primitive->color()); });
 		withSignalsBlocked(ui.matrix, [&](){ ui.matrix->setMatrix(primitive->transformationMatrix()); });
 		withSignalsBlocked(ui.inverted, [&](){ ui.inverted->setChecked(primitive->isInverted()); });
--- a/src/dialogs/circularprimitiveeditor.ui	Thu Jun 21 18:46:03 2018 +0300
+++ b/src/dialogs/circularprimitiveeditor.ui	Thu Jun 21 19:21:49 2018 +0300
@@ -13,60 +13,27 @@
   <property name="windowTitle">
    <string>Edit circular primitive</string>
   </property>
-  <layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0,1,0">
+  <layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0,0,1,0">
+   <item>
+    <widget class="CircularSectionEditor" name="section" native="true"/>
+   </item>
    <item>
     <layout class="QFormLayout" name="formLayout_2">
      <item row="0" column="0">
-      <widget class="QLabel" name="label_2">
-       <property name="text">
-        <string>Segments:</string>
-       </property>
-      </widget>
-     </item>
-     <item row="0" column="1">
-      <widget class="QSpinBox" name="segments"/>
-     </item>
-     <item row="1" column="0">
-      <widget class="QLabel" name="label">
-       <property name="text">
-        <string>Divisions:</string>
-       </property>
-      </widget>
-     </item>
-     <item row="1" column="1">
-      <widget class="QComboBox" name="divisions">
-       <item>
-        <property name="text">
-         <string>8</string>
-        </property>
-       </item>
-       <item>
-        <property name="text">
-         <string>16</string>
-        </property>
-       </item>
-       <item>
-        <property name="text">
-         <string>48</string>
-        </property>
-       </item>
-      </widget>
-     </item>
-     <item row="2" column="0">
       <widget class="QLabel" name="label_3">
        <property name="text">
         <string>Colour:</string>
        </property>
       </widget>
      </item>
-     <item row="2" column="1">
+     <item row="0" column="1">
       <widget class="ColorButton" name="color">
        <property name="text">
         <string/>
        </property>
       </widget>
      </item>
-     <item row="3" column="1">
+     <item row="1" column="1">
       <widget class="QCheckBox" name="inverted">
        <property name="text">
         <string>Inverted</string>
@@ -177,6 +144,12 @@
  </widget>
  <customwidgets>
   <customwidget>
+   <class>CircularSectionEditor</class>
+   <extends>QWidget</extends>
+   <header>widgets/circularsectioneditor.h</header>
+   <container>1</container>
+  </customwidget>
+  <customwidget>
    <class>MatrixEditor</class>
    <extends>QWidget</extends>
    <header>widgets/matrixeditor.h</header>
--- a/src/linetypes/circularprimitive.cpp	Thu Jun 21 18:46:03 2018 +0300
+++ b/src/linetypes/circularprimitive.cpp	Thu Jun 21 19:21:49 2018 +0300
@@ -8,12 +8,12 @@
 
 QString LDCircularPrimitive::buildFilename() const
 {
-	int numerator = this->m_segments;
-	int denominator = this->m_divisions;
+	int numerator = segments();
+	int denominator = divisions();
 	QString prefix;
 
-	if (m_divisions != MediumResolution)
-		prefix = QString::number(m_divisions) + '\\';
+	if (divisions() != MediumResolution)
+		prefix = QString::number(divisions()) + '\\';
 
 	simplify(numerator, denominator);
 
@@ -33,8 +33,7 @@
 	const QMatrix4x4& matrix) :
 	LDMatrixObject {matrix},
 	m_type {type},
-	m_segments {segments},
-	m_divisions {divisions} {}
+	m_section {segments, divisions} {}
 
 LDObjectType LDCircularPrimitive::type() const
 {
@@ -48,11 +47,11 @@
 
 void LDCircularPrimitive::getVertices(DocumentManager* /* context */, QSet<Vertex>& vertices) const
 {
-	int endSegment = (m_segments == m_divisions) ? m_segments : m_segments + 1;
+	int endSegment = (segments() == divisions()) ? segments() : segments() + 1;
 
 	for (int i = 0; i < endSegment; i += 1)
 	{
-		QPointF point2d = pointOnLDrawCircumference(i, m_divisions);
+		QPointF point2d = pointOnLDrawCircumference(i, divisions());
 
 		for (double y_value : {0.0, 1.0})
 		{
@@ -139,8 +138,8 @@
 {
 	PrimitiveModel primitive;
 	primitive.type = m_type;
-	primitive.segments = m_segments;
-	primitive.divisions = m_divisions;
+	primitive.segments = segments();
+	primitive.divisions = divisions();
 	primitive.ringNumber = 0;
 	primitive.generateBody(model, deep);
 }
@@ -182,18 +181,18 @@
 {
 	QString prefix;
 
-	if (m_divisions == HighResolution)
+	if (divisions() == HighResolution)
 		prefix = "Hi-Res";
-	else if (m_divisions == LowResolution)
+	else if (divisions() == LowResolution)
 		prefix = "Lo-Res";
-	else if (m_divisions != MediumResolution)
-		prefix = format("%1-resolution", m_divisions);
+	else if (divisions() != MediumResolution)
+		prefix = format("%1-resolution", divisions());
 
 	QString result = format(
 		"%1 %2 %3 %4, (",
 		prefix,
 		PrimitiveModel::typeName(m_type),
-		m_segments / m_divisions,
+		double(segments()) / double(divisions()),
 		position().toString(true)
 	).simplified();
 
@@ -217,22 +216,32 @@
 
 int LDCircularPrimitive::segments() const
 {
-	return m_segments;
+	return m_section.segments;
 }
 
 void LDCircularPrimitive::setSegments(int newSegments)
 {
-	changeProperty(&m_segments, newSegments);
+	changeProperty(&m_section.segments, newSegments);
 }
 
 int LDCircularPrimitive::divisions() const
 {
-	return m_divisions;
+	return m_section.divisions;
 }
 
 void LDCircularPrimitive::setDivisions(int newDivisions)
 {
-	changeProperty(&m_divisions, newDivisions);
+	changeProperty(&m_section.divisions, newDivisions);
+}
+
+const CircularSection&LDCircularPrimitive::section() const
+{
+	return m_section;
+}
+
+void LDCircularPrimitive::setSection(const CircularSection& newSection)
+{
+	changeProperty(&m_section, newSection);
 }
 
 int LDCircularPrimitive::triangleCount(DocumentManager*) const
@@ -244,19 +253,21 @@
 		throw std::logic_error("Bad primitive type to LDCircularPrimitive");
 
 	case PrimitiveModel::Cylinder:
+	case PrimitiveModel::CylinderOpen:
+		return 2 * segments();
+
 	case PrimitiveModel::CylinderClosed:
-	case PrimitiveModel::CylinderOpen:
-		return 2 * m_segments;
+		return 3 * segments();
 
 	case PrimitiveModel::Disc:
 	case PrimitiveModel::DiscNegative:
-		return m_segments;
+		return segments();
 
 	case PrimitiveModel::Circle:
 		return 0;
 
 	case PrimitiveModel::Chord:
-		return qBound(0, m_segments - 1, m_divisions - 2);
+		return qBound(0, segments() - 1, divisions() - 2);
 	}
 
 	return 0;
@@ -298,5 +309,5 @@
 void LDCircularPrimitive::serialize(class Serializer& serializer)
 {
 	LDMatrixObject::serialize(serializer);
-	serializer << m_segments << m_divisions << m_type;
+	serializer << m_section << m_type;
 }
--- a/src/linetypes/circularprimitive.h	Thu Jun 21 18:46:03 2018 +0300
+++ b/src/linetypes/circularprimitive.h	Thu Jun 21 19:21:49 2018 +0300
@@ -35,6 +35,8 @@
 	void setSegments(int newSegments);
 	int divisions() const;
 	void setDivisions(int newDivisions);
+	const CircularSection& section() const;
+	void setSection(const CircularSection& newSection);
 	int triangleCount(DocumentManager*) const override;
 	QString iconName() const override;
 	void serialize(class Serializer& serializer) override;
@@ -45,6 +47,5 @@
 	QString stem() const;
 
 	PrimitiveModel::Type m_type = PrimitiveModel::Circle;
-	int m_segments = MediumResolution;
-	int m_divisions = MediumResolution;
+	CircularSection m_section;
 };

mercurial