added subfile reference editor

Sun, 18 Mar 2018 12:00:39 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 18 Mar 2018 12:00:39 +0200
changeset 1297
389516787a4c
parent 1296
aee618d087ff
child 1298
dbc8bb2a4d84

added subfile reference editor

CMakeLists.txt file | annotate | diff | comparison | revisions
src/dialogs/subfilereferenceeditor.cpp file | annotate | diff | comparison | revisions
src/dialogs/subfilereferenceeditor.h file | annotate | diff | comparison | revisions
src/dialogs/subfilereferenceeditor.ui file | annotate | diff | comparison | revisions
src/guiutilities.cpp file | annotate | diff | comparison | revisions
src/guiutilities.h file | annotate | diff | comparison | revisions
src/linetypes/modelobject.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.h file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/primitives.h file | annotate | diff | comparison | revisions
src/toolsets/basictoolset.cpp file | annotate | diff | comparison | revisions
src/widgets/vertexobjecteditor.cpp file | annotate | diff | comparison | revisions
src/widgets/vertexobjecteditor.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Sat Mar 17 23:59:15 2018 +0200
+++ b/CMakeLists.txt	Sun Mar 18 12:00:39 2018 +0200
@@ -62,6 +62,7 @@
 	src/dialogs/externalprogrampathdialog.cpp
 	src/dialogs/generateprimitivedialog.cpp
 	src/dialogs/ldrawpathdialog.cpp
+	src/dialogs/subfilereferenceeditor.cpp
 	src/dialogs/newpartdialog.cpp
 	src/dialogs/openprogressdialog.cpp
 	src/editmodes/abstractEditMode.cpp
@@ -133,6 +134,7 @@
 	src/dialogs/generateprimitivedialog.h
 	src/dialogs/ldrawpathdialog.h
 	src/dialogs/newpartdialog.h
+	src/dialogs/subfilereferenceeditor.h
 	src/dialogs/openprogressdialog.h
 	src/editmodes/abstractEditMode.h
 	src/editmodes/circleMode.h
@@ -183,6 +185,7 @@
 	src/dialogs/rectifierdialog.ui
 	src/dialogs/replacecoordinatesdialog.ui
 	src/dialogs/rotationpointdialog.ui
