WorkArea.cpp 4.6 KB

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