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