Esempio n. 1
0
//! [3]
void DiagramScene::setItemColor(const QColor &color)
{
    myItemColor = color;
    if (isItemChange(DiagramItem::Type)) {
        DiagramItem *item = qgraphicsitem_cast<DiagramItem *>(selectedItems().first());
        item->setBrush(myItemColor);
    }
}
//! [6]
void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (mouseEvent->button() != Qt::LeftButton)
        return;

    DiagramItem *item;
    switch (myMode) {
        case InsertItem:
            item = new DiagramItem(myItemType, myItemMenu);
            item->setBrush(myItemColor);
            addItem(item);
            item->setPos(mouseEvent->scenePos());
            emit itemInserted(item);
            break;
//! [6] //! [7]
        case InsertLine:
            line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
                                        mouseEvent->scenePos()));
            line->setPen(QPen(myLineColor, 2));
            addItem(line);
            break;
//! [7] //! [8]
        case InsertText:
            textItem = new DiagramTextItem();
            textItem->setFont(myFont);
            textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
            textItem->setZValue(1000.0);
            connect(textItem, SIGNAL(lostFocus(DiagramTextItem*)),
                    this, SLOT(editorLostFocus(DiagramTextItem*)));
            connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
                    this, SIGNAL(itemSelected(QGraphicsItem*)));
            addItem(textItem);
            textItem->setDefaultTextColor(myTextColor);
            textItem->setPos(mouseEvent->scenePos());
            emit textInserted(textItem);
//! [8] //! [9]
    default:
        ;
    }
    QGraphicsScene::mousePressEvent(mouseEvent);
}
Esempio n. 3
0
void DiagramScene::addHexagon(int count)
{
    std::cout << "-------------- addHexagon------------------,count = " << count << std::endl;

    DiagramItem *item;
    item = new DiagramItem(myItemType, myItemMenu);
    item->setBrush(myItemColor);
    addItem(item);
    count*=100;
    QPointF point(100+count,100+count);
    item->setPos(point);

//    // draw polygon
//    static const QPointF points[6] = {
//        QPointF(-0.5, 0.866+count),
//        QPointF(0.5, 0.866+count),
//        QPointF(1, 0+count),
//        QPointF(0.5, -0.866+count),
//        QPointF(-0.5, -0.866+count),
//        QPointF(-1,0+count)
//    };
//    this->painter->drawPolygon(points, 6);

}