35 #include "KDGanttXMLTools.h"
41 namespace KDGanttXML {
43 void createBoolNode( TQDomDocument& doc, TQDomNode& parent,
44 const TQString& elementName,
bool value )
46 TQDomElement newElement =
47 doc.createElement( elementName );
48 parent.appendChild( newElement );
49 TQDomText elementContent =
50 doc.createTextNode( value ?
"true" :
"false" );
51 newElement.appendChild( elementContent );
56 void createSizeNode( TQDomDocument& doc, TQDomNode& parent,
57 const TQString& elementName,
const TQSize& value )
59 TQDomElement newElement =
60 doc.createElement( elementName );
61 parent.appendChild( newElement );
62 newElement.setAttribute(
"Width", value.width() );
63 newElement.setAttribute(
"Height", value.height() );
67 void createIntNode( TQDomDocument& doc, TQDomNode& parent,
68 const TQString& elementName,
int value )
70 TQDomElement newElement =
71 doc.createElement( elementName );
72 parent.appendChild( newElement );
73 TQDomText elementContent =
74 doc.createTextNode( TQString::number( value ) );
75 newElement.appendChild( elementContent );
79 void createDoubleNode( TQDomDocument& doc, TQDomNode& parent,
80 const TQString& elementName,
double value )
82 TQDomElement newElement =
83 doc.createElement( elementName );
84 parent.appendChild( newElement );
85 TQDomText elementContent =
86 doc.createTextNode( TQString::number( value ) );
87 newElement.appendChild( elementContent );
91 void createStringNode( TQDomDocument& doc, TQDomNode& parent,
92 const TQString& elementName,
93 const TQString& text )
95 TQDomElement newElement =
96 doc.createElement( elementName );
97 parent.appendChild( newElement );
98 TQDomText elementContent =
99 doc.createTextNode( text );
100 newElement.appendChild( elementContent );
104 void createColorNode( TQDomDocument& doc, TQDomNode& parent,
105 const TQString& elementName,
const TQColor& color )
107 TQDomElement colorElement = doc.createElement( elementName );
108 parent.appendChild( colorElement );
109 colorElement.setAttribute(
"Red",
110 TQString::number( color.red() ) );
111 colorElement.setAttribute(
"Green",
112 TQString::number( color.green() ) );
113 colorElement.setAttribute(
"Blue",
114 TQString::number( color.blue() ) );
118 void createBrushNode( TQDomDocument& doc, TQDomNode& parent,
119 const TQString& elementName,
const TQBrush& brush )
122 TQDomElement brushElement = doc.createElement( elementName );
123 parent.appendChild( brushElement );
124 createColorNode( doc, brushElement,
"Color", brush.color() );
125 createStringNode( doc, brushElement,
"Style",
126 KDGanttXML::brushStyleToString( brush.style() ) );
127 if( brush.style() == TQt::CustomPattern && brush.pixmap() )
128 createPixmapNode( doc, brushElement,
"Pixmap", *brush.pixmap() );
132 void createPixmapNode( TQDomDocument& doc, TQDomNode& parent,
133 const TQString& elementName,
const TQPixmap& pixmap )
135 TQDomElement pixmapElement = doc.createElement( elementName );
136 parent.appendChild( pixmapElement );
142 TQBuffer buffer( ba );
143 buffer.open( IO_WriteOnly );
144 TQImageIO imgio( &buffer,
"XPM" );
145 TQImage image = pixmap.convertToImage();
146 imgio.setImage( image );
149 ulong len = ba.size() * 2;
150 TQByteArray bazip( len );
151 ::compress( (uchar*) bazip.data(), &len, (uchar*) ba.data(), ba.size() );
153 static const char hexchars[] =
"0123456789abcdef";
154 for (
int i = 0; i < (int)len; ++i ) {
155 uchar c = (uchar) bazip[i];
156 dataString += hexchars[c >> 4];
157 dataString += hexchars[c & 0x0f];
160 createStringNode( doc, pixmapElement,
"Format",
"XPM.GZ" );
161 createIntNode( doc, pixmapElement,
"Length", ba.size() );
162 createStringNode( doc, pixmapElement,
"Data", dataString );
166 void createRectNode( TQDomDocument& doc, TQDomNode& parent,
167 const TQString& elementName,
const TQRect& rect )
169 TQDomElement rectElement = doc.createElement( elementName );
170 parent.appendChild( rectElement );
171 TQDomElement xElement = doc.createElement(
"X" );
172 rectElement.appendChild( xElement );
173 TQDomText xContent = doc.createTextNode( TQString::number( rect.x() ) );
174 xElement.appendChild( xContent );
175 TQDomElement yElement = doc.createElement(
"Y" );
176 rectElement.appendChild( yElement );
177 TQDomText yContent = doc.createTextNode( TQString::number( rect.y() ) );
178 yElement.appendChild( yContent );
179 TQDomElement widthElement = doc.createElement(
"Width" );
180 rectElement.appendChild( widthElement );
181 TQDomText widthContent = doc.createTextNode( TQString::number( rect.width() ) );
182 widthElement.appendChild( widthContent );
183 TQDomElement heightElement = doc.createElement(
"Height" );
184 rectElement.appendChild( heightElement );
185 TQDomText heightContent = doc.createTextNode( TQString::number( rect.height() ) );
186 heightElement.appendChild( heightContent );
190 void createStringListNodes( TQDomDocument& doc, TQDomNode& parent,
191 const TQString& elementName,
192 const TQStringList* list )
197 for( TQStringList::ConstIterator it = list->begin();
198 it != list->end(); ++it ) {
199 TQDomElement element = doc.createElement( elementName );
200 parent.appendChild( element );
201 TQDomText elementContent = doc.createTextNode( *it );
202 element.appendChild( elementContent );
207 void createFontNode( TQDomDocument& doc, TQDomNode& parent,
208 const TQString& elementName,
const TQFont& font )
210 TQDomElement fontElement = doc.createElement( elementName );
211 parent.appendChild( fontElement );
212 createStringNode( doc, fontElement,
"Family", font.family() );
213 createIntNode( doc, fontElement,
"PointSize", font.pointSize() );
214 createIntNode( doc, fontElement,
"PixelSize", font.pixelSize() );
215 createIntNode( doc, fontElement,
"Weight", font.weight() );
216 createBoolNode( doc, fontElement,
"Italic", font.italic() );
220 void createPenNode( TQDomDocument& doc, TQDomNode& parent,
221 const TQString& elementName,
const TQPen& pen )
223 TQDomElement penElement = doc.createElement( elementName );
224 parent.appendChild( penElement );
225 createIntNode( doc, penElement,
"Width", pen.width() );
226 createColorNode( doc, penElement,
"Color", pen.color() );
227 createStringNode( doc, penElement,
"Style", penStyleToString( pen.style() ) );
231 void createDateTimeNode( TQDomDocument& doc, TQDomNode& parent,
232 const TQString& elementName,
233 const TQDateTime& datetime )
235 TQDomElement dateTimeElement = doc.createElement( elementName );
236 parent.appendChild( dateTimeElement );
237 createDateNode( doc, dateTimeElement,
"Date", datetime.date() );
238 createTimeNode( doc, dateTimeElement,
"Time", datetime.time() );
242 void createDateNode( TQDomDocument& doc, TQDomNode& parent,
243 const TQString& elementName,
const TQDate& date )
245 TQDomElement dateElement = doc.createElement( elementName );
246 parent.appendChild( dateElement );
247 dateElement.setAttribute(
"Year", TQString::number( date.year() ) );
248 dateElement.setAttribute(
"Month", TQString::number( date.month() ) );
249 dateElement.setAttribute(
"Day", TQString::number( date.day() ) );
253 void createTimeNode( TQDomDocument& doc, TQDomNode& parent,
254 const TQString& elementName,
const TQTime& time )
256 TQDomElement timeElement = doc.createElement( elementName );
257 parent.appendChild( timeElement );
258 timeElement.setAttribute(
"Hour",
259 TQString::number( time.hour() ) );
260 timeElement.setAttribute(
"Minute",
261 TQString::number( time.minute() ) );
262 timeElement.setAttribute(
"Second",
263 TQString::number( time.second() ) );
264 timeElement.setAttribute(
"Millisecond",
265 TQString::number( time.msec() ) );
269 TQString penStyleToString( TQt::PenStyle style )
280 case TQt::DashDotLine:
281 return "DashDotLine";
282 case TQt::DashDotDotLine:
283 return "DashDotDotLine";
291 TQString brushStyleToString( TQt::BrushStyle style )
297 case TQt::SolidPattern:
298 return "SolidPattern";
299 case TQt::Dense1Pattern:
300 return "Dense1Pattern";
301 case TQt::Dense2Pattern:
302 return "Dense2Pattern";
303 case TQt::Dense3Pattern:
304 return "Dense3Pattern";
305 case TQt::Dense4Pattern:
306 return "Dense4Pattern";
307 case TQt::Dense5Pattern:
308 return "Dense5Pattern";
309 case TQt::Dense6Pattern:
310 return "Dense6Pattern";
311 case TQt::Dense7Pattern:
312 return "Dense7Pattern";
313 case TQt::HorPattern:
315 case TQt::VerPattern:
317 case TQt::CrossPattern:
318 return "CrossPattern";
319 case TQt::BDiagPattern:
320 return "BDiagPattern";
321 case TQt::FDiagPattern:
322 return "FDiagPattern";
323 case TQt::DiagCrossPattern:
324 return "DiagCrossPattern";
326 return "SolidPattern";
331 bool readStringNode(
const TQDomElement& element, TQString& value )
333 value = element.text();
338 bool readIntNode(
const TQDomElement& element,
int& value )
341 int temp = element.text().toInt( &ok );
348 bool readDoubleNode(
const TQDomElement& element,
double& value )
351 double temp = element.text().toDouble( &ok );
358 bool readBoolNode(
const TQDomElement& element,
bool& value )
360 if( element.text() ==
"true" ) {
363 }
else if( element.text() ==
"false" ) {
371 bool readColorNode(
const TQDomElement& element, TQColor& value )
374 int red, green, blue;
375 if( element.hasAttribute(
"Red" ) ) {
377 red = element.attribute(
"Red" ).toInt( &redOk );
380 if( element.hasAttribute(
"Green" ) ) {
381 bool greenOk =
false;
382 green = element.attribute(
"Green" ).toInt( &greenOk );
385 if( element.hasAttribute(
"Blue" ) ) {
387 blue = element.attribute(
"Blue" ).toInt( &blueOk );
392 value.setRgb( red, green, blue );
398 bool readBrushNode(
const TQDomElement& element, TQBrush& brush )
402 TQt::BrushStyle tempStyle;
404 TQDomNode node = element.firstChild();
405 while( !node.isNull() ) {
406 TQDomElement element = node.toElement();
407 if( !element.isNull() ) {
408 TQString tagName = element.tagName();
409 if( tagName ==
"Color" ) {
410 ok = ok & readColorNode( element, tempColor );
411 }
else if( tagName ==
"Style" ) {
413 ok = ok & readStringNode( element, value );
414 tempStyle = stringToBrushStyle( value );
415 }
else if( tagName ==
"Pixmap" ) {
416 ok = ok & readPixmapNode( element, tempPixmap );
418 tqDebug(
"Unknown tag in brush" );
421 node = node.nextSibling();
425 brush.setColor( tempColor );
426 brush.setStyle( tempStyle );
427 if( !tempPixmap.isNull() )
428 brush.setPixmap( tempPixmap );
435 bool readPixmapNode(
const TQDomElement& element, TQPixmap& pixmap )
440 TQDomNode node = element.firstChild();
441 while( !node.isNull() ) {
442 TQDomElement element = node.toElement();
443 if( !element.isNull() ) {
444 TQString tagName = element.tagName();
445 if( tagName ==
"Format" ) {
447 ok = ok & readStringNode( element, formatName );
449 if( formatName !=
"XPM.GZ" )
450 tqDebug(
"Unsupported pixmap format in XML file" );
452 }
else if( tagName ==
"Length" ) {
453 ok = ok & readIntNode( element, tempLengthi );
454 }
else if( tagName ==
"Data" ) {
455 ok = ok & readStringNode( element, tempData );
457 tqDebug(
"Unknown tag in Pixmap" );
460 node = node.nextSibling();
464 if( 0 < tempLengthi ) {
466 char *ba =
new char[ tempData.length() / 2 ];
467 for (
int i = 0; i < (int)tempData.length() / 2; ++i ) {
468 char h = tempData[ 2 * i ].latin1();
469 char l = tempData[ 2 * i + 1 ].latin1();
483 if( tempLengthi < (
int)tempData.length() * 5 )
484 tempLengthi = tempData.length() * 5;
485 unsigned long tempLength = tempLengthi;
486 TQByteArray baunzip( tempLength );
487 ::uncompress( (uchar*) baunzip.data(), &tempLength,
488 (uchar*) ba, tempData.length()/2 );
490 image.loadFromData( (
const uchar*)baunzip.data(), tempLength,
"XPM" );
493 pixmap.resize( 0, 0 );
495 ok = ok & pixmap.convertFromImage( image, 0 );
497 pixmap.resize( 0, 0 );
504 bool readPenNode(
const TQDomElement& element, TQPen& pen )
509 TQt::PenStyle tempStyle;
510 TQDomNode node = element.firstChild();
511 while( !node.isNull() ) {
512 TQDomElement element = node.toElement();
513 if( !element.isNull() ) {
514 TQString tagName = element.tagName();
515 if( tagName ==
"Width" ) {
516 ok = ok & readIntNode( element, tempWidth );
517 }
else if( tagName ==
"Color" ) {
518 ok = ok & readColorNode( element, tempColor );
519 }
else if( tagName ==
"Style" ) {
521 ok = ok & readStringNode( element, value );
522 tempStyle = stringToPenStyle( value );
524 tqDebug(
"Unknown tag in brush" );
527 node = node.nextSibling();
531 pen.setWidth( tempWidth );
532 pen.setColor( tempColor );
533 pen.setStyle( tempStyle );
539 bool readFontNode(
const TQDomElement& element, TQFont& font )
543 int pointSize, pixelSize, weight;
546 TQDomNode node = element.firstChild();
547 while( !node.isNull() ) {
548 TQDomElement element = node.toElement();
549 if( !element.isNull() ) {
550 TQString tagName = element.tagName();
551 if( tagName ==
"Family" ) {
552 ok = ok & readStringNode( element, family );
553 }
else if( tagName ==
"PointSize" ) {
554 ok = ok & readIntNode( element, pointSize );
555 }
else if( tagName ==
"PixelSize" ) {
556 ok = ok & readIntNode( element, pixelSize );
557 }
else if( tagName ==
"Weight" ) {
558 ok = ok & readIntNode( element, weight );
559 }
else if( tagName ==
"Italic" ) {
560 ok = ok & readBoolNode( element, italic );
561 }
else if( tagName ==
"CharSet" ) {
562 ok = ok & readIntNode( element, charSet );
564 tqDebug(
"Unknown tag in color map" );
567 node = node.nextSibling();
571 font.setFamily( family );
572 if ( pointSize > 0 ) font.setPointSize( pointSize );
573 if ( pixelSize > 0 ) font.setPixelSize( pixelSize );
574 font.setWeight( weight );
575 font.setItalic( italic );
581 bool readRectNode(
const TQDomElement& element, TQRect& value )
584 int width, height, x, y;
585 TQDomNode node = element.firstChild();
586 while( !node.isNull() ) {
587 TQDomElement element = node.toElement();
588 if( !element.isNull() ) {
589 TQString tagName = element.tagName();
590 if( tagName ==
"Width" ) {
591 ok = ok & readIntNode( element, width );
592 }
else if( tagName ==
"Height" ) {
593 ok = ok & readIntNode( element, height );
594 }
else if( tagName ==
"X" ) {
595 ok = ok & readIntNode( element, x );
596 }
else if( tagName ==
"Y" ) {
597 ok = ok & readIntNode( element, y );
599 tqDebug(
"Unknown tag in rect" );
602 node = node.nextSibling();
608 value.setWidth( width );
609 value.setHeight( height );
617 bool readDateTimeNode(
const TQDomElement& element, TQDateTime& datetime )
622 TQDomNode node = element.firstChild();
623 while( !node.isNull() ) {
624 TQDomElement element = node.toElement();
625 if( !element.isNull() ) {
626 TQString tagName = element.tagName();
627 if( tagName ==
"Date" ) {
628 ok = ok & readDateNode( element, tempDate );
629 }
else if( tagName ==
"Time" ) {
630 ok = ok & readTimeNode( element, tempTime );
632 tqDebug(
"Unknown tag in datetime" );
635 node = node.nextSibling();
639 datetime.setDate( tempDate );
640 datetime.setTime( tempTime );
647 bool readDateNode(
const TQDomElement& element, TQDate& value )
650 int year, month, day;
651 if( element.hasAttribute(
"Year" ) ) {
653 year = element.attribute(
"Year" ).toInt( &yearOk );
656 if( element.hasAttribute(
"Month" ) ) {
657 bool monthOk =
false;
658 month = element.attribute(
"Month" ).toInt( &monthOk );
661 if( element.hasAttribute(
"Day" ) ) {
663 day = element.attribute(
"Day" ).toInt( &dayOk );
668 value.setYMD( year, month, day );
675 bool readTimeNode(
const TQDomElement& element, TQTime& value )
678 int hour, minute, second, msec;
679 if( element.hasAttribute(
"Hour" ) ) {
681 hour = element.attribute(
"Hour" ).toInt( &hourOk );
684 if( element.hasAttribute(
"Minute" ) ) {
685 bool minuteOk =
false;
686 minute = element.attribute(
"Minute" ).toInt( &minuteOk );
689 if( element.hasAttribute(
"Second" ) ) {
690 bool secondOk =
false;
691 second = element.attribute(
"Second" ).toInt( &secondOk );
694 if( element.hasAttribute(
"Millisecond" ) ) {
696 msec = element.attribute(
"Millisecond" ).toInt( &msecOk );
701 value.setHMS( hour, minute, second, msec );
708 TQt::PenStyle stringToPenStyle(
const TQString& style )
710 if( style ==
"NoPen" )
712 else if( style ==
"SolidLine" )
713 return TQt::SolidLine;
714 else if( style ==
"DashLine" )
715 return TQt::DashLine;
716 else if( style ==
"DotLine" )
718 else if( style ==
"DashDotLine" )
719 return TQt::DashDotLine;
720 else if( style ==
"DashDotDotLine" )
721 return TQt::DashDotDotLine;
723 return TQt::SolidLine;
727 TQt::BrushStyle stringToBrushStyle(
const TQString& style )
730 if( style ==
"NoBrush" )
732 else if( style ==
"SolidPattern" )
733 return TQt::SolidPattern;
734 else if( style ==
"Dense1Pattern" )
735 return TQt::Dense1Pattern;
736 else if( style ==
"Dense2Pattern" )
737 return TQt::Dense2Pattern;
738 else if( style ==
"Dense3Pattern" )
739 return TQt::Dense3Pattern;
740 else if( style ==
"Dense4Pattern" )
741 return TQt::Dense4Pattern;
742 else if( style ==
"Dense5Pattern" )
743 return TQt::Dense5Pattern;
744 else if( style ==
"Dense6Pattern" )
745 return TQt::Dense6Pattern;
746 else if( style ==
"Dense7Pattern" )
747 return TQt::Dense7Pattern;
748 else if( style ==
"HorPattern" )
749 return TQt::HorPattern;
750 else if( style ==
"VerPattern" )
751 return TQt::VerPattern;
752 else if( style ==
"CrossPattern" )
753 return TQt::CrossPattern;
754 else if( style ==
"BDiagPattern" )
755 return TQt::BDiagPattern;
756 else if( style ==
"FDiagPattern" )
757 return TQt::FDiagPattern;
758 else if( style ==
"DiagCrossPattern" )
759 return TQt::DiagCrossPattern;
761 return TQt::SolidPattern;