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