WorkArea.cpp 9.0 KB

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