Salome HOME
5fad4d76a1592f2e35da06816f45627a4ee4704e
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetExprEditor.h
1 // Copyright (C) 2014-2022  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_WIDGETEXPREDITOR_H_
21 #define MODULEBASE_WIDGETEXPREDITOR_H_
22
23 #include <ModuleBase.h>
24 #include <ModuleBase_ModelWidget.h>
25
26 #include <QList>
27 #include <QLabel>
28 #include <QString>
29 #include <QStringList>
30 #include <QPlainTextEdit>
31
32 class QWidget;
33 class QStringListModel;
34 class QCompleter;
35
36 /**
37 * \ingroup GUI
38 * A multi-line text editor which lets to input formula and provides a list of completion strings
39 */
40 class ExpressionEditor: public QPlainTextEdit
41 {
42   Q_OBJECT
43  public:
44    /// Constructor
45    /// \param theParent a parent widget
46   explicit ExpressionEditor(QWidget* theParent = 0);
47   virtual ~ExpressionEditor();
48
49   /// Set list of completion strings
50   void setCompletionList(QStringList&);
51
52   /// Set a text which will be shown when the listr is empty
53   void setPlaceHolderText( const QString& );
54
55   /// Returns placeholder list
56   QString placeHolderText() const;
57
58  public slots:
59   /// Insert additional string for completion
60   /// \param theCompletion a string to insert
61   /// \param isSingleWord a flag shows that inserted string is single word or not
62   void insertCompletion(const QString& theCompletion, bool isSingleWord = false);
63
64   /// Perform completion
65   void performCompletion();
66
67   /// A slot for processing text changed event
68   void onTextChanged();
69
70 signals:
71   /// The signal about text change in the text editor
72   void valueModified();
73
74   /// The signal about key release on the control, that corresponds to the attribute
75   /// \param theObject a sender of the event
76   /// \param theEvent key release event
77   void keyReleased(QObject* theObject, QKeyEvent* theEvent);
78
79  protected:
80   /// Perform completion by prefix
81   /// \param theCompletionPrefix a prefix for looking for completion
82   void performCompletion(const QString& theCompletionPrefix);
83
84   /// Redefinition of virtual method
85   /// \param theEvent a key press event
86   virtual void keyPressEvent(QKeyEvent* theEvent);
87
88   /// Key events processing
89   /// theEvent a key event
90   bool handledCompletedAndSelected(QKeyEvent* theEvent);
91
92   /// Redefinition of virtual method
93   virtual void paintEvent( QPaintEvent* );
94
95 private:
96   QStringListModel* myCompleterModel;
97   QCompleter* myCompleter;
98   bool myCompletedAndSelected;
99   QString myPlaceHolderText;
100 };
101
102 /**
103 * \ingroup GUI
104 * A Widget which provides an input of an expression
105 */
106 class MODULEBASE_EXPORT ModuleBase_WidgetExprEditor : public ModuleBase_ModelWidget
107 {
108   Q_OBJECT
109  public:
110   /// Constructor
111   /// \param theParent the parent object
112   /// \param theData the widget configuration.
113   /// \param thePlaceHolder a placeholder string
114   ModuleBase_WidgetExprEditor( QWidget* theParent,
115                                const Config_WidgetAPI* theData,
116                                const std::string& thePlaceHolder );
117   virtual ~ModuleBase_WidgetExprEditor();
118
119   /// The methiod called when widget is activated
120   virtual void activateCustom();
121
122   /// Redefinition of virtual method
123   virtual QList<QWidget*> getControls() const;
124
125 protected slots:
126   /// A slot for processing text changed event
127   void onTextChanged();
128
129 protected:
130   /// Returns true if the event is processed.
131   virtual bool processEnter();
132
133   /// Do not initialize value on the widget activation
134   virtual void initializeValueByActivate();
135
136   /// Saves the internal parameters to the given feature
137   /// \return True in success
138   virtual bool storeValueCustom();
139
140   /// Redefinition of virtual method
141   virtual bool restoreValueCustom();
142
143 private:
144    /// A line edit control
145   QLabel* myResultLabel;
146   ExpressionEditor* myEditor;
147 };
148
149 #endif /* MODULEBASE_WIDGETEXPREDITOR_H_ */