No Description

map.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. myCountry = new Country;
  11. myCountry->readInfoFromJSON("/Users/rarce/Dropbox/CCOM3033/2014S/qt/prMap/data/cityLimitsPR.json");
  12. cityColorMap = NULL;
  13. gisLocations = NULL;
  14. drawRoutes = false;
  15. qDebug() << "Computing limits...";
  16. myCountry->limits();
  17. QDesktopWidget widget;
  18. QRect mainScreenSize = widget.availableGeometry(widget.primaryScreen());
  19. double diffX = myCountry->maxX - myCountry->minX > 0 ? myCountry->maxX - myCountry->minX : myCountry->minX - myCountry->maxX;
  20. double diffY = myCountry->maxY - myCountry->minY > 0 ? myCountry->maxY - myCountry->minY : myCountry->minY - myCountry->maxY;
  21. if (diffX > diffY) resize(mainScreenSize.width() * 0.5 , 0.5 * mainScreenSize.width()*(diffY/diffX));
  22. else resize(mainScreenSize.width() * 0.5, 0.5 * mainScreenSize.width()*(diffX/diffY));
  23. }
  24. Map::~Map() {
  25. if (myCountry != NULL) delete myCountry;
  26. if (cityColorMap != NULL) delete cityColorMap;
  27. if (gisLocations != NULL) delete [] gisLocations;
  28. }
  29. QPoint **qpA = new QPoint*[100];
  30. unsigned int qpASize = 0;
  31. void Map::paintEvent(QPaintEvent *event) {
  32. // the QPainter is the 'canvas' to which we will draw
  33. // the QPen is the pen that will be used to draw to the 'canvas'
  34. this->setWindowTitle("PR Visualization");
  35. QDesktopWidget widget;
  36. QRect mainScreenSize = widget.availableGeometry(widget.primaryScreen());
  37. if (qpASize == 0) {
  38. //qDebug() << "creating the arrays " << qpArraySize;
  39. for (int i = 0; i < 100; i++) {
  40. qpA[i] = new QPoint[20000];
  41. }
  42. }
  43. QPainter *p = new QPainter(this);
  44. QPen myPen;
  45. double factorX, factorY;
  46. double diffX = myCountry->maxX - myCountry->minX > 0 ? myCountry->maxX - myCountry->minX : myCountry->minX - myCountry->maxX;
  47. double diffY = myCountry->maxY - myCountry->minY > 0 ? myCountry->maxY - myCountry->minY : myCountry->minY - myCountry->maxY;
  48. if (diffX > diffY) resize(mainScreenSize.width() * 0.5 , 0.5 * mainScreenSize.width()*(diffY/diffX));
  49. else resize(mainScreenSize.width() * 0.5, 0.5 * mainScreenSize.width()*(diffX/diffY));
  50. factorX = this->width() / diffX;
  51. factorY = this->height() / diffY;
  52. myPen.setWidth(1);
  53. myPen.setColor(QColor(0x100000));
  54. myPen.setBrush(QBrush(Qt::black));
  55. p->setPen(myPen);
  56. int colorCtr = 0;
  57. QMap<QString,City*>::iterator it;
  58. unsigned int randColor;
  59. int cityCounter = 0;
  60. for (it = myCountry->Cities.begin() ; it != myCountry->Cities.end(); ++it) {
  61. City *c = it.value();
  62. int x1 ,y1, x2, y2;
  63. x1 = factorX * (c->getGeometry()->at(0).x - myCountry->minX);
  64. y1 = height() - factorY*(c->getGeometry()->at(0).y - myCountry->minY) ;
  65. DoublePoint p1 = c->getGeometry()->at(0);
  66. DoublePoint p2;
  67. int ctr = 0;
  68. QPoint *qp = qpA[cityCounter];
  69. QPoint *qpBegin = qp;
  70. // if no color map provided, we'll just color every city white.
  71. randColor = 0xffffff;
  72. if (cityColorMap)
  73. randColor = (cityColorMap->find(it.key()) == cityColorMap->end()) ?
  74. 0xffffff : (*cityColorMap)[it.key()];
  75. for(int i = 0; i < c->getSize() + 1; i++) {
  76. p2 = c->getGeometry()->at((i+1)%c->getSize());
  77. x2 = factorX * (p2.x - myCountry->minX);
  78. y2 = height() - factorY*(p2.y - myCountry->minY) ;
  79. if (p2.x != 0 && p1.x != 0) {
  80. qp->setX(x2);
  81. qp->setY(y2);
  82. ctr++;
  83. qp++;
  84. }
  85. else if (p2.x == 0) {
  86. QPolygon yourPoly;
  87. for (int i = 0; i < ctr; i++) yourPoly.push_back(qpBegin[i]);
  88. QPainterPath tmpPath;
  89. tmpPath.addPolygon(yourPoly);
  90. p->fillPath(tmpPath,QBrush(randColor));
  91. p->drawPolygon(qpBegin,ctr);
  92. ctr = 0;
  93. qpBegin = qp;
  94. }
  95. x1 = x2;
  96. y1 = y2;
  97. p1 = p2;
  98. }
  99. QPolygon yourPoly;
  100. for (int i = 0; i < ctr; i++) yourPoly.push_back(qpBegin[i]);
  101. QPainterPath *tmpPath = new QPainterPath;
  102. tmpPath->addPolygon(yourPoly);
  103. p->fillPath(*tmpPath,QBrush(randColor));
  104. p->drawPolygon(qpBegin,ctr,Qt::WindingFill);
  105. delete tmpPath;
  106. colorCtr++;
  107. cityCounter++;
  108. }
  109. qpASize = cityCounter;
  110. // Draw the city centers
  111. int circleRadius = (this->height() > this->width()) ? this->width()/20 : this->height()/20 ;
  112. int cX, cY, pX, pY;
  113. cX = cY = pX = pY = -1;
  114. qDebug() << "gisLocations:" << gisLocations << endl;
  115. if(gisLocations) {
  116. myPen.setWidth(2);
  117. myPen.setColor(QColor(0x100000));
  118. myPen.setBrush(QBrush(Qt::black));
  119. p->setPen(myPen);
  120. for (int i = 0; i < numLocations; i++) {
  121. qDebug() << "name from locations:" << gisLocations[i].getName();
  122. cX = factorX * (gisLocations[i].getLon() - myCountry->minX);
  123. cY = height() - factorY*(gisLocations[i].getLat() - myCountry->minY);
  124. p->setBrush(Qt::SolidPattern);
  125. p->setBrush(QBrush(0xff0000));
  126. p->drawEllipse(cX, cY, circleRadius, circleRadius);
  127. p->setBrush(Qt::NoBrush);
  128. if (drawRoutes && pX > 0) {
  129. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  130. }
  131. pX = cX; pY = cY;
  132. }
  133. }
  134. for (QPair<const GISPOI *, const GISPOI *> linePair: this->cityLines) {
  135. // qDebug() << "A line from " << linePair.first->getName() << " to " << linePair.second->getName() << endl;
  136. cX = factorX * (linePair.first->getLon() - myCountry->minX);
  137. cY = height() - factorY*(linePair.first->getLat() - myCountry->minY);
  138. pX = factorX * (linePair.second->getLon() - myCountry->minX);
  139. pY = height() - factorY*(linePair.second->getLat() - myCountry->minY);
  140. p->drawLine(pX+circleRadius/2,pY+circleRadius/2,cX+circleRadius/2,cY+circleRadius/2);
  141. }
  142. delete p;
  143. }
  144. void Map::drawLine(const GISPOI &city01, const GISPOI &city02) {
  145. this->cityLines.push_back(QPair<const GISPOI *, const GISPOI *> (&city01, &city02));
  146. }