Salome HOME
updated copyright message
[modules/gui.git] / tools / PyEditor / src / PyEditor_Widget.cxx
1 // Copyright (C) 2015-2023  OPEN CASCADE
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 // File   : PyEditor_Widget.cxx
20 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
21 //
22
23 #include "PyEditor_Editor.h"
24 #include "PyEditor_FindTool.h"
25 #include "PyEditor_Widget.h"
26
27 #include <QVBoxLayout>
28
29 /*!
30   \class PyEditor_Widget
31   \brief Wraps Python editor with the find/replace functionality to a single widget.
32 */
33
34 /*!
35   \brief Constructor.
36   \param parent Parent widget.
37 */
38 PyEditor_Widget::PyEditor_Widget( QWidget* parent ) : QWidget( parent )
39 {
40   // Create editor.
41   myEditor = new PyEditor_Editor( this );
42
43   // Create find tool.
44   myFindTool = new PyEditor_FindTool( myEditor, this );
45
46   // Set-up layout
47   QVBoxLayout* layout = new QVBoxLayout( this );
48   layout->setContentsMargins( 0, 0, 0, 0 );
49   layout->setSpacing( 3 );
50   layout->addWidget( myEditor );
51   layout->addWidget( myFindTool );
52
53   connect( myEditor, SIGNAL( modificationChanged( bool ) ),
54            this, SIGNAL( modificationChanged( bool ) ) );
55   connect( myEditor, SIGNAL( undoAvailable( bool ) ),
56            this, SIGNAL( undoAvailable( bool ) ) );
57   connect( myEditor, SIGNAL( redoAvailable( bool ) ),
58            this, SIGNAL( redoAvailable( bool ) ) );
59   connect( myEditor, SIGNAL( copyAvailable( bool ) ),
60            this, SIGNAL( copyAvailable( bool ) ) );
61
62   connect( myEditor, SIGNAL( selectionChanged() ),
63            this, SIGNAL( selectionChanged() ) );
64   connect( myEditor, SIGNAL( textChanged() ),
65            this, SIGNAL( textChanged() ) );
66   connect( myEditor, SIGNAL( cursorPositionChanged() ),
67            this, SIGNAL( cursorPositionChanged() ) );
68
69   setFocusProxy( myEditor );
70 }
71
72 /*!
73   \brief Get editor.
74   \return Pointer to editor.
75 */
76 PyEditor_Editor* PyEditor_Widget::editor()
77 {
78   return myEditor;
79 }
80
81 /*!
82   \brief Get find tool.
83   \return Pointer to find tool.
84 */
85 PyEditor_FindTool* PyEditor_Widget::findTool()
86 {
87   return myFindTool;
88 }
89
90 /*!
91   \brief Get all custom keywords from editor.
92   \return List of keywords.
93 */
94 QStringList PyEditor_Widget::keywords() const
95 {
96   return myEditor->keywords();
97 }
98
99 /*!
100   \brief Set custom keywords to editor.
101   \param keywords List of keywords.
102   \param type Type of keywords (group id).
103   \param color Color of keywords.
104 */
105 void PyEditor_Widget::appendKeywords( const QStringList& keywords, int type, const QColor& color )
106 {
107   myEditor->appendKeywords( keywords, type, color );
108 }
109
110 /*!
111   \brief Remove given custom keywords from editor.
112   \param keywords List of keywords to remove.
113 */
114 void PyEditor_Widget::removeKeywords( const QStringList& keywords )
115 {
116   myEditor->removeKeywords( keywords );
117 }
118
119 /*!
120   \brief Get current editor's completion policy.
121   \return Completion policy (see PyEditor_Editor::CompletionPolicy).
122 */
123 int PyEditor_Widget::completionPolicy() const
124 {
125   return (int) myEditor->completionPolicy();
126 }
127
128 /*!
129   \brief Set editor's completion policy.
130   \param policy Completion policy (see PyEditor_Editor::CompletionPolicy).
131 */
132 void PyEditor_Widget::setCompletionPolicy( int policy )
133 {
134   myEditor->setCompletionPolicy( (PyEditor_Editor::CompletionPolicy) policy );
135 }
136
137 /*!
138   \brief Activate Find dialog.
139 */
140 void PyEditor_Widget::find()
141 {
142   myFindTool->activateFind();
143 }
144
145 /*!
146   \brief Activate Replace dialog.
147 */
148 void PyEditor_Widget::replace()
149 {
150   myFindTool->activateReplace();
151 }
152
153 /*!
154   \brief Undo last editor's operation.
155 */
156 void PyEditor_Widget::undo()
157 {
158   myEditor->undo();
159 }
160
161 /*!
162   \brief Redo last undone editor's operation.
163 */
164 void PyEditor_Widget::redo()
165 {
166   myEditor->redo();
167 }
168
169 /*!
170   \brief Cut text selected in editor and put it into clipboard.
171 */
172 void PyEditor_Widget::cut()
173 {
174   myEditor->cut();
175 }
176
177 /*!
178   \brief Copy text selected in editor into clipboard.
179 */
180 void PyEditor_Widget::copy()
181 {
182   myEditor->copy();
183 }
184
185 /*!
186   \brief Paste text from clipboard into editor.
187 */
188 void PyEditor_Widget::paste()
189 {
190   myEditor->paste();
191 }
192
193 /*!
194   \brief Delete text selected in editor.
195 */
196 void PyEditor_Widget::deleteSelected()
197 {
198   myEditor->deleteSelected();
199 }
200
201 /*!
202   \brief Select all text in editor.
203 */
204 void PyEditor_Widget::selectAll()
205 {
206   myEditor->selectAll();
207 }
208
209 /*!
210   \brief Clear content of editor.
211 */
212 void PyEditor_Widget::clear()
213 {
214   myEditor->clear();
215 }
216
217 /*!
218   \brief Set/clear modified flag of editor.
219   \param on 'Modified' flag's value.
220 */
221 void PyEditor_Widget::setModified( bool on )
222 {
223   myEditor->document()->setModified( on );
224 }
225
226 /*!
227   \brief Get modified flag of editor.
228   \return 'Modified' flag's value.
229 */
230 bool PyEditor_Widget::isModified()
231 {
232   return myEditor->document()->isModified();
233 }
234
235 /*!
236   \brief Set text to editor.
237   \param text Text to be put into editor.
238 */
239 void PyEditor_Widget::setText( const QString& text )
240 {
241   myEditor->setPlainText( text );
242 }
243
244 /*!
245   \brief Get text from editor.
246   \return Current editor contents.
247 */
248 QString PyEditor_Widget::text() const
249 {
250   return myEditor->toPlainText();
251 }
252
253 /*!
254   \brief Set editor's settings.
255   \param settings Settings object.
256 */
257 void PyEditor_Widget::setSettings( const PyEditor_Settings& settings )
258 {
259   myEditor->setSettings( settings );
260 }
261
262 /*!
263   \brief Get editor's settings.
264   \return Settings object.
265 */
266 const PyEditor_Settings& PyEditor_Widget::settings() const
267 {
268   return myEditor->settings();
269 }
270
271 /*!
272   \brief Move editor's cursor to the given line.
273   \note Line count starts from 1.
274   \param line Line number.
275 */
276 void PyEditor_Widget::setCurrentLine( int line )
277 {
278   myEditor->setCurrentLine( line );
279 }