BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <QtGui/QLineEdit>
28 #include <QtGui/QComboBox>
29 #include <QtGui/QTabWidget>
30 #include <QtGui/QSyntaxHighlighter>
31 #include <QtGui/QValidator>
32 #include <QtCore/QStringList>
33 
34 #include <QtCore/QThread>
35 
36 // currently doesnt work right
37 #undef BALL_QT_HAS_THREADS
38 
39 namespace BALL
40 {
41  namespace VIEW
42  {
43 
44 class PythonSettings;
45 class Preferences;
46 
47 class RunPythonThread;
48 
53  : public QSyntaxHighlighter
54 {
55  public:
56 
58 
60 
61  void compilePattern();
62  void highlightBlock(const QString& text);
63 
64  QStringList python_keywords;
65  QStringList BALL_keywords;
66  QTextCharFormat my_class_format;
67  QTextCharFormat python_format;
68  QTextCharFormat string_format;
69  QTextCharFormat comment_format;
70  vector<QRegExp> python_patterns;
71  vector<QRegExp> BALL_patterns;
72  QRegExp string_pattern;
73  QRegExp comment_pattern;
74 };
75 
80  : public QValidator
81 {
82  public:
83  PythonValidator(QObject* parent = 0);
84  virtual ~PythonValidator();
85 
86  virtual State validate(QString& input, int& pos) const;
87 };
88 
90  : public QThread
91 {
92  public:
94 
96  virtual void run();
97 
98  bool state;
101 };
102 
105 {
106 
107  public:
108 
109  static Hotkey createHotkey(String modifier, String key, String command,
110  bool& ok, String comment = "");
111 
113  bool operator == (const Hotkey& hotkey) const;
114 
116  bool operator == (const QKeyEvent& e) const;
117 
119  const Hotkey& operator = (const Hotkey& s);
120 
122  bool operator < (const Hotkey& key) { return this < &key;}
123 
125  bool set(const String& data);
126 
128  void get(String& data) const;
129 
132  Qt::KeyboardModifiers button_state;
133  Qt::Key key;
134 };
135 
143  : public DockWidget
144 {
145  class MyLineEdit
146  : public QLineEdit
147  {
148  public:
149  MyLineEdit(QWidget* parent)
150  : QLineEdit(parent) {};
151 
152  void setPyWidget(PyWidget* pw) { pw_ = pw;}
153 
154  protected:
155 
156  virtual void keyPressEvent(QKeyEvent* event);
157 
158  PyWidget* pw_;
159  };
160 
161  class MyTextEdit
162  : public TextEditorWidget
163  {
164  public:
165  MyTextEdit(QWidget* parent)
166  : TextEditorWidget(parent) {};
167 
168  void setPyWidget(PyWidget* pw) { pw_ = pw;}
169 
170  protected:
171 
172  virtual void keyPressEvent(QKeyEvent* event);
173 
174  PyWidget* pw_;
175  };
176 
177  friend class MyLineEdit;
178 
179  Q_OBJECT
180 
181  public:
182 
184 
185 
188 
195  PyWidget(QWidget* parent = 0, const char* name = 0);
196 
198  PyWidget(const PyWidget& p);
199 
201  ~PyWidget();
202 
204  bool isValid() const
205  { return valid_;}
206 
210 
213  virtual void initializeWidget(MainControl& main_control);
214 
217  virtual void finalizeWidget(MainControl& main_control);
218 
220  void initializePreferencesTab(Preferences &preferences);
221 
223  void finalizePreferencesTab(Preferences &preferences);
224 
226  virtual void applyPreferences();
227 
229  virtual void scriptDialog(bool run = false);
230 
232  bool toAbortScript();
233 
238  void map(String modifier, String key, String command, String comment = "");
239 
241  void unmap(String modifier, String key);
242 
244  void insertHotkey(const Hotkey& hotkey);
245 
247  void removeHotkey(const Hotkey& hotkey);
248 
250  void reactTo(const QKeyEvent& e);
251 
257  virtual bool canHandle(const String& fileformat) const;
258 
263  virtual bool openFile(const String& filename);
264 
268  bool openFile(const String& filename, bool run, bool is_current = false);
269 
270  //
271  bool runString(String command);
272 
273  //
274  String getCurrentLine() const;
275 
276  //
277  void dump(std::ostream& s, Size depth) const;
278 
279  //
280  void showClassDocu(String classname, String member);
281 
282  //
283  virtual void fetchPreferences(INIFile& inifile);
284 
285  //
286  virtual void writePreferences(INIFile& inifile);
287 
288  //
289  void checkMenu(MainControl& main_control);
290 
292  QString getCurrentScript();
293 
295  bool isInDirectMode() const;
296 
297  public slots:
298 
299  //
300  void showEditContextMenu(const QPoint& point);
301 
302  //
303  void showContextMenu(const QPoint& point);
304 
305  //
306  void showCompletion();
307 
308  //
309  void clear();
310 
311  //
312  void exportHistory();
313 
314  //
315  void abortScript();
316 
317  // Show the docu for the current line
318  void showDocumentation();
319 
321  bool runCurrentScript();
322 
324  void loadScript();
325 
327  void execScript();
328 
330  void saveScript();
331 
333  void clearScript();
334 
335  virtual void hotkeyItem();
336 
337  virtual void modifyHotkeys();
338 
339  void appendText(const String& text, bool output = false, bool state_message = false);
340 
341  bool getMembers(const String& classname, QStringList& sl, const String& prefix);
342 
343  protected slots:
344 
345  virtual bool returnPressed();
346 
347  virtual bool completionSelected_();
348 
349  virtual void showHelp_();
350 
351  virtual void printCursorPosition_();
352 
353  virtual void createScript_();
354 
355  virtual void clearHistory_();
356 
357  protected:
358 
359  bool getClassAndMember_(String toc);
360 
361  void setError_(bool state);
362 
368  virtual void startInterpreter();
369 
370  virtual void paste();
371 
373  bool parseLine_(String line);
374 
375  void appendToHistory_(const String& line);
376 
382  void newPrompt_();
383 
384  bool testMultilineStart_(const String& line);
385 
386  //_
387  const char* getPrompt_() const;
388 
389  //_
390  bool storeScript_();
391 
392  //_ Wrapper for multi and single threading call
393  String runCommand_(const String& command, bool& state);
394 
395  void appendText_(TextEditorWidget* te, String text);
396 
403  void retrieveHistoryLine_(Position index);
404 
405  virtual void contentsDragEnterEvent(QDragEnterEvent* e);
406 
407  virtual void contentsDropEvent(QDropEvent* e);
408 
409  bool keyPressed(QKeyEvent* e);
410 
411  void createMenuHelpEntry_(QMenu* menu, TextEditorWidget* text_edit, const QPoint& point);
412 
413  void findError_(String result);
414 
415  String getCurrentWord_(QTextCursor& text_cursor);
416 
417  QComboBox* getCompletionBox_();
418 
419  std::list<Hotkey>::iterator findKey_(Hotkey& hotkey);
420 
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
QTextCharFormat my_class_format
Definition: pyWidget.h:66
QTextCharFormat string_format
Definition: pyWidget.h:68
String multi_line_text_
Definition: pyWidget.h:438
vector< String > history_
Definition: pyWidget.h:439
MyLineEdit * line_edit_
Definition: pyWidget.h:426
TextEditorWidget * text_edit_
Definition: pyWidget.h:421
RunPythonThread * thread_
Definition: pyWidget.h:444
PythonValidator validator_
Definition: pyWidget.h:425
Position history_position_
Definition: pyWidget.h:441
QTabWidget * tab_widget_
Definition: pyWidget.h:423
Qt::KeyboardModifiers button_state
Definition: pyWidget.h:132
Position current_line_
Definition: pyWidget.h:435
vector< QRegExp > python_patterns
Definition: pyWidget.h:70
vector< bool > results_
Definition: pyWidget.h:440
#define BALL_VIEW_EXPORT
Definition: COMMON/global.h:52
String startup_script_
Definition: pyWidget.h:442
PythonSettings * python_settings_
Definition: pyWidget.h:443
PythonHighlighter highlighter_2_
Definition: pyWidget.h:424
QTextCharFormat comment_format
Definition: pyWidget.h:69
vector< QRegExp > BALL_patterns
Definition: pyWidget.h:71
#define BALL_EMBEDDABLE(TYPE, BASE)
Definition: embeddable.h:31
MyTextEdit * script_edit_
Definition: pyWidget.h:422
BALL_EXPORT bool operator==(const String &s1, const String &s2)
bool started_startup_script_
Definition: pyWidget.h:432
String current_script_
Definition: pyWidget.h:452
QTextCharFormat python_format
Definition: pyWidget.h:67
std::list< Hotkey > hotkeys_
Definition: pyWidget.h:428
Preferences * preferences_
Definition: pyWidget.h:433
QComboBox * editor_combo_box_
Definition: pyWidget.h:427
BALL_EXPORT bool operator<(const String &s1, const String &s2)