forme.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. #include "forme.h"
  2. #include "selectsettingwindow.h"
  3. #include <QPainter>
  4. #include <QGraphicsScene>
  5. #include <QGraphicsSceneDragDropEvent>
  6. #include <QDebug>
  7. #include <QMimeData>
  8. /*map:
  9. * constructors
  10. * copie makers
  11. * herited fonctions
  12. * fonctions
  13. * getter/setters
  14. * events
  15. * destruction!!!
  16. */
  17. void Forme::setcolor(QString color)
  18. {this->fillcolor.setNamedColor(color);
  19. if(!this->fillcolor.isValid())
  20. {
  21. this->brush=Qt::NoBrush;
  22. // this->fillcolor(Qt::NoPen);
  23. }
  24. else{
  25. this->brush=Qt::SolidPattern;
  26. this->brush.setColor(this->fillcolor);
  27. }
  28. this->update();}
  29. void Forme::setstrokewidth(QString width)
  30. {
  31. if(width=="none")
  32. {
  33. this->pen.setStyle(Qt::NoPen);
  34. //painter->setBrush(this->strokecolor);
  35. // painter->setPen(Qt::NoPen);
  36. // painter->setPen(QPen(this->strokecolor, 1));
  37. // painter->setPen(Qt::SolidLine);
  38. }
  39. else
  40. {
  41. this->pen.setStyle(Qt::SolidLine);
  42. }
  43. }
  44. void Forme::setstrokecolor(QString color)
  45. {this->strokecolor.setNamedColor(color);
  46. if(!this->strokecolor.isValid())
  47. {
  48. this->pen.setStyle(Qt::NoPen);
  49. //painter->setBrush(this->strokecolor);
  50. // painter->setPen(Qt::NoPen);
  51. // painter->setPen(QPen(this->strokecolor, 1));
  52. // painter->setPen(Qt::SolidLine);
  53. }
  54. else
  55. {
  56. this->pen.setColor(this->strokecolor);
  57. this->pen.setStyle(Qt::SolidLine);
  58. }
  59. this->update();}
  60. /*****************constructors******************/
  61. Forme::Forme(QPointF origin, QGraphicsItem *parent):
  62. QGraphicsItem(parent)
  63. {
  64. //this->drpathsnmbr=0;
  65. this->strokecolor= QColor(Qt::blue);
  66. this->fillcolor=QColor(Qt::green);
  67. setAcceptDrops(true);
  68. //!! this->setAcceptedMouseButtons(Qt::AllButtons);
  69. //this->setAcceptedMouseButtons(Qt::AllButtons);
  70. this->setAcceptTouchEvents(true);
  71. this->pathsnmbr=0;
  72. this->nodenumber=1;
  73. this->isrealdraw=true;
  74. QStringList pathstringcomputedlist;
  75. this->pathstringcomputedlist.append("break");
  76. this->pathstringcomputedlist.append(QVariant(origin.x()).toString()+':'+QVariant(origin.y()).toString());
  77. QStringList pathstringlist;// ("");
  78. QList<drawPath> shape;
  79. this->shape<<drawPath(origin);
  80. // drawPath *shape[]={new drawPath(origin)};
  81. // this->shape[pathsnmbr]=new drawPath(QPointF(x,y));
  82. this->path= QPainterPath(origin);
  83. this->lastpoint=origin;
  84. //this->nodenumber++;
  85. ///this->pathstringlist=NULL;
  86. //this->setParent(parent);
  87. //setCursor(Qt::OpenHandCursor);
  88. this->setZValue(0);
  89. // this->setFlag(QGraphicsItem::ItemIsSelectable,true);
  90. // this->setFlag(QGraphicsItem::ItemIsMovable,true);
  91. // drawPath temp(currentPosition());
  92. // this->shape.append(temp);
  93. }
  94. //*****************copie makers******************//
  95. Forme& Forme::operator=(Forme const& sourcedecopie)
  96. {
  97. this->lastpoint=sourcedecopie.lastpoint;
  98. //QGraphicsItem(this);
  99. std::cout<<"copiteur=!\n"<<std::endl;
  100. // this->setSelected(sourcedecopie.isSelected());
  101. int i=0;
  102. QStringList pathstringcomputedlist(sourcedecopie.pathstringcomputedlist);
  103. QStringList pathstringlist(sourcedecopie.pathstringlist);
  104. this->shape=((sourcedecopie.shape));
  105. this->path= QPainterPath(sourcedecopie.path);
  106. this->pathsnmbr=sourcedecopie.pathsnmbr;
  107. this->nodenumber=sourcedecopie.nodenumber;
  108. //!! this->setAcceptedMouseButtons(Qt::AllButtons);
  109. this->setFlags(sourcedecopie.flags());
  110. setAcceptDrops(true);
  111. return *this;
  112. }
  113. ////hazardous copy constructor
  114. //Forme::Forme(const Forme &sourcedecopie):QGraphicsItem()
  115. //{
  116. // //wtf QPainterPath path(arg.path);
  117. // int i;
  118. // std::cout<<"copiteur&!\n"<<std::endl;
  119. // this->lastpoint=sourcedecopie.lastpoint;
  120. // this->shape=sourcedecopie.shape;
  121. // // this->shape=sourcedecopie.shape;
  122. // this->pathsnmbr=sourcedecopie.pathsnmbr;
  123. // this->nodenumber=sourcedecopie.nodenumber;
  124. // this->path= QPainterPath(sourcedecopie.path);
  125. // // setCursor(Qt::OpenHandCursor);
  126. // std::cout<<"copiteur de fonction!\n"<<std::endl;
  127. // this->setFlags(sourcedecopie.flags());
  128. // this->setSelected(sourcedecopie.isSelected());
  129. // QStringList pathstringcomputedlist(sourcedecopie.pathstringcomputedlist);
  130. // QStringList pathstringlist(sourcedecopie.pathstringlist);
  131. //}
  132. //*****************************"herited"**************//
  133. void Forme::lineTo(QPointF target_node)
  134. {
  135. this->lineto(target_node.x(),target_node.y());
  136. // this->pathstringcomputedlist.append(QVariant(target_node.x()).toString()+':'+QVariant(target_node.y()).toString());
  137. }
  138. void Forme::paint(QPainter *painter,
  139. const QStyleOptionGraphicsItem *option, QWidget *widget)
  140. {
  141. Q_UNUSED(option);
  142. Q_UNUSED(widget);
  143. // std::cout<<"Forme painteur!\n"<<std::endl;
  144. // if(isrealdraw)
  145. //{
  146. // painter->setBrush(Qt::Dense3Pattern);
  147. painter->setBrush(this->brush);
  148. //painter->setBrush(this->strokecolor);
  149. painter->setPen(this->pen);
  150. // painter->setPen(QPen(this->strokecolor, 1));
  151. // painter->setPen(Qt::SolidLine);
  152. // }
  153. // else
  154. // painter->setPen(QPen(Qt::red, 1));
  155. // painter->scale(-1,-1);
  156. painter->drawPath(this->path);
  157. //painter->scale(-1,-1);
  158. //this->setParent(widget);
  159. //painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize);
  160. //ca cest maarant
  161. bool visu=false;
  162. int i;
  163. if(visu)
  164. {
  165. for (i=0;i<this->path.elementCount();i++)
  166. {
  167. if(i==0)
  168. { painter->setPen(QPen(Qt::green, 1));
  169. painter->setBrush(QColor(Qt::green));
  170. painter->drawText(path.elementAt(i).x-5,path.elementAt(i).y-5,"Start");
  171. painter->drawEllipse(path.elementAt(i).x-2,path.elementAt(i).y-2,4,4);
  172. }
  173. else if (i==this->path.elementCount()-1)
  174. {
  175. painter->setBrush(QColor(Qt::yellow));
  176. painter->setPen(QPen(Qt::yellow, 1));
  177. painter->drawText(path.elementAt(i).x-2,path.elementAt(i).y-2,"End");
  178. painter->drawEllipse(path.elementAt(i).x-2,path.elementAt(i).y-2,4,4);
  179. }
  180. else {
  181. painter->drawEllipse(path.elementAt(i).x-2,path.elementAt(i).y-2,4,4);
  182. }
  183. }
  184. }}
  185. QRectF Forme::boundingRect() const
  186. { return this->path.boundingRect();
  187. }
  188. QPointF Forme:: currentPosition(void)
  189. {
  190. return this->path.currentPosition();
  191. }
  192. /*************************************fonctions***************************************************/
  193. void Forme:: ping(void) const
  194. {
  195. // qDebug("pingselectdrawpath");
  196. std::cout<<this->getnode_number()<<"nodes."<<this->getpath_number()<<"subpaths:"<<std::endl;
  197. }
  198. void Forme:: pingname(void) const
  199. {
  200. std::cout<<"Forme(pingname)"<<std::endl;
  201. }
  202. void Forme::lineto(float x, float y)
  203. {
  204. // std::cout<<"Forme::lineto(coords="<<QVariant(lastpoint.x()).toString().toStdString()+QVariant(lastpoint.y()).toString().toStdString()<<")"<<std::endl;
  205. //std::cout<<"Forme::lineto(coords="<<QVariant(x).toString()<<':'<<QVariant(y).toString()<<")"<<std::endl;
  206. // std::cout<<"Forme::lineto(coords="<<QVariant(lastpoint.x()).toString().toStdString()+QVariant(lastpoint.y()).toString().toStdString()<<")"<<std::endl;
  207. this->pathstringcomputedlist.append(QVariant(x).toString()+':'+QVariant(y).toString());
  208. //std::cout<<pathstringcomputedlist.join("").toStdString();
  209. if( this->nodenumber==0)
  210. {
  211. // std::cout<<"Forme:lineto:butnodenmber=0!"<<std::endl;
  212. //QStringList pathstringcomputedlist;;
  213. //QStringList pathstringlist ("");
  214. //pathstringcomputedlist<<"break";
  215. this->lastpoint=QPointF(x,y);
  216. this->path.lineTo(QPointF(x,y));
  217. //this->pathstringcomputedlist.append(QVariant(x).toString()+':'+QVariant(y).toString());
  218. }
  219. else
  220. {
  221. if(nodenumber%2)
  222. {
  223. //1,3,5nodes
  224. // std::cout<<"Forme:lineto:impair!"<<nodenumber<<std::endl;
  225. this->shape<<drawPath(lastpoint);
  226. this->shape.last().setrealdraw(this->isrealdraw);
  227. this->shape.last().lineTo(QPointF(x,y));
  228. this->nodenumber++;
  229. this->pathsnmbr++;
  230. this->path.lineTo(QPointF(x,y));
  231. this->lastpoint=QPointF(x,y);
  232. }else
  233. {
  234. // std::cout<<"Forme:lineto:pair!"<<nodenumber<<std::endl;
  235. //2,4,6nodes
  236. this->nodenumber++;
  237. this->shape<<drawPath(lastpoint);
  238. this->shape.last().setrealdraw(this->isrealdraw);
  239. this->shape.last().lineTo(QPointF(x,y));
  240. // this->shape<<drawPath(QPointF(x,y));
  241. this-> pathsnmbr++;
  242. this-> lastpoint=QPointF(x,y);
  243. this->path.lineTo(QPointF(x,y));
  244. }
  245. }
  246. }
  247. /*
  248. void Forme::breakpath(QPointF dest)
  249. {
  250. //this->tempbroke=true;
  251. // this->nodenumber=0;
  252. // this->pathsnmbr++;
  253. //suspect!!!
  254. std::cout<<"Forme(breakpath)"<<std::endl;
  255. // this->shape[pathsnmbr]=new drawPath(this->path.currentPosition(););
  256. // this->path.moveTo(dest);
  257. //his->shape[pathsnmbr]->lineTo(this->path.currentPosition());
  258. this->shape[pathsnmbr]->moveTo(dest);
  259. this->pathstringcomputedlist.append("break");
  260. this->pathstringcomputedlist.append(QVariant(dest.x()).toString()+':'+QVariant(dest.y()).toString());
  261. }*/
  262. //***************************************setters and getters************************************//
  263. //***************************************setters and getters************************************//
  264. //***************************************setters and getters************************************//
  265. QPainterPath Forme::getpath() const{
  266. return this->path;
  267. }
  268. QList<drawPath> Forme::getshape() {
  269. return (this->shape);
  270. }
  271. int Forme::getnode_number() const{
  272. return this->nodenumber;
  273. }
  274. // :/
  275. int Forme::getpath_number() const{
  276. return this->pathsnmbr;
  277. // :/
  278. }
  279. QStringList Forme::getstring(void) const
  280. {
  281. return this->pathstringlist;
  282. }
  283. QStringList Forme::getcomputedstring(void) const
  284. {
  285. return this->pathstringcomputedlist;
  286. }
  287. void Forme::setstring(QStringList toset)
  288. {
  289. this->pathstringlist=toset;
  290. }
  291. //********************************************events*****************************************//
  292. //********************************************events*****************************************//
  293. //********************************************events*****************************************//
  294. void Forme::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
  295. {
  296. if (event->mimeData()->hasColor()) {
  297. event->setAccepted(true);
  298. //dragOver = true;
  299. update();
  300. } else {
  301. event->setAccepted(false);
  302. }
  303. std::cout<<"Forme(dragEnterEvent)"<<std::endl;
  304. }
  305. void Forme::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
  306. {
  307. Q_UNUSED(event);
  308. std::cout<<"Forme(dragLeaveEvent)"<<std::endl;
  309. //dragOver = false;
  310. update();
  311. }
  312. void Forme::dropEvent(QGraphicsSceneDragDropEvent *event)
  313. {
  314. //dragOver = false;
  315. if (event->mimeData()->hasColor())
  316. update();
  317. std::cout<<"Forme(dropEvent)"<<std::endl;
  318. //this->fillcolor = qvariant_cast<QColor>(event->mimeData()->colorData());
  319. this->update();
  320. }
  321. void Forme::mousePressEvent(QGraphicsSceneMouseEvent *event)
  322. {
  323. //switch()
  324. // event->accept();
  325. qDebug()<<"forme::mousePressEvent";
  326. // selectsettingwindow *truc=new selectsettingwindow();
  327. // truc->show();
  328. //emit(formeclicked());
  329. // std::cout<<"forme mousePressEvent"<<std::endl;
  330. // // setCursor(Qt::ClosedHandCursor);
  331. // if(this->isSelected())
  332. // {
  333. // this->setSelected(false);
  334. // //this->setActive(false);
  335. // this->setGraphicsEffect(0);
  336. // }
  337. // else{
  338. // this->setSelected(true);
  339. // //this->setActive(true);
  340. // QGraphicsDropShadowEffect* green_colorize=new QGraphicsDropShadowEffect();
  341. // green_colorize->setColor(QColor().green());
  342. // this->setGraphicsEffect(green_colorize);
  343. // }
  344. // event->accept();
  345. }
  346. void Forme::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  347. {
  348. //setCursor(Qt::OpenHandCursor);
  349. // event->accept();
  350. qDebug()<<"forme::mouserelevent";
  351. //this->scene()
  352. }
  353. //accesseur: set true if param =true
  354. void Forme::setrealdraw(bool param)
  355. {
  356. isrealdraw=param;
  357. //foreachsubpath: setreal or not;
  358. }
  359. //june2017
  360. //Forme::~Forme()
  361. //{
  362. //delete this->shape;
  363. //}
  364. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  365. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  366. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  367. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  368. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  369. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  370. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  371. //blablablablablablablablablablablablablablablablablablablablablablablablablablablabla
  372. //to delete
  373. //Forme::Forme(QGraphicsItem *parent):
  374. // QGraphicsItem(parent)
  375. //{
  376. // path= QPainterPath();
  377. // this->pathsnmbr=-1;
  378. //this->drpathsnmbr=0;
  379. // this->path=QPainterPath();
  380. // this->nodenumber=0;
  381. //QString name=("notset");
  382. // std::cout<<"Forme(0);"<<std::endl;
  383. // QList<drawPath> shape;
  384. // QStringList pathstringcomputedlist;
  385. // pathstringcomputedlist<<"break";
  386. // QStringList pathstringlist ("");
  387. // this->setAcceptedMouseButtons(Qt::AllButtons);
  388. //setAcceptedMouseButtons(Qt::LeftButton);
  389. //this->tempbroke=false;
  390. // setAcceptDrops(true);
  391. //this->setParent(parent);
  392. // this->setFlag(QGraphicsItem::ItemIsSelectable,true);
  393. // this->setFlag(QGraphicsItem::ItemIsMovable,true);
  394. // setCursor(Qt::OpenHandCursor);
  395. // this->lastpoint=QPointF(-999,-999);
  396. //}
  397. /*
  398. void Forme::add_shape(QStringList *toadd)
  399. {
  400. int i=0,point_count=toadd->size();
  401. QStringList list1 = toadd->at(0).split(":", QString::SkipEmptyParts);
  402. QList<drawPath> *shape;
  403. //ontrace le prmuer trait
  404. //!!!
  405. //!
  406. //!
  407. //!
  408. //!
  409. //! this->shape[0]= drawPath(QPointF(list1.at(0).toFloat(),list1.at(1).toFloat()));
  410. void Forme::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  411. {
  412. std::cout<<"Forme(mousemoveEvent)"<<std::endl;
  413. event->ignore();
  414. setCursor(Qt::OpenHandCursor);
  415. }
  416. //QStringList list2 = toadd->at(i+1).split(":", QString::SkipEmptyParts);
  417. for (i;i < point_count-1; ++i)
  418. {
  419. pathsnmbr++;
  420. std::cout<<i<<".275addshpe(stringlist):Forme."<<list1.at(0).toFloat()<<":"<<list1.at(1).toFloat()<<std::endl;
  421. list1 = toadd->at(i).split(":", QString::SkipEmptyParts);
  422. //!\//pseudo worked: added multiple ligne with a very rounded value :p
  423. // remeber!pth.lineTo(cl.toDouble( list1.at(0)),cl.toDouble( list1.at(1)));
  424. //!!! this->lineTo(list1.at(0).toFloat(),list1.at(1).toFloat());
  425. }
  426. QPen visiblepen(Qt::blue, 2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
  427. QPen normalpen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
  428. //Qpath.setPen(pen);
  429. // Qpath.setPath(pth);
  430. //this->sceneConteneur->addPath(pth_end,redpen);
  431. drawPath pth_start;
  432. switch(this->startpath_mode)
  433. {
  434. case topright:
  435. this-> pth_start=new drawPath(this->path.boundingRect().topRight());
  436. this->pth_start->lineTo(QPointF(list1.at(0).toFloat(),list1.at(1).toFloat()));
  437. pth_start.is_realdraw=false;
  438. break;
  439. case bottomright:
  440. //pth_start= drawPath(this->path.boundingRect().bottomRight());
  441. this-> pth_start=new drawPath(this->path.boundingRect().bottomRight());
  442. pth_start.lineTo(QPointF(list1.at(0).toFloat(),list1.at(1).toFloat()));
  443. pth_start.is_realdraw=false;
  444. break;
  445. case topleft:
  446. //pth_start= drawPath(this->path.boundingRect().topLeft());
  447. this-> pth_start=new drawPath(this->path.boundingRect().topLeft());
  448. pth_start.lineTo(QPointF(list1.at(0).toFloat(),list1.at(1).toFloat()));
  449. pth_start.is_realdraw=false;
  450. break;
  451. case bottomleft:
  452. this-> pth_start=new drawPath(this->path.boundingRect().bottomLeft());
  453. drawPath pth_start(this->path.boundingRect().bottomLeft());
  454. pth_start.lineTo(QPointF(list1.at(0).toFloat(),list1.at(1).toFloat()));
  455. pth_start.is_realdraw=false;
  456. break;
  457. defaut:break;
  458. }
  459. drawPath pth_end(this->currentPosition());
  460. switch(this->endpath_mode)
  461. {
  462. case topright:pth_end.lineTo(this->path.boundingRect().topRight());
  463. break;
  464. case bottomright:pth_end.lineTo(this->path.boundingRect().bottomRight());
  465. pth_end.is_realdraw=false;
  466. break;
  467. case topleft:pth_end.lineTo(this->path.boundingRect().topLeft());
  468. break;
  469. case bottomleft:pth_end.lineTo(this->path.boundingRect().bottomLeft());
  470. break;
  471. }
  472. }
  473. // this->sceneVue->fitInView(this->sceneConteneur->addPath(pth,visiblepen));
  474. */
  475. //hazardous copy constructor
  476. /*
  477. //!??
  478. Forme::Forme(const QGraphicsItem &arg)
  479. {
  480. //wtf QPainterPath path(arg.path);
  481. int i=0;
  482. for(i;i<arg.)
  483. this->shape=arg.shape;
  484. this->pathsnmbr=arg.pthnmb;
  485. }
  486. */