QtiPlot 0.9.7.3

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     void enableShortcuts();
00068 
00069   public slots:
00070     void execute();
00071     void executeAll();
00072     void evaluate();
00073     void print();
00074     void print(QPrinter*);
00075     void exportPDF(const QString& fileName);
00076     QString save();
00077     QString exportASCII(const QString &file=QString::null);
00078     QString importASCII(const QString &file=QString::null);
00079     void insertFunction(const QString &);
00080     void insertFunction(QAction * action);
00081     void setContext(QObject *context) { myScript->setContext(context); }
00082     void scriptPrint(const QString&);
00083 
00084     void updateIndentation();
00085     void setDirPath(const QString& path);
00086     void showFindDialog(bool replace = false);
00087     void replace(){showFindDialog(true);};
00088     bool find(const QString& searchString, QTextDocument::FindFlags flags, bool previous = false);
00089     void findNext();
00090     void findPrevious();
00091     void commentSelection();
00092     void uncommentSelection();
00093 
00094   signals:
00095     void dirPathChanged(const QString& path);
00096     void error(const QString&, const QString&, int);
00097     void activated(ScriptEdit *);
00098 
00099   protected:
00100     virtual void contextMenuEvent(QContextMenuEvent *e);
00101     virtual void keyPressEvent(QKeyEvent *e);
00102     void focusInEvent(QFocusEvent *e);
00103 
00104   private:
00105     void clearErrorHighlighting();
00106     void highlightErrorLine(int offset);
00107 
00108     Script *myScript;
00109     QAction *actionExecute, *actionExecuteAll, *actionEval, *actionPrint, *actionImport, *actionSave, *actionExport;
00110     QAction *actionFind, *actionReplace, *actionFindNext, *actionFindPrevious;
00112     QMenu *functionsMenu;
00114     QTextCursor printCursor;
00115     QString scriptsDirPath;
00116 
00118     QTextBlockFormat d_fmt_default;
00120     bool d_error;
00121     QString d_err_message;
00122 
00123     QCompleter *d_completer;
00124     SyntaxHighlighter *d_highlighter;
00125     QString d_file_name;
00126     QString d_search_string;
00127     QTextDocument::FindFlags d_search_flags;
00128     QTextEdit *d_output_widget;
00129 
00130   private slots:
00132 
00136     void insertErrorMsg(const QString &message);
00137     void insertCompletion(const QString &completion);
00138     void matchParentheses();
00139 
00140   private:
00141     QString textUnderCursor() const;
00142     bool matchLeftParenthesis(QTextBlock currentBlock, int index, int numRightParentheses);
00143     bool matchRightParenthesis(QTextBlock currentBlock, int index, int numLeftParentheses);
00144     void createParenthesisSelection(int pos);
00145 };
00146 
00148 struct ParenthesisInfo
00149 {
00150     char character;
00151     int position;
00152 };
00153 
00155 class TextBlockData : public QTextBlockUserData
00156 {
00157 public:
00158     TextBlockData(){};
00159 
00160     QVector<ParenthesisInfo *> parentheses(){return m_parentheses;};
00161     void insert(ParenthesisInfo *info)
00162     {
00163         int i = 0;
00164         while (i < m_parentheses.size() &&
00165             info->position > m_parentheses.at(i)->position)
00166             ++i;
00167 
00168         m_parentheses.insert(i, info);
00169     }
00170 
00171 private:
00172     QVector<ParenthesisInfo *> m_parentheses;
00173 };
00174 
00175 #endif