[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: [PATCH] KoTextDocument
From:       Girish Ramakrishnan <girish () forwardbias ! in>
Date:       2008-08-29 8:43:54
Message-ID: 48B7B3FA.5050605 () forwardbias ! in
[Download RAW message or body]

Girish Ramakrishnan wrote:
> Hi Guys,
> Currently, we have no place to store meta data of the text shape's
> QTextDocument. Currently, we have the style manager residing in the
> layout which is not an ideal place. I also have an upcoming series of
> patches for support of automatic list styles and some redesign of
> KoListStyle. All this meta data needs to go somewhere. (Currently, I
> have stashed automatic list styles in KoStyleManager which is incorrect).
> 
> My initial thoughts were to have a KoTextDocument that subclasses
> QTextDocument that store all this information. Thomas pointed out that
> it would be nice to stick in all information in a QTextDocument somehow.
> So, the approach chosen, is to store all the meta data as resources
> using QTextDocument::addResource(). To have a nice mechanism to access
> them, KoTextDocument is just a wrapper on top of QTextDocument.
> 
> Currently, the patch only stores the style manager but more will follow.
> 
> Any important thing to know is that we should now use
> QTextDocument::clear() with care since it will clear away the resources
> too. If you want to only clear the contents, one must use a QTextCursor
> to select all + clear selection.
> 
> Any comments on the patch before I commit away? I did consider renaming
>  KoTextDocument to KoTextDocumentResource (or similar) but I foresee
> putting helper functions for lists (and maybe other things) in this class.
> 

The diffs don't have KoTextDocument.cpp/.h. Look for them in the
attached patch.

Girish

["kotextdocument.patch" (text/x-diff)]

commit 8117cd389e34d8bd38aae737fd44003e82d44ec9
Author: Girish Ramakrishnan <girish@forwardbias.in>
Date:   Fri Aug 29 13:43:51 2008 +0530

    Say hello to KoTextDocument.
    
    Currently, we have no place to store meta data of the text shape's
    QTextDocument. We have the style manager residing in the
    layout which is not an ideal place. A place to store automatic
    list styles is also needed.
    
    My initial thoughts were to have a KoTextDocument that subclasses
    QTextDocument that stores all this information. Thomas pointed out that
    it would be nice to stick in all information in a QTextDocument somehow.
    So, the approach chosen, is to store all the meta data as resources
    using QTextDocument::addResource(). To have a nice mechanism to access
    them, KoTextDocument is just a wrapper on top of QTextDocument.
    
    Currently, the patch only stores the style manager but more will follow.
    
    An important thing to know is that we should now use
    QTextDocument::clear() with care since it will clear away the resources
    too. If you want to only clear the contents, one must use a QTextCursor
    to select all + clear selection.

diff --git a/kword/part/KWDLoader.cpp b/kword/part/KWDLoader.cpp
index a94f709..1fe286e 100644
--- a/kword/part/KWDLoader.cpp
+++ b/kword/part/KWDLoader.cpp
@@ -550,9 +550,10 @@ void KWDLoader::fill(KWTextFrameSet *fs, const KoXmlElement \
&framesetElem)  if (framesetElem.hasAttribute("protectContent"))
         fs->setProtectContent((bool)framesetElem.attribute("protectContent").toInt());
  
-    fs->document()->clear(); // Get rid of dummy paragraph (and more if any)
-
     QTextCursor cursor(fs->document());
+    cursor.select(QTextCursor::Document);
+    cursor.removeSelectedText();
+
     // <PARAGRAPH>
     bool firstParag = true;
     KoXmlElement paragraph;
diff --git a/kword/part/frames/KWTextFrameSet.cpp \
b/kword/part/frames/KWTextFrameSet.cpp index ccfcebe..1a83ba4 100644
--- a/kword/part/frames/KWTextFrameSet.cpp
+++ b/kword/part/frames/KWTextFrameSet.cpp
@@ -29,6 +29,7 @@
 #include <KoTextShapeData.h>
 #include <KoStyleManager.h>
 #include <KoParagraphStyle.h>
+#include <KoTextDocument.h>
 
 #include <klocale.h>
 #include <kdebug.h>
@@ -49,7 +50,7 @@ KWTextFrameSet::KWTextFrameSet(const KWDocument *doc)
         layout->setInlineObjectTextManager(m_kwordDocument->inlineTextObjectManager());
                
         KoStyleManager *styleManager = dynamic_cast<KoStyleManager \
*>(m_kwordDocument->dataCenterMap()["StyleManager"]);  Q_ASSERT(styleManager);
-        layout->setStyleManager(styleManager);
+        KoTextDocument(m_document).setStyleManager(styleManager);
     }
     m_document->setDocumentLayout(layout);
     m_document->setUseDesignMetrics(true);
