diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -99,6 +99,7 @@ script/katescriptaction.cpp # scripting wrappers +script/katescripteditor.cpp script/katescriptdocument.cpp script/katescriptview.cpp script/katescripthelpers.cpp diff --git a/src/script/katescript.h b/src/script/katescript.h --- a/src/script/katescript.h +++ b/src/script/katescript.h @@ -29,6 +29,7 @@ namespace KTextEditor { class ViewPrivate; } +class KateScriptEditor; class KateScriptDocument; class KateScriptView; @@ -194,10 +195,10 @@ private: /** Whether or not there has been a call to load */ - bool m_loaded; + bool m_loaded = false; /** Whether or not the script loaded successfully into memory */ - bool m_loadSuccessful; + bool m_loadSuccessful = false; /** The script's URL */ QString m_url; @@ -207,15 +208,16 @@ protected: /** The Qt interpreter for this script */ - QJSEngine *m_engine; + QJSEngine *m_engine = nullptr; private: /** general header data */ KateScriptHeader m_generalHeader; - /** document/view wrapper objects */ - KateScriptDocument *m_document; - KateScriptView *m_view; + /** wrapper objects */ + KateScriptEditor *m_editor = nullptr; + KateScriptDocument *m_document = nullptr; + KateScriptView *m_view = nullptr; private: /** if input is script or url**/ diff --git a/src/script/katescript.cpp b/src/script/katescript.cpp --- a/src/script/katescript.cpp +++ b/src/script/katescript.cpp @@ -19,6 +19,7 @@ // Boston, MA 02110-1301, USA. #include "katescript.h" +#include "katescripteditor.h" #include "katescriptdocument.h" #include "katescriptview.h" #include "katescripthelpers.h" @@ -37,12 +38,7 @@ #include KateScript::KateScript(const QString &urlOrScript, enum InputType inputType) - : m_loaded(false) - , m_loadSuccessful(false) - , m_url(inputType == InputURL ? urlOrScript : QString()) - , m_engine(nullptr) - , m_document(nullptr) - , m_view(nullptr) + : m_url(inputType == InputURL ? urlOrScript : QString()) , m_inputType(inputType) , m_script(inputType == InputSCRIPT ? urlOrScript : QString()) { @@ -52,6 +48,7 @@ { if (m_loadSuccessful) { // remove data... + delete m_editor; delete m_document; delete m_view; delete m_engine; @@ -183,6 +180,7 @@ } // AFTER SCRIPT: set the view/document objects as necessary + m_engine->globalObject().setProperty(QStringLiteral("editor"), m_engine->newQObject(m_editor = new KateScriptEditor(m_engine))); m_engine->globalObject().setProperty(QStringLiteral("document"), m_engine->newQObject(m_document = new KateScriptDocument(m_engine))); m_engine->globalObject().setProperty(QStringLiteral("view"), m_engine->newQObject(m_view = new KateScriptView(m_engine))); diff --git a/src/script/katescripteditor.h b/src/script/katescripteditor.h new file mode 100644 --- /dev/null +++ b/src/script/katescripteditor.h @@ -0,0 +1,50 @@ +/* This file is part of the KDE libraries. + * + * Copyright (C) 2017 Dominik Haumann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifndef KATE_SCRIPT_EDITOR_H +#define KATE_SCRIPT_EDITOR_H + +#include +#include + +#include + +namespace KTextEditor { class ViewPrivate; } +class QJSEngine; + +/** + * This class wraps the global editor instance KateGlobal, exposing some + * helper methods such as the clipboard history etc. + */ +class KTEXTEDITOR_EXPORT KateScriptEditor : public QObject +{ + Q_OBJECT + +public: + KateScriptEditor(QJSEngine *engine, QObject *parent = nullptr); + + Q_INVOKABLE QString clipboardText() const; + Q_INVOKABLE QStringList clipboardHistory() const; + Q_INVOKABLE void setClipboardText(const QString &text); + +private: + QJSEngine *m_engine = nullptr; +}; + +#endif diff --git a/src/script/katescripteditor.cpp b/src/script/katescripteditor.cpp new file mode 100644 --- /dev/null +++ b/src/script/katescripteditor.cpp @@ -0,0 +1,50 @@ +/* This file is part of the KDE libraries. + * + * Copyright (C) 2017 Dominik Haumann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "katescripteditor.h" + +#include "kateglobal.h" + +#include +#include +#include + +using KTextEditor::EditorPrivate; + +KateScriptEditor::KateScriptEditor(QJSEngine *engine, QObject *parent) + : QObject(parent) + , m_engine(engine) +{ +} + +QString KateScriptEditor::clipboardText() const +{ + return QApplication::clipboard()->text(); +} + +QStringList KateScriptEditor::clipboardHistory() const +{ + return KTextEditor::EditorPrivate::self()->clipboardHistory(); +} + +void KateScriptEditor::setClipboardText(const QString &text) +{ + KTextEditor::EditorPrivate::self()->copyToClipboard(text); +}