summaryrefslogtreecommitdiffstats
path: root/filters/karbon/oodraw/oodrawimport.cc
blob: fba488671cb1d26de730d9fdea23a6addb3ebbe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/* This file is part of the KDE project
   Copyright (c) 2003 Rob Buis <buis@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 "oodrawimport.h"

#include <math.h>

#include <tqdatetime.h>
#include <tqfileinfo.h>
#include <tqdir.h>

#include <kzip.h>
#include <karchive.h>
#include <kdebug.h>
#include <KoUnit.h>
#include <KoPageLayout.h>
#include <KoDocumentInfo.h>
#include <KoDocument.h>
#include <ooutils.h>
#include <KoDom.h>

#include <kgenericfactory.h>
#include <KoFilterChain.h>
#include <KoGlobal.h>

#include <shapes/vellipse.h>
#include <shapes/vrectangle.h>
#include <shapes/vpolygon.h>

#include <core/vfill.h>
#include <core/vgroup.h>

typedef KGenericFactory<OoDrawImport, KoFilter> OoDrawImportFactory;
K_EXPORT_COMPONENT_FACTORY( liboodrawimport, OoDrawImportFactory( "kofficefilters" ) )


OoDrawImport::OoDrawImport( KoFilter *, const char *, const TQStringList & )
    : KoFilter(),
      m_styles( 23, true ),
      m_styleStack( ooNS::style, ooNS::fo )
{
	m_styles.setAutoDelete( true );
}

OoDrawImport::~OoDrawImport()
{
}

KoFilter::ConversionStatus OoDrawImport::convert( TQCString const & from, TQCString const & to )
{
    kdDebug() << "Entering Oodraw Import filter: " << from << " - " << to << endl;

    if( from != "application/vnd.sun.xml.draw" || to != "application/x-karbon" )
    {
        kdWarning() << "Invalid mimetypes " << from << " " << to << endl;
        return KoFilter::NotImplemented;
    }

    m_zip = new KZip(  m_chain->inputFile() );

    if ( !m_zip->open( IO_ReadOnly ) )
    {
        kdError(30518) << "Couldn't open the requested file "<< m_chain->inputFile() << endl;
        delete m_zip;
        return KoFilter::FileNotFound;
    }

    KoFilter::ConversionStatus preStatus = openFile();

    if( preStatus != KoFilter::OK )
    {
        m_zip->close();
        delete m_zip;
        return preStatus;
    }
/*TQDomDocument docinfo;
  createDocumentInfo( docinfo );

// store document info
KoStoreDevice* out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
if( out )
{
TQCString info = docinfo.toCString();
//kdDebug() << " info :" << info << endl;
// WARNING: we cannot use KoStore::write(const TQByteArray&) because it gives an extra NULL character at the end.
out->writeBlock( info , info.length() );
}*/
    TQDomDocument docinfo;
    createDocumentInfo( docinfo );
    // store document info
    KoStoreDevice* out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
    if( out )
    {
        TQCString info = docinfo.toCString();
        //kdDebug(30518) << " info :" << info << endl;
        // WARNING: we cannot use KoStore::write(const TQByteArray&) because it gives an extra NULL character at the end.
        out->writeBlock( info , info.length() );
    }

    convert();
    TQDomDocument outdoc = m_document.saveXML();
	// add paper info, we always need custom for svg (Rob)
	TQDomElement paper = outdoc.createElement( "PAPER" );
	outdoc.documentElement().appendChild( paper );
	paper.setAttribute( "format", PG_CUSTOM );
	paper.setAttribute( "width", m_document.width() );
	paper.setAttribute( "height", m_document.height() );

    // store document content
    out = m_chain->storageFile( "maindoc.xml", KoStore::Write );
    if( out )
    {
        TQCString content = outdoc.toCString();
        kdDebug() << " content :" << content << endl;
        out->writeBlock( content , content.length() );
    }
    m_zip->close();
    delete m_zip;

    kdDebug() << "######################## OoDrawImport::convert done ####################" << endl;

    return KoFilter::OK;
}

