WorkArea.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. original = new QImage (QString::fromStdString (filename));
  78. img =
  79. new QImage (original->scaled (screenSizeX, screenSizeY - this->geometry ().y (),
  80. Qt::KeepAspectRatio, Qt::FastTransformation));
  81. resizeFactor = (img->width()*1.0)/original->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::exportPNG (const std::string & filename)
  129. {
  130. QImage tempImg(*original);
  131. QPainter painter(&tempImg);
  132. for (int i = 0; i < liste_points.length (); i++)
  133. {
  134. if ((i % 2) == 1)
  135. {
  136. painter.setPen (QPen (Qt::red, epaisseurLigne/resizeFactor));
  137. std::pair < QPoint *, QPoint * > endPoints =
  138. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (&tempImg);
  139. painter.drawLine (*(std::get < 0 > (endPoints))/resizeFactor,
  140. *(std::get < 1 > (endPoints))/resizeFactor);
  141. }
  142. }
  143. tempImg.save(QString::fromStdString (filename));
  144. }
  145. void
  146. WorkArea::setReadOnly(bool readOnly){
  147. this->readOnly = readOnly;
  148. }
  149. bool
  150. WorkArea::getReadOnly(){
  151. return this->readOnly;
  152. }
  153. void
  154. WorkArea::setModificationInProgress(bool mip){
  155. this->modificationInProgress = mip;
  156. }
  157. bool
  158. WorkArea::getModificationInProgress(){
  159. return this->modificationInProgress;
  160. }
  161. void
  162. WorkArea::paintEvent (QPaintEvent * event)
  163. {
  164. QPainter painter (this);
  165. paint (painter);
  166. }
  167. void
  168. WorkArea::paint (QPainter & painter)
  169. {
  170. if (img != NULL)
  171. painter.drawImage (0, 0, *img);
  172. for (int i = 0; i < liste_points.length (); i++)
  173. {
  174. if(readOnly==false)
  175. {
  176. painter.setBrush (QBrush (Qt::green));
  177. painter.setPen (QPen (Qt::black));
  178. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  179. liste_points[i]->y () - taillePoint / 2,
  180. taillePoint, taillePoint);
  181. }
  182. if ((i % 2) == 1)
  183. {
  184. painter.setPen (QPen (Qt::red, epaisseurLigne));
  185. std::pair < QPoint *, QPoint * >endPoints =
  186. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  187. painter.drawLine (*(std::get < 0 > (endPoints)),
  188. *(std::get < 1 > (endPoints)));
  189. }
  190. }
  191. }
  192. void
  193. WorkArea::mousePressEvent (QMouseEvent * event)
  194. {
  195. if(readOnly==false)
  196. {
  197. if (img != NULL)
  198. {
  199. this->modificationInProgress = true;
  200. // Est ce qu'on a cliqué sur un point existant ?
  201. indexPointClicked = -1;
  202. for (int i = 0; i < liste_points.length (); i++)
  203. {
  204. QPoint point = event->pos () - *(liste_points[i]);
  205. if (point.manhattanLength () < taillePoint)
  206. {
  207. indexPointClicked = i;
  208. }
  209. }
  210. // Clic gauche
  211. if (event->button () == Qt::LeftButton)
  212. {
  213. if (indexPointClicked == -1)
  214. {
  215. if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
  216. indexPointClicked = liste_points.size ();
  217. liste_points << new QPoint (event->x (), event->y ());
  218. }
  219. }
  220. }
  221. // Clic droit
  222. if (event->button () == Qt::RightButton)
  223. {
  224. if (indexPointClicked != -1)
  225. {
  226. if (indexPointClicked == (liste_points.size () - 1))
  227. liste_points.removeAt (indexPointClicked);
  228. else
  229. {
  230. if ((indexPointClicked % 2) == 0)
  231. {
  232. QPoint *single =
  233. liste_points.takeAt (indexPointClicked + 1);
  234. liste_points.removeAt (indexPointClicked);
  235. liste_points << single;
  236. }
  237. else
  238. {
  239. QPoint *single =
  240. liste_points.takeAt (indexPointClicked - 1);
  241. liste_points.removeAt (indexPointClicked - 1);
  242. liste_points << single;
  243. }
  244. }
  245. indexPointClicked = -1;
  246. }
  247. }
  248. }
  249. }
  250. repaint ();
  251. }
  252. void
  253. WorkArea::mouseMoveEvent (QMouseEvent * event)
  254. {
  255. if(readOnly==false)
  256. {
  257. if (indexPointClicked != -1)
  258. {
  259. liste_points[indexPointClicked]->setX (event->x ());
  260. liste_points[indexPointClicked]->setY (event->y ());
  261. }
  262. }
  263. repaint ();
  264. }
  265. void
  266. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  267. {
  268. indexPointClicked = -1;
  269. }