Debian Codegraph @

src/Gui/QuantitySpinBox.h

raw
1 /***************************************************************************
2 * Copyright (c) 2014 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef GUI_QUANTITYSPINBOX_H
25 #define GUI_QUANTITYSPINBOX_H
26
27 #include <Gui/MetaTypes.h>
28 #include <Gui/SpinBox.h>
29
30 #ifdef Q_MOC_RUN
31 Q_DECLARE_METATYPE(Base::Quantity)
32 #endif
33
34 namespace Gui
35 {
36
37 class QuantitySpinBoxPrivate;
38 class GuiExport QuantitySpinBox: public QAbstractSpinBox, public ExpressionSpinBox
39 {
40 Q_OBJECT
41
42 Q_PROPERTY(QString unit READ unitText WRITE setUnitText) // clazy:exclude=qproperty-without-notify
43 Q_PROPERTY(double minimum READ minimum WRITE setMinimum) // clazy:exclude=qproperty-without-notify
44 Q_PROPERTY(double maximum READ maximum WRITE setMaximum) // clazy:exclude=qproperty-without-notify
45 Q_PROPERTY(
46 double singleStep READ singleStep WRITE setSingleStep
47 ) // clazy:exclude=qproperty-without-notify
48 Q_PROPERTY(double rawValue READ rawValue WRITE setValue NOTIFY valueChanged)
49 Q_PROPERTY(Base::Quantity value READ value WRITE setValue NOTIFY valueChanged USER true)
50 Q_PROPERTY(
51 QString binding READ boundToName WRITE setBoundToByName
52 ) // clazy:exclude=qproperty-without-notify
53 Q_PROPERTY(QString expression READ expressionText) // clazy:exclude=qproperty-without-notify
54 Q_PROPERTY(
55 bool autoNormalize READ autoNormalize WRITE setAutoNormalize
56 ) // clazy:exclude=qproperty-without-notify
57
58 public:
59 explicit QuantitySpinBox(QWidget* parent = nullptr);
60 ~QuantitySpinBox() override;
61
62 /// Get the current quantity
63 Base::Quantity value() const;
64 /// Get the current quantity without unit
65 double rawValue() const;
66
67 void normalize();
68 bool isNormalized();
69
70 /// Gives the current state of the user input, gives true if it is a valid input with correct
71 /// quantity or returns false if the input is a unparsable string or has a wrong unit.
72 bool hasValidInput() const;
73
74 /** Sets the Unit this widget is working with.
75 * After setting the Unit the widget will only accept
76 * user input with this unit type. Or if the user input
77 * a value without unit, this one will be added to the resulting
78 * Quantity.
79 */
80 Base::Unit unit() const;
81 void setUnit(const Base::Unit& unit);
82 /// Set the unit property
83 void setUnitText(const QString&);
84 /// Get the unit property
85 QString unitText();
86
87 /// Get the value of the singleStep property
88 double singleStep() const;
89 /// Set the value of the singleStep property
90 void setSingleStep(double val);
91
92 /// Gets the value of the minimum property
93 double minimum() const;
94 /// Sets the value of the minimum property
95 void setMinimum(double min);
96
97 /// Gets the value of the maximum property
98 double maximum() const;
99 /// Sets the value of the maximum property
100 void setMaximum(double max);
101
102 /// Gets the number of decimals
103 int decimals() const;
104 /// Sets the number of decimals
105 void setDecimals(int v);
106
107 /// Checks if auto normalization is enabled
108 bool autoNormalize() const;
109 /// Enables or disables automatic normalization on enter
110 void setAutoNormalize(bool normalize);
111
112 /// Sets a specific unit schema to handle quantities.
113 /// The system-wide schema won't be used any more.
114 void setSchema(int s);
115
116 /// Clears the schemaand again use the system-wide schema.
117 void clearSchema();
118
119 /// Gets the path of the bound property
120 QString boundToName() const;
121 /// Sets the path of the bound property
122 void setBoundToByName(const QString& path);
123
124 /// Gets the expression as a string
125 QString expressionText() const;
126 void evaluateExpression();
127
128 /// Set the number portion selected
129 void selectNumber();
130
131 void setRange(double min, double max);
132 void checkRangeInExpression(bool);
133 bool isCheckedRangeInExpresion() const;
134
135 Base::Quantity valueFromText(const QString& text) const;
136 QString textFromValue(const Base::Quantity& val) const;
137 void stepBy(int steps) override;
138 void clear() override;
139 QValidator::State validate(QString& input, int& pos) const override;
140 void fixup(QString& str) const override;
141
142 /// This is a helper function to determine the size this widget requires to fully display the text
143 QSize sizeForText(const QString&) const;
144 QSize sizeHint() const override;
145 QSize minimumSizeHint() const override;
146 bool event(QEvent* event) override;
147
148 void setNumberExpression(App::NumberExpression*) override;
149 void bind(const App::ObjectIdentifier& _path) override;
150 bool apply(const std::string& propName) override;
151 using ExpressionSpinBox::apply;
152
153 public Q_SLOTS:
154 /// Sets the field with a quantity
155 void setValue(const Base::Quantity& val);
156 /// Set a numerical value which gets converted to a quantity with the currently set unit type
157 void setValue(double);
158
159 protected Q_SLOTS:
160 void userInput(const QString& text);
161 void handlePendingEmit(bool updateUnit = true);
162
163 protected:
164 void setExpression(std::shared_ptr<App::Expression> expr) override;
165 void openFormulaDialog() override;
166 void showIcon() override;
167 StepEnabled stepEnabled() const override;
168 void showEvent(QShowEvent* event) override;
169 void hideEvent(QHideEvent* event) override;
170 void closeEvent(QCloseEvent* event) override;
171 void focusInEvent(QFocusEvent* event) override;
172 void focusOutEvent(QFocusEvent* event) override;
173 void keyPressEvent(QKeyEvent* event) override;
174 void resizeEvent(QResizeEvent* event) override;
175 void paintEvent(QPaintEvent* event) override;
176
177 private:
178 void validateInput() override;
179 void updateText(const Base::Quantity&);
180 void updateEdit(const QString& text);
181 void updateFromCache(bool notify, bool updateUnit = true);
182 QString getUserString(const Base::Quantity& val, double& factor, QString& unitString) const;
183 QString getUserString(const Base::Quantity& val) const;
184 QSize sizeHintCalculator(int height) const;
185
186 Q_SIGNALS:
187 /** Gets emitted if the user has entered a VALID input
188 * Valid means the user inputted string obeys all restrictions
189 * like: minimum, maximum and/or the right Unit (if specified).
190 */
191 void valueChanged(const Base::Quantity&); // clazy:exclude=overloaded-signal
192 /** Gets emitted if the user has entered a VALID input
193 * Valid means the user inputted string obeys all restrictions
194 * like: minimum, maximum and/or the right Unit (if specified).
195 */
196 void valueChanged(double); // clazy:exclude=overloaded-signal
197 /**
198 * The new value is passed in \a text with unit.
199 */
200 void textChanged(const QString&);
201 /** Gets emitted if formula dialog is about to be opened (true)
202 * or finished (false).
203 */
204 void showFormulaDialog(bool);
205 /** Gets emitted if user confirms the value with return. This
206 * is very similar to editingFinished() but does not fire on
207 * focus out.
208 */
209 void returnPressed();
210
211 private:
212 QScopedPointer<QuantitySpinBoxPrivate> d_ptr;
213 Q_DISABLE_COPY(QuantitySpinBox)
214 Q_DECLARE_PRIVATE(QuantitySpinBox)
215 };
216
217 } // namespace Gui
218
219 #endif // GUI_QUANTITYSPINBOX_H