// Very related to OoWriterImport::createDocumentInfo
void OoDrawImport::createDocumentInfo( TQDomDocument &docinfo )
{
    docinfo = KoDocument::createDomDocument( "document-info" /*DTD name*/, "document-info" /*tag name*/, "1.1" );

    OoUtils::createDocumentInfo(m_meta, docinfo);
    //kdDebug(30518) << " meta-info :" << m_meta.toCString() << endl;
}


// Very related to OoWriterImport::openFile()
KoFilter::ConversionStatus OoDrawImport::openFile()
{
    KoFilter::ConversionStatus status = loadAndParse( "content.xml", m_content );
    if ( status != KoFilter::OK )
    {
        kdError(30518) << "Content.xml could not be parsed correctly! Aborting!" << endl;
        return status;
    }

    // We do not stop if the following calls fail.
    TQDomDocument styles;
    loadAndParse( "styles.xml", styles );
    loadAndParse( "meta.xml", m_meta );
    loadAndParse( "settings.xml", m_settings );

    emit sigProgress( 10 );
    createStyleMap( styles );

    return KoFilter::OK;
}

void OoDrawImport::convert()
{
	m_document.saveAsPath( false );

	TQDomElement content = m_content.documentElement();

	// content.xml contains some automatic-styles that we need to store
	TQDomNode automaticStyles = KoDom::namedItemNS( content, ooNS::office, "automatic-styles" );
	if( !automaticStyles.isNull() )
		insertStyles( automaticStyles.toElement() );

	TQDomNode body = KoDom::namedItemNS( content, ooNS::office, "body" );
	if( body.isNull() )
		return;

	// we take the settings of the first slide for the whole document.
	TQDomElement drawPage = KoDom::namedItemNS( body, ooNS::draw, "page" );
	if( drawPage.isNull() ) // no pages? give up.
		return;

	TQDomElement *master = m_styles[drawPage.attributeNS( ooNS::draw, "master-page-name", TQString() )];
	TQDomElement *style = m_styles[master->attributeNS( ooNS::style, "page-master-name", TQString() )];
	TQDomElement properties = KoDom::namedItemNS( *style, ooNS::style, "properties" ).toElement();

	if( properties.isNull() )
	{
		m_document.setWidth( 550.0 );
		m_document.setHeight( 841.0 );
	}
	else
	{
		m_document.setWidth( KoUnit::parseValue(properties.attributeNS( ooNS::fo, "page-width", TQString() ) ) );
		m_document.setHeight( KoUnit::parseValue(properties.attributeNS( ooNS::fo, "page-height", TQString() ) ) );
	}

    // parse all pages
    for( TQDomNode drawPage = body.firstChild(); !drawPage.isNull(); drawPage = drawPage.nextSibling() )
    {
        TQDomElement dp = drawPage.toElement();
        m_styleStack.clear(); // remove all styles
        fillStyleStack( dp );
        //m_styleStack.setPageMark();

        // set the pagetitle
        //TQDomElement titleElement = doc.createElement( "Title" );
        //titleElement.setAttribute( "title", dp.attributeNS( "name" ) );
        //pageTitleElement.appendChild( titleElement );

	parseGroup( 0L, dp );
    }
}


KoFilter::ConversionStatus OoDrawImport::loadAndParse(const TQString& filename, TQDomDocument& doc)
{
     return OoUtils::loadAndParse( filename, doc, m_zip);
}

