WorkArea.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. liste_points << new QPoint(parts[0].toInt(), parts[1].toInt());
  42. liste_points << new QPoint(parts[2].toInt(), parts[3].toInt());
  43. }
  44. file.close();
  45. }
  46. WorkArea::~WorkArea ()
  47. {
  48. }
  49. void
  50. WorkArea::loadImage (const std::string & filename)
  51. {
  52. img =
  53. new QImage (QImage (QString::fromStdString (filename)).
  54. scaled (screenSizeX, screenSizeY - this->geometry ().y (),
  55. Qt::KeepAspectRatio, Qt::FastTransformation));
  56. liste_points.clear ();
  57. resize (img->width (), img->height ());
  58. }
  59. void
  60. WorkArea::loadSL (const std::string & filename)
  61. {
  62. liste_points.clear();
  63. QFile file(QString::fromStdString (filename));
  64. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  65. return;
  66. QTextStream in(&file);
  67. while (!in.atEnd()) {
  68. QString line = in.readLine();
  69. QStringList parts = line.split(" ");
  70. liste_points << new QPoint(parts[0].toInt(), parts[1].toInt());
  71. liste_points << new QPoint(parts[2].toInt(), parts[3].toInt());
  72. }
  73. file.close();
  74. }
  75. void
  76. WorkArea::saveStrengthLine (const std::string & filename)
  77. {
  78. QFile file (QString::fromStdString (filename));
  79. if (!file.open (QIODevice::WriteOnly | QIODevice::Text))
  80. return;
  81. QTextStream out (&file);
  82. for (int i = 0; i < liste_points.size () / 2; i++)
  83. out << liste_points[i*2]->x () << " " << liste_points[i*2]->y () << " " << liste_points[i*2+1]->x () << " " << liste_points[i*2+1]->y () << "\n";
  84. file.close ();
  85. }
  86. void
  87. WorkArea::setReadOnly(bool readOnly){
  88. this->readOnly = readOnly;
  89. }
  90. void
  91. WorkArea::paintEvent (QPaintEvent * event)
  92. {
  93. QPainter painter (this);
  94. paint (painter);
  95. }
  96. void
  97. WorkArea::paint (QPainter & painter)
  98. {
  99. if (img != NULL)
  100. painter.drawImage (0, 0, *img);
  101. for (int i = 0; i < liste_points.length (); i++)
  102. {
  103. if(readOnly==false)
  104. {
  105. painter.setBrush (QBrush (Qt::green));
  106. painter.setPen (QPen (Qt::black));
  107. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  108. liste_points[i]->y () - taillePoint / 2,
  109. taillePoint, taillePoint);
  110. }
  111. if ((i % 2) == 1)
  112. {
  113. painter.setPen (QPen (Qt::red, epaisseurLigne));
  114. std::pair < QPoint *, QPoint * >endPoints =
  115. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  116. painter.drawLine (*(std::get < 0 > (endPoints)),
  117. *(std::get < 1 > (endPoints)));
  118. }
  119. }
  120. }
  121. void
  122. WorkArea::mousePressEvent (QMouseEvent * event)
  123. {
  124. if(readOnly==false)
  125. {
  126. if (img != NULL)
  127. {
  128. // Est ce qu'on a cliqué sur un point existant ?
  129. indexPointClicked = -1;
  130. for (int i = 0; i < liste_points.length (); i++)
  131. {
  132. QPoint point = event->pos () - *(liste_points[i]);
  133. if (point.manhattanLength () < taillePoint)
  134. {
  135. indexPointClicked = i;
  136. }
  137. }
  138. // Clic gauche
  139. if (event->button () == Qt::LeftButton)
  140. {
  141. if (indexPointClicked == -1)
  142. {
  143. indexPointClicked = liste_points.size ();
  144. liste_points << new QPoint (event->x (), event->y ());
  145. }
  146. }
  147. // Clic droit
  148. if (event->button () == Qt::RightButton)
  149. {
  150. if (indexPointClicked != -1)
  151. {
  152. if (indexPointClicked == (liste_points.size () - 1))
  153. liste_points.removeAt (indexPointClicked);
  154. else
  155. {
  156. if ((indexPointClicked % 2) == 0)
  157. {
  158. QPoint *single =
  159. liste_points.takeAt (indexPointClicked + 1);
  160. liste_points.removeAt (indexPointClicked);
  161. liste_points << single;
  162. }
  163. else
  164. {
  165. QPoint *single =
  166. liste_points.takeAt (indexPointClicked - 1);
  167. liste_points.removeAt (indexPointClicked - 1);
  168. liste_points << single;
  169. }
  170. }
  171. indexPointClicked = -1;
  172. }
  173. }
  174. }
  175. }
  176. repaint ();
  177. }
  178. void
  179. WorkArea::mouseMoveEvent (QMouseEvent * event)
  180. {
  181. if(readOnly==false)
  182. {
  183. if (indexPointClicked != -1)
  184. {
  185. liste_points[indexPointClicked]->setX (event->x ());
  186. liste_points[indexPointClicked]->setY (event->y ());
  187. }
  188. }
  189. repaint ();
  190. }
  191. void
  192. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  193. {
  194. indexPointClicked = -1;
  195. }