pyWidget.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: pyWidget.h,v 1.27.16.2 2007/05/13 15:35:31 amoll Exp $
00005 //
00006 
00007 #ifndef BALL_VIEW_WIDGETS_PYWIDGET_H
00008 #define BALL_VIEW_WIDGETS_PYWIDGET_H
00009 
00010 #ifndef BALL_COMMON_H
00011 # include <BALL/common.h>
00012 #endif
00013 
00014 #ifndef BALL_DATATYPE_STRING_H
00015 # include <BALL/DATATYPE/string.h>
00016 #endif
00017 
00018 #ifndef BALL_VIEW_WIDGETS_DOCKWIDGET_H
00019 # include <BALL/VIEW/WIDGETS/dockWidget.h>
00020 #endif
00021 
00022 #include <QtGui/QTextEdit>
00023 #include <QtGui/QDragEnterEvent>
00024 #include <QtGui/QKeyEvent>
00025 #include <QtGui/QDropEvent>
00026 #include <QtGui/QLineEdit>
00027 #include <QtGui/QComboBox>
00028 #include <QtGui/QTabWidget>
00029 #include <QtGui/QSyntaxHighlighter>
00030 #include <QtCore/QStringList>
00031 
00032 #include <QtCore/qthread.h>
00033 
00034 // currently doesnt work right
00035 #undef BALL_QT_HAS_THREADS
00036 
00037 namespace BALL
00038 {
00039   namespace VIEW
00040   {
00041 
00042 class PythonSettings;
00043 class Preferences;
00044 
00045 class RunPythonThread;
00046 
00050 class BALL_VIEW_EXPORT PythonHighlighter
00051   : public QSyntaxHighlighter
00052 {
00053   public:
00054 
00055   PythonHighlighter();
00056 
00057   ~PythonHighlighter() {};
00058 
00059   void compilePattern();
00060   void highlightBlock(const QString& text);
00061 
00062   QStringList python_keywords;
00063   QStringList BALL_keywords;
00064   QTextCharFormat my_class_format;
00065   QTextCharFormat python_format;
00066   QTextCharFormat string_format;
00067   QTextCharFormat comment_format;
00068   vector<QRegExp> python_patterns;
00069   vector<QRegExp> BALL_patterns;
00070   QRegExp         string_pattern;
00071   QRegExp         comment_pattern;
00072 };
00073 
00074 class BALL_VIEW_EXPORT RunPythonThread
00075   : public QThread
00076 {
00077   public:
00078     RunPythonThread();
00079 
00081     virtual void run();
00082 
00083     bool state;
00084     String input;
00085     String output;
00086 };
00087 
00089 struct BALL_VIEW_EXPORT Hotkey
00090 {
00091   public:
00092 
00093   static Hotkey createHotkey(String modifier, String key, String command, 
00094                              bool& ok, String comment = "");
00095 
00097   bool operator == (const Hotkey& hotkey) const;
00098 
00100   bool operator == (const QKeyEvent& e) const;
00101 
00103   const Hotkey& operator = (const Hotkey& s);
00104 
00106   bool operator < (const Hotkey& key) { return this < &key;}
00107 
00109   bool set(const String& data);
00110 
00112   void get(String& data) const;
00113 
00114   String                comment;
00115   String                action;
00116   Qt::KeyboardModifiers button_state;
00117   Qt::Key               key;
00118 };
00119 
00126 class BALL_VIEW_EXPORT PyWidget
00127   : public DockWidget
00128 {
00129   class MyLineEdit
00130     : public QLineEdit
00131   {
00132     public:
00133       MyLineEdit(QWidget* parent)
00134         : QLineEdit(parent) {};
00135 
00136       void setPyWidget(PyWidget* pw) { pw_ = pw;}
00137 
00138     protected:
00139 
00140       virtual void keyPressEvent(QKeyEvent* event);
00141 
00142       PyWidget* pw_;
00143   };
00144 
00145   class MyTextEdit
00146     : public QTextEdit
00147   {
00148     public:
00149       MyTextEdit(QWidget* parent)
00150         : QTextEdit(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   friend class MyLineEdit;
00162 
00163   Q_OBJECT
00164 
00165   public:
00166   
00167   BALL_EMBEDDABLE(PyWidget, Embeddable)
00168 
00169   
00172 
00179   PyWidget(QWidget* parent = 0, const char* name = 0);
00180 
00182   PyWidget(const PyWidget& p);
00183 
00185   ~PyWidget();
00186   
00188   bool isValid() const 
00189     { return valid_;}
00190 
00194 
00197   virtual void initializeWidget(MainControl& main_control);
00198 
00201   virtual void finalizeWidget(MainControl& main_control);
00202   
00204   void initializePreferencesTab(Preferences &preferences);
00205   
00207   void finalizePreferencesTab(Preferences &preferences);
00208 
00210   virtual void applyPreferences();
00211   
00213   virtual void scriptDialog(bool run = false);
00214 
00216   bool toAbortScript();
00217 
00222   void map(String modifier, String key, String command, String comment = "");
00223 
00225   void unmap(String modifier, String key);
00226 
00228   void insertHotkey(const Hotkey& hotkey);
00229 
00231   void removeHotkey(const Hotkey& hotkey);
00232 
00234   void reactTo(const QKeyEvent& e);
00235 
00241   virtual bool canHandle(const String& fileformat) const;
00242 
00247   virtual bool openFile(const String& filename);
00248 
00252   bool openFile(const String& filename, bool run, bool is_current = false);
00253 
00254   //
00255   bool runString(String command);
00256 
00257   //
00258   String getCurrentLine() const;
00259 
00260   //
00261   void dump(std::ostream& s, Size depth) const;
00262 
00263   //
00264   void showClassDocu(String classname, String member);
00265 
00266   //
00267   virtual void fetchPreferences(INIFile& inifile);
00268   
00269   //
00270   virtual void writePreferences(INIFile& inifile);
00271 
00272   //
00273   void checkMenu(MainControl& main_control);
00274 
00276   QString getCurrentScript();
00277 
00279   bool isInDirectMode() const;
00280 
00281   public slots:
00282 
00283   //
00284   void showEditContextMenu(const QPoint& point);
00285 
00286   //
00287   void showContextMenu(const QPoint& point);
00288 
00289   //
00290   void showCompletion();
00291 
00292   //
00293   void clear();
00294 
00295   //
00296   void exportHistory();
00297 
00298   //
00299   void abortScript();
00300 
00301   // Show the docu for the current line
00302   void showDocumentation();
00303 
00305   bool runCurrentScript();
00306 
00308   void loadScript();
00309   
00311   void execScript();
00312 
00314   void saveScript();
00315 
00317   void clearScript();
00318 
00319   virtual void hotkeyItem();
00320 
00321   virtual void modifyHotkeys();
00322 
00323   void appendText(const String& text, bool output = false, bool state_message = false);
00324 
00325   bool getMembers(const String& classname, QStringList& sl, const String& prefix);
00326 
00327   protected slots:
00328 
00329   virtual bool returnPressed();
00330 
00331   virtual bool completionSelected_();
00332 
00333   virtual void showHelp_();
00334 
00335   virtual void printCursorPosition_();
00336 
00337   virtual void createScript_();
00338 
00339   virtual void clearHistory_();
00340 
00341   protected:
00342 
00343   bool getClassAndMember_(String toc);
00344 
00345   void setError_(bool state);
00346 
00352   virtual void startInterpreter();
00353 
00354   virtual void paste();
00355 
00357   bool parseLine_(String line);
00358 
00359   void appendToHistory_(const String& line);
00360   
00366   void newPrompt_();
00367 
00368   bool testMultilineStart_(const String& line);
00369 
00370   //_
00371   const char* getPrompt_() const;
00372 
00373   //_
00374   bool storeScript_();
00375 
00376   //_ Wrapper for multi and single threading call
00377   String runCommand_(const String& command, bool& state);
00378 
00379   void appendText_(QTextEdit* te, String text);
00380 
00387   void retrieveHistoryLine_(Position index);
00388 
00389   virtual void contentsDragEnterEvent(QDragEnterEvent* e);
00390 
00391   virtual void contentsDropEvent(QDropEvent* e);
00392 
00393   bool keyPressed(QKeyEvent* e);
00394 
00395   void createMenuHelpEntry_(QMenu* menu, QTextEdit* text_edit, const QPoint& point);
00396 
00397   void findError_(String result);
00398 
00399   String getCurrentWord_(QTextCursor& text_cursor);
00400 
00401   QComboBox* getCompletionBox_();
00402 
00403   List<Hotkey>::Iterator findKey_(Hotkey& hotkey);
00404 
00405   QTextEdit*        text_edit_, *script_output_;
00406   MyTextEdit*       script_edit_;
00407   QTabWidget*       tab_widget_;
00408   PythonHighlighter highlighter_1_, highlighter_2_;
00409   MyLineEdit*       line_edit_;
00410   QComboBox*        combo_box_, *editor_combo_box_;
00411   List<Hotkey>      hotkeys_;
00412   //                we use an own working dir to find Python Scripts
00413   String            working_dir_;
00414   bool              valid_;
00415   bool              started_startup_script_;
00416   Preferences*      preferences_;
00417 
00418   Position          current_line_;
00419   bool              multi_line_mode_;
00420   Size              multi_lines_;
00421   String            multi_line_text_;
00422   vector<String>    history_;
00423   vector<bool>      results_;
00424   Position          history_position_;
00425   String            startup_script_;
00426   PythonSettings*   python_settings_;
00427   RunPythonThread*  thread_;
00428   bool              stop_script_;
00429   Size              complete_prefix_;
00430   String            class_, member_;
00431   Position          intend_;
00432   bool              running_;
00433   bool              silent_, full_silent_;
00434   bool              script_mode_;
00435   String            current_script_;
00436 };
00437 
00438   } // namespaces 
00439 } // namespaces
00440   
00441 #endif // BALL_VIEW_WIDGETS_PYWIDGET_H