I'm working on an example in the QT Documentation,Diagram Scene Example.

In the example, the DiagramItem class is designed for the items to be drawn in to the scene. Different kinds of items(Conditional,Process,Input/Output,Text) exists and these are enumerated in the DiagramItem class and are distinct with switch... But I don't know if it's the best way.Wouldn't it be better if the Conditional,Process,IO,Text items inherits from the DiagramItem class.
here's the code and i want to hear you:
Qt Code:
  1. #ifndef DIAGRAMITEM_H
  2. #define DIAGRAMITEM_H
  3.  
  4. #include <QGraphicsPixmapItem>
  5. #include <QList>
  6.  
  7. class QPixmap;
  8. class QTextEdit;
  9. class QMenu;
  10. class QPainter;
  11. class QWidget;
  12. class QPolygonF;
  13. class Arrow;
  14.  
  15. class DiagramItem : public QGraphicsPolygonItem
  16. {
  17. public:
  18. enum { Type = UserType + 15 };
  19. enum DiagramType { Step, Conditional, StartEnd, Io };
  20.  
  21. DiagramItem(DiagramType diagramType, QMenu *contextMenu,
  22. QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
  23.  
  24. void removeArrow(Arrow *arrow);
  25. void removeArrows();
  26. DiagramType diagramType() const
  27. { return myDiagramType; }
  28. QPolygonF polygon() const
  29. { return myPolygon; }
  30. void addArrow(Arrow *arrow);
  31. QPixmap image() const;
  32. int type() const
  33. { return Type;}
  34.  
  35. protected:
  36. void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
  37. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
  38.  
  39. private:
  40. DiagramType myDiagramType;
  41. QPolygonF myPolygon;
  42. QMenu *myContextMenu;
  43. QList<Arrow *> arrows;
  44. };
  45.  
  46. #endif
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include <QtGui>
  2.  
  3. #include "diagramitem.h"
  4. #include "arrow.h"
  5.  
  6. DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
  7. QGraphicsItem *parent, QGraphicsScene *scene)
  8. : QGraphicsPolygonItem(parent, scene)
  9. {
  10. myDiagramType = diagramType;
  11. myContextMenu = contextMenu;
  12.  
  13. switch (myDiagramType) {
  14. case StartEnd:
  15. path.moveTo(200, 50);
  16. path.arcTo(150, 0, 50, 50, 0, 90);
  17. path.arcTo(50, 0, 50, 50, 90, 90);
  18. path.arcTo(50, 50, 50, 50, 180, 90);
  19. path.arcTo(150, 50, 50, 50, 270, 90);
  20. path.lineTo(200, 25);
  21. myPolygon = path.toFillPolygon();
  22. break;
  23. case Conditional:
  24. myPolygon << QPointF(-100, 0) << QPointF(0, 100)
  25. << QPointF(100, 0) << QPointF(0, -100)
  26. << QPointF(-100, 0);
  27. break;
  28. case Step:
  29. myPolygon << QPointF(-100, -100) << QPointF(100, -100)
  30. << QPointF(100, 100) << QPointF(-100, 100)
  31. << QPointF(-100, -100);
  32. break;
  33. default:
  34. myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
  35. << QPointF(120, 80) << QPointF(70, -80)
  36. << QPointF(-120, -80);
  37. break;
  38. }
  39. setPolygon(myPolygon);
  40. setFlag(QGraphicsItem::ItemIsMovable, true);
  41. setFlag(QGraphicsItem::ItemIsSelectable, true);
  42. }
  43.  
  44. void DiagramItem::removeArrow(Arrow *arrow)
  45. {
  46. int index = arrows.indexOf(arrow);
  47.  
  48. if (index != -1)
  49. arrows.removeAt(index);
  50. }
  51.  
  52. void DiagramItem::removeArrows()
  53. {
  54. foreach (Arrow *arrow, arrows) {
  55. arrow->startItem()->removeArrow(arrow);
  56. arrow->endItem()->removeArrow(arrow);
  57. scene()->removeItem(arrow);
  58. delete arrow;
  59. }
  60. }
  61.  
  62. void DiagramItem::addArrow(Arrow *arrow)
  63. {
  64. arrows.append(arrow);
  65. }
  66.  
  67. QPixmap DiagramItem::image() const
  68. {
  69. QPixmap pixmap(250, 250);
  70. pixmap.fill(Qt::transparent);
  71. QPainter painter(&pixmap);
  72. painter.setPen(QPen(Qt::black, 8));
  73. painter.translate(125, 125);
  74. painter.drawPolyline(myPolygon);
  75.  
  76. return pixmap;
  77. }
  78.  
  79. void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  80. {
  81. scene()->clearSelection();
  82. setSelected(true);
  83. myContextMenu->exec(event->screenPos());
  84. }
  85.  
  86. QVariant DiagramItem::itemChange(GraphicsItemChange change,
  87. const QVariant &value)
  88. {
  89. if (change == QGraphicsItem::ItemPositionChange) {
  90. foreach (Arrow *arrow, arrows) {
  91. arrow->updatePosition();
  92. }
  93. }
  94.  
  95. return value;
  96. }
To copy to clipboard, switch view to plain text mode