From ae8898561a424f43bf71833a1710b17d9dafa911 Mon Sep 17 00:00:00 2001 From: jriegel Date: Sat, 9 Nov 2013 07:34:56 +0100 Subject: [PATCH] implementing UnitsCalculator and disable changes in Sketcher --- src/Base/Quantity.cpp | 29 ++++++++++++ src/Base/Quantity.h | 10 ++++ src/Gui/DlgUnitsCalculator.ui | 19 +++++--- src/Gui/DlgUnitsCalculatorImp.cpp | 41 +++++++++++++++++ src/Gui/DlgUnitsCalculatorImp.h | 18 ++++++-- src/Gui/InputField.cpp | 58 +++++++++++++++++++++++- src/Gui/InputField.h | 57 ++++++++++++++++++++++- src/Mod/Sketcher/Gui/EditDatumDialog.cpp | 2 +- src/Mod/Sketcher/Gui/InsertDatum.ui | 2 +- 9 files changed, 221 insertions(+), 15 deletions(-) diff --git a/src/Base/Quantity.cpp b/src/Base/Quantity.cpp index 33d5bdf875..315e2c5157 100644 --- a/src/Base/Quantity.cpp +++ b/src/Base/Quantity.cpp @@ -120,6 +120,35 @@ double Quantity::getUserPrefered(QString &unitString)const return Base::UnitsApi::schemaPrefUnit(_Unit,unitString).getValue() * _Value; } +std::string Quantity::getUserString(void)const +{ + std::stringstream sstream; + sstream << _Value << _Unit.getString(); + //TODO: implementing + return sstream.str(); +} + +/// true if it has a number without a unit +bool Quantity::isDimensionless(void)const +{ + return _Value != DOUBLE_MIN && _Unit.isEmpty(); +} +// true if it has a number and a valid unit +bool Quantity::isQuantity(void)const +{ + return _Value != DOUBLE_MIN && !_Unit.isEmpty(); +} +// true if it has a number with or without a unit +bool Quantity::isValid(void)const +{ + return _Value != DOUBLE_MIN ; +} + +void Quantity::setInvalid(void) +{ + _Value = DOUBLE_MIN ; +} + // === Parser & Scanner stuff =============================================== // include the Scanner and the Parser for the Quantitys diff --git a/src/Base/Quantity.h b/src/Base/Quantity.h index 724357a4da..c64d2f74b1 100644 --- a/src/Base/Quantity.h +++ b/src/Base/Quantity.h @@ -66,6 +66,16 @@ public: double getValue(void) const{return _Value;} void setValue(double val){_Value = val;} + /// true if it has a number without a unit + bool isDimensionless(void)const; + /// true if it has a number and a valid unit + bool isQuantity(void)const; + /// true if it has a number with or without a unit + bool isValid(void)const; + /// sets the quantity invalid + void setInvalid(void); + + protected: double _Value; Unit _Unit; diff --git a/src/Gui/DlgUnitsCalculator.ui b/src/Gui/DlgUnitsCalculator.ui index 723b2a82c9..68c87598b4 100644 --- a/src/Gui/DlgUnitsCalculator.ui +++ b/src/Gui/DlgUnitsCalculator.ui @@ -17,7 +17,7 @@ - + 100 @@ -34,7 +34,7 @@ - + 100 @@ -51,7 +51,7 @@ - + 100 @@ -75,7 +75,7 @@ - + Help @@ -95,14 +95,14 @@ - + Copy - + Close @@ -112,6 +112,13 @@ + + + Gui::InputField + QLineEdit +
Gui/InputField.h
+
+
diff --git a/src/Gui/DlgUnitsCalculatorImp.cpp b/src/Gui/DlgUnitsCalculatorImp.cpp index d6f892ae3d..544b04108c 100644 --- a/src/Gui/DlgUnitsCalculatorImp.cpp +++ b/src/Gui/DlgUnitsCalculatorImp.cpp @@ -44,6 +44,14 @@ DlgUnitsCalculator::DlgUnitsCalculator( QWidget* parent, Qt::WFlags fl ) // create widgets setupUi(this); + connect(this->ValueInput, SIGNAL(valueChanged(Base::Quantity)), this, SLOT(valueChanged(Base::Quantity))); + connect(this->UnitInput, SIGNAL(valueChanged(Base::Quantity)), this, SLOT(unitValueChanged(Base::Quantity))); + + connect(this->pushButton_Help, SIGNAL(pressed()), this, SLOT(help())); + connect(this->pushButton_Close, SIGNAL(pressed()), this, SLOT(accept())); + connect(this->pushButton_Copy, SIGNAL(pressed()), this, SLOT(copy())); + + actUnit.setInvalid(); } /** Destroys the object and frees any allocated resources */ @@ -66,4 +74,37 @@ void DlgUnitsCalculator::reject() delete this; } +void DlgUnitsCalculator::unitValueChanged(const Base::Quantity& unit) +{ + actUnit = unit; + valueChanged(actValue); +} + +void DlgUnitsCalculator::valueChanged(const Base::Quantity& quant) +{ + if(actUnit.isValid()){ + this->ValueOutput->setValue(Base::Quantity(quant.getValue()/actUnit.getValue(),actUnit.getUnit())); + }else{ + this->ValueOutput->setValue(quant); + } + actValue = quant; + +} + +void DlgUnitsCalculator::copy(void) +{ + //TODO: copy the value to the clipboard + QDialog::accept(); + delete this; + +} + +void DlgUnitsCalculator::help(void) +{ + //TODO: call help page Std_UnitsCalculator +} + + + + #include "moc_DlgUnitsCalculatorImp.cpp" diff --git a/src/Gui/DlgUnitsCalculatorImp.h b/src/Gui/DlgUnitsCalculatorImp.h index e74309af26..917890cc1b 100644 --- a/src/Gui/DlgUnitsCalculatorImp.h +++ b/src/Gui/DlgUnitsCalculatorImp.h @@ -30,10 +30,8 @@ namespace Gui { namespace Dialog { /** - * The DlgUnitsCalculator class provides a dialog to activate the MDI window - * of the main window you wish. Since there could be a lot of MDI windows in - * an application you cannot put all of them into the "Windows" popup menu. - * \author Werner Mayer + * The DlgUnitsCalculator provides a unit conversion dialog + * \author Juergen Riegel */ class DlgUnitsCalculator : public QDialog, public Ui_DlgUnitCalculator { @@ -46,6 +44,18 @@ public: protected: void accept(); void reject(); + +protected Q_SLOTS: + void unitValueChanged(const Base::Quantity&); + void valueChanged(const Base::Quantity&); + + void copy(void); + void help(void); + +private: + Base::Quantity actValue; + Base::Quantity actUnit; + }; } // namespace Dialog diff --git a/src/Gui/InputField.cpp b/src/Gui/InputField.cpp index 93b28925d4..157c2ed60a 100644 --- a/src/Gui/InputField.cpp +++ b/src/Gui/InputField.cpp @@ -39,7 +39,7 @@ InputField::InputField ( QWidget * parent ) { this->setContextMenuPolicy(Qt::DefaultContextMenu); - QObject::connect(this, SIGNAL(textEdited (QString)), + QObject::connect(this, SIGNAL(textChanged (QString)), this, SLOT(newInput(QString))); } @@ -67,6 +67,7 @@ void InputField::newInput(const QString & text) QPalette *palette = new QPalette(); palette->setColor(QPalette::Base,QColor(255,200,200)); setPalette(*palette); + parseError(QString::fromAscii(ErrorText.c_str())); return; } QPalette *palette = new QPalette(); @@ -74,6 +75,8 @@ void InputField::newInput(const QString & text) setPalette(*palette); ErrorText = ""; this->setToolTip(QString::fromAscii(ErrorText.c_str())); + // signaling + valueChanged(res); } @@ -121,6 +124,59 @@ QByteArray InputField::paramGrpPath() const return sGroupString.c_str(); } +/// sets the field with a quantity +void InputField::setValue(const Base::Quantity& quant) +{ + actQuantity = quant; + if(!quant.getUnit().isEmpty()) + actUnit = quant.getUnit(); + + setText(QString::fromAscii(quant.getUserString().c_str())); +} + +void InputField::setUnit(const Base::Unit& unit) +{ + actUnit = unit; +} + + + +/// get the value of the singleStep property +double InputField::singleStep(void)const +{ + return 0.0; +} + +/// set the value of the singleStep property +void InputField::setSingleStep(double) +{ + +} + +/// get the value of the maximum property +double InputField::maximum(void)const +{ + return 0.0; +} + +/// set the value of the maximum property +void InputField::setMaximum(double) +{ + +} + +/// get the value of the minimum property +double InputField::minimum(void)const +{ + return 0.0; +} + +/// set the value of the minimum property +void InputField::setMinimum(double) +{ + +} + // -------------------------------------------------------------------- diff --git a/src/Gui/InputField.h b/src/Gui/InputField.h index d698825a00..f0274028f3 100644 --- a/src/Gui/InputField.h +++ b/src/Gui/InputField.h @@ -25,6 +25,7 @@ #define GUI_INPUTFIELD_H #include +#include #include "Widgets.h" #include "Window.h" #include "SpinBox.h" @@ -37,19 +38,48 @@ namespace Gui { * The InputField class * The input field widget handles all around user input of Quantities. Thats * include parsing and checking input. Providing a context menu for common operations - * and managing default and history values. + * and managing default and history values. + * Although its derived from a QLineEdit widget, its supports most of the properties and signals + * of a * \author Jürgen Riegel */ class GuiExport InputField : public QLineEdit { Q_OBJECT - Q_PROPERTY( QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath ) + Q_PROPERTY( QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath ) + Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep ) + Q_PROPERTY(double maximum READ maximum WRITE setMaximum ) + Q_PROPERTY(double minimum READ minimum WRITE setMinimum ) + public: InputField ( QWidget * parent = 0 ); virtual ~InputField(); + /// sets the field with a quantity + void setValue(const Base::Quantity&); + /** sets the Unit this field working with. + * After seting the Unit the field will only acceppt + * user input with this unit type. Or if the user input + * a value without unit, this one will be added to the resulting + * Quantity. + */ + void setUnit(const Base::Unit&); + + /// get the value of the singleStep property + double singleStep(void)const; + /// set the value of the singleStep property + void setSingleStep(double); + /// get the value of the maximum property + double maximum(void)const; + /// set the value of the maximum property + void setMaximum(double); + /// get the value of the minimum property + double minimum(void)const; + /// set the value of the minimum property + void setMinimum(double); + /** @name history and default management */ //@{ /// the param group path where the widget write and read the dafault values @@ -62,6 +92,26 @@ public: std::vector getHistory(void); //@} + +Q_SIGNALS: + /** gets emited if the user has entered a VALID input + * Valid means the user inputed string obays all restrictions + * like: minimum, maximum and/or the right Unit (if specified). + * If you want the unfiltered/unvalidated input use valueChanged(const QString&) + * instead: + */ + void valueChanged(const Base::Quantity&); + /** gets emited if the user has entered a VALID input + * Valid means the user inputed string obays all restrictions + * like: minimum, maximum and/or the right Unit (if specified). + * If you want the unfiltered/unvalidated input use valueChanged(const QString&) + * instead: + */ + void valueChanged(double); + + /// signal for an invalid user input (signals a lot while typing!) + void parseError(const QString& errorText); + protected Q_SLOTS: void newInput(const QString & text); @@ -75,6 +125,9 @@ private: /// handle to the parameter group for defaults and history ParameterGrp::handle _handle; std::string sGroupString; + + Base::Quantity actQuantity; + Base::Unit actUnit; }; diff --git a/src/Mod/Sketcher/Gui/EditDatumDialog.cpp b/src/Mod/Sketcher/Gui/EditDatumDialog.cpp index dfdfa940ef..cae9d5c48a 100644 --- a/src/Mod/Sketcher/Gui/EditDatumDialog.cpp +++ b/src/Mod/Sketcher/Gui/EditDatumDialog.cpp @@ -93,7 +93,7 @@ void EditDatumDialog::exec(bool atCursor) Ui::InsertDatum ui_ins_datum; ui_ins_datum.setupUi(&dlg); - ui_ins_datum.lineEdit->setParamGrpPath("User parameter:History/Sketcher/SetDatum"); + //ui_ins_datum.lineEdit->setParamGrpPath("User parameter:History/Sketcher/SetDatum"); double init_val; if (Constr->Type == Sketcher::Angle || diff --git a/src/Mod/Sketcher/Gui/InsertDatum.ui b/src/Mod/Sketcher/Gui/InsertDatum.ui index 39b96cafe5..d27befec34 100644 --- a/src/Mod/Sketcher/Gui/InsertDatum.ui +++ b/src/Mod/Sketcher/Gui/InsertDatum.ui @@ -27,7 +27,7 @@
- +