No Description

map.cpp 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // the QPainter is the 'canvas' to which we will draw
  37. // the QPen is the pen that will be used to draw to the 'canvas'
  38. this->setWindowTitle("PR Visualization");
  39. QDesktopWidget widget;
  40. QRect mainScreenSize = widget.availableGeometry(widget.primaryScreen());
  41. if (qpASize == 0) {
  42. //qDebug() << "creating the arrays " << qpArraySize;
  43. for (int i = 0; i < 100; i++) {
  44. qpA[i] = new QPoint[20000];
  45. }
  46. }
  47. QPainter *p = new QPainter(this);
  48. QPen myPen;
  49. double factorX, factorY;
  50. double diffX = myCountry->maxX - myCountry->minX > 0 ? myCountry->maxX - myCountry->minX : myCountry->minX - myCountry->maxX;
  51. double diffY = myCountry->maxY - myCountry->minY > 0 ? myCountry->maxY - myCountry->minY : myCountry->minY - myCountry->maxY;
  52. if (diffX > diffY) resize(mainScreenSize.width() * 0.5 , 0.5 * mainScreenSize.width()*(diffY/diffX));
  53. else resize(mainScreenSize.width() * 0.5, 0.5 * mainScreenSize.width()*(diffX/diffY));
  54. factorX = this->width() / diffX;
  55. factorY = this->height() / diffY;
  56. myPen.setWidth(1);
  57. myPen.setColor(QColor(0x100000));
  58. myPen.setBrush(QBrush(Qt::black));
  59. p->setPen(myPen);
  60. int colorCtr = 0;
  61. QMap<QString,City*>::iterator it;
  62. unsigned int randColor;
  63. int cityCounter = 0;
  64. for (it = myCountry->Cities.begin() ; it != myCountry->Cities.end(); ++it) {
  65. City *c = it.value();
  66. int x1 ,y1, x2, y2;
  67. x1 = factorX * (c->getGeometry()->at(0).x - myCountry->minX);
  68. y1 = height() - factorY*(c->getGeometry()->at(0).y - myCountry->minY) ;
  69. DoublePoint p1 = c->getGeometry()->at(0);
  70. DoublePoint p2;
  71. int ctr = 0;
  72. QPoint *qp = qpA[cityCounter];
  73. QPoint *qpBegin = qp;
  74. // if no color map provided, we'll just color every city white.
  75. randColor = 0xffffff;
  76. if (cityColorMap)
  77. randColor = (cityColorMap->find(it.key()) == cityColorMap->end()) ?
  78. 0xffffff : (*cityColorMap)[it.key()];
  79. for(int i = 0; i < c->getSize() + 1; i++) {
  80. p2 = c->getGeometry()->at((i+1)%c->getSize());
  81. x2 = factorX * (p2.x - myCountry->minX);
  82. y2 = height() - factorY*(p2.y - myCountry->minY) ;
  83. if (p2.x != 0 && p1.x != 0) {
  84. qp->setX(x2);
  85. qp->setY(y2);
  86. ctr++;
  87. qp++;
  88. }
  89. else if (p2.x == 0) {
  90. QPolygon yourPoly;
  91. for (int i = 0; i < ctr; i++) yourPoly.push_back(qpBegin[i]);
  92. QPainterPath tmpPath;
  93. tmpPath.addPolygon(yourPoly);
  94. p->fillPath(tmpPath,QBrush(randColor));
  95. p->drawPolygon(qpBegin,ctr);
  96. ctr = 0;
  97. qpBegin = qp;
  98. }
  99. x1 = x2;
  100. y1 = y2;
  101. p1 = p2;
  102. }
  103. QPolygon yourPoly;
  104. for (int i = 0; i < ctr; i++) yourPoly.push_back(qpBegin[i]);
  105. QPainterPath *tmpPath = new QPainterPath;
  106. tmpPath->addPolygon(yourPoly);
  107. p->fillPath(*tmpPath,QBrush(randColor));
  108. p->drawPolygon(qpBegin,ctr,Qt::WindingFill);
  109. delete tmpPath;
  110. colorCtr++;
  111. cityCounter++;
  112. }
  113. qpASize = cityCounter;
  114. // Draw the city centers
  115. int circleRadius = (this->height() > this->width()) ? this->width()/20 : this->height()/20 ;
  116. int cX, cY, pX, pY;
  117. cX = cY = pX = pY = -1;
  118. if(gisLocations) {
  119. myPen.setWidth(2);
  120. myPen.setColor(QColor(0x100000));
  121. myPen.setBrush(QBrush(Qt::black));
  122. p->setPen(myPen);
  123. for (unsigned int i = 0; i < numLocations; i++) {
  124. qDebug() << "name from locations:" << gisLocations[i].getName();
  125. cX = factorX * (gisLocations[i].getLon() - myCountry->minX);
  126. cY = height() - factorY*(gisLocations[i].getLat() - myCountry->minY);
  127. p->setBrush(Qt::SolidPattern);
  128. p->setBrush(QBrush(0xff0000));
  129. p->drawEllipse(cX, cY, circleRadius, circleRadius);
  130. p->setBrush(Qt::NoBrush);
  131. if (drawRoutes && pX > 0) {
  132. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  133. }
  134. pX = cX; pY = cY;
  135. }
  136. }
  137. // Draws any lines between the cities which have been added using
  138. // the drawLine method.
  139. QVector < QPair<const GISPOI *,const GISPOI *> > ::iterator vit = this->cityLines.begin();
  140. for (; vit != this->cityLines.end(); vit++) {
  141. cX = factorX * (vit->first->getLon() - myCountry->minX);
  142. cY = height() - factorY*(vit->first->getLat() - myCountry->minY);
  143. pX = factorX * (vit->second->getLon() - myCountry->minX);
  144. pY = height() - factorY*(vit->second->getLat() - myCountry->minY);
  145. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  146. }
  147. //c++11 way
  148. /*
  149. for (QPair<const GISPOI *, const GISPOI *> linePair: this->cityLines) {
  150. // qDebug() << "A line from " << linePair.first->getName() << " to " << linePair.second->getName() << endl;
  151. cX = factorX * (linePair.first->getLon() - myCountry->minX);
  152. cY = height() - factorY*(linePair.first->getLat() - myCountry->minY);
  153. pX = factorX * (linePair.second->getLon() - myCountry->minX);
  154. pY = height() - factorY*(linePair.second->getLat() - myCountry->minY);
  155. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  156. }
  157. */
  158. delete p;
  159. }