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