WorkArea.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <QFile>
  2. #include <QColor>
  3. #include <QTextStream>
  4. #include <QDebug>
  5. #include <QStringList>
  6. #include "WorkArea.hpp"
  7. #include "StrengthLine.hpp"
  8. WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):
  9. QWidget(parent)
  10. {
  11. this->screenSizeX = screenSizeX;
  12. this->screenSizeY = screenSizeY;
  13. readOnly = false;
  14. img = NULL;
  15. resizeFactor = 1;
  16. indexPointClicked = -1;
  17. }
  18. WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & filename, QWidget * parent):QWidget(parent)
  19. {
  20. this->screenSizeX = screenSizeX;
  21. this->screenSizeY = screenSizeY;
  22. readOnly = false;
  23. img = NULL;
  24. resizeFactor = 1;
  25. indexPointClicked = -1;
  26. loadImage(filename);
  27. }
  28. WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & imageFilename, const std::string & SLFilename, bool readOnly, QWidget * parent):QWidget(parent)
  29. {
  30. this->screenSizeX = screenSizeX;
  31. this->screenSizeY = screenSizeY;
  32. img = NULL;
  33. resizeFactor = 1;
  34. indexPointClicked = -1;
  35. loadImage(imageFilename);
  36. this->readOnly = readOnly;
  37. loadSL(SLFilename);
  38. }
  39. WorkArea::~WorkArea ()
  40. {
  41. }
  42. void
  43. WorkArea::loadImage (const std::string & filename)
  44. {
  45. QImage opened (QString::fromStdString (filename));
  46. img =
  47. new QImage (opened. scaled (screenSizeX, screenSizeY - this->geometry ().y (),
  48. Qt::KeepAspectRatio, Qt::FastTransformation));
  49. resizeFactor = (img->width()*1.0)/opened.width();
  50. liste_points.clear ();
  51. setFixedSize (img->width (), img->height ());
  52. }
  53. void
  54. WorkArea::loadSL (const std::string & filename)
  55. {
  56. //qDebug() << resizeFactor << Qt::endl;
  57. liste_points.clear();
  58. QFile file(QString::fromStdString (filename));
  59. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  60. return;
  61. QTextStream in(&file);
  62. while (!in.atEnd()) {
  63. QString line = in.readLine();
  64. QStringList parts = line.split(" ");
  65. liste_points << new QPoint((int)(parts[0].toDouble()*resizeFactor), (int)(parts[1].toDouble()*resizeFactor));
  66. liste_points << new QPoint((int)(parts[2].toDouble()*resizeFactor), (int)(parts[3].toDouble()*resizeFactor));
  67. //qDebug() << liste_points.back()->x() << " " << liste_points.back()->y() << Qt::endl;
  68. }
  69. file.close();
  70. }
  71. void
  72. WorkArea::saveStrengthLine (const std::string & filename)
  73. {
  74. QFile file (QString::fromStdString (filename));
  75. if (!file.open (QIODevice::WriteOnly | QIODevice::Text))
  76. return;
  77. QTextStream out (&file);
  78. for (int i = 0; i < liste_points.size () / 2; i++)
  79. out << (liste_points[i*2]->x ()/resizeFactor) << " " << (liste_points[i*2]->y ()/resizeFactor) << " " << (liste_points[i*2+1]->x ()/resizeFactor) << " " << (liste_points[i*2+1]->y ()/resizeFactor) << "\n";
  80. file.close ();
  81. }
  82. void
  83. WorkArea::setReadOnly(bool readOnly){
  84. this->readOnly = readOnly;
  85. }
  86. void
  87. WorkArea::paintEvent (QPaintEvent * event)
  88. {
  89. QPainter painter (this);
  90. paint (painter);
  91. }
  92. void
  93. WorkArea::paint (QPainter & painter)
  94. {
  95. if (img != NULL)
  96. painter.drawImage (0, 0, *img);
  97. for (int i = 0; i < liste_points.length (); i++)
  98. {
  99. if(readOnly==false)
  100. {
  101. painter.setBrush (QBrush (Qt::green));
  102. painter.setPen (QPen (Qt::black));
  103. painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
  104. liste_points[i]->y () - taillePoint / 2,
  105. taillePoint, taillePoint);
  106. }
  107. if ((i % 2) == 1)
  108. {
  109. painter.setPen (QPen (Qt::red, epaisseurLigne));
  110. std::pair < QPoint *, QPoint * >endPoints =
  111. StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img);
  112. painter.drawLine (*(std::get < 0 > (endPoints)),
  113. *(std::get < 1 > (endPoints)));
  114. }
  115. }
  116. }
  117. void
  118. WorkArea::mousePressEvent (QMouseEvent * event)
  119. {
  120. if(readOnly==false)
  121. {
  122. if (img != NULL)
  123. {
  124. // Est ce qu'on a cliqué sur un point existant ?
  125. indexPointClicked = -1;
  126. for (int i = 0; i < liste_points.length (); i++)
  127. {
  128. QPoint point = event->pos () - *(liste_points[i]);
  129. if (point.manhattanLength () < taillePoint)
  130. {
  131. indexPointClicked = i;
  132. }
  133. }
  134. // Clic gauche
  135. if (event->button () == Qt::LeftButton)
  136. {
  137. if (indexPointClicked == -1)
  138. {
  139. indexPointClicked = liste_points.size ();
  140. liste_points << new QPoint (event->x (), event->y ());
  141. }
  142. }
  143. // Clic droit
  144. if (event->button () == Qt::RightButton)
  145. {
  146. if (indexPointClicked != -1)
  147. {
  148. if (indexPointClicked == (liste_points.size () - 1))
  149. liste_points.removeAt (indexPointClicked);
  150. else
  151. {
  152. if ((indexPointClicked % 2) == 0)
  153. {
  154. QPoint *single =
  155. liste_points.takeAt (indexPointClicked + 1);
  156. liste_points.removeAt (indexPointClicked);
  157. liste_points << single;
  158. }
  159. else
  160. {
  161. QPoint *single =
  162. liste_points.takeAt (indexPointClicked - 1);
  163. liste_points.removeAt (indexPointClicked - 1);
  164. liste_points << single;
  165. }
  166. }
  167. indexPointClicked = -1;
  168. }
  169. }
  170. }
  171. }
  172. repaint ();
  173. }
  174. void
  175. WorkArea::mouseMoveEvent (QMouseEvent * event)
  176. {
  177. if(readOnly==false)
  178. {
  179. if (indexPointClicked != -1)
  180. {
  181. liste_points[indexPointClicked]->setX (event->x ());
  182. liste_points[indexPointClicked]->setY (event->y ());
  183. }
  184. }
  185. repaint ();
  186. }
  187. void
  188. WorkArea::mouseReleaseEvent (QMouseEvent * event)
  189. {
  190. indexPointClicked = -1;
  191. }