void DrawBox::mousePressEvent(QGraphicsSceneMouseEvent *me){ if (me->button() != Qt::LeftButton) return; switch(myMode) { case free: QGraphicsScene::mousePressEvent(me); break; case fPoint: if(myType == ellipse) { QGraphicsEllipseItem *ellip; ellip = new QGraphicsEllipseItem; ellip->setRect(QRectF(me->scenePos(), me->scenePos())); ellip->setPen(QPen(Qt::red, 5, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin)); addItem(ellip); myMode = sPoint; } else if(myType == rectangle) { QGraphicsRectItem *rectn; rectn = new QGraphicsRectItem; rectn->setRect(QRectF(me->scenePos(), me->scenePos())); rectn->setPen(QPen(Qt::red, 5, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin)); addItem(rectn); myMode = sPoint; } else if(myType == line) { QGraphicsLineItem *line; line = new QGraphicsLineItem; line->setLine(QLineF(me->scenePos(), me->scenePos())); line->setPen(QPen(Qt::red, 5, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin)); addItem(line); myMode = sPoint; } break; case sPoint: break; }} void DrawBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *me){ if (me->button() != Qt::LeftButton) return; switch(myMode) { case free: QGraphicsScene::mouseReleaseEvent(me); break; case fPoint: myMode = free; break; case sPoint: if(myType == ellipse) { QGraphicsEllipseItem *ellip; ellip = static_cast<QGraphicsEllipseItem *>(items().first()); ellip->setPen(pen); ellip->setBrush(brush); ellip->setRect(QRectF(ellip->rect().topLeft(), me->scenePos())); myMode = free; } else if(myType == rectangle) { QGraphicsRectItem *rectn; rectn = static_cast<QGraphicsRectItem *>(items().first()); rectn->setPen(pen); rectn->setBrush(brush); rectn->setRect(QRectF(rectn->rect().topLeft(), me->scenePos())); myMode = free; } else if(myType == line) { QGraphicsLineItem *line; line = static_cast<QGraphicsLineItem *>(items().first()); line->setPen(pen); line->setLine(QLineF(line->line().p1(), me->scenePos())); myMode = free; } break; }} void DrawBox::mouseMoveEvent(QGraphicsSceneMouseEvent *me){ switch(myMode) { case free: QGraphicsScene::mouseMoveEvent(me); break; case fPoint: break; case sPoint: if(myType == ellipse) { QGraphicsEllipseItem *ellip = static_cast<QGraphicsEllipseItem *>(items().first()); ellip->setRect(QRectF(ellip->rect().topLeft(), me->scenePos())); } else if(myType == rectangle) { QGraphicsRectItem *rectn = static_cast<QGraphicsRectItem *>(items().first()); rectn->setRect(QRectF(rectn->rect().topLeft(), me->scenePos())); } else if(myType == line) { QGraphicsLineItem *line = static_cast<QGraphicsLineItem *>(items().first()); line->setLine(QLineF(line->line().p1(), me->scenePos())); } break; }}