WorkArea.cpp 5.1 KB

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