WorkArea.cpp 6.0 KB

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