WorkArea.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <QFile>
  2. #include <QColor>
  3. #include <QTextStream>
  4. #include <QJsonObject>
  5. #include <QJsonArray>
  6. #include <QJsonDocument>
  7. #include <QDebug>
  8. #include <QStringList>
  9. #include "WorkArea.hpp"
  10. #include "StrengthLine.hpp"
  11. WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):
  12. QWidget(parent)
  13. {
  14. this->screenSizeX = screenSizeX;
  15. this->screenSizeY = screenSizeY;
  16. readOnly = false;
  17. img = NULL;
  18. resizeFactor = 1;
  19. indexPointClicked = -1;
  20. maxLines = 0;
  21. }
  22. WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & filename, QWidget * parent):QWidget(parent)
  23. {
  24. this->screenSizeX = screenSizeX;
  25. this->screenSizeY = screenSizeY;
  26. readOnly = false;
  27. img = NULL;
  28. resizeFactor = 1;
  29. indexPointClicked = -1;
  30. maxLines = 0;
  31. loadImage(filename);
  32. }
  33. WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & imageFilename, const std::string & SLFilename, bool readOnly, QWidget * parent):QWidget(parent)
  34. {
  35. this->screenSizeX = screenSizeX;
  36. this->screenSizeY = screenSizeY;
  37. img = NULL;
  38. resizeFactor = 1;
  39. indexPointClicked = -1;
  40. maxLines = 0;
  41. loadImage(imageFilename);
  42. this->readOnly = readOnly;
  43. loadSL(SLFilename);
  44. }
  45. WorkArea::~WorkArea ()
  46. {
  47. }
  48. void
  49. WorkArea::loadImage (const std::string & filename)
  50. {
  51. QImage opened (QString::fromStdString (filename));
  52. img =
  53. new QImage (opened. scaled (screenSizeX, screenSizeY - this->geometry ().y (),
  54. Qt::KeepAspectRatio, Qt::FastTransformation));
  55. resizeFactor = (img->width()*1.0)/opened.width();
  56. liste_points.clear ();
  57. setFixedSize (img->width (), img->height ());
  58. }
  59. void
  60. WorkArea::loadSL (const std::string & filename)
  61. {
  62. liste_points.clear();
  63. QFile fin(QString::fromStdString (filename));
  64. fin.open(QIODevice::ReadOnly);
  65. QByteArray ba2 = fin.readAll();
  66. QJsonParseError parseError;
  67. QJsonDocument doc2 = QJsonDocument::fromJson(ba2, &parseError);
  68. QJsonArray lines = doc2["lines"].toArray();
  69. for(unsigned int i=0; i<lines.size(); i++){
  70. QJsonArray line = lines[i].toArray();
  71. QJsonArray point1 = line[0].toArray();
  72. QJsonArray point2 = line[1].toArray();
  73. liste_points << new QPoint((int)(point1[0].toDouble()*resizeFactor), (int)(point1[1].toDouble()*resizeFactor));
  74. liste_points << new QPoint((int)(point2[0].toDouble()*resizeFactor), (int)(point2[1].toDouble()*resizeFactor));
  75. }
  76. }
  77. void
  78. WorkArea::saveStrengthLine (const std::string & filename)
  79. {
  80. QJsonObject root;
  81. QJsonArray lines;
  82. for (int i = 0; i < liste_points.size () / 2; i++){
  83. QJsonArray line;
  84. QJsonArray point1;
  85. point1.push_back(liste_points[i*2]->x ()/resizeFactor);
  86. point1.push_back(liste_points[i*2]->y ()/resizeFactor);
  87. line.push_back(point1);
  88. QJsonArray point2;
  89. point2.push_back(liste_points[i*2+1]->x ()/resizeFactor);
  90. point2.push_back(liste_points[i*2+1]->y ()/resizeFactor);
  91. line.push_back(point2);
  92. lines.push_back(line);
  93. }
  94. root["lines"] = lines;
  95. QByteArray ba = QJsonDocument(root).toJson();
  96. QFile fout(QString::fromStdString (filename));
  97. fout.open(QIODevice::WriteOnly);
  98. fout.write(ba);
  99. }
  100. void
  101. WorkArea::setReadOnly(bool readOnly){
  102. this->readOnly = readOnly;
  103. }
  104. void
  105. WorkArea::paintEvent (QPaintEvent * event)
  106. {
  107. QPainter painter (this);
  108. paint (painter);
  109. }
  110. void
  111. WorkArea::paint (QPainter & painter)
  112. {
  113. if (img != NULL)
  114. painter.drawImage (0, 0, *img);
  115. for (int i = 0; i < liste_points.length (); i++)
  116. {
  117. if(readOnly==false)
  118. {
  119. painter.setBrush (QBrush (Qt::green));
  120. painter.setPen (QPen (Qt::black));
  121. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  122. liste_points[i]->y () - taillePoint / 2,
  123. taillePoint, taillePoint);
  124. }
  125. if ((i % 2) == 1)
  126. {
  127. painter.setPen (QPen (Qt::red, epaisseurLigne));
  128. std::pair < QPoint *, QPoint * >endPoints =
  129. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  130. painter.drawLine (*(std::get < 0 > (endPoints)),
  131. *(std::get < 1 > (endPoints)));
  132. }
  133. }
  134. }
  135. void
  136. WorkArea::mousePressEvent (QMouseEvent * event)
  137. {
  138. if(readOnly==false)
  139. {
  140. if (img != NULL)
  141. {
  142. // Est ce qu'on a cliqué sur un point existant ?
  143. indexPointClicked = -1;
  144. for (int i = 0; i < liste_points.length (); i++)
  145. {
  146. QPoint point = event->pos () - *(liste_points[i]);
  147. if (point.manhattanLength () < taillePoint)
  148. {
  149. indexPointClicked = i;
  150. }
  151. }
  152. // Clic gauche
  153. if (event->button () == Qt::LeftButton)
  154. {
  155. if (indexPointClicked == -1)
  156. {
  157. if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
  158. indexPointClicked = liste_points.size ();
  159. liste_points << new QPoint (event->x (), event->y ());
  160. }
  161. }
  162. }
  163. // Clic droit
  164. if (event->button () == Qt::RightButton)
  165. {
  166. if (indexPointClicked != -1)
  167. {
  168. if (indexPointClicked == (liste_points.size () - 1))
  169. liste_points.removeAt (indexPointClicked);
  170. else
  171. {
  172. if ((indexPointClicked % 2) == 0)
  173. {
  174. QPoint *single =
  175. liste_points.takeAt (indexPointClicked + 1);
  176. liste_points.removeAt (indexPointClicked);
  177. liste_points << single;
  178. }
  179. else
  180. {
  181. QPoint *single =
  182. liste_points.takeAt (indexPointClicked - 1);
  183. liste_points.removeAt (indexPointClicked - 1);
  184. liste_points << single;
  185. }
  186. }
  187. indexPointClicked = -1;
  188. }
  189. }
  190. }
  191. }
  192. repaint ();
  193. }
  194. void
  195. WorkArea::mouseMoveEvent (QMouseEvent * event)
  196. {
  197. if(readOnly==false)
  198. {
  199. if (indexPointClicked != -1)
  200. {
  201. liste_points[indexPointClicked]->setX (event->x ());
  202. liste_points[indexPointClicked]->setY (event->y ());
  203. }
  204. }
  205. repaint ();
  206. }
  207. void
  208. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  209. {
  210. indexPointClicked = -1;
  211. }