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