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

List:       koffice-devel
Subject:    Page variable support for kopageapp
From:       Thorsten Zachmann <t.zachmann () zagge ! de>
Date:       2009-09-16 13:29:42
Message-ID: 200909161529.43000.t.zachmann () zagge ! de
[Download RAW message or body]

Hello all,

in the quest for getting bug    
https://bugs.kde.org/show_bug.cgi?id=187838 fixed, I added a new way to the 
textshape to update the KoTextPage. This is done as the current way, while it 
works nicely for kword, does not work for kopageapp. The reasons for this are:

- You need to trigger a relayout on the textshape to get an update on the page 
variable if the page has changed. This is easyly done in kword where a 
relayout happens if something is changed. However this does not happen in 
kopageapp e.g. when a new page is added or a page is removed. One way to make 
that work would be to iterate over all shapes in every page for which the page 
has changed and trigger a relayout which is not nice.
- The application needed to set the KoTextPage onto the KoTextShapeData. In 
kword this is implemented on inserting of a textshape. This is not the best 
way as you need special code to handle inserts of textshapes into the 
application.
- It is not possible to show different page numbers inside the same textshape. 
This is needed for master pages which can be shown on different pages.

The patch adds a KoPageProvider that can give back a KoTextPage for shape. 
This is done in the paint event of the textshape. It will relayout the 
textshape if needed (when the page number changed) and will block all update 
calls when doing so to avoid retriggering of the paint event. 

If a application does not offer a page provider the behaviour is the same as 
before. It is possible to use this behaviour for selected shapes only if an 
app whishes.

I'm open for comments.

Have a nice day,

Thorsten


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

Index: libs/kopageapp/KoPATextPage.h
===================================================================
--- libs/kopageapp/KoPATextPage.h	(revision 0)
+++ libs/kopageapp/KoPATextPage.h	(revision 0)
@@ -0,0 +1,38 @@
+/* This file is part of the KDE project
+   Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
+
+   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 KOPATEXTPAGE_H
+#define KOPATEXTPAGE_H
+
+#include <KoTextPage.h>
+
+class KoPATextPage : public KoTextPage
+{
+public:
+    KoPATextPage(int page);
+
+    virtual ~KoPATextPage();
+
+    virtual int pageNumber(PageSelection select = CurrentPage, int adjustment = 0) const;
+
+private:
+    int m_page;
+};
+
+#endif /* KOPATEXTPAGE_H */
Index: libs/kopageapp/KoPAPageProvider.h
===================================================================
--- libs/kopageapp/KoPAPageProvider.h	(revision 0)
+++ libs/kopageapp/KoPAPageProvider.h	(revision 0)
@@ -0,0 +1,39 @@
+/* This file is part of the KDE project
+   Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
+
+   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 KOPAPAGEPROVIDER_H
+#define KOPAPAGEPROVIDER_H
+
+#include <KoPageProvider.h>
+
+class KoPAPageProvider : public KoPageProvider
+{
+public:
+    KoPAPageProvider();
+    virtual ~KoPAPageProvider();
+
+    virtual KoTextPage * page(KoShape * shape);
+
+    void setMasterPageNumber(int pageNumber);
+
+private:
+    int m_masterPageNumber;
+};
+
+#endif /* KOPAPAGEPROVIDER_H */

Property changes on: libs/kopageapp/KoPAPageProvider.h
___________________________________________________________________
Added: svn:mergeinfo

Index: libs/kopageapp/KoPADocumentModel.cpp
===================================================================
--- libs/kopageapp/KoPADocumentModel.cpp	(revision 1023636)
+++ libs/kopageapp/KoPADocumentModel.cpp	(working copy)
@@ -20,8 +20,9 @@
 
 #include "KoPADocumentModel.h"
 
-#include <KoPADocument.h>
-#include <KoPAPageBase.h>
+#include "KoPADocument.h"
+#include "KoPAPageBase.h"
+#include "KoPAPageProvider.h"
 #include <KoShapePainter.h>
 #include <KoShapeManager.h>
 #include <KoShapeBorderModel.h>
