1 #ifndef Header_ScriptEngine 2 #define Header_ScriptEngine 3 4 #include "mostQtHeaders.h" 5 //#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) && (__GNUC__ >= 8) 6 //#pragma GCC diagnostic push 7 //#pragma GCC diagnostic ignored "-Wcast-function-type" 8 //#endif 9 10 #include <QJSEngine> 11 #include <QJSValueIterator> 12 #include <QQmlEngine> 13 14 //#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) && (__GNUC__ >= 8) 15 //#pragma GCC diagnostic pop 16 //#endif 17 18 #include "qeditor.h" 19 //#include "latexeditorview.h" 20 class BuildManager; 21 class Texstudio; 22 class ProcessX; 23 24 class LatexEditorView; 25 class Macro; 26 class scriptengine : public QObject 27 { 28 Q_OBJECT 29 30 public: 31 scriptengine(QObject *parent = nullptr); 32 ~scriptengine(); 33 void run(const bool quiet=false); 34 void setScript(const QString &script, bool allowWrite = false); 35 void setEditorView(LatexEditorView *edView); 36 37 static BuildManager *buildManager; 38 static Texstudio *app; 39 40 QStringList triggerMatches; 41 int triggerId; 42 43 static QList<Macro> *macros; 44 45 protected slots: 46 void insertSnippet(const QString& arg); 47 QJSValue replaceSelectedText(QJSValue replacementText,QJSValue options=QJSValue()); 48 QJSValue searchFunction(QJSValue searchFor, QJSValue arg1=QJSValue(), QJSValue arg2=QJSValue(), QJSValue arg3=QJSValue()); 49 QJSValue replaceFunction(QJSValue searchFor, QJSValue arg1=QJSValue(), QJSValue arg2=QJSValue(), QJSValue arg3=QJSValue()); 50 void alert(const QString& message); 51 void information(const QString &message); 52 void critical(const QString &message); 53 void warning(const QString &message); 54 bool confirm(const QString &message); 55 bool confirmWarning(const QString &message); 56 void debug(const QString &message); 57 58 #ifndef QT_NO_DEBUG 59 void crash_assert(); 60 #endif 61 void crash_sigsegv(); 62 void crash_sigfpe(); 63 void crash_stack(); 64 void crash_loop(); 65 void crash_throw(); 66 67 ProcessX *system(const QString &commandline, const QString &workingDirectory=QString()); 68 69 void writeFile(const QString &filename, const QString &content); 70 QVariant readFile(const QString &filename); 71 72 bool hasPersistent(const QString &name); 73 void setPersistent(const QString &name, const QVariant &value); 74 QVariant getPersistent(const QString &name); 75 76 void registerAsBackgroundScript(const QString &name = ""); 77 78 bool setTimeout(const QString &fun,const int timeout); 79 80 void save(const QString fn=""); 81 void saveCopy(const QString& fileName); 82 void runTimed(const QString fun); 83 84 protected: 85 QByteArray getScriptHash(); 86 void registerAllowedWrite(); 87 88 bool hasReadPrivileges(); 89 bool hasWritePrivileges(); 90 91 bool needReadPrivileges(const QString &fn, const QString ¶m); 92 bool needWritePrivileges(const QString &fn, const QString ¶m); 93 94 QJSEngine *engine; 95 96 QJSValue searchReplaceFunction(QJSValue searchText, QJSValue arg1, QJSValue arg2, QJSValue arg3, bool replace); 97 98 QPointer<LatexEditorView> m_editorView; 99 QPointer<QEditor> m_editor; 100 QString m_script; 101 bool m_allowWrite; 102 static int writeSecurityMode,readSecurityMode; 103 static QStringList privilegedReadScripts, privilegedWriteScripts; 104 }; 105 106 #include "universalinputdialog.h" 107 108 class UniversalInputDialogScript: public UniversalInputDialog 109 { 110 Q_OBJECT 111 112 public: 113 Q_INVOKABLE UniversalInputDialogScript(QWidget *parent = nullptr); 114 ~UniversalInputDialogScript(); 115 116 public slots: 117 QWidget* add(const QJSValue &def, const QJSValue &description, const QJSValue &id = QJSValue()); 118 119 QJSValue execDialog(); 120 121 QJSValue getAll(); 122 QVariant get(const QJSValue &id); 123 124 private: 125 QJSEngine *engine; 126 }; 127 128 #endif // SCRIPTENGINE_H 129