Commit 004e4301 authored by Sebastian Sauer's avatar Sebastian Sauer

move frame-related code into an own class.

svn path=/trunk/koffice/; revision=688819
parent 9b35a8bf
......@@ -64,6 +64,7 @@ set(kotext_LIB_SRCS ${libkohyphen_SRCS}
styles/ChangeFollower.cpp
opendocument/KoTextLoader.cpp
opendocument/KoTextFrameLoader.cpp
opendocument/KoTextLoadingContext.cpp
)
......
/* This file is part of the KDE project
* Copyright (C) 2004-2006 David Faure <faure@kde.org>
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2007 Sebastian Sauer <mail@dipe.org>
* Copyright (C) 2007 Pierre Ducroquet <pinaraf@gmail.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 "KoTextFrameLoader.h"
#include "KoTextLoader.h"
#include "KoTextLoadingContext.h"
//#include "KWDocument.h"
//#include "frames/KWTextFrameSet.h"
//#include "frames/KWTextFrame.h"
// koffice
#include <KoOasisStyles.h>
#include <KoOasisSettings.h>
#include <KoXmlNS.h>
#include <KoDom.h>
#include <KoUnit.h>
#include <KoPageLayout.h>
#include <KoShapeRegistry.h>
#include <KoShapeFactory.h>
#include <KoShape.h>
#include <KoShapeLoadingContext.h>
#include <KoImageData.h>
#include <KoTextAnchor.h>
#include <KoTextDocumentLayout.h>
#include <KoVariableManager.h>
#include <KoInlineTextObjectManager.h>
#include <KoInlineObjectRegistry.h>
#include <KoProperties.h>
#include <KoVariable.h>
#include "../styles/KoStyleManager.h"
#include "../styles/KoParagraphStyle.h"
#include "../styles/KoCharacterStyle.h"
#include "../styles/KoListStyle.h"
#include "../styles/KoListLevelProperties.h"
// KDE + Qt includes
#include <QDomDocument>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextBlock>
#include <QTextList>
#include <klocale.h>
// if defined then debugging is enabled
#define KOOPENDOCUMENTLOADER_DEBUG
/// \internal d-pointer class.
class KoTextFrameLoader::Private
{
public:
KoTextLoader* loader;
//QList<KoTextAnchor*> anchors;
};
KoTextFrameLoader::KoTextFrameLoader(KoTextLoader* loader)
: d(new Private())
{
d->loader = loader;
}
KoTextFrameLoader::~KoTextFrameLoader()
{
delete d;
}
void KoTextFrameLoader::loadFrame(KoTextLoadingContext& context, const KoXmlElement& frameElem, QTextCursor& cursor)
{
for(KoXmlNode node = frameElem.firstChild(); !node.isNull(); node = node.nextSibling()) {
KoXmlElement ts = node.toElement();
if( ts.isNull() ) continue;
const QString localName( ts.localName() );
//const bool isTextNS = ( ts.namespaceURI() == KoXmlNS::text );
const bool isDrawNS = ( ts.namespaceURI() == KoXmlNS::draw );
if (isDrawNS && localName == "image") {
loadImage(context, frameElem, ts, cursor);
}
else {
kWarning(32500) << "KoTextFrameLoader::loadFrame Unhandled frame: " << localName << endl;
}
}
}
void KoTextFrameLoader::loadImage(KoTextLoadingContext& context, const KoXmlElement& _frameElem, const KoXmlElement& _imageElem, QTextCursor& cursor)
{
const KoXmlElement frameElem = _frameElem;
const KoXmlElement imageElem = _imageElem;
KoShape* shape = loadImageShape(context, frameElem, imageElem, cursor);
if( ! shape ) {
kWarning(32500) << "KoTextFrameLoader::loadImage Failed to create picture shape" << endl;
return;
}
KoShapeLoadingContext shapecontext(context);
if( ! shape->loadOdf(frameElem, shapecontext) )
kWarning(32500) << "KoTextFrameLoader::loadImage Failed to load picture shape" << endl;
}
/* This file is part of the KDE project
* Copyright (C) 2004-2006 David Faure <faure@kde.org>
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2007 Sebastian Sauer <mail@dipe.org>
* Copyright (C) 2007 Pierre Ducroquet <pinaraf@gmail.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.
*/
#ifndef KOTEXTFRAMELOADER_H
#define KOTEXTFRAMELOADER_H
#include <QObject>
#include <KoStore.h>
#include <KoXmlReader.h>
//#include "KoText.h"
#include "kotext_export.h"
//class KWDocument;
//class KWTextFrameSet;
//class KWFrameSet;
//class KWFrame;
//class KWPageSettings;
//class KWPageManager;
//class KWTextFrameSet;
//class KoParagraphStyle;
//class KoCharacterStyle;
//class KoStore;
class KoTextLoader;
class KoTextLoadingContext;
//class KoStyleManager;
class KoShape;
//class KoImageCollection;
//class KoImageData;
class KoTextAnchor;
//class QDomDocument;
class QTextCursor;
//class QColor;
class KOTEXT_EXPORT KoTextFrameLoader
{
public:
explicit KoTextFrameLoader(KoTextLoader* loader);
virtual ~KoTextFrameLoader();
/**
* Load the frame element \p frameElem into the \p cursor .
*/
virtual void loadFrame(KoTextLoadingContext& context, const KoXmlElement& frameElem, QTextCursor& cursor);
/**
* Load the image frame into the \p cursor .
*/
virtual void loadImage(KoTextLoadingContext& context, const KoXmlElement& frameElem, const KoXmlElement& imageElem, QTextCursor& cursor);
/**
* Load the image and return a KoShape instance for it.
*
* The following code provides a sample how loading an image may work. You may
* also like to take a look at KWord how KWImageFrame is used there to lazy
* load the image.
* \code
* KoShapeFactory *factory = KoShapeRegistry::instance()->value("PictureShape");
* KoShape *shape = factory ? factory->createDefaultShape() : 0;
* if( ! shape ) return 0;
* KoStore* store = context.store();
* QString href = imageElem.attribute("href");
* if( ! store->hasFile(href) ) return shape;
* if( store->isOpen() ) return shape;
* if( ! store->open(href) ) return shape;
* KoImageCollection* imagecollection = new KoImageCollection();
* imagecollection->loadFromStore(store);
* KoImageData* imagedata = new KoImageData(imagecollection);
* //bool ok = imagedata->loadFromStore( store->device() );
* shape->setUserData( imagedata );
* store->close();
* \endcode
*/
virtual KoShape* loadImageShape(KoTextLoadingContext& context, const KoXmlElement& frameElem, const KoXmlElement& imageElem, QTextCursor& cursor) = 0;
/**
* Load an anchor for the shape and return a KoTextAnchor instance for it.
*/
virtual KoTextAnchor* loadImageAnchor(KoTextLoadingContext& context, const KoXmlElement& anchorElem, QTextCursor& cursor, KoShape* shape) = 0;
private:
/// \internal d-pointer class.
class Private;
/// \internal d-pointer instance.
Private* const d;
};
#endif
......@@ -21,6 +21,7 @@
*/
#include "KoTextLoader.h"
#include "KoTextFrameLoader.h"
#include "KoTextLoadingContext.h"
//#include "KWDocument.h"
//#include "frames/KWTextFrameSet.h"
......@@ -67,13 +68,21 @@
class KoTextLoader::Private
{
public:
explicit Private() {}
KoStyleManager* stylemanager;
int bodyProgressTotal;
int bodyProgressValue;
int lastElapsed;
QTime dt;
explicit Private() {
bodyProgressTotal = bodyProgressValue = lastElapsed = 0;
dt.start();
}
~Private() {
qDeleteAll(listStyles);
kDebug() << "Loading took " << (float)(dt.elapsed()) / 1000 << " seconds" << endl;
}
KoStyleManager* stylemanager;
KoParagraphStyle *paragraphStyle(const QString &name) {
if (paragraphStyles.contains(name))
return paragraphStyles[name];
......@@ -587,9 +596,9 @@ void KoTextLoader::loadList(KoTextLoadingContext& context, const KoXmlElement& p
//listStyle->applyStyle(cursor.block());
QTextBlock current = cursor.block();
list->add(current);
loadBody(context, e, cursor);
if( ! listStyle->hasPropertiesForLevel(level) ) { // set default style
KoListLevelProperties props;
props.setStyle(KoListStyle::DecimalItem);
......@@ -695,88 +704,6 @@ static QString normalizeWhitespace( const QString& in, bool leadingSpace )
return text;
}
void KoTextLoader::loadFrame(KoTextLoadingContext& context, const KoXmlElement& parent, QTextCursor& cursor)
{
for(KoXmlNode node = parent.firstChild(); !node.isNull(); node = node.nextSibling()) {
KoXmlElement ts = node.toElement();
if( ts.isNull() ) continue;
const QString localName( ts.localName() );
//const bool isTextNS = ts.namespaceURI() == KoXmlNS::text;
const bool isDrawNS = ts.namespaceURI() == KoXmlNS::draw;
if (isDrawNS && localName == "image") {
loadImage(context, parent, ts, cursor);
}
else {
kDebug(32500) << "KoTextLoader::loadFrame Unhandled frame: " << localName << endl;
}
}
}
void KoTextLoader::loadImage(KoTextLoadingContext& context, const KoXmlElement& _frameElem, const KoXmlElement& _imageElem, QTextCursor& cursor)
{
Q_UNUSED(cursor);
const KoXmlElement frameElem = _frameElem;
const KoXmlElement imageElem = _imageElem;
/*
//context.fillStyleStack( parent, KoXmlNS::text, "style-name", "graphic-properties" );
//double width = 0.0, height = 0.0;
QDomNamedNodeMap attrs = parent.attributes();
for (int iAttr = 0 ; iAttr < attrs.count() ; iAttr++) {
kDebug(32500) << "KoTextLoader::loadImage Attribute " << iAttr << " : " << attrs.item(iAttr).nodeName() << "\t" << attrs.item(iAttr).nodeValue() << endl;
//if (attrs.item(iAttr).nodeName() == "svg:width") width = KoUnit::parseValue(attrs.item(iAttr).nodeValue());
//else if (attrs.item(iAttr).nodeName() == "svg:height") height = KoUnit::parseValue(attrs.item(iAttr).nodeValue());
}
attrs = ts.attributes();
QString href;
for (int iAttr = 0 ; iAttr < attrs.count() ; iAttr++) {
kDebug(32500) << "KoTextLoader::loadImage Attribute " << iAttr << " : " << attrs.item(iAttr).nodeName() << "\t" << attrs.item(iAttr).nodeValue() << endl;
if (attrs.item(iAttr).localName() == "href") href = attrs.item(iAttr).nodeValue();
}
*/
KoShape* shape = loadImageShape(context, frameElem, imageElem, cursor);
if( ! shape ) {
kWarning(32500) << "KoTextLoader::loadImage Failed to create picture shape" << endl;
}
else {
KoShapeLoadingContext shapecontext(context);
if( ! shape->loadOdf(frameElem, shapecontext) ) {
kWarning(32500) << "Failed to load picture shape" << endl;
}
else {
kDebug(32500)<<"Successful loaded picture shape."<<endl;
}
}
}
KoShape* KoTextLoader::loadImageShape(KoTextLoadingContext& context, const KoXmlElement& frameElem, const KoXmlElement& imageElem, QTextCursor& cursor)
{
/*
KoShapeFactory *factory = KoShapeRegistry::instance()->value("PictureShape");
KoShape *shape = factory ? factory->createDefaultShape() : 0;
if( ! shape ) return 0;
KoStore* store = context.store();
QString href = imageElem.attribute("href");
if( ! store->hasFile(href) ) return shape;
if( store->isOpen() ) return shape;
if( ! store->open(href) ) return shape;
KoImageCollection* imagecollection = new KoImageCollection();
imagecollection->loadFromStore(store);
KoImageData* imagedata = new KoImageData(imagecollection);
//bool ok = imagedata->loadFromStore( store->device() );
shape->setUserData( imagedata );
store->close();
return shape;
*/
return 0;
}
KoTextAnchor* KoTextLoader::loadShapeAnchor(KoTextLoadingContext&, const KoXmlElement&, QTextCursor&, KoShape*)
{
return 0; //new KoTextAnchor(shape);
}
//1.6: KoTextParag::loadOasisSpan
void KoTextLoader::loadSpan(KoTextLoadingContext& context, const KoXmlElement& parent, QTextCursor& cursor, bool* stripLeadingSpace)
{
......@@ -957,4 +884,24 @@ void KoTextLoader::loadSpan(KoTextLoadingContext& context, const KoXmlElement& p
}
//#endif
void KoTextLoader::startBody(int total)
{
d->bodyProgressTotal += total;
}
void KoTextLoader::processBody()
{
d->bodyProgressValue++;
if( d->dt.elapsed() >= d->lastElapsed + 1000 ) { // update only once per second
d->lastElapsed = d->dt.elapsed();
Q_ASSERT( d->bodyProgressTotal > 0 );
const int percent = d->bodyProgressValue * 100 / d->bodyProgressTotal;
emit sigProgress( percent );
}
}
void KoTextLoader::endBody()
{
}
#include "KoTextLoader.moc"
......@@ -138,40 +138,40 @@ class KOTEXT_EXPORT KoTextLoader : public QObject
/**
* Load the frame element \p frameElem into the \p cursor .
*
* This method does normaly something like at the example above with an
* own implementation of \a KoTextFrameLoader to handle frames.
* \code
* KoTextFrameLoader frameloader(this);
* frameloader.loadFrame(context, parent, cursor);
* \endcode
*/
virtual void loadFrame(KoTextLoadingContext& context, const KoXmlElement& frameElem, QTextCursor& cursor);
virtual void loadFrame(KoTextLoadingContext& context, const KoXmlElement& frameElem, QTextCursor& cursor) = 0;
/**
* Load the image frame into the \p cursor .
*/
virtual void loadImage(KoTextLoadingContext& context, const KoXmlElement& frameElem, const KoXmlElement& imageElem, QTextCursor& cursor);
protected:
Q_SIGNALS:
/**
* Load the image and return a KoShape instance for it.
* This signal is emitted during loading with a percentage within 1-100 range
* \param percent the progress as a percentage
*/
virtual KoShape* loadImageShape(KoTextLoadingContext& context, const KoXmlElement& frameElem, const KoXmlElement& imageElem, QTextCursor& cursor);
void sigProgress(int percent);
/**
* Load an anchor for the shape and return a KoTextAnchor instance for it.
*/
virtual KoTextAnchor* loadShapeAnchor(KoTextLoadingContext& context, const KoXmlElement& anchorElem, QTextCursor& cursor, KoShape* shape);
protected:
/**
* This is called in loadBody before reading the body starts.
*/
virtual void startBody(int total) { Q_UNUSED(total); }
virtual void startBody(int total);
/**
* This is called in loadBody on each item that is readed within the body.
*/
virtual void processBody() {}
virtual void processBody();
/**
* This is called in loadBody once the body was readed.
*/
virtual void endBody() {}
virtual void endBody();
private:
/// \internal d-pointer class.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment