//! [11]
void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (line != 0 && myMode == InsertLine) {
        QList<QGraphicsItem *> startItems = items(line->line().p1());
        if (startItems.count() && startItems.first() == line)
            startItems.removeFirst();
        QList<QGraphicsItem *> endItems = items(line->line().p2());
        if (endItems.count() && endItems.first() == line)
            endItems.removeFirst();

        removeItem(line);
        delete line;
//! [11] //! [12]

        if (startItems.count() > 0 && endItems.count() > 0 &&
            startItems.first()->type() == DiagramItem::Type &&
            endItems.first()->type() == DiagramItem::Type &&
            startItems.first() != endItems.first()) {
            DiagramItem *startItem =
                qgraphicsitem_cast<DiagramItem *>(startItems.first());
            DiagramItem *endItem =
                qgraphicsitem_cast<DiagramItem *>(endItems.first());
            Arrow *arrow = new Arrow(startItem, endItem);
            arrow->setColor(myLineColor);
            startItem->addArrow(arrow);
            endItem->addArrow(arrow);
            arrow->setZValue(-1000.0);
            addItem(arrow);
            arrow->updatePosition();
        }
    }
//! [12] //! [13]
    line = 0;
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
示例#2
0
void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (line != 0 && myMode == InsertLine) {
        QList<QGraphicsItem *> startItems = items(line->line().p1());
        while(startItems.count() && startItems.first()->type() != DiagramItem::Type) {
            startItems.removeFirst();
        }
        QList<QGraphicsItem *> endItems = items(line->line().p2());
        while(endItems.count() && endItems.first()->type() != DiagramItem::Type) {
            endItems.removeFirst();
        }
        removeItem(line);
        delete line;

        if (startItems.count() > 0 && endItems.count() > 0)
            if(startItems.first()->type() == DiagramItem::Type &&
                    endItems.first()->type() == DiagramItem::Type &&
                    startItems.first() != endItems.first()) {
                DiagramItem *startItem =
                    qgraphicsitem_cast<DiagramItem *>(startItems.first());
                DiagramItem *endItem =
                    qgraphicsitem_cast<DiagramItem *>(endItems.first());
                if(startItem->diagramType()!=endItem->diagramType()) {
                    Arrow *arrow = new Arrow(startItem, endItem, myItemMenu);
                    if(!startItem->haveSuchArrow(arrow)) {
                        arrow->setColor(myLineColor);
                        startItem->addArrow(arrow);
                        endItem->addArrow(arrow);
                        arrow->setZValue(-1000);
                        addItem(arrow);
                        arrow->updatePosition();
                    }
                }
            }
    }
    line = 0;
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
}