WorkArea.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <iostream>
  32. #include "WorkArea.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.width()*resizeFactor, tempImg.height()*resizeFactor);
  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::exportJPG (const std::string & filename)
  149. {
  150. QImage tempImg(*original);
  151. QPainter painter(&tempImg);
  152. for (int i = 0; i < liste_points.length (); i++)
  153. {
  154. if ((i % 2) == 1)
  155. {
  156. painter.setPen (QPen (Qt::red, epaisseurLigne/resizeFactor));
  157. std::pair < QPoint *, QPoint * > endPoints =
  158. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (tempImg.width()*resizeFactor, tempImg.height()*resizeFactor);
  159. painter.drawLine (*(std::get < 0 > (endPoints))/resizeFactor,
  160. *(std::get < 1 > (endPoints))/resizeFactor);
  161. }
  162. }
  163. tempImg.save(QString::fromStdString (filename));
  164. }
  165. void
  166. WorkArea::setReadOnly(bool readOnly){
  167. this->readOnly = readOnly;
  168. }
  169. bool
  170. WorkArea::getReadOnly(){
  171. return this->readOnly;
  172. }
  173. QImage*
  174. WorkArea::getOriginalImage() const
  175. {
  176. return new QImage(*original);
  177. }
  178. QImage*
  179. WorkArea::getResizedImage() const
  180. {
  181. return new QImage(*img);
  182. }
  183. void
  184. WorkArea::setModificationInProgress(bool mip){
  185. this->modificationInProgress = mip;
  186. }
  187. bool
  188. WorkArea::getModificationInProgress(){
  189. return this->modificationInProgress;
  190. }
  191. int
  192. WorkArea::getNumberOfLines() const
  193. {
  194. return ((int)(liste_points.size()/2));
  195. }
  196. void
  197. WorkArea::addSL (const StrengthLine *sl)
  198. {
  199. liste_points << new QPoint((int)(sl->getP1()->x()*resizeFactor), (int)(sl->getP1()->y()*resizeFactor));
  200. liste_points << new QPoint((int)(sl->getP2()->x()*resizeFactor), (int)(sl->getP2()->y()*resizeFactor));
  201. }
  202. void
  203. WorkArea::addRandomSL ()
  204. {
  205. StrengthLine *sl = StrengthLine::getRandomLine(original);
  206. liste_points << new QPoint((int)(sl->getP1()->x()*resizeFactor), (int)(sl->getP1()->y()*resizeFactor));
  207. liste_points << new QPoint((int)(sl->getP2()->x()*resizeFactor), (int)(sl->getP2()->y()*resizeFactor));
  208. }
  209. void
  210. WorkArea::paintEvent (QPaintEvent * event)
  211. {
  212. QPainter painter (this);
  213. paint (painter);
  214. }
  215. void
  216. WorkArea::paint (QPainter & painter)
  217. {
  218. if (img != NULL)
  219. painter.drawImage (0, 0, *img);
  220. for (int i = 0; i < liste_points.length (); i++)
  221. {
  222. if(readOnly==false)
  223. {
  224. painter.setBrush (QBrush (Qt::green));
  225. painter.setPen (QPen (Qt::black));
  226. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  227. liste_points[i]->y () - taillePoint / 2,
  228. taillePoint, taillePoint);
  229. }
  230. if ((i % 2) == 1)
  231. {
  232. painter.setPen (QPen (Qt::red, epaisseurLigne));
  233. std::pair < QPoint *, QPoint * >endPoints =
  234. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  235. painter.drawLine (*(std::get < 0 > (endPoints)),
  236. *(std::get < 1 > (endPoints)));
  237. }
  238. }
  239. }
  240. void
  241. WorkArea::mousePressEvent (QMouseEvent * event)
  242. {
  243. if(readOnly==false)
  244. {
  245. if (img != NULL)
  246. {
  247. this->modificationInProgress = true;
  248. // Est ce qu'on a cliqué sur un point existant ?
  249. indexPointClicked = -1;
  250. for (int i = 0; i < liste_points.length (); i++)
  251. {
  252. QPoint point = event->pos () - *(liste_points[i]);
  253. if (point.manhattanLength () < taillePoint)
  254. {
  255. indexPointClicked = i;
  256. }
  257. }
  258. // Clic gauche
  259. if (event->button () == Qt::LeftButton)
  260. {
  261. if (indexPointClicked == -1)
  262. {
  263. if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
  264. indexPointClicked = liste_points.size ();
  265. liste_points << new QPoint (event->x (), event->y ());
  266. }
  267. }
  268. }
  269. // Clic droit
  270. if (event->button () == Qt::RightButton)
  271. {
  272. if (indexPointClicked != -1)
  273. {
  274. if (indexPointClicked == (liste_points.size () - 1))
  275. liste_points.removeAt (indexPointClicked);
  276. else
  277. {
  278. if ((indexPointClicked % 2) == 0)
  279. {
  280. QPoint *single =
  281. liste_points.takeAt (indexPointClicked + 1);
  282. liste_points.removeAt (indexPointClicked);
  283. liste_points << single;
  284. }
  285. else
  286. {
  287. QPoint *single =
  288. liste_points.takeAt (indexPointClicked - 1);
  289. liste_points.removeAt (indexPointClicked - 1);
  290. liste_points << single;
  291. }
  292. }
  293. indexPointClicked = -1;
  294. }
  295. }
  296. }
  297. }
  298. repaint ();
  299. }
  300. void
  301. WorkArea::mouseMoveEvent (QMouseEvent * event)
  302. {
  303. if(readOnly==false)
  304. {
  305. if (indexPointClicked != -1)
  306. {
  307. liste_points[indexPointClicked]->setX (event->x ());
  308. liste_points[indexPointClicked]->setY (event->y ());
  309. }
  310. }
  311. repaint ();
  312. }
  313. void
  314. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  315. {
  316. indexPointClicked = -1;
  317. emit click();
  318. }