void
OoDrawImport::parseGroup( VGroup *parent, const TQDomElement& parentobject )
{
	// parse all objects
	for( TQDomNode object = parentobject.firstChild(); !object.isNull(); object = object.nextSibling() )
	{
		TQDomElement o = object.toElement();
		if( o.namespaceURI() != ooNS::draw ) continue;
		TQString name = o.localName();
		TQString drawID = o.attributeNS( ooNS::draw, "id", TQString() );
		VObject *obj = 0L;

		if( name == "g" ) // polyline
		{
			storeObjectStyles( o );
			VGroup *group = new VGroup( parent );
			appendPen( *group );
			appendBrush( *group );
			obj = group;
			parseGroup( group, o );
		}
		else if( name == "rect" ) // rectangle
		{
			storeObjectStyles( o );
			double x = KoUnit::parseValue( o.attributeNS( ooNS::svg, "x", TQString() ) );
			double y = ymirror( KoUnit::parseValue( o.attributeNS( ooNS::svg, "y", TQString() ) ) );
			double w = KoUnit::parseValue( o.attributeNS( ooNS::svg, "width", TQString() ) );
			double h = KoUnit::parseValue( o.attributeNS( ooNS::svg, "height", TQString() ) );
			int corner = static_cast<int>( KoUnit::parseValue( o.attributeNS( ooNS::draw, "corner-radius", TQString() ) ) );
			VRectangle *rect = new VRectangle( parent, KoPoint( x, y ), w, h, corner );
			appendPen( *rect );
			appendBrush( *rect );
			obj = rect;
		}
		else if( name == "circle" || name == "ellipse" )
		{
			storeObjectStyles( o );
			double w = KoUnit::parseValue( o.attributeNS( ooNS::svg, "width", TQString() ) );
			double h = KoUnit::parseValue( o.attributeNS( ooNS::svg, "height", TQString() ) );
			double x = KoUnit::parseValue( o.attributeNS( ooNS::svg, "x", TQString() ) );
			double y = ymirror( KoUnit::parseValue( o.attributeNS( ooNS::svg, "y", TQString() ) ) ) - h;
			double start = o.attributeNS( ooNS::draw, "start-angle", TQString() ).toDouble();
			double end = o.attributeNS( ooNS::draw, "end-angle", TQString() ).toDouble();
			TQString kind = o.attributeNS( ooNS::draw, "kind", TQString() );
			VEllipse::VEllipseType type = VEllipse::full;
			if( !kind.isEmpty() )
			{
				if( kind == "section" )
					type = VEllipse::cut;
				else if( kind == "cut" )
					type = VEllipse::section;
				else if( kind == "arc" )
					type = VEllipse::arc;
			}
			VEllipse *ellipse = new VEllipse( parent, KoPoint( x, y ), w, h, type, start, end );
			appendPen( *ellipse );
			// arc has no brush
			if( kind != "arc" )
				appendBrush( *ellipse );
			obj = ellipse;
		}
		else if( name == "line" ) // line
		{
			storeObjectStyles( o );
			VPath *line = new VPath( parent );
			double x1 = KoUnit::parseValue( o.attributeNS( ooNS::svg, "x1", TQString() ) );
			double y1 = ymirror( KoUnit::parseValue( o.attributeNS( ooNS::svg, "y1", TQString() ) ) );
			double x2 = KoUnit::parseValue( o.attributeNS( ooNS::svg, "x2", TQString() ) );
			double y2 = ymirror( KoUnit::parseValue( o.attributeNS( ooNS::svg, "y2", TQString() ) ) );
			line->moveTo( KoPoint( x1, y1 ) );
			line->lineTo( KoPoint( x2, y2 ) );
			appendPen( *line );
			appendBrush( *line );
			obj = line;
		}
		else if( name == "polyline" ) // polyline
		{
			storeObjectStyles( o );
			VPath *polyline = new VPath( parent );
			appendPoints( *polyline, o);
			appendPen( *polyline );
			appendBrush( *polyline );
			obj = polyline;
		}
		else if( name == "polygon" ) // polygon
		{
			storeObjectStyles( o );
			//VPolygon *polygon = new VPolygon( parent );
			//polygon->load( o );
			VPath *polygon = new VPath( parent );
			appendPoints( *polygon, o );
			appendPen( *polygon );
			appendBrush( *polygon );
			obj = polygon;
		}
		else if( name == "path" ) // path
		{
			storeObjectStyles( o );
			VPath *path = new VPath( parent );
			path->loadSvgPath( o.attributeNS( ooNS::svg, "d", TQString() ) );
			KoRect rect = parseViewBox( o );
			double x = KoUnit::parseValue( o.attributeNS( ooNS::svg, "x", TQString() ) );
			double y = ymirror( KoUnit::parseValue( o.attributeNS( ooNS::svg, "y", TQString() ) ) );
			double w = KoUnit::parseValue( o.attributeNS( ooNS::svg, "width", TQString() ) );
			double h = KoUnit::parseValue( o.attributeNS( ooNS::svg, "height", TQString() ) );
			TQWMatrix mat;
			mat.translate( x, y );
			mat.scale( w / rect.width(), -h / rect.height() );
			path->transform( mat );
			appendPen( *path );
			appendBrush( *path );
			obj = path;
		}
/*else if( name == "draw:image" ) // image
{
storeObjectStyles( o );
e = doc.createElement( "OBJECT" );
e.setAttribute( "type", 0 );
appendImage( doc, e, pictureElement, o );
}*/
		else
		{
			kdDebug() << "Unsupported object '" << name << "'" << endl;
			continue;
		}
		if( parent && obj )
			parent->append( obj );
		else if( obj )
			m_document.append( obj );
	}
}

