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