mardi 12 octobre 2021

Qt resize child rectangles

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?

Aucun commentaire:

Enregistrer un commentaire