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