WorkArea.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <QFile>
  2. #include <QColor>
  3. #include <QTextStream>
  4. #include "WorkArea.hpp"
  5. #include "StrengthLine.hpp"
  6. WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):
  7. QWidget
  8. {
  9. parent}
  10. {
  11. this->screenSizeX = screenSizeX;
  12. this->screenSizeY = screenSizeY;
  13. indexPointClicked = -1;
  14. img = NULL;
  15. }
  16. WorkArea::~WorkArea ()
  17. {
  18. }
  19. void
  20. WorkArea::loadImage (const std::string & filename)
  21. {
  22. img =
  23. new QImage (QImage (QString::fromStdString (filename)).
  24. scaled (screenSizeX, screenSizeY - this->geometry ().y (),
  25. Qt::KeepAspectRatio, Qt::FastTransformation));
  26. liste_points.clear ();
  27. resize (img->width (), img->height ());
  28. }
  29. void
  30. WorkArea::saveStrengthLine (const std::string & filename)
  31. {
  32. QFile file (QString::fromStdString (filename));
  33. if (!file.open (QIODevice::WriteOnly | QIODevice::Text))
  34. return;
  35. QTextStream out (&file);
  36. for (int i = 0; i < liste_points.size () / 2; i++)
  37. out << liste_points[i*2]->x () << " " << liste_points[i*2]->y () << " " << liste_points[i*2+1]->x () << " " << liste_points[i*2+1]->y () << "\n";
  38. file.close ();
  39. }
  40. void
  41. WorkArea::paintEvent (QPaintEvent * event)
  42. {
  43. QPainter painter (this);
  44. paint (painter);
  45. }
  46. void
  47. WorkArea::paint (QPainter & painter)
  48. {
  49. if (img != NULL)
  50. painter.drawImage (0, 0, *img);
  51. for (int i = 0; i < liste_points.length (); i++)
  52. {
  53. painter.setBrush (QBrush (Qt::green));
  54. painter.setPen (QPen (Qt::black));
  55. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  56. liste_points[i]->y () - taillePoint / 2,
  57. taillePoint, taillePoint);
  58. if ((i % 2) == 1)
  59. {
  60. painter.setPen (QPen (Qt::red, epaisseurLigne));
  61. std::pair < QPoint *, QPoint * >endPoints =
  62. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  63. painter.drawLine (*(std::get < 0 > (endPoints)),
  64. *(std::get < 1 > (endPoints)));
  65. }
  66. }
  67. }
  68. void
  69. WorkArea::mousePressEvent (QMouseEvent * event)
  70. {
  71. if (img != NULL)
  72. {
  73. // Est ce qu'on a cliqué sur un point existant ?
  74. indexPointClicked = -1;
  75. for (int i = 0; i < liste_points.length (); i++)
  76. {
  77. QPoint point = event->pos () - *(liste_points[i]);
  78. if (point.manhattanLength () < taillePoint)
  79. {
  80. indexPointClicked = i;
  81. }
  82. }
  83. // Clic gauche
  84. if (event->button () == Qt::LeftButton)
  85. {
  86. if (indexPointClicked == -1)
  87. {
  88. indexPointClicked = liste_points.size ();
  89. liste_points << new QPoint (event->x (), event->y ());
  90. }
  91. }
  92. // Clic droit
  93. if (event->button () == Qt::RightButton)
  94. {
  95. if (indexPointClicked != -1)
  96. {
  97. if (indexPointClicked == (liste_points.size () - 1))
  98. liste_points.removeAt (indexPointClicked);
  99. else
  100. {
  101. if ((indexPointClicked % 2) == 0)
  102. {
  103. QPoint *single =
  104. liste_points.takeAt (indexPointClicked + 1);
  105. liste_points.removeAt (indexPointClicked);
  106. liste_points << single;
  107. }
  108. else
  109. {
  110. QPoint *single =
  111. liste_points.takeAt (indexPointClicked - 1);
  112. liste_points.removeAt (indexPointClicked - 1);
  113. liste_points << single;
  114. }
  115. }
  116. indexPointClicked = -1;
  117. }
  118. }
  119. }
  120. repaint ();
  121. }
  122. void
  123. WorkArea::mouseMoveEvent (QMouseEvent * event)
  124. {
  125. if (indexPointClicked != -1)
  126. {
  127. liste_points[indexPointClicked]->setX (event->x ());
  128. liste_points[indexPointClicked]->setY (event->y ());
  129. }
  130. repaint ();
  131. }
  132. void
  133. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  134. {
  135. indexPointClicked = -1;
  136. }