Add QQuickTextDocument
authorFrederik Gladhorn <frederik.gladhorn@digia.com>
Fri, 15 Feb 2013 17:25:47 +0000 (18:25 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 19 Feb 2013 22:22:33 +0000 (23:22 +0100)
Adding QQuickTextDocument as a way to
access the QTextDocument of QQuickTextEdit from C++.

This makes it possible to use a
QSyntaxHighlighter from C++ without exposing more
than one single qml property in the TextEdit.

Change-Id: If1790b591493adcb0b68aef0c8ca706e00450657
Reviewed-by: Alan Alpert <aalpert@rim.com>
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
src/quick/items/items.pri
src/quick/items/qquickitemsmodule.cpp
src/quick/items/qquicktextdocument.cpp [new file with mode: 0644]
src/quick/items/qquicktextdocument.h [new file with mode: 0644]
src/quick/items/qquicktextedit.cpp
src/quick/items/qquicktextedit_p.h
src/quick/items/qquicktextedit_p_p.h
tests/auto/quick/qquicktextdocument/data/text.qml [new file with mode: 0644]
tests/auto/quick/qquicktextdocument/qquicktextdocument.pro [new file with mode: 0644]
tests/auto/quick/qquicktextdocument/tst_qquicktextdocument.cpp [new file with mode: 0644]
tests/auto/quick/quick.pro

index 2183a2b..fe406ec 100644 (file)
@@ -21,6 +21,7 @@ HEADERS += \
     $$PWD/qquicktextinput_p_p.h \
     $$PWD/qquicktextcontrol_p.h \
     $$PWD/qquicktextcontrol_p_p.h \
+    $$PWD/qquicktextdocument.h \
     $$PWD/qquicktextedit_p.h \
     $$PWD/qquicktextedit_p_p.h \
     $$PWD/qquicktextutil_p.h \
@@ -85,6 +86,7 @@ SOURCES += \
     $$PWD/qquicktextnode.cpp \
     $$PWD/qquicktextinput.cpp \
     $$PWD/qquicktextcontrol.cpp \
+    $$PWD/qquicktextdocument.cpp \
     $$PWD/qquicktextedit.cpp \
     $$PWD/qquicktextutil.cpp \
     $$PWD/qquickimagebase.cpp \
index 71c2311..5321f5e 100644 (file)
@@ -49,6 +49,7 @@
 #include "qquicktext_p.h"
 #include "qquicktextinput_p.h"
 #include "qquicktextedit_p.h"
+#include "qquicktextdocument.h"
 #include "qquickimage_p.h"
 #include "qquickborderimage_p.h"
 #include "qquickscalegrid_p_p.h"
@@ -178,6 +179,9 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor)
     qRegisterMetaType<QQuickAnchorLine>("QQuickAnchorLine");
     QQmlMetaType::setQQuickAnchorLineCompareFunction(compareQQuickAnchorLines);
 
+    qmlRegisterType<QQuickTextDocument>();
+
+
     qmlRegisterUncreatableType<QQuickKeyNavigationAttached>(uri,major,minor,"KeyNavigation",QQuickKeyNavigationAttached::tr("KeyNavigation is only available via attached properties"));
     qmlRegisterUncreatableType<QQuickKeysAttached>(uri,major,minor,"Keys",QQuickKeysAttached::tr("Keys is only available via attached properties"));
     qmlRegisterUncreatableType<QQuickLayoutMirroringAttached>(uri,major,minor,"LayoutMirroring", QQuickLayoutMirroringAttached::tr("LayoutMirroring is only available via attached properties"));
