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. QJsonArray lines;
  81. for (int i = 0; i < liste_points.size () / 2; i++){
  82. QJsonArray line;
  83. QJsonArray point1;
  84. point1.push_back(liste_points[i*2]->x ()/resizeFactor);
  85. point1.push_back(liste_points[i*2]->y ()/resizeFactor);
  86. line.push_back(point1);
  87. QJsonArray point2;
  88. point2.push_back(liste_points[i*2+1]->x ()/resizeFactor);
  89. point2.push_back(liste_points[i*2+1]->y ()/resizeFactor);
  90. line.push_back(point2);
  91. lines.push_back(line);
  92. }
  93. root["lines"] = lines;
  94. QByteArray ba = QJsonDocument(root).toJson();
  95. QFile fout(QString::fromStdString (filename));
  96. fout.open(QIODevice::WriteOnly);
  97. fout.write(ba);
  98. }
  99. void
  100. WorkArea::setReadOnly(bool readOnly){
  101. this->readOnly = readOnly;
  102. }
  103. void
  104. WorkArea::paintEvent (QPaintEvent * event)
  105. {
  106. QPainter painter (this);
  107. paint (painter);
  108. }
  109. void
  110. WorkArea::paint (QPainter & painter)
  111. {
  112. if (img != NULL)
  113. painter.drawImage (0, 0, *img);
  114. for (int i = 0; i < liste_points.length (); i++)
  115. {
  116. if(readOnly==false)
  117. {
  118. painter.setBrush (QBrush (Qt::green));
  119. painter.setPen (QPen (Qt::black));
  120. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  121. liste_points[i]->y () - taillePoint / 2,
  122. taillePoint, taillePoint);
  123. }
  124. if ((i % 2) == 1)
  125. {
  126. painter.setPen (QPen (Qt::red, epaisseurLigne));
  127. std::pair < QPoint *, QPoint * >endPoints =
  128. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  129. painter.drawLine (*(std::get < 0 > (endPoints)),
  130. *(std::get < 1 > (endPoints)));
  131. }
  132. }
  133. }
  134. void
  135. WorkArea::mousePressEvent (QMouseEvent * event)
  136. {
  137. if(readOnly==false)
  138. {
  139. if (img != NULL)
  140. {
  141. // Est ce qu'on a cliqué sur un point existant ?
  142. indexPointClicked = -1;
  143. for (int i = 0; i < liste_points.length (); i++)
  144. {
  145. QPoint point = event->pos () - *(liste_points[i]);
  146. if (point.manhattanLength () < taillePoint)
  147. {
  148. indexPointClicked = i;
  149. }
  150. }
  151. // Clic gauche
  152. if (event->button () == Qt::LeftButton)
  153. {
  154. if (indexPointClicked == -1)
  155. {
  156. if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
  157. indexPointClicked = liste_points.size ();
  158. liste_points << new QPoint (event->x (), event->y ());
  159. }
  160. }
  161. }
  162. // Clic droit
  163. if (event->button () == Qt::RightButton)
  164. {
  165. if (indexPointClicked != -1)
  166. {
  167. if (indexPointClicked == (liste_points.size () - 1))
  168. liste_points.removeAt (indexPointClicked);
  169. else
  170. {
  171. if ((indexPointClicked % 2) == 0)
  172. {
  173. QPoint *single =
  174. liste_points.takeAt (indexPointClicked + 1);
  175. liste_points.removeAt (indexPointClicked);
  176. liste_points << single;
  177. }
  178. else
  179. {
  180. QPoint *single =
  181. liste_points.takeAt (indexPointClicked - 1);
  182. liste_points.removeAt (indexPointClicked - 1);
  183. liste_points << single;
  184. }
  185. }
  186. indexPointClicked = -1;
  187. }
  188. }
  189. }
  190. }
  191. repaint ();
  192. }
  193. void
  194. WorkArea::mouseMoveEvent (QMouseEvent * event)
  195. {
  196. if(readOnly==false)
  197. {
  198. if (indexPointClicked != -1)
  199. {
  200. liste_points[indexPointClicked]->setX (event->x ());
  201. liste_points[indexPointClicked]->setY (event->y ());
  202. }
  203. }
  204. repaint ();
  205. }
  206. void
  207. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  208. {
  209. indexPointClicked = -1;
  210. }