cesarblum / sizegripitem

A size grip QGraphicsItem for interactive resizing.
Other
30 stars 19 forks source link

Resizer other shapes? #5

Open sgbzona opened 7 years ago

sgbzona commented 7 years ago

Hi, I'm trying to add other shapes (Polygon, Line, etc) but when it is created the resizer:

class PolyResizer : public SizeGripItem::Resizer
    {
        public:
            virtual void operator()(QGraphicsItem* item, const QRectF& rect)
            {
                QGraphicsPolygonItem* polyItem =
                    dynamic_cast<QGraphicsPolygonItem*>(item);

                if (polyItem)
                {
                   // << there is not rect on this class...  polyItem->setRect(rect);
                }
            }
    };

Or maybe, do I need to add something else?

sabirmgd commented 5 years ago

basically you get back the rect of the handles, I implemented the line this way for now but its not the best but its a progress for now.

virtual void operator()(QGraphicsItem *item, const QRectF &rect)
    {
        auto lineItem =
                dynamic_cast<LineAnnotationGraphicsItem*>(item);

        if (lineItem)
        {
            const auto lineAngle = QLineF(lineItem->line()).angle();

            if (lineAngle >= 0 && lineAngle <= 90)
            {
                lineItem->setLine(QLine(rect.bottomLeft().toPoint(), rect.topRight().toPoint()));
            }
            else if (lineAngle >= 91 && lineAngle <= 180)
            {
                lineItem->setLine(QLine(rect.bottomRight().toPoint(), rect.topLeft().toPoint()));
            }
            else if (lineAngle >= 181 && lineAngle <= 270)
            {
                lineItem->setLine(QLine(rect.topRight().toPoint(), rect.bottomLeft().toPoint()));
            }
            else
            {
                lineItem->setLine(QLine(rect.topLeft().toPoint(), rect.bottomRight().toPoint()));
            }
        }
    }
sridharan1986 commented 2 years ago

I need to resize QGraphicsTextItem. It has only setTextWidth(), which changes only the width. No explicit function to set bounding rectangle. On resize using left resize handler, I need QGraphicsTextItem to move to new position. How to do that ?

class TextResizer : public SizeGripItem::Resizer { public: virtual void operator()(QGraphicsItem item, const QRectF& rect) { QGraphicsTextItem textItem = dynamic_cast<QGraphicsTextItem*>(item);

    if (textItem)
    {
        textItem->setTextWidth(rect.width());
    }
}

};