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