Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
PythonCodeEditor.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 /**
21  *
22  * This file is part of Tulip (www.tulip-software.org)
23  *
24  * Authors: David Auber and the Tulip development Team
25  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
26  *
27  * Tulip is free software; you can redistribute it and/or modify
28  * it under the terms of the GNU Lesser General Public License
29  * as published by the Free Software Foundation, either version 3
30  * of the License, or (at your option) any later version.
31  *
32  * Tulip is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35  * See the GNU General Public License for more details.
36  *
37  */
38 
39 #ifndef PYTHONCODEEDITOR2_H_
40 #define PYTHONCODEEDITOR2_H_
41 
42 #include <tulip/AutoCompletionDataBase.h>
43 
44 #include <QDateTime>
45 #include <QListWidget>
46 #include <QPlainTextEdit>
47 #include <QDialog>
48 
49 namespace Ui {
50 class FindReplaceDialogData;
51 }
52 
53 class PythonCodeHighlighter;
54 class ParenMatcherHighlighter;
55 
56 namespace tlp {
57 
58 class PythonCodeEditor;
59 class LineNumberArea;
60 
61 class TLP_PYTHON_SCOPE AutoCompletionList : public QListWidget {
62 
63  tlp::PythonCodeEditor *_codeEditor;
64  bool _activated;
65  bool _wasActivated;
66 
67 public :
68 
69  explicit AutoCompletionList(tlp::PythonCodeEditor *parent=0);
70 
71 protected :
72 
73  void insertSelectedItem();
74  void keyPressEvent(QKeyEvent *e);
75  void showEvent(QShowEvent * event);
76  void hideEvent(QHideEvent * event);
77  void mouseDoubleClickEvent(QMouseEvent * event);
78  bool eventFilter(QObject *obj, QEvent * event);
79 
80 };
81 
82 class TLP_PYTHON_SCOPE FindReplaceDialog : public QDialog {
83 
84  Q_OBJECT
85 
86  Ui::FindReplaceDialogData *_ui;
87  QPlainTextEdit *_editor;
88  QString _lastSearch;
89  bool _resetSearch;
90 
91  void setSearchResult(const bool result);
92 
93 public :
94 
95  FindReplaceDialog(QPlainTextEdit *_editor, QWidget *parent=NULL);
96  ~FindReplaceDialog();
97 
98  void setFindMode(const bool findMode);
99 
100  void setTextToFind(const QString &text);
101 
102 
103 public slots:
104 
105  void textToFindChanged();
106  bool doFind();
107  bool doReplace();
108  void doReplaceFind();
109  void doReplaceAll();
110  void setResetSearch() {
111  _resetSearch = true;
112  }
113  void regexpToggled(bool toggled);
114 
115 
116 protected:
117 
118  void hideEvent(QHideEvent * event);
119 
120 };
121 
122 class TLP_PYTHON_SCOPE PythonCodeEditor : public QPlainTextEdit {
123 
124  Q_OBJECT
125 
126  friend class LineNumberArea;
127  friend class AutoCompletionList;
128 
129 public :
130 
131  explicit PythonCodeEditor(QWidget *parent=0);
132  ~PythonCodeEditor();
133 
134  QString getCleanCode() const;
135 
136  int lineNumberAreaWidth() const;
137 
138  void indicateScriptCurrentError(int lineNumber);
139  void clearErrorIndicator();
140 
141  void zoomIn();
142  void zoomOut();
143 
144  void getCursorPosition(int &line, int &col) const;
145  void setCursorPosition(int line, int col);
146  void scrollToLine(int line);
147 
148  void getSelection(int &lineFrom, int &indexFrom, int &lineTo, int &indexTo) const;
149  void setSelection(int startLine, int startCol, int endLine, int endCol);
150  void removeSelectedText();
151  bool hasSelectedText() const;
152  QString selectedText() const;
153 
154  int lines() const;
155  int lineLength(int lineNumber) const;
156 
157  void insertAt(QString text, int line, int col);
158 
159  void commentSelectedCode();
160  void uncommentSelectedCode();
161 
162  void indentSelectedCode();
163  void unindentSelectedCode();
164 
165  void setAutoIndentation(const bool autoIndent) {
166  this->_autoIndent = autoIndent;
167  }
168 
169  bool autoIndentation() const {
170  return _autoIndent;
171  }
172 
173  void setIndentationGuides(const bool indentGuides) {
174  this->_indentGuides = indentGuides;
175  }
176 
177  bool indentationGuides() const {
178  return _indentGuides;
179  }
180 
181  void setHighlightEditedLine(const bool highlightCurLine) {
182  this->_highlightCurLine = highlightCurLine;
183  }
184 
185  bool highlightEditedLine() const {
186  return _highlightCurLine;
187  }
188 
189  void setFindReplaceActivated(const bool activateFindReplace) {
190  _findReplaceActivate = activateFindReplace;
191  }
192 
193  bool findReplaceActivated() const {
194  return _findReplaceActivate;
195  }
196 
197  void setCommentShortcutsActivated(const bool activateCommentShortcuts) {
198  _commentShortcutsActivate = activateCommentShortcuts;
199  }
200 
201  bool commentShortcutsActivated() const {
202  return _commentShortcutsActivate;
203  }
204 
205  void setIndentShortcutsActivated(const bool activateIndentShortcuts) {
206  _indentShortcutsActivate = activateIndentShortcuts;
207  }
208 
209  bool indentShortcutsActivated() const {
210  return _indentShortcutsActivate;
211  }
212 
213  void setFileName(const QString &fileName) {
214  _pythonFileName = fileName;
215  }
216 
217  QString getFileName() const {
218  return _pythonFileName;
219  }
220 
221  bool loadCodeFromFile(const QString &filePath);
222 
223  bool saveCodeToFile();
224 
225  QDateTime getLastSavedTime() const {
226  return _lastSavedTime;
227  }
228 
229  void setModuleEditor(const bool moduleEditor) {
230  this->_moduleEditor = moduleEditor;
231  }
232 
233  void analyseScriptCode(const bool wholeText=false);
234 
235  AutoCompletionDataBase *getAutoCompletionDb() const {
236  return _autoCompletionDb;
237  }
238 
239 protected:
240 
241  void resizeEvent(QResizeEvent *event);
242  void showEvent(QShowEvent *);
243  void paintEvent(QPaintEvent *event);
244  void keyPressEvent (QKeyEvent * e);
245  void wheelEvent(QWheelEvent * event);
246  void mouseDoubleClickEvent(QMouseEvent * event);
247  void mouseMoveEvent(QMouseEvent * event);
248  void mousePressEvent(QMouseEvent * event);
249  void mouseReleaseEvent(QMouseEvent * event);
250  void lineNumberAreaPaintEvent(QPaintEvent *event);
251  void insertFromMimeData(const QMimeData * source);
252 
253 
254 protected slots:
255 
256  void updateLineNumberAreaWidth();
257  void updateLineNumberArea(const QRect &, int);
258  void resetExtraSelections();
259  void matchParens();
260  virtual void highlightCurrentLine();
261  void highlightErrors();
262  virtual void showAutoCompletionList(bool dotContext=false);
263  virtual void updateAutoCompletionList(bool dotContext=false);
264  void highlightSelection();
265 
266 protected:
267 
268  virtual void updateAutoCompletionListPosition();
269 
270  void createParenSelection(int pos);
271  void updateTabStopWidth();
272 
273  QString getEditedFunctionName() const;
274 
275  void showTooltip(int line, int col, const QString &text);
276  void hideTooltip();
277  bool isTooltipActive() const;
278 
279  QFontMetrics fontMetrics() const;
280 
281  QWidget *_lineNumberArea;
282  PythonCodeHighlighter *_highlighter;
283  ParenMatcherHighlighter *_parenHighlighter;
284  QFont _currentFont;
285  QVector<int> _currentErrorLines;
286 
287  AutoCompletionList *_autoCompletionList;
288  AutoCompletionDataBase *_autoCompletionDb;
289 
290  FindReplaceDialog *_findReplaceDialog;
291 
292  bool _autoIndent;
293  bool _indentGuides;
294  bool _highlightCurLine;
295  bool _tooltipActive;
296  bool _findReplaceActivate;
297  bool _commentShortcutsActivate;
298  bool _indentShortcutsActivate;
299 
300  QPoint _toolTipPos;
301  QString _toolTipText;
302  QString _toolTipFunc;
303 
304  QString _pythonFileName;
305  QDateTime _lastSavedTime;
306 
307  bool _shellWidget;
308  bool _moduleEditor;
309 
310 };
311 
312 }
313 
314 #endif /* PYTHONCODEEDITOR2_H_ */