Tulip  4.2.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 <QtCore/QDateTime>
45 #include <QtGui/QListWidget>
46 #include <QtGui/QPlainTextEdit>
47 #include <QtGui/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 keyPressEvent(QKeyEvent *e);
74  void showEvent(QShowEvent * event);
75  void hideEvent(QHideEvent * event);
76  bool eventFilter(QObject *obj, QEvent * event);
77 
78 };
79 
80 class TLP_PYTHON_SCOPE FindReplaceDialog : public QDialog {
81 
82  Q_OBJECT
83 
84  Ui::FindReplaceDialogData *_ui;
85  QPlainTextEdit *_editor;
86  QString _lastSearch;
87  bool _resetSearch;
88 
89  void setSearchResult(const bool result);
90 
91 public :
92 
93  FindReplaceDialog(QPlainTextEdit *_editor, QWidget *parent=NULL);
94  ~FindReplaceDialog();
95 
96  void setFindMode(const bool findMode);
97 
98  void setTextToFind(const QString &text);
99 
100 
101 public slots:
102 
103  void textToFindChanged();
104  bool doFind();
105  bool doReplace();
106  void doReplaceFind();
107  void doReplaceAll();
108  void setResetSearch() {
109  _resetSearch = true;
110  }
111  void regexpToggled(bool toggled);
112 
113 
114 protected:
115 
116  void hideEvent(QHideEvent * event);
117 
118 };
119 
120 class TLP_PYTHON_SCOPE PythonCodeEditor : public QPlainTextEdit {
121 
122  Q_OBJECT
123 
124  friend class LineNumberArea;
125  friend class AutoCompletionList;
126 
127 public :
128 
129  explicit PythonCodeEditor(QWidget *parent=0);
130  ~PythonCodeEditor();
131 
132  QString getCleanCode() const;
133 
134  int lineNumberAreaWidth() const;
135 
136  void indicateScriptCurrentError(int lineNumber);
137  void clearErrorIndicator();
138 
139  void zoomIn();
140  void zoomOut();
141 
142  void getCursorPosition(int &line, int &col) const;
143  void setCursorPosition(int line, int col);
144  void scrollToLine(int line);
145 
146  void getSelection(int &lineFrom, int &indexFrom, int &lineTo, int &indexTo) const;
147  void setSelection(int startLine, int startCol, int endLine, int endCol);
148  void removeSelectedText();
149  bool hasSelectedText() const;
150  QString selectedText() const;
151 
152  int lines() const;
153  int lineLength(int lineNumber) const;
154 
155  void insertAt(QString text, int line, int col);
156 
157  void commentSelectedCode();
158  void uncommentSelectedCode();
159 
160  void indentSelectedCode();
161  void unindentSelectedCode();
162 
163  void setAutoIndentation(const bool autoIndent) {
164  this->_autoIndent = autoIndent;
165  }
166 
167  bool autoIndentation() const {
168  return _autoIndent;
169  }
170 
171  void setIndentationGuides(const bool indentGuides) {
172  this->_indentGuides = indentGuides;
173  }
174 
175  bool indentationGuides() const {
176  return _indentGuides;
177  }
178 
179  void setHighlightEditedLine(const bool highlightCurLine) {
180  this->_highlightCurLine = highlightCurLine;
181  }
182 
183  bool highlightEditedLine() const {
184  return _highlightCurLine;
185  }
186 
187  void setFindReplaceActivated(const bool activateFindReplace) {
188  _findReplaceActivate = activateFindReplace;
189  }
190 
191  bool findReplaceActivated() const {
192  return _findReplaceActivate;
193  }
194 
195  void setCommentShortcutsActivated(const bool activateCommentShortcuts) {
196  _commentShortcutsActivate = activateCommentShortcuts;
197  }
198 
199  bool commentShortcutsActivated() const {
200  return _commentShortcutsActivate;
201  }
202 
203  void setIndentShortcutsActivated(const bool activateIndentShortcuts) {
204  _indentShortcutsActivate = activateIndentShortcuts;
205  }
206 
207  bool indentShortcutsActivated() const {
208  return _indentShortcutsActivate;
209  }
210 
211  void setFileName(const QString &fileName) {
212  _pythonFileName = fileName;
213  }
214 
215  QString getFileName() const {
216  return _pythonFileName;
217  }
218 
219  bool loadCodeFromFile(const QString &filePath);
220 
221  bool saveCodeToFile();
222 
223  QDateTime getLastSavedTime() const {
224  return _lastSavedTime;
225  }
226 
227  void setModuleEditor(const bool moduleEditor) {
228  this->_moduleEditor = moduleEditor;
229  }
230 
231  void analyseScriptCode(const bool wholeText=false);
232 
233  AutoCompletionDataBase *getAutoCompletionDb() const {
234  return _autoCompletionDb;
235  }
236 
237 protected:
238 
239  void resizeEvent(QResizeEvent *event);
240  void showEvent(QShowEvent *);
241  void paintEvent(QPaintEvent *event);
242  void keyPressEvent (QKeyEvent * e);
243  void wheelEvent(QWheelEvent * event);
244  void mouseDoubleClickEvent(QMouseEvent * event);
245  void mouseMoveEvent(QMouseEvent * event);
246  void mousePressEvent(QMouseEvent * event);
247  void mouseReleaseEvent(QMouseEvent * event);
248  void lineNumberAreaPaintEvent(QPaintEvent *event);
249  void insertFromMimeData(const QMimeData * source);
250 
251 
252 protected slots:
253 
254  void updateLineNumberAreaWidth();
255  void updateLineNumberArea(const QRect &, int);
256  void resetExtraSelections();
257  void matchParens();
258  virtual void highlightCurrentLine();
259  void highlightErrors();
260  virtual void showAutoCompletionList(bool dotContext=false);
261  virtual void updateAutoCompletionList(bool dotContext=false);
262  void highlightSelection();
263 
264 protected:
265 
266  virtual void updateAutoCompletionListPosition();
267 
268  void createParenSelection(int pos);
269  void updateTabStopWidth();
270 
271  QString getEditedFunctionName() const;
272 
273  void showTooltip(int line, int col, const QString &text);
274  void hideTooltip();
275  bool isTooltipActive() const;
276 
277  QFontMetrics fontMetrics() const;
278 
279  QWidget *_lineNumberArea;
280  PythonCodeHighlighter *_highlighter;
281  ParenMatcherHighlighter *_parenHighlighter;
282  QFont _currentFont;
283  QVector<int> _currentErrorLines;
284 
285  AutoCompletionList *_autoCompletionList;
286  AutoCompletionDataBase *_autoCompletionDb;
287 
288  FindReplaceDialog *_findReplaceDialog;
289 
290  bool _autoIndent;
291  bool _indentGuides;
292  bool _highlightCurLine;
293  bool _tooltipActive;
294  bool _findReplaceActivate;
295  bool _commentShortcutsActivate;
296  bool _indentShortcutsActivate;
297 
298  QPoint _toolTipPos;
299  QString _toolTipText;
300  QString _toolTipFunc;
301 
302  QString _pythonFileName;
303  QDateTime _lastSavedTime;
304 
305  bool _shellWidget;
306  bool _moduleEditor;
307 
308 };
309 
310 }
311 
312 #endif /* PYTHONCODEEDITOR2_H_ */