chemin.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "chemin.h"
  2. #include <QPainter>
  3. /*map:
  4. * constructors
  5. * copie makers
  6. * herited fonctions
  7. * fonctions
  8. * getter/setters
  9. * events
  10. * destruction!!!
  11. */
  12. //constructors:
  13. drawPath::drawPath( ):QGraphicsItem()
  14. {
  15. //this->setAcceptedMouseButtons(Qt::AllButtons);
  16. this->dest=QPointF(-111,-111);
  17. this->path=QPainterPath();
  18. //std::cout<<"drawpath(void)"<<std::endl;
  19. this->nodes_number=0;
  20. setAcceptDrops(true);
  21. }
  22. drawPath::~drawPath()
  23. {}
  24. //accesseur: set true if param =true
  25. void drawPath::setrealdraw(bool param)
  26. {
  27. isrealdraw=param;
  28. //foreachsubpath: setreal or not;
  29. }
  30. void drawPath::mousePressEvent(QMouseEvent *event)
  31. {
  32. std::cout<<"drawpath(reealmousePressEvent)"<<std::endl;
  33. // this->setAcceptedMouseButtons(Qt::AllButtons);
  34. event->accept();
  35. }
  36. drawPath& drawPath::operator=(drawPath const&
  37. sourcedecopie)
  38. {
  39. this->origin=sourcedecopie.origin;
  40. this->dest=sourcedecopie.dest;
  41. this->nodes_number=sourcedecopie.nodes_number;
  42. this->path=QPainterPath(sourcedecopie.path);
  43. this->isrealdraw=sourcedecopie.isrealdraw;
  44. std::cout<<"drawpath=()"<<std::endl;
  45. // this->setAcceptedMouseButtons(Qt::AllButtons);
  46. return *this;
  47. }
  48. //hazardous copy constructor
  49. drawPath::drawPath(const drawPath &sourcedecopie):QGraphicsItem()
  50. {
  51. this->origin=sourcedecopie.origin;
  52. this->dest=sourcedecopie.dest;
  53. this->path=QPainterPath(sourcedecopie.path);
  54. this->isrealdraw=sourcedecopie.isrealdraw;
  55. this->nodes_number=sourcedecopie.nodes_number;
  56. //this->setAcceptedMouseButtons(Qt::AllButtons);
  57. /*
  58. std::cout<<":drawpath l53:drawPath(const drawPath &)"<<std::endl;
  59. std::cout<<sourcedecopie.path.currentPosition().toPoint().x()<<":"<<sourcedecopie.path.currentPosition().toPoint().y()<<std::endl;
  60. std::cout<<this->path.currentPosition().toPoint().x()<<":"<<this->path.currentPosition().toPoint().y()<<std::endl;
  61. */
  62. }
  63. QRectF drawPath::boundingRect() const
  64. {
  65. //qreal penWidth = 1;
  66. return this->path.boundingRect();
  67. }
  68. void drawPath::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  69. QWidget *widget)
  70. {
  71. Q_UNUSED(option);
  72. Q_UNUSED(widget);
  73. //Q_UNUSED(painter);
  74. // painter->setBrush(Qt::NoBrush);
  75. if(isrealdraw)
  76. painter->setPen(QPen(Qt::blue, 1));
  77. else
  78. painter->setPen(QPen(Qt::red, 1));
  79. painter->drawEllipse(-2,-2,4,4);
  80. painter->drawPath(this->path);
  81. }
  82. // void drawPath::moveTo(QPointF desti)
  83. // {
  84. // this->path.moveTo(desti);
  85. // this->node_number++;
  86. // }
  87. void drawPath::lineTo(QPointF desti)
  88. {
  89. //ahaha whaaaaaaaaat ????
  90. //ne jamais remettre!!!
  91. // std::cout<<"drawpath:lineto(qpf)"<<desti.x()<<desti.y()<<std::endl;
  92. if(nodes_number==1)
  93. {
  94. this->dest=desti;
  95. // std::cout<<" drawpath:lineto(dest)const:"<<this->dest.x()<<this->dest.y()<<std::endl;
  96. }
  97. else
  98. {
  99. this->dest=desti;
  100. std::cout<<" drawpath:lineto(dest)const: but nodeisempty"<<this->dest.x()<<this->dest.y()<<std::endl;
  101. }
  102. //ca marche mieux!
  103. this->path.lineTo(desti);
  104. this->nodes_number++;
  105. }
  106. drawPath::drawPath(QPointF origine)
  107. {
  108. this->origin=origine;
  109. // this->setAcceptedMouseButtons(Qt::AllButtons);
  110. // std::cout<<"drawpath(qpf)"<<std::endl;
  111. // std::cout<<"drawpath(qpf)"<<origine.x()<<origine.y()<<std::endl;
  112. this->path=QPainterPath(origine);
  113. this->nodes_number=1;
  114. this->dest=QPointF(-666,-666);
  115. // this->lineTo(origine);
  116. }
  117. drawPath::drawPath(QPointF origine,QPointF desti)
  118. {
  119. this->path=QPainterPath(origine);
  120. this->nodes_number=1;
  121. this->dest=desti;
  122. std::cout<<" drawpath(orig,dest)const:"<<this->dest.x()<<this->dest.y()<<std::endl;
  123. this->origin=origine;
  124. this->lineTo(desti);
  125. // this->setAcceptedMouseButtons(Qt::AllButtons);
  126. }
  127. //is static
  128. int drawPath::getnode_number()const{
  129. return this->nodes_number;
  130. // :/
  131. }
  132. //is static
  133. bool drawPath::isvalid() const{
  134. if(getnode_number()==2)
  135. return true;
  136. else return false;
  137. // :/
  138. }
  139. QPainterPath drawPath::getpath() const{
  140. std::cout<<"QPainterPath drawpath::getpath()const origin:"<<this->origin.x()<<this->origin.y()<<std::endl;
  141. std::cout<<"QPainterPath drawpath::getpath()const dest:"<<this->dest.x()<<this->dest.y()<<std::endl;
  142. return this->path;
  143. }
  144. //drawPath::drawPath(QStringList *toadd)
  145. //{
  146. // QStringList list1 = toadd->at(0).split(":", QString::SkipEmptyParts);
  147. //int i=0;
  148. // //l'origine x du chemin est le premier numbre de la list, y le deuxieme
  149. // this->origin=QPointF(list1.at(0).toFloat(),list1.at(1).toFloat());
  150. // QPainterPath path (this->origin);
  151. // //node_number++;
  152. // int node_number=1;
  153. ////on trace le chemin pour tous les points restants
  154. // for (i=1;i < toadd->size(); ++i)
  155. // {
  156. // node_number++;
  157. // list1 = toadd->at(i).split(":", QString::SkipEmptyParts);
  158. // this->dest=QPointF(list1.at(0).toFloat(),list1.at(1).toFloat());
  159. // path.lineTo(this->dest);
  160. // //path.setAcceptedMouseButtons(Qt::AllButtons);
  161. // //std::cout<<i<<".addshpe(stringlist)."<<list1.at(0).toFloat()<<":"<<list1.at(1).toFloat()<<std::endl;
  162. // }
  163. // if (i>2)
  164. // qDebug("drawpath with more than 2nodes");
  165. // //QPen pen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
  166. // //this->sceneVue->fitInView(this->sceneConteneur->addPath(shape,pen));
  167. //}
  168. /*
  169. void drawPath::lineTo(float x, float y)
  170. {this->path->lineTo(x,y);
  171. }
  172. void drawPath::lineTo(QPointF target_node)
  173. {this->path->lineTo(target_node);
  174. }
  175. */