ScriptEdit.h
Go to the documentation of this file.
/***************************************************************************
File : ScriptEdit.h
Project : QtiPlot
--------------------------------------------------------------------
Copyright : (C) 2006 by Ion Vasilief, Knut Franke
Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de
Description : Scripting classes
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#ifndef SCRIPTEDIT_H
#define SCRIPTEDIT_H
#include "ScriptingEnv.h"
#include "Script.h"
#include <QMenu>
#include <QTextEdit>
class QAction;
class QMenu;
class QCompleter;
class SyntaxHighlighter;
class ScriptEdit: public QTextEdit, public scripted
{
Q_OBJECT
public:
ScriptEdit(ScriptingEnv *env, QWidget *parent=0, const char *name=0);
~ScriptEdit();
void customEvent(QEvent*);
int lineNumber(int pos) const;
bool error(){return d_error;};
void setCompleter(QCompleter *c);
void setFileName(const QString& fn);
void rehighlight();
void redirectOutputTo(QTextEdit *);
void enableShortcuts();
public slots:
void execute();
void executeAll();
void evaluate();
void print();
void print(QPrinter*);
void exportPDF(const QString& fileName);
QString save();
QString exportASCII(const QString &file=QString::null);
QString importASCII(const QString &file=QString::null);
void insertFunction(const QString &);
void insertFunction(QAction * action);
void setContext(QObject *context) { myScript->setContext(context); }
void scriptPrint(const QString&);
void updateIndentation();
void setDirPath(const QString& path);
void showFindDialog(bool replace = false);
void replace(){showFindDialog(true);};
bool find(const QString& searchString, QTextDocument::FindFlags flags, bool previous = false);
void findNext();
void findPrevious();
void commentSelection();
void uncommentSelection();
signals:
void dirPathChanged(const QString& path);
void error(const QString&, const QString&, int);
void activated(ScriptEdit *);
protected:
virtual void contextMenuEvent(QContextMenuEvent *e);
virtual void keyPressEvent(QKeyEvent *e);
void focusInEvent(QFocusEvent *e);
private:
void clearErrorHighlighting();
void highlightErrorLine(int offset);
Script *myScript;
QAction *actionExecute, *actionExecuteAll, *actionEval, *actionPrint, *actionImport, *actionSave, *actionExport;
QAction *actionFind, *actionReplace, *actionFindNext, *actionFindPrevious;
QMenu *functionsMenu;
QTextCursor printCursor;
QString scriptsDirPath;
QTextBlockFormat d_fmt_default;
bool d_error;
QString d_err_message;
QCompleter *d_completer;
SyntaxHighlighter *d_highlighter;
QString d_file_name;
QString d_search_string;
QTextDocument::FindFlags d_search_flags;
QTextEdit *d_output_widget;
private slots:
void insertErrorMsg(const QString &message);
void insertCompletion(const QString &completion);
void matchParentheses();
private:
QString textUnderCursor() const;
bool matchLeftParenthesis(QTextBlock currentBlock, int index, int numRightParentheses);
bool matchRightParenthesis(QTextBlock currentBlock, int index, int numLeftParentheses);
void createParenthesisSelection(int pos);
};
struct ParenthesisInfo
{
char character;
int position;
};
class TextBlockData : public QTextBlockUserData
{
public:
TextBlockData(){};
QVector<ParenthesisInfo *> parentheses(){return m_parentheses;};
void insert(ParenthesisInfo *info)
{
int i = 0;
while (i < m_parentheses.size() &&
info->position > m_parentheses.at(i)->position)
++i;
m_parentheses.insert(i, info);
}
private:
QVector<ParenthesisInfo *> m_parentheses;
};
#endif
ScriptEdit::executeAll
void executeAll()
Definition: ScriptEdit.cpp:431
ScriptEdit::showFindDialog
void showFindDialog(bool replace=false)
Definition: ScriptEdit.cpp:732
ScriptEdit::actionPrint
QAction * actionPrint
Definition: ScriptEdit.h:109
ScriptEdit::exportASCII
QString exportASCII(const QString &file=QString::null)
Definition: ScriptEdit.cpp:560
ScriptEdit::setContext
void setContext(QObject *context)
Definition: ScriptEdit.h:81
ScriptEdit::~ScriptEdit
~ScriptEdit()
Definition: ScriptEdit.cpp:935
ScriptEdit::actionImport
QAction * actionImport
Definition: ScriptEdit.h:109
ScriptEdit::replace
void replace()
Definition: ScriptEdit.h:87
ScriptEdit::actionExecute
QAction * actionExecute
Definition: ScriptEdit.h:109
ScriptEdit::error
bool error()
Definition: ScriptEdit.h:61
ScriptEdit::setCompleter
void setCompleter(QCompleter *c)
Definition: ScriptEdit.cpp:631
ScriptEdit::setFileName
void setFileName(const QString &fn)
Definition: ScriptEdit.cpp:598
TextBlockData::m_parentheses
QVector< ParenthesisInfo * > m_parentheses
Definition: ScriptEdit.h:172
ScriptEdit::ScriptEdit
ScriptEdit(ScriptingEnv *env, QWidget *parent=0, const char *name=0)
Definition: ScriptEdit.cpp:51
ScriptEdit::d_file_name
QString d_file_name
Definition: ScriptEdit.h:125
ScriptEdit::print
void print()
Definition: ScriptEdit.cpp:493
Script::setContext
virtual void setContext(QObject *context)
Set the context in which the code is to be executed.
Definition: Script.h:73
ScriptEdit::matchLeftParenthesis
bool matchLeftParenthesis(QTextBlock currentBlock, int index, int numRightParentheses)
Definition: ScriptEdit.cpp:861
ScriptEdit::d_output_widget
QTextEdit * d_output_widget
Definition: ScriptEdit.h:128
ScriptingEnv
An interpreter for evaluating scripting code. Abstract.
Definition: ScriptingEnv.h:50
ScriptEdit::exportPDF
void exportPDF(const QString &fileName)
Definition: ScriptEdit.cpp:483
ScriptEdit::insertFunction
void insertFunction(const QString &)
Definition: ScriptEdit.cpp:354
ScriptEdit::highlightErrorLine
void highlightErrorLine(int offset)
Definition: ScriptEdit.cpp:799
ScriptingEnv.h
ScriptEdit::save
QString save()
Definition: ScriptEdit.cpp:555
ScriptEdit::findNext
void findNext()
Definition: ScriptEdit.cpp:766
ScriptEdit::activated
void activated(ScriptEdit *)
ScriptEdit::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *e)
Definition: ScriptEdit.cpp:234
ScriptEdit::focusInEvent
void focusInEvent(QFocusEvent *e)
Definition: ScriptEdit.cpp:170
ScriptEdit::createParenthesisSelection
void createParenthesisSelection(int pos)
Definition: ScriptEdit.cpp:915
Script.h
ScriptEdit::customEvent
void customEvent(QEvent *)
Handle changing of scripting environment.
Definition: ScriptEdit.cpp:156
Script
A chunk of scripting code. Abstract.
Definition: Script.h:50
ScriptEdit::d_err_message
QString d_err_message
Definition: ScriptEdit.h:121
ScriptEdit::actionSave
QAction * actionSave
Definition: ScriptEdit.h:109
ParenthesisInfo
Structure used for parentheses matching.
Definition: ScriptEdit.h:148
ScriptEdit::lineNumber
int lineNumber(int pos) const
Map cursor positions to line numbers.
Definition: ScriptEdit.cpp:399
ScriptEdit::matchRightParenthesis
bool matchRightParenthesis(QTextBlock currentBlock, int index, int numLeftParentheses)
Definition: ScriptEdit.cpp:889
ScriptEdit::scriptsDirPath
QString scriptsDirPath
Definition: ScriptEdit.h:115
ScriptEdit::setDirPath
void setDirPath(const QString &path)
Definition: ScriptEdit.cpp:622
ScriptEdit::uncommentSelection
void uncommentSelection()
Definition: ScriptEdit.cpp:383
ScriptEdit::find
bool find(const QString &searchString, QTextDocument::FindFlags flags, bool previous=false)
Definition: ScriptEdit.cpp:741
ScriptEdit
Editor widget with support for evaluating expressions and executing code.
Definition: ScriptEdit.h:50
ScriptEdit::matchParentheses
void matchParentheses()
Definition: ScriptEdit.cpp:835
ScriptEdit::redirectOutputTo
void redirectOutputTo(QTextEdit *)
Definition: ScriptEdit.cpp:826
ScriptEdit::printCursor
QTextCursor printCursor
Cursor used for output of evaluation results and error messages.
Definition: ScriptEdit.h:114
ScriptEdit::myScript
Script * myScript
Definition: ScriptEdit.h:108
ScriptEdit::dirPathChanged
void dirPathChanged(const QString &path)
ScriptEdit::execute
void execute()
Definition: ScriptEdit.cpp:407
ScriptEdit::d_search_string
QString d_search_string
Definition: ScriptEdit.h:126
ScriptEdit::d_error
bool d_error
True if we are inside evaluate(), execute() or executeAll() there were errors.
Definition: ScriptEdit.h:120
ScriptEdit::textUnderCursor
QString textUnderCursor() const
Definition: ScriptEdit.cpp:679
ScriptEdit::commentSelection
void commentSelection()
Definition: ScriptEdit.cpp:370
ScriptEdit::actionEval
QAction * actionEval
Definition: ScriptEdit.h:109
ScriptEdit::actionFindPrevious
QAction * actionFindPrevious
Definition: ScriptEdit.h:110
ScriptEdit::importASCII
QString importASCII(const QString &file=QString::null)
Definition: ScriptEdit.cpp:511
ScriptEdit::d_completer
QCompleter * d_completer
Definition: ScriptEdit.h:123
ParenthesisInfo::character
char character
Definition: ScriptEdit.h:150
ScriptEdit::d_fmt_default
QTextBlockFormat d_fmt_default
Format used for resetting success/failure markers.
Definition: ScriptEdit.h:118
ScriptEdit::actionReplace
QAction * actionReplace
Definition: ScriptEdit.h:110
ScriptEdit::clearErrorHighlighting
void clearErrorHighlighting()
Definition: ScriptEdit.cpp:788
scripted
Interface for maintaining a reference to the current ScriptingEnv.
Definition: Script.h:152
ScriptEdit::actionExport
QAction * actionExport
Definition: ScriptEdit.h:109
TextBlockData::insert
void insert(ParenthesisInfo *info)
Definition: ScriptEdit.h:161
ScriptEdit::insertErrorMsg
void insertErrorMsg(const QString &message)
Insert an error message from the scripting system at printCursor.
Definition: ScriptEdit.cpp:326
TextBlockData::TextBlockData
TextBlockData()
Definition: ScriptEdit.h:158
ScriptEdit::actionFind
QAction * actionFind
Definition: ScriptEdit.h:110
ScriptEdit::insertCompletion
void insertCompletion(const QString &completion)
Definition: ScriptEdit.cpp:645
ScriptEdit::d_highlighter
SyntaxHighlighter * d_highlighter
Definition: ScriptEdit.h:124
ScriptEdit::updateIndentation
void updateIndentation()
Definition: ScriptEdit.cpp:609
SyntaxHighlighter
Definition: PythonSyntaxHighlighter.h:44
ScriptEdit::actionFindNext
QAction * actionFindNext
Definition: ScriptEdit.h:110
ScriptEdit::scriptPrint
void scriptPrint(const QString &)
Definition: ScriptEdit.cpp:347
TextBlockData::parentheses
QVector< ParenthesisInfo * > parentheses()
Definition: ScriptEdit.h:160
ScriptEdit::functionsMenu
QMenu * functionsMenu
Submenu of context menu with mathematical functions.
Definition: ScriptEdit.h:112
ScriptEdit::actionExecuteAll
QAction * actionExecuteAll
Definition: ScriptEdit.h:109
ScriptEdit::rehighlight
void rehighlight()
Definition: ScriptEdit.cpp:719
ParenthesisInfo::position
int position
Definition: ScriptEdit.h:151
ScriptEdit::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
Definition: ScriptEdit.cpp:179
TextBlockData
Help class used for parentheses matching (code taken from Qt Quarterly Issue 31 · Q3 2009) ...
Definition: ScriptEdit.h:155
ScriptEdit::evaluate
void evaluate()
Definition: ScriptEdit.cpp:445
ScriptEdit::findPrevious
void findPrevious()
Definition: ScriptEdit.cpp:777
ScriptEdit::d_search_flags
QTextDocument::FindFlags d_search_flags
Definition: ScriptEdit.h:127
ScriptEdit::enableShortcuts
void enableShortcuts()
Definition: ScriptEdit.cpp:132