@@ -313,6 +314,9 @@ QImage KoPADocumentModel::createThumbnai
 
     KoPAPageBase *page = dynamic_cast<KoPAPageBase*>(shape);
     if (page) { // We create a thumbnail with actual width / height ratio for page
+        int pageNumber = m_document->pageIndex( page ) + 1;
+        static_cast<KoPAPageProvider*>( m_document->dataCenterMap()[KoPAPageProvider::ID] \
)->setMasterPageNumber( pageNumber ); +
         KoZoomHandler zoomHandler;
         KoPageLayout layout = page->pageLayout();
         qreal ratio = (zoomHandler.resolutionX() * layout.width) / (zoomHandler.resolutionY() \
                * layout.height);
Index: libs/kopageapp/KoPADocument.cpp
===================================================================
--- libs/kopageapp/KoPADocument.cpp	(revision 1024008)
+++ libs/kopageapp/KoPADocument.cpp	(working copy)
@@ -50,6 +50,7 @@
 #include "KoPASavingContext.h"
 #include "KoPALoadingContext.h"
 #include "KoPAViewMode.h"
+#include "KoPAPageProvider.h"
 #include "commands/KoPAPageDeleteCommand.h"
 
 #include <kdebug.h>
@@ -83,7 +84,7 @@ KoPADocument::KoPADocument( QWidget* par
 
     //Populate the document undoStack in the dataCenterMap. This can be used later by shapes \
for their undo/redo mechanism.  d->dataCenterMap["UndoStack"] = undoStack();
-
+    d->dataCenterMap[KoPageProvider::ID] = new KoPAPageProvider();
     loadConfig();
 }
 
Index: libs/kopageapp/CMakeLists.txt
===================================================================
--- libs/kopageapp/CMakeLists.txt	(revision 1023636)
+++ libs/kopageapp/CMakeLists.txt	(working copy)
@@ -35,6 +35,8 @@ set( kopageapp_LIB_SRCS
      KoPADocumentStructureDocker.cpp
      KoPAUtil.cpp
      KoPAPrintJob.cpp
+     KoPATextPage.cpp
+     KoPAPageProvider.cpp
      commands/KoPAPageInsertCommand.cpp
      commands/KoPAPageDeleteCommand.cpp
      commands/KoPAPageMoveCommand.cpp
Index: libs/kopageapp/KoPATextPage.cpp
===================================================================
--- libs/kopageapp/KoPATextPage.cpp	(revision 0)
+++ libs/kopageapp/KoPATextPage.cpp	(revision 0)
@@ -0,0 +1,34 @@
+/* This file is part of the KDE project
+   Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
+
+   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 "KoPATextPage.h"
+
+KoPATextPage::KoPATextPage(int page)
+: m_page(page)
+{
+}
+
+KoPATextPage::~KoPATextPage()
+{
+}
+
+int KoPATextPage::pageNumber(PageSelection select, int adjustment) const
+{
+    return m_page + adjustment;
+}
Index: libs/kopageapp/KoPAPageProvider.cpp
===================================================================
--- libs/kopageapp/KoPAPageProvider.cpp	(revision 0)
+++ libs/kopageapp/KoPAPageProvider.cpp	(revision 0)
@@ -0,0 +1,41 @@
+/* This file is part of the KDE project
+   Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
+
+   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 "KoPAPageProvider.h"
+
+#include "KoPATextPage.h"
+
+KoPAPageProvider::KoPAPageProvider()
+: m_masterPageNumber( 0 )
+{
+}
+
+KoPAPageProvider::~KoPAPageProvider()
+{
+}
+
+KoTextPage * KoPAPageProvider::page(KoShape * shape)
+{
+    return new KoPATextPage( m_masterPageNumber );
+}
+
+void KoPAPageProvider::setMasterPageNumber( int pageNumber )
+{
+    m_masterPageNumber = pageNumber;
+}

Property changes on: libs/kopageapp/KoPAPageProvider.cpp
___________________________________________________________________
Added: svn:mergeinfo

Index: libs/kopageapp/KoPACanvas.cpp
===================================================================
--- libs/kopageapp/KoPACanvas.cpp	(revision 1023636)
+++ libs/kopageapp/KoPACanvas.cpp	(working copy)
@@ -27,8 +27,10 @@
 #include "KoPAView.h"
 #include "KoPAViewMode.h"
 #include "KoPAPage.h"
+#include "KoPAPageProvider.h"
 
 #include <kxmlguifactory.h>
+#include <kdebug.h>
 
 #include <KAction>
 #include <QMenu>
@@ -188,6 +190,8 @@ const QPoint & KoPACanvas::documentOffse
 void KoPACanvas::paintEvent( QPaintEvent *event )
 {
     if (d->view->activePage()) {
+        int pageNumber = d->doc->pageIndex( d->view->activePage() ) + 1;
+        static_cast<KoPAPageProvider*>( d->doc->dataCenterMap()[KoPAPageProvider::ID] \
)->setMasterPageNumber( pageNumber );  d->view->viewMode()->paintEvent( this, event );
     }
 }
Index: libs/kotext/KoPageProvider.h
===================================================================
--- libs/kotext/KoPageProvider.h	(revision 0)
+++ libs/kotext/KoPageProvider.h	(revision 0)
@@ -0,0 +1,50 @@
+/* This file is part of the KDE project
+
+   Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
+
+   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 KOPAGEPROVIDER_H
+#define KOPAGEPROVIDER_H
+
+#include <KoDataCenter.h>
+
+#include "kotext_export.h"
+
+class KoShape;
+class KoTextPage;
+
+class KOTEXT_EXPORT KoPageProvider : public KoDataCenter
+{
+public:
+    KoPageProvider();
+    virtual ~KoPageProvider();
+
+    /// reimplemented
+    virtual bool completeLoading(KoStore *store);
+
+    /// reimplemented
+    virtual bool completeSaving(KoStore *store, KoXmlWriter * manifestWriter, \
KoShapeSavingContext * context); +
+    /**
+     * Get the page number for the given shape
+     */
+    virtual KoTextPage * page( KoShape * shape ) = 0;
+
+    static const char ID[];
+};
+#endif // KOPAGEPROVIDER_H

Property changes on: libs/kotext/KoPageProvider.h
___________________________________________________________________
Added: svn:mergeinfo

Index: libs/kotext/KoTextShapeData.cpp
===================================================================
--- libs/kotext/KoTextShapeData.cpp	(revision 1023636)
+++ libs/kotext/KoTextShapeData.cpp	(working copy)
@@ -193,8 +193,18 @@ KoText::Direction KoTextShapeData::pageD
 
 void KoTextShapeData::setPage(KoTextPage* textpage)
 {
+    int oldPageNumber = d->textpage ? d->textpage->pageNumber(): -1;
     delete d->textpage;
     d->textpage = textpage;
+    kDebug() << "setPage" << oldPageNumber << textpage << ( d->textpage ? \
d->textpage->pageNumber(): - 100 ); +    if ( d->textpage && d->textpage->pageNumber() != \
oldPageNumber ) { +        foul();
+        KoTextDocumentLayout *layout = \
qobject_cast<KoTextDocumentLayout*>(d->document->documentLayout()); +        if ( layout ) {
+            layout->interruptLayout();
+        }
+        fireResizeEvent();
+    }
 }
 
 KoTextPage* KoTextShapeData::page() const
Index: libs/kotext/KoPageProvider.cpp
===================================================================
--- libs/kotext/KoPageProvider.cpp	(revision 0)
+++ libs/kotext/KoPageProvider.cpp	(revision 0)
@@ -0,0 +1,45 @@
+/* This file is part of the KDE project
+
+   Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
+
+   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 "KoPageProvider.h"
+
+const char KoPageProvider::ID[] = "PageProvider";
+
+KoPageProvider::KoPageProvider()
+{
+}
+
+KoPageProvider::~KoPageProvider()
+{
+}
+
+bool KoPageProvider::completeLoading(KoStore *store)
+{
+    Q_UNUSED( store );
+    return true;
+}
+
+bool KoPageProvider::completeSaving(KoStore *store, KoXmlWriter * manifestWriter, \
KoShapeSavingContext * context) +{
+    Q_UNUSED( store );
+    Q_UNUSED( manifestWriter );
+    Q_UNUSED( context );
+    return true;
+}

Property changes on: libs/kotext/KoPageProvider.cpp
___________________________________________________________________
Added: svn:mergeinfo

Index: libs/kotext/CMakeLists.txt
===================================================================
--- libs/kotext/CMakeLists.txt	(revision 1023636)
+++ libs/kotext/CMakeLists.txt	(working copy)
@@ -50,6 +50,7 @@ set(kotext_LIB_SRCS
     KoFind.cpp
     KoTextDebug.cpp
     KoTextPage.cpp
+    KoPageProvider.cpp
     KoTableColumnAndRowStyleManager.cpp
     KoUndoStack.cpp
 
Index: libs/flake/KoShape.h
===================================================================
--- libs/flake/KoShape.h	(revision 1023636)
+++ libs/flake/KoShape.h	(working copy)
@@ -488,7 +488,7 @@ public:
      * will be merged into an appropriate repaint action.
      * @param shape the rectangle (in pt) to queue for repaint.
      */
-    void update(const QRectF &shape) const;
+    virtual void update(const QRectF &shape) const;
 
     /**
      * This is a method used to sort a list using the STL sorting methods.
Index: plugins/textshape/TextShape.h
===================================================================
--- plugins/textshape/TextShape.h	(revision 1023636)
+++ plugins/textshape/TextShape.h	(working copy)
@@ -34,6 +34,7 @@
 #define TextShape_SHAPEID "TextShapeID"
 
 class KoInlineTextObjectManager;
+class KoPageProvider;
 
 /**
  * A text shape.
@@ -102,6 +103,10 @@ public:
 
     void markLayoutDone();
 
+    virtual void update() const;
+
+    virtual void update(const QRectF &shape) const;
+
 protected:
     virtual bool loadOdfFrameElement(const KoXmlElement & element, KoShapeLoadingContext & \
context);  
@@ -114,6 +119,9 @@ private:
     bool m_demoText;
     mutable QMutex m_mutex;
     mutable QWaitCondition m_waiter;
+    KoPageProvider * m_pageProvider;
+
+    QRegion m_paintRegion;
 };
 
 #endif
Index: plugins/textshape/TextShape.cpp
===================================================================
--- plugins/textshape/TextShape.cpp	(revision 1023636)
+++ plugins/textshape/TextShape.cpp	(working copy)
@@ -60,7 +60,9 @@ struct Finalizer {
 #include <KoTextDocument.h>
 #include <KoTextDocumentLayout.h>
 #include <KoTextEditor.h>
+#include <KoTextPage.h>
 #include <KoTextShapeContainerModel.h>
+#include <KoPageProvider.h>
 #include <KoUndoStack.h>
 #include <KoViewConverter.h>
 #include <KoXmlWriter.h>
@@ -77,6 +79,7 @@ struct Finalizer {
 
 #include <kdebug.h>
 
+
 TextShape::TextShape(KoInlineTextObjectManager *inlineTextObjectManager)
         : KoShapeContainer(new KoTextShapeContainerModel())
         , KoFrameShape(KoXmlNS::draw, "text-box")
@@ -131,14 +134,37 @@ void TextShape::paintComponent(QPainter 
     QTextDocument *doc = m_textShapeData->document();
     Q_ASSERT(doc);
     KoTextDocumentLayout *lay = qobject_cast<KoTextDocumentLayout*>(doc->documentLayout());
+
     if (m_textShapeData->endPosition() < 0) { // not layouted yet.
-        if (lay == 0)
+        if (lay == 0) {
             kWarning(32500) << "Painting shape that doesn't have a kotext doc-layout, which \
                can't work";
-        else if (! lay->hasLayouter())
+            return;
+        }
+        else if (! lay->hasLayouter()) {
             lay->setLayout(new Layout(lay));
-        return;
+        }
+        if (!m_pageProvider) {
+            return;
+        }
     }
     Q_ASSERT(lay);
+
+    if (m_pageProvider) {
+        KoTextPage* page = m_pageProvider->page(this);
+        if (page) {
+            // this is used to not trigger repaints if layout during the painting is done
+            // this enbales to use the same shapes on different pages showing different page \
numbers +            m_paintRegion = painter.clipRegion();
+
+            m_textShapeData->setPage(page);
+            if ( lay ) {
+                while (m_textShapeData->isDirty()){
+                    lay->layout();
+                }
+            }
+        }
+    }
+
     QAbstractTextDocumentLayout::PaintContext pc;
     KoTextDocumentLayout::PaintContext context;
     context.textContext = pc;
@@ -156,6 +182,7 @@ void TextShape::paintComponent(QPainter 
         painter.translate(0, size().height() - m_footnotes->size().height());
         m_footnotes->documentLayout()->draw(&painter, pc);
     }
+    m_paintRegion = QRegion();
 }
 
 QPointF TextShape::convertScreenPos(const QPointF &point)
@@ -354,6 +381,7 @@ void TextShape::init(const QMap<QString,
     document.setUndoStack(undoStack);
 //    KoChangeTracker *changeTracker = dynamic_cast<KoChangeTracker \
*>(dataCenterMap["ChangeTracker"]);  //    document.setChangeTracker(changeTracker);
+    m_pageProvider = dynamic_cast<KoPageProvider *>(dataCenterMap[KoPageProvider::ID]);
 }
 
 QTextDocument *TextShape::footnoteDocument()
@@ -375,6 +403,20 @@ void TextShape::markLayoutDone()
     }
 }
 
+void TextShape::update() const
+{
+//    kDebug(32500) << "TextShape::update" << kBacktrace( 10 );
+    KoShapeContainer::update();
+}
+
+void TextShape::update(const QRectF &shape) const
+{
+    // this is done to avoid updates which are called during the paint event and not needed.
+    if (!m_paintRegion.contains(shape.toRect())) {
+        KoShape::update(shape);
+    }
+}
+
 void TextShape::waitUntilReady(const KoViewConverter &, bool asynchronous) const
 {
     if (asynchronous) {



_______________________________________________
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