WorkArea.cpp 5.3 KB

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