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