File indexing completed on 2024-04-21 15:55:23

0001 /******************************************************************************
0002   Copyright (C) 2006-2008 by Michel Ludwig (michel.ludwig@kdemail.net)
0003                 2011-2012 by Holger Danielsson (holger.danielsson@versanet.de)
0004  ******************************************************************************/
0005 
0006 /**************************************************************************
0007 *                                                                         *
0008 *   This program is free software; you can redistribute it and/or modify  *
0009 *   it under the terms of the GNU General Public License as published by  *
0010 *   the Free Software Foundation; either version 2 of the License, or     *
0011 *   (at your option) any later version.                                   *
0012 *                                                                         *
0013 ***************************************************************************/
0014 
0015 #ifndef SCRIPT_H
0016 #define SCRIPT_H
0017 
0018 #include <QScriptEngine>
0019 #include <QScriptContext>
0020 #include <QMap>
0021 
0022 #include <QAction>
0023 #include <KTextEditor/View>
0024 
0025 
0026 class KileInfo;
0027 
0028 namespace KileScript {
0029 
0030 class KileScriptObject;
0031 class KileScriptView;
0032 class KileScriptDocument;
0033 
0034 ////////////////////////////// Script //////////////////////////////
0035 
0036 /**
0037  * This class represents a script.
0038  **/
0039 class Script {
0040 public:
0041     enum SequenceType {
0042         KEY_SEQUENCE = 0,
0043         KEY_SHORTCUT
0044     };
0045 
0046     /**
0047      * Constructs a new JavaScript script.
0048      * @param file the file that contains the script
0049      **/
0050     Script(unsigned int id, const QString& file);
0051     virtual ~Script() {}
0052 
0053     /**
0054      * Returns the code of this script, i.e. the file is read and its contents are
0055      * returned.
0056      **/
0057     QString getCode() const;
0058 
0059     /**
0060      * Returns the name of the script (the base name of the file).
0061      **/
0062     QString getName() const;
0063 
0064     /**
0065      * Returns the file of the script (the full path, including the base name).
0066      **/
0067     QString getFileName() const;
0068 
0069     /**
0070      * Returns the unique identifier of this script.
0071      **/
0072     unsigned int getID() const;
0073 
0074     /**
0075      * Sets the unique identifier of this script.
0076      **/
0077     void setID(unsigned int id);
0078 
0079 
0080     /**
0081      *
0082      **/
0083     void setActionObject(QAction * action);
0084 //      const QAction * getActionObject() const;
0085     QAction * getActionObject() const;
0086 
0087     void setKeySequence(const QString& str);
0088     QString getKeySequence() const;
0089 
0090     int getSequenceType() const;
0091     void setSequenceType(int type);
0092 
0093     static QString readFile(const QString &filename);
0094 
0095 private:
0096     unsigned int m_id;
0097     QString m_code;
0098     QString m_file;
0099     QString m_name;
0100     QAction *m_action;
0101     QString m_keySequence;
0102     int m_sequencetype;
0103 
0104 
0105 };
0106 
0107 ////////////////////////////// ScriptEnvironment //////////////////////////////
0108 
0109 
0110 /**
0111  * This class represents the environment that is used to execute Kile's scripts
0112  * in.
0113  **/
0114 class ScriptEnvironment {
0115 public:
0116     /**
0117      * Constructs a new environment.
0118      **/
0119     ScriptEnvironment(KileInfo *kileInfo, KileScriptView *scriptView, KileScriptDocument *scriptDocument,
0120                       KileScriptObject *scriptObject, const QString &pluginCode);
0121     virtual ~ScriptEnvironment();
0122 
0123     /**
0124      * Executes script code in this environment.
0125      * @param s the script that should be executed
0126      **/
0127     void execute(const Script *script);
0128 
0129 protected:
0130     KileInfo *m_kileInfo;
0131     KileScriptView *m_scriptView;
0132     KileScriptDocument *m_scriptDocument;
0133     KileScriptObject *m_kileScriptObject;
0134 
0135     QScriptEngine *m_engine;
0136     QString m_enginePluginCode;
0137 
0138     void scriptError(const QString &name);
0139 
0140 };
0141 
0142 ////////////////////////////// ScriptHelpers //////////////////////////////
0143 
0144 QScriptValue debug(QScriptContext *context, QScriptEngine *engine);
0145 
0146 }
0147 
0148 // metatype registration only necessary until KF5 5.9
0149 #include <ktexteditor_version.h>
0150 #if KTEXTEDITOR_VERSION < QT_VERSION_CHECK(5, 10, 0)
0151 Q_DECLARE_METATYPE(KTextEditor::Cursor)
0152 Q_DECLARE_METATYPE(KTextEditor::Range)
0153 #endif
0154 
0155 #endif