]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetExprEditor.h
Salome HOME
eb2ff9aa1e1d861f68b164aa0e3efc207e30d452
[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   void editingFinished();
68   void valueModified();
69   /// A signal that is emitted by the "Tab" key event. It is emitted before the key is processed.
70   void valueStored();
71   void focusNextPrev();
72
73   /// The signal about key release on the control, that corresponds to the attribute
74   /// \param theEvent key release event
75   void keyReleased(QKeyEvent* theEvent);
76
77  protected:
78   /// Perform completion by prefix
79   /// \param theCompletionPrefix a prefix for looking for completion
80   void performCompletion(const QString& theCompletionPrefix);
81
82   /// Redefinition of virtual method
83   /// \param theEvent a key press event
84   virtual void keyPressEvent(QKeyEvent* theEvent);
85
86   /// Key events processing
87   /// theEvent a key event
88   bool handledCompletedAndSelected(QKeyEvent* theEvent);
89
90   /// Redefinition of virtual method
91   virtual void paintEvent( QPaintEvent* );
92
93   /// The parent method that processes the "Tab"/"SHIF + Tab" keyboard events
94   /// Emits a signal about focus change
95   /// If theIsNext is true, this function searches forward, if next is false, it searches backward.
96   virtual bool focusNextPrevChild(bool theIsNext);
97
98  private:
99   QStringListModel* myCompleterModel;
100   QCompleter* myCompleter;
101   bool myCompletedAndSelected;
102   QString myPlaceHolderText;
103
104   /// Boolean value whether the spin box content is modified
105   bool myIsModified;
106 };
107
108 /**
109 * \ingroup GUI
110 * A Widget which provides an input of an expression
111 */
112 class MODULEBASE_EXPORT ModuleBase_WidgetExprEditor : public ModuleBase_ModelWidget
113 {
114   Q_OBJECT
115  public:
116   /// Constructor
117   /// \param theParent the parent object
118   /// \param theData the widget configuration.
119   /// \param theParentId is Id of a parent of the current attribute
120   /// \param thePlaceHolder a placeholder string
121   ModuleBase_WidgetExprEditor( QWidget* theParent,
122                                const Config_WidgetAPI* theData,
123                                const std::string& theParentId,
124                                const std::string& thePlaceHolder );
125   virtual ~ModuleBase_WidgetExprEditor();
126
127   /// Redefinition of virtual method
128   virtual QList<QWidget*> getControls() const;
129
130   /// Returns true if the event is processed.
131   virtual bool processEnter();
132
133 protected slots:
134   /// A slot for processing text changed event
135   void onTextChanged();
136
137 protected:
138   /// Saves the internal parameters to the given feature
139   /// \return True in success
140   virtual bool storeValueCustom() const;
141
142   /// Redefinition of virtual method
143   virtual bool restoreValueCustom();
144
145 private:
146    /// A line edit control
147   QLabel* myResultLabel;
148   ExpressionEditor* myEditor;
149 };
150
151 #endif /* MODULEBASE_WIDGETEXPREDITOR_H_ */