void
OoDrawImport::appendPen( VObject &obj )
{
	if( m_styleStack.hasAttributeNS( ooNS::draw, "stroke" ) )
	{
		VStroke stroke;

		if( m_styleStack.attributeNS( ooNS::draw, "stroke" ) == "none" )
			stroke.setType( VStroke::none );
		else if( m_styleStack.attributeNS( ooNS::draw, "stroke" ) == "solid" )
			stroke.setType( VStroke::solid );
		else if( m_styleStack.attributeNS( ooNS::draw, "stroke" ) == "dash" )
		{
			TQValueList<float> dashes;
			stroke.setType( VStroke::solid );
			TQString style = m_styleStack.attributeNS( ooNS::draw, "stroke-dash" );
			if( style == "Ultrafine Dashed" ||
				style == "Fine Dashed (var)" || style == "Dashed (var)" )
				stroke.dashPattern().setArray( dashes << 2 << 2 );
			else if( style == "Fine Dashed" )
				stroke.dashPattern().setArray( dashes << 10 << 10 );
			else if( style == "Fine Dotted" || style == "Ultrafine Dotted (var)" ||
				style == "Line with Fine Dots" )
				stroke.dashPattern().setArray( dashes << 2 << 10 );
			else if( style == "3 Dashes 3 Dots (var)" || style == "Ultrafine 2 Dots 3 Dashes" )
				stroke.dashPattern().setArray( dashes << 3 << 3 );
			else if( style == "2 Dots 1 Dash" )
				stroke.dashPattern().setArray( dashes << 2 << 1 );
		}
		if( m_styleStack.hasAttributeNS( ooNS::svg, "stroke-width" ) )
		{
			double lwidth = KoUnit::parseValue( m_styleStack.attributeNS( ooNS::svg, "stroke-width" ) );
			if( lwidth == 0 )
				lwidth = 1.0;
			stroke.setLineWidth( lwidth );
		}
		if( m_styleStack.hasAttributeNS( ooNS::svg, "stroke-color" ) )
		{
			VColor c;
			parseColor( c, m_styleStack.attributeNS( ooNS::svg, "stroke-color" ) );
			stroke.setColor( c );
		}

		obj.setStroke( stroke );
	}
}