+	src/dialogs/subfilereferenceeditor.ui
 	src/dialogs/ytruderdialog.ui
 	src/toolsets/fixroundingerrors.ui
 	src/mainwindow.ui
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dialogs/subfilereferenceeditor.cpp	Sun Mar 18 12:00:39 2018 +0200
@@ -0,0 +1,69 @@
+#include "subfilereferenceeditor.h"
+#include "ui_subfilereferenceeditor.h"
+#include "../linetypes/modelobject.h"
+#include "../primitives.h"
+#include "../guiutilities.h"
+
+SubfileReferenceEditor::SubfileReferenceEditor(LDSubfileReference* reference, QWidget* parent) :
+	QDialog {parent},
+	reference {reference},
+	ui {*new Ui::SubfileReferenceEditor}
+{
+	this->ui.setupUi(this);
+	this->ui.referenceName->setText(reference->referenceName());
+	this->color = reference->color();
+	this->ui.positionX->setValue(reference->position().x());
+	this->ui.positionY->setValue(reference->position().y());
+	this->ui.positionZ->setValue(reference->position().z());
+	::setupColorButton(parent, this->ui.colorButton, &this->color);
+	for (int i : {0, 1, 2})
+	for (int j : {0, 1, 2})
+	{
+		QLayoutItem* item = this->ui.matrixLayout->itemAtPosition(i, j);
+		QDoubleSpinBox* spinbox = item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : nullptr;
+		spinbox->setValue(reference->transformationMatrix()(i, j));
+	}
+	connect(
+		this->ui.primitivesTreeView,
+		&QTreeView::clicked,
+		[&](const QModelIndex& index)
+		{
+			QAbstractItemModel* model = this->ui.primitivesTreeView->model();
+			QVariant primitiveName = model->data(index, PrimitiveManager::PrimitiveNameRole);
+
+			if (primitiveName.isValid())
+				this->ui.referenceName->setText(primitiveName.toString());
+		}
+	);
+}
+
+void SubfileReferenceEditor::accept()
+{
+	this->reference->setReferenceName(this->ui.referenceName->text());
+	Matrix transformationMatrix;
+	for (int i : {0, 1, 2})
+	for (int j : {0, 1, 2})
+	{
+		QLayoutItem* item = this->ui.matrixLayout->itemAtPosition(i, j);
+		QDoubleSpinBox* spinbox = item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : nullptr;
+		transformationMatrix(i, j) = spinbox->value();
+	}
+	this->reference->setTransformationMatrix(transformationMatrix);
+	this->reference->setPosition({
+		this->ui.positionX->value(),
+		this->ui.positionY->value(),
+		this->ui.positionZ->value()
+	});
+	this->reference->setColor(this->color);
+	QDialog::accept();
+}
+
+void SubfileReferenceEditor::setPrimitivesTree(PrimitiveManager* primitives)
+{
+	this->ui.primitivesTreeView->setModel(primitives);
+}
+
+SubfileReferenceEditor::~SubfileReferenceEditor()
+{
+	delete &this->ui;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dialogs/subfilereferenceeditor.h	Sun Mar 18 12:00:39 2018 +0200
@@ -0,0 +1,20 @@
+#pragma once
+#include <QDialog>
+#include "../main.h"
+
+class SubfileReferenceEditor : public QDialog
+{
+	Q_OBJECT
+
+public:
+	SubfileReferenceEditor(class LDSubfileReference* reference, QWidget *parent = nullptr);
+	~SubfileReferenceEditor();
+
+	void accept() override;
+	void setPrimitivesTree(class PrimitiveManager* primitives);
+
+private:
+	class Ui_SubfileReferenceEditor& ui;
+	class LDSubfileReference* const reference;
+	LDColor color;
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dialogs/subfilereferenceeditor.ui	Sun Mar 18 12:00:39 2018 +0200
@@ -0,0 +1,297 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>SubfileReferenceEditor</class>
+ <widget class="QDialog" name="SubfileReferenceEditor">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>625</width>
+    <height>642</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout" stretch="1,0,0">
+   <item>
+    <widget class="QTreeView" name="primitivesTreeView"/>
+   </item>
+   <item>
+    <layout class="QFormLayout" name="formLayout">
+     <item row="0" column="0">
+      <widget class="QLabel" name="label">
+       <property name="text">
+        <string>Name of subfile:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="1">
+      <widget class="QLineEdit" name="referenceName"/>
+     </item>
+     <item row="1" column="0">
+      <widget class="QLabel" name="label_4">
+       <property name="text">
+        <string>Colour:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="1">
+      <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="3,10">
+       <item>
+        <widget class="QPushButton" name="colorButton">
+         <property name="text">
+          <string/>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <spacer name="horizontalSpacer">
+         <property name="orientation">
+          <enum>Qt::Horizontal</enum>
+         </property>
+         <property name="sizeHint" stdset="0">
+          <size>
+           <width>40</width>
+           <height>20</height>
+          </size>
+         </property>
+        </spacer>
+       </item>
+      </layout>
+     </item>
+     <item row="2" column="0">
+      <widget class="QLabel" name="label_2">
+       <property name="text">
+        <string>Position:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="2" column="1">
+      <layout class="QHBoxLayout" name="horizontalLayout">
+       <item>
+        <widget class="QDoubleSpinBox" name="positionX">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QDoubleSpinBox" name="positionY">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QDoubleSpinBox" name="positionZ">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+      </layout>
+     </item>
+     <item row="4" column="0">
+      <widget class="QLabel" name="label_3">
+       <property name="text">
+        <string>Transformation matrix:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="4" column="1">
+      <layout class="QGridLayout" name="matrixLayout">
+       <item row="1" column="1">
+        <widget class="QDoubleSpinBox" name="matrixE">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="0">
+        <widget class="QDoubleSpinBox" name="matrixA">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="2" column="0">
+        <widget class="QDoubleSpinBox" name="matrixG">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="2" column="1">
+        <widget class="QDoubleSpinBox" name="matrixH">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="2" column="2">
+        <widget class="QDoubleSpinBox" name="matrixI">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="1" column="2">
+        <widget class="QDoubleSpinBox" name="matrixF">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="2">
+        <widget class="QDoubleSpinBox" name="matrixC">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="1" column="0">
+        <widget class="QDoubleSpinBox" name="matrixD">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="1">
+        <widget class="QDoubleSpinBox" name="matrixB">
+         <property name="decimals">
+          <number>5</number>
+         </property>
+         <property name="minimum">
+          <double>-10000.000000000000000</double>
+         </property>
+         <property name="maximum">
+          <double>10000.000000000000000</double>
+         </property>
+        </widget>
+       </item>
+      </layout>
+     </item>
+     <item row="3" column="1">
+      <widget class="Line" name="line">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>SubfileReferenceEditor</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>SubfileReferenceEditor</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
--- a/src/guiutilities.cpp	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/guiutilities.cpp	Sun Mar 18 12:00:39 2018 +0200
@@ -18,9 +18,11 @@
 
 #include <QComboBox>
 #include <QPainter>
+#include <QPushButton>
 #include "colors.h"
 #include "guiutilities.h"
 #include "lddocument.h"
+#include "dialogs/colorselector.h"
 #include "mainwindow.h"
 
 GuiUtilities::GuiUtilities (QObject* parent) :
@@ -146,3 +148,35 @@
 
 	return colors;
 }
+
+
+void setColorButton(QPushButton* button, LDColor color)
+{
+	if (color.isValid())
+	{
+		button->setFlat(true);
+		button->setText(color.name());
+		button->setStyleSheet(format("background-color: %1", color.hexcode()));
+		button->setStyleSheet(format("color: %1", color.edgeColor().name()));
+	}
+	else
+	{
+		button->setFlat(false);
+		button->setText("");
+		button->setStyleSheet("");
+	}
+}
+
+void setupColorButton(QWidget* parent, QPushButton* button, LDColor* color)
+{
+	QObject::connect(
+		button,
+		&QPushButton::clicked,
+		[&]()
+		{
+			if (ColorSelector::selectColor(parent, *color, *color))
+				::setColorButton(button, *color);
+		}
+	);
+	setColorButton(button, *color);
+}
--- a/src/guiutilities.h	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/guiutilities.h	Sun Mar 18 12:00:39 2018 +0200
@@ -38,3 +38,5 @@
 };
 
 QIcon makeColorIcon(LDColor color, int size);
