QtiPlot  0.9.8.2
ScriptEdit.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : ScriptEdit.h
3  Project : QtiPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2006 by Ion Vasilief, Knut Franke
6  Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de
7  Description : Scripting classes
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 #ifndef SCRIPTEDIT_H
30 #define SCRIPTEDIT_H
31 
32 #include "ScriptingEnv.h"
33 #include "Script.h"
34 
35 #include <QMenu>
36 #include <QTextEdit>
37 
38 class QAction;
39 class QMenu;
40 class QCompleter;
41 
42 class SyntaxHighlighter;
43 
50 class ScriptEdit: public QTextEdit, public scripted
51 {
52  Q_OBJECT
53 
54  public:
55  ScriptEdit(ScriptingEnv *env, QWidget *parent=0, const char *name=0);
56  ~ScriptEdit();
58  void customEvent(QEvent*);
60  int lineNumber(int pos) const;
61  bool error(){return d_error;};
62 
63  void setCompleter(QCompleter *c);
64  void setFileName(const QString& fn);
65  void rehighlight();
66  void redirectOutputTo(QTextEdit *);
67  void enableShortcuts();
68 
69  public slots:
70  void execute();
71  void executeAll();
72  void evaluate();
73  void print();
74  void print(QPrinter*);
75  void exportPDF(const QString& fileName);
76  QString save();
77  QString exportASCII(const QString &file=QString::null);
78  QString importASCII(const QString &file=QString::null);
79  void insertFunction(const QString &);
80  void insertFunction(QAction * action);
81  void setContext(QObject *context) { myScript->setContext(context); }
82  void scriptPrint(const QString&);
83 
84  void updateIndentation();
85  void setDirPath(const QString& path);
86  void showFindDialog(bool replace = false);
87  void replace(){showFindDialog(true);};
88  bool find(const QString& searchString, QTextDocument::FindFlags flags, bool previous = false);
89  void findNext();
90  void findPrevious();
91  void commentSelection();
92  void uncommentSelection();
93 
94  signals:
95  void dirPathChanged(const QString& path);
96  void error(const QString&, const QString&, int);
97  void activated(ScriptEdit *);
98 
99  protected:
100  virtual void contextMenuEvent(QContextMenuEvent *e);
101  virtual void keyPressEvent(QKeyEvent *e);
102  void focusInEvent(QFocusEvent *e);
103 
104  private:
105  void clearErrorHighlighting();
106  void highlightErrorLine(int offset);
107 
114  QTextCursor printCursor;
115  QString scriptsDirPath;
116 
118  QTextBlockFormat d_fmt_default;
120  bool d_error;
121  QString d_err_message;
122 
123  QCompleter *d_completer;
125  QString d_file_name;
127  QTextDocument::FindFlags d_search_flags;
128  QTextEdit *d_output_widget;
129 
130  private slots:
132 
136  void insertErrorMsg(const QString &message);
137  void insertCompletion(const QString &completion);
138  void matchParentheses();
139 
140  private:
141  QString textUnderCursor() const;
142  bool matchLeftParenthesis(QTextBlock currentBlock, int index, int numRightParentheses);
143  bool matchRightParenthesis(QTextBlock currentBlock, int index, int numLeftParentheses);
144  void createParenthesisSelection(int pos);
145 };
146 
149 {
150  char character;
151  int position;
152 };
153 
155 class TextBlockData : public QTextBlockUserData
156 {
157 public:
159 
160  QVector<ParenthesisInfo *> parentheses(){return m_parentheses;};
162  {
163  int i = 0;
164  while (i < m_parentheses.size() &&
165  info->position > m_parentheses.at(i)->position)
166  ++i;
167 
168  m_parentheses.insert(i, info);
169  }
170 
171 private:
172  QVector<ParenthesisInfo *> m_parentheses;
173 };
174 
175 #endif