diff --git a/src/quick/items/qquicktextdocument.cpp b/src/quick/items/qquicktextdocument.cpp
new file mode 100644 (file)
index 0000000..e29e48c
--- /dev/null
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQml module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qquicktextdocument.h"
+
+#include "qquicktextedit_p.h"
+#include "qquicktextedit_p_p.h"
+#include "qquicktext_p_p.h"
+
+QT_BEGIN_NAMESPACE
+
+class QQuickTextDocumentPrivate : public QObjectPrivate
+{
+public:
+    QPointer<QTextDocument> document;
+};
+
+QQuickTextDocument::QQuickTextDocument(QQuickItem *parent)
+    : QObject(*(new QQuickTextDocumentPrivate), parent)
+{
+    Q_D(QQuickTextDocument);
+    Q_ASSERT(parent);
+    Q_ASSERT(qobject_cast<QQuickTextEdit*>(parent));
+    d->document = QPointer<QTextDocument>(qobject_cast<QQuickTextEdit*>(parent)->d_func()->document);
+}
+
+QTextDocument* QQuickTextDocument::textDocument() const
+{
+    Q_D(const QQuickTextDocument);
+    return d->document.data();
+}
+
+QT_END_NAMESPACE
diff --git a/src/quick/items/qquicktextdocument.h b/src/quick/items/qquicktextdocument.h
new file mode 100644 (file)
index 0000000..25d3bbe
--- /dev/null
@@ -0,0 +1,86 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQml module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QQUICKTEXTDOCUMENT_H
+#define QQUICKTEXTDOCUMENT_H
+
+#include <QtGui/QTextDocument>
+#include <QtQuick/QQuickItem>
+
+QT_BEGIN_NAMESPACE
+
+/*!
+    \class QQuickTextDocument
+    \since 5.1
+    \brief The QQuickTextDocument class provides access to the QTextDocument of QQuickTextEdit
+    \inmodule QtQuick
+
+    This class provides access to the QTextDocument of QQuickTextEdit elements.
+    This is provided to allow usage of the \l{Rich Text Processing} functionalities of Qt.
+    You are not allowed to modify the document, but it can be used to output content, for example with \l{QTextDocumentWriter}),
+    or provide additional formatting, for example with \l{QSyntaxHighlighter}.
+
+    The class has to be used from C++ directly, using the property of the \l TextEdit.
+
+    Warning: The QTextDocument provided is used internally by QtQuick elements to provide text manipulation primitives.
+    You are not allowed to perform any modification of the internal state of the QTextDocument. If you do, the element
+    in question may stop functioning or crash.
+*/
+
+class QQuickTextDocumentPrivate;
+class Q_QUICK_EXPORT QQuickTextDocument : public QObject
+{
+    Q_OBJECT
+
+public:
+    QQuickTextDocument(QQuickItem *parent);
+    QTextDocument *textDocument() const;
+
+private:
+    Q_DISABLE_COPY(QQuickTextDocument)
+    Q_DECLARE_PRIVATE(QQuickTextDocument)
+};
+
+QT_END_NAMESPACE
+
+QML_DECLARE_TYPE(QQuickTextDocument)
+
+#endif
index d543b6b..e051b52 100644 (file)
@@ -61,6 +61,7 @@
 #include <private/qtextengine_p.h>
 #include <private/qsgadaptationlayer_p.h>
 
+#include "qquicktextdocument.h"
 
 QT_BEGIN_NAMESPACE
 
@@ -2179,4 +2180,23 @@ void QQuickTextEdit::remove(int start, int end)
     d->control->updateCursorRectangle(false);
 }
 
+/*!
+    \qmlproperty TextDocument QtQuick2::TextEdit::textDocument
+    \since 5.1
+
+    Returns the QQuickTextDocument of this TextEdit.
+    It can be used to implement syntax highlighting using
+    \l QSyntaxHighlighter.
+
+    \sa QQuickTextDocument
+*/
+
+QQuickTextDocument *QQuickTextEdit::textDocument()
+{
+    Q_D(QQuickTextEdit);
+    if (!d->quickDocument)
+        d->quickDocument = new QQuickTextDocument(this);
+    return d->quickDocument;
+}
+
 QT_END_NAMESPACE
index 0538270..744a7e2 100644 (file)
@@ -48,6 +48,7 @@
 
 QT_BEGIN_NAMESPACE
 
+class QQuickTextDocument;
 class QQuickTextEditPrivate;
 class Q_QUICK_PRIVATE_EXPORT QQuickTextEdit : public QQuickImplicitSizeItem
 {
@@ -99,6 +100,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickTextEdit : public QQuickImplicitSizeItem
 #endif
     Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl RESET resetBaseUrl NOTIFY baseUrlChanged)
     Q_PROPERTY(RenderType renderType READ renderType WRITE setRenderType NOTIFY renderTypeChanged)
+    Q_PROPERTY(QQuickTextDocument *textDocument READ textDocument FINAL)
 
 public:
     QQuickTextEdit(QQuickItem *parent=0);
@@ -247,6 +249,8 @@ public:
     Q_INVOKABLE QString getText(int start, int end) const;
     Q_INVOKABLE QString getFormattedText(int start, int end) const;
 
+    QQuickTextDocument *textDocument();
+
 Q_SIGNALS:
     void textChanged();
     void contentSizeChanged();
@@ -339,6 +343,7 @@ protected:
     QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData);
 
     friend class QQuickTextUtil;
