WorkArea.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*********************************************************************/
  2. /* */
  3. /* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr */
  4. /* */
  5. /* This file is part of DSL. */
  6. /* This software uses Qt to build the Graphical User Interface */
  7. /* https://www.qt.io/ */
  8. /* */
  9. /* DSL is free software: you can redistribute it and/or modify */
  10. /* it under the terms of the GNU General Public License as published */
  11. /* by the Free Software Foundation, either version 3 of the License, */
  12. /* or (at your option) any later version. */
  13. /* */
  14. /* DSL is distributed in the hope that it will be useful, */
  15. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  16. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  17. /* GNU General Public License for more details. */
  18. /* */
  19. /* You should have received a copy of the GNU General Public License */
  20. /* along with DSL. If not, see <http://www.gnu.org/licenses/>. */
  21. /* */
  22. /*********************************************************************/
  23. #include <QFile>
  24. #include <QColor>
  25. #include <QTextStream>
  26. #include <QJsonObject>
  27. #include <QJsonArray>
  28. #include <QJsonDocument>
  29. #include <QDebug>
  30. #include <QStringList>
  31. #include "WorkArea.hpp"
  32. #include "StrengthLine.hpp"
  33. WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):
  34. QWidget(parent)
  35. {
  36. this->screenSizeX = screenSizeX;
  37. this->screenSizeY = screenSizeY;
  38. readOnly = false;
  39. modificationInProgress = false;
  40. img = NULL;
  41. resizeFactor = 1;
  42. indexPointClicked = -1;
  43. maxLines = 0;
  44. }
  45. WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & filename, QWidget * parent):QWidget(parent)
  46. {
  47. this->screenSizeX = screenSizeX;
  48. this->screenSizeY = screenSizeY;
  49. readOnly = false;
  50. modificationInProgress = false;
  51. img = NULL;
  52. resizeFactor = 1;
  53. indexPointClicked = -1;
  54. maxLines = 0;
  55. loadImage(filename);
  56. }
  57. WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & imageFilename, const std::string & SLFilename, bool readOnly, QWidget * parent):QWidget(parent)
  58. {
  59. this->screenSizeX = screenSizeX;
  60. this->screenSizeY = screenSizeY;
  61. readOnly = false;
  62. modificationInProgress = false;
  63. img = NULL;
  64. resizeFactor = 1;
  65. indexPointClicked = -1;
  66. maxLines = 0;
  67. loadImage(imageFilename);
  68. this->readOnly = readOnly;
  69. loadSL(SLFilename);
  70. }
  71. WorkArea::~WorkArea ()
  72. {
  73. }
  74. void
  75. WorkArea::loadImage (const std::string & filename)
  76. {
  77. QImage opened (QString::fromStdString (filename));
  78. img =
  79. new QImage (opened. scaled (screenSizeX, screenSizeY - this->geometry ().y (),
  80. Qt::KeepAspectRatio, Qt::FastTransformation));
  81. resizeFactor = (img->width()*1.0)/opened.width();
  82. liste_points.clear ();
  83. setFixedSize (img->width (), img->height ());
  84. }
  85. void
  86. WorkArea::loadSL (const std::string & filename)
  87. {
  88. liste_points.clear();
  89. QFile fin(QString::fromStdString (filename));
  90. fin.open(QIODevice::ReadOnly);
  91. QByteArray ba2 = fin.readAll();
  92. QJsonParseError parseError;
  93. QJsonDocument doc2 = QJsonDocument::fromJson(ba2, &parseError);
  94. QJsonArray lines = doc2["lines"].toArray();
  95. for(int i=0; i<lines.size(); i++){
  96. QJsonArray line = lines[i].toArray();
  97. QJsonArray point1 = line[0].toArray();
  98. QJsonArray point2 = line[1].toArray();
  99. liste_points << new QPoint((int)(point1[0].toDouble()*resizeFactor), (int)(point1[1].toDouble()*resizeFactor));
  100. liste_points << new QPoint((int)(point2[0].toDouble()*resizeFactor), (int)(point2[1].toDouble()*resizeFactor));
  101. }
  102. }
  103. void
  104. WorkArea::saveStrengthLine (const std::string & filename)
  105. {
  106. QJsonObject root;
  107. QJsonArray lines;
  108. for (int i = 0; i < liste_points.size () / 2; i++){
  109. QJsonArray line;
  110. QJsonArray point1;
  111. point1.push_back(liste_points[i*2]->x ()/resizeFactor);
  112. point1.push_back(liste_points[i*2]->y ()/resizeFactor);
  113. line.push_back(point1);
  114. QJsonArray point2;
  115. point2.push_back(liste_points[i*2+1]->x ()/resizeFactor);
  116. point2.push_back(liste_points[i*2+1]->y ()/resizeFactor);
  117. line.push_back(point2);
  118. lines.push_back(line);
  119. }
  120. root["lines"] = lines;
  121. QByteArray ba = QJsonDocument(root).toJson();
  122. QFile fout(QString::fromStdString (filename));
  123. fout.open(QIODevice::WriteOnly);
  124. fout.write(ba);
  125. this->modificationInProgress = false;
  126. }
  127. void
  128. WorkArea::setReadOnly(bool readOnly){
  129. this->readOnly = readOnly;
  130. }
  131. bool
  132. WorkArea::getReadOnly(){
  133. return this->readOnly;
  134. }
  135. void
  136. WorkArea::setModificationInProgress(bool mip){
  137. this->modificationInProgress = mip;
  138. }
  139. bool
  140. WorkArea::getModificationInProgress(){
  141. return this->modificationInProgress;
  142. }
  143. void
  144. WorkArea::paintEvent (QPaintEvent * event)
  145. {
  146. QPainter painter (this);
  147. paint (painter);
  148. }
  149. void
  150. WorkArea::paint (QPainter & painter)
  151. {
  152. if (img != NULL)
  153. painter.drawImage (0, 0, *img);
  154. for (int i = 0; i < liste_points.length (); i++)
  155. {
  156. if(readOnly==false)
  157. {
  158. painter.setBrush (QBrush (Qt::green));
  159. painter.setPen (QPen (Qt::black));
  160. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  161. liste_points[i]->y () - taillePoint / 2,
  162. taillePoint, taillePoint);
  163. }
  164. if ((i % 2) == 1)
  165. {
  166. painter.setPen (QPen (Qt::red, epaisseurLigne));
  167. std::pair < QPoint *, QPoint * >endPoints =
  168. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  169. painter.drawLine (*(std::get < 0 > (endPoints)),
  170. *(std::get < 1 > (endPoints)));
  171. }
  172. }
  173. }
  174. void
  175. WorkArea::mousePressEvent (QMouseEvent * event)
  176. {
  177. if(readOnly==false)
  178. {
  179. if (img != NULL)
  180. {
  181. this->modificationInProgress = true;
  182. // Est ce qu'on a cliqué sur un point existant ?
  183. indexPointClicked = -1;
  184. for (int i = 0; i < liste_points.length (); i++)
  185. {
  186. QPoint point = event->pos () - *(liste_points[i]);
  187. if (point.manhattanLength () < taillePoint)
  188. {
  189. indexPointClicked = i;
  190. }
  191. }
  192. // Clic gauche
  193. if (event->button () == Qt::LeftButton)
  194. {
  195. if (indexPointClicked == -1)
  196. {
  197. if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
  198. indexPointClicked = liste_points.size ();
  199. liste_points << new QPoint (event->x (), event->y ());
  200. }
  201. }
  202. }
  203. // Clic droit
  204. if (event->button () == Qt::RightButton)
  205. {
  206. if (indexPointClicked != -1)
  207. {
  208. if (indexPointClicked == (liste_points.size () - 1))
  209. liste_points.removeAt (indexPointClicked);
  210. else
  211. {
  212. if ((indexPointClicked % 2) == 0)
  213. {
  214. QPoint *single =
  215. liste_points.takeAt (indexPointClicked + 1);
  216. liste_points.removeAt (indexPointClicked);
  217. liste_points << single;
  218. }
  219. else
  220. {
  221. QPoint *single =
  222. liste_points.takeAt (indexPointClicked - 1);
  223. liste_points.removeAt (indexPointClicked - 1);
  224. liste_points << single;
  225. }
  226. }
  227. indexPointClicked = -1;
  228. }
  229. }
  230. }
  231. }
  232. repaint ();
  233. }
  234. void
  235. WorkArea::mouseMoveEvent (QMouseEvent * event)
  236. {
  237. if(readOnly==false)
  238. {
  239. if (indexPointClicked != -1)
  240. {
  241. liste_points[indexPointClicked]->setX (event->x ());
  242. liste_points[indexPointClicked]->setY (event->y ());
  243. }
  244. }
  245. repaint ();
  246. }
  247. void
  248. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  249. {
  250. indexPointClicked = -1;
  251. }