diff options
| author | Jesper K. Pedersen <jesper.pedersen@kdab.com> | 2013-04-17 12:09:57 +0200 |
|---|---|---|
| committer | Jesper K. Pedersen <jesper.pedersen@kdab.com> | 2013-04-17 20:50:24 +0200 |
| commit | 487770e422e4c55e128c210bb82b30ed99f3f401 (patch) | |
| tree | 2827f7ec704ea06cc4de858eaf56f4ce68a8b35c | |
| parent | e793225595a8010d35506011ac2a915df8e21119 (diff) | |
Introduced the class TextEditor
This is to make it similar in heirarchy as the IEditor subclasses.
This commit also wraps QPoint and QRect so they are usable from the scripts
Change-Id: I214a7323cf6cc20cb9df509d0ea0515358248714
Reviewed-by: Nicolas Arnaud-Cormos <nicolas@kdab.com>
| -rw-r--r-- | enumwappers.cpp | 34 | ||||
| -rw-r--r-- | enumwappers.h | 14 | ||||
| -rw-r--r-- | objects/basetexteditor.cpp | 10 | ||||
| -rw-r--r-- | objects/basetexteditor.h | 6 | ||||
| -rw-r--r-- | objects/editor.cpp | 2 | ||||
| -rw-r--r-- | objects/editor.h | 2 | ||||
| -rw-r--r-- | objects/enums.h | 28 | ||||
| -rw-r--r-- | objects/texteditor.cpp | 75 | ||||
| -rw-r--r-- | objects/texteditor.h | 40 | ||||
| -rw-r--r-- | scripting.pro | 11 | ||||
| -rw-r--r-- | scriptrunner.cpp | 6 | ||||
| -rw-r--r-- | scriptwrappers.cpp | 48 | ||||
| -rw-r--r-- | scriptwrappers.h | 14 |
13 files changed, 276 insertions, 14 deletions
diff --git a/enumwappers.cpp b/enumwappers.cpp new file mode 100644 index 0000000..3bfe303 --- /dev/null +++ b/enumwappers.cpp @@ -0,0 +1,34 @@ +#include "enumwappers.h" +#include <QScriptEngine> +#include "objects/enums.h" + +namespace Scripting { +namespace Internal { + +#define createEnumStatics(CLASS,ENUM) \ +static QScriptValue toScriptValue##ENUM(QScriptEngine *engine, const CLASS::ENUM &p)\ + {\ + return engine->newVariant((int)p);\ + }\ +\ + static void fromScriptValue##ENUM(const QScriptValue &obj,CLASS::ENUM &s)\ + {\ + s = (CLASS::ENUM)obj.toInt32();\ + }\ + +#define registerEnum(ENGINE,CLASS,ENUM) \ +{\ + qScriptRegisterMetaType(ENGINE, toScriptValue##ENUM, fromScriptValue##ENUM);\ + QScriptValue metaObject = ENGINE->newQMetaObject( &CLASS::staticMetaObject );\ + ENGINE->globalObject().setProperty( QLatin1String(#ENUM), metaObject );\ +} + +createEnumStatics(Enums,PositionOperation) + +void registerEnums(QScriptEngine* engine ) +{ + registerEnum(engine, Enums, PositionOperation); +} + +} // namespace Internal +} // namespace Scripting diff --git a/enumwappers.h b/enumwappers.h new file mode 100644 index 0000000..1fe3c75 --- /dev/null +++ b/enumwappers.h @@ -0,0 +1,14 @@ +#ifndef SCRIPTING_INTERNAL_ENUMWAPPERS_H +#define SCRIPTING_INTERNAL_ENUMWAPPERS_H + +class QScriptEngine; + +namespace Scripting { +namespace Internal { + +void registerEnums(QScriptEngine* engine ); + +} // namespace Internal +} // namespace Scripting + +#endif // SCRIPTING_INTERNAL_ENUMWAPPERS_H diff --git a/objects/basetexteditor.cpp b/objects/basetexteditor.cpp index 3df3638..8bc0ded 100644 --- a/objects/basetexteditor.cpp +++ b/objects/basetexteditor.cpp @@ -38,12 +38,10 @@ #include <QTextCursor> -using namespace Scripting; using namespace Scripting::Internal; - BaseTextEditor::BaseTextEditor(QObject *parent) : - Editor(parent) + TextEditor(parent) { } @@ -406,10 +404,10 @@ QString BaseTextEditor::text() return QString(); } -TextEditor::BaseTextEditorWidget *BaseTextEditor::textEditorWidget() +::TextEditor::BaseTextEditorWidget * BaseTextEditor::textEditorWidget() { - TextEditor::BaseTextEditor *textEditor = - qobject_cast<TextEditor::BaseTextEditor*>(editor()); + ::TextEditor::BaseTextEditor *textEditor = + qobject_cast< ::TextEditor::BaseTextEditor* >(editor()); if (textEditor) return textEditor->editorWidget(); return 0; diff --git a/objects/basetexteditor.h b/objects/basetexteditor.h index 7ac9b75..1f8fe07 100644 --- a/objects/basetexteditor.h +++ b/objects/basetexteditor.h @@ -33,7 +33,7 @@ #ifndef TEXTEDITORWRAPPER_H #define TEXTEDITORWRAPPER_H -#include "editor.h" +#include "texteditor.h" namespace TextEditor { @@ -44,7 +44,7 @@ namespace TextEditor { namespace Scripting { namespace Internal { -class BaseTextEditor : public Editor +class BaseTextEditor : public TextEditor { Q_OBJECT public: @@ -115,7 +115,7 @@ public slots: QString text(); protected: - TextEditor::BaseTextEditorWidget *textEditorWidget(); + ::TextEditor::BaseTextEditorWidget *textEditorWidget(); }; } // namespace Internal diff --git a/objects/editor.cpp b/objects/editor.cpp index 7968394..0a32282 100644 --- a/objects/editor.cpp +++ b/objects/editor.cpp @@ -83,7 +83,7 @@ QString Editor::fileName() const return QString(); } -Core::IEditor * Editor::editor() +Core::IEditor * Editor::editor() const { return m_editor; } diff --git a/objects/editor.h b/objects/editor.h index eb6dc6b..91ae144 100644 --- a/objects/editor.h +++ b/objects/editor.h @@ -63,7 +63,7 @@ public slots: QString fileName() const; protected: - Core::IEditor *editor(); + virtual Core::IEditor *editor() const; friend class Editors; virtual void waitForInitialized() {}; diff --git a/objects/enums.h b/objects/enums.h new file mode 100644 index 0000000..54748cd --- /dev/null +++ b/objects/enums.h @@ -0,0 +1,28 @@ +#ifndef ENUMS_H +#define ENUMS_H + +#include <QObject> +#include <QMetaType> + +/** This class exports enums needed on the scripting side + + This is a placeholder for those enums that are in QtCreator (hence outside our control), + and which we do not have Q_ENUMS registered fo them. +*/ +class Enums :public QObject { + Q_OBJECT +public: + /** copied from ::TextEditor::ITextEditor::PositionOperation */ + enum PositionOperation { + Current = 1, + EndOfLine = 2, + StartOfLine = 3, + Anchor = 4, + EndOfDoc = 5 + }; + Q_ENUMS(PositionOperation) +}; + +Q_DECLARE_METATYPE(Enums::PositionOperation) + +#endif // ENUMS_H diff --git a/objects/texteditor.cpp b/objects/texteditor.cpp new file mode 100644 index 0000000..e919802 --- /dev/null +++ b/objects/texteditor.cpp @@ -0,0 +1,75 @@ +#include "texteditor.h" + +namespace Scripting { +namespace Internal { + +TextEditor::TextEditor(QObject *parent) : + Editor(parent) +{ +} + +int TextEditor::find(const QString &string) const +{ + // Doesn't seem to do much. + return editor()->find(string); +} + +int TextEditor::position(Enums::PositionOperation posOp, int at) const +{ + return editor()->position( static_cast< ::TextEditor::ITextEditor::PositionOperation>(posOp), at); +} + +QPoint TextEditor::convertPosition(int pos) const +{ + int line, column; + editor()->convertPosition(pos,&line,&column); + return QPoint(column,line); +} + +QRect TextEditor::cursorRect(int pos) const +{ + return editor()->cursorRect(pos); +} + +int TextEditor::columnCount() const +{ + return editor()->columnCount(); +} + +int TextEditor::rowCount() const +{ + return editor()->rowCount(); +} + +void TextEditor::remove(int length) +{ + editor()->remove(length); +} + +void TextEditor::insert(const QString &string) +{ + editor()->insert(string); +} + +void TextEditor::replace(int length, const QString &string) +{ + editor()->replace(length, string); +} + +void TextEditor::setCursorPosition(int pos) +{ + editor()->setCursorPosition(pos); +} + +void TextEditor::select(int toPos) +{ + editor()->select(toPos); +} + +::TextEditor::ITextEditor *TextEditor::editor() const +{ + return qobject_cast< ::TextEditor::ITextEditor*>(Editor::editor()); +} + +} // namespace Internal +} // namespace Scripting diff --git a/objects/texteditor.h b/objects/texteditor.h new file mode 100644 index 0000000..6900169 --- /dev/null +++ b/objects/texteditor.h @@ -0,0 +1,40 @@ +#ifndef SCRIPTING_INTERNAL_TEXTEDITOR_H +#define SCRIPTING_INTERNAL_TEXTEDITOR_H + +#include "editor.h" +#include <texteditor/itexteditor.h> +#include "enums.h" + +namespace Scripting { +namespace Internal { + +class TextEditor : public Editor +{ + Q_OBJECT +public: + + explicit TextEditor(QObject *parent = 0); + +public slots: + int find(const QString &string) const; + int position(Enums::PositionOperation posOp = Enums::Current, int at = -1) const; + QPoint convertPosition(int pos) const; + QRect cursorRect(int pos = -1) const; + int columnCount() const; + int rowCount() const; + + + void remove(int length); + void insert(const QString &string); + void replace(int length, const QString &string); + void setCursorPosition(int pos); + void select(int toPos); + +protected: + ::TextEditor::ITextEditor * editor() const; +}; + +} // namespace Internal +} // namespace Scripting + +#endif // SCRIPTING_INTERNAL_TEXTEDITOR_H diff --git a/scripting.pro b/scripting.pro index 55c3f0a..d7645ac 100644 --- a/scripting.pro +++ b/scripting.pro @@ -15,7 +15,10 @@ SOURCES += scriptingplugin.cpp \ objects/editor.cpp \ objects/basetexteditor.cpp \ objects/cppeditor.cpp \ - objects/signalwaiter.cpp + objects/signalwaiter.cpp \ + objects/texteditor.cpp \ + scriptwrappers.cpp \ + enumwappers.cpp HEADERS += scriptingplugin.h \ scripting_global.h \ @@ -28,7 +31,11 @@ HEADERS += scriptingplugin.h \ objects/editor.h \ objects/basetexteditor.h \ objects/cppeditor.h \ - objects/signalwaiter.h + objects/signalwaiter.h \ + objects/texteditor.h \ + scriptwrappers.h \ + objects/enums.h \ + enumwappers.h # Qt Creator linking diff --git a/scriptrunner.cpp b/scriptrunner.cpp index b982180..a02aaaf 100644 --- a/scriptrunner.cpp +++ b/scriptrunner.cpp @@ -41,6 +41,8 @@ #include <coreplugin/editormanager/ieditor.h> #include <wrap_helpers.h> +#include "scriptwrappers.h" +#include "enumwappers.h" using namespace Scripting; using namespace Scripting::Internal; @@ -75,6 +77,7 @@ bool ScriptRunner::runScript(const QString &sourceCode, const QString fileName) return !failed; } + ScriptRunner::QScriptEnginePtr ScriptRunner::ensureEngineInitialized() { if (!m_engine.isNull()) @@ -89,7 +92,8 @@ ScriptRunner::QScriptEnginePtr ScriptRunner::ensureEngineInitialized() registerGlobal(new Console, QLatin1String("console")); registerGlobal(new Editors, QLatin1String("editors")); - + registerWrappers(m_engine.data()); + registerEnums(m_engine.data()); return m_engine; } diff --git a/scriptwrappers.cpp b/scriptwrappers.cpp new file mode 100644 index 0000000..2111504 --- /dev/null +++ b/scriptwrappers.cpp @@ -0,0 +1,48 @@ +#include "scriptwrappers.h" +#include <QPoint> +#include <QRect> + +namespace Scripting { +namespace Internal { + +QScriptValue scriptValueFromQPoint(QScriptEngine *engine, const QPoint &point) +{ + QScriptValue obj = engine->newObject(); + obj.setProperty(QLatin1String("x"), point.x()); + obj.setProperty(QLatin1String("y"), point.y()); + return obj; +} + +void QPointFromScriptValue(const QScriptValue &obj, QPoint& point) +{ + point.setX( obj.property(QLatin1String("x")).toInt32() ); + point.setY( obj.property(QLatin1String("y")).toInt32() ); +} + +QScriptValue scriptValueFromQRect(QScriptEngine *engine, const QRect &rect) +{ + QScriptValue obj = engine->newObject(); + obj.setProperty(QLatin1String("x"), rect.x()); + obj.setProperty(QLatin1String("y"), rect.y()); + obj.setProperty(QLatin1String("width"), rect.width()); + obj.setProperty(QLatin1String("height"), rect.height()); + return obj; +} + +void QRectFromScriptValue(const QScriptValue &obj, QRect& rect) +{ + rect.setX( obj.property(QLatin1String("x")).toInt32() ); + rect.setY( obj.property(QLatin1String("y")).toInt32() ); + rect.setWidth( obj.property(QLatin1String("width")).toInt32() ); + rect.setHeight( obj.property(QLatin1String("height")).toInt32() ); +} + + +void registerWrappers(QScriptEngine* engine ) +{ + qScriptRegisterMetaType(engine, scriptValueFromQPoint, QPointFromScriptValue ); + qScriptRegisterMetaType(engine, scriptValueFromQRect, QRectFromScriptValue ); +} + +} // namespace Internal +} // namespace Scripting diff --git a/scriptwrappers.h b/scriptwrappers.h new file mode 100644 index 0000000..48d5882 --- /dev/null +++ b/scriptwrappers.h @@ -0,0 +1,14 @@ +#ifndef SCRIPTING_INTERNAL_SCRIPTWRAPPERS_H +#define SCRIPTING_INTERNAL_SCRIPTWRAPPERS_H + +#include <QScriptEngine> + +namespace Scripting { +namespace Internal { + +void registerWrappers(QScriptEngine* engine ); + +} // namespace Internal +} // namespace Scripting + +#endif // SCRIPTING_INTERNAL_SCRIPTWRAPPERS_H |