+void setColorButton(class QPushButton* button, LDColor color);
+void setupColorButton(QWidget* parent, QPushButton* button, LDColor* color);
--- a/src/linetypes/modelobject.cpp	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/linetypes/modelobject.cpp	Sun Mar 18 12:00:39 2018 +0200
@@ -236,6 +236,11 @@
 	}
 }
 
+void LDSubfileReference::setReferenceName(const QString& newReferenceName)
+{
+	changeProperty(&this->m_referenceName, newReferenceName);
+}
+
 // =============================================================================
 //
 // Moves this object using the given vertex as a movement List
--- a/src/linetypes/modelobject.h	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/linetypes/modelobject.h	Sun Mar 18 12:00:39 2018 +0200
@@ -230,6 +230,7 @@
 	bool hasMatrix() const override { return true; }
 	QString typeName() const override { return "subfilereference"; }
 	void serialize(class Serializer& serializer) override;
+	void setReferenceName(const QString& newReferenceName);
 
 private:
 	QString m_referenceName;
--- a/src/primitives.cpp	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/primitives.cpp	Sun Mar 18 12:00:39 2018 +0200
@@ -551,8 +551,16 @@
 			{
 			case Qt::DisplayRole:
 				return format("%1 - %2", primitive.name, primitive.title);
+
 			case Qt::DecorationRole:
 				return MainWindow::getIcon("subfilereference");
+
+			case PrimitiveNameRole:
+				return primitive.name;
+
+			case PrimitiveDescriptionRole:
+				return primitive.title;
+
 			default:
 				return {};
 			}
@@ -566,8 +574,10 @@
 			{
 			case Qt::DisplayRole:
 				return category->name();
+
 			case Qt::DecorationRole:
 				return MainWindow::getIcon("folder");
+
 			default:
 				return {};
 			}
--- a/src/primitives.h	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/primitives.h	Sun Mar 18 12:00:39 2018 +0200
@@ -91,6 +91,12 @@
 	Q_OBJECT
 
 public:
+	enum
+	{
+		PrimitiveNameRole = Qt::UserRole + 10,
+		PrimitiveDescriptionRole,
+	};
+
 	PrimitiveManager(QObject* parent);
 
 	PrimitiveScanner* activeScanner();
--- a/src/toolsets/basictoolset.cpp	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/toolsets/basictoolset.cpp	Sun Mar 18 12:00:39 2018 +0200
@@ -32,6 +32,7 @@
 #include "../ldobjectiterator.h"
 #include "../mainwindow.h"
 #include "../dialogs/colorselector.h"
