Wed, 18 Mar 2020 17:11:23 +0200
added a matrix editing widget
--- a/CMakeLists.txt Wed Mar 18 15:54:30 2020 +0200 +++ b/CMakeLists.txt Wed Mar 18 17:11:23 2020 +0200 @@ -59,6 +59,7 @@ src/ui/polygonobjecteditor.cpp src/widgets/colorbutton.cpp src/widgets/doublespinbox.cpp + src/widgets/matrixeditor.cpp src/widgets/vec3editor.cpp ) set (LDFORGE_HEADERS @@ -106,6 +107,7 @@ src/ui/polygonobjecteditor.h src/widgets/colorbutton.h src/widgets/doublespinbox.h + src/widgets/matrixeditor.h src/widgets/vec3editor.h ) set (LDFORGE_FORMS @@ -114,6 +116,7 @@ src/settingseditor/librarieseditor.ui src/settingseditor/settingseditor.ui src/ui/multiplyfactordialog.ui + src/widgets/matrixeditor.ui src/widgets/vec3editor.ui )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/matrixeditor.cpp Wed Mar 18 17:11:23 2020 +0200 @@ -0,0 +1,89 @@ +#include "main.h" +#include "matrixeditor.h" +#include "ui_matrixeditor.h" +#include "../ui/multiplyfactordialog.h" + +constexpr char BUTTON_COLUMN_PROPERTY[] = "_ldforge_column"; + +MatrixEditor::MatrixEditor(const glm::mat4 value, QWidget* parent) : + QWidget(parent), + ui(new Ui::MatrixEditor) +{ + ui->setupUi(this); + for (int column = 0; column < countof(this->spinboxes); column += 1) + { + for (int row = 0; row < countof(this->spinboxes[0]); row += 1) + { + const QString name = "cell"_q + QString::number(column) + QString::number(row); + QDoubleSpinBox** spinbox = &this->spinboxes[column][row]; + *spinbox = this->findChild<QDoubleSpinBox*>(name); + connect(*spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [&]() + { + emit this->valueChanged(this->value()); + }); + Q_ASSERT(*spinbox != nullptr); + } + QAbstractButton* button = this->findChild<QAbstractButton*>("multiply"_q + QString::number(column)); + button->setProperty(BUTTON_COLUMN_PROPERTY, column); + connect(button, &QAbstractButton::clicked, this, &MatrixEditor::multiplyButtonPressed); + } + this->setValue(value); +} + +MatrixEditor::MatrixEditor(QWidget *parent) : + MatrixEditor{glm::mat4{1}, parent} +{ +} + +MatrixEditor::~MatrixEditor() +{ + delete ui; +} + +glm::mat4 MatrixEditor::value() const +{ + glm::mat4 result{1}; + for (int column = 0; column < countof(this->spinboxes); column += 1) + { + for (int row = 0; row < countof(this->spinboxes[0]); row += 1) + { + result[column][row] = this->spinboxes[column][row]->value(); + } + } + return result; +} + +void MatrixEditor::setValue(const glm::mat4& value) +{ + for (int column = 0; column < countof(this->spinboxes); column += 1) + { + for (int row = 0; row < countof(this->spinboxes[0]); row += 1) + { + QDoubleSpinBox* spinbox = this->spinboxes[column][row]; + QSignalBlocker blocker{spinbox}; + spinbox->setValue(value[column][row]); + } + } +} + +void MatrixEditor::multiplyButtonPressed() +{ + QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender()); + if (button != nullptr) + { + bool ok; + const int column = button->property(BUTTON_COLUMN_PROPERTY).toInt(&ok); + if (ok and column >= 0 and column < this->matrixSize()) + { + glm::mat4 newValue = this->value(); + MultiplyFactorDialog dialog{newValue[column], this}; + const int result = dialog.exec(); + if (result == QDialog::Accepted) + { + newValue[column] = glm::vec4{dialog.value(), (column == 3) ? 1 : 0}; + this->setValue(newValue); + emit valueChanged(newValue); + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/matrixeditor.h Wed Mar 18 17:11:23 2020 +0200 @@ -0,0 +1,30 @@ +#pragma once +#include <QWidget> +#include "main.h" + +namespace Ui { +class MatrixEditor; +} + +class MatrixEditor : public QWidget +{ + Q_OBJECT +public: + explicit MatrixEditor(QWidget *parent = nullptr); + explicit MatrixEditor(const glm::mat4 value, QWidget* parent = nullptr); + ~MatrixEditor(); + glm::mat4 value() const; + void setValue(const glm::mat4& value); +Q_SIGNALS: + void valueChanged(const glm::mat4& value); +private: + constexpr int matrixSize() const; + Q_SLOT void multiplyButtonPressed(); + class QDoubleSpinBox* spinboxes[4][3]; + Ui::MatrixEditor *ui; +}; + +constexpr int MatrixEditor::matrixSize() const +{ + return 4; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/matrixeditor.ui Wed Mar 18 17:11:23 2020 +0200 @@ -0,0 +1,123 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MatrixEditor</class> + <widget class="QWidget" name="MatrixEditor"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>356</width> + <height>172</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="DoubleSpinBox" name="cell00"/> + </item> + <item row="0" column="1"> + <widget class="DoubleSpinBox" name="cell10"/> + </item> + <item row="0" column="2"> + <widget class="DoubleSpinBox" name="cell20"/> + </item> + <item row="0" column="3"> + <widget class="DoubleSpinBox" name="cell30"/> + </item> + <item row="1" column="0"> + <widget class="DoubleSpinBox" name="cell01"/> + </item> + <item row="1" column="1"> + <widget class="DoubleSpinBox" name="cell11"/> + </item> + <item row="1" column="2"> + <widget class="DoubleSpinBox" name="cell21"/> + </item> + <item row="1" column="3"> + <widget class="DoubleSpinBox" name="cell31"/> + </item> + <item row="2" column="0"> + <widget class="DoubleSpinBox" name="cell02"/> + </item> + <item row="2" column="1"> + <widget class="DoubleSpinBox" name="cell12"/> + </item> + <item row="2" column="2"> + <widget class="DoubleSpinBox" name="cell22"/> + </item> + <item row="2" column="3"> + <widget class="DoubleSpinBox" name="cell32"/> + </item> + <item row="3" column="0"> + <widget class="DoubleSpinBox" name="doubleSpinBox"> + <property name="enabled"> + <bool>false</bool> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="DoubleSpinBox" name="doubleSpinBox_2"> + <property name="enabled"> + <bool>false</bool> + </property> + </widget> + </item> + <item row="3" column="2"> + <widget class="DoubleSpinBox" name="doubleSpinBox_3"> + <property name="enabled"> + <bool>false</bool> + </property> + </widget> + </item> + <item row="3" column="3"> + <widget class="DoubleSpinBox" name="doubleSpinBox_4"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="value"> + <double>1.000000000000000</double> + </property> + </widget> + </item> + <item row="4" column="0"> + <widget class="QPushButton" name="multiply0"> + <property name="text"> + <string>×</string> + </property> + </widget> + </item> + <item row="4" column="1"> + <widget class="QPushButton" name="multiply1"> + <property name="text"> + <string>×</string> + </property> + </widget> + </item> + <item row="4" column="2"> + <widget class="QPushButton" name="multiply2"> + <property name="text"> + <string>×</string> + </property> + </widget> + </item> + <item row="4" column="3"> + <widget class="QPushButton" name="multiply3"> + <property name="text"> + <string>×</string> + </property> + </widget> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>DoubleSpinBox</class> + <extends>QDoubleSpinBox</extends> + <header>widgets/doublespinbox.h</header> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui>