svgview.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the examples of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** You may use this file under the terms of the BSD license as follows:
  10. **
  11. ** "Redistribution and use in source and binary forms, with or without
  12. ** modification, are permitted provided that the following conditions are
  13. ** met:
  14. ** * Redistributions of source code must retain the above copyright
  15. ** notice, this list of conditions and the following disclaimer.
  16. ** * Redistributions in binary form must reproduce the above copyright
  17. ** notice, this list of conditions and the following disclaimer in
  18. ** the documentation and/or other materials provided with the
  19. ** distribution.
  20. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  21. ** of its contributors may be used to endorse or promote products derived
  22. ** from this software without specific prior written permission.
  23. **
  24. **
  25. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. #include "svgview.h"
  41. #include <QFile>
  42. #include <QWheelEvent>
  43. #include <QMouseEvent>
  44. #include <QGraphicsRectItem>
  45. //#include <QGraphicsSvgItem>
  46. #include <QPaintEvent>
  47. #include <qmath.h>
  48. #include <QDebug>
  49. #include <QString>
  50. #include <QXmlStreamReader>
  51. #include <QMimeData>
  52. #include <QDebug>
  53. #include <QMenu>
  54. #include <QAction>
  55. #ifndef QT_NO_OPENGL
  56. #include <QGLWidget>
  57. #endif
  58. /*map:
  59. * constructors
  60. * copie makers
  61. * herited fonctions
  62. * fonctions
  63. * getter/setters
  64. * events
  65. * destruction!!!
  66. */
  67. SvgView::SvgView(QWidget *parent)
  68. : QGraphicsView(parent), totalScaleFactor(1)
  69. , m_renderer(Native)
  70. , m_svgItem(0)
  71. , m_backgroundItem(0)
  72. , m_outlineItem(0)
  73. {
  74. setScene(new QGraphicsScene(this));
  75. //!! setTransformationAnchor(AnchorUnderMouse);
  76. setDragMode(ScrollHandDrag);
  77. // setViewportUpdateMode(FullViewportUpdate);
  78. this->setAcceptDrops(true);
  79. this->setMouseTracking(true);
  80. // Prepare background check-board pattern
  81. QPixmap tilePixmap(64, 64);
  82. tilePixmap.fill(Qt::white);
  83. QPainter tilePainter(&tilePixmap);
  84. QColor color(220, 220, 220);
  85. tilePainter.fillRect(0, 0, 32, 32, color);
  86. tilePainter.fillRect(32, 32, 32, 32, color);
  87. tilePainter.end();
  88. //this->setTabletTracking(true);
  89. viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
  90. //parent->setAcceptTouchEvents(true);
  91. this->setEnabled(true);
  92. setBackgroundBrush(tilePixmap);
  93. }
  94. bool SvgView::viewportEvent(QEvent *event) {
  95. qDebug()<<"svgview::eventFilter:"<<event->type();
  96. if (event->type() == QEvent::MouseMove) {
  97. // code for mouse event
  98. } else if (event->type() == QEvent::Wheel) {
  99. // code for wheel event
  100. } else if (event->type() == QEvent::TouchBegin || event->type() == QEvent::TouchUpdate || event->type() == QEvent::TouchEnd) {
  101. QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
  102. QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
  103. if (touchPoints.count() == 2) {
  104. // determine scale factor
  105. const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
  106. const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
  107. qreal currentScaleFactor = QLineF(touchPoint0.pos(), touchPoint1.pos()).length() / QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length();
  108. if (touchEvent->touchPointStates() & Qt::TouchPointReleased) {
  109. // if one of the fingers is released, remember the current scale
  110. // factor so that adding another finger later will continue zooming
  111. // by adding new scale factor to the existing remembered value.
  112. totalScaleFactor *= currentScaleFactor;
  113. //currentScaleFactor = 1;
  114. }
  115. gentle_zoom(currentScaleFactor);
  116. }
  117. return true;
  118. }
  119. return QGraphicsView::viewportEvent(event);
  120. }
  121. void SvgView::gentle_zoom(double factor) {
  122. qDebug()<<" if"<<factor;
  123. if (this->currentScale * factor < scaleMin) {
  124. this->scale(factor, factor); // this->fitInView(this->scene()->sceneRect(), Qt::KeepAspectRatio);
  125. // emit zoomed(false); qDebug()<<"svgview::gentle_zoom:scale="<<factor;
  126. return;
  127. }
  128. else { qDebug()<<"else"<<factor;
  129. this->fitInView(this->scene()->sceneRect(), Qt::KeepAspectRatio);
  130. // this->scale(factor, factor);
  131. // currentScale *= factor;
  132. // this->centerOn(this->scene()->sceneRect().center());//!
  133. // QPointF delta_viewport_pos = this->scene()->sceneRect().center() - QPointF(this->viewport()->width() / 2.0,
  134. // this->viewport()->height() / 2.0);
  135. // QPointF viewport_center = this->mapFromScene(this->scene()->sceneRect().center()) - delta_viewport_pos;
  136. // this->centerOn(this->mapToScene(viewport_center.toPoint()));
  137. //! emit zoomed(true);
  138. }
  139. }
  140. bool SvgView::qevent(QEvent *event)
  141. {
  142. switch(event->type())
  143. {
  144. case QEvent::TouchBegin:
  145. qDebug()<<"svgview::TouchBegin event()";
  146. event->accept();
  147. break;
  148. case QEvent::TouchUpdate:
  149. qDebug()<<"svgview::TouchUpdate event()";
  150. break;
  151. case QEvent::TouchEnd:
  152. qDebug()<<"svgview::TouchEnd event()";
  153. break;
  154. case QEvent::TouchCancel:
  155. qDebug()<<"svgview::TouchCancel event()";
  156. break;
  157. case QEvent::InputMethodQuery:
  158. qDebug()<<"svgview::InputMethodQuery event()";
  159. event->accept();
  160. break;
  161. default:
  162. qDebug()<<"svgview::event() not handled"<<event->type();
  163. }
  164. event->ignore();
  165. // event->accept();
  166. return true;
  167. }
  168. //void SvgView::touchEvent(QTouchEvent *event)
  169. //{
  170. // qDebug()<<"svgview::tabletEvent";
  171. // event->ignore();
  172. //// event->accept();
  173. //}
  174. //void SvgView::mouseReleaseEvent(QMouseEvent *event)
  175. //{
  176. // // qDebug()<<"svgview::mouserelEvent";
  177. // event->ignore();
  178. //// event->accept();
  179. //}
  180. //void SvgView::mouseReleaseEvent(QMouseEvent *event)
  181. //{
  182. // // qDebug()<<"svgview::mouserelEvent";
  183. // event->ignore();
  184. //// event->accept();
  185. //}
  186. //void SvgView::mousePressEvent(QMouseEvent *event)
  187. //{
  188. //// qDebug()<<"svgview::mousePressEvent";
  189. // event->ignore();
  190. //}
  191. void SvgView::dragEnterEvent(QDragEnterEvent *event)
  192. {
  193. event->ignore();
  194. qDebug()<<"svgview dragEnterEvent";
  195. if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
  196. if (children().contains(event->source())) {
  197. event->setDropAction(Qt::MoveAction);
  198. event->accept();
  199. } else {
  200. event->acceptProposedAction();
  201. }
  202. } else if (event->mimeData()->hasText()) {
  203. event->acceptProposedAction();
  204. } else {
  205. event->ignore();
  206. }
  207. }
  208. void SvgView::dragMoveEvent(QDragMoveEvent *event)
  209. {
  210. event->ignore();
  211. qDebug()<<"svgview dragMoveEvent";
  212. if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
  213. if (children().contains(event->source())) {
  214. event->setDropAction(Qt::MoveAction);
  215. event->accept();
  216. } else {
  217. event->acceptProposedAction();
  218. }
  219. } else if (event->mimeData()->hasText()) {
  220. event->acceptProposedAction();
  221. } else {
  222. event->ignore();
  223. }
  224. }
  225. void SvgView::dropEvent(QDropEvent *event)
  226. {
  227. qDebug()<<"svgview dropEvent";
  228. event->ignore();
  229. if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
  230. qDebug()<<"fridgemagnet dropEvent";
  231. if (event->mimeData()->hasColor())
  232. { qDebug()<<"hasColor dropEvent";
  233. }
  234. }
  235. /*
  236. const QMimeData *mime = event->mimeData();
  237. QByteArray itemData = mime->data("application/x-fridgemagnet");
  238. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
  239. QColor text;
  240. QColor text1;
  241. QPoint offset;
  242. dataStream >> text >> offset;
  243. DragLabel *newLabel = new DragLabel(text.name(), this);
  244. newLabel->move(event->pos());
  245. newLabel->show();
  246. qDebug()<<"svgview dropEvent";
  247. // newLabel->setAttribute(Qt::WA_DeleteOnClose);
  248. if (event->source() == this) {
  249. event->setDropAction(Qt::MoveAction);
  250. event->accept();
  251. } else {
  252. event->acceptProposedAction();
  253. }
  254. } else if (event->mimeData()->hasText()) {
  255. QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
  256. QString::SkipEmptyParts);
  257. QPoint position = event->pos();
  258. foreach (QString piece, pieces) {
  259. DragLabel *newLabel = new DragLabel(piece, this);
  260. newLabel->move(position);
  261. newLabel->show();
  262. // newLabel->setAttribute(Qt::WA_DeleteOnClose);
  263. position += QPoint(newLabel->width(), 0);
  264. }
  265. event->acceptProposedAction();
  266. } else {
  267. event->ignore();
  268. }*/
  269. }
  270. //void SvgView::mousePressEvent(QMouseEvent *event)
  271. //{
  272. // qDebug()<<"svgview mousePressEvent";
  273. /*event->accept();
  274. Forme *child = static_cast<Forme*>(itemAt(event->pos()));
  275. if (!child)
  276. {qDebug()<<"svgview mousePressEvent nochild";return;
  277. }else qDebug()<<child->getcomputedstring();
  278. QPoint hotSpot = event->pos() - child->pos().toPoint();
  279. QByteArray itemData;
  280. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
  281. dataStream << child->fillcolor << QPoint(hotSpot)<<child->strokecolor;
  282. QMimeData *mimeData = new QMimeData;
  283. mimeData->setData("application/x-fridgemagnet", itemData);
  284. mimeData->setText("?");
  285. QDrag *drag = new QDrag(this);
  286. drag->setMimeData(mimeData);
  287. //drag->setPixmap(*child->pixmap());
  288. drag->setHotSpot(hotSpot);
  289. // child->hide();
  290. if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
  291. {} //child->close();
  292. else
  293. child->show();
  294. }*/
  295. void SvgView::drawBackground(QPainter *p, const QRectF &)
  296. {
  297. p->save();
  298. p->resetTransform();
  299. p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
  300. p->restore();
  301. }
  302. //prio
  303. //converti les points d'un fichier en path (appelé par les load_trucfile)
  304. void SvgView::ajouter_forme(QStringList toadd, QGraphicsScene *s)
  305. {
  306. int pathcount=0;
  307. QList<Forme*> pth;//={0};
  308. QList<QGraphicsTextItem*> txt;//={0};
  309. int txtcount=0;
  310. QXmlStreamReader xml;
  311. xml.addData(toadd.join(""));
  312. while (!xml.atEnd()) {
  313. xml.readNext();
  314. //qDebug()<<xml.name();
  315. if(xml.isStartElement())
  316. {
  317. if(xml.name()=="rect")
  318. {
  319. //pth<<new Forme(xml.attributes())
  320. qDebug()<<"rect pos x:"<<xml.attributes().value("x").toString().toFloat();
  321. // qDebug()<<"rect pos y:"<<xml.attributes().value("y");
  322. // qDebug()<<"rect height"<<xml.attributes().value("height");
  323. // qDebug()<<"rect width:"<<xml.attributes().value("width");
  324. qDebug()<<"rect style:"<<xml.attributes().value("style");
  325. QString style=xml.attributes().value("style").toString();
  326. int pos1=style.indexOf("fill:");
  327. int pos2=style.indexOf("stroke:");
  328. int pos3=style.indexOf("fill-opacity:");
  329. int pos4=style.indexOf("stroke-width:");
  330. //hide inffil!!!!fill-opacity:0""
  331. // qDebug()<< style.section("fill:",pos1-1,pos2);
  332. qDebug()<< pos1<<pos2;
  333. qDebug()<<style.mid(pos1+5,7);
  334. qDebug()<<style.mid(pos2+7,7);
  335. qDebug()<<style.mid(pos3+13,1);
  336. qDebug()<<style.mid(pos4+13,2);
  337. // qDebug()<< style.section("stroke:",pos2-1,pos2+6);
  338. // qDebug()<<"rect strokecolor:"<<xml.attributes().value("stroke");
  339. Forme *tmp=new Forme(QPointF(
  340. xml.attributes().value("x").toString().toFloat(),
  341. xml.attributes().value("y").toString().toFloat()
  342. ));
  343. //must extractfrom xml.attributes().value("style");
  344. //TODO
  345. if(style.mid(pos3+13,1)=="1")
  346. {tmp->setcolor( style.mid(pos1+5,7));
  347. tmp->setZValue(1);
  348. }
  349. else
  350. { tmp->setZValue(0);
  351. tmp->setcolor("none");
  352. }
  353. // stroke-width:0.
  354. QString tmpbis=QString(style.mid(pos4,22));
  355. int pos5=tmpbis.indexOf(";");
  356. QString tmpter=QString(tmpbis.left(pos5));
  357. int pos6=tmpter.indexOf(":");
  358. QString tmpquat=QString(tmpter.mid(pos6+1,tmpter.size()-pos6));
  359. qDebug()<<"tmpquat"<<tmpquat;
  360. if(tmpquat=="0")
  361. {
  362. tmp->setstrokecolor("none");
  363. }
  364. else
  365. {//oups
  366. tmp->setstrokewidth(tmpquat);
  367. }
  368. tmp->setToolTip( xml.attributes().value("id").toString()
  369. +"\nColor:"+style.mid(pos1+5,7)
  370. +", opacity:"+style.mid(pos3+13,1)
  371. +"\nStroke-color:"+style.mid(pos2+7,7)
  372. //+", width"+style.mid(pos4+13,2)
  373. +", width"+tmpquat
  374. );
  375. tmp->setFlags(QGraphicsItem::ItemClipsToShape);
  376. tmp->setCacheMode(QGraphicsItem::NoCache);
  377. // tmp->setFlag(QGraphicsItem::ItemIsSelectable,true);
  378. tmp->setAcceptDrops(true);
  379. // tmp->setFlag(QGraphicsItem::ItemIsMovable,true);
  380. //go point n2
  381. tmp->lineto(
  382. xml.attributes().value("x").toString().toFloat(),
  383. xml.attributes().value("y").toString().toFloat()+xml.attributes().value("height").toString().toFloat()
  384. );
  385. //go dot n3
  386. tmp->lineto(
  387. xml.attributes().value("x").toString().toFloat()+xml.attributes().value("width").toString().toFloat(),
  388. xml.attributes().value("y").toString().toFloat()+xml.attributes().value("height").toString().toFloat()
  389. );
  390. //go dot n4
  391. tmp->lineto(
  392. xml.attributes().value("x").toString().toFloat()+xml.attributes().value("width").toString().toFloat(),
  393. xml.attributes().value("y").toString().toFloat()
  394. );
  395. //close loop going back to node1
  396. tmp->lineto(
  397. xml.attributes().value("x").toString().toFloat(),
  398. xml.attributes().value("y").toString().toFloat()
  399. );
  400. pth<<tmp;
  401. pathcount++;
  402. //pth++;
  403. }
  404. else if(xml.name()=="g")
  405. { qDebug()<<"svgview:g not shown yet\n";
  406. }
  407. else if(xml.name()=="line")
  408. { qDebug()<<"svgview:line not shown yet\n";
  409. }
  410. else if(xml.name()=="path")
  411. { qDebug()<<"svgview:WIP\n";
  412. qDebug()<<"path style:"<<xml.attributes().value("style");
  413. // style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
  414. //https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
  415. QString style=xml.attributes().value("style").toString();
  416. int pos1=style.indexOf("fill:");
  417. int pos2=style.indexOf("fill-opacity:");
  418. int pos3=style.indexOf("stroke:");
  419. int pos4=style.indexOf("stroke-width:");
  420. qDebug()<<"path data:"<<xml.attributes().value("d").toString();
  421. QStringList tmpcoords=xml.attributes().value("d").toString().split(" ");
  422. int subpathcount=0;
  423. QString pathcommand="";
  424. bool didstart=false;
  425. for(int i=0;i<tmpcoords.size();i++)
  426. {
  427. if(tmpcoords.at(i).length()<2)
  428. {pathcommand=tmpcoords.at(i);
  429. qDebug()<<"pathcmd:"<<pathcommand;
  430. }
  431. else
  432. {
  433. // qDebug()<<"des buggys" <<tmpcoords[i];
  434. // qDebug()<<"des buggys" <<tmpcoords.at(i);
  435. QStringList tmpret=tmpcoords[i].split(',');
  436. if(!didstart)
  437. {
  438. Forme *tmp=new Forme(QPointF(
  439. tmpret.at(0).toFloat(),
  440. tmpret.at(1).toFloat()
  441. ));
  442. pth<<tmp;
  443. subpathcount++;pathcount++;
  444. qDebug()<<"Start x:"<<tmpret.at(0)<<"y:"<< tmpret.at(1);
  445. didstart=true;
  446. }
  447. else{
  448. if(tmpret.size()>1)
  449. {
  450. qDebug()<<"inloopx:"<<tmpret.at(0).toFloat()<<"y:"<< tmpret.at(1).toFloat();
  451. pth[subpathcount-1]->lineto( tmpret.at(0).toFloat(),
  452. tmpret.at(1).toFloat());
  453. }else{qDebug()<<"bug!";}
  454. }
  455. }
  456. }
  457. } else if(xml.name()=="image")
  458. {
  459. qDebug()<<"svgview:image not shown yet\n";
  460. }else if(xml.name()=="text")
  461. {
  462. //qDebug()<<"TEXT:\n";
  463. // qDebug()<<xml.attributes().value("style").toString()<<endl;
  464. QString style=xml.attributes().value("style").toString();
  465. int pos3=style.indexOf("font-size:");
  466. int pos4=style.indexOf("px;line-height");
  467. qDebug()<<"font-size:"<<style.mid(pos3+10,pos4-pos3-10).toFloat();
  468. this->serifFont= QFont("Sans-serif", style.mid(pos3+10,pos4-pos3-10).toFloat(), QFont::Normal);
  469. // qDebug()<<xml.attributes().value("style").toString()<<endl;
  470. }else if(xml.name()=="tspan")
  471. {txtcount++;
  472. qDebug()<<"tspan (from TEXT!):\n";
  473. // qDebug()<<xml.attributes().value("style").toString()<<endl;
  474. QString style=xml.attributes().value("style").toString();
  475. QFontMetrics fm(this->serifFont);
  476. QString txte=xml.readElementText();
  477. int textWidthInPixels = fm.horizontalAdvance(txte);
  478. int textHeightInPixels = fm.height();
  479. qDebug()<<"string width"<<fm.horizontalAdvance(txte);
  480. qDebug()<<"string height"<<fm.height();
  481. // qDebug()<<xml.readElementText();
  482. QGraphicsTextItem *tmptxt=new QGraphicsTextItem(txte);
  483. tmptxt->setPos(QPointF(xml.attributes().value("x").toFloat(),
  484. xml.attributes().value("y").toFloat()));
  485. tmptxt->setFont(this->serifFont);
  486. //tmptxt->setTextWidth(1.41111112);
  487. tmptxt->setDefaultTextColor(QColor(Qt::red));
  488. //tmptxt->setPlainText(xml.readElementText());
  489. txt<<tmptxt;
  490. }else
  491. {
  492. qDebug()<<xml.name()<<"not shown yet(TODOWIP/TEXT)\n";
  493. }
  494. }
  495. }
  496. if (xml.hasError()) {
  497. qDebug()<<xml.errorString();
  498. }
  499. int i=0;
  500. int j=0;
  501. for (i; i < pathcount; i++)
  502. {
  503. //!this->scene()->addPath(pth[i].getpath(),
  504. //! QPen (Qt::darkRed, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin));
  505. s->addItem(pth[i]);
  506. // std::cout<<"render"<<pth[i]->getcomputedstring().join("\n").toStdString()<<std::endl;
  507. // this->pathList.append(pth[i]);
  508. }
  509. for (j; j < txtcount; j++)
  510. {
  511. //!this->scene()->addPath(pth[i].getpath(),
  512. //! QPen (Qt::darkRed, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin));
  513. s->addItem(txt[j]);
  514. // std::cout<<"render"<<pth[i]->getcomputedstring().join("\n").toStdString()<<std::endl;
  515. // this->pathList.append(pth[i]);
  516. }
  517. qDebug()<<i<<" qgraphictextitem and "<<j<< "forme added";
  518. // this->scene()->update();
  519. qDebug("svgview: end of xml processing");
  520. return;
  521. //!int pathcount=-1;
  522. bool makenew=false;
  523. i=0;j=0;
  524. int point_count=toadd.size();
  525. // QGraphicsPathItem *Qpath=new QGraphicsPathItem();
  526. QLocale cl(QLocale::system());
  527. QStringList list1;
  528. int nmbpth=toadd.join("").count("<path");
  529. // Forme **pth=(Forme**)malloc(point_count*sizeof(Forme));
  530. //QVector <Forme>pth;
  531. //(Forme**)malloc(point_count*sizeof(Forme));
  532. std::cout <<"zoneDeDessin::ajouter_forme(QStringList toadd):point_count"<< point_count <<std::endl;
  533. std::cout <<"zoneDeDessin::ajouter_forme(QStringList toadd):nombredesubpath"<< nmbpth <<std::endl;
  534. // QList<Forme> pth;//=(Forme*)malloc(toadd.join("").count("break")*sizeof(Forme));
  535. //fu!!!
  536. do
  537. {
  538. //std::cout<<"zoneDeDessin::ajouter_forme() (#"<<i<<"):"<<toadd.at(i).toStdString()<<std::endl;
  539. if(toadd.at(i).contains("break"))
  540. {
  541. makenew=true;
  542. // list1 = toadd.at(i).split(":", QString::SkipEmptyParts);
  543. }//end ifbreak
  544. else
  545. {
  546. if(toadd.at(i).contains(":"))
  547. { if(makenew)
  548. {
  549. pathcount++;
  550. list1 = toadd.at(i).split(":", QString::SkipEmptyParts);
  551. // std::cout<<"zoneDeDessin::ajouter_forme():makeNU:"<<toadd.at(i).toStdString()<<std::endl;
  552. //coords
  553. pth<< new Forme(QPointF(list1.at(0).toFloat(),list1.at(1).toFloat()));
  554. // std::cout<<"zoneDeDessin::ajouter_forme():makeNU:"<<list1.join("").toStdString()<<std::endl;
  555. makenew=false;
  556. }
  557. else
  558. {
  559. // std::cout<<"zoneDeDessin::ajouter_forme():line2:"<<toadd.at(i).toStdString()<<std::endl;
  560. //coords
  561. list1 = toadd.at(i).split(":", QString::SkipEmptyParts);
  562. // pth[pathcount].lineto(list1.at(0).toFloat(),list1.at(1).toFloat());
  563. pth.last()->setrealdraw(true);
  564. pth.last()->lineto(list1.at(0).toFloat(),list1.at(1).toFloat());
  565. // std::cout<<pth[pathcount].getnode_number()<<std::endl;
  566. }
  567. }
  568. else {
  569. std::cout<<"zoneDeDessin::ajouter_forme():read line failed:"<<toadd.at(i).toStdString()<<std::endl;
  570. }
  571. }
  572. i++;
  573. } while(i<point_count);
  574. //end while
  575. //affichage:
  576. if (this->simplepathview==true)
  577. {
  578. std::cout<<"zoneDeDessin::ajouter_forme():drawSIMPLEstarted"<<std::endl;
  579. //shapemode
  580. for (i=0; i < pathcount+1; i++)
  581. {
  582. //!this->scene()->addPath(pth[i].getpath(),
  583. //! QPen (Qt::darkRed, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin));
  584. this->scene()->addItem(pth[i]);
  585. // std::cout<<"render"<<pth[i]->getcomputedstring().join("\n").toStdString()<<std::endl;
  586. this->pathList.append(pth[i]);
  587. }
  588. s->update();
  589. // setpathnumbr(pth[i].getpath_number());
  590. this->update();
  591. }
  592. else
  593. {
  594. std::cout<<"zoneDeDessin::ajouter_forme():drawCOMPLEXstarted"<<std::endl;
  595. QPen pen[]={
  596. QPen(Qt::black, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  597. QPen(Qt::red, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  598. QPen (Qt::darkRed, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  599. QPen(Qt::green, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  600. QPen(Qt::darkGreen, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  601. QPen(Qt::blue, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  602. QPen (Qt::darkBlue, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  603. QPen(Qt::cyan, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  604. QPen(Qt::darkCyan, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  605. QPen(Qt::magenta, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  606. QPen (Qt::darkMagenta, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  607. QPen(Qt::yellow, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  608. QPen(Qt::darkYellow, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  609. QPen(Qt::gray, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  610. QPen (Qt::darkGray, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin),
  611. QPen(Qt::lightGray, 1, Qt::SolidLine, Qt::SquareCap, Qt::SvgMiterJoin)
  612. };
  613. //subpathmode
  614. int i;
  615. // std::cout<<pth->getpath_number()<<std::endl;
  616. // for(i=0;i<pth->getpath_number();i++)
  617. for (i=0; i < pathcount+1; i++)
  618. {
  619. for(j=0;j<pth[i]->getshape().size();j++)
  620. {
  621. QGraphicsItem *toaddb = this->scene()->addPath(pth[i]->getshape()[j].getpath(),pen[i%16]);
  622. // QGraphicsItem *toaddb =
  623. // this->scene()->addPath(pth[i].getshape()[j].getpath(),pen[i%16]);
  624. // QGraphicsItem *toaddb = this->scene()->addItem(pth[i].getshape()[j]);//,pen[i%16]);
  625. //emit(item_ready(pth->shape[i].path));
  626. toaddb->setFlag(QGraphicsItem::ItemIsSelectable,true);
  627. toaddb->setFlag(QGraphicsItem::ItemIsMovable,true);
  628. //this->scene()->fitInView(toaddb);
  629. //toaddb->setScale(-1);
  630. }
  631. // setpathnumbr(i);
  632. }
  633. }
  634. std::cout<<"zoneDeDessin::ajouter_forme():drawended"<<std::endl;
  635. }//end
  636. void SvgView::openFile(const QFile &file)
  637. {
  638. if (!file.exists())
  639. return;
  640. QGraphicsScene *s = scene();
  641. bool drawBackground = (m_backgroundItem ? m_backgroundItem->isVisible() : false);
  642. bool drawOutline = (m_outlineItem ? m_outlineItem->isVisible() : true);
  643. s->clear();
  644. resetTransform();
  645. QFile svgfile(file.fileName());
  646. m_svgItem = new QGraphicsSvgItem(file.fileName());
  647. QStringList filecontent("");
  648. if(!file.fileName().contains("bub"))
  649. {
  650. if (!svgfile.open(QIODevice::ReadOnly | QIODevice::Text))
  651. return;
  652. else{
  653. QTextStream in(&svgfile);
  654. while (!in.atEnd()) {
  655. QString line = in.readLine();
  656. filecontent<<line;
  657. }
  658. ajouter_forme(filecontent,s);
  659. // m_svgItem->dumpObjectTree();
  660. // m_svgItem->setElementId("cutpower");
  661. //if(m_svgItem->elementId()=="cutpower")
  662. //{qDebug("matriiiiiiiice");m_svgItem->setToolTip("cutpower");}
  663. m_svgItem->setFlags(QGraphicsItem::ItemClipsToShape);
  664. m_svgItem->setCacheMode(QGraphicsItem::NoCache);
  665. m_svgItem->setZValue(0);
  666. }
  667. }
  668. else { m_backgroundItem = new QGraphicsRectItem(m_svgItem->boundingRect());
  669. m_backgroundItem->setBrush(Qt::white);
  670. m_backgroundItem->setPen(Qt::NoPen);
  671. m_backgroundItem->setVisible(drawBackground);
  672. m_backgroundItem->setZValue(-1);
  673. s->addItem(m_backgroundItem);
  674. s->addItem(m_svgItem);
  675. }
  676. m_outlineItem = new QGraphicsRectItem(s->itemsBoundingRect());
  677. QPen outline(Qt::black, 2, Qt::DashLine);
  678. outline.setCosmetic(true);
  679. m_outlineItem->setPen(outline);
  680. m_outlineItem->setBrush(Qt::NoBrush);
  681. m_outlineItem->setVisible(drawOutline);
  682. m_outlineItem->setZValue(-1);
  683. s->addItem(m_outlineItem);
  684. s->setSceneRect(m_outlineItem->boundingRect().adjusted(-10, -10, 10, 10));
  685. this->update();
  686. }
  687. void SvgView::setRenderer(RendererType type)
  688. {
  689. m_renderer = type;
  690. if (m_renderer == OpenGL) {
  691. #ifndef QT_NO_OPENGL
  692. setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
  693. #endif
  694. } else {
  695. setViewport(new QWidget);
  696. }
  697. }
  698. void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing)
  699. {
  700. #ifndef QT_NO_OPENGL
  701. setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
  702. #else
  703. Q_UNUSED(highQualityAntialiasing);
  704. #endif
  705. }
  706. void SvgView::setViewBackground(bool enable)
  707. {
  708. if (!m_backgroundItem)
  709. return;
  710. m_backgroundItem->setVisible(enable);
  711. }
  712. void SvgView::setViewOutline(bool enable)
  713. {
  714. if (!m_outlineItem)
  715. return;
  716. m_outlineItem->setVisible(enable);
  717. }
  718. void SvgView::paintEvent(QPaintEvent *event)
  719. {
  720. if (m_renderer == Image) {
  721. if (m_image.size() != viewport()->size()) {
  722. m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
  723. }
  724. QPainter imagePainter(&m_image);
  725. QGraphicsView::render(&imagePainter);
  726. imagePainter.end();
  727. QPainter p(viewport());
  728. p.drawImage(0, 0, m_image);
  729. } else {
  730. QGraphicsView::paintEvent(event);
  731. }
  732. }
  733. void SvgView::wheelEvent(QWheelEvent *event)
  734. {
  735. //std::cout<<"zoneDeDessin::wheelEvent"<<std::endl;
  736. qDebug()<<"SvgView::wheelEvent";
  737. qreal factor = qPow(1.2, event->delta() / 240.0);
  738. scale(factor, factor);
  739. event->accept();
  740. }