Salome HOME
Copyright update 2020
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.h
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef ModuleBase_ParamSpinBox_H
21 #define ModuleBase_ParamSpinBox_H
22
23 #include "ModuleBase.h"
24
25 #include <QAbstractSpinBox>
26 #include <QValidator>
27 #include <QLineEdit>
28
29 class QStringListModel;
30 class QCompleter;
31
32 /**
33 * \ingroup GUI
34 * An extension of a double spin box which let to use parameters and expressions for value definition
35 */
36 class MODULEBASE_EXPORT ModuleBase_ParamSpinBox : public QAbstractSpinBox
37 {
38   Q_OBJECT
39
40   enum State { Invalid = 0, NoVariable, Incompatible, Acceptable };
41
42 public:
43   /*!
44    \brief Constructor.
45
46    Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
47    a step value of 1.0 and a precision of 2 decimal places.
48    The value is initially set to 0.00.
49
50    \param theParent a parent object
51    \param thePrecision a precision of values display
52    */
53   ModuleBase_ParamSpinBox( QWidget* theParent = 0, int thePrecision = 12 );
54
55   /// Set list of completion strings
56   void setCompletionList(QStringList&);
57
58   virtual ~ModuleBase_ParamSpinBox();
59
60   virtual void stepBy(int);
61
62 //  virtual double valueFromText(const QString&) const;
63 //  virtual QString textFromValue (double value) const;
64
65   virtual QValidator::State validate(QString&, int&) const;
66
67   virtual void setValue(double);
68
69   double value() const;
70
71   virtual void setText(const QString&);
72
73   QString text() const { return lineEdit()->text(); }
74
75   /// Set a flag about accepted variable
76   void setAcceptVariables(const bool);
77
78   /// Returns accepted variables flag
79   bool isAcceptVariables() const;
80
81   /// Returns True if the input value contains variable
82   bool hasVariable() const;
83
84   double minimum() const { return myMinimum; }
85   double maximum() const { return myMaximum; }
86
87   void setMinimum(double theMin) { myMinimum = theMin; myValidator->setBottom(theMin); }
88   void setMaximum(double theMax) { myMaximum = theMax; myValidator->setTop(theMax); }
89
90   int decimals() const { return myValidator->decimals(); }
91   void setDecimals(int thePrecision) { myValidator->setDecimals(thePrecision); }
92
93   double singleStep() const { return mySingleStep; }
94   void setSingleStep(double theStep) { mySingleStep = theStep; }
95
96   void setValueEnabled(bool theEnable);
97
98 protected:
99   /*!
100   \brief This function is called when the spinbox receives key release event.
101   */
102   virtual void keyReleaseEvent(QKeyEvent *event);
103
104   virtual bool eventFilter(QObject* theObj, QEvent* theEvent);
105
106   /// The virtual function is reimplemented in order to avoid extra increasing of value by StepBy
107   /// method
108   virtual void timerEvent(QTimerEvent *event) {}
109
110   virtual StepEnabled stepEnabled() const { return StepUpEnabled | StepDownEnabled; }
111
112    /// Returns True if the given text contains variable
113    /// \param theText a text string
114   bool hasVariable(const QString& theText) const;
115
116 //  /// Returns state of the control
117 //  State isValid(const QString&, double&) const;
118 //
119 //  /// Returns True if the given value is within min and max of the control
120 //  bool checkRange(const double) const;
121 //
122 //  /// Finds a variable by its name. Returns true in success
123 //  /// \param theName a name of variable
124 //  /// \param outValue an output value of the variable
125 //  bool findVariable(const QString& theName, double& outValue) const;
126
127 signals:
128   void textChanged(const QString&);
129
130 // protected:
131 //  virtual void showEvent(QShowEvent*);
132 //
133 // protected slots:
134 //   /// A slot called on text change
135 //  void onTextChanged(const QString&);
136 //
137 // private:
138 //  void connectSignalsAndSlots();
139
140 private slots:
141   void insertCompletion(const QString&);
142
143   void onTextChanged(const QString&);
144
145 private:
146   QString getPrefix(int& theStart, int& theEnd) const;
147   void showCompletion(bool checkPrefix);
148
149   bool myIsEquation;
150   bool myAcceptVariables;
151
152   QStringListModel* myCompleterModel;
153   QCompleter* myCompleter;
154
155   double myMinimum;
156   double myMaximum;
157
158   int myCompletePos;
159
160   double mySingleStep;
161
162   /// Cashed color of active base palette
163   QColor myEnabledBaseColor;
164
165   QDoubleValidator* myValidator;
166 };
167
168 #endif