@@ -69,7 +70,7 @@ KWTextFrameSet::KWTextFrameSet(const KWDocument *doc, \
                KWord::TextFrameSetType ty
         layout->setInlineObjectTextManager(m_kwordDocument->inlineTextObjectManager());
                
         KoStyleManager *styleManager = dynamic_cast<KoStyleManager \
*>(m_kwordDocument->dataCenterMap()["StyleManager"]);  Q_ASSERT(styleManager);
-        layout->setStyleManager(styleManager);
+        KoTextDocument(m_document).setStyleManager(styleManager);
     }
     m_document->setDocumentLayout(layout);
     m_document->setUseDesignMetrics(true);
@@ -115,7 +116,7 @@ void KWTextFrameSet::setupFrame(KWFrame *frame)
             layout->setInlineObjectTextManager(m_kwordDocument->inlineTextObjectManager());
                
             KoStyleManager *styleManager = dynamic_cast<KoStyleManager \
*>(m_kwordDocument->dataCenterMap()["StyleManager"]);  Q_ASSERT(styleManager);
-            layout->setStyleManager(styleManager);
+            KoTextDocument(m_document).setStyleManager(styleManager);
         }
         m_document->setDocumentLayout(layout);
         data->setDocument(m_document, false);
diff --git a/kword/part/frames/tests/TestDocumentLayout.cpp \
b/kword/part/frames/tests/TestDocumentLayout.cpp index 56a7f36..9f961e1 100644
--- a/kword/part/frames/tests/TestDocumentLayout.cpp
+++ b/kword/part/frames/tests/TestDocumentLayout.cpp
@@ -8,7 +8,7 @@
 #include <KoListStyle.h>
 #include <KoTextBlockData.h>
 #include <KoStyleManager.h>
-
+#include <KoTextDocument.h>
 
 #include <kdebug.h>
 #include <kcomponentdata.h>
@@ -40,7 +40,7 @@ void TestDocumentLayout::initForNewTest(const QString &initText)
     layout = dynamic_cast<KWTextDocumentLayout*>(doc->documentLayout());
     Q_ASSERT(layout);
     styleManager = new KoStyleManager();
-    layout->setStyleManager(styleManager);
+    KoTextDocument(doc).setStyleManager(styleManager);
 
     QTextBlock block = doc->begin();
     if (initText.length() > 0) {
diff --git a/libs/kotext/CMakeLists.txt b/libs/kotext/CMakeLists.txt
index 03556a3..5beb9b6 100644
--- a/libs/kotext/CMakeLists.txt
+++ b/libs/kotext/CMakeLists.txt
@@ -28,6 +28,7 @@ set(kotext_LIB_SRCS ${libkohyphen_SRCS}
     KoTextDocumentLayout.cpp
     KoTextOdfSaveHelper.cpp
     KoTextPaste.cpp
+    KoTextDocument.cpp
 
     KoTextEditingRegistry.cpp
     KoTextEditingFactory.cpp
diff --git a/libs/kotext/KoTextDebug.cpp b/libs/kotext/KoTextDebug.cpp
index c449a3c..c20fd3d 100644
--- a/libs/kotext/KoTextDebug.cpp
+++ b/libs/kotext/KoTextDebug.cpp
@@ -30,6 +30,7 @@
 #include "styles/KoCharacterStyle.h"
 #include "styles/KoListStyle.h"
 #include "styles/KoStyleManager.h"
+#include "KoTextDocument.h"
 #include "KoTextBlockData.h"
 #include <KoTextDocumentLayout.h>
 #include <KoInlineTextObjectManager.h>
@@ -151,8 +152,6 @@ QString KoTextDebug::textAttributes(const QTextCharFormat \
&textFormat)  {
     QString attrs;
 
-    KoTextDocumentLayout *lay = document ? dynamic_cast<KoTextDocumentLayout \
                *>(document->documentLayout()) : 0;
-
     QTextImageFormat imageFormat = textFormat.toImageFormat();
 
     if (imageFormat.isValid()) {
@@ -160,9 +159,10 @@ QString KoTextDebug::textAttributes(const QTextCharFormat \
&textFormat)  return attrs;
     }
 
-    if (lay && lay->styleManager()) {
+    KoStyleManager *styleManager = KoTextDocument(document).styleManager();
+    if (styleManager) {
         int id = textFormat.intProperty(KoCharacterStyle::StyleId);
-        KoCharacterStyle *characterStyle = lay->styleManager()->characterStyle(id);
+        KoCharacterStyle *characterStyle = styleManager->characterStyle(id);
         attrs.append(" characterStyle=\"id:").append(QString::number(id));
         if (characterStyle)
             attrs.append(" name:").append(characterStyle->name());
@@ -287,10 +287,10 @@ QString KoTextDebug::paraAttributes(const KoParagraphStyle \
&style)  QString KoTextDebug::paraAttributes(const QTextBlockFormat &blockFormat)
 {
     QString attrs;
-    KoTextDocumentLayout *lay = document ? dynamic_cast<KoTextDocumentLayout \
                *>(document->documentLayout()) : 0;
-    if (lay && lay->styleManager()) {
+    KoStyleManager *styleManager = KoTextDocument(document).styleManager();
+    if (styleManager) {
         int id = blockFormat.intProperty(KoParagraphStyle::StyleId);
-        KoParagraphStyle *paragraphStyle = lay->styleManager()->paragraphStyle(id);
+        KoParagraphStyle *paragraphStyle = styleManager->paragraphStyle(id);
         attrs.append(" paragraphStyle=\"id:").append(QString::number(id));
         if (paragraphStyle)
             attrs.append(" name:").append(paragraphStyle->name());
@@ -407,10 +407,10 @@ QString KoTextDebug::paraAttributes(const QTextBlockFormat \
&blockFormat)  QString KoTextDebug::listAttributes(const QTextListFormat &listFormat)
 {
     QString attrs;
-    KoTextDocumentLayout *lay = document ? dynamic_cast<KoTextDocumentLayout \
                *>(document->documentLayout()) : 0;
-    if (lay && lay->styleManager()) {
+    KoStyleManager *styleManager = KoTextDocument(document).styleManager();
+    if (styleManager) {
         int id = listFormat.intProperty(KoListStyle::StyleId);
-        KoListStyle *listStyle = lay->styleManager()->listStyle(id);
+        KoListStyle *listStyle = styleManager->listStyle(id);
         attrs.append(" listStyle=\"id:").append(QString::number(id));
         if (listStyle)
             attrs.append(" name:").append(listStyle->name());
diff --git a/libs/kotext/KoTextDocument.cpp b/libs/kotext/KoTextDocument.cpp
new file mode 100644
index 0000000..83f578d
--- /dev/null
+++ b/libs/kotext/KoTextDocument.cpp
@@ -0,0 +1,71 @@
+/* This file is part of the KDE project
+ * Copyright (C) 2008 Girish Ramakrishnan <girish@forwardbias.in>
+ *
+ * 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 <QTextDocument>
+#include <QTextCursor>
+#include <QUrl>
+#include <QVariant>
+
+#include <kdebug.h>
+
+#include "styles/KoStyleManager.h"
+#include "KoTextDocument.h"
+
+class KoTextDocument::Private
+{
+public:
+    QTextDocument *document;
+};
+
+KoTextDocument::KoTextDocument(QTextDocument *document)
+: d(new KoTextDocument::Private)
+{
+    d->document = document;
+}
+
+KoTextDocument::KoTextDocument(const QTextDocument *document)
+: d(new KoTextDocument::Private)
+{
+    d->document = const_cast<QTextDocument *>(document);
+}
+
+QTextDocument *KoTextDocument::document() const
+{
+    return d->document;
+}
+
+void KoTextDocument::setStyleManager(KoStyleManager *sm)
+{
+    QVariant v;
+    v.setValue(sm);
+    d->document->addResource(KoTextDocument::StyleManager, QUrl(), v);
+}
+
+KoStyleManager *KoTextDocument::styleManager() const
+{
+    QVariant resource = d->document->resource(KoTextDocument::StyleManager, QUrl());
+    return resource.value<KoStyleManager *>();
+}
+
+void KoTextDocument::clearContents()
+{
+    QTextCursor cursor(d->document);
+    cursor.select(QTextCursor::Document);
+    cursor.removeSelectedText();
+}
diff --git a/libs/kotext/KoTextDocument.h b/libs/kotext/KoTextDocument.h
new file mode 100644
index 0000000..1fdb045
--- /dev/null
+++ b/libs/kotext/KoTextDocument.h
@@ -0,0 +1,49 @@
+/* This file is part of the KDE project
+ * Copyright (C) 2008 Girish Ramakrishnan <girish@forwardbias.in>
+ *
+ * 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 KOTEXTDOCUMENT_H
+#define KOTEXTDOCUMENT_H
+
+#include <QTextDocument>
+
+class KoStyleManager;
+
+class KOTEXT_EXPORT KoTextDocument
+{
+public:
+    KoTextDocument(QTextDocument *document);
+    KoTextDocument(const QTextDocument *document);
+
+    enum ResourceType {
+        StyleManager = QTextDocument::UserResource
+    };
+
+    QTextDocument *document() const;
+
+    void setStyleManager(KoStyleManager *styleManager);
+    KoStyleManager *styleManager() const;
+
+    void clearContents();
+
+private:
+    class Private;
+    Private *const d;
+};
+
+#endif // KOTEXTDOCUMENT_H
diff --git a/libs/kotext/KoTextDocumentLayout.cpp \
b/libs/kotext/KoTextDocumentLayout.cpp index 1aadf3a..205e382 100644
--- a/libs/kotext/KoTextDocumentLayout.cpp
+++ b/libs/kotext/KoTextDocumentLayout.cpp
@@ -39,7 +39,7 @@
 class LayoutStateDummy : public KoTextDocumentLayout::LayoutState
 {
 public:
-    LayoutStateDummy() : m_styleManager(0) {}
+    LayoutStateDummy() {}
     bool start() {
         return false;
     }
@@ -76,12 +76,7 @@ public:
         return 0;
     }
     void draw(QPainter *, const KoTextDocumentLayout::PaintContext &) {}
-    KoStyleManager *styleManager() const {
-        return m_styleManager;
-    }
-    void setStyleManager(KoStyleManager *sm) {
-        m_styleManager = sm;
-    }
+
     bool setFollowupShape(KoShape *) {
         return false;
     }
@@ -90,8 +85,6 @@ public:
         return 0;
     }
     void registerInlineObject(const QTextInlineObject &) {}
-
-    KoStyleManager *m_styleManager;
 };
 
 class KoTextDocumentLayout::Private
@@ -138,12 +131,7 @@ KoTextDocumentLayout::~KoTextDocumentLayout()
 void KoTextDocumentLayout::setLayout(LayoutState *layout)
 {
     Q_ASSERT(layout);
-    KoStyleManager *sm = 0;
-    if (m_state)
-        sm = m_state->styleManager();
-    delete m_state;
     m_state = layout;
-    m_state->setStyleManager(sm);
     scheduleLayout();
 }
 
@@ -167,16 +155,6 @@ void KoTextDocumentLayout::addShape(KoShape *shape)
     }
 }
 
-void KoTextDocumentLayout::setStyleManager(KoStyleManager *sm)
-{
-    m_state->setStyleManager(sm);
-    if (document()->isEmpty()) {
-        QTextBlock block = document()->begin();
-        if (block.blockFormat().intProperty(KoParagraphStyle::StyleId) < 100)
-            sm->defaultParagraphStyle()->applyStyle(block);
-    }
-}
-
 void KoTextDocumentLayout::setInlineObjectTextManager(KoInlineTextObjectManager \
*iom)  {
     d->inlineTextObjectManager = iom;
@@ -408,11 +386,6 @@ QList<KoShape*> KoTextDocumentLayout::shapes() const
     return d->shapes;
 }
 
-KoStyleManager *KoTextDocumentLayout::styleManager() const
-{
-    return m_state->styleManager();
-}
-
 KoShape* KoTextDocumentLayout::shapeForPosition(int position) const
 {
     foreach(KoShape *shape, shapes()) {
diff --git a/libs/kotext/KoTextDocumentLayout.h b/libs/kotext/KoTextDocumentLayout.h
index 2085aa1..e1c4176 100644
--- a/libs/kotext/KoTextDocumentLayout.h
+++ b/libs/kotext/KoTextDocumentLayout.h
@@ -57,13 +57,6 @@ public:
     explicit KoTextDocumentLayout(QTextDocument *document, \
KoTextDocumentLayout::LayoutState *layout = 0);  virtual ~KoTextDocumentLayout();
 
-    /**
-     * While the text document is standalone, the text can refer to the character
-     * and paragraph styles, and doing so is needed in doing proper text-layout.
-     * Setting the stylemanager on this layouter is therefor required if there is \
                one.
-     */
-    void setStyleManager(KoStyleManager *sm);
-
     /// set the layoutState for this document layout
     void setLayout(LayoutState *layout);
 
@@ -123,9 +116,6 @@ public:
     /// return the list of shapes that will be used to run all the text into.
     virtual QList<KoShape*> shapes() const;
 
-    /// return the current styleManager.  Can be 0 if none set.
-    KoStyleManager *styleManager() const;
-
     /**
      * This inner class is an interface that allows the KoTextDocumentLayout to do \
                rough layout
      * while the LayoutState implementation can do all the boring details.
@@ -190,13 +180,6 @@ public:
         KoShape *shape;
         /// The current paragraph layout.
         QTextLayout *layout;
-
-    protected:
-        friend class KoTextDocumentLayout;
-        /// see KoTextDocumentLayout::setStyleManager()
-        virtual void setStyleManager(KoStyleManager *sm) = 0;
-        /// see KoTextDocumentLayout::styleManager()
-        virtual KoStyleManager *styleManager() const = 0;
     };
 
     /**
diff --git a/libs/kotext/KoTextSelectionHandler.cpp \
b/libs/kotext/KoTextSelectionHandler.cpp index 6c08778..3746744 100644
--- a/libs/kotext/KoTextSelectionHandler.cpp
+++ b/libs/kotext/KoTextSelectionHandler.cpp
@@ -28,6 +28,7 @@
 #include "styles/KoParagraphStyle.h"
 #include "styles/KoCharacterStyle.h"
 #include "styles/KoStyleManager.h"
+#include "KoTextDocument.h"
 
 #include <kdebug.h>
 #include <KLocale>
@@ -279,11 +280,9 @@ void KoTextSelectionHandler::decreaseFontSize()
 
 void KoTextSelectionHandler::setDefaultFormat()
 {
-    KoTextDocumentLayout *layout = \
                dynamic_cast<KoTextDocumentLayout*>(d->textShapeData->document()->documentLayout());
                
-    if (!layout)
-        return;
+    KoTextDocument koDocument(d->textShapeData->document());
     emit startMacro(i18n("Set default format"));
-    if (KoStyleManager *styleManager = layout->styleManager()) {
+    if (KoStyleManager *styleManager = koDocument.styleManager()) {
         KoCharacterStyle *defaultCharStyle = \
styleManager->defaultParagraphStyle()->characterStyle();  QTextCharFormat \
defaultFormat;  defaultCharStyle->applyStyle(defaultFormat);
@@ -435,15 +434,16 @@ QTextCursor KoTextSelectionHandler::caret() const
 void KoTextSelectionHandler::nextParagraph()
 {
     emit startMacro(i18n("Insert Linebreak"));
-    KoTextDocumentLayout *lay = \
dynamic_cast<KoTextDocumentLayout*>(d->textShapeData->document()->documentLayout()); \
+    KoTextDocument koDocument(d->textShapeData->document()); +    KoStyleManager \
*styleManager = koDocument.styleManager();  KoParagraphStyle *nextStyle = 0;
-    if (lay && lay->styleManager()) {
+    if (styleManager) {
         int id = d->caret->blockFormat().intProperty(KoParagraphStyle::StyleId);
-        KoParagraphStyle *currentStyle = lay->styleManager()->paragraphStyle(id);
+        KoParagraphStyle *currentStyle = styleManager->paragraphStyle(id);
         if (currentStyle == 0) // not a style based parag.  Lets make the next one \
                correct.
-            nextStyle = lay->styleManager()->defaultParagraphStyle();
+            nextStyle = styleManager->defaultParagraphStyle();
         else
-            nextStyle = \
lay->styleManager()->paragraphStyle(currentStyle->nextStyle()); +            \
nextStyle = styleManager->paragraphStyle(currentStyle->nextStyle());  \
Q_ASSERT(nextStyle);  if (currentStyle == nextStyle)
             nextStyle = 0;
diff --git a/libs/kotext/KoTextShapeData.cpp b/libs/kotext/KoTextShapeData.cpp
index e5ae196..f56230e 100644
--- a/libs/kotext/KoTextShapeData.cpp
+++ b/libs/kotext/KoTextShapeData.cpp
@@ -44,6 +44,7 @@
 #include "styles/KoListLevelProperties.h"
 #include "KoTextDocumentLayout.h"
 #include "KoTextBlockData.h"
+#include "KoTextDocument.h"
 
 #include "opendocument/KoTextLoader.h"
 
@@ -208,7 +209,8 @@ bool KoTextShapeData::loadOdf(const KoXmlElement & element, \
KoShapeLoadingContex  KoTextLoader loader(context);
 
     QTextCursor cursor(document());
-    document()->clear();
+    cursor.select(QTextCursor::Document);
+    cursor.removeSelectedText();
     loader.loadBody(element, cursor);   // now let's load the body from the ODF \
KoXmlElement.  
     return true;
@@ -321,7 +323,7 @@ void KoTextShapeData::saveOdf(KoShapeSavingContext & context, int \
from, int to,  Q_ASSERT(layout);
     Q_ASSERT(layout->inlineObjectTextManager());
 
-    KoStyleManager *styleManager = layout->styleManager();
+    KoStyleManager *styleManager = KoTextDocument(d->document).styleManager();
     if (styleManager && saveDefaultStyles)
         styleManager->saveOdfDefaultStyles(context.mainStyles());
 
diff --git a/libs/kotext/opendocument/KoTextLoader.cpp \
b/libs/kotext/opendocument/KoTextLoader.cpp index 948f0f6..aba0354 100644
--- a/libs/kotext/opendocument/KoTextLoader.cpp
+++ b/libs/kotext/opendocument/KoTextLoader.cpp
@@ -56,6 +56,7 @@
 #include "styles/KoListStyle.h"
 #include "styles/KoListLevelProperties.h"
 #include "KoTextSharedLoadingData.h"
+#include "KoTextDocument.h"
 
 // KDE + Qt includes
 #include <QTextCursor>
@@ -152,10 +153,7 @@ void KoTextLoader::loadBody(const KoXmlElement& bodyElem, \
QTextCursor& cursor)  kDebug(32500) << "";
 
     const QTextDocument *document = cursor.block().document();
-    KoTextDocumentLayout *layout = dynamic_cast<KoTextDocumentLayout \
                *>(document->documentLayout());
-    if (layout) {
-        d->styleManager = layout->styleManager();
-    }
+    d->styleManager = KoTextDocument(document).styleManager();
 
     if ((document->isEmpty()) && (d->styleManager)) {
         QTextBlock block = cursor.block();
diff --git a/libs/kotext/opendocument/tests/TestLoading.cpp \
b/libs/kotext/opendocument/tests/TestLoading.cpp index e35f79e..e613eb5 100644
--- a/libs/kotext/opendocument/tests/TestLoading.cpp
+++ b/libs/kotext/opendocument/tests/TestLoading.cpp
@@ -56,6 +56,7 @@
 #include <KoEmbeddedDocumentSaver.h>
 #include <KoInlineTextObjectManager.h>
 #include <KoTextSharedLoadingData.h>
+#include <KoTextDocument.h>
 
 typedef KoText::Tab KoTextTab;
 // because in a QtScript, I don't seem to be able to use a namespaced type
@@ -691,7 +692,7 @@ QTextDocument *TestLoading::documentFromOdt(const QString &odt)
     textShapeData->setDocument(document, false /* ownership */);
     KoTextDocumentLayout *layout = new \
                KoTextDocumentLayout(textShapeData->document());
     layout->setInlineObjectTextManager(new KoInlineTextObjectManager(layout)); // \
                required while saving
-    layout->setStyleManager(styleManager);
+    KoTextDocument(document).setStyleManager(styleManager);
     textShapeData->document()->setDocumentLayout(layout);
 
     if (!textShapeData->loadOdf(body, shapeLoadingContext)) {
@@ -743,7 +744,7 @@ QString TestLoading::documentToOdt(QTextDocument *document)
         textShapeData->document()->setDocumentLayout(layout);
         layout->setInlineObjectTextManager(new KoInlineTextObjectManager(layout)); \
// required while saving  KoStyleManager *styleManager = new KoStyleManager;
-        layout->setStyleManager(styleManager);
+        KoTextDocument(textShapeData->document()).setStyleManager(styleManager);
     }
     textShapeData->saveOdf(context);
 
diff --git a/libs/kotext/styles/KoParagraphStyle.cpp \
b/libs/kotext/styles/KoParagraphStyle.cpp index 7e31c57..1375d72 100644
--- a/libs/kotext/styles/KoParagraphStyle.cpp
+++ b/libs/kotext/styles/KoParagraphStyle.cpp
@@ -28,6 +28,7 @@
 #include "KoListLevelProperties.h"
 #include "KoGenStyle.h"
 #include "Styles_p.h"
+#include "KoTextDocument.h"
 
 #include <KDebug>
 
@@ -1407,15 +1408,12 @@ KoParagraphStyle *KoParagraphStyle::fromBlock(const \
QTextBlock &block)  KoCharacterStyle *charStyle = 0;
     int styleId = format.intProperty(StyleId);
     if (styleId > 0) {
-        KoTextDocumentLayout *layout = \
                dynamic_cast<KoTextDocumentLayout*>(block.document()->documentLayout());
                
-        if (layout) {
-            KoStyleManager *sm = layout->styleManager();
-            if (sm) {
-                KoParagraphStyle *style = sm->paragraphStyle(styleId);
-                if (style)
-                    answer = new KoParagraphStyle(*style);
-                charStyle = \
                sm->characterStyle(format.intProperty(KoCharacterStyle::StyleId));
-            }
+        KoStyleManager *sm = KoTextDocument(block.document()).styleManager();
+        if (sm) {
+            KoParagraphStyle *style = sm->paragraphStyle(styleId);
+            if (style)
+                answer = new KoParagraphStyle(*style);
+            charStyle = \
sm->characterStyle(format.intProperty(KoCharacterStyle::StyleId));  }
     }
     if (answer == 0) {
diff --git a/libs/kotext/styles/KoStyleManager.cpp \
b/libs/kotext/styles/KoStyleManager.cpp index 2c8e31e..91fe85d 100644
--- a/libs/kotext/styles/KoStyleManager.cpp
+++ b/libs/kotext/styles/KoStyleManager.cpp
@@ -24,6 +24,7 @@
 #include "KoListStyle.h"
 #include "KoListLevelProperties.h"
 #include "ChangeFollower.h"
+#include "KoTextDocument.h"
 
 #include <KoGenStyle.h>
 #include <KoGenStyles.h>
diff --git a/libs/kotext/styles/KoStyleManager.h \
b/libs/kotext/styles/KoStyleManager.h index 7dc0b0e..76711f1 100644
--- a/libs/kotext/styles/KoStyleManager.h
+++ b/libs/kotext/styles/KoStyleManager.h
@@ -25,6 +25,7 @@
 #include "kotext_export.h"
 
 #include <QObject>
+#include <QMetaType>
 #include <KoDataCenter.h>
 
 class QTextDocument;
@@ -237,4 +238,6 @@ private:
     Private* const d;
 };
 
+Q_DECLARE_METATYPE(KoStyleManager*)
+
 #endif
diff --git a/plugins/textshape/Layout.cpp b/plugins/textshape/Layout.cpp
index f4be1b4..ac538f2 100644
--- a/plugins/textshape/Layout.cpp
+++ b/plugins/textshape/Layout.cpp
@@ -36,6 +36,7 @@
 #include <KoInlineTextObjectManager.h>
 #include <KoShape.h>
 #include <KoUnit.h>
+#include <KoTextDocument.h>
 
 #include <KDebug>
 #include <QTextList>
@@ -59,6 +60,7 @@ Layout::Layout(KoTextDocumentLayout *parent)
         m_dropCapsAffectedLineWidthAdjust(0),
         m_y_justBelowDropCaps(0)
 {
+    m_styleManager = KoTextDocument(m_parent->document()).styleManager();
 }
 
 bool Layout::start()
@@ -479,8 +481,11 @@ void Layout::nextShape()
     m_data->foul(); // make dirty since this one needs relayout at this point.
     m_textShape = dynamic_cast<TextShape*>(shape);
     Q_ASSERT(m_textShape);
-    if (m_textShape->hasFootnoteDocument())
-        m_textShape->footnoteDocument()->clear();
+    if (m_textShape->hasFootnoteDocument()) {
+        QTextCursor cursor(m_textShape->footnoteDocument());
+        cursor.select(QTextCursor::Document);
+        cursor.removeSelectedText();
+    }
     m_shapeBorder = shape->borderInsets();
     m_y += m_shapeBorder.top;
 }
@@ -596,8 +601,11 @@ void Layout::resetPrivate()
 
     m_textShape = dynamic_cast<TextShape*>(shape);
     Q_ASSERT(m_textShape);
-    if (m_textShape->hasFootnoteDocument())
-        m_textShape->footnoteDocument()->clear();
+    if (m_textShape->hasFootnoteDocument()) {
+        QTextCursor cursor(m_textShape->footnoteDocument());
+        cursor.select(QTextCursor::Document);
+        cursor.removeSelectedText();
+    }
     m_demoText = m_textShape->demoText();
     m_data = dynamic_cast<KoTextShapeData*>(shape->userData());
     m_shapeBorder = shape->borderInsets();
@@ -1330,9 +1338,3 @@ qreal Layout::findFootnote(const QTextLine &line)
         return m_textShape->footnoteDocument()->size().height();
     return 0;
 }
-
-void Layout::setStyleManager(KoStyleManager *sm)
-{
-    m_styleManager = sm;
-}
-
diff --git a/plugins/textshape/Layout.h b/plugins/textshape/Layout.h
index c4ae434..0c34b33 100644
--- a/plugins/textshape/Layout.h
+++ b/plugins/textshape/Layout.h
@@ -68,11 +68,6 @@ public:
     /// paint the document
     virtual void draw(QPainter *painter, const KoTextDocumentLayout::PaintContext & \
context);  
-    virtual void setStyleManager(KoStyleManager *sm);
-    virtual KoStyleManager *styleManager() const {
-        return m_styleManager;
-    }
-
     /// reimplemented from superclass
     virtual void clearTillEnd();
 
diff --git a/plugins/textshape/TextShape.cpp b/plugins/textshape/TextShape.cpp
index 68da289..9685a55 100644
--- a/plugins/textshape/TextShape.cpp
+++ b/plugins/textshape/TextShape.cpp
@@ -58,6 +58,7 @@ struct Finalizer {
 #include <KoXmlReader.h>
 #include <KoXmlNS.h>
 #include <KoShapeBackground.h>
+#include <KoTextDocument.h>
 
 #include <QTextLayout>
 #include <QFont>
@@ -99,11 +100,12 @@ void TextShape::setDemoText(bool on)
         for (int i = 0; i < 10; i ++)
             cursor.insertText("Lorem ipsum dolor sit amet, consectetuer adipiscing \
elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat \
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper \
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure \
dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu \
feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit \
praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.\n");  } \
                else if (m_demoText) {
-        m_textShapeData->document()->clear();
-        KoTextDocumentLayout *lay = \
                dynamic_cast<KoTextDocumentLayout*>(m_textShapeData->document()->documentLayout());
                
-        if (lay && lay->styleManager()) {
+        KoTextDocument document(m_textShapeData->document());
+        document.clearContents();
+        KoStyleManager *styleManager = document.styleManager();
+        if (styleManager) {
             QTextBlock block = m_textShapeData->document()->begin();
-            lay->styleManager()->defaultParagraphStyle()->applyStyle(block);
+            styleManager->defaultParagraphStyle()->applyStyle(block);
         }
     }
     m_demoText = on;
@@ -268,12 +270,8 @@ bool TextShape::loadOdfFrameElement(const KoXmlElement & \
element, KoShapeLoading  
 void TextShape::init(QMap<QString, KoDataCenter *>  dataCenterMap)
 {
-    KoStyleManager * styleManager = dynamic_cast<KoStyleManager \
                *>(dataCenterMap["StyleManager"]);
-
-    KoTextDocumentLayout *lay = \
                dynamic_cast<KoTextDocumentLayout*>(m_textShapeData->document()->documentLayout());
                
-    if (lay) {
-        lay->setStyleManager(styleManager);
-    }
+    KoStyleManager *styleManager = dynamic_cast<KoStyleManager \
*>(dataCenterMap["StyleManager"]); +    \
KoTextDocument(m_textShapeData->document()).setStyleManager(styleManager);  }
 
 QTextDocument *TextShape::footnoteDocument()
diff --git a/plugins/textshape/TextTool.cpp b/plugins/textshape/TextTool.cpp
index 2f426cd..2a53db0 100644
--- a/plugins/textshape/TextTool.cpp
+++ b/plugins/textshape/TextTool.cpp
@@ -54,6 +54,7 @@
 #include <KoDrag.h>
 #include <KoOdf.h>
 #include <KoTextPaste.h>
+#include <KoTextDocument.h>
 
 #include <kdebug.h>
 #include <KStandardShortcut>
@@ -1059,13 +1060,8 @@ void TextTool::updateActions()
 void TextTool::updateStyleManager()
 {
     Q_ASSERT(m_textShapeData);
-    KoTextDocumentLayout *lay = \
                dynamic_cast<KoTextDocumentLayout*>(m_textShapeData->document()->documentLayout());
                
-    if (lay)
-        emit styleManagerChanged(lay->styleManager());
-    else {
-        emit styleManagerChanged(0);
-        kWarning(32500) << "Shape does not have a KoTextDocumentLayout\n";
-    }
+    KoStyleManager *styleManager = \
KoTextDocument(m_textShapeData->document()).styleManager(); +    emit \
styleManagerChanged(styleManager);  }
 
 void TextTool::activate(bool temporary)
@@ -1482,15 +1478,14 @@ void TextTool::stopMacro()
 
 void TextTool::showStyleManager()
 {
-    KoTextDocumentLayout *lay = \
                dynamic_cast<KoTextDocumentLayout*>(m_textShapeData->document()->documentLayout());
                
-    if (lay) {
-        Q_ASSERT(lay->styleManager());
-        if (! lay->styleManager()) return;  //don't crash
-        StyleManagerDialog *dia = new StyleManagerDialog(m_canvas->canvasWidget());
-        dia->setStyleManager(lay->styleManager());
-        dia->setUnit(m_canvas->unit());
-        dia->show();
-    }
+    KoStyleManager *styleManager = \
KoTextDocument(m_textShapeData->document()).styleManager(); +    \
Q_ASSERT(styleManager); +    if (!styleManager)
+        return;  //don't crash
+    StyleManagerDialog *dia = new StyleManagerDialog(m_canvas->canvasWidget());
+    dia->setStyleManager(styleManager);
+    dia->setUnit(m_canvas->unit());
+    dia->show();
 }
 
 void TextTool::startTextEditingPlugin(const QString &pluginId)
diff --git a/plugins/textshape/tests/TestDocumentLayout.cpp \
b/plugins/textshape/tests/TestDocumentLayout.cpp index ec0b46c..a05b28c 100644
--- a/plugins/textshape/tests/TestDocumentLayout.cpp
+++ b/plugins/textshape/tests/TestDocumentLayout.cpp
@@ -6,6 +6,7 @@
 #include <KoStyleManager.h>
 #include <KoTextBlockData.h>
 #include <KoTextBlockBorderData.h>
+#include <KoTextDocument.h>
 
 #include <QtGui>
 
@@ -39,7 +40,7 @@ void TestDocumentLayout::initForNewTest(const QString &initText)
     m_textLayout = new Layout(layout);
     layout->setLayout(m_textLayout);
     styleManager = new KoStyleManager();
-    layout->setStyleManager(styleManager);
+    KoTextDocument(doc).setStyleManager(styleManager);
 
     QTextBlock block = doc->begin();
     if (initText.length() > 0) {



_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic