1 //***************************************************************
2 // CLass: Utils
3 //
4 // Description: Various static functions
5 //
6 //
7 // Author: Chris Browet <cbro@semperpax.com> (C) 2010
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //******************************************************************
12
13 #include "Utils.h"
14 #include "MerkaartorPreferences.h"
15
16 #include <QNetworkAccessManager>
17 #include <QNetworkReply>
18 #include <QEventLoop>
19 #include <QTimer>
20
encodeAttributes(const QString & text)21 const QString Utils::encodeAttributes(const QString & text)
22 {
23 QString s = text;
24 s.replace( "&", "&" );
25 s.replace( ">", ">" );
26 s.replace( "<", "<" );
27 s.replace( "\"", """ );
28 s.replace( "\'", "'" );
29 return s;
30 }
31
QRectInterstects(const QRectF & r,const QLineF & lF,QPointF & a,QPointF & b)32 bool Utils::QRectInterstects(const QRectF& r, const QLineF& lF, QPointF& a, QPointF& b)
33 {
34 QPointF pF;
35 bool hasP1 = false;
36 bool hasP2 = false;
37
38 if (QLineF(r.topLeft(), r.bottomLeft()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
39 a = pF;
40 hasP1 = true;
41 }
42 if (QLineF(r.bottomLeft(), r.bottomRight()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
43 if (hasP1) {
44 b = pF;
45 hasP2 = true;
46 } else {
47 a = pF;
48 hasP1 = true;
49 }
50 }
51 if (QLineF(r.bottomRight(), r.topRight()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
52 if (hasP1) {
53 b = pF;
54 hasP2 = true;
55 } else {
56 a = pF;
57 hasP1 = true;
58 }
59 }
60 if (QLineF(r.topRight(), r.topLeft()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
61 if (hasP1) {
62 b = pF;
63 hasP2 = true;
64 } else {
65 a = pF;
66 hasP1 = true;
67 }
68 }
69
70 if (hasP1 && hasP2) {
71 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
72 qreal la1 = QLineF(a,b).angleTo(lF);
73 #else
74 qreal la1 = QLineF(a,b).angle(lF);
75 #endif
76 if (la1 > 15.0 && la1 < 345.0) {
77 QPointF t = b;
78 b = a;
79 a = t;
80 }
81 }
82 if (hasP1)
83 return true;
84 else
85 return false;
86 }
87
sendBlockingNetRequest(const QUrl & theUrl,QString & reply)88 bool Utils::sendBlockingNetRequest(const QUrl& theUrl, QString& reply)
89 {
90 QNetworkAccessManager manager;
91 QEventLoop q;
92 QTimer tT;
93
94 manager.setProxy(M_PREFS->getProxy(QUrl("http://merkaartor.be")));
95
96 tT.setSingleShot(true);
97 connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
98 connect(&manager, SIGNAL(finished(QNetworkReply*)),
99 &q, SLOT(quit()));
100
101 QNetworkReply *netReply = manager.get(QNetworkRequest(theUrl));
102
103 tT.start(M_PREFS->getNetworkTimeout());
104 q.exec();
105 if(tT.isActive()) {
106 // download complete
107 tT.stop();
108 } else {
109 return false;
110 }
111
112 reply = netReply->readAll();
113 return true;
114 }
115
116