/**
 * Draws a 3D cuboid by extending a 2D rectangle in the z-axis
 *
 * @param rect The rectangle to draw
 * @param brush The brush fill the surfaces of the cuboid with
 * @param pen The pen to draw the edges with
 * @param props The 3D properties to use for drawing the cuboid
 * @return The drawn cuboid as a polygon
 */
QPolygonF StockDiagram::Private::ThreeDPainter::drawThreeDRect( const QRectF &rect, const QBrush &brush,
                                                                const QPen &pen, const ThreeDProperties &props )
{
    // Restores the painting properties when destroyed
    PainterSaver painterSaver( painter );

    // Make sure that the top really is the top
    const QRectF normalizedRect = rect.normalized();

    // Calculate all the four sides of the rectangle
    const QLineF topSide = QLineF( normalizedRect.topLeft(), normalizedRect.topRight() );
    const QLineF bottomSide = QLineF( normalizedRect.bottomLeft(), normalizedRect.bottomRight() );
    const QLineF leftSide = QLineF( normalizedRect.topLeft(), normalizedRect.bottomLeft() );
    const QLineF rightSide = QLineF( normalizedRect.topRight(), normalizedRect.bottomRight() );

    QPolygonF drawnPolygon;

    // Shorter names are easier on the eyes
    const qreal angle = props.angle;

    // Only top and right side is visible
    if ( angle >= 0.0 && angle < 90.0 ) {
        drawnPolygon = drawnPolygon.united( drawThreeDLine( topSide, brush, pen, props ) );
        drawnPolygon = drawnPolygon.united( drawThreeDLine( rightSide, brush, pen, props ) );
    // Only top and left side is visible
    } else if ( angle >= 90.0 && angle < 180.0 ) {
        drawnPolygon = drawnPolygon.united( drawThreeDLine( topSide, brush, pen, props ) );
        drawnPolygon = drawnPolygon.united( drawThreeDLine( leftSide, brush, pen, props ) );
    // Only bottom and left side is visible
    } else if ( angle >= 180.0 && angle < 270.0 ) {
        drawnPolygon = drawnPolygon.united( drawThreeDLine( bottomSide, brush, pen, props ) );
        drawnPolygon = drawnPolygon.united( drawThreeDLine( leftSide, brush, pen, props ) );
    // Only bottom and right side is visible
    } else if ( angle >= 270.0 && angle <= 360.0 ) {
        drawnPolygon = drawnPolygon.united( drawThreeDLine( bottomSide, brush, pen, props ) );
        drawnPolygon = drawnPolygon.united( drawThreeDLine( rightSide, brush, pen, props ) );
    }

    // Draw the front side
    painter->setPen( pen );
    painter->setBrush( brush );
    painter->drawRect( normalizedRect );

    return drawnPolygon;
}
Exemplo n.º 2
0
void Edge::calcShape() {
    QPainterPath p;

    auto route = mConnectionReference->displayRoute();
    std::vector<QRectF> v;
    for(size_t i = 0; i < route.size() - 1; ++i) {
        QPointF p1(route.at(i).x, route.at(i).y);
        QPointF p2(route.at(i+1).x, route.at(i+1).y);
        QRectF r(p1, p2);
        grow(r, 10);
        v.push_back(r);
    }

    QPolygonF poly;
    for(auto r : v) {
        poly = poly.united(r);
    }

    p.addPolygon(poly);
    p.closeSubpath();
    mShape = p;
}