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