From d521b8d0b2ed55af04a66649dc8d733623fa92f9 Mon Sep 17 00:00:00 2001 From: Sven Goldberg Date: Tue, 4 Nov 2025 09:08:39 +0100 Subject: [PATCH] Replace QDoubleSpinBox by QLineEdit --- .../src/TIGLCreatorAddSpotlightDialog.cpp | 20 ++++++-- .../src/TIGLCreatorAddSpotlightDialog.h | 10 ++-- TIGLCreator/src/TIGLCreatorApp.cpp | 2 + TIGLCreator/src/TIGLDoubleLineEdit.cpp | 45 ++++++++++++++++++ TIGLCreator/src/TIGLDoubleLineEdit.h | 38 +++++++++++++++ TIGLCreator/src/TIGLDoubleValidator.cpp | 46 +++++++++++++++++++ TIGLCreator/src/TIGLDoubleValidator.h | 37 +++++++++++++++ 7 files changed, 188 insertions(+), 10 deletions(-) create mode 100644 TIGLCreator/src/TIGLDoubleLineEdit.cpp create mode 100644 TIGLCreator/src/TIGLDoubleLineEdit.h create mode 100644 TIGLCreator/src/TIGLDoubleValidator.cpp create mode 100644 TIGLCreator/src/TIGLDoubleValidator.h diff --git a/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.cpp b/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.cpp index b97bef2143..3fd443287a 100644 --- a/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.cpp +++ b/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include TIGLCreatorAddSpotlightDialog::TIGLCreatorAddSpotlightDialog(QWidget *parent) @@ -34,7 +33,7 @@ TIGLCreatorAddSpotlightDialog::TIGLCreatorAddSpotlightDialog(QWidget *parent) , dx(new QDoubleSpinBox()) , dy(new QDoubleSpinBox()) , dz(new QDoubleSpinBox()) - , concentration(new QDoubleSpinBox()) + , concentration(new TIGLDoubleLineEdit()) { setWindowTitle("Add a spotlight"); @@ -69,7 +68,8 @@ TIGLCreatorAddSpotlightDialog::TIGLCreatorAddSpotlightDialog(QWidget *parent) dx->setValue(1.); dy->setValue(1.); dz->setValue(1.); - concentration->setValue(0.5); + + concentration->setValue(0.5, 2); // The main layout is vertical with a horizontal layout on top // and the dialog buttons below @@ -149,5 +149,15 @@ tigl::CTiglPoint TIGLCreatorAddSpotlightDialog::getDirection() const double TIGLCreatorAddSpotlightDialog::getConcentration() const { - return concentration->value(); -} \ No newline at end of file + double value = concentration->text().toDouble(); + + // Probably not needed anymore + // Intention of this part is to round the user input to some defined decimal places + /*if(auto *validator = qobject_cast(concentration->validator())) { + int maxDecimals = validator->decimals(); + + double factorRounding = std::pow(10., maxDecimals); + value = std::round(value * factorRounding) / factorRounding; + }*/ + return value; +} diff --git a/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.h b/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.h index d3f5d40e01..db141d6b35 100644 --- a/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.h +++ b/TIGLCreator/src/TIGLCreatorAddSpotlightDialog.h @@ -21,9 +21,9 @@ #include #include - - -class QDoubleSpinBox; +#include +#include +#include class TIGLCreatorAddSpotlightDialog : public QDialog { @@ -43,7 +43,7 @@ class TIGLCreatorAddSpotlightDialog : public QDialog QDoubleSpinBox* dx; QDoubleSpinBox* dy; QDoubleSpinBox* dz; - QDoubleSpinBox* concentration; + TIGLDoubleLineEdit* concentration; }; -#endif // TIGLCREATORADDSPOTLIGHTDIALOG_H \ No newline at end of file +#endif // TIGLCREATORADDSPOTLIGHTDIALOG_H diff --git a/TIGLCreator/src/TIGLCreatorApp.cpp b/TIGLCreator/src/TIGLCreatorApp.cpp index 869fe4c432..b6dd889ba4 100644 --- a/TIGLCreator/src/TIGLCreatorApp.cpp +++ b/TIGLCreator/src/TIGLCreatorApp.cpp @@ -60,6 +60,8 @@ TIGLCreatorApp::TIGLCreatorApp(int& argc, char* argv[]) setlocale(LC_NUMERIC, "C"); #endif + QLocale::setDefault(QLocale(QLocale::English)); + mainwindow.reset(new TIGLCreatorWindow); for (int iarg = 0; iarg < argc; ++iarg) { diff --git a/TIGLCreator/src/TIGLDoubleLineEdit.cpp b/TIGLCreator/src/TIGLDoubleLineEdit.cpp new file mode 100644 index 0000000000..c334676735 --- /dev/null +++ b/TIGLCreator/src/TIGLDoubleLineEdit.cpp @@ -0,0 +1,45 @@ +/* +* Copyright (C) 2025 German Aerospace Center (DLR/SC) +* +* Created: 2025-11-04 Sven Goldberg +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include + +TIGLDoubleLineEdit::TIGLDoubleLineEdit() + : QLineEdit() +{} + +TIGLDoubleLineEdit::TIGLDoubleLineEdit(double minValue, double maxValue, double value, int nrDecimalsPrint) + : QLineEdit() +{ + this->setRange(minValue, maxValue); + this->setValue(value, nrDecimalsPrint); +} + +void TIGLDoubleLineEdit::setRange(double minValue, double maxValue) +{ + // Do no explicitely forbid a specific number of decimals + // As a value has to be set, we decided to choose a large value here + // For some Qt reasons, the number of decimals places must not be arbitrary large + this->setValidator(new TIGLDoubleValidator(minValue, maxValue, 300., this)); +} + +void TIGLDoubleLineEdit::setValue(double value, int nrDecimalsPrint) +{ + this->setText(QString::number(value, 'f', nrDecimalsPrint)); +} diff --git a/TIGLCreator/src/TIGLDoubleLineEdit.h b/TIGLCreator/src/TIGLDoubleLineEdit.h new file mode 100644 index 0000000000..a9fb80ccb5 --- /dev/null +++ b/TIGLCreator/src/TIGLDoubleLineEdit.h @@ -0,0 +1,38 @@ +/* +* Copyright (C) 2025 German Aerospace Center (DLR/SC) +* +* Created: 2025-11-04 Sven Goldberg +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef TIGLDOUBLELINEEDIT_H +#define TIGLDOUBLELINEEDIT_H + +#include + +class TIGLDoubleLineEdit : public QLineEdit +{ + Q_OBJECT + +public: + TIGLDoubleLineEdit(); + TIGLDoubleLineEdit(double minValue, double maxValue, double value, int nrDecimalsPrint); + + void setRange(double minValue, double maxValue); + + void setValue(double value, int nrDecimalsPrint); + +}; + +#endif // TIGLDOUBLELINEEDIT_H diff --git a/TIGLCreator/src/TIGLDoubleValidator.cpp b/TIGLCreator/src/TIGLDoubleValidator.cpp new file mode 100644 index 0000000000..51c1556b35 --- /dev/null +++ b/TIGLCreator/src/TIGLDoubleValidator.cpp @@ -0,0 +1,46 @@ +/* +* Copyright (C) 2025 German Aerospace Center (DLR/SC) +* +* Created: 2025-11-04 Sven Goldberg +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "TIGLDoubleValidator.h" + +TIGLDoubleValidator::TIGLDoubleValidator(double bottom, double top, int decimals, QObject *parent) + : QDoubleValidator(bottom, top, decimals, parent) +{} + +TIGLDoubleValidator::State TIGLDoubleValidator::validate(QString &input, int &pos) const { + Q_UNUSED(pos); + + // Allow empty input and number with leading "." + if (input.isEmpty() || input == ".") { + return Intermediate; + } + // A leading "-" is only allowed if the range includes negative values + if (bottom() < 0 && input == "-") { + return Intermediate; + } + + bool valueValid = false; + double value = input.toDouble(&valueValid); + + // Input that is not convertible to a C++ double or lies outside the defined range is invalid + if (!valueValid || value < bottom() || value > top()) { + return Invalid; + } + + return Intermediate; +} diff --git a/TIGLCreator/src/TIGLDoubleValidator.h b/TIGLCreator/src/TIGLDoubleValidator.h new file mode 100644 index 0000000000..3ea81a6ea9 --- /dev/null +++ b/TIGLCreator/src/TIGLDoubleValidator.h @@ -0,0 +1,37 @@ +/* +* Copyright (C) 2025 German Aerospace Center (DLR/SC) +* +* Created: 2025-11-04 Sven Goldberg +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef TIGLDOUBLEVALIDATOR_H +#define TIGLDOUBLEVALIDATOR_H + +#include +#include + + +class TIGLDoubleValidator : public QDoubleValidator +{ + Q_OBJECT + +public: + TIGLDoubleValidator(double bottom, double top, int decimals, QObject *parent = nullptr); + + // Defines valid status of String inputs + State validate(QString &input, int &pos) const override; +}; + +#endif // TIGLDOUBLEVALIDATOR_H