WorkArea.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*********************************************************************/
  2. /* */
  3. /* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr */
  4. /* */
  5. /* This file is part of DSL. */
  6. /* This software uses Qt to build the Graphical User Interface */
  7. /* https://www.qt.io/ */
  8. /* */
  9. /* DSL is free software: you can redistribute it and/or modify */
  10. /* it under the terms of the GNU General Public License as published */
  11. /* by the Free Software Foundation, either version 3 of the License, */
  12. /* or (at your option) any later version. */
  13. /* */
  14. /* DSL is distributed in the hope that it will be useful, */
  15. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  16. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  17. /* GNU General Public License for more details. */
  18. /* */
  19. /* You should have received a copy of the GNU General Public License */
  20. /* along with DSL. If not, see <http://www.gnu.org/licenses/>. */
  21. /* */
  22. /*********************************************************************/
  23. #include <QFile>
  24. #include <QColor>
  25. #include <QTextStream>
  26. #include <QJsonObject>
  27. #include <QJsonArray>
  28. #include <QJsonDocument>
  29. #include <QPalette>
  30. #include <QDebug>
  31. #include <QStringList>
  32. #include <iostream>
  33. #include "WorkArea.hpp"
  34. WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):QWidget(parent)
  35. {
  36. this->screenSizeX = screenSizeX;
  37. this->screenSizeY = screenSizeY;
  38. this->setGeometry(0,0,screenSizeX,screenSizeY);
  39. QPalette pal = QPalette();
  40. pal.setColor(QPalette::Window, Qt::gray);
  41. this->setAutoFillBackground(true);
  42. this->setPalette(pal);
  43. setFixedSize (screenSizeX, screenSizeY);
  44. indexPointClicked = -1;
  45. }
  46. WorkArea::~WorkArea ()
  47. {
  48. }
  49. void
  50. WorkArea::addSL (const StrengthLine *sl)
  51. {
  52. liste_points << new QPoint((int)(sl->getP1()->x()), (int)(sl->getP1()->y()));
  53. liste_points << new QPoint((int)(sl->getP2()->x()), (int)(sl->getP2()->y()));
  54. }
  55. void
  56. WorkArea::paintEvent (QPaintEvent * event)
  57. {
  58. QPainter painter (this);
  59. paint (painter);
  60. }
  61. void
  62. WorkArea::paint (QPainter & painter)
  63. {
  64. for (int i = 0; i < liste_points.length (); i++)
  65. {
  66. painter.setBrush (QBrush (Qt::green));
  67. painter.setPen (QPen (Qt::black));
  68. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  69. liste_points[i]->y () - taillePoint / 2,
  70. taillePoint, taillePoint);
  71. if ((i % 2) == 1)
  72. {
  73. painter.setPen (QPen (Qt::red, epaisseurLigne));
  74. std::pair < QPoint *, QPoint * >endPoints =
  75. StrengthLine (liste_points[i - 1], liste_points[i]).interpolateToEdge (this->screenSizeX, this->screenSizeY);
  76. QPoint *first = std::get < 0 > (endPoints);
  77. QPoint *last = std::get < 1 > (endPoints);
  78. painter.drawLine (*first,
  79. *last);
  80. painter.setBrush (QBrush (Qt::blue));
  81. painter.setPen (QPen (Qt::blue));
  82. painter.drawEllipse (*first,
  83. taillePoint, taillePoint);
  84. painter.drawEllipse (*last,
  85. taillePoint, taillePoint);
  86. }
  87. }
  88. }
  89. void
  90. WorkArea::mousePressEvent (QMouseEvent * event)
  91. {
  92. // Est ce qu'on a cliqué sur un point existant ?
  93. indexPointClicked = -1;
  94. for (int i = 0; i < liste_points.length (); i++)
  95. {
  96. QPoint point = event->pos () - *(liste_points[i]);
  97. if (point.manhattanLength () < taillePoint)
  98. {
  99. indexPointClicked = i;
  100. }
  101. }
  102. repaint ();
  103. }
  104. void
  105. WorkArea::mouseMoveEvent (QMouseEvent * event)
  106. {
  107. if (indexPointClicked != -1)
  108. {
  109. liste_points[indexPointClicked]->setX (event->x ());
  110. liste_points[indexPointClicked]->setY (event->y ());
  111. }
  112. repaint ();
  113. }
  114. void
  115. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  116. {
  117. indexPointClicked = -1;
  118. emit click();
  119. }
  120. double
  121. WorkArea::getDistance() const
  122. {
  123. StrengthLine *sl1 = new StrengthLine(liste_points[0], liste_points[1]);
  124. StrengthLine *sl2 = new StrengthLine(liste_points[2], liste_points[3]);
  125. return sl1->ea_score(sl2, screenSizeX, screenSizeY);
  126. }