void
OoDrawImport::appendBrush( VObject &obj )
{
	if( m_styleStack.hasAttributeNS( ooNS::draw, "fill" ) )
	{
		const TQString fill = m_styleStack.attributeNS( ooNS::draw, "fill" );
		VFill f;

		if( fill == "solid" )
		{
			f.setType( VFill::solid );
			if( m_styleStack.hasAttributeNS( ooNS::draw, "fill-color" ) )
			{
				VColor c;
				parseColor( c, m_styleStack.attributeNS( ooNS::draw, "fill-color" ) );
				f.setColor( c );
			}
		}
		else if( fill == "gradient" )
		{
			VGradient gradient;
			gradient.clearStops();
			gradient.setRepeatMethod( VGradient::none );
			TQString style = m_styleStack.attributeNS( ooNS::draw, "fill-gradient-name" );

			TQDomElement* draw = m_draws[style];
			if( draw )
			{
				double border = 0.0;
				if( draw->hasAttributeNS( ooNS::draw, "border" ) )
					border += draw->attributeNS( ooNS::draw, "border", TQString() ).remove( '%' ).toDouble() / 100.0;
				VColor c;
				parseColor( c, draw->attributeNS( ooNS::draw, "start-color", TQString() ) );
				gradient.addStop( c, border, 0.5 );
				parseColor( c, draw->attributeNS( ooNS::draw, "end-color", TQString() ) );
				gradient.addStop( c, 1.0, 0.5 );

				TQString type = draw->attributeNS( ooNS::draw, "style", TQString() );
				if( type == "linear" || type == "axial" )
				{
					gradient.setType( VGradient::linear );
					int angle = draw->attributeNS( ooNS::draw, "angle", TQString() ).toInt() / 10;

					// make sure the angle is between 0 and 359
					angle = abs( angle );
					angle -= ( (int) ( angle / 360 ) ) * 360;

					// What we are trying to do here is to find out if the given
					// angle belongs to a horizontal, vertical or diagonal gradient.
					int lower, upper, nearAngle = 0;
					for ( lower = 0, upper = 45; upper < 360; lower += 45, upper += 45 )
					{
						if( upper >= angle )
						{
							int distanceToUpper = abs( angle - upper );
							int distanceToLower = abs( angle - lower );
							nearAngle = distanceToUpper > distanceToLower ? lower : upper;
							break;
						}
					}
					KoRect rect = obj.boundingBox();
					KoPoint origin, vector;
					// nearAngle should now be one of: 0, 45, 90, 135, 180...
					kdDebug() << "nearAngle: " << nearAngle << endl;
					if( nearAngle == 0 || nearAngle == 180 )
					{
						origin.setX( rect.x() + rect.width() );
						origin.setY( rect.y() + rect.height());
						vector.setX( rect.x() + rect.width() );
						vector.setY( rect.y() );
					}
					else if( nearAngle == 90 || nearAngle == 270 )
					{
						origin.setX( rect.x() );
						origin.setY( rect.y() + rect.height() );
						vector.setX( rect.x() + rect.width() );
						vector.setY( rect.y() + rect.height() );
					}
					else if( nearAngle == 45 || nearAngle == 225 )
					{
						origin.setX( rect.x() );
						origin.setY( rect.y() );
						vector.setX( rect.x() + rect.width() );
						vector.setY( rect.y() + rect.height() );
					}
					else if( nearAngle == 135 || nearAngle == 315 )
					{
						origin.setX( rect.x() + rect.width() );
						origin.setY( rect.y() + rect.height() );
						vector.setX( rect.x() );
						vector.setY( rect.y() );
					}

					gradient.setOrigin( origin );
					gradient.setVector( vector );
				}
				else if( type == "radial" || type == "ellipsoid" )
				{
					gradient.setType( VGradient::radial );
//else if( type == "square" || type == "rectangular" )
//gradient.setAttribute( "type", 6 ); // rectangle
//else if( type == "axial" )
//gradient.setAttribute( "type", 7 ); // pipecross

					// Hard to map between x- and y-center settings of oodraw
					// and (un-)balanced settings of kpresenter. Let's try it.
					double x, y;
					if( draw->hasAttributeNS( ooNS::draw, "cx" ) )
						x = draw->attributeNS( ooNS::draw, "cx", TQString() ).remove( '%' ).toDouble() / 100.0;
					else
						x = 0.5;

					if( draw->hasAttributeNS( ooNS::draw, "cy" ) )
						y = draw->attributeNS( ooNS::draw, "cy", TQString() ).remove( '%' ).toDouble() / 100.0;
					else
						y = 0.5;

					KoRect rect = obj.boundingBox();
					gradient.setOrigin( KoPoint( rect.x() + x * rect.width(),
												 rect.y() + y * rect.height() ) );
					gradient.setFocalPoint( KoPoint( rect.x() + x * rect.width(),
													 rect.y() + y * rect.height() ) );
					gradient.setVector( KoPoint( rect.x() + rect.width(),
												 rect.y() + y * rect.height() ) );
				}
				f.gradient() = gradient;
				f.setType( VFill::grad );
			}
		}
		obj.setFill( f );
	}
/*else if( fill == "hatch" )
{
TQDomElement brush = doc.createElement( "BRUSH" );
TQString style = m_styleStack.attributeNS( "fill-hatch-name" );
if( style == "Black 0 Degrees" )
brush.setAttribute( "style", 9 );
else if( style == "Black 90 Degrees" )
brush.setAttribute( "style", 10 );
else if( style == "Red Crossed 0 Degrees" || style == "Blue Crossed 0 Degrees" )
brush.setAttribute( "style", 11 );
else if( style == "Black 45 Degrees" || style == "Black 45 Degrees Wide" )
brush.setAttribute( "style", 12 );
else if( style == "Black -45 Degrees" )
brush.setAttribute( "style", 13 );
else if( style == "Red Crossed 45 Degrees" || style == "Blue Crossed 45 Degrees" )
brush.setAttribute( "style", 14 );

TQDomElement* draw = m_draws[style];
if( draw && draw->hasAttributeNS( "color" ) )
brush.setAttribute( "color", draw->attributeNS( "color" ) );
e.appendChild( brush );
}*/
}