+    friend class QQuickTextDocument;
 
 private:
     Q_DISABLE_COPY(QQuickTextEdit)
index 597874d..e4819e4 100644 (file)
@@ -73,6 +73,7 @@ public:
     QQuickTextEditPrivate()
         : color(QRgb(0xFF000000)), selectionColor(QRgb(0xFF000080)), selectedTextColor(QRgb(0xFFFFFFFF))
         , textMargin(0.0), xoff(0), yoff(0), font(sourceFont), cursorComponent(0), cursorItem(0), document(0), control(0)
+        , quickDocument(0)
         , lastSelectionStart(0), lastSelectionEnd(0), lineCount(0)
         , hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop)
         , format(QQuickTextEdit::PlainText), wrapMode(QQuickTextEdit::NoWrap)
@@ -124,6 +125,7 @@ public:
     QQuickItem* cursorItem;
     QQuickTextDocumentWithImageResources *document;
     QQuickTextControl *control;
+    QQuickTextDocument *quickDocument;
 
     int lastSelectionStart;
     int lastSelectionEnd;
diff --git a/tests/auto/quick/qquicktextdocument/data/text.qml b/tests/auto/quick/qquicktextdocument/data/text.qml
new file mode 100644 (file)
index 0000000..43a8c6b
--- /dev/null
@@ -0,0 +1,6 @@
+import QtQuick 2.1
+
+TextEdit {
+    text: ""
+}
+
diff --git a/tests/auto/quick/qquicktextdocument/qquicktextdocument.pro b/tests/auto/quick/qquicktextdocument/qquicktextdocument.pro
new file mode 100644 (file)
index 0000000..e6bfdbd
--- /dev/null
@@ -0,0 +1,15 @@
+CONFIG += testcase
+TARGET = tst_qquicktextdocument
+macx:CONFIG -= app_bundle
+
+SOURCES += tst_qquicktextdocument.cpp
+
+include (../../shared/util.pri)
+
+TESTDATA = data/*
+
+CONFIG += parallel_test
+
+QT += core-private gui-private qml-private quick-private testlib
+DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
+
diff --git a/tests/auto/quick/qquicktextdocument/tst_qquicktextdocument.cpp b/tests/auto/quick/qquicktextdocument/tst_qquicktextdocument.cpp
new file mode 100644 (file)
index 0000000..717496c
--- /dev/null
@@ -0,0 +1,87 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQml module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <qtest.h>
+#include <QtTest/QtTest>
+#include <QtQuick/QQuickTextDocument>
+#include <QtQuick/QQuickItem>
+#include <QtQuick/private/qquicktextedit_p.h>
+#include <QtGui/QTextDocument>
+#include <QtGui/QTextDocumentWriter>
+#include <QtQml/QQmlEngine>
+#include <QtQml/QQmlComponent>
+#include "../../shared/util.h"
+
+class tst_qquicktextdocument : public QQmlDataTest
+{
+    Q_OBJECT
+private slots:
+    void textDocumentWriter();
+};
+
+QString text = QStringLiteral("foo bar");
+
+void tst_qquicktextdocument::textDocumentWriter()
+{
+    QQmlEngine e;
+    QQmlComponent c(&e, testFileUrl("text.qml"));
+    QObject* o = c.create();
+    QVERIFY(o);
+    QQuickTextEdit *edit = qobject_cast<QQuickTextEdit*>(o);
+    QVERIFY(edit);
+
+    QQuickTextDocument* quickDocument = qobject_cast<QQuickTextDocument*>(edit->property("textDocument").value<QObject*>());
+    QVERIFY(quickDocument->textDocument() != 0);
+
+    QBuffer output;
+    output.open(QBuffer::ReadWrite);
+    QVERIFY(output.buffer().isEmpty());
+
+    edit->setProperty("text", QVariant(text));
+    QTextDocumentWriter writer(&output, "plaintext");
+    QVERIFY(writer.write(quickDocument->textDocument()));
+    QCOMPARE(output.buffer(), text.toLatin1());
+    delete o;
+}
+
+QTEST_MAIN(tst_qquicktextdocument)
+
+#include "tst_qquicktextdocument.moc"
index 3ed6ca8..8ce17a1 100644 (file)
@@ -60,6 +60,7 @@ QUICKTESTS =  \
     qquickshadereffect \
     qquickspritesequence \
     qquicktext \
+    qquicktextdocument \
     qquicktextedit \
     qquicktextinput \
     qquickvisualdatamodel \