misc.cpp
author insilmaril
Fri Dec 08 20:18:58 2006 +0000 (2006-12-08)
changeset 409 81ffad43d53f
parent 366 e95081c21da2
child 413 c3f7beb4e9f4
permissions -rw-r--r--
1.6.1 Port to QGraphics
     1 #include <math.h>
     2 
     3 #include <qregexp.h>
     4 #include <qpoint.h>
     5 #include <stdlib.h>
     6 
     7 #include "misc.h"
     8 
     9 QString qpointToString (const QPoint &p)
    10 {
    11 	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
    12 }
    13 
    14 QString qpointfToString (const QPointF &p)
    15 {
    16 // FIXME check this QT4
    17 	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
    18 }
    19 
    20 ostream &operator<< (ostream &stream, QPoint const &p)
    21 { 
    22 	stream << "("<<p.x()<<","<<p.y()<<")";
    23 	return stream;
    24 }
    25 
    26 ostream &operator<< (ostream &stream, QPointF const &p)
    27 { 
    28 	stream << "("<<p.x()<<","<<p.y()<<")";
    29 	return stream;
    30 }
    31 
    32 qreal getAngle(const QPointF &p)
    33 {	
    34 	// Calculate angle of vector to y-axis
    35 	if (p.y()==0)
    36 	{
    37 		if (p.x()>=0)
    38 			return M_PI_2;
    39 		else
    40 			return 3* M_PI_2;
    41 	} else
    42 	{
    43 		if (p.y()>0) 
    44 			return (qreal)(M_PI  - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    45 		else	
    46 			if (p.x()<0)
    47 				return (qreal)( 2*M_PI - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    48 			else	
    49 				return (qreal)( - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    50 	}	
    51 }
    52 
    53 QPointF normalise(const QPointF &p)
    54 {	
    55 	// Calculate normalised position (fixed length) 
    56 
    57 	qreal px=p.x();
    58 	qreal py=p.y();
    59 	qreal x;
    60 	qreal y;
    61 	qreal r=150;
    62 
    63 	if (px==0)
    64 	{
    65 		x=0;
    66 		if (py>=0)
    67 			y=r;
    68 		else
    69 			y=-r;
    70 	} else
    71 	{
    72 		qreal sign;
    73 		qreal a;
    74 		if (px>0) 
    75 			sign=1; 
    76 		else 
    77 			sign=-1;
    78 		
    79 		a=atan (py / px);
    80 		x=cos (a) * r *sign;
    81 		y=sin (a) * r *sign;
    82 	}	
    83 	return QPoint ((int) (x),(int) (y));
    84 }
    85 
    86 
    87 // returns masked "<" ">" "&"
    88 QString quotemeta(const QString &s)
    89 {
    90 	QString r=s;
    91     QRegExp  rx("&(?!amp;)");
    92     r.replace ( rx,"&amp;");
    93     rx.setPattern( ">");
    94     r.replace ( rx,"&gt;");
    95     rx.setPattern( "<");
    96     r.replace ( rx,"&lt;");
    97     rx.setPattern( "\"");
    98     r.replace ( rx,"&quot;");
    99     return r;
   100 }
   101 
   102 
   103 qreal max(qreal a, qreal b)
   104 {
   105 	if (a>b)
   106 		return a;
   107 	return b;
   108 }
   109 
   110 int xmlObj::actindent=0;		// make instance of actindent
   111 
   112 xmlObj::xmlObj()
   113 {
   114     indentwidth=4;
   115 }
   116 
   117 
   118 // returns <s at />
   119 QString xmlObj::singleElement(QString s, QString at)
   120 {
   121     return indent() + "<" + s +" " + at +" " + "/>\n";
   122 }
   123 
   124 // returns <s>
   125 QString xmlObj::beginElement(QString s)
   126 {
   127     return indent() + "<" + s + ">\n";
   128 }
   129 
   130 // returns <s at>
   131 QString xmlObj::beginElement(QString s, QString at)
   132 {
   133     return indent() + "<" + s + " " + at + ">\n";
   134 }
   135 
   136 // returns </s>
   137 QString xmlObj::endElement(QString s)
   138 {
   139     return indent() + "</" + s + ">\n";
   140 }
   141 
   142 // returns  at="val"
   143 QString xmlObj::attribut(QString at, QString val)
   144 {
   145     return " " + at + "=\"" + quotemeta (val) + "\""; 
   146 }
   147 
   148 // returns <s> val </s>
   149 QString xmlObj::valueElement(QString el, QString val)
   150 {
   151     return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
   152 }
   153 
   154 // returns <s at> val </s>
   155 QString xmlObj::valueElement(QString el, QString val, QString at)
   156 {
   157     return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
   158 }
   159 
   160 void xmlObj::incIndent()
   161 {
   162     actindent++;
   163 }	
   164 
   165 void xmlObj::decIndent()
   166 {
   167     actindent--;
   168     if (actindent<0) actindent=0;
   169 }	
   170 
   171 QString xmlObj::indent()
   172 {
   173     QString s;
   174     int i;
   175     for (i=0; i<actindent*indentwidth; i++)
   176     {
   177 		s= s + " ";
   178     }
   179     return s;
   180 }	
   181