skyview.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "skyview.h"
  2. #include "skyview_p.h"
  3. #include <QPainter>
  4. #include <QtGlobal>
  5. #include <QDebug>
  6. #include <QtCore/qmath.h>
  7. #include <cmath> // for fmod()
  8. namespace {
  9. const qreal PI = 3.14159265;
  10. const qreal DEFAULT_FOV = 0.785;
  11. qreal rad2hours(qreal rad) {
  12. return 24.0 * (rad / (2.0 * PI));
  13. }
  14. qreal hours2rad(qreal hours) {
  15. return 1.0;
  16. }
  17. }
  18. SkyView::SkyView(QDeclarativeItem *parent) :
  19. QDeclarativeItem(parent), d(new SkyViewPrivate)
  20. {
  21. setClip(true);
  22. setFlag(QGraphicsItem::ItemHasNoContents, false);
  23. d->azimuth = 0;
  24. for (qreal ra = 0.0; ra <= 24.0; ra += 2.0) {
  25. for (qreal dec = -90.0; dec <= 90.0; dec += 30.0) {
  26. SkyObject* o = new SkyObject;
  27. o->ra = ra;
  28. o->dec = dec;
  29. d->objects.push_back(o);
  30. }
  31. }
  32. d->fov = DEFAULT_FOV;
  33. }
  34. SkyView::~SkyView()
  35. {
  36. foreach(SkyObject* o, d->objects) delete o;
  37. delete d;
  38. }
  39. void SkyView::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
  40. {
  41. paintGrid(painter);
  42. paintObjects(painter);
  43. }
  44. void SkyView::paintGrid(QPainter *painter) const
  45. {
  46. painter->setPen(QPen(QColor("white"), 2));
  47. painter->setRenderHints(QPainter::Antialiasing, true);
  48. // draw horizon
  49. int horizon_y = mapToScreenPoint(0, 0).y();
  50. painter->drawLine(0, horizon_y, width(), horizon_y);
  51. int last_visible_hour = floor(leftEdgeAzimuth());
  52. qDebug() << "left az" << leftEdgeAzimuth() << "h -> last hour mark" << last_visible_hour;
  53. int current_hour = floor(fmod(24.0 + (leftEdgeAzimuth() - viewportWidthHours()), 24.0));
  54. qDebug() << "wp width" << viewportWidthHours() << "h -> first hour mark" << current_hour;
  55. do {
  56. QPoint p1 = mapToScreenPoint(current_hour, -90);
  57. QPoint p2 = mapToScreenPoint(current_hour, 90);
  58. qDebug() << "for" << current_hour << "p1 =" << p1 << "p2 =" << p2;
  59. painter->drawLine(p1, p2);
  60. current_hour = (current_hour + 1) % 24;
  61. } while (current_hour != last_visible_hour);
  62. }
  63. void SkyView::paintObjects(QPainter *painter) const
  64. {
  65. painter->setPen(QPen(QColor("white"), 1));
  66. painter->setBrush(QBrush(QColor("white")));
  67. foreach(const SkyObject* o, d->objects) {
  68. painter->drawEllipse(mapToScreenPoint(o->ra, o->dec), 5, 5);
  69. }
  70. }
  71. QPoint SkyView::mapToScreenPoint(qreal ra, qreal dec) const
  72. {
  73. qreal relative_ra = azimuth() - ra; // inverted because RA increases to the left
  74. if (relative_ra > 12.0) relative_ra -= 24.0; // account for angles passing the 0 mark
  75. qreal relative_dec = dec - altitude();
  76. qreal rel_ra_norm = relative_ra / viewportWidthHours();
  77. qreal rel_dec_norm = relative_dec / viewportHeightDegrees();
  78. return QPoint(rel_ra_norm * width() + width() / 2, -1 * rel_dec_norm * height() + height() / 2);
  79. }
  80. qreal SkyView::azimuth() const
  81. {
  82. return d->azimuth;
  83. }
  84. void SkyView::setAzimuth(qreal azimuth)
  85. {
  86. // Limit to 0..23.9999...
  87. azimuth = fmod(24.0 + azimuth, 24.0);
  88. if (!qFuzzyCompare(azimuth, d->azimuth)) {
  89. d->azimuth = azimuth;
  90. emit azimuthChanged();
  91. }
  92. }
  93. qreal SkyView::fov() const
  94. {
  95. return d->fov;
  96. }
  97. void SkyView::setFov(qreal fov)
  98. {
  99. if (!qFuzzyCompare(fov, d->fov)) {
  100. d->fov = fov;
  101. emit fovChanged();
  102. emit viewportWidthHoursChanged();
  103. emit viewportHeightDegreesChanged();
  104. }
  105. }
  106. qreal SkyView::leftEdgeAzimuth() const
  107. {
  108. qreal az = azimuth() + viewportWidthHours() / 2.0;
  109. return fmod(24.0 + az, 24.0);
  110. }
  111. qreal SkyView::viewportHeightDegrees() const
  112. {
  113. return ((fov() / (2.0 * PI)) * static_cast<qreal>(height() / width())) * 360.0;
  114. }
  115. qreal SkyView::viewportWidthHours() const
  116. {
  117. return (fov() / (2.0 * PI)) * 24.0;
  118. }
  119. qreal SkyView::altitude() const
  120. {
  121. return d->altitude;
  122. }
  123. void SkyView::setAltitude(qreal altitude)
  124. {
  125. altitude = qBound(-90.0, altitude, 90.0);
  126. if (!qFuzzyCompare(d->altitude, altitude)) {
  127. d->altitude = altitude;
  128. emit altitudeChanged();
  129. }
  130. }