Map Matching
qmapscene.cpp
Go to the documentation of this file.
1 #include "qmapscene.h"
2 #include <QPen>
3 
4 #define POINT_SIZE 15
5 #define ROAD_SIZE 5
6 #define RADIUS 200
7 
8 #define KEY_ISROAD 0
9 #define KEY_ROADID 1
10 
11 QMapScene::QMapScene(QObject* parent)
12  : QGraphicsScene(parent)
13 {
14  setItemIndexMethod(QGraphicsScene::NoIndex);
15 }
16 
17 void QMapScene::initialize(int startx, int starty, int width, int height)
18 {
19  m_width = width;
20  m_height = height;
21  this->setSceneRect(QRectF(startx, starty, m_width - startx, m_height - starty));
22 }
23 
25 {
26  QGraphicsRectItem* it = pointItems[pointId];
27  it->setPen(QPen(QColor(GPSSELECTED), POINT_SIZE * 2));
28  //it->setBrush(QBrush(QColor(GPSSELECTED)));
29  // show circle 200m radius
30  m_selectionCircle->setPen(QPen(QColor(COLORCLEAR), POINT_SIZE));
31  m_selectionCircle->setPos(it->x() - RADIUS, it->y() - RADIUS);
32  m_selectionCircle->setPen(QPen(QColor(GPSSELECTED), POINT_SIZE));
33  //update();
34 }
35 
37 {
38  QList<QGraphicsItem*> list = m_selectionCircle->collidingItems();
39  for (auto* item : list) {
40  if (item->data(KEY_ISROAD).toBool()) {
41  if (basic)
42  ((QGraphicsPathItem*)item)->setPen(QPen(QColor(ROADDEFAULT)));
43  else
44  ((QGraphicsPathItem*)item)->setPen(QPen(QColor(ROADSELECTED)));
45  }
46  }
47 }
48 
49 void QMapScene::setBackToDefault(int pointId)
50 {
51  QGraphicsRectItem* it = pointItems[pointId];
52  it->setPen(QPen(QColor(GPSDEFAULT), POINT_SIZE));
53 }
54 
56 {
57  QList<QGraphicsItem*> list = m_selectionCircle->collidingItems();
58  m_neighboursId.clear();
59  for (auto* item : list) {
60  if (item->data(KEY_ISROAD).toBool()) {
61  m_neighboursId.push_back(item->data(KEY_ROADID).toLongLong());
62  }
63  }
64 }
65 
66 void QMapScene::onSignalAllPoints(std::vector<PointGPS*>* p)
67 {
68  m_trackPoints = p;
69  int count = 0;
70 
71  for (auto p : *m_trackPoints) {
72  /*
73  QGraphicsRectItem* rect = new QGraphicsRectItem(p->x(), p->y(), POINT_SIZE, POINT_SIZE);*/
74  QGraphicsRectItem* rect = new QGraphicsRectItem(0, 0, POINT_SIZE, POINT_SIZE);
75  rect->setPos(p->x(), p->y());
76  rect->setPen(QPen(QColor(GPSDEFAULT), POINT_SIZE));
77  rect->setData(KEY_ISROAD, false);
78  addItem(rect);
79 
80  pointItems.push_back(rect);
81  //qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = Q_NULLPTR
82  ++count;
83  }
84  PointGPS* p0 = m_trackPoints->at(0);
85  m_selectionCircle = new QGraphicsEllipseItem(0, 0, 2 * RADIUS, 2 * RADIUS);
86  m_selectionCircle->setPos(p0->x() - RADIUS, p0->y() - RADIUS);
87  m_selectionCircle->setPen(QPen(QColor(GPSSELECTED), POINT_SIZE));
88  addItem(m_selectionCircle);
89  emit signalTrackCompleted("Track has been graphically processed");
90 }
91 
92 void QMapScene::onSignalAllRoads(std::unordered_map<long, Road>* roads, std::vector<PointRoad>* points)
93 {
94  m_roads = roads;
95  m_roadPoints = points;
96  for (auto it : *roads) {
97  Road r = it.second;
98  const std::vector<int>& listOfPointId = it.second.vectorOfPointsId();
99  QPainterPath* path = new QPainterPath(QPointF(m_roadPoints->at(listOfPointId[0]).x(), m_roadPoints->at(listOfPointId[0]).y()));
100  for (uint i = 1; i < listOfPointId.size(); i++) {
101  path->lineTo(m_roadPoints->at(listOfPointId[i]).x(), m_roadPoints->at(listOfPointId[i]).y());
102  }
103  QGraphicsPathItem* pathItem = new QGraphicsPathItem();
104  pathItem->setPath(*path);
105  pathItem->setData(KEY_ISROAD, true);
106  qlonglong v = r.edgeId();
107  pathItem->setData(KEY_ROADID, v);
108  pathItem->setPen(QPen(QColor(ROADDEFAULT)));
109  addItem(pathItem);
110  }
111 }
112 
114 {
115  QGraphicsRectItem* it = pointItems[0];
117  emit signalItemToShow(it);
118 }
119 
121 {
122  if (pointId > 0) {
123  setBackToDefault(pointId - 1);
124  highlightRoadsDefault(true);
125  }
126  highlightSelectedPoint(pointId);
127 
128  QList<QGraphicsItem*> list = m_selectionCircle->collidingItems();
129  highlightRoadsDefault(false);
131 
132  std::cout << "Point " << pointId << std::endl;
133 
135  emit signalItemToShow(pointItems[pointId]);
136 }
137 
138 void QMapScene::onSignalRoadPath(std::vector<long>* rp)
139 {
140  bool found;
141  QPen pen(QColor(GPSSELECTED), POINT_SIZE);
142  QPen defaultPen(QColor(ROADDEFAULT));
143  foreach (QGraphicsItem* item, items()) {
144  QGraphicsPathItem* path = qgraphicsitem_cast<QGraphicsPathItem*>(item);
145  if (!path)
146  continue;
147  // check if in road path
148  found = false;
149  for (auto r : *rp) {
150  if (path->data(KEY_ROADID).toLongLong() == r) {
151  path->setPen(pen);
152  found = true;
153  }
154  }
155  if (!found)
156  path->setPen(defaultPen);
157  }
158  delete rp;
159 }
void onSignalAllRoads(std::unordered_map< long, Road > *roads, std::vector< PointRoad > *)
Definition: qmapscene.cpp:92
std::vector< QGraphicsRectItem * > pointItems
Definition: qmapscene.h:53
const std::vector< int > & vectorOfPointsId() const
Get the vector of points composing the road.
Definition: road.h:71
void setBackToDefault(int pointId)
Definition: qmapscene.cpp:49
void onSignalAllPoints(std::vector< PointGPS * > *)
Definition: qmapscene.cpp:66
#define COLORCLEAR
Definition: qmapscene.h:16
void setNeighborRoads()
Definition: qmapscene.cpp:55
std::vector< PointGPS * > * m_trackPoints
Definition: qmapscene.h:43
#define ROADDEFAULT
Definition: qmapscene.h:14
Road is an element of a network. Road are strongly linked with Points.
Definition: road.h:14
double y() const
Definition: point.cpp:52
#define GPSDEFAULT
Definition: qmapscene.h:12
double x() const
Definition: point.cpp:75
void onSignalRoadPath(std::vector< long > *rp)
Definition: qmapscene.cpp:138
#define RADIUS
Definition: qmapscene.cpp:6
std::vector< long > m_neighboursId
Definition: qmapscene.h:46
void signalNeighboursId(std::vector< long > *)
void highlightRoadsDefault(bool basic)
Definition: qmapscene.cpp:36
QGraphicsEllipseItem * m_selectionCircle
Definition: qmapscene.h:54
int m_height
Definition: qmapscene.h:49
std::unordered_map< long, Road > * m_roads
Definition: qmapscene.h:44
void onSignalStart()
Definition: qmapscene.cpp:113
#define GPSSELECTED
Definition: qmapscene.h:13
long edgeId() const
Get the id of the road.
Definition: road.h:65
#define POINT_SIZE
Definition: qmapscene.cpp:4
std::vector< PointRoad > * m_roadPoints
Definition: qmapscene.h:45
void onSignalCurrentPoint(int pointId)
Definition: qmapscene.cpp:120
QMapScene(QObject *parent=0)
Definition: qmapscene.cpp:11
void highlightSelectedPoint(int pointId)
Definition: qmapscene.cpp:24
#define ROADSELECTED
Definition: qmapscene.h:15
#define KEY_ISROAD
Definition: qmapscene.cpp:8
void signalTrackCompleted(QString)
#define KEY_ROADID
Definition: qmapscene.cpp:9
int m_width
Definition: qmapscene.h:48
void signalItemToShow(QGraphicsItem *)
void initialize(int startx, int starty, int width, int height)
Definition: qmapscene.cpp:17