WorkArea.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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]).toDraw (this->screenSizeX, this->screenSizeY);
  76. painter.drawLine (*(std::get < 0 > (endPoints)),
  77. *(std::get < 1 > (endPoints)));
  78. }
  79. }
  80. }
  81. void
  82. WorkArea::mousePressEvent (QMouseEvent * event)
  83. {
  84. // Est ce qu'on a cliqué sur un point existant ?
  85. indexPointClicked = -1;
  86. for (int i = 0; i < liste_points.length (); i++)
  87. {
  88. QPoint point = event->pos () - *(liste_points[i]);
  89. if (point.manhattanLength () < taillePoint)
  90. {
  91. indexPointClicked = i;
  92. }
  93. }
  94. // Clic gauche
  95. /*if (event->button () == Qt::LeftButton)
  96. {
  97. if (indexPointClicked == -1)
  98. {
  99. if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
  100. indexPointClicked = liste_points.size ();
  101. liste_points << new QPoint (event->x (), event->y ());
  102. }
  103. }
  104. }*/
  105. repaint ();
  106. }
  107. void
  108. WorkArea::mouseMoveEvent (QMouseEvent * event)
  109. {
  110. if (indexPointClicked != -1)
  111. {
  112. liste_points[indexPointClicked]->setX (event->x ());
  113. liste_points[indexPointClicked]->setY (event->y ());
  114. }
  115. repaint ();
  116. }
  117. void
  118. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  119. {
  120. indexPointClicked = -1;
  121. emit click();
  122. }
  123. double
  124. WorkArea::getDistance() const
  125. {
  126. StrengthLine *sl1 = new StrengthLine(liste_points[0], liste_points[1]);
  127. StrengthLine *sl2 = new StrengthLine(liste_points[2], liste_points[3]);
  128. return sl1->distance(sl2);
  129. }