void
OoDrawImport::createStyleMap( TQDomDocument &docstyles )
{
	TQDomElement styles = docstyles.documentElement();
	if( styles.isNull() )
		return;

	TQDomNode fixedStyles = KoDom::namedItemNS( styles, ooNS::office, "styles" );
	if( !fixedStyles.isNull() )
	{
		insertDraws( fixedStyles.toElement() );
		insertStyles( fixedStyles.toElement() );
	}
	TQDomNode automaticStyles = KoDom::namedItemNS( styles, ooNS::office, "automatic-styles" );
	if( !automaticStyles.isNull() )
		insertStyles( automaticStyles.toElement() );

	TQDomNode masterStyles = KoDom::namedItemNS( styles, ooNS::office, "master-styles" );
	if( !masterStyles.isNull() )
		insertStyles( masterStyles.toElement() );
}

void
OoDrawImport::insertDraws( const TQDomElement& styles )
{
	for( TQDomNode n = styles.firstChild(); !n.isNull(); n = n.nextSibling() )
	{
		TQDomElement e = n.toElement();

		if( !e.hasAttributeNS( ooNS::draw, "name" ) )
			continue;

		TQString name = e.attributeNS( ooNS::draw, "name", TQString() );
		m_draws.insert( name, new TQDomElement( e ) );
	}
}


void
OoDrawImport::insertStyles( const TQDomElement& styles )
{
	for ( TQDomNode n = styles.firstChild(); !n.isNull(); n = n.nextSibling() )
	{
		TQDomElement e = n.toElement();

		if( !e.hasAttributeNS( ooNS::style, "name" ) )
			continue;

		TQString name = e.attributeNS( ooNS::style, "name", TQString() );
		m_styles.insert( name, new TQDomElement( e ) );
		//kdDebug() << "Style: '" << name << "' loaded " << endl;
	}
}

void
OoDrawImport::fillStyleStack( const TQDomElement& object )
{
    // find all styles associated with an object and push them on the stack
    if( object.hasAttributeNS( ooNS::presentation, "style-name" ) )
        addStyles( m_styles[object.attributeNS( ooNS::presentation, "style-name", TQString() )] );

    if( object.hasAttributeNS( ooNS::draw, "style-name" ) )
        addStyles( m_styles[object.attributeNS( ooNS::draw, "style-name", TQString() )] );

    if( object.hasAttributeNS( ooNS::draw, "text-style-name" ) )
        addStyles( m_styles[object.attributeNS( ooNS::draw, "text-style-name", TQString() )] );

    if( object.hasAttributeNS( ooNS::text, "style-name" ) )
        addStyles( m_styles[object.attributeNS( ooNS::text, "style-name", TQString() )] );
}

void
OoDrawImport::addStyles( const TQDomElement* style )
{
    // this function is necessary as parent styles can have parents themself
    if( style->hasAttributeNS( ooNS::style, "parent-style-name" ) )
        addStyles( m_styles[style->attributeNS( ooNS::style, "parent-style-name", TQString() )] );

    m_styleStack.push( *style );
}

