what's the better practice?
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:
Code:
#ifndef DIAGRAMITEM_H
#define DIAGRAMITEM_H
#include <QGraphicsPixmapItem>
#include <QList>
class Arrow;
{
public:
enum { Type = UserType + 15 };
enum DiagramType { Step, Conditional, StartEnd, Io };
DiagramItem
(DiagramType diagramType,
QMenu *contextMenu,
void removeArrow(Arrow *arrow);
void removeArrows();
DiagramType diagramType() const
{ return myDiagramType; }
{ return myPolygon; }
void addArrow(Arrow *arrow);
int type() const
{ return Type;}
protected:
private:
DiagramType myDiagramType;
QList<Arrow *> arrows;
};
#endif
Code:
#include <QtGui>
#include "diagramitem.h"
#include "arrow.h"
DiagramItem
::DiagramItem(DiagramType diagramType,
QMenu *contextMenu,
{
myDiagramType = diagramType;
myContextMenu = contextMenu;
switch (myDiagramType) {
case StartEnd:
path.moveTo(200, 50);
path.arcTo(150, 0, 50, 50, 0, 90);
path.arcTo(50, 0, 50, 50, 90, 90);
path.arcTo(50, 50, 50, 50, 180, 90);
path.arcTo(150, 50, 50, 50, 270, 90);
path.lineTo(200, 25);
myPolygon = path.toFillPolygon();
break;
case Conditional:
break;
case Step:
break;
default:
break;
}
setPolygon(myPolygon);
}
void DiagramItem::removeArrow(Arrow *arrow)
{
int index = arrows.indexOf(arrow);
if (index != -1)
arrows.removeAt(index);
}
void DiagramItem::removeArrows()
{
foreach (Arrow *arrow, arrows) {
arrow->startItem()->removeArrow(arrow);
arrow->endItem()->removeArrow(arrow);
scene()->removeItem(arrow);
delete arrow;
}
}
void DiagramItem::addArrow(Arrow *arrow)
{
arrows.append(arrow);
}
{
pixmap.fill(Qt::transparent);
painter.
setPen(QPen(Qt
::black,
8));
painter.translate(125, 125);
painter.drawPolyline(myPolygon);
return pixmap;
}
{
scene()->clearSelection();
setSelected(true);
myContextMenu->exec(event->screenPos());
}
QVariant DiagramItem
::itemChange(GraphicsItemChange change,
{
foreach (Arrow *arrow, arrows) {
arrow->updatePosition();
}
}
return value;
}
Re: what's the better practice?
In general it depends. In most cases it'd be better to make each type a separate item class inheriting a common base.