Debian Codegraph @

src/Gui/InputField.h

raw
1 /***************************************************************************
2 * Copyright (c) 2013 Jürgen Riegel <[email protected]> *
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_INPUTFIELD_H
25 #define GUI_INPUTFIELD_H
26
27 #include <QValidator>
28 #include <Base/Parameter.h>
29
30 #include "ExpressionBinding.h"
31 #include "ExpressionCompleter.h"
32 #include "MetaTypes.h"
33 #include "Widgets.h"
34
35
36 #ifdef Q_MOC_RUN
37 Q_DECLARE_METATYPE(Base::Quantity)
38 #endif
39
40 namespace App
41 {
42 class DocumentObject;
43 class ObjectIdentifier;
44 class Expression;
45 } // namespace App
46
47 namespace Gui
48 {
49
50
51 /**
52 * The InputField class
53 * The input field widget handles all around user input of Quantities. That
54 * includes parsing and checking input. Providing a context menu for common operations
55 * and managing default and history values.
56 * Although it's derived from a QLineEdit widget, it supports most of the properties and signals
57 * of a spin box.
58 * \author Jürgen Riegel
59 */
60 class GuiExport InputField: public ExpressionLineEdit, public ExpressionWidget
61 {
62 Q_OBJECT
63
64 Q_PROPERTY(
65 QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath
66 ) // clazy:exclude=qproperty-without-notify
67 Q_PROPERTY(
68 double singleStep READ singleStep WRITE setSingleStep
69 ) // clazy:exclude=qproperty-without-notify
70 Q_PROPERTY(double maximum READ maximum WRITE setMaximum) // clazy:exclude=qproperty-without-notify
71 Q_PROPERTY(double minimum READ minimum WRITE setMinimum) // clazy:exclude=qproperty-without-notify
72 Q_PROPERTY(double rawValue READ rawValue WRITE setValue NOTIFY valueChanged)
73 Q_PROPERTY(
74 int historySize READ historySize WRITE setHistorySize
75 ) // clazy:exclude=qproperty-without-notify
76 Q_PROPERTY(QString unit READ getUnitText WRITE setUnitText) // clazy:exclude=qproperty-without-notify
77 Q_PROPERTY(int precision READ getPrecision WRITE setPrecision) // clazy:exclude=qproperty-without-notify
78 Q_PROPERTY(QString format READ getFormat WRITE setFormat) // clazy:exclude=qproperty-without-notify
79 Q_PROPERTY(Base::Quantity quantity READ getQuantity WRITE setValue NOTIFY valueChanged)
80 Q_PROPERTY(
81 QString quantityString READ getQuantityString WRITE setQuantityString
82 ) // clazy:exclude=qproperty-without-notify
83 Q_PROPERTY(QString rawText READ rawText WRITE setRawText) // clazy:exclude=qproperty-without-notify
84
85
86 public:
87 InputField(QWidget* parent = nullptr);
88 ~InputField() override;
89
90 /// set the field with a quantity
91 void setValue(const Base::Quantity&);
92 // set a numerical value which gets converted to a quantity with the currently set unit type
93 void setValue(const double&);
94
95 /// get the current value
96 Base::Quantity getQuantity() const
97 {
98 return this->actQuantity;
99 }
100 /// Get the current quantity without unit
101 double rawValue() const;
102
103 /// get stored, valid quantity as a string (user string - avoid storing)
104 QString getQuantityString() const;
105
106 /// set, validate and display quantity from a string. Must match existing units.
107 void setQuantityString(const QString& text);
108
109 /// return the quantity in C locale, i.e. decimal separator is a dot.
110 QString rawText() const;
111
112 /// expects the string in C locale and internally converts it into the OS-specific locale
113 void setRawText(const QString& text);
114
115 /// gives the current state of the user input, gives true if it is a valid input with correct
116 /// quantity (shown by the green pixmap), returns false if the input is a unparsable string or
117 /// has a wrong unit (shown by the red pixmap in the gui)
118 bool hasValidInput()
119 {
120 return validInput;
121 }
122
123 /** sets the Unit this field is working with.
124 * After setting the Unit the field will only accept
125 * user input with this unit type. Or if the user input
126 * a value without unit, this one will be added to the resulting
127 * Quantity.
128 */
129 void setUnit(const Base::Unit&);
130 const Base::Unit& getUnit() const;
131
132 /// set the input field to the last used value (works only if the setParamGrpPath() was called)
133 void setToLastUsedValue();
134 /// get the value of the singleStep property
135 double singleStep() const;
136 /// set the value of the singleStep property
137 void setSingleStep(double);
138 /// get the value of the maximum property
139 double maximum() const;
140 /// set the value of the maximum property
141 void setMaximum(double);
142 /// get the value of the minimum property
143 double minimum() const;
144 /// set the value of the minimum property
145 void setMinimum(double);
146 /// get the value of the minimum property
147 int historySize() const;
148 /// set the value of the minimum property
149 void setHistorySize(int);
150 /// set the unit by a string (can be used in the *.ui file)
151 void setUnitText(const QString&);
152 /// get the unit as a string (can be used in the *.ui file)
153 QString getUnitText();
154 /// get the value of the precision property
155 int getPrecision() const;
156 /// set the value of the precision property (can be used in the *.ui file)
157 void setPrecision(const int);
158 /// get the value of the format property: "f" (fixed), "e" (scientific), "g" (general)
159 QString getFormat() const;
160 /// set the value of the format property (can be used in the *.ui file)
161 void setFormat(const QString&);
162 /// set the number portion selected (use after setValue())
163 void selectNumber();
164 /// input validation
165 void fixup(QString& input) const;
166 /// input validation
167 QValidator::State validate(QString& input, int& pos) const;
168
169 /** @name history and default management */
170 //@{
171 /// the param group path where the widget writes and reads the default values
172 QByteArray paramGrpPath() const;
173 /// set the param group path where the widget writes and reads the default values
174 void setParamGrpPath(const QByteArray& name);
175 /// push a new value to the history, if no string given the actual text of the input field is used.
176 void pushToHistory(const QString& valueq = QString());
177 /// get the history of the field, newest first
178 std::vector<QString> getHistory();
179 /// push a new value to the history, if no string given the actual text of the input field is used.
180 void pushToSavedValues(const QString& valueq = QString());
181 /// get the history of the field, newest first
182 std::vector<QString> getSavedValues();
183 //@}
184
185 void bind(const App::ObjectIdentifier& _path) override;
186 bool apply(const std::string& propName) override;
187 bool apply() override;
188
189 Q_SIGNALS:
190 /** gets emitted if the user has entered a VALID input
191 * Valid means the user inputted string obeys all restrictions
192 * like: minimum, maximum and/or the right Unit (if specified).
193 * If you want the unfiltered/non-validated input use textChanged(const QString&)
194 * instead:
195 */
196 void valueChanged(const Base::Quantity&); // clazy:exclude=overloaded-signal
197 /** gets emitted if the user has entered a VALID input
198 * Valid means the user inputted string obeys all restrictions
199 * like: minimum, maximum and/or the right Unit (if specified).
200 * If you want the unfiltered/non-validated input use textChanged(const QString&)
201 * instead:
202 */
203 void valueChanged(double); // clazy:exclude=overloaded-signal
204
205 /// signal for an invalid user input (signals a lot while typing!)
206 void parseError(const QString& errorText);
207
208 protected Q_SLOTS:
209 void newInput(const QString& text);
210 void updateIconLabel(const QString& text);
211
212 protected:
213 void showEvent(QShowEvent* event) override;
214 void focusInEvent(QFocusEvent* event) override;
215 void focusOutEvent(QFocusEvent* event) override;
216 void keyPressEvent(QKeyEvent* event) override;
217 void wheelEvent(QWheelEvent* event) override;
218 void contextMenuEvent(QContextMenuEvent* event) override;
219 void resizeEvent(QResizeEvent*) override;
220 int getMargin();
221
222 private:
223 QPixmap getValidationIcon(const char* name, const QSize& size) const;
224 void updateText(const Base::Quantity&);
225
226 private:
227 QByteArray m_sPrefGrp;
228 bool validInput;
229
230 /// handle to the parameter group for defaults and history
231 ParameterGrp::handle _handle;
232 std::string sGroupString;
233
234 Base::Quantity actQuantity;
235 Base::Unit actUnit;
236 double actUnitValue;
237
238 double Maximum;
239 double Minimum;
240 double StepSize;
241 int HistorySize;
242 int SaveSize;
243
244 QPalette defaultPalette;
245 };
246
247 } // namespace Gui
248
249 #endif // GUI_INPUTFIELD_H