QElectroTech  0.70
qetshapeitem.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "qetshapeitem.h"
19 #include "createdxf.h"
20 #include "diagram.h"
21 #include "qet.h"
26 #include "qetxml.h"
27 #include "diagramview.h"
28 #include "qeticons.h"
29 
38 QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem *parent) :
39  QetGraphicsItem(parent),
40  m_shapeType(type),
41  m_P1 (p1),
42  m_P2 (p2),
43  m_hovered(false)
44 {
45  if (type == Polygon) m_polygon << m_P1 << m_P2;
46  setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
47  setAcceptHoverEvents(true);
48  m_pen.setStyle(Qt::SolidLine);
49  //ensure handlers are always above this item
50  connect(this, &QetShapeItem::zChanged, [this]()
51  {
53  qghi->setZValue(this->zValue()+1);
54  });
55 
56  m_insert_point = new QAction(tr("Ajouter un point"), this);
58  connect(m_insert_point, &QAction::triggered, this, &QetShapeItem::insertPoint);
59  m_remove_point = new QAction(tr("Supprimer ce point"), this);
61  connect(m_remove_point, &QAction::triggered, this, &QetShapeItem::removePoint);
62 
63 }
64 
66 {
67  if(!m_handler_vector.isEmpty())
68  qDeleteAll(m_handler_vector);
69 }
70 
76 void QetShapeItem::setPen(const QPen &pen)
77 {
78  if (m_pen == pen) return;
79  m_pen = pen;
80  update();
81  emit penChanged();
82 }
83 
89 void QetShapeItem::setBrush(const QBrush &brush)
90 {
91  if (m_brush == brush) return;
92  m_brush = brush;
93  update();
94  emit brushChanged();
95 }
96 
104 void QetShapeItem::setP2(const QPointF &P2)
105 {
106  if (m_shapeType == Polygon && m_polygon.last() != P2)
107  {
108  prepareGeometryChange();
109  m_polygon.replace(m_polygon.size()-1, P2);
110  }
111  else if (P2 != m_P2)
112  {
113  prepareGeometryChange();
114  m_P2 = P2;
115  }
116 }
117 
124 bool QetShapeItem::setLine(const QLineF &line)
125 {
126  if (Q_UNLIKELY(m_shapeType != Line)) return false;
127  prepareGeometryChange();
128  m_P1 = line.p1();
129  m_P2 = line.p2();
131  return true;
132 }
133 
140 bool QetShapeItem::setRect(const QRectF &rect)
141 {
142  if (Q_LIKELY(m_shapeType == Rectangle || m_shapeType == Ellipse))
143  {
144  prepareGeometryChange();
145  m_P1 = rect.topLeft();
146  m_P2 = rect.bottomRight();
148  return true;
149  }
150 
151  return false;
152 }
153 
160 bool QetShapeItem::setPolygon(const QPolygonF &polygon)
161 {
162  if (Q_UNLIKELY(m_shapeType != Polygon)) {
163  return false;
164  }
165  prepareGeometryChange();
166  m_polygon = polygon;
168  return true;
169 }
170 
176 void QetShapeItem::setClosed(bool close)
177 {
178  if (m_shapeType == Polygon && close != m_closed)
179  {
180  prepareGeometryChange();
181  m_closed = close;
182  emit closeChanged();
183  }
184 }
185 
187 {
188  m_xRadius = X;
189  update();
191  emit XRadiusChanged();
192 }
193 
195 {
196  m_yRadius = Y;
197  update();
199  emit YRadiusChanged();
200 }
201 
207  return m_polygon.size();
208 }
209 
216 {
217  prepareGeometryChange();
218  m_polygon.append(Diagram::snapToGrid(P));
219 }
220 
228 {
229  if (pointsCount() == 2 || number < 1) return;
230  if ((pointsCount()-2) < number)
231  number = pointsCount() - 2;
232 
233  int i = 0;
234  do
235  {
236  i++;
237  prepareGeometryChange();
238  m_polygon.pop_back();
239  setTransformOriginPoint(boundingRect().center());
240 
241  } while (i < number);
242 }
243 
249  return shape().boundingRect().adjusted(-6, -6, 6, 6);
250 }
251 
256 QPainterPath QetShapeItem::shape() const
257 {
258  QPainterPath path;
259 
260  switch (m_shapeType)
261  {
262  case Line:
263  path.moveTo(m_P1);
264  path.lineTo(m_P2);
265  break;
266  case Rectangle:
267  path.addRoundedRect(QRectF(m_P1, m_P2), m_xRadius, m_yRadius);
268  break;
269  case Ellipse:
270  path.addEllipse(QRectF(m_P1, m_P2));
271  break;
272  case Polygon:
273  path.addPolygon(m_polygon);
274  if (m_closed) {
275  path.closeSubpath();
276  }
277  break;
278  default:
279  Q_ASSERT(false);
280  break;
281  }
282 
283  QPainterPathStroker pps;
284  pps.setWidth(m_hovered? m_pen.widthF()+10 : m_pen.widthF());
285  pps.setJoinStyle(Qt::RoundJoin);
286  path = pps.createStroke(path);
287 
288  return (path);
289 }
290 
298 void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
299 {
300  Q_UNUSED(option); Q_UNUSED(widget);
301 
302  painter->save();
303  painter->setRenderHint(QPainter::Antialiasing, true);
304  painter->setPen(m_pen);
305  painter->setBrush(m_brush);
306 
307  //Draw hovered shadow
308  if (m_hovered)
309  {
310  painter->save();
311  QColor color(Qt::darkBlue);
312  color.setAlpha(50);
313  painter -> setBrush (QBrush (color));
314  painter -> setPen (Qt::NoPen);
315  painter -> drawPath (shape());
316  painter -> restore ();
317  }
318 
319  switch (m_shapeType)
320  {
321  case Line: painter->drawLine(QLineF(m_P1, m_P2)); break;
322  case Rectangle: painter->drawRoundedRect(QRectF(m_P1, m_P2), m_xRadius, m_yRadius); break;
323  case Ellipse: painter->drawEllipse(QRectF(m_P1, m_P2)); break;
324  case Polygon: m_closed ? painter->drawPolygon(m_polygon) : painter->drawPolyline(m_polygon); break;
325  }
326 
327  painter->restore();
328 }
329 
335 void QetShapeItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
336 {
337  m_hovered = true;
338  QetGraphicsItem::hoverEnterEvent(event);
339 }
340 
346 void QetShapeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
347 {
348  m_hovered = false;
349  QetGraphicsItem::hoverLeaveEvent(event);
350 }
351 
352 void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
353 {
354  event->ignore();
356 
357  if (event->button() == Qt::LeftButton) {
359  event->accept();
360  }
361 }
362 
369 QVariant QetShapeItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
370 {
371  if (change == ItemSelectedHasChanged)
372  {
373  if (value.toBool() == true) { //If this is selected, wa add handlers.
374  addHandler();
375  }
376  else //Else this is deselected, we remove handlers
377  {
378  if(!m_handler_vector.isEmpty())
379  {
380  qDeleteAll(m_handler_vector);
381  m_handler_vector.clear();
382  }
383  m_resize_mode = 1;
384  }
385  }
386  else if (change == ItemPositionHasChanged) {
388  }
389  else if (change == ItemSceneHasChanged)
390  {
391  if (!scene()) //This is removed from scene, then we deselect this, and so, the handlers is also removed.
392  {
393  setSelected(false);
394  }
395  }
396 
397  return QGraphicsItem::itemChange(change, value);
398 }
399 
406 bool QetShapeItem::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
407 {
408  //Watched must be an handler
409  if(watched->type() == QetGraphicsHandlerItem::Type)
410  {
411  QetGraphicsHandlerItem *qghi = qgraphicsitem_cast<QetGraphicsHandlerItem *>(watched);
412 
413  if(m_handler_vector.contains(qghi)) //Handler must be in m_vector_index, then we can start resize
414  {
415  m_vector_index = m_handler_vector.indexOf(qghi);
416  if (m_vector_index != -1)
417  {
418  if(event->type() == QEvent::GraphicsSceneMousePress) //Click
419  {
420  handlerMousePressEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
421  return true;
422  }
423  else if(event->type() == QEvent::GraphicsSceneMouseMove) //Move
424  {
425  handlerMouseMoveEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
426  return true;
427  }
428  else if (event->type() == QEvent::GraphicsSceneMouseRelease) //Release
429  {
430  handlerMouseReleaseEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
431  return true;
432  }
433  }
434  }
435  }
436 
437  return false;
438 }
439 
444 void QetShapeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
445 {
446  m_context_menu_pos = event->pos();
447 
449  {
450  if (diagram()->selectedItems().isEmpty()) {
451  this->setSelected(true);
452  }
453 
454  if (isSelected() && scene()->selectedItems().size() == 1)
455  {
456  if (diagram())
457  {
458  DiagramView *d_view = nullptr;
459  for (QGraphicsView *view : diagram()->views())
460  {
461  if (view->isActiveWindow())
462  {
463  d_view = dynamic_cast<DiagramView *>(view);
464  if (d_view)
465  continue;
466  }
467  }
468 
469  if (d_view)
470  {
471  QScopedPointer<QMenu> menu(new QMenu());
472  menu.data()->addAction(m_insert_point);
473 
474  if (m_handler_vector.count() > 2)
475  {
477  {
478  if (qghi->contains(qghi->mapFromScene(event->scenePos())))
479  {
480  menu.data()->addAction(m_remove_point);
481  break;
482  }
483  }
484  }
485 
486  menu.data()->addSeparator();
487  menu.data()->addActions(d_view->contextMenuActions());
488  menu.data()->exec(event->screenPos());
489  event->accept();
490  return;
491  }
492  }
493  }
494  }
495 
496  QetGraphicsItem::contextMenuEvent(event);
497 }
498 
503 {
504  if (m_shapeType == Ellipse)
505  {
506  if (m_resize_mode == 1)
507  {
508  m_resize_mode = 2;
510  qghi->setColor(Qt::darkGreen);
511  }
512  }
513  else
514  {
515  m_resize_mode = 1;
517  qghi->setColor(Qt::blue);
518  }
519  }
520  }
521  else if (m_shapeType == Rectangle)
522  {
523  if (m_resize_mode == 1)
524  {
525  m_resize_mode = 2;
527  qghi->setColor(Qt::darkGreen);
528  }
529  else if (m_resize_mode == 2)
530  {
531  m_resize_mode = 3;
532  qDeleteAll(m_handler_vector);
533  m_handler_vector.clear();
534  addHandler();
536  qghi->setColor(Qt::magenta);
537  }
538  }
539  else if (m_resize_mode == 3)
540  {
541  m_resize_mode = 1;
542  qDeleteAll(m_handler_vector);
543  m_handler_vector.clear();
544  addHandler();
546  qghi->setColor(Qt::blue);
547  }
548 
549  }
550  }
551 }
552 
554 {
555  if (m_handler_vector.isEmpty())
556  {
557  QVector <QPointF> points_vector;
558  switch (m_shapeType)
559  {
560  case Line:
561  points_vector << m_P1 << m_P2;
562  break;
563  case Rectangle:
564  if (m_resize_mode == 3) {
566  }
567  else {
568  points_vector = QetGraphicsHandlerUtility::pointsForRect(QRectF(m_P1, m_P2));
569  }
570  break;
571  case Ellipse:
572  points_vector = QetGraphicsHandlerUtility::pointsForRect(QRectF(m_P1, m_P2));
573  break;
574  case Polygon:
575  points_vector = m_polygon;
576  break;
577  }
578 
579  if(!points_vector.isEmpty() && scene())
580  {
581  m_handler_vector = QetGraphicsHandlerItem::handlerForPoint(mapToScene(points_vector));
582 
584  {
585  handler->setZValue(this->zValue()+1);
586  handler->setColor(Qt::blue);
587  scene()->addItem(handler);
588  handler->installSceneEventFilter(this);
589  }
590  }
591  }
592 }
593 
599 {
600  if (m_handler_vector.isEmpty()) {
601  return;
602  }
603 
604  QVector <QPointF> points_vector;
605  switch (m_shapeType)
606  {
607  case Line: {
608  points_vector << m_P1 << m_P2;
609  break;
610  }
611  case Rectangle: {
612  if (m_resize_mode != 3) {
613  points_vector = QetGraphicsHandlerUtility::pointsForRect(QRectF(m_P1, m_P2));
614  }
615  else {
617  }
618  break;
619  }
620  case Ellipse: {
621  points_vector = QetGraphicsHandlerUtility::pointsForRect(QRectF(m_P1, m_P2));
622  break;
623  }
624  case Polygon: {
625  points_vector = m_polygon;
626  break;
627  }
628  }
629 
630  if (m_handler_vector.size() == points_vector.size())
631  {
632  points_vector = mapToScene(points_vector);
633  for (int i = 0 ; i < points_vector.size() ; ++i)
634  m_handler_vector.at(i)->setPos(points_vector.at(i));
635  }
636  else
637  {
638  qDeleteAll(m_handler_vector);
639  m_handler_vector.clear();
640  addHandler();
641  }
642 }
643 
645 {
647  {
649 
650  if(new_polygon != m_polygon)
651  {
652  //Wrap the undo for avoid to merge the undo commands when user add several points.
653  QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone"));
654  new QPropertyUndoCommand(this, "polygon", m_polygon, new_polygon, undo);
655  diagram()->undoStack().push(undo);
656  }
657  }
658 }
659 
661 {
663  return;
664  }
665 
666  if (m_handler_vector.size() == 2) {
667  return;
668  }
669 
670  QPointF point = mapToScene(m_context_menu_pos);
671  int index = -1;
672  for (int i=0 ; i<m_handler_vector.size() ; i++)
673  {
675  if (qghi->contains(qghi->mapFromScene(point)))
676  {
677  index = i;
678  break;
679  }
680  }
681  if (index > -1 && index<m_handler_vector.count())
682  {
683  QPolygonF polygon = this->polygon();
684  polygon.removeAt(index);
685 
686  //Wrap the undo for avoid to merge the undo commands when user add several points.
687  QUndoCommand *undo = new QUndoCommand(tr("Supprimer un point d'un polygone"));
688  new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
689  diagram()->undoStack().push(undo);
690  }
691 }
692 
698 void QetShapeItem::handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
699 {
700  Q_UNUSED(qghi);
701  Q_UNUSED(event);
702 
703  m_old_P1 = m_P1;
704  m_old_P2 = m_P2;
708  if(m_xRadius == 0 && m_yRadius == 0) {
710  }
711 }
712 
718 void QetShapeItem::handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
719 {
720  Q_UNUSED(qghi);
721 
722  QPointF new_pos = event->scenePos();
723  if (event->modifiers() != Qt::ControlModifier)
724  new_pos = Diagram::snapToGrid(event->scenePos());
725  new_pos = mapFromScene(new_pos);
726 
727  switch (m_shapeType)
728  {
729  case Line:
730  prepareGeometryChange();
731  m_vector_index == 0 ? m_P1 = new_pos : m_P2 = new_pos;
733  break;
734 
735  case Rectangle:
736  if (m_resize_mode == 1) {
738  break;
739  }
740  else if (m_resize_mode == 2) {
742  break;
743  }
744  else {
745  qreal radius = QetGraphicsHandlerUtility::radiusForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index);
747  setXRadius(radius);
748  setYRadius(radius);
749  }
750  else if(m_vector_index == 0) {
751  setXRadius(radius);
752  }
753  else {
754  setYRadius(radius);
755  }
757  break;
758  }
759  case Ellipse:
760  if (m_resize_mode == 1) {
762  break;
763  }
764  else {
766  break;
767  }
768 
769  case Polygon:
770  prepareGeometryChange();
771  m_polygon.replace(m_vector_index, new_pos);
773  break;
774  } //End switch
775 }
776 
782 void QetShapeItem::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
783 {
784  Q_UNUSED(qghi);
785  Q_UNUSED(event);
786 
787  m_modifie_radius_equaly = false;
788 
789  if (diagram())
790  {
791  QPropertyUndoCommand *undo = nullptr;
792  if ((m_shapeType & (Line | Rectangle | Ellipse)) && ((m_P1 != m_old_P1 || m_P2 != m_old_P2) ||
794  )
795  {
796  switch(m_shapeType)
797  {
798  case Line: {
799  undo = new QPropertyUndoCommand(this, "line",QLineF(m_old_P1, m_old_P2), QLineF(m_P1, m_P2));
800  break;
801  }
802  case Rectangle: {
803  if (m_resize_mode == 1 || m_resize_mode == 2) {
804  undo = new QPropertyUndoCommand(this, "rect",QRectF(m_old_P1, m_old_P2), QRectF(m_P1, m_P2).normalized());
805  }
806  else if (m_resize_mode == 3)
807  {
808  undo = new QPropertyUndoCommand(this, "xRadius", m_old_xRadius, m_xRadius);
809  QPropertyUndoCommand *undo_ = new QPropertyUndoCommand(this, "yRadius", m_old_yRadius, m_yRadius, undo);
810  undo_->setAnimated();
811  }
812  break;
813  }
814  case Ellipse: {
815  undo = new QPropertyUndoCommand(this, "rect",QRectF(m_old_P1, m_old_P2), QRectF(m_P1, m_P2).normalized());
816  break;
817  }
818  case Polygon: break;
819  }
820  if (undo) {
821  undo->setAnimated(true, false);
822  }
823  }
824  else if (m_shapeType == Polygon && (m_polygon != m_old_polygon))
825  undo = new QPropertyUndoCommand(this, "polygon", m_old_polygon, m_polygon);
826 
827  if(undo)
828  {
829  undo->setText(tr("Modifier %1").arg(name()));
830  diagram()->undoStack().push(undo);
831  }
832  }
833 }
834 
841 bool QetShapeItem::fromXml(const QDomElement &e)
842 {
843  if (e.tagName() != "shape") return (false);
844 
845  is_movable_ = (e.attribute("is_movable").toInt());
846  m_closed = e.attribute("closed", "0").toInt();
847  m_pen = QETXML::penFromXml(e.firstChildElement("pen"));
848  m_brush = QETXML::brushFromXml(e.firstChildElement("brush"));
849 
850  QString type = e.attribute("type");
851  //@TODO Compatibility for version older than N°4075, shape type was stored with an int
852  if (type.size() == 1)
853  {
854  switch(e.attribute("type","0").toInt())
855  {
856  case 0: m_shapeType = Line; break;
857  case 1: m_shapeType = Rectangle; break;
858  case 2: m_shapeType = Ellipse; break;
859  case 3: m_shapeType = Polygon; break;
860  }
861  }
862  //For version after N°4075, shape is stored with a string
863  else
864  {
865  QMetaEnum me = metaObject()->enumerator(metaObject()->indexOfEnumerator("ShapeType"));
866  m_shapeType = QetShapeItem::ShapeType(me.keysToValue(type.toStdString().data()));
867  }
868 
869  if (m_shapeType != Polygon)
870  {
871  m_P1.setX(e.attribute("x1", nullptr).toDouble());
872  m_P1.setY(e.attribute("y1", nullptr).toDouble());
873  m_P2.setX(e.attribute("x2", nullptr).toDouble());
874  m_P2.setY(e.attribute("y2", nullptr).toDouble());
875 
876  if (m_shapeType == Rectangle)
877  {
878  setXRadius(e.attribute("rx", "0").toDouble());
879  setYRadius(e.attribute("ry", "0").toDouble());
880  }
881  }
882  else {
883  for(const QDomElement& de : QET::findInDomElement(e, "points", "point")) {
884  m_polygon << QPointF(de.attribute("x", nullptr).toDouble(), de.attribute("y", nullptr).toDouble());
885  }
886  }
887  setZValue(e.attribute("z", QString::number(this->zValue())).toDouble());
888 
889  return (true);
890 }
891 
898 QDomElement QetShapeItem::toXml(QDomDocument &document) const
899 {
900  QDomElement result = document.createElement("shape");
901 
902  //write some attribute
903  QMetaEnum me = metaObject()->enumerator(metaObject()->indexOfEnumerator("ShapeType"));
904  result.setAttribute("type", me.valueToKey(m_shapeType));
905  result.appendChild(QETXML::penToXml(document, m_pen));
906  result.appendChild(QETXML::brushToXml(document, m_brush));
907  result.setAttribute("is_movable", bool(is_movable_));
908  result.setAttribute("closed", bool(m_closed));
909 
910  if (m_shapeType != Polygon)
911  {
912  result.setAttribute("x1", QString::number(mapToScene(m_P1).x()));
913  result.setAttribute("y1", QString::number(mapToScene(m_P1).y()));
914  result.setAttribute("x2", QString::number(mapToScene(m_P2).x()));
915  result.setAttribute("y2", QString::number(mapToScene(m_P2).y()));
916 
917  if (m_shapeType == Rectangle)
918  {
919  QRectF rect(m_P1, m_P2);
920  rect = rect.normalized();
921  qreal x = m_xRadius;
922  if (x > rect.width()/2) {
923  x = rect.width()/2;
924  }
925  qreal y = m_yRadius;
926  if (y > rect.height()/2) {
927  y = rect.height()/2;
928  }
929 
930  result.setAttribute("rx", QString::number(m_xRadius));
931  result.setAttribute("ry", QString::number(m_yRadius));
932  }
933  }
934  else
935  {
936  QDomElement points = document.createElement("points");
937  for (QPointF p : m_polygon)
938  {
939  QDomElement point = document.createElement("point");
940  QPointF pf = mapToScene(p);
941  point.setAttribute("x", QString::number(pf.x()));
942  point.setAttribute("y", QString::number(pf.y()));
943  points.appendChild(point);
944  }
945  result.appendChild(points);
946  }
947  result.setAttribute("z", QString::number(this->zValue()));
948 
949  return(result);
950 }
951 
958 bool QetShapeItem::toDXF(const QString &filepath,const QPen &pen)
959 {
960 
961  switch (m_shapeType)
962  {
963  case Line: Createdxf::drawLine (filepath, QLineF(mapToScene(m_P1), mapToScene(m_P2)), Createdxf::getcolorCode(pen.color().red(),pen.color().green(),pen.color().blue())); return true;
964  case Rectangle: Createdxf::drawRectangle(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), Createdxf::getcolorCode(pen.color().red(),pen.color().green(),pen.color().blue())); return true;
965  case Ellipse: Createdxf::drawEllipse (filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), Createdxf::getcolorCode(pen.color().red(),pen.color().green(),pen.color().blue())); return true;
966  default: return false;
967  }
968 }
969 
975 {
976  if (diagram() -> isReadOnly()) return;
977 
978  PropertiesEditorDialog ped(new ShapeGraphicsItemPropertiesWidget(this), diagram()->views().at(0));
979  ped.exec();
980 }
981 
986 QString QetShapeItem::name() const {
987  switch (m_shapeType) {
988  case Line: return tr("une ligne");
989  case Rectangle: return tr("un rectangle");
990  case Ellipse: return tr("une éllipse");
991  case Polygon: return tr("une polyligne");
992  default: return tr("une shape");
993  }
994 }
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override
QetShapeItem::sceneEventFilter.
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override
QetShapeItem::contextMenuEvent.
bool setPolygon(const QPolygonF &polygon)
QetShapeItem::setPolygon Set this item geometry to polygon (only available if shape is a polyline) ...
QLineF line() const
Definition: qetshapeitem.h:86
QPen pen() const
METHODS.
Definition: qetshapeitem.h:72
QPointF m_P2
Definition: qetshapeitem.h:132
QVector< QetGraphicsHandlerItem * > m_handler_vector
Definition: qetshapeitem.h:143
QRectF boundingRect() const override
QetShapeItem::boundingRect.
virtual bool fromXml(const QDomElement &)
QetShapeItem::fromXml Build this item from the xml description.
void removePoints(int number=1)
QetShapeItem::removePoints Number of point to remove on the polygon If is superior to number of poly...
QBrush m_brush
Definition: qetshapeitem.h:131
static int getcolorCode(const long red, const long green, const long blue)
Createdxf::getcolorCode This function returns the ACI color which is the "nearest" color to the color...
Definition: createdxf.cpp:389
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
QetShapeItem::itemChange.
The QetGraphicsHandlerItem class This graphics item represents a point, destined to be used as an han...
QPolygonF polygon() const
Definition: qetshapeitem.h:90
static void drawRectangle(const QString &filepath, double, double, double, double, const int &colorcode)
Definition: createdxf.cpp:565
Diagram * diagram() const
QetGraphicsItem::diagram return the diagram of this item.
void handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMousePressEvent.
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
QetShapeItem::hoverLeaveEvent Handle hover leave event.
void insertPoint()
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
QetGraphicsItem::mousePressEvent handle the mouse click.
qreal m_old_yRadius
Definition: qetshapeitem.h:147
QPointF m_P1
Definition: qetshapeitem.h:132
void setYRadius(qreal Y)
void adjusteHandlerPos()
QetShapeItem::adjusteHandlerPos Adjust the position of the handler item.
static QVector< QPointF > pointForRadiusRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
QetGraphicsHandlerUtility::pointForRadiusRect.
static qreal radiusForPosAtIndex(const QRectF &rect, const QPointF &pos, int index, Qt::SizeMode mode=Qt::AbsoluteSize)
QetGraphicsHandlerUtility::radiusForPosAtIndex.
void XRadiusChanged()
QPointF m_old_P2
Definition: qetshapeitem.h:132
QDomElement penToXml(QDomDocument &parent_document, const QPen &pen)
QETXML::penToXml Write attribute of a QPen in xml element.
Definition: qetxml.cpp:30
QetShapeItem(QPointF, QPointF=QPointF(0, 0), ShapeType=Line, QGraphicsItem *parent=nullptr)
QetShapeItem::QetShapeItem Constructor of shape item. point 1 and 2 must be in scene coordinate...
void brushChanged()
The ShapeGraphicsItemPropertiesWidget class Provide a widget to edit the properties of a QetShapeItem...
void handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMouseMoveEvent.
void YRadiusChanged()
The PropertiesEditorDialog class Create a dialog to edit some properties of a thing. Only create a instance of this class and call exec, all is done for you in this class. The first argument (a template) must be a subclass of QWidget and provide the 3 methods bellow : QString::title() void::apply() void::reset() You can subclass the interface PropertiesEditorWidget who provide all this methods. This dialog take ownership of the editor, so the editor will be deleted by this dialog.
void setP2(const QPointF &P2)
QetShapeItem::setP2 Set the second point of this item. If this item is a polyline, the last point of the polyline is replaced by P2.
QAction * m_insert_point
Definition: qetshapeitem.h:144
QPainterPath shape() const override
QetShapeItem::shape.
QAction * m_remove_point
Definition: qetshapeitem.h:144
int type() const override
Definition: qetshapeitem.h:69
static QPolygonF polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos)
QetGraphicsHandlerUtility::polygonForInsertPoint.
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
static QRectF mirrorRectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
QetGraphicsHandlerUtility::mirrorRectForPosAtIndex Return a rectangle after modification of the point...
QDomElement brushToXml(QDomDocument &parent_document, const QBrush &brush)
QETXML::brushToXml Write attribute of a QBrush in xml element.
Definition: qetxml.cpp:91
virtual QDomElement toXml(QDomDocument &document) const
QetShapeItem::toXml Save this item to xml element.
void closeChanged()
static QRectF rectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
QetGraphicsHandlerUtility::rectForPosAtIndex Return a rectangle after modification of the point &#39;&#39; at...
void removePoint()
QPointF m_old_P1
Definition: qetshapeitem.h:132
QList< QDomElement > findInDomElement(const QDomElement &, const QString &)
Definition: qet.cpp:300
QIcon tr
Definition: qeticons.cpp:204
QPen penFromXml(const QDomElement &element)
QETXML::penFromXml Build a QPen from a xml description.
Definition: qetxml.cpp:59
QRectF rect() const
Definition: qetshapeitem.h:88
int pointsCount() const
QetShapeItem::pointCount.
ShapeType m_shapeType
ATTRIBUTES.
Definition: qetshapeitem.h:129
QPolygonF m_old_polygon
Definition: qetshapeitem.h:137
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
QetShapeItem::paint Paint this item.
void editProperty() override
QetShapeItem::editProperty Edit the property of this item.
void setClosed(bool close)
QetShapeItem::setClosed Close this item, have effect only if this item is a polygon.
bool setRect(const QRectF &rect)
QetShapeItem::setRect Set this item geometry to rect (only available if shape is a rectangle or an el...
QBrush brush() const
Definition: qetshapeitem.h:74
qreal XRadius() const
Definition: qetshapeitem.h:94
QIcon Remove
Definition: qeticons.cpp:155
bool m_modifie_radius_equaly
Definition: qetshapeitem.h:141
~QetShapeItem() override
QIcon Add
Definition: qeticons.cpp:24
QList< QAction * > contextMenuActions() const
DiagramView::contextMenuActions.
void setPen(const QPen &pen)
QetShapeItem::setPen Set the pen to use for draw the shape.
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
QetShapeItem::hoverEnterEvent Handle hover enter event.
static QPointF snapToGrid(const QPointF &p)
Diagram::snapToGrid Return a nearest snap point of p.
Definition: diagram.cpp:1653
static QVector< QPointF > pointsForRect(const QRectF &rect)
QetGraphicsHandlerUtility::pointsForRect Return the keys points of the rectangle, stored in a vector...
QUndoStack & undoStack()
Definition: diagram.h:337
static void drawEllipse(const QString &filepath, const QRectF &rect, const int &colorcode)
Createdxf::drawEllipse Conveniance function for draw ellipse.
Definition: createdxf.cpp:556
QPolygonF m_polygon
Definition: qetshapeitem.h:137
void handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMouseReleaseEvent.
QPointF m_context_menu_pos
Definition: qetshapeitem.h:132
bool setLine(const QLineF &line)
QetShapeItem::setLine Set item geometry to line (only available for line shape)
static QVector< QetGraphicsHandlerItem * > handlerForPoint(const QVector< QPointF > &points, int size=10)
QetGraphicsHandlerItem::handlerForPoint.
void setBrush(const QBrush &brush)
QetShapeItem::setBrush Set the brush to use for the fill the shape.
void setAnimated(bool animate=true, bool first_time=true)
QPropertyUndoCommand::setAnimated.
void penChanged()
void setXRadius(qreal X)
QString name() const override
QetShapeItem::name.
static void drawLine(const QString &filapath, double, double, double, double, const int &clorcode)
Definition: createdxf.cpp:290
QBrush brushFromXml(const QDomElement &element)
QETXML::brushFromXml Build a QBrush from a xml description.
Definition: qetxml.cpp:128
void setNextPoint(QPointF P)
QetShapeItem::setNextPoint Add a new point to the curent polygon.
virtual bool toDXF(const QString &filepath, const QPen &pen)
QetShapeItem::toDXF Draw this element to the dxf document.
QIcon de
Definition: qeticons.cpp:188
qreal m_old_xRadius
Definition: qetshapeitem.h:147
void switchResizeMode()
QetShapeItem::switchResizeMode.