WorkArea.cpp 9.0 KB

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