WorkArea.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. QImageReader qImgReader(QString::fromStdString (filename));
  78. qImgReader.setAutoTransform(true);
  79. original = new QImage (qImgReader.read());
  80. img =
  81. new QImage (original->scaled (screenSizeX, screenSizeY - this->geometry ().y (),
  82. Qt::KeepAspectRatio, Qt::FastTransformation));
  83. resizeFactor = (img->width()*1.0)/original->width();
  84. liste_points.clear ();
  85. setFixedSize (img->width (), img->height ());
  86. }
  87. void
  88. WorkArea::loadSL (const std::string & filename)
  89. {
  90. liste_points.clear();
  91. QFile fin(QString::fromStdString (filename));
  92. fin.open(QIODevice::ReadOnly);
  93. QByteArray ba2 = fin.readAll();
  94. QJsonParseError parseError;
  95. QJsonDocument doc2 = QJsonDocument::fromJson(ba2, &parseError);
  96. QJsonArray lines = doc2["lines"].toArray();
  97. for(int i=0; i<lines.size(); i++){
  98. QJsonArray line = lines[i].toArray();
  99. QJsonArray point1 = line[0].toArray();
  100. QJsonArray point2 = line[1].toArray();
  101. liste_points << new QPoint((int)(point1[0].toDouble()*resizeFactor), (int)(point1[1].toDouble()*resizeFactor));
  102. liste_points << new QPoint((int)(point2[0].toDouble()*resizeFactor), (int)(point2[1].toDouble()*resizeFactor));
  103. }
  104. }
  105. void
  106. WorkArea::saveStrengthLine (const std::string & filename)
  107. {
  108. QJsonObject root;
  109. QJsonArray lines;
  110. for (int i = 0; i < liste_points.size () / 2; i++){
  111. QJsonArray line;
  112. QJsonArray point1;
  113. point1.push_back(liste_points[i*2]->x ()/resizeFactor);
  114. point1.push_back(liste_points[i*2]->y ()/resizeFactor);
  115. line.push_back(point1);
  116. QJsonArray point2;
  117. point2.push_back(liste_points[i*2+1]->x ()/resizeFactor);
  118. point2.push_back(liste_points[i*2+1]->y ()/resizeFactor);
  119. line.push_back(point2);
  120. lines.push_back(line);
  121. }
  122. root["lines"] = lines;
  123. QByteArray ba = QJsonDocument(root).toJson();
  124. QFile fout(QString::fromStdString (filename));
  125. fout.open(QIODevice::WriteOnly);
  126. fout.write(ba);
  127. this->modificationInProgress = false;
  128. }
  129. void
  130. WorkArea::exportPNG (const std::string & filename)
  131. {
  132. QImage tempImg(*original);
  133. QPainter painter(&tempImg);
  134. for (int i = 0; i < liste_points.length (); i++)
  135. {
  136. if ((i % 2) == 1)
  137. {
  138. painter.setPen (QPen (Qt::red, epaisseurLigne/resizeFactor));
  139. std::pair < QPoint *, QPoint * > endPoints =
  140. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (&tempImg);
  141. painter.drawLine (*(std::get < 0 > (endPoints))/resizeFactor,
  142. *(std::get < 1 > (endPoints))/resizeFactor);
  143. }
  144. }
  145. tempImg.save(QString::fromStdString (filename));
  146. }
  147. void
  148. WorkArea::setReadOnly(bool readOnly){
  149. this->readOnly = readOnly;
  150. }
  151. bool
  152. WorkArea::getReadOnly(){
  153. return this->readOnly;
  154. }
  155. void
  156. WorkArea::setModificationInProgress(bool mip){
  157. this->modificationInProgress = mip;
  158. }
  159. bool
  160. WorkArea::getModificationInProgress(){
  161. return this->modificationInProgress;
  162. }
  163. void
  164. WorkArea::paintEvent (QPaintEvent * event)
  165. {
  166. QPainter painter (this);
  167. paint (painter);
  168. }
  169. void
  170. WorkArea::paint (QPainter & painter)
  171. {
  172. if (img != NULL)
  173. painter.drawImage (0, 0, *img);
  174. for (int i = 0; i < liste_points.length (); i++)
  175. {
  176. if(readOnly==false)
  177. {
  178. painter.setBrush (QBrush (Qt::green));
  179. painter.setPen (QPen (Qt::black));
  180. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  181. liste_points[i]->y () - taillePoint / 2,
  182. taillePoint, taillePoint);
  183. }
  184. if ((i % 2) == 1)
  185. {
  186. painter.setPen (QPen (Qt::red, epaisseurLigne));
  187. std::pair < QPoint *, QPoint * >endPoints =
  188. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  189. painter.drawLine (*(std::get < 0 > (endPoints)),
  190. *(std::get < 1 > (endPoints)));
  191. }
  192. }
  193. }
  194. void
  195. WorkArea::mousePressEvent (QMouseEvent * event)
  196. {
  197. if(readOnly==false)
  198. {
  199. if (img != NULL)
  200. {
  201. this->modificationInProgress = true;
  202. // Est ce qu'on a cliqué sur un point existant ?
  203. indexPointClicked = -1;
  204. for (int i = 0; i < liste_points.length (); i++)
  205. {
  206. QPoint point = event->pos () - *(liste_points[i]);
  207. if (point.manhattanLength () < taillePoint)
  208. {
  209. indexPointClicked = i;
  210. }
  211. }
  212. // Clic gauche
  213. if (event->button () == Qt::LeftButton)
  214. {
  215. if (indexPointClicked == -1)
  216. {
  217. if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
  218. indexPointClicked = liste_points.size ();
  219. liste_points << new QPoint (event->x (), event->y ());
  220. }
  221. }
  222. }
  223. // Clic droit
  224. if (event->button () == Qt::RightButton)
  225. {
  226. if (indexPointClicked != -1)
  227. {
  228. if (indexPointClicked == (liste_points.size () - 1))
  229. liste_points.removeAt (indexPointClicked);
  230. else
  231. {
  232. if ((indexPointClicked % 2) == 0)
  233. {
  234. QPoint *single =
  235. liste_points.takeAt (indexPointClicked + 1);
  236. liste_points.removeAt (indexPointClicked);
  237. liste_points << single;
  238. }
  239. else
  240. {
  241. QPoint *single =
  242. liste_points.takeAt (indexPointClicked - 1);
  243. liste_points.removeAt (indexPointClicked - 1);
  244. liste_points << single;
  245. }
  246. }
  247. indexPointClicked = -1;
  248. }
  249. }
  250. }
  251. }
  252. repaint ();
  253. }
  254. void
  255. WorkArea::mouseMoveEvent (QMouseEvent * event)
  256. {
  257. if(readOnly==false)
  258. {
  259. if (indexPointClicked != -1)
  260. {
  261. liste_points[indexPointClicked]->setX (event->x ());
  262. liste_points[indexPointClicked]->setY (event->y ());
  263. }
  264. }
  265. repaint ();
  266. }
  267. void
  268. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  269. {
  270. indexPointClicked = -1;
  271. }