No Description

map.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "map.h"
  2. #include <QDebug>
  3. #include <QDesktopWidget>
  4. #include <QPainter>
  5. #include <QPen>
  6. Map::Map(QWidget *parent) :
  7. QWidget(parent)
  8. {
  9. srand(time(NULL));
  10. // Create country object and read data from the json file
  11. myCountry = new Country;
  12. myCountry->readInfoFromJSON(":/data/cityLimitsPR.json");
  13. cityColorMap = NULL;
  14. gisLocations = NULL;
  15. drawRoutes = false;
  16. // Compute the limits of the coordinates
  17. qDebug() << "Computing limits...";
  18. myCountry->limits();
  19. // Create the main widget and resize according to the map aspect ratio
  20. QDesktopWidget widget;
  21. QRect mainScreenSize = widget.availableGeometry(widget.primaryScreen());
  22. double diffX = myCountry->maxX - myCountry->minX > 0 ? myCountry->maxX - myCountry->minX : myCountry->minX - myCountry->maxX;
  23. double diffY = myCountry->maxY - myCountry->minY > 0 ? myCountry->maxY - myCountry->minY : myCountry->minY - myCountry->maxY;
  24. if (diffX > diffY) resize(mainScreenSize.width() * 0.5 , 0.5 * mainScreenSize.width()*(diffY/diffX));
  25. else resize(mainScreenSize.width() * 0.5, 0.5 * mainScreenSize.width()*(diffX/diffY));
  26. }
  27. Map::~Map() {
  28. if (myCountry != NULL) delete myCountry;
  29. if (cityColorMap != NULL) delete cityColorMap;
  30. if (gisLocations != NULL) delete [] gisLocations;
  31. }
  32. // These are used during the map drawing.
  33. QPoint **qpA = new QPoint*[100];
  34. unsigned int qpASize = 0;
  35. void Map::paintEvent(QPaintEvent *) {
  36. this->setWindowTitle("PR Visualization");
  37. QDesktopWidget widget;
  38. QRect mainScreenSize = widget.availableGeometry(widget.primaryScreen());
  39. // Creating the arrays that will hold the points for the cities
  40. if (qpASize == 0) {
  41. for (int i = 0; i < 100; i++) {
  42. qpA[i] = new QPoint[20000];
  43. }
  44. }
  45. // the QPainter is the 'canvas' to which we will draw
  46. // the QPen is the pen that will be used to draw to the 'canvas
  47. QPainter *p = new QPainter(this);
  48. QPen myPen;
  49. myPen.setWidth(1);
  50. myPen.setColor(QColor(0x100000));
  51. myPen.setBrush(QBrush(Qt::black));
  52. p->setPen(myPen);
  53. double factorX, factorY;
  54. // Computing the factors by which we'll scale the x and y coordinates.
  55. double diffX = myCountry->maxX - myCountry->minX > 0 ?
  56. myCountry->maxX - myCountry->minX : myCountry->minX - myCountry->maxX;
  57. double diffY = myCountry->maxY - myCountry->minY > 0 ?
  58. myCountry->maxY - myCountry->minY : myCountry->minY - myCountry->maxY;
  59. if (diffX > diffY) resize(mainScreenSize.width() * 0.5 , 0.5 * mainScreenSize.width()*(diffY/diffX));
  60. else resize(mainScreenSize.width() * 0.5, 0.5 * mainScreenSize.width()*(diffX/diffY));
  61. factorX = this->width() / diffX;
  62. factorY = this->height() / diffY;
  63. int colorCtr = 0;
  64. QMap<QString,City*>::iterator it;
  65. unsigned int randColor;
  66. int cityCounter = 0;
  67. for (it = myCountry->Cities.begin() ; it != myCountry->Cities.end(); ++it) {
  68. City *c = it.value();
  69. int x1 ,y1, x2, y2;
  70. x1 = factorX * (c->getGeometry()->at(0).x - myCountry->minX);
  71. y1 = height() - factorY*(c->getGeometry()->at(0).y - myCountry->minY) ;
  72. DoublePoint p1 = c->getGeometry()->at(0);
  73. DoublePoint p2;
  74. int ctr = 0;
  75. QPoint *qp = qpA[cityCounter];
  76. QPoint *qpBegin = qp;
  77. // if no color map provided, we'll just color every city white.
  78. randColor = 0xffffff;
  79. if (cityColorMap)
  80. randColor = (cityColorMap->find(it.key()) == cityColorMap->end()) ?
  81. 0xffffff : (*cityColorMap)[it.key()];
  82. for(int i = 0; i < c->getSize() + 1; i++) {
  83. p2 = c->getGeometry()->at((i+1)%c->getSize());
  84. x2 = factorX * (p2.x - myCountry->minX);
  85. y2 = height() - factorY*(p2.y - myCountry->minY) ;
  86. if (p2.x != 0 && p1.x != 0) {
  87. qp->setX(x2);
  88. qp->setY(y2);
  89. ctr++;
  90. qp++;
  91. }
  92. else if (p2.x == 0) {
  93. QPolygon yourPoly;
  94. for (int i = 0; i < ctr; i++) yourPoly.push_back(qpBegin[i]);
  95. QPainterPath tmpPath;
  96. tmpPath.addPolygon(yourPoly);
  97. p->fillPath(tmpPath,QBrush(randColor));
  98. p->drawPolygon(qpBegin,ctr);
  99. ctr = 0;
  100. qpBegin = qp;
  101. }
  102. x1 = x2;
  103. y1 = y2;
  104. p1 = p2;
  105. }
  106. QPolygon yourPoly;
  107. for (int i = 0; i < ctr; i++) yourPoly.push_back(qpBegin[i]);
  108. QPainterPath *tmpPath = new QPainterPath;
  109. tmpPath->addPolygon(yourPoly);
  110. p->fillPath(*tmpPath,QBrush(randColor));
  111. p->drawPolygon(qpBegin,ctr,Qt::WindingFill);
  112. delete tmpPath;
  113. colorCtr++;
  114. cityCounter++;
  115. }
  116. qpASize = cityCounter;
  117. // Draw the city centers
  118. int circleRadius = (this->height() > this->width()) ? this->width()/20 : this->height()/20 ;
  119. int cX, cY, pX, pY;
  120. cX = cY = pX = pY = -1;
  121. if(gisLocations) {
  122. myPen.setWidth(2);
  123. myPen.setColor(QColor(0x100000));
  124. myPen.setBrush(QBrush(Qt::black));
  125. p->setPen(myPen);
  126. for (unsigned int i = 0; i < numLocations; i++) {
  127. qDebug() << "name from locations:" << gisLocations[i].getName();
  128. cX = factorX * (gisLocations[i].getLon() - myCountry->minX);
  129. cY = height() - factorY*(gisLocations[i].getLat() - myCountry->minY);
  130. p->setBrush(Qt::SolidPattern);
  131. p->setBrush(QBrush(0xff0000));
  132. p->drawEllipse(cX, cY, circleRadius, circleRadius);
  133. p->setBrush(Qt::NoBrush);
  134. if (drawRoutes && pX > 0) {
  135. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  136. }
  137. pX = cX; pY = cY;
  138. }
  139. }
  140. // Draws any lines between the cities which have been added using
  141. // the drawLine method.
  142. QVector < QPair<const GISPOI *,const GISPOI *> > ::iterator vit = this->cityLines.begin();
  143. for (; vit != this->cityLines.end(); vit++) {
  144. cX = factorX * (vit->first->getLon() - myCountry->minX);
  145. cY = height() - factorY*(vit->first->getLat() - myCountry->minY);
  146. pX = factorX * (vit->second->getLon() - myCountry->minX);
  147. pY = height() - factorY*(vit->second->getLat() - myCountry->minY);
  148. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  149. }
  150. //c++11 way
  151. /*
  152. for (QPair<const GISPOI *, const GISPOI *> linePair: this->cityLines) {
  153. // qDebug() << "A line from " << linePair.first->getName() << " to " << linePair.second->getName() << endl;
  154. cX = factorX * (linePair.first->getLon() - myCountry->minX);
  155. cY = height() - factorY*(linePair.first->getLat() - myCountry->minY);
  156. pX = factorX * (linePair.second->getLon() - myCountry->minX);
  157. pY = height() - factorY*(linePair.second->getLat() - myCountry->minY);
  158. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  159. }
  160. */
  161. delete p;
  162. }