Salome HOME
a2a4086980645fd47b89535bcce26c736edd48f6
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.h
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef ModuleBase_ParamSpinBox_H
22 #define ModuleBase_ParamSpinBox_H
23
24 #include "ModuleBase.h"
25
26 #include <QAbstractSpinBox>
27 #include <QValidator>
28 #include <QLineEdit>
29
30 class QStringListModel;
31 class QCompleter;
32
33 /**
34 * \ingroup GUI
35 * An extension of a double spin box which let to use parameters and expressions for value definition
36 */
37 class MODULEBASE_EXPORT ModuleBase_ParamSpinBox : public QAbstractSpinBox
38 {
39   Q_OBJECT
40
41   enum State { Invalid = 0, NoVariable, Incompatible, Acceptable };
42
43 public:
44   /*!
45    \brief Constructor.
46
47    Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
48    a step value of 1.0 and a precision of 2 decimal places.
49    The value is initially set to 0.00.
50
51    \param theParent a parent object
52    \param thePrecision a precision of values display
53    */
54   ModuleBase_ParamSpinBox( QWidget* theParent = 0, int thePrecision = 12 );
55
56   /// Set list of completion strings
57   void setCompletionList(QStringList&);
58
59   virtual ~ModuleBase_ParamSpinBox();
60
61   virtual void stepBy(int);
62
63 //  virtual double valueFromText(const QString&) const;
64 //  virtual QString textFromValue (double value) const;
65
66   virtual QValidator::State validate(QString&, int&) const;
67
68   virtual void setValue(double);
69
70   double value() const;
71
72   virtual void setText(const QString&);
73
74   QString text() const { return lineEdit()->text(); }
75
76   /// Set a flag about accepted variable
77   void setAcceptVariables(const bool);
78
79   /// Returns accepted variables flag
80   bool isAcceptVariables() const;
81
82   /// Returns True if the input value contains variable
83   bool hasVariable() const;
84
85   double minimum() const { return myMinimum; }
86   double maximum() const { return myMaximum; }
87
88   void setMinimum(double theMin) { myMinimum = theMin; myValidator->setBottom(theMin); }
89   void setMaximum(double theMax) { myMaximum = theMax; myValidator->setTop(theMax); }
90
91   int decimals() const { return myValidator->decimals(); }
92   void setDecimals(int thePrecision) { myValidator->setDecimals(thePrecision); }
93
94   double singleStep() const { return mySingleStep; }
95   void setSingleStep(double theStep) { mySingleStep = theStep; }
96
97   void setValueEnabled(bool theEnable);
98
99 protected:
100   /*!
101   \brief This function is called when the spinbox receives key release event.
102   */
103   virtual void keyReleaseEvent(QKeyEvent *event);
104
105   virtual bool eventFilter(QObject* theObj, QEvent* theEvent);
106
107   /// The virtual function is reimplemented in order to avoid extra increasing of value by StepBy
108   /// method
109   virtual void timerEvent(QTimerEvent *event) {}
110
111   virtual StepEnabled stepEnabled() const { return StepUpEnabled | StepDownEnabled; }
112
113    /// Returns True if the given text contains variable
114    /// \param theText a text string
115   bool hasVariable(const QString& theText) const;
116
117 //  /// Returns state of the control
118 //  State isValid(const QString&, double&) const;
119 //
120 //  /// Returns True if the given value is within min and max of the control
121 //  bool checkRange(const double) const;
122 //
123 //  /// Finds a variable by its name. Returns true in success
124 //  /// \param theName a name of variable
125 //  /// \param outValue an output value of the variable
126 //  bool findVariable(const QString& theName, double& outValue) const;
127
128 signals:
129   void textChanged(const QString&);
130
131 // protected:
132 //  virtual void showEvent(QShowEvent*);
133 //
134 // protected slots:
135 //   /// A slot called on text change
136 //  void onTextChanged(const QString&);
137 //
138 // private:
139 //  void connectSignalsAndSlots();
140
141 private slots:
142   void insertCompletion(const QString&);
143
144   void onTextChanged(const QString&);
145
146 private:
147   QString getPrefix(int& theStart, int& theEnd) const;
148   void showCompletion(bool checkPrefix);
149
150   bool myIsEquation;
151   bool myAcceptVariables;
152
153   QStringListModel* myCompleterModel;
154   QCompleter* myCompleter;
155   int myPrecision;
156
157   double myMinimum;
158   double myMaximum;
159
160   int myCompletePos;
161
162   double mySingleStep;
163
164   /// Cashed color of active base palette
165   QColor myEnabledBaseColor;
166
167   QDoubleValidator* myValidator;
168 };
169
170 #endif