Make LDForge widgets visible in Qt Designer

Wed, 22 Jun 2022 21:42:10 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 22 Jun 2022 21:42:10 +0300
changeset 253
8b994c917f69
parent 252
da4876bfd822
child 254
b7b29cb82360

Make LDForge widgets visible in Qt Designer

CMakeLists.txt file | annotate | diff | comparison | revisions
src/widgets/doublespinbox.cpp file | annotate | diff | comparison | revisions
src/widgets/doublespinbox.h file | annotate | diff | comparison | revisions
widgets/CMakeLists.txt file | annotate | diff | comparison | revisions
widgets/designerplugins.cpp file | annotate | diff | comparison | revisions
widgets/designerplugins.h file | annotate | diff | comparison | revisions
widgets/doublespinbox.cpp file | annotate | diff | comparison | revisions
widgets/doublespinbox.h file | annotate | diff | comparison | revisions
widgets/matrixeditor.h file | annotate | diff | comparison | revisions
widgets/matrixeditor.ui file | annotate | diff | comparison | revisions
widgets/multiplyfactordialog.ui file | annotate | diff | comparison | revisions
widgets/vec3editor.ui file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Wed Jun 22 20:27:53 2022 +0300
+++ b/CMakeLists.txt	Wed Jun 22 21:42:10 2022 +0300
@@ -20,22 +20,7 @@
 include_directories(${GLM_INCLUDE_DIR})
 add_definitions(-DQT_NO_KEYWORDS)
 source_group("LDForge" REGULAR_EXPRESSION "src/.+\\.(cpp|h|ui)")
-
-qt5_wrap_ui(LDFORGEWIDGETS_FORMS
-	widgets/vec3editor.ui
-	widgets/multiplyfactordialog.ui
-	widgets/matrixeditor.ui
-)
-add_library(ldforgewidgets STATIC
-	widgets/matrixeditor.cpp
-	widgets/matrixeditor.h
-	widgets/vec3editor.cpp
-	widgets/vec3editor.h
-	widgets/multiplyfactordialog.cpp
-	widgets/multiplyfactordialog.h
-	${LDFORGEWIDGETS_FORMS}
-)
-target_link_libraries(ldforgewidgets Qt5::Widgets)
+add_subdirectory(widgets)
 
 set (LDFORGE_SOURCES
 	src/colors.cpp
@@ -69,7 +54,6 @@
 	src/widgets/colorbutton.cpp
 	src/widgets/colorindexinput.cpp
 	src/widgets/colorselectdialog.cpp
-	src/widgets/doublespinbox.cpp
 )
 set (LDFORGE_HEADERS
 	src/basics.h
@@ -110,7 +94,6 @@
 	src/widgets/colorbutton.h
 	src/widgets/colorindexinput.h
 	src/widgets/colorselectdialog.h
-	src/widgets/doublespinbox.h
 )
 set (LDFORGE_FORMS
 	src/mainwindow.ui
--- a/src/widgets/doublespinbox.cpp	Wed Jun 22 20:27:53 2022 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-/*
- *  LDForge: LDraw parts authoring CAD
- *  Copyright (C) 2013 - 2020 Teemu Piippo
- *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "doublespinbox.h"
-
-/*
- * Constructs a new double spin box. The locale is fixed to system default "C".
- */
-DoubleSpinBox::DoubleSpinBox(QWidget* parent) :
-	QDoubleSpinBox {parent}
-{
-	this->setLocale({"C"});
-	this->setRange(-1e6, 1e6);
-	this->setDecimals(4);
-}
-
-/*
- * Reimplementation of QDoubleSpinBox::textFromValue to remove trailing zeros.
- */
-QString DoubleSpinBox::textFromValue(double value) const
-{
-	QString result = QDoubleSpinBox::textFromValue(value);
-	if (result.contains("."))
-	{
-		// Remove trailing zeros
-		while (result.endsWith("0"))
-			result.chop(1);
-		// Remove trailing decimal point if we just removed all the zeros.
-		if (result.endsWith("."))
-			result.chop(1);
-	}
-	return result;
-}
-
-/*
- * Reimplementation of QDoubleSpinBox::validate to fix the decimal point if the locale-specific
- * decimal point was used.
- */
-QValidator::State DoubleSpinBox::validate(QString& input, int& pos) const
-{
-	input.replace(QLocale().decimalPoint(), this->locale().decimalPoint());
-	return QDoubleSpinBox::validate(input, pos);
-}
--- a/src/widgets/doublespinbox.h	Wed Jun 22 20:27:53 2022 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-/*
- *  LDForge: LDraw parts authoring CAD
- *  Copyright (C) 2013 - 2020 Teemu Piippo
- *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-#include <QDoubleSpinBox>
-
-/*
- * A version of QDoubleSpinBox that consistently uses "." as the decimal separator
- * and does not display trailing zeros.
- */
-class DoubleSpinBox : public QDoubleSpinBox
-{
-public:
-	DoubleSpinBox(QWidget* parent = nullptr);
-protected:
-	QString textFromValue(double value) const override;
-	QValidator::State validate(QString& input, int& pos) const override;
-};
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/CMakeLists.txt	Wed Jun 22 21:42:10 2022 +0300
@@ -0,0 +1,35 @@
+find_package(Qt5 REQUIRED COMPONENTS UiPlugin)
+qt5_wrap_ui(LDFORGEWIDGETS_FORMS
+	vec3editor.ui
+	multiplyfactordialog.ui
+	matrixeditor.ui
+)
+
+add_library(ldforgewidgets SHARED
+	designerplugins.cpp
+	designerplugins.h
+	doublespinbox.cpp
+	doublespinbox.h
+	matrixeditor.cpp
+	matrixeditor.h
+	vec3editor.cpp
+	vec3editor.h
+	multiplyfactordialog.cpp
+	multiplyfactordialog.h
+	${LDFORGEWIDGETS_FORMS}
+)
+
+get_target_property(Qt5UiPlugin_INCLUDES Qt5::UiPlugin INTERFACE_INCLUDE_DIRECTORIES)
+target_include_directories(ldforgewidgets PUBLIC ${Qt5UiPlugin_INCLUDES})
+target_include_directories(ldforgewidgets PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
+target_include_directories(ldforgewidgets PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")
+target_link_libraries(ldforgewidgets Qt5::Widgets)
+
+get_target_property(QT_QMAKE_EXECUTABLE Qt5::qmake LOCATION)
+execute_process(COMMAND ${QT_QMAKE_EXECUTABLE} -query QT_INSTALL_PLUGINS
+	OUTPUT_VARIABLE QT_INSTALL_PLUGINS OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+install(TARGETS ldforgewidgets LIBRARY DESTINATION lib)
+install(TARGETS ldforgewidgets
+	LIBRARY DESTINATION ${QT_INSTALL_PLUGINS}/designer
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/designerplugins.cpp	Wed Jun 22 21:42:10 2022 +0300
@@ -0,0 +1,95 @@
+#include "designerplugins.h"
+#include "vec3editor.h"
+#include "matrixeditor.h"
+
+LDForgeWidgetCollection::LDForgeWidgetCollection(QObject* parent) :
+	QObject{parent}
+{
+	this->interfaces.append(new Vec3EditorPlugin{this});
+	this->interfaces.append(new MatrixEditorPlugin{this});
+}
+
+QList<QDesignerCustomWidgetInterface*> LDForgeWidgetCollection::customWidgets() const
+{
+	return this->interfaces;
+}
+
+QString Vec3EditorPlugin::name() const
+{
+	return "Vec3Editor";
+}
+
+QString Vec3EditorPlugin::group() const
+{
+	return "LDForge";
+}
+
+QString Vec3EditorPlugin::toolTip() const
+{
+	return "";
+}
+
+QString Vec3EditorPlugin::whatsThis() const
+{
+	return "";
+}
+
+QString Vec3EditorPlugin::includeFile() const
+{
+	return "vec3editor.h";
+}
+
+QIcon Vec3EditorPlugin::icon() const
+{
+	return {};
+}
+
+bool Vec3EditorPlugin::isContainer() const
+{
+	return false;
+}
+
+QWidget* Vec3EditorPlugin::createWidget(QWidget* parent)
+{
+	return new Vec3Editor{{0, 0, 0}, parent};
+}
+
+QString MatrixEditorPlugin::name() const
+{
+	return "MatrixEditor";
+}
+
+QString MatrixEditorPlugin::group() const
+{
+	return "LDForge";
+}
+
+QString MatrixEditorPlugin::toolTip() const
+{
+	return "";
+}
+
+QString MatrixEditorPlugin::whatsThis() const
+{
+	return "";
+}
+
+QString MatrixEditorPlugin::includeFile() const
+{
+	return "matrixeditor.h";
+}
+
+QIcon MatrixEditorPlugin::icon() const
+{
+	return {};
+}
+
+bool MatrixEditorPlugin::isContainer() const
+{
+	return false;
+}
+
+QWidget* MatrixEditorPlugin::createWidget(QWidget* parent)
+{
+	return new MatrixEditor{parent};
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/designerplugins.h	Wed Jun 22 21:42:10 2022 +0300
@@ -0,0 +1,49 @@
+#pragma once
+#include <QObject>
+#include <QDesignerCustomWidgetInterface>
+
+class LDForgeWidgetCollection final : public QObject, public QDesignerCustomWidgetCollectionInterface
+{
+	Q_OBJECT
+	Q_PLUGIN_METADATA(IID "org.qgis.customwidgets")
+	Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
+	// QDesignerCustomWidgetCollectionInterface interface
+	QList<QDesignerCustomWidgetInterface*> interfaces;
+public:
+	LDForgeWidgetCollection(QObject* parent = nullptr);
+	QList<QDesignerCustomWidgetInterface*> customWidgets() const override;
+};
+
+class Vec3EditorPlugin final : public QObject, public QDesignerCustomWidgetInterface
+{
+	Q_OBJECT
+	Q_INTERFACES(QDesignerCustomWidgetInterface)
+	// QDesignerCustomWidgetInterface interface
+public:
+	Vec3EditorPlugin(QObject* parent) : QObject{parent}{}
+	QString name() const override;
+	QString group() const override;
+	QString toolTip() const override;
+	QString whatsThis() const override;
+	QString includeFile() const override;
+	QIcon icon() const override;
+	bool isContainer() const override;
+	QWidget* createWidget(QWidget* parent) override;
+};
+
+class MatrixEditorPlugin final : public QObject, public QDesignerCustomWidgetInterface
+{
+	Q_OBJECT
+	Q_INTERFACES(QDesignerCustomWidgetInterface)
+	// QDesignerCustomWidgetInterface interface
+public:
+	MatrixEditorPlugin(QObject* parent) : QObject{parent}{}
+	QString name() const override;
+	QString group() const override;
+	QString toolTip() const override;
+	QString whatsThis() const override;
+	QString includeFile() const override;
+	QIcon icon() const override;
+	bool isContainer() const override;
+	QWidget* createWidget(QWidget* parent) override;
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/doublespinbox.cpp	Wed Jun 22 21:42:10 2022 +0300
@@ -0,0 +1,58 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2020 Teemu Piippo
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "doublespinbox.h"
+
+/*
+ * Constructs a new double spin box. The locale is fixed to system default "C".
+ */
+DoubleSpinBox::DoubleSpinBox(QWidget* parent) :
+	QDoubleSpinBox {parent}
+{
+	this->setLocale({"C"});
+	this->setRange(-1e6, 1e6);
+	this->setDecimals(4);
+}
+
+/*
+ * Reimplementation of QDoubleSpinBox::textFromValue to remove trailing zeros.
+ */
+QString DoubleSpinBox::textFromValue(double value) const
+{
+	QString result = QDoubleSpinBox::textFromValue(value);
+	if (result.contains("."))
+	{
+		// Remove trailing zeros
+		while (result.endsWith("0"))
+			result.chop(1);
+		// Remove trailing decimal point if we just removed all the zeros.
+		if (result.endsWith("."))
+			result.chop(1);
+	}
+	return result;
+}
+
+/*
+ * Reimplementation of QDoubleSpinBox::validate to fix the decimal point if the locale-specific
+ * decimal point was used.
+ */
+QValidator::State DoubleSpinBox::validate(QString& input, int& pos) const
+{
+	input.replace(QLocale().decimalPoint(), this->locale().decimalPoint());
+	return QDoubleSpinBox::validate(input, pos);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/doublespinbox.h	Wed Jun 22 21:42:10 2022 +0300
@@ -0,0 +1,34 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2020 Teemu Piippo
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+#include <QDoubleSpinBox>
+
+/*
+ * A version of QDoubleSpinBox that consistently uses "." as the decimal separator
+ * and does not display trailing zeros.
+ */
+class DoubleSpinBox : public QDoubleSpinBox
+{
+public:
+	DoubleSpinBox(QWidget* parent = nullptr);
+protected:
+	QString textFromValue(double value) const override;
+	QValidator::State validate(QString& input, int& pos) const override;
+};
+
--- a/widgets/matrixeditor.h	Wed Jun 22 20:27:53 2022 +0300
+++ b/widgets/matrixeditor.h	Wed Jun 22 21:42:10 2022 +0300
@@ -2,10 +2,6 @@
 #include <QWidget>
 #include <glm/glm.hpp>
 
-namespace Ui {
-class MatrixEditor;
-}
-
 class MatrixEditor : public QWidget
 {
 	Q_OBJECT
@@ -21,7 +17,7 @@
 	constexpr int matrixSize() const;
 	Q_SLOT void multiplyButtonPressed();
 	class QDoubleSpinBox* spinboxes[4][3];
-	Ui::MatrixEditor *ui;
+	class Ui_MatrixEditor *ui;
 };
 
 constexpr int MatrixEditor::matrixSize() const
--- a/widgets/matrixeditor.ui	Wed Jun 22 20:27:53 2022 +0300
+++ b/widgets/matrixeditor.ui	Wed Jun 22 21:42:10 2022 +0300
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>356</width>
-    <height>172</height>
+    <width>376</width>
+    <height>215</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -115,7 +115,7 @@
   <customwidget>
    <class>DoubleSpinBox</class>
    <extends>QDoubleSpinBox</extends>
-   <header>widgets/doublespinbox.h</header>
+   <header>doublespinbox.h</header>
   </customwidget>
  </customwidgets>
  <resources/>
--- a/widgets/multiplyfactordialog.ui	Wed Jun 22 20:27:53 2022 +0300
+++ b/widgets/multiplyfactordialog.ui	Wed Jun 22 21:42:10 2022 +0300
@@ -62,7 +62,7 @@
   <customwidget>
    <class>DoubleSpinBox</class>
    <extends>QDoubleSpinBox</extends>
-   <header>widgets/doublespinbox.h</header>
+   <header>doublespinbox.h</header>
   </customwidget>
  </customwidgets>
  <resources/>
--- a/widgets/vec3editor.ui	Wed Jun 22 20:27:53 2022 +0300
+++ b/widgets/vec3editor.ui	Wed Jun 22 21:42:10 2022 +0300
@@ -75,7 +75,7 @@
   <customwidget>
    <class>DoubleSpinBox</class>
    <extends>QDoubleSpinBox</extends>
-   <header>widgets/doublespinbox.h</header>
+   <header>doublespinbox.h</header>
   </customwidget>
  </customwidgets>
  <resources/>

mercurial