Commit c0ab8087 authored by Jan Hambrecht's avatar Jan Hambrecht

Implemented loading of shape groups. This was a little more

work as the child shapes of the group had to be added to 
the document after loading so i put them into the loading
context. After loading the document, the apps KoShapeControllerBase
implementaion has to transfer the collected shapes from the
context to itself. The creation of the group shape is 
hardwired into the shape registry as we do not want a shape
group appear inside the shape selector (when using a KoShapeGroupFactory).
Additionally i changed the shapes to save their global
transformation matrix, as that is what they are in ODF.


svn path=/trunk/koffice/; revision=698767
parent 9f632253
......@@ -922,12 +922,13 @@ void KoShape::saveOdfAttributes(KoShapeSavingContext &context, int attributes) c
context.xmlWriter().addAttribute( "draw:transform", transform );
}
*/
if( ! d->localMatrix.isIdentity() )
QMatrix matrix = transformationMatrix(0);
if( ! matrix.isIdentity() )
{
QString m = QString( "matrix(%1 %2 %3 %4 %5pt %6pt)" )
.arg( d->localMatrix.m11() ).arg( d->localMatrix.m12() )
.arg( d->localMatrix.m21() ).arg( d->localMatrix.m22() )
.arg( d->localMatrix.dx() ) .arg( d->localMatrix.dy() );
.arg( matrix.m11() ).arg( matrix.m12() )
.arg( matrix.m21() ).arg( matrix.m22() )
.arg( matrix.dx() ) .arg( matrix.dy() );
context.xmlWriter().addAttribute( "draw:transform", m );
}
}
......
/* This file is part of the KDE project
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
* Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -21,7 +22,10 @@
#include "KoShapeContainerModel.h"
#include "SimpleShapeContainerModel.h"
#include "KoShapeSavingContext.h"
#include "KoShapeLoadingContext.h"
#include "KoXmlWriter.h"
#include "KoXmlReader.h"
#include "KoShapeRegistry.h"
#include <QPainter>
......@@ -59,5 +63,35 @@ void KoShapeGroup::saveOdf( KoShapeSavingContext & context ) const {
}
bool KoShapeGroup::loadOdf( const KoXmlElement & element, KoShapeLoadingContext &context ) {
return false; // TODO
KoXmlElement child;
forEachElement( child, element )
{
KoShape * shape = KoShapeRegistry::instance()->createShapeFromOdf( child, context );
if( shape )
{
addChild( shape );
// add shape to context for later adding to the document
context.addShapeToDocument( shape );
}
}
loadOdfAttributes( element, context, OdfMandatories );
QRectF bound;
bool boundInitialized = false;
foreach( KoShape * shape, iterator() )
{
if( ! boundInitialized )
bound = shape->boundingRect();
else
bound = bound.united( shape->boundingRect() );
}
setSize( bound.size() );
setPosition( bound.topLeft() );
foreach( KoShape * shape, iterator() )
shape->moveBy( -bound.topLeft().x(), -bound.topLeft().y() );
return true;
}
......@@ -26,6 +26,9 @@
#include <flake_export.h>
class KoShapeSavingContext;
class KoShapeLoadingContext;
/**
* Provide grouping for shapes.
* The group shape allows you to add children which will then be grouped in selections
......
/* This file is part of the KDE project
Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -18,13 +19,34 @@
*/
#include "KoShapeLoadingContext.h"
#include "KoShapeControllerBase.h"
#include "KoShape.h"
#include "KoShapeContainer.h"
#include <kdebug.h>
class KoShapeLoadingContext::Private {
public:
Private(KoOasisLoadingContext &c) : context(c) {}
~Private()
{
if( shapesForDocument.count() )
kWarning(30006) << "KoShapeLoadingContext: there are loaded shapes not added to the document";
// the collected shapes were not added to a document
// so we remove them from their parents and delete them
// to not leak any memory
foreach( KoShape * shape, shapesForDocument )
{
if( shape->parent() )
shape->parent()->removeChild( shape );
delete shape;
}
shapesForDocument.clear();
}
KoOasisLoadingContext &context;
QMap<QString, KoShapeLayer*> layers;
QMap<QString, KoShape*> drawIds;
QList<KoShape*> shapesForDocument;
};
KoShapeLoadingContext::KoShapeLoadingContext( KoOasisLoadingContext & context )
......@@ -32,6 +54,11 @@ KoShapeLoadingContext::KoShapeLoadingContext( KoOasisLoadingContext & context )
{
}
KoShapeLoadingContext::~KoShapeLoadingContext()
{
delete d;
}
KoOasisLoadingContext & KoShapeLoadingContext::koLoadingContext()
{
return d->context;
......@@ -57,3 +84,20 @@ KoShape * KoShapeLoadingContext::shapeById( const QString & id )
return d->drawIds.value( id, 0 );
}
void KoShapeLoadingContext::addShapeToDocument( KoShape * shape )
{
d->shapesForDocument.append( shape );
}
void KoShapeLoadingContext::transferShapesToDocument( KoShapeControllerBase * controller )
{
if( ! controller )
return;
// add all the shapes collected during loading to the shape controller
foreach( KoShape * shape, d->shapesForDocument )
controller->addShape( shape );
// the shape controller now owns the shapes, so we can clear the list
d->shapesForDocument.clear();
}
/* This file is part of the KDE project
Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -28,6 +29,7 @@
class KoOasisLoadingContext;
class KoShapeLayer;
class KoShape;
class KoShapeControllerBase;
/**
* Context passed to shapes during loading.
......@@ -43,6 +45,9 @@ public:
*/
KoShapeLoadingContext( KoOasisLoadingContext & context );
/// destructor
~KoShapeLoadingContext();
/// return the embedded loading context
KoOasisLoadingContext & koLoadingContext();
......@@ -56,6 +61,15 @@ public:
/// return the shape formerly registered using addShapeId()
KoShape * shapeById( const QString & id );
/// sets a shape controller to add shapes during loading (e.g. group loading)
void setShapeController( KoShapeControllerBase * shapeController );
/// Adds a shape to the context for later adding to the document
void addShapeToDocument( KoShape * shape );
/// Transfers ownership of all collected shapes during loading to the shape controller.
void transferShapesToDocument( KoShapeControllerBase * controller );
private:
class Private;
Private * const d;
......
......@@ -22,6 +22,7 @@
#include "KoShapeRegistry.h"
#include "KoPathShapeFactory.h"
#include "KoShapeLoadingContext.h"
#include "KoShapeGroup.h"
#include <KoPluginLoader.h>
#include <KoXmlReader.h>
......@@ -131,6 +132,21 @@ KoShape * KoShapeRegistry::createShapeFromOdf(const KoXmlElement & e, KoShapeLoa
return shape;
}
}
// Hardwire the group shape into the loading as it should not appear
// in the shape selector
else if( e.localName() == "g" && e.namespaceURI() == KoXmlNS::draw )
{
KoShapeGroup * group = new KoShapeGroup();
context.koLoadingContext().styleStack().save();
bool loaded = group->loadOdf( e, context );
context.koLoadingContext().styleStack().restore();
if( loaded )
return group;
delete group;
}
else {
return createShapeInternal( e, context );
}
......
......@@ -517,6 +517,8 @@ void KoToolManager::selectionChanged(QList<KoShape*> shapes) {
}
KoCanvasController *KoToolManager::activeCanvasController() const {
if( ! d->canvasData )
return 0;
return d->canvasData->canvas;
}
......
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