Export editor controller class
authorKevin Krammer <kevin.krammer@kdab.com>
Tue, 6 Nov 2012 07:51:14 +0000 (08:51 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Wed, 7 Nov 2012 18:54:21 +0000 (19:54 +0100)
examples/text-editor/editor.cpp [new file with mode: 0644]
examples/text-editor/editor.h [new file with mode: 0644]
examples/text-editor/main.cpp
examples/text-editor/main.qml
examples/text-editor/text-editor.pro

diff --git a/examples/text-editor/editor.cpp b/examples/text-editor/editor.cpp
new file mode 100644 (file)
index 0000000..f14e868
--- /dev/null
@@ -0,0 +1,20 @@
+#include "editor.h"
+
+#include <QTextDocument>
+
+Editor::Editor(QObject *parent)
+  : QObject(parent)
+  , m_document(new QTextDocument)
+{
+}
+
+Editor::~Editor()
+{
+  delete m_document;
+}
+
+QTextDocument *Editor::document() const
+{
+  return m_document;
+}
+
diff --git a/examples/text-editor/editor.h b/examples/text-editor/editor.h
new file mode 100644 (file)
index 0000000..9080db2
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef EDITOR_H
+#define EDITOR_H
+
+#include <QObject>
+
+class QTextDocument;
+
+class Editor : public QObject
+{
+  Q_OBJECT
+  Q_PROPERTY(QTextDocument* document READ document CONSTANT)
+
+  public:
+    explicit Editor(QObject *parent = 0);
+    ~Editor();
+    
+    QTextDocument *document() const;
+
+  private:
+    QTextDocument *m_document;
+};
+
+#endif // EDITOR_H
index bb72b1e..f2f20c6 100644 (file)
@@ -18,6 +18,8 @@
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
 
+#include "editor.h"
+
 #include "declarativewidgetsdocument.h"
 
 #include <QApplication>
@@ -34,6 +36,9 @@ int main(int argc, char **argv)
   DeclarativeWidgetsDocument document(documentUrl);
   QObject::connect(document.engine(), SIGNAL(quit()), &app, SLOT(quit()));
 
+  Editor editor;
+  document.setContextProperty("_editor", &editor);
+
   QWidget *widget = document.create<QWidget>();
   if (!widget)
     qFatal("Failed to create widget from document");
index c31671c..03316ed 100644 (file)
@@ -1,3 +1,4 @@
+import QtQuick 1.0
 import QtGui 1.0
 
 MainWindow {
@@ -141,4 +142,6 @@ MainWindow {
       text: "Pos:"
     }
   }
+
+  Component.onCompleted: textEdit.document = _editor.document
 }
index d960ab0..7a9e6f3 100644 (file)
@@ -9,10 +9,14 @@ QT += declarative
 
 # Input
 SOURCES += \
-    main.cpp
+    main.cpp \
+    editor.cpp
 
 RESOURCES += \
     text-editor.qrc
 
 OTHER_FILES += \
     main.qml
+
+HEADERS += \
+    editor.h