+#include "../dialogs/subfilereferenceeditor.h"
 #include "../grid.h"
 #include "../parser.h"
 #include "../widgets/vertexobjecteditor.h"
@@ -292,36 +293,44 @@
 }
 
 template<typename T>
-void createBasicObject(MainWindow* window)
+static T* createObject(MainWindow* window)
 {
-	LDObject* object = window->currentDocument()->emplaceAt<T>(window->suggestInsertPoint());
+	return window->currentDocument()->emplaceAt<T>(window->suggestInsertPoint());
+}
+
+template<typename T>
+static void createVertexObject(MainWindow* window)
+{
+	LDObject* object = createObject<T>(window);
 	VertexObjectEditor editor {object};
 	editor.exec();
 }
 
 void BasicToolset::newSubfile()
 {
-	// TODO:
+	LDSubfileReference* reference = createObject<LDSubfileReference>(this->m_window);
+	SubfileReferenceEditor editor {reference, this->m_window};
+	editor.exec();
 }
 
 void BasicToolset::newLine()
 {
-	createBasicObject<LDEdgeLine>(this->m_window);
+	createVertexObject<LDEdgeLine>(this->m_window);
 }
 
 void BasicToolset::newTriangle()
 {
-	createBasicObject<LDTriangle>(this->m_window);
+	createVertexObject<LDTriangle>(this->m_window);
 }
 
 void BasicToolset::newQuadrilateral()
 {
-	createBasicObject<LDQuadrilateral>(this->m_window);
+	createVertexObject<LDQuadrilateral>(this->m_window);
 }
 
 void BasicToolset::newConditionalLine()
 {
-	createBasicObject<LDConditionalEdge>(this->m_window);
+	createVertexObject<LDConditionalEdge>(this->m_window);
 }
 
 void BasicToolset::newComment()
--- a/src/widgets/vertexobjecteditor.cpp	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/widgets/vertexobjecteditor.cpp	Sun Mar 18 12:00:39 2018 +0200
@@ -3,6 +3,7 @@
 #include "vertexobjecteditor.h"
 #include "ui_vertexobjecteditor.h"
 #include "../dialogs/colorselector.h"
+#include "../guiutilities.h"
 
 VertexObjectEditor::VertexObjectEditor(LDObject* object, QWidget *parent) :
 	QDialog {parent},
@@ -12,15 +13,8 @@
 {
 	this->ui.setupUi(this);
 	this->ui.verticesContainer->setLayout(this->vertexGrid);
-	connect(
-		this->ui.color,
-		&QPushButton::clicked,
-		[&]()
-		{
-			ColorSelector::selectColor(this, this->currentColor, this->currentColor);
-			this->setColorButton(this->currentColor);
-		}
-	);
+	this->currentColor = this->object->color();
+	::setupColorButton(parent, this->ui.color, &this->currentColor);
 
 	for (int i : range(0, 1, object->numVertices() - 1))
 	{
@@ -44,9 +38,6 @@
 				spinbox->setValue(this->object->vertex(i)[axis]);
 		}
 	}
-
-	this->currentColor = this->object->color();
-	this->setColorButton(this->object->color());
 }
 
 VertexObjectEditor::~VertexObjectEditor()
@@ -54,21 +45,6 @@
 	delete &this->ui;
 }
 
-void VertexObjectEditor::setColorButton(LDColor color)
-{
-	if (color.isValid())
-	{
-		this->ui.color->setText(color.name());
-		this->ui.color->setStyleSheet(format("background-color: %1", color.hexcode()));
-		this->ui.color->setStyleSheet(format("color: %1", color.edgeColor().name()));
-	}
-	else
-	{
-		this->ui.color->setText("");
-		this->ui.color->setStyleSheet("");
-	}
-}
-
 QDoubleSpinBox* VertexObjectEditor::spinboxAt(int i, Axis axis)
 {
 	QWidget* widget = this->vertexGrid->itemAtPosition(i, axis)->widget();
--- a/src/widgets/vertexobjecteditor.h	Sat Mar 17 23:59:15 2018 +0200
+++ b/src/widgets/vertexobjecteditor.h	Sun Mar 18 12:00:39 2018 +0200
@@ -14,7 +14,6 @@
 
 private:
 	class QDoubleSpinBox* spinboxAt(int i, Axis axis);
-	void setColorButton(LDColor color);
 
 	class Ui_VertexObjectEditor& ui;
 	class QGridLayout* vertexGrid;

mercurial