-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPaintLayer.cpp
266 lines (229 loc) · 8.03 KB
/
PaintLayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*!
* @file PaintLayer.cpp
* @author Nyfor, RetroShare Team
*
* Copyright (C) 2014 - Nyfor, RetroShare Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "PaintLayer.h"
#include <QMessageBox>
#include <QFileInfo>
#include <QMap>
#include <QFont>
#include <math.h>
#include <retroshare/rsdisc.h>
#include <marble/GeoDataLineString.h>
#include "gui/common/AvatarDefs.h"
#define PI 3.14159265
//!
//! \brief PaintLayer::PaintLayer
// Obtains settings and the map data file.
//! \param peers
//! \param settings
//!
PaintLayer::PaintLayer(RsPeers *peers, RsDisc *disc, const FriendMapSettings *settings)
{
this->rsPeers = peers;
this->rsDisc = disc;
this->mSettings = settings;
const std::string& geoip_data_path = settings->getGeoIpDataPath();
showingLinks = settings->getShowLinks();
this->geoip = GeoIP_open(geoip_data_path.c_str(), GEOIP_STANDARD);
if(geoip == NULL){
std::cerr<<"Error: GeoLiteCity.dat not found!"<<std::endl;
//QMessageBox::information(this, tr("FriendMap"), QString(tr("Error: GeoLiteCity.dat not found!")));
}
genPeerCache();
}
//!
//! \brief PaintLayer::~PaintLayer
//!
PaintLayer::~PaintLayer()
{
GeoIP_delete(this->geoip);
}
//!
//! \brief PaintLayer::renderPosition
//! \return
//!
QStringList PaintLayer::renderPosition() const
{
// We will paint in exactly one of the following layers.
// The current one can be changed by pressing the '+' key
/*
* "NONE"
* "STARS"
* "BEHIND_TARGET"
* "SURFACE"
* "HOVERS_ABOVE_SURFACE"
* "ATMOSPHERE"
* "ORBIT"
* "ALWAYS_ON_TOP"
* "FLOAT_ITEM"
* "USER_TOOLS"
*/
QStringList layers = QStringList() << "ALWAYS_ON_TOP"<<"SURFACE" << "HOVERS_ABOVE_SURFACE";
layers << "ORBIT" << "USER_TOOLS" << "STARS";
return QStringList()<<layers.at(0);
}
//!
//! \brief The GeoPeerLoc class
//!
class GeoPeerLoc{
public:
QString gpg_id;
QString ssl_id;
QString name;
GeoDataCoordinates coord;
GeoDataCoordinates coordOff;
};
//!
//! \brief The GeoPeer class
//!
class GeoPeer{
public:
QString gpg_id;
QList<GeoPeerLoc> locations;
//QMap<QString, GeoPeer> connections;//could use this
QList<std::string> connectionsList;
QPixmap avatar;
};
//!
//!//! \brief geoPeers
//!
QList<GeoPeer> geoPeers;
QMap<std::string, GeoPeer> peerTable;
void PaintLayer::genPeerCache(){
srand(42);
std::list<std::string> gpg_ids;
rsPeers->getGPGAcceptedList(gpg_ids);
gpg_ids.push_back(rsPeers->getGPGId(rsPeers->getOwnId()));
//QList<GeoPeer> geoPeers;
//QMap<QString, GeoPeer> peerTable;
geoPeers.clear();
peerTable.clear();
//LIST GPG
foreach(const std::string& gpg_id, gpg_ids){
GeoPeer gFriend;
gFriend.locations.clear();
RsPeerDetails peer_details;
//rsPeers->getGPGDetails(gpg_id, gpg_detail);
rsPeers->getPeerDetails(gpg_id, peer_details);
std::list<std::string> ssl_ids;
rsPeers->getAssociatedSSLIds(gpg_id, ssl_ids);
//LIST SSL
foreach(const std::string& ssl_id, ssl_ids){
RsPeerDetails peer_ssl_details;
rsPeers->getPeerDetails(ssl_id, peer_ssl_details);
if(peer_ssl_details.extAddr.compare("0.0.0.0")){
//std::cout<<peer_ssl_details.extAddr<<"\n";
GeoIPRecord *r = GeoIP_record_by_name(geoip, peer_ssl_details.extAddr.c_str());
if(r){
GeoPeerLoc sLocation;
sLocation.coord = GeoDataCoordinates(r->longitude, r->latitude, 0.0, GeoDataCoordinates::Degree);
float rr = ((double) rand() / (RAND_MAX))*PI*2.0f;
sLocation.coordOff = GeoDataCoordinates(r->longitude+cos(rr)*0.3f, r->latitude+sin(rr)*0.3f, 0.0, GeoDataCoordinates::Degree);
sLocation.ssl_id = QString::fromStdString(ssl_id);
sLocation.name = QString::fromUtf8(peer_ssl_details.name.c_str());
gFriend.locations.push_back(sLocation);
GeoIPRecord_delete(r);
}
}
}
if(gFriend.locations.length()>0){
gFriend.gpg_id = QString::fromStdString(gpg_id);
if(mSettings->getShowAvatars()){
AvatarDefs::getAvatarFromGpgId(gpg_id, gFriend.avatar);
gFriend.avatar = gFriend.avatar.scaledToWidth(22);
}
geoPeers.push_back(gFriend);
peerTable.insert(gpg_id,gFriend);
}
}
//Cache connections
QList<GeoPeer>::iterator geoPeer;
for (geoPeer = geoPeers.begin(); geoPeer != geoPeers.end(); ++geoPeer){
//foreach(GeoPeer geoPeer, geoPeers){
std::list<std::string> friendList;
rsDisc->getDiscGPGFriends(geoPeer->gpg_id.toStdString(), friendList);
//geoPeer->connections.clear();
geoPeer->connectionsList.clear();
foreach(const std::string& gpg_id, friendList){
if (peerTable.contains(gpg_id)){
//QString q_id = QString::fromStdString(gpg_id);
//GeoPeer gp = peerTable[q_id];
//geoPeer->connections.insert(q_id,gp);
geoPeer->connectionsList.push_back(gpg_id);
}
}
}
}
//!
//! \brief PaintLayer::render
//! \param painter
//! \param viewport
//! \param renderPos
//! \param layer
//! \return
//!
bool PaintLayer::render( GeoPainter *painter, ViewportParams *viewport,
const QString& renderPos, GeoSceneLayer * layer )
{
srand(42);
foreach(const GeoPeer& geoPeerc, geoPeers){
GeoPeer geoPeer = geoPeerc;
foreach(const GeoPeerLoc& geoPeerLoc, geoPeer.locations){
float rr = ((double) rand() / (RAND_MAX));
//GeoDataCoordinates coord = geoPeerLoc.coord;
GeoDataCoordinates coord = geoPeerLoc.coordOff;
//std::cerr << "FRIENDMAP: show avatars: " << mSettings->getShowAvatars() << "\n";
if(mSettings->getShowAvatars())
painter->drawPixmap(coord, geoPeer.avatar);
if(rsPeers->isOnline(geoPeerLoc.ssl_id.toStdString()))
painter->setPen(Qt::green);
else
painter->setPen(Qt::red);
painter->drawEllipse(geoPeerLoc.coord, 2.5f+5.f*rr, 2.5f+5.f*rr);
painter->drawEllipse(coord, 10, 10);
GeoDataLineString cLine;
cLine.append(coord);
cLine.append(geoPeerLoc.coord);
painter->drawPolyline(cLine);
painter->setPen(Qt::white);
QFont fonz;
fonz.setBold(true);
fonz.setStyleStrategy(QFont::ForceOutline);
painter->setFont(fonz);
painter->drawText(coord, geoPeerLoc.name);
if(showingLinks){
painter->setPen(Qt::yellow);
foreach(const std::string& gpg_id, geoPeer.connectionsList){
GeoPeer other = peerTable[gpg_id];
if (other.locations.length()>0){
GeoPeerLoc oloc = other.locations.first();
GeoDataLineString conLine;
conLine.append(oloc.coord);
conLine.append(geoPeerLoc.coord);
painter->drawPolyline(conLine);
} else std::cerr << "error, missing all locations for a peer\n";
}
}
}
}
return true;
}
// eof