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
BALL::VIEW::Hotkey::action
String action
Definition: pyWidget.h:128
BALL::VIEW::PyWidget::line_edit_
MyLineEdit * line_edit_
Definition: pyWidget.h:426
BALL::VIEW::PythonHighlighter::python_patterns
vector< QRegExp > python_patterns
Definition: pyWidget.h:67
BALL::VIEW::PyWidget::member_
String member_
Definition: pyWidget.h:447
BALL::VIEW::PythonHighlighter::comment_pattern
QRegExp comment_pattern
Definition: pyWidget.h:70
BALL::VIEW::PythonValidator
Definition: pyWidget.h:76
BALL::VIEW::PyWidget::multi_line_text_
String multi_line_text_
Definition: pyWidget.h:438
textEditorWidget.h
BALL::VIEW::PyWidget::highlighter_2_
PythonHighlighter highlighter_2_
Definition: pyWidget.h:424
QSyntaxHighlighter
BALL::VIEW::PyWidget::started_startup_script_
bool started_startup_script_
Definition: pyWidget.h:432
BALL::VIEW::PyWidget::thread_
RunPythonThread * thread_
Definition: pyWidget.h:444
QObject
BALL::VIEW::PyWidget::multi_line_mode_
bool multi_line_mode_
Definition: pyWidget.h:436
BALL::operator<
BALL_EXPORT bool operator<(const String &s1, const String &s2)
BALL::VIEW::RunPythonThread::input
String input
Definition: pyWidget.h:96
BALL::VIEW::PyWidget::intend_
Position intend_
Definition: pyWidget.h:448
BALL::VIEW::PythonHighlighter::string_pattern
QRegExp string_pattern
Definition: pyWidget.h:69
dockWidget.h
BALL_DEPRECATED
#define BALL_DEPRECATED
Definition: COMMON/global.h:64
BALL::VIEW::PyWidget::curr_modifying_
QLabel * curr_modifying_
Definition: pyWidget.h:420
BALL::VIEW::PyWidget::python_settings_
PythonSettings * python_settings_
Definition: pyWidget.h:443
QValidator
BALL::String
Definition: string.h:56
BALL_EMBEDDABLE
#define BALL_EMBEDDABLE(TYPE, BASE)
Definition: embeddable.h:31
BALL::VIEW::MainControl
Definition: mainControl.h:114
BALL::VIEW::PythonHighlighter::~PythonHighlighter
~PythonHighlighter()
Definition: pyWidget.h:56
QThread
BALL
Definition: constants.h:12
QLineEdit
BALL::VIEW::PyWidget
Definition: pyWidget.h:139
BALL::VIEW::PyWidget::isValid
bool isValid() const
Is full Python support available?
Definition: pyWidget.h:201
BALL::VIEW::PyWidget::multi_lines_
Size multi_lines_
Definition: pyWidget.h:437
BALL::VIEW::PythonHighlighter::BALL_keywords
QStringList BALL_keywords
Definition: pyWidget.h:62
BALL::VIEW::PyWidget::valid_
bool valid_
Definition: pyWidget.h:431
BALL::VIEW::PyWidget::results_
vector< bool > results_
Definition: pyWidget.h:440
BALL::VIEW::PythonHighlighter::python_keywords
QStringList python_keywords
Definition: pyWidget.h:61
BALL::VIEW::RunPythonThread
Definition: pyWidget.h:86
BALL::VIEW::PyWidget::complete_prefix_
Size complete_prefix_
Definition: pyWidget.h:446
BALL::VIEW::PyWidget::current_script_
String current_script_
Definition: pyWidget.h:452
BALL::VIEW::PyWidget::hotkeys_
std::list< Hotkey > hotkeys_
Definition: pyWidget.h:428
BALL::VIEW::PyWidget::history_position_
Position history_position_
Definition: pyWidget.h:441
BALL_SIZE_TYPE
BALL::VIEW::PyWidget::script_mode_
bool script_mode_
Definition: pyWidget.h:451
BALL::VIEW::PyWidget::editor_combo_box_
QComboBox * editor_combo_box_
Definition: pyWidget.h:427
BALL::VIEW::PyWidget::preferences_
Preferences * preferences_
Definition: pyWidget.h:433
BALL::VIEW::PythonHighlighter
Definition: pyWidget.h:49
BALL::VIEW::PyWidget::running_
bool running_
Definition: pyWidget.h:449
BALL::INIFile
Definition: INIFile.h:28
string.h
BALL::VIEW::PyWidget::script_edit_
MyTextEdit * script_edit_
Definition: pyWidget.h:422
QWidget
BALL::VIEW::RunPythonThread::output
String output
Definition: pyWidget.h:97
BALL::VIEW::DockWidget
Definition: dockWidget.h:25
BALL::operator==
BALL_EXPORT bool operator==(const String &s1, const String &s2)
BALL::VIEW::PythonHighlighter::my_class_format
QTextCharFormat my_class_format
Definition: pyWidget.h:63
BALL::VIEW::PythonHighlighter::comment_format
QTextCharFormat comment_format
Definition: pyWidget.h:66
BALL::VIEW::PythonHighlighter::python_format
QTextCharFormat python_format
Definition: pyWidget.h:64
common.h
BALL::VIEW::Preferences
Definition: preferences.h:40
BALL::VIEW::PyWidget::startup_script_
String startup_script_
Definition: pyWidget.h:442
BALL::VIEW::PyWidget::history_
vector< String > history_
Definition: pyWidget.h:439
BALL_VIEW_EXPORT
#define BALL_VIEW_EXPORT
Definition: COMMON/global.h:52
BALL::Embeddable
Definition: embeddable.h:81
BALL::VIEW::PyWidget::stop_script_
bool stop_script_
Definition: pyWidget.h:445
BALL::VIEW::Hotkey::key
Qt::Key key
Definition: pyWidget.h:130
BALL::VIEW::PyWidget::tab_widget_
QTabWidget * tab_widget_
Definition: pyWidget.h:423
BALL::VIEW::PyWidget::text_edit_
TextEditorWidget * text_edit_
Definition: pyWidget.h:421
BALL::VIEW::PyWidget::current_line_
Position current_line_
Definition: pyWidget.h:435
BALL::VIEW::PyWidget::working_dir_
String working_dir_
Definition: pyWidget.h:430
BALL::VIEW::Hotkey::comment
String comment
Definition: pyWidget.h:127
BALL::VIEW::Hotkey::button_state
Qt::KeyboardModifiers button_state
Definition: pyWidget.h:129
BALL::VIEW::PythonHighlighter::BALL_patterns
vector< QRegExp > BALL_patterns
Definition: pyWidget.h:68
BALL::VIEW::PythonHighlighter::string_format
QTextCharFormat string_format
Definition: pyWidget.h:65
BALL::VIEW::TextEditorWidget
Definition: textEditorWidget.h:31
BALL::VIEW::PythonSettings
Definition: pythonSettings.h:34
BALL::VIEW::PyWidget::validator_
PythonValidator validator_
Definition: pyWidget.h:425
BALL::VIEW::RunPythonThread::state
bool state
Definition: pyWidget.h:95
BALL::VIEW::Hotkey
Definition: pyWidget.h:101
BALL::VIEW::PyWidget::silent_
bool silent_
Definition: pyWidget.h:450