BALL  1.5.0
pyWidget.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_VIEW_WIDGETS_PYWIDGET_H
6 #define BALL_VIEW_WIDGETS_PYWIDGET_H
7 
8 #ifndef BALL_COMMON_H
9 # include <BALL/common.h>
10 #endif
11 
12 #ifndef BALL_DATATYPE_STRING_H
13 # include <BALL/DATATYPE/string.h>
14 #endif
15 
16 #ifndef BALL_VIEW_WIDGETS_DOCKWIDGET_H
18 #endif
19 
20 #ifndef BALL_VIEW_WIDGETS_TEXTEDITORWIDGET_H
22 #endif
23 
24 #include <QtGui/QDragEnterEvent>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QDropEvent>
27 #include <QtWidgets/QLineEdit>
28 #include <QtWidgets/QComboBox>
29 #include <QtWidgets/QTabWidget>
30 #include <QtGui/QSyntaxHighlighter>
31 #include <QtGui/QValidator>
32 #include <QtCore/QStringList>
33 
34 #include <QtCore/QThread>
35 
36 namespace BALL
37 {
38  namespace VIEW
39  {
40 
41 class PythonSettings;
42 class Preferences;
43 
44 class RunPythonThread;
45 
50  : public QSyntaxHighlighter
51 {
52  public:
53 
55 
57 
58  void compilePattern();
59  void highlightBlock(const QString& text);
60 
61  QStringList python_keywords;
62  QStringList BALL_keywords;
63  QTextCharFormat my_class_format;
64  QTextCharFormat python_format;
65  QTextCharFormat string_format;
66  QTextCharFormat comment_format;
67  vector<QRegExp> python_patterns;
68  vector<QRegExp> BALL_patterns;
69  QRegExp string_pattern;
70  QRegExp comment_pattern;
71 };
72 
77  : public QValidator
78 {
79  public:
80  PythonValidator(QObject* parent = 0);
81  virtual ~PythonValidator();
82 
83  virtual State validate(QString& input, int& pos) const;
84 };
85 
87  : public QThread
88 {
89  public:
91 
93  virtual void run();
94 
95  bool state;
98 };
99 
102 {
103 
104  public:
105 
106  static Hotkey createHotkey(String modifier, String key, String command,
107  bool& ok, String comment = "");
108 
110  bool operator == (const Hotkey& hotkey) const;
111 
113  bool operator == (const QKeyEvent& e) const;
114 
116  const Hotkey& operator = (const Hotkey& s);
117 
119  bool operator < (const Hotkey& key) { return this < &key;}
120 
122  bool set(const String& data);
123 
125  void get(String& data) const;
126 
129  Qt::KeyboardModifiers button_state;
130  Qt::Key key;
131 };
132 
140  : public DockWidget
141 {
142  class MyLineEdit
143  : public QLineEdit
144  {
145  public:
146  MyLineEdit(QWidget* parent)
147  : QLineEdit(parent) {};
148 
149  void setPyWidget(PyWidget* pw) { pw_ = pw;}
150 
151  protected:
152 
153  virtual void keyPressEvent(QKeyEvent* event);
154 
155  PyWidget* pw_;
156  };
157 
158  class MyTextEdit
159  : public TextEditorWidget
160  {
161  public:
162  MyTextEdit(QWidget* parent)
163  : TextEditorWidget(parent) {};
164 
165  void setPyWidget(PyWidget* pw) { pw_ = pw;}
166 
167  protected:
168 
169  virtual void keyPressEvent(QKeyEvent* event);
170 
171  PyWidget* pw_;
172  };
173 
174  friend class MyLineEdit;
175 
176  Q_OBJECT
177 
178  public:
179 
181 
182 
185 
192  PyWidget(QWidget* parent = 0, const char* name = 0);
193 
195  PyWidget(const PyWidget& p);
196 
198  ~PyWidget();
199 
201  bool isValid() const
202  { return valid_;}
203 
207 
210  virtual void initializeWidget(MainControl& main_control);
211 
214  virtual void finalizeWidget(MainControl& main_control);
215 
217  void initializePreferencesTab(Preferences &preferences);
218 
220  void finalizePreferencesTab(Preferences &preferences);
221 
223  virtual void applyPreferences();
224 
226  virtual void scriptDialog(bool run = false);
227 
229  bool toAbortScript();
230 
235  void map(String modifier, String key, String command, String comment = "");
236 
238  void unmap(String modifier, String key);
239 
241  void insertHotkey(const Hotkey& hotkey);
242 
244  void removeHotkey(const Hotkey& hotkey);
245 
247  void reactTo(const QKeyEvent& e);
248 
254  virtual bool canHandle(const String& fileformat) const;
255 
260  virtual bool openFile(const String& filename);
261 
265  bool openFile(const String& filename, bool run, bool is_current = false);
266 
267  //
268  bool runString(String command);
269 
270  //
271  String getCurrentLine() const;
272 
273  //
274  void dump(std::ostream& s, Size depth) const;
275 
277  BALL_DEPRECATED void showClassDocu(String /* classname */, String /* member */);
278 
279  //
280  virtual void fetchPreferences(INIFile& inifile);
281 
282  //
283  virtual void writePreferences(INIFile& inifile);
284 
285  //
286  void checkMenu(MainControl& main_control);
287 
289  QString getCurrentScript();
290 
292  bool isInDirectMode() const;
293 
294  public Q_SLOTS:
295 
296  //
297  void showEditContextMenu(const QPoint& point);
298 
299  //
300  void showContextMenu(const QPoint& point);
301 
302  //
303  void showCompletion();
304 
305  //
306  void clear();
307 
308  //
309  void exportHistory();
310 
311  //
312  void abortScript();
313 
315  BALL_DEPRECATED void showDocumentation();
316 
318  bool runCurrentScript();
319 
321  void loadScript();
322 
324  void execScript();
325 
327  void saveScript();
328 
330  void clearScript();
331 
332  virtual void hotkeyItem();
333 
334  virtual void modifyHotkeys();
335 
336  void appendText(const String& text, bool output = false, bool state_message = false);
337 
338  bool getMembers(const String& classname, QStringList& sl, const String& prefix);
339 
340  protected Q_SLOTS:
341 
342  virtual bool returnPressed();
343 
344  virtual bool completionSelected_();
345 
347  BALL_DEPRECATED virtual void showHelp_();
348 
349  virtual void printCursorPosition_();
350 
351  virtual void createScript_();
352 
353  virtual void clearHistory_();
354 
355  protected:
356 
357  bool getClassAndMember_(String toc);
358 
359  void setError_(bool state);
360 
366  virtual void startInterpreter();
367 
368  virtual void paste();
369 
371  bool parseLine_(String line);
372 
373  void appendToHistory_(const String& line);
374 
380  void newPrompt_();
381 
382  bool testMultilineStart_(const String& line);
383 
384  //_
385  const char* getPrompt_() const;
386 
387  //_
388  bool storeScript_();
389 
390  //_ Wrapper for multi and single threading call
391  String runCommand_(const String& command, bool& state);
392 
393  void appendText_(TextEditorWidget* te, String text);
394 
401  void retrieveHistoryLine_(Position index);
402 
403  virtual void contentsDragEnterEvent(QDragEnterEvent* e);
404 
405  virtual void contentsDropEvent(QDropEvent* e);
406 
407  bool keyPressed(QKeyEvent* e);
408 
410  BALL_DEPRECATED void createMenuHelpEntry_(QMenu* /* menu */, TextEditorWidget* /* text_edit */, const QPoint& /* point */);
411 
412  void findError_(String result);
413 
414  String getCurrentWord_(QTextCursor& text_cursor);
415 
416  QComboBox* getCompletionBox_();
417 
418  std::list<Hotkey>::iterator findKey_(Hotkey& hotkey);
419 
421  TextEditorWidget* text_edit_, *script_output_;
422  MyTextEdit* script_edit_;
423  QTabWidget* tab_widget_;
426  MyLineEdit* line_edit_;
427  QComboBox* combo_box_, *editor_combo_box_;
428  std::list<Hotkey> hotkeys_;
429  // we use an own working dir to find Python Scripts
431  bool valid_;
434 
439  vector<String> history_;
440  vector<bool> results_;
447  String class_, member_;
449  bool running_;
450  bool silent_, full_silent_;
453 };
454 
455  } // namespaces
456 } // namespaces
457 
458 #endif // BALL_VIEW_WIDGETS_PYWIDGET_H
#define BALL_EMBEDDABLE(TYPE, BASE)
Definition: embeddable.h:31
std::list< Hotkey > hotkeys_
Definition: pyWidget.h:428
MyLineEdit * line_edit_
Definition: pyWidget.h:426
vector< QRegExp > BALL_patterns
Definition: pyWidget.h:68
#define BALL_VIEW_EXPORT
Definition: COMMON/global.h:52
QLabel * curr_modifying_
Definition: pyWidget.h:420
Position history_position_
Definition: pyWidget.h:441
QTextCharFormat comment_format
Definition: pyWidget.h:66
QComboBox * editor_combo_box_
Definition: pyWidget.h:427
TextEditorWidget * text_edit_
Definition: pyWidget.h:421
QTextCharFormat python_format
Definition: pyWidget.h:64
QTabWidget * tab_widget_
Definition: pyWidget.h:423
vector< String > history_
Definition: pyWidget.h:439
QTextCharFormat string_format
Definition: pyWidget.h:65
RunPythonThread * thread_
Definition: pyWidget.h:444
QTextCharFormat my_class_format
Definition: pyWidget.h:63
Preferences * preferences_
Definition: pyWidget.h:433
String multi_line_text_
Definition: pyWidget.h:438
BALL_EXPORT bool operator<(const String &s1, const String &s2)
Qt::KeyboardModifiers button_state
Definition: pyWidget.h:129
String startup_script_
Definition: pyWidget.h:442
PythonValidator validator_
Definition: pyWidget.h:425
Definition: constants.h:12
PythonHighlighter highlighter_2_
Definition: pyWidget.h:424
#define BALL_DEPRECATED
Definition: COMMON/global.h:64
vector< bool > results_
Definition: pyWidget.h:440
vector< QRegExp > python_patterns
Definition: pyWidget.h:67
PythonSettings * python_settings_
Definition: pyWidget.h:443
String current_script_
Definition: pyWidget.h:452
BALL_EXPORT bool operator==(const String &s1, const String &s2)
Position current_line_
Definition: pyWidget.h:435
bool started_startup_script_
Definition: pyWidget.h:432
bool isValid() const
Is full Python support available?
Definition: pyWidget.h:201
MyTextEdit * script_edit_
Definition: pyWidget.h:422