19#include "kivio_common.h"
20#include "kivio_connector_point.h"
22#include <tqstringlist.h>
37float XmlReadFloat(
const TQDomElement &e,
const TQString &att,
const float &def)
40 if( e.hasAttribute( att )==
false )
44 TQString val = e.attribute( att );
48 float fVal = val.toFloat( &ok );
51 kdDebug(43000) <<
"Invalid XML-value read for " << att.ascii() <<
", expected float\n" << endl;
69void XmlWriteFloat( TQDomElement &e,
const TQString &att,
const float &val )
71 e.setAttribute( att, (
double)val );
85int XmlReadInt(
const TQDomElement &e,
const TQString &att,
const int &def)
88 if( e.hasAttribute( att )==
false )
92 TQString val = e.attribute( att,
"1" );
96 int iVal = val.toInt( &ok );
99 kdDebug(43000) <<
"Invalid XML-value read for " << att <<
" expected int\n" << endl;
117void XmlWriteInt( TQDomElement &e,
const TQString &att,
const int &val )
119 e.setAttribute( att, (
int)val );
133uint XmlReadUInt(
const TQDomElement &e,
const TQString &att,
const uint &def)
136 if( e.hasAttribute( att )==
false )
140 TQString val = e.attribute( att,
"1" );
144 uint iVal = val.toUInt( &ok );
147 kdDebug(43000) <<
"Invalid XML-value read for " << att.ascii() <<
", expected uint\n" << endl;
165void XmlWriteUInt( TQDomElement &e,
const TQString &att,
const uint &val )
167 e.setAttribute( att, (uint)val );
181TQString XmlReadString(
const TQDomElement &e,
const TQString &att,
const TQString &def )
184 if( e.hasAttribute( att )==
false )
185 return TQString(def);
187 else return e.attribute( att );
200void XmlWriteString( TQDomElement &e,
const TQString &att,
const TQString &val )
202 e.setAttribute( att, val );
216TQColor XmlReadColor(
const TQDomElement &e,
const TQString &att,
const TQColor &def)
219 if( e.hasAttribute( att )==
false )
223 TQString val = e.attribute( att,
"1" );
227 if( val.contains(
"#") )
229 newColor.setNamedColor(val);
235 uint iVal = val.toUInt( &ok );
238 kdDebug(43000) <<
"Invalid XML-value read for " << att.ascii() <<
", expected TQColor" << endl;
243 return TQColor(iVal);
256void XmlWriteColor( TQDomElement &e,
const TQString &att,
const TQColor &val )
259 e.setAttribute( att, val.name() );
273double XmlReadDouble(
const TQDomElement &e,
const TQString &att,
const double &def)
276 if( e.hasAttribute( att )==
false )
280 TQString val = e.attribute( att,
"1.0" );
284 double dVal = val.toDouble( &ok );
287 kdDebug(43000) <<
"Invalid XML-value read for ," << att.ascii() <<
" expected double" << endl;
305void XmlWriteDouble( TQDomElement &e,
const TQString &att,
const double &val )
307 e.setAttribute( att, (
double)val );
311#define WHICH_QUAD( vertex, hitPos ) \
312 ( (vertex.x() > hitPos->x()) ? ((vertex.y() > hitPos->y()) ? 1 : 4 ) : ((vertex.y() > hitPos->y())?2:3))
314#define X_INTERCEPT( point1, point2, hitY ) \
315 (point2.x() - (((point2.y()-hitY)*(point1.x()-point2.x()))/(point1.y()-point2.y())))
337bool PointInPoly( KoPoint *points,
int numPoints, KoPoint *hitPos )
340 int quad, next_quad, delta, total;
344 quad = WHICH_QUAD( points[ edge ], hitPos );
349 next = (edge + 1) % numPoints;
350 next_quad = WHICH_QUAD( points[ next ], hitPos );
353 delta = next_quad - quad;
360 if( X_INTERCEPT( points[edge], points[next], hitPos->y() ) > hitPos->x() )
377 }
while( edge != 0 );
381 if((total==4) || (total==-4))
387KoRect XmlReadRect(
const TQDomElement &e,
const TQString &att,
const KoRect &def )
390 if( e.hasAttribute( att )==
false )
394 TQString val = e.attribute( att );
396 if (val.find(
"[") == 0 && val.find(
"]") == (
int)val.length()-1) {
398 val.remove(val.length()-1,1);
399 TQStringList vlist = TQStringList::split(
",",val);
400 if (vlist.count() == 4) {
404 double x = vlist[0].toDouble(&ok);
407 double y = vlist[1].toDouble(&ok);
410 double w = vlist[2].toDouble(&ok);
413 double h = vlist[3].toDouble(&ok);
417 return KoRect(x, y, w, h);
424void XmlWriteRect( TQDomElement &e,
const TQString &att,
const KoRect &val )
426 e.setAttribute( att, TQString(
"[%1,%2,%3,%4]").arg(val.x()).arg(val.y()).arg(val.width()).arg(val.height()) );
430float shortestDistance( KivioConnectorPoint *pStart, KivioConnectorPoint *pEnd, KivioConnectorPoint *q )
435 uX = pStart->x() - pEnd->x();
436 uY = pStart->y() - pEnd->y();
438 pqX = pStart->x() - q->x();
439 pqY = pStart->y() - q->y();
441 float magTop = fabs(pqX*uY - (pqY*uX));
443 float magU = sqrt( uX*uX + uY*uY );
447 kdDebug(43000) <<
"shortestDistance() - SERIOUS ERROR: magU is 0.0f!\n";
451 return magTop / magU;