ScriptEdit.h

Go to the documentation of this file.
00001 /***************************************************************************
00002     File                 : ScriptEdit.h
00003     Project              : QtiPlot
00004     --------------------------------------------------------------------
00005     Copyright            : (C) 2006 by Ion Vasilief, Knut Franke
00006     Email (use @ for *)  : ion_vasilief*yahoo.fr, knut.franke*gmx.de
00007     Description          : Scripting classes
00008 
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *  This program is free software; you can redistribute it and/or modify   *
00014  *  it under the terms of the GNU General Public License as published by   *
00015  *  the Free Software Foundation; either version 2 of the License, or      *
00016  *  (at your option) any later version.                                    *
00017  *                                                                         *
00018  *  This program is distributed in the hope that it will be useful,        *
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00021  *  GNU General Public License for more details.                           *
00022  *                                                                         *
00023  *   You should have received a copy of the GNU General Public License     *
00024  *   along with this program; if not, write to the Free Software           *
00025  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
00026  *   Boston, MA  02110-1301  USA                                           *
00027  *                                                                         *
00028  ***************************************************************************/
00029 #ifndef SCRIPTEDIT_H
00030 #define SCRIPTEDIT_H
00031 
00032 #include "ScriptingEnv.h"
00033 #include "Script.h"
00034 
00035 #include <QMenu>
00036 #include <QTextEdit>
00037 
00038 class QAction;
00039 class QMenu;
00040 class QCompleter;
00041 
00042 class SyntaxHighlighter;
00043 
00050 class ScriptEdit: public QTextEdit, public scripted
00051 {
00052   Q_OBJECT
00053 
00054   public:
00055     ScriptEdit(ScriptingEnv *env, QWidget *parent=0, const char *name=0);
00056     ~ScriptEdit();
00058     void customEvent(QEvent*);
00060     int lineNumber(int pos) const;
00061     bool error(){return d_error;};
00062 
00063     void setCompleter(QCompleter *c);
00064     void setFileName(const QString& fn);
00065     void rehighlight();
00066     void redirectOutputTo(QTextEdit *);
00067 
00068   public slots:
00069     void execute();
00070     void executeAll();
00071     void evaluate();
00072     void print();
00073     void print(QPrinter*);
00074     void exportPDF(const QString& fileName);
00075     QString save();
00076     QString exportASCII(const QString &file=QString::null);
00077     QString importASCII(const QString &file=QString::null);
00078     void insertFunction(const QString &);
00079     void insertFunction(QAction * action);
00080     void setContext(QObject *context) { myScript->setContext(context); }
00081     void scriptPrint(const QString&);
00082 
00083     void updateIndentation();
00084     void setDirPath(const QString& path);
00085     void showFindDialog(bool replace = false);
00086     void replace(){showFindDialog(true);};
00087     bool find(const QString& searchString, QTextDocument::FindFlags flags, bool previous = false);
00088     void findNext();
00089     void findPrevious();
00090 
00091   signals:
00092     void dirPathChanged(const QString& path);
00093     void error(const QString&, const QString&, int);
00094 
00095   protected:
00096     virtual void contextMenuEvent(QContextMenuEvent *e);
00097     virtual void keyPressEvent(QKeyEvent *e);
00098     void focusInEvent(QFocusEvent *e);
00099 
00100   private:
00101     void clearErrorHighlighting();
00102     void highlightErrorLine(int offset);
00103 
00104     Script *myScript;
00105     QAction *actionExecute, *actionExecuteAll, *actionEval, *actionPrint, *actionImport, *actionSave, *actionExport;
00106     QAction *actionFind, *actionReplace, *actionFindNext, *actionFindPrevious;
00108     QMenu *functionsMenu;
00110     QTextCursor printCursor;
00111     QString scriptsDirPath;
00112 
00114     QTextBlockFormat d_fmt_default;
00116     bool d_error;
00117     QString d_err_message;
00118 
00119     QCompleter *d_completer;
00120     SyntaxHighlighter *d_highlighter;
00121     QString d_file_name;
00122     QString d_search_string;
00123     QTextDocument::FindFlags d_search_flags;
00124     QTextEdit *d_output_widget;
00125 
00126   private slots:
00128 
00132     void insertErrorMsg(const QString &message);
00133     void insertCompletion(const QString &completion);
00134     void matchParentheses();
00135 
00136   private:
00137     QString textUnderCursor() const;
00138     bool matchLeftParenthesis(QTextBlock currentBlock, int index, int numRightParentheses);
00139     bool matchRightParenthesis(QTextBlock currentBlock, int index, int numLeftParentheses);
00140     void createParenthesisSelection(int pos);
00141 };
00142 
00144 struct ParenthesisInfo
00145 {
00146     char character;
00147     int position;
00148 };
00149 
00151 class TextBlockData : public QTextBlockUserData
00152 {
00153 public:
00154     TextBlockData(){};
00155 
00156     QVector<ParenthesisInfo *> parentheses(){return m_parentheses;};
00157     void insert(ParenthesisInfo *info)
00158     {
00159         int i = 0;
00160         while (i < m_parentheses.size() &&
00161             info->position > m_parentheses.at(i)->position)
00162             ++i;
00163 
00164         m_parentheses.insert(i, info);
00165     }
00166 
00167 private:
00168     QVector<ParenthesisInfo *> m_parentheses;
00169 };
00170 
00171 #endif

Generated by  doxygen 1.6.2