00001
00002
00003
00004
00005 #ifndef BALL_VIEW_WIDGETS_PYWIDGET_H
00006 #define BALL_VIEW_WIDGETS_PYWIDGET_H
00007
00008 #ifndef BALL_COMMON_H
00009 # include <BALL/common.h>
00010 #endif
00011
00012 #ifndef BALL_DATATYPE_STRING_H
00013 # include <BALL/DATATYPE/string.h>
00014 #endif
00015
00016 #ifndef BALL_VIEW_WIDGETS_DOCKWIDGET_H
00017 # include <BALL/VIEW/WIDGETS/dockWidget.h>
00018 #endif
00019
00020 #ifndef BALL_VIEW_WIDGETS_TEXTEDITORWIDGET_H
00021 # include <BALL/VIEW/WIDGETS/textEditorWidget.h>
00022 #endif
00023
00024 #include <QtGui/QDragEnterEvent>
00025 #include <QtGui/QKeyEvent>
00026 #include <QtGui/QDropEvent>
00027 #include <QtGui/QLineEdit>
00028 #include <QtGui/QComboBox>
00029 #include <QtGui/QTabWidget>
00030 #include <QtGui/QSyntaxHighlighter>
00031 #include <QtGui/QValidator>
00032 #include <QtCore/QStringList>
00033
00034 #include <QtCore/QThread>
00035
00036
00037 #undef BALL_QT_HAS_THREADS
00038
00039 namespace BALL
00040 {
00041 namespace VIEW
00042 {
00043
00044 class PythonSettings;
00045 class Preferences;
00046
00047 class RunPythonThread;
00048
00052 class BALL_VIEW_EXPORT PythonHighlighter
00053 : public QSyntaxHighlighter
00054 {
00055 public:
00056
00057 PythonHighlighter();
00058
00059 ~PythonHighlighter() {};
00060
00061 void compilePattern();
00062 void highlightBlock(const QString& text);
00063
00064 QStringList python_keywords;
00065 QStringList BALL_keywords;
00066 QTextCharFormat my_class_format;
00067 QTextCharFormat python_format;
00068 QTextCharFormat string_format;
00069 QTextCharFormat comment_format;
00070 vector<QRegExp> python_patterns;
00071 vector<QRegExp> BALL_patterns;
00072 QRegExp string_pattern;
00073 QRegExp comment_pattern;
00074 };
00075
00079 class BALL_VIEW_EXPORT PythonValidator
00080 : public QValidator
00081 {
00082 public:
00083 PythonValidator(QObject* parent = 0);
00084 virtual ~PythonValidator();
00085
00086 virtual State validate(QString& input, int& pos) const;
00087 };
00088
00089 class BALL_VIEW_EXPORT RunPythonThread
00090 : public QThread
00091 {
00092 public:
00093 RunPythonThread();
00094
00096 virtual void run();
00097
00098 bool state;
00099 String input;
00100 String output;
00101 };
00102
00104 struct BALL_VIEW_EXPORT Hotkey
00105 {
00106
00107 public:
00108
00109 static Hotkey createHotkey(String modifier, String key, String command,
00110 bool& ok, String comment = "");
00111
00113 bool operator == (const Hotkey& hotkey) const;
00114
00116 bool operator == (const QKeyEvent& e) const;
00117
00119 const Hotkey& operator = (const Hotkey& s);
00120
00122 bool operator < (const Hotkey& key) { return this < &key;}
00123
00125 bool set(const String& data);
00126
00128 void get(String& data) const;
00129
00130 String comment;
00131 String action;
00132 Qt::KeyboardModifiers button_state;
00133 Qt::Key key;
00134 };
00135
00142 class BALL_VIEW_EXPORT PyWidget
00143 : public DockWidget
00144 {
00145 class MyLineEdit
00146 : public QLineEdit
00147 {
00148 public:
00149 MyLineEdit(QWidget* parent)
00150 : QLineEdit(parent) {};
00151
00152 void setPyWidget(PyWidget* pw) { pw_ = pw;}
00153
00154 protected:
00155
00156 virtual void keyPressEvent(QKeyEvent* event);
00157
00158 PyWidget* pw_;
00159 };
00160
00161 class MyTextEdit
00162 : public TextEditorWidget
00163 {
00164 public:
00165 MyTextEdit(QWidget* parent)
00166 : TextEditorWidget(parent) {};
00167
00168 void setPyWidget(PyWidget* pw) { pw_ = pw;}
00169
00170 protected:
00171
00172 virtual void keyPressEvent(QKeyEvent* event);
00173
00174 PyWidget* pw_;
00175 };
00176
00177 friend class MyLineEdit;
00178
00179 Q_OBJECT
00180
00181 public:
00182
00183 BALL_EMBEDDABLE(PyWidget, Embeddable)
00184
00185
00188
00195 PyWidget(QWidget* parent = 0, const char* name = 0);
00196
00198 PyWidget(const PyWidget& p);
00199
00201 ~PyWidget();
00202
00204 bool isValid() const
00205 { return valid_;}
00206
00210
00213 virtual void initializeWidget(MainControl& main_control);
00214
00217 virtual void finalizeWidget(MainControl& main_control);
00218
00220 void initializePreferencesTab(Preferences &preferences);
00221
00223 void finalizePreferencesTab(Preferences &preferences);
00224
00226 virtual void applyPreferences();
00227
00229 virtual void scriptDialog(bool run = false);
00230
00232 bool toAbortScript();
00233
00238 void map(String modifier, String key, String command, String comment = "");
00239
00241 void unmap(String modifier, String key);
00242
00244 void insertHotkey(const Hotkey& hotkey);
00245
00247 void removeHotkey(const Hotkey& hotkey);
00248
00250 void reactTo(const QKeyEvent& e);
00251
00257 virtual bool canHandle(const String& fileformat) const;
00258
00263 virtual bool openFile(const String& filename);
00264
00268 bool openFile(const String& filename, bool run, bool is_current = false);
00269
00270
00271 bool runString(String command);
00272
00273
00274 String getCurrentLine() const;
00275
00276
00277 void dump(std::ostream& s, Size depth) const;
00278
00279
00280 void showClassDocu(String classname, String member);
00281
00282
00283 virtual void fetchPreferences(INIFile& inifile);
00284
00285
00286 virtual void writePreferences(INIFile& inifile);
00287
00288
00289 void checkMenu(MainControl& main_control);
00290
00292 QString getCurrentScript();
00293
00295 bool isInDirectMode() const;
00296
00297 public slots:
00298
00299
00300 void showEditContextMenu(const QPoint& point);
00301
00302
00303 void showContextMenu(const QPoint& point);
00304
00305
00306 void showCompletion();
00307
00308
00309 void clear();
00310
00311
00312 void exportHistory();
00313
00314
00315 void abortScript();
00316
00317
00318 void showDocumentation();
00319
00321 bool runCurrentScript();
00322
00324 void loadScript();
00325
00327 void execScript();
00328
00330 void saveScript();
00331
00333 void clearScript();
00334
00335 virtual void hotkeyItem();
00336
00337 virtual void modifyHotkeys();
00338
00339 void appendText(const String& text, bool output = false, bool state_message = false);
00340
00341 bool getMembers(const String& classname, QStringList& sl, const String& prefix);
00342
00343 protected slots:
00344
00345 virtual bool returnPressed();
00346
00347 virtual bool completionSelected_();
00348
00349 virtual void showHelp_();
00350
00351 virtual void printCursorPosition_();
00352
00353 virtual void createScript_();
00354
00355 virtual void clearHistory_();
00356
00357 protected:
00358
00359 bool getClassAndMember_(String toc);
00360
00361 void setError_(bool state);
00362
00368 virtual void startInterpreter();
00369
00370 virtual void paste();
00371
00373 bool parseLine_(String line);
00374
00375 void appendToHistory_(const String& line);
00376
00382 void newPrompt_();
00383
00384 bool testMultilineStart_(const String& line);
00385
00386
00387 const char* getPrompt_() const;
00388
00389
00390 bool storeScript_();
00391
00392
00393 String runCommand_(const String& command, bool& state);
00394
00395 void appendText_(TextEditorWidget* te, String text);
00396
00403 void retrieveHistoryLine_(Position index);
00404
00405 virtual void contentsDragEnterEvent(QDragEnterEvent* e);
00406
00407 virtual void contentsDropEvent(QDropEvent* e);
00408
00409 bool keyPressed(QKeyEvent* e);
00410
00411 void createMenuHelpEntry_(QMenu* menu, TextEditorWidget* text_edit, const QPoint& point);
00412
00413 void findError_(String result);
00414
00415 String getCurrentWord_(QTextCursor& text_cursor);
00416
00417 QComboBox* getCompletionBox_();
00418
00419 std::list<Hotkey>::iterator findKey_(Hotkey& hotkey);
00420
00421 TextEditorWidget* text_edit_, *script_output_;
00422 MyTextEdit* script_edit_;
00423 QTabWidget* tab_widget_;
00424 PythonHighlighter highlighter_1_, highlighter_2_;
00425 PythonValidator validator_;
00426 MyLineEdit* line_edit_;
00427 QComboBox* combo_box_, *editor_combo_box_;
00428 std::list<Hotkey> hotkeys_;
00429
00430 String working_dir_;
00431 bool valid_;
00432 bool started_startup_script_;
00433 Preferences* preferences_;
00434
00435 Position current_line_;
00436 bool multi_line_mode_;
00437 Size multi_lines_;
00438 String multi_line_text_;
00439 vector<String> history_;
00440 vector<bool> results_;
00441 Position history_position_;
00442 String startup_script_;
00443 PythonSettings* python_settings_;
00444 RunPythonThread* thread_;
00445 bool stop_script_;
00446 Size complete_prefix_;
00447 String class_, member_;
00448 Position intend_;
00449 bool running_;
00450 bool silent_, full_silent_;
00451 bool script_mode_;
00452 String current_script_;
00453 };
00454
00455 }
00456 }
00457
00458 #endif // BALL_VIEW_WIDGETS_PYWIDGET_H