Commit a245e73f authored by Jan Hambrecht's avatar Jan Hambrecht

parse the draw:transform attribute and start loading/saving shape transformations


svn path=/trunk/koffice/; revision=680338
parent ab075ff5
...@@ -180,6 +180,8 @@ bool KoPathShape::loadOdf( const KoXmlElement & element, KoShapeLoadingContext & ...@@ -180,6 +180,8 @@ bool KoPathShape::loadOdf( const KoXmlElement & element, KoShapeLoadingContext &
normalize(); normalize();
loadOdfAttributes( element, context, OdfTransformation );
return true; return true;
} }
......
...@@ -647,9 +647,89 @@ bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingCon ...@@ -647,9 +647,89 @@ bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingCon
resize( size ); resize( size );
} }
if( attributes & OdfTransformation )
{
QString transform = element.attributeNS( KoXmlNS::draw, "transform", QString() );
if( ! transform.isEmpty() )
applyTransformation( parseOdfTransform( transform ) );
}
return true; return true;
} }
QMatrix KoShape::parseOdfTransform( const QString &transform )
{
QMatrix matrix;
// Split string for handling 1 transform statement at a time
QStringList subtransforms = transform.split(')', QString::SkipEmptyParts);
QStringList::ConstIterator it = subtransforms.begin();
QStringList::ConstIterator end = subtransforms.end();
for(; it != end; ++it)
{
QStringList subtransform = (*it).split('(', QString::SkipEmptyParts);
subtransform[0] = subtransform[0].trimmed().toLower();
subtransform[1] = subtransform[1].simplified();
QRegExp reg("[,( ]");
QStringList params = subtransform[1].split(reg, QString::SkipEmptyParts);
if(subtransform[0].startsWith(";") || subtransform[0].startsWith(","))
subtransform[0] = subtransform[0].right(subtransform[0].length() - 1);
if(subtransform[0] == "rotate")
{
// TODO find out what oo2 really does when rotating, it seems severly broken
if(params.count() == 3)
{
double x = KoUnit::parseValue( params[1] );
double y = KoUnit::parseValue( params[2] );
matrix.translate(x, y);
// oo2 rotates by radians
matrix.rotate( params[0].toDouble()*180.0/M_PI );
matrix.translate(-x, -y);
}
else
{
// oo2 rotates by radians
matrix.rotate( params[0].toDouble()*180.0/M_PI );
}
}
else if(subtransform[0] == "translate")
{
if(params.count() == 2)
{
double x = KoUnit::parseValue( params[0] );
double y = KoUnit::parseValue( params[1] );
matrix.translate(x, y);
}
else // Spec : if only one param given, assume 2nd param to be 0
matrix.translate( KoUnit::parseValue( params[0] ) , 0);
}
else if(subtransform[0] == "scale")
{
if(params.count() == 2)
matrix.scale(params[0].toDouble(), params[1].toDouble());
else // Spec : if only one param given, assume uniform scaling
matrix.scale(params[0].toDouble(), params[0].toDouble());
}
else if(subtransform[0] == "skewx")
matrix.shear(tan(params[0].toDouble()), 0.0F);
else if(subtransform[0] == "skewy")
matrix.shear(tan(params[0].toDouble()), 0.0F);
else if(subtransform[0] == "skewy")
matrix.shear(0.0F, tan(params[0].toDouble()));
else if(subtransform[0] == "matrix")
{
if(params.count() >= 6)
matrix.setMatrix(params[0].toDouble(), params[1].toDouble(), params[2].toDouble(), params[3].toDouble(), KoUnit::parseValue( params[4] ), KoUnit::parseValue( params[5] ) );
}
}
return matrix;
}
void KoShape::saveOdfFrameAttributes(KoShapeSavingContext &context) const { void KoShape::saveOdfFrameAttributes(KoShapeSavingContext &context) const {
saveOdfAttributes(context, FrameAttributes); saveOdfAttributes(context, FrameAttributes);
context.addOption(KoShapeSavingContext::FrameOpened); context.addOption(KoShapeSavingContext::FrameOpened);
...@@ -735,9 +815,8 @@ void KoShape::saveOdfAttributes(KoShapeSavingContext &context, int attributes) c ...@@ -735,9 +815,8 @@ void KoShape::saveOdfAttributes(KoShapeSavingContext &context, int attributes) c
*/ */
if( ! d->localMatrix.isIdentity() ) if( ! d->localMatrix.isIdentity() )
{ {
QString m = QString( "matrix(0 0 %3 %4 %5pt %6pt)" ) QString m = QString( "matrix(%1 %2 %3 %4 %5pt %6pt)" )
.arg( d->localMatrix.m11() ) .arg( d->localMatrix.m11() ).arg( d->localMatrix.m12() )
.arg( d->localMatrix.m12() )
.arg( d->localMatrix.m21() ).arg( d->localMatrix.m22() ) .arg( d->localMatrix.m21() ).arg( d->localMatrix.m22() )
.arg( d->localMatrix.dx() ) .arg( d->localMatrix.dy() ); .arg( d->localMatrix.dx() ) .arg( d->localMatrix.dy() );
context.xmlWriter().addAttribute( "draw:transform", m ); context.xmlWriter().addAttribute( "draw:transform", m );
......
...@@ -610,6 +610,13 @@ protected: ...@@ -610,6 +610,13 @@ protected:
*/ */
bool loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes ); bool loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes );
/**
* Parses the transformation attribute from the given string
* @param transform the transform attribute string
* @return the resulting transformation matrix
*/
QMatrix parseOdfTransform( const QString &transform );
/** /**
* This method can be used while saving the shape as ODF to add the data * This method can be used while saving the shape as ODF to add the data
* stored on this shape to the current element. * stored on this shape to the current element.
......
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