# HG changeset patch # User Teemu Piippo # Date 1655923330 -10800 # Node ID 8b994c917f695d64e3702731516f8ebfcb653320 # Parent da4876bfd822cbd3db6b5eccc38c25240cd8bffa Make LDForge widgets visible in Qt Designer diff -r da4876bfd822 -r 8b994c917f69 CMakeLists.txt --- 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 diff -r da4876bfd822 -r 8b994c917f69 src/widgets/doublespinbox.cpp --- 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 . - */ - -#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); -} diff -r da4876bfd822 -r 8b994c917f69 src/widgets/doublespinbox.h --- 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 . - */ - -#pragma once -#include - -/* - * 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; -}; - diff -r da4876bfd822 -r 8b994c917f69 widgets/CMakeLists.txt --- /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 +) diff -r da4876bfd822 -r 8b994c917f69 widgets/designerplugins.cpp --- /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 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}; +} diff -r da4876bfd822 -r 8b994c917f69 widgets/designerplugins.h --- /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 +#include + +class LDForgeWidgetCollection final : public QObject, public QDesignerCustomWidgetCollectionInterface +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qgis.customwidgets") + Q_INTERFACES(QDesignerCustomWidgetCollectionInterface) + // QDesignerCustomWidgetCollectionInterface interface + QList interfaces; +public: + LDForgeWidgetCollection(QObject* parent = nullptr); + QList 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; +}; diff -r da4876bfd822 -r 8b994c917f69 widgets/doublespinbox.cpp --- /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 . + */ + +#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); +} diff -r da4876bfd822 -r 8b994c917f69 widgets/doublespinbox.h --- /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 . + */ + +#pragma once +#include + +/* + * 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; +}; + diff -r da4876bfd822 -r 8b994c917f69 widgets/matrixeditor.h --- 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 #include -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 diff -r da4876bfd822 -r 8b994c917f69 widgets/matrixeditor.ui --- 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 @@ 0 0 - 356 - 172 + 376 + 215 @@ -115,7 +115,7 @@ DoubleSpinBox QDoubleSpinBox -
widgets/doublespinbox.h
+
doublespinbox.h
diff -r da4876bfd822 -r 8b994c917f69 widgets/multiplyfactordialog.ui --- 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 @@ DoubleSpinBox QDoubleSpinBox -
widgets/doublespinbox.h
+
doublespinbox.h
diff -r da4876bfd822 -r 8b994c917f69 widgets/vec3editor.ui --- 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 @@ DoubleSpinBox QDoubleSpinBox -
widgets/doublespinbox.h
+
doublespinbox.h