From 8e949e741bab0ee084444bb176a57ad61deb0f3f Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Fri, 15 Feb 2013 18:25:47 +0100 Subject: Add QQuickTextDocument 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 Reviewed-by: Jens Bache-Wiig --- src/quick/items/items.pri | 2 + src/quick/items/qquickitemsmodule.cpp | 4 + src/quick/items/qquicktextdocument.cpp | 71 ++++++++++++++++++ src/quick/items/qquicktextdocument.h | 86 +++++++++++++++++++++ src/quick/items/qquicktextedit.cpp | 20 +++++ src/quick/items/qquicktextedit_p.h | 5 ++ src/quick/items/qquicktextedit_p_p.h | 2 + tests/auto/quick/qquicktextdocument/data/text.qml | 6 ++ .../qquicktextdocument/qquicktextdocument.pro | 15 ++++ .../qquicktextdocument/tst_qquicktextdocument.cpp | 87 ++++++++++++++++++++++ tests/auto/quick/quick.pro | 1 + 11 files changed, 299 insertions(+) create mode 100644 src/quick/items/qquicktextdocument.cpp create mode 100644 src/quick/items/qquicktextdocument.h create mode 100644 tests/auto/quick/qquicktextdocument/data/text.qml create mode 100644 tests/auto/quick/qquicktextdocument/qquicktextdocument.pro create mode 100644 tests/auto/quick/qquicktextdocument/tst_qquicktextdocument.cpp diff --git a/src/quick/items/items.pri b/src/quick/items/items.pri index 2183a2bc63..fe406ecaac 100644 --- a/src/quick/items/items.pri +++ b/src/quick/items/items.pri @@ -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 \ diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index 71c23111d5..5321f5e588 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -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"); QQmlMetaType::setQQuickAnchorLineCompareFunction(compareQQuickAnchorLines); + qmlRegisterType(); + + qmlRegisterUncreatableType(uri,major,minor,"KeyNavigation",QQuickKeyNavigationAttached::tr("KeyNavigation is only available via attached properties")); qmlRegisterUncreatableType(uri,major,minor,"Keys",QQuickKeysAttached::tr("Keys is only available via attached properties")); qmlRegisterUncreatableType(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 index 0000000000..e29e48c5d0 --- /dev/null +++ b/src/quick/items/qquicktextdocument.cpp @@ -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 document; +}; + +QQuickTextDocument::QQuickTextDocument(QQuickItem *parent) + : QObject(*(new QQuickTextDocumentPrivate), parent) +{ + Q_D(QQuickTextDocument); + Q_ASSERT(parent); + Q_ASSERT(qobject_cast(parent)); + d->document = QPointer(qobject_cast(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 index 0000000000..25d3bbeaf0 --- /dev/null +++ b/src/quick/items/qquicktextdocument.h @@ -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 +#include + +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 diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index d543b6bd41..e051b5202c 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -61,6 +61,7 @@ #include #include +#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 diff --git a/src/quick/items/qquicktextedit_p.h b/src/quick/items/qquicktextedit_p.h index 0538270cd4..744a7e290d 100644 --- a/src/quick/items/qquicktextedit_p.h +++ b/src/quick/items/qquicktextedit_p.h @@ -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) diff --git a/src/quick/items/qquicktextedit_p_p.h b/src/quick/items/qquicktextedit_p_p.h index 597874d864..e4819e4d2d 100644 --- a/src/quick/items/qquicktextedit_p_p.h +++ b/src/quick/items/qquicktextedit_p_p.h @@ -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 index 0000000000..43a8c6bb82 --- /dev/null +++ b/tests/auto/quick/qquicktextdocument/data/text.qml @@ -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 index 0000000000..e6bfdbd099 --- /dev/null +++ b/tests/auto/quick/qquicktextdocument/qquicktextdocument.pro @@ -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 index 0000000000..717496cf0e --- /dev/null +++ b/tests/auto/quick/qquicktextdocument/tst_qquicktextdocument.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#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(o); + QVERIFY(edit); + + QQuickTextDocument* quickDocument = qobject_cast(edit->property("textDocument").value()); + 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" diff --git a/tests/auto/quick/quick.pro b/tests/auto/quick/quick.pro index 3ed6ca8c99..8ce17a10d0 100644 --- a/tests/auto/quick/quick.pro +++ b/tests/auto/quick/quick.pro @@ -60,6 +60,7 @@ QUICKTESTS = \ qquickshadereffect \ qquickspritesequence \ qquicktext \ + qquicktextdocument \ qquicktextedit \ qquicktextinput \ qquickvisualdatamodel \ -- cgit v1.2.3