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