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

List:       koffice-devel
Subject:    Table of content patch
From:       Jean-Nicolas Artaud <jeannicolasartaud () gmail ! com>
Date:       2010-02-24 8:38:05
Message-ID: 4816864d1002240038o60a38b5fg21b32e4837720b02 () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello,

I send a patch of the not ended project of the table of content, as Thomas
wanted me to.

Regards,
Jean-Nicolas

[Attachment #5 (text/html)]

Hello,<br><br>I send a patch of the not ended project of the table of content, as Thomas wanted \
me to.<br><br>Regards,<br>Jean-Nicolas<br>

--0015174789e8ca04cb04805495af--


["ToC_Generate.patch" (text/x-patch)]

Index: kword/part/kword.rc
===================================================================
--- kword/part/kword.rc	(revision 1087538)
+++ kword/part/kword.rc	(working copy)
@@ -70,6 +70,7 @@
   <Separator/>
   <!-- Frames - the order matches the order of the keybindings -->
   <Action name="insert_table"/>
+  <Action name="insert_tableofcontents"/>
   <Action name="insert_picture"/>
   <Action name="tools_createtext"/>
   <Action name="tools_formula"/>
@@ -396,7 +397,7 @@
 </Menu>
 <Menu name="frame_popup">
  <Action name="edit_delframe"/>
- <Separator/> 
+ <Separator/>
  <Action name="create_framestyle"/>
  <Action name="border_backgroundcolor"/>
  <Separator/>
Index: kword/part/KWDocument.cpp
===================================================================
--- kword/part/KWDocument.cpp	(revision 1087538)
+++ kword/part/KWDocument.cpp	(working copy)
@@ -847,7 +847,7 @@
             kDebug(32001) << " +-- Columns:" << columns.columns << columns.columnSpacing << \
"pt spacing";  KoPageLayout layout = style.pageLayout();
         kDebug(32001) << "     PageSize: " << layout.width << "x" << layout.height;
-        kDebug(32001) << "     Indents: (tlbr, edge, binding)" 
+        kDebug(32001) << "     Indents: (tlbr, edge, binding)"
                       << layout.topMargin << layout.leftMargin
                       << layout.bottomMargin << layout.rightMargin
                       << layout.pageEdge << layout.bindingSide;
Index: kword/part/frames/KWTextDocumentLayout.cpp
===================================================================
--- kword/part/frames/KWTextDocumentLayout.cpp	(revision 1087538)
+++ kword/part/frames/KWTextDocumentLayout.cpp	(working copy)
@@ -319,6 +319,10 @@
 
 void KWTextDocumentLayout::layout()
 {
+
+    kDebug()<<"emit KoTextDocumentLayout::layoutFinished()";
+    emit KoTextDocumentLayout::layoutFinished();
+
     TDEBUG << "starting layout pass";
     QList<Outline*> outlines;
     class End
Index: plugins/textshape/TextTool.cpp
===================================================================
--- plugins/textshape/TextTool.cpp	(revision 1087538)
+++ plugins/textshape/TextTool.cpp	(working copy)
@@ -360,6 +360,11 @@
     action->setToolTip(i18n("Insert a table into the document."));
     connect(action, SIGNAL(triggered()), this, SLOT(insertTable()));
 
+    action = new KAction(i18n("Table of Contents..."), this);
+    addAction("insert_tableofcontents", action);
+    action->setToolTip(i18n("Insert a table of contents in the document."));
+    connect(action, SIGNAL(triggered()), this, SLOT(insertTableOfContents()));
+
     action = new KAction(i18n("Paragraph..."), this);
     addAction("format_paragraph", action);
     action->setShortcut(Qt::ALT + Qt::CTRL + Qt::Key_P);
@@ -1642,6 +1647,10 @@
     delete dia;
 }
 
+void TextTool::insertTableOfContents()
+{
+    m_textEditor->insertTableOfContents();
+}
 
 void TextTool::formatParagraph()
 {
Index: plugins/textshape/TextTool.h
===================================================================
--- plugins/textshape/TextTool.h	(revision 1087538)
+++ plugins/textshape/TextTool.h	(working copy)
@@ -185,6 +185,8 @@
     void insertIndexMarker();
     /// shows a dialog to insert a table
     void insertTable();
+    /// insert a table of contents into the document
+    void insertTableOfContents();
     /// shows a dialog to alter the paragraph properties
     void formatParagraph();
     //When enabled, display changes
Index: libs/kopageapp/KoPADocumentModel.cpp
===================================================================
--- libs/kopageapp/KoPADocumentModel.cpp	(revision 1087538)
+++ libs/kopageapp/KoPADocumentModel.cpp	(working copy)
@@ -92,7 +92,7 @@
 {
     if( !m_document )
         return QModelIndex();
-    
+
     // check if parent is root node
     if( ! parent.isValid() )
     {
Index: libs/kotext/styles/KoParagraphStyle.h
===================================================================
--- libs/kotext/styles/KoParagraphStyle.h	(revision 1087538)
+++ libs/kotext/styles/KoParagraphStyle.h	(working copy)
@@ -466,7 +466,7 @@
      * Change this block outline level
      */
     void setOutlineLevel(int outline);
-    
+
     /**
      * Return the default outline level of this style, or 0 if there is none.
      */
Index: libs/kotext/styles/KoParagraphStyle.cpp
===================================================================
--- libs/kotext/styles/KoParagraphStyle.cpp	(revision 1087538)
+++ libs/kotext/styles/KoParagraphStyle.cpp	(working copy)
@@ -191,7 +191,8 @@
         QVariant variant = d->stylesPrivate.value(keys[i]);
         format.setProperty(keys[i], variant);
     }
-    if ((hasProperty(DefaultOutlineLevel)) && (!format.hasProperty(OutlineLevel))) {
+
+    if ((hasProperty(DefaultOutlineLevel)) && (format.property(OutlineLevel) == 0)) {
        format.setProperty(OutlineLevel, defaultOutlineLevel());
     }
 }
@@ -934,7 +935,7 @@
         if (ok)
             setDefaultOutlineLevel(level);
     }
-    
+
     //1.6: KoTextFormat::load
     KoCharacterStyle *charstyle = characterStyle();
     context.styleStack().setTypeProperties("text");   // load all style attributes from \
                "style:text-properties"
Index: libs/kotext/KoTableOfContents.h
===================================================================
--- libs/kotext/KoTableOfContents.h	(revision 1087538)
+++ libs/kotext/KoTableOfContents.h	(working copy)
@@ -20,6 +20,37 @@
 #ifndef KOTABLEOFCONTENTS_H
 #define KOTABLEOFCONTENTS_H
 
+#include "KoParagraphStyle.h"
+#include <QTextCharFormat>
+#include <QTextDocument>
+#include <QTextCursor>
+
 const int tocType = 80200;
 
+class KoTableOfContents: public QObject
+{
+    Q_OBJECT
+    public:
+        /**
+        * Constructor.
+        *
+        * @param cursor where I am in the document
+        * @param document the document
+        */
+        KoTableOfContents(QTextCursor cursor, QTextDocument *doc);
+
+        void generate();
+
+        void setStyle(KoParagraphStyle *style);
+
+    public slots:
+        void update();
+
+    private:
+        QTextCursor cursor;
+        QTextDocument *doc;
+        QList<QString> m_listToC;
+        int m_begin;
+};
+
 #endif // KOTABLEOFCONTENTS_H
Index: libs/kotext/KoTextDocumentLayout.h
===================================================================
--- libs/kotext/KoTextDocumentLayout.h	(revision 1087538)
+++ libs/kotext/KoTextDocumentLayout.h	(working copy)
@@ -200,6 +200,7 @@
 
 signals:
     void shapeAdded(KoShape *shape);
+    void layoutFinished();
 
 public slots:
     /// make sure we start a layout run (returns immediately)
Index: libs/kotext/KoTextEditor.h
===================================================================
--- libs/kotext/KoTextEditor.h	(revision 1087538)
+++ libs/kotext/KoTextEditor.h	(working copy)
@@ -193,6 +193,8 @@
      */
     void insertTable(int rows, int columns);
 
+    void insertTableOfContents();
+
     void insertText(const QString &text);
 
     void insertText(const QString &text, const QTextCharFormat &format);
Index: libs/kotext/CMakeLists.txt
===================================================================
--- libs/kotext/CMakeLists.txt	(revision 1087538)
+++ libs/kotext/CMakeLists.txt	(working copy)
@@ -51,6 +51,7 @@
     KoTextPage.cpp
     KoPageProvider.cpp
     KoTableColumnAndRowStyleManager.cpp
+    KoTableOfContents.cpp
 
     styles/Styles_p.cpp
     styles/KoCharacterStyle.cpp
Index: libs/kotext/KoTextDocumentLayout.cpp
===================================================================
--- libs/kotext/KoTextDocumentLayout.cpp	(revision 1087538)
+++ libs/kotext/KoTextDocumentLayout.cpp	(working copy)
@@ -208,7 +208,7 @@
 
 int KoTextDocumentLayout::hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const
 {
-    return hitTestIterated(document()->rootFrame()->begin(), 
+    return hitTestIterated(document()->rootFrame()->begin(),
                         document()->rootFrame()->end(), point, accuracy);
 }
 
@@ -255,7 +255,7 @@
             }
             return block.position() + line.xToCursor(point.x());
         }
-        
+
     }
     if (accuracy == Qt::ExactHit)
         return -1;