void
OoDrawImport::storeObjectStyles( const TQDomElement& object )
{
    //m_styleStack.clearPageMark(); // remove styles of previous object
    fillStyleStack( object );
    //m_styleStack.setObjectMark();
}

KoRect
OoDrawImport::parseViewBox( const TQDomElement& object )
{
	KoRect rect;
	if( !object.attributeNS( ooNS::svg, "viewBox", TQString() ).isEmpty() )
	{
		// allow for viewbox def with ',' or whitespace
		TQString viewbox( object.attributeNS( ooNS::svg, "viewBox", TQString() ) );
		TQStringList points = TQStringList::split( ' ', viewbox.replace( ',', ' ').simplifyWhiteSpace() );

		rect.setX( points[0].toFloat() );
		rect.setY( points[1].toFloat() );
		rect.setWidth( points[2].toFloat() );
		rect.setHeight( points[3].toFloat() );
	}
	return rect;
}

void
OoDrawImport::appendPoints(VPath &path, const TQDomElement& object)
{
	double x = KoUnit::parseValue( object.attributeNS( ooNS::svg, "x", TQString() ) );
	double y = KoUnit::parseValue( object.attributeNS( ooNS::svg, "y", TQString() ) );
	double w = KoUnit::parseValue( object.attributeNS( ooNS::svg, "width", TQString() ) );
	double h = KoUnit::parseValue( object.attributeNS( ooNS::svg, "height", TQString() ) );

	KoRect rect = parseViewBox( object );
	rect.setX( rect.x() + x );
	rect.setY( rect.y() + y );

	TQStringList ptList = TQStringList::split( ' ', object.attributeNS( ooNS::draw, "points", TQString() ) );

	TQString pt_x, pt_y;
	double tmp_x, tmp_y;
	KoPoint point;
	bool bFirst = true;
	for( TQStringList::Iterator it = ptList.begin(); it != ptList.end(); ++it )
	{
		tmp_x = rect.x() + ( (*it).section( ',', 0, 0 ).toInt() * w ) / rect.width();
		tmp_y = rect.y() + ( (*it).section( ',', 1, 1 ).toInt() * h ) / rect.height();

		point.setX( tmp_x );
		point.setY( ymirror( tmp_y ) );
		if( bFirst )
		{
			path.moveTo( point );
			bFirst = false;
		}
		else
			path.lineTo( point );
    }
}

void
OoDrawImport::parseColor( VColor &color, const TQString &s )
{
	if( s.startsWith( "rgb(" ) )
	{
		TQString parse = s.stripWhiteSpace();
		TQStringList colors = TQStringList::split( ',', parse );
		TQString r = colors[0].right( ( colors[0].length() - 4 ) );
		TQString g = colors[1];
		TQString b = colors[2].left( ( colors[2].length() - 1 ) );

		if( r.contains( "%" ) )
		{
			r = r.left( r.length() - 1 );
			r = TQString::number( int( ( double( 255 * r.toDouble() ) / 100.0 ) ) );
		}

		if( g.contains( "%" ) )
		{
			g = g.left( g.length() - 1 );
			g = TQString::number( int( ( double( 255 * g.toDouble() ) / 100.0 ) ) );
		}

		if( b.contains( "%" ) )
		{
			b = b.left( b.length() - 1 );
			b = TQString::number( int( ( double( 255 * b.toDouble() ) / 100.0 ) ) );
		}

		TQColor c( r.toInt(), g.toInt(), b.toInt() );
		color.set( c.red() / 255.0, c.green() / 255.0, c.blue() / 255.0 );
	}
	else
	{
		TQString rgbColor = s.stripWhiteSpace();
		TQColor c;
		if( rgbColor.startsWith( "#" ) )
			c.setNamedColor( rgbColor );
		//else
		//	c = parseColor( rgbColor );
		color.set( c.red() / 255.0, c.green() / 255.0, c.blue() / 255.0 );
	}
}

double
OoDrawImport::ymirror( double y )
{
	return m_document.height() - y;
}

#include "oodrawimport.moc"