Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

MapLocationDialog.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2017 Syping
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "MapLocationDialog.h"
  19. #include "ui_MapLocationDialog.h"
  20. #include "IconLoader.h"
  21. #include "AppEnv.h"
  22. #include <QPainter>
  23. #include <QDebug>
  24. #include <QStyle>
  25. MapLocationDialog::MapLocationDialog(double x, double y, QWidget *parent) :
  26. QDialog(parent), xpos_old(x), ypos_old(y),
  27. ui(new Ui::MapLocationDialog)
  28. {
  29. // Set Window Flags
  30. setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
  31. ui->setupUi(this);
  32. ui->cmdDone->setVisible(false);
  33. ui->cmdApply->setVisible(false);
  34. ui->cmdRevert->setVisible(false);
  35. ui->cmdDone->setCursor(Qt::ArrowCursor);
  36. ui->cmdClose->setCursor(Qt::ArrowCursor);
  37. // DPI calculation
  38. qreal screenRatio = AppEnv::screenRatio();
  39. int widgetMargin = qRound(3 * screenRatio);
  40. ui->hlMapDialog->setContentsMargins(widgetMargin, widgetMargin, widgetMargin, widgetMargin);
  41. ui->vlMapDialog->setSpacing(widgetMargin);
  42. setMinimumSize(500 * screenRatio, 600 * screenRatio);
  43. setMaximumSize(500 * screenRatio, 600 * screenRatio);
  44. setFixedSize(500 * screenRatio, 600 * screenRatio);
  45. setMouseTracking(true);
  46. changeMode = false;
  47. propUpdate = false;
  48. drawPointOnMap(xpos_old, ypos_old);
  49. }
  50. MapLocationDialog::~MapLocationDialog()
  51. {
  52. delete ui;
  53. }
  54. void MapLocationDialog::drawPointOnMap(double xpos_d, double ypos_d)
  55. {
  56. qreal screenRatio = AppEnv::screenRatio();
  57. int pointMakerSize = 8 * screenRatio;
  58. QPixmap pointMakerPixmap = IconLoader::loadingPointmakerIcon().pixmap(QSize(pointMakerSize, pointMakerSize));
  59. QSize mapPixelSize = size();
  60. int pointMakerHalfSize = pointMakerSize / 2;
  61. long xpos_ms = qRound(xpos_d);
  62. long ypos_ms = qRound(ypos_d);
  63. double xpos_ma = xpos_ms + 4000;
  64. double ypos_ma = ypos_ms + 4000;
  65. double xrat = (double)mapPixelSize.width() / 10000;
  66. double yrat = (double)mapPixelSize.height() / 12000;
  67. long xpos_mp = qRound(xpos_ma * xrat);
  68. long ypos_mp = qRound(ypos_ma * yrat);
  69. long xpos_pr = xpos_mp - pointMakerHalfSize;
  70. long ypos_pr = ypos_mp + pointMakerHalfSize;
  71. QPixmap mapPixmap(mapPixelSize);
  72. QPainter mapPainter(&mapPixmap);
  73. mapPainter.drawPixmap(0, 0, mapPixelSize.width(), mapPixelSize.height(), QPixmap(":/img/mappreview.jpg").scaled(mapPixelSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  74. mapPainter.drawPixmap(xpos_pr, mapPixelSize.height() - ypos_pr, pointMakerSize, pointMakerSize, pointMakerPixmap);
  75. mapPainter.end();
  76. QPalette backgroundPalette;
  77. backgroundPalette.setBrush(backgroundRole(), QBrush(mapPixmap));
  78. setPalette(backgroundPalette);
  79. xpos_new = xpos_d;
  80. ypos_new = ypos_d;
  81. ui->labPos->setText(tr("X: %1\nY: %2", "X and Y position").arg(QString::number(xpos_d), QString::number(ypos_d)));
  82. }
  83. void MapLocationDialog::on_cmdChange_clicked()
  84. {
  85. qreal screenRatio = AppEnv::screenRatio();
  86. int pointMakerSize = 8 * screenRatio;
  87. QPixmap pointMakerPixmap = IconLoader::loadingPointmakerIcon().pixmap(QSize(pointMakerSize, pointMakerSize));
  88. QCursor pointMakerCursor(pointMakerPixmap);
  89. ui->cmdDone->setVisible(true);
  90. ui->cmdApply->setVisible(false);
  91. ui->cmdChange->setVisible(false);
  92. ui->cmdRevert->setVisible(false);
  93. setCursor(pointMakerCursor);
  94. changeMode = true;
  95. }
  96. void MapLocationDialog::on_cmdDone_clicked()
  97. {
  98. ui->cmdDone->setVisible(false);
  99. ui->cmdChange->setVisible(true);
  100. if (xpos_new != xpos_old || ypos_new != ypos_old)
  101. {
  102. ui->cmdApply->setVisible(true);
  103. ui->cmdRevert->setVisible(true);
  104. }
  105. setCursor(Qt::ArrowCursor);
  106. changeMode = false;
  107. }
  108. void MapLocationDialog::updatePosFromEvent(int x, int y)
  109. {
  110. QSize mapPixelSize = size();
  111. int xpos_ad = x;
  112. int ypos_ad = mapPixelSize.height() - y;
  113. double xrat = 10000 / (double)mapPixelSize.width();
  114. double yrat = 12000 / (double)mapPixelSize.height();
  115. double xpos_rv = xrat * xpos_ad;
  116. double ypos_rv = yrat * ypos_ad;
  117. double xpos_fp = xpos_rv - 4000;
  118. double ypos_fp = ypos_rv - 4000;
  119. drawPointOnMap(xpos_fp, ypos_fp);
  120. }
  121. void MapLocationDialog::mouseMoveEvent(QMouseEvent *ev)
  122. {
  123. if (!changeMode) { ev->ignore(); }
  124. else if (ev->buttons() & Qt::LeftButton)
  125. {
  126. updatePosFromEvent(ev->x(), ev->y());
  127. ev->accept();
  128. }
  129. else
  130. {
  131. ev->ignore();
  132. }
  133. }
  134. void MapLocationDialog::mouseReleaseEvent(QMouseEvent *ev)
  135. {
  136. if (!changeMode) { ev->ignore(); }
  137. else if (ev->button() == Qt::LeftButton)
  138. {
  139. updatePosFromEvent(ev->x(), ev->y());
  140. ev->accept();
  141. }
  142. else
  143. {
  144. ev->ignore();
  145. }
  146. }
  147. void MapLocationDialog::on_cmdApply_clicked()
  148. {
  149. propUpdate = true;
  150. xpos_old = xpos_new;
  151. ypos_old = ypos_new;
  152. ui->cmdApply->setVisible(false);
  153. ui->cmdRevert->setVisible(false);
  154. }
  155. void MapLocationDialog::on_cmdRevert_clicked()
  156. {
  157. drawPointOnMap(xpos_old, ypos_old);
  158. ui->cmdApply->setVisible(false);
  159. ui->cmdRevert->setVisible(false);
  160. }
  161. bool MapLocationDialog::propUpdated()
  162. {
  163. return propUpdate;
  164. }
  165. double MapLocationDialog::getXpos()
  166. {
  167. return xpos_old;
  168. }
  169. double MapLocationDialog::getYpos()
  170. {
  171. return ypos_old;
  172. }
  173. void MapLocationDialog::on_cmdClose_clicked()
  174. {
  175. close();
  176. }