Qt resize child rectangles

Viewed 133

Working on a school project, but I'm completely stuck. I have to write a paint application, in which you can draw Rectangles & Ellipses, select them, group them, move and resize them. After that I have to start implementing several design patterns.

I have to following problem. I have an abstract class Figure which inherits QRect, then there are the classes Rectangle, Ellipse and Group which all inherit the Figure class.

I am able to resize a single figure (ellipse or shape), by clicking on the corner of a selected shape, but the problem is resizing a group which contains several other figures (group, rectangle or ellipse), they have to resize relatively to their parent figure:

Mouse move event

void MainWindow::OnPaintBoxMouseMove(PaintBox *sender, QMouseEvent *event) {
    //Mouse move event based on which tool is selected
    switch(tool){
        //When rectangle tool is selected, update rectangle size to mouse position
        //in command
        case rectangle: {
            tempCmd->update(event->pos());
            break;
        }
        //When ellipse tool is selected, update ellipse size to mouse position
        //in command
        case ellipse: {
            tempCmd->update(event->pos());
            break;
        }
        //When select tool is selected
        case select: {
            //If moving is selected, update shape in command
            if (moving){
                sRect->move(startPoint,event->pos());
                tempCmd->update(event->pos());
                startPoint = event->pos();
            }
            //If resizing is selected, update shape in command
            else if (resizing){
                sRect->resize(startPoint,event->pos());
                tempCmd->update(event->pos());
                startPoint = event->pos();
            }
            //Otherwise update size of selection rectangle
            else if (sRect != nullptr) {
                sRect->updateDimensions(event->pos());
            }
            break;
        }
    }
    paintBox->update();
}

Resize method in resize command called 'update' here

void ResizeCmd::update(QPoint mousePosition) {
    switch (direction){
        case 1: {
            _figure->setTopLeft(_figure->topLeft()+mousePosition-startMousePosition);
            break;
        }
        case 2: {
            _figure->setTopRight(_figure->topRight()+mousePosition-startMousePosition);
            break;
        }
        case 3: {
            _figure->setBottomLeft(_figure->bottomLeft()+mousePosition-startMousePosition);
            break;
        }
        case 4: {
            _figure->setBottomRight(_figure->bottomRight()+mousePosition-startMousePosition);
            break;
        }
    }
    startMousePosition = mousePosition;
}

So the question is, how am I going to implement the resize method in the group class, which may contain other child groups as well?

1 Answers

I think you are looking for something like this Repo :

https://github.com/cl0ne/resizer-item

For doing your project you should use graphics libs of Qt like QGraphicsItem ,QGraphicsScene, and QGraphicsView

you shouldn't separate all your Items and write a resizer. this repo is general for all QGraphicsItems which means that If you want Rectangle you should define one QGraphicsRectItem and add this resizer to it like this :

QGraphicsScene *scene = new QGraphicsScene(this);
    QGraphicsRectItem *item = new QGraphicsRectItem(QRectF(0, 0, 100, 100));
    item->setPos(10, 10);
    item->setFlag(QGraphicsItem::ItemIsMovable);
    item->setPen(QColor(102, 102, 102));
    item->setBrush(QColor(158, 204, 255));
    scene->addItem(item);

    GraphicsItemResizer *resizer = new GraphicsItemResizer(item);
    resizer->setBrush(QColor(64, 64, 64));
    resizer->setMinSize(QSizeF(30, 30));
    resizer->setTargetSize(item->boundingRect().size());
    resizer->setHandlersIgnoreTransformations(true);
    QObject::connect(
        resizer,
        &GraphicsItemResizer::targetRectChanged,
        [item](const QRectF & rect)
        {
            QPointF pos = item->pos();
            item->setPos(pos + rect.topLeft());
            QRectF old = item->rect();
            item->setRect(QRectF(old.topLeft(), rect.size()));
        }
    );

    ui->graphicsView->setScene(scene);

enter image description here

Related