@@ -399,8 +399,10 @@
                 m_state->shape->update(QRectF(0, posY,
                                               m_state->shape->size().width(), m_state->y() - \
posY));  
-            if (! moreText)
+            if (! moreText) {
+                emit layoutFinished();
                 return; // done!
+            }
             continue;
         }
         if (m_state->numColumns() > 0)
Index: libs/kotext/KoTableOfContents.cpp
===================================================================
--- libs/kotext/KoTableOfContents.cpp	(revision 0)
+++ libs/kotext/KoTableOfContents.cpp	(revision 0)
@@ -0,0 +1,190 @@
+/* This file is part of the KDE project
+* Copyright (C) 2010 ko gmbh <jean.nicolas.artaud@kogmbh.com>
+*
+* 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 "KoTableOfContents.h"
+
+#include "KoStyleManager.h"
+#include "KoTextDocument.h"
+#include "KoTextShapeData.h"
+#include "KoTextPage.h"
+#include "KoTextDocumentLayout.h"
+
+#include <KoShape.h>
+
+#include <QList>
+
+#include <KDebug>
+
+KoTableOfContents::KoTableOfContents(QTextCursor cursor, QTextDocument *doc)
+        :cursor(cursor), doc(doc)
+{
+}
+
+void KoTableOfContents::generate()
+{
+    // Add a frame to the current layout
+    QTextFrameFormat tocFormat;
+    tocFormat.setProperty(tocType, true);
+    KoTextDocument koDocument(doc);
+    KoStyleManager *styleManager = koDocument.styleManager();
+    QList<KoParagraphStyle *> paragStyle;
+
+    cursor.insertFrame(tocFormat);
+
+    // Add the title
+    cursor.beginEditBlock();
+    cursor.insertText("Table of Contents");
+    KoParagraphStyle * titleStyle = styleManager->paragraphStyle("Contents Heading");
+    if(titleStyle) {
+        setStyle(titleStyle);
+    }
+    cursor.insertBlock();
+
+    // looks for blocks to add in the ToC
+    QTextBlock block = doc->begin();
+    KoTextDocumentLayout *docLayout = \
qobject_cast<KoTextDocumentLayout*>(doc->documentLayout()); +
+    while (block.isValid()) {
+        int outlineLevel = block.blockFormat().intProperty(KoParagraphStyle::OutlineLevel);
+
+        if (outlineLevel > 0) {
+            KoShape *shape = docLayout->shapeForPosition(block.position());
+            if (shape) {
+                // Wrong page number, it will be corrected in the update
+                m_listToC.append(block.text()+"\t \
999999");//+QString::number(shapeData->page()->pageNumber())); +                \
KoParagraphStyle *currentStyle = styleManager->paragraphStyle("Contents \
"+QString::number(outlineLevel)); +                if (currentStyle) {
+                    paragStyle.append(currentStyle);
+                } else {
+                    KoParagraphStyle *newStyle = new KoParagraphStyle();
+                    newStyle->setName("Contents "+QString::number(outlineLevel));
+                    newStyle->setParent(styleManager->paragraphStyle("Standard"));
+
+                    newStyle->setLeftMargin(8*(outlineLevel-1));
+
+                    QList<KoText::Tab> tabList;
+                    struct KoText::Tab aTab;
+                    aTab.type = QTextOption::RightTab;
+                    aTab.leaderText = '.';
+                    aTab.position = 490-outlineLevel*8;
+                    tabList.append(aTab);
+                    newStyle->setTabPositions(tabList);
+
+                    styleManager->add(newStyle);
+
+                    paragStyle.append(newStyle);
+                }
+            }
+        }
+        block = block.next();
+    }
+
+    // Add blocks at the good position, to make move the whole doc
+    m_begin = cursor.position();
+    int listRunner = 0;
+    foreach(QString tocElement, m_listToC) {
+        KoShape *shape = docLayout->shapeForPosition(block.position());
+        if (shape) {
+
+            cursor.insertBlock();
+            cursor.insertText(tocElement);
+            setStyle(paragStyle.value(listRunner));
+        }
+        listRunner++;
+    }
+    cursor.endEditBlock();
+    // emit the fire signal (say that the toc is generated and the doc changed)
+    KoShape *shapeVariable = docLayout->shapeForPosition(0);
+    if(shapeVariable){
+        KoTextShapeData *shapeData = qobject_cast<KoTextShapeData \
*>(shapeVariable->userData()); +        connect(docLayout, SIGNAL(layoutFinished()), this, \
SLOT(update())); +        shapeData->fireResizeEvent();
+    }
+
+}
+
+void KoTableOfContents::update()
+{
+    // Regenerate (good pages number)
+    KoTextDocument koDocument(doc);
+    KoStyleManager *styleManager = koDocument.styleManager();
+    QList<KoParagraphStyle *> paragStyle;
+    m_listToC.clear();
+
+    // looks for blocks to add in the ToC
+    QTextBlock block = doc->begin();
+    KoTextDocumentLayout *docLayout = \
qobject_cast<KoTextDocumentLayout*>(doc->documentLayout()); +
+    // disconnect the layoutFinished once the docLayout is accessible
+    disconnect(docLayout, SIGNAL(layoutFinished()), this, SLOT(update()));
+
+    while (block.isValid()) {
+        int outlineLevel = block.blockFormat().intProperty(KoParagraphStyle::OutlineLevel);
+
+        if (outlineLevel > 0) {
+
+            KoShape *shape = docLayout->shapeForPosition(block.position());
+            if (shape) {
+                KoTextShapeData *shapeData = qobject_cast<KoTextShapeData \
*>(shape->userData()); +                \
m_listToC.append(block.text()+'\t'+QString::number(shapeData->page()->pageNumber())); +         \
KoParagraphStyle *currentStyle = styleManager->paragraphStyle("Contents \
"+QString::number(outlineLevel)); +                if (currentStyle) {
+                    paragStyle.append(currentStyle);
+                }
+            }
+        }
+        block = block.next();
+    }
+
+    // Add blocks at the good position, to make move the whole doc
+    cursor.setPosition(m_begin);
+    int listRunner = 0;
+    foreach(QString tocElement, m_listToC) {
+        KoShape *shape = docLayout->shapeForPosition(block.position());
+        if (shape) {
+            cursor.insertBlock();
+            cursor.insertText(tocElement);
+            setStyle(paragStyle.value(listRunner));
+
+        }
+        listRunner++;
+    }
+    cursor.endEditBlock();
+
+    // Remove existing ToC
+    listRunner = m_listToC.length();
+    while (listRunner > 0) {
+        cursor.movePosition(QTextCursor::NextBlock);
+        cursor.select(QTextCursor::BlockUnderCursor);
+        if (cursor.hasSelection()) {
+            cursor.removeSelectedText();
+        }
+        listRunner--;
+    }
+
+}
+
+void KoTableOfContents::setStyle(KoParagraphStyle *style)
+{
+    QTextBlockFormat format;
+    if (style) {
+        style->applyStyle(format);
+    }
+    cursor.setBlockFormat(format);
+}
\ No newline at end of file
Index: libs/kotext/KoTextEditor.cpp
===================================================================
--- libs/kotext/KoTextEditor.cpp	(revision 1087538)
+++ libs/kotext/KoTextEditor.cpp	(working copy)
@@ -30,6 +30,7 @@
 #include "KoTextLocator.h"
 #include "KoTextOdfSaveHelper.h"
 #include "KoTextPaste.h"
+#include "KoTableOfContents.h"
 #include "changetracker/KoChangeTracker.h"
 #include "changetracker/KoChangeTrackerElement.h"
 #include "changetracker/KoDeleteChangeMarker.h"
@@ -889,6 +890,18 @@
     d->updateState(KoTextEditor::Private::NoOp);
 }
 
+void KoTextEditor::insertTableOfContents()
+{
+    d->updateState(KoTextEditor::Private::Custom, i18n("Insert a table of contents"));
+
+    KoTableOfContents *toc = new KoTableOfContents(d->caret, d->document);
+    toc->generate();
+
+    d->caret.movePosition(QTextCursor::NextBlock);
+
+    d->updateState(KoTextEditor::Private::NoOp);
+}
+
 void KoTextEditor::insertText(const QString &text)
 {
     d->updateState(KoTextEditor::Private::KeyPress, i18n("Key Press"));



_______________________________________________
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