#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsPolygonItem>
#include <QLineEdit>
#include <QButtonGroup>
#include <QPushButton>
#include <QTimer>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QDebug>
#include <QRadioButton>
#include <QVBoxLayout>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    speedInput(nullptr),
    directionButtons(nullptr),
    speed(0),
    direction(0),
    timer(nullptr),
    object(nullptr)
{
    ui->setupUi(this);

    // Set window title
    setWindowTitle("Your Name - Group Number - Variant Number QT-GI-15");

    // Create QGraphicsScene and set it as the central widget
    scene = new QGraphicsScene(this);
    ui->setupUi(this);
    ui->graphicsView->setScene(scene); // заменить на соответствующее имя QGraphicsView объекта

    // Create the menu
    createMenu();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createMenu()
{
    QMenu *menu = new QMenu("Menu", this);

    QAction *showPictureAction = menu->addAction("Show picture");
    connect(showPictureAction, &QAction::triggered, this, &MainWindow::showPicture);

    QAction *chooseAction = menu->addAction("Choose");
    connect(chooseAction, &QAction::triggered, this, &MainWindow::choose);

    QAction *animateAction = menu->addAction("Animate");
    connect(animateAction, &QAction::triggered, this, &MainWindow::animate);

    QAction *stopAction = menu->addAction("Stop");
    connect(stopAction, &QAction::triggered, this, &MainWindow::stop);

    QAction *quitAction = menu->addAction("Quit");
    connect(quitAction, &QAction::triggered, this, &MainWindow::close);

    menuBar()->addMenu(menu);
}

void MainWindow::showPicture()
{
// Remove any existing object in the scene
if(object != nullptr)
scene->removeItem(object);

// Add a rectangle
QGraphicsRectItem *rectangle = new QGraphicsRectItem(-50, -50, 100, 100);
rectangle->setBrush(Qt::red);
scene->addItem(rectangle);

// Add a triangle
QGraphicsPolygonItem *triangle = new QGraphicsPolygonItem();
QPolygonF polygon;
polygon << QPointF(-50, 0) << QPointF(50, 0) << QPointF(0, 50);
triangle->setPolygon(polygon);
triangle->setPos(0, -50);
triangle->setBrush(Qt::green);
scene->addItem(triangle);

// Add a circle
QGraphicsEllipseItem *circle = new QGraphicsEllipseItem(-25, -25, 50, 50);
circle->setBrush(Qt::blue);
scene->addItem(circle);

// Add a trapezoid
QGraphicsPolygonItem *trapezoid = new QGraphicsPolygonItem();
QPolygonF trapezoidPolygon;
trapezoidPolygon << QPointF(-50, 0) << QPointF(-25, -50) << QPointF(25, -50) << QPointF(50, 0);
trapezoid->setPolygon(trapezoidPolygon);
trapezoid->setBrush(Qt::yellow);
scene->addItem(trapezoid);

// Set the object to the center of the scene
object = rectangle;
object->setPos(0, 0);
}



void MainWindow::choose()
{
// Create a dialog to get the speed and direction input
QDialog *dialog = new QDialog(this);
dialog->setWindowTitle("Choose options");

speedInput = new QLineEdit(dialog);
speedInput->setPlaceholderText("Enter speed (pixels per second)");

directionButtons = new QButtonGroup(dialog);
QRadioButton *upButton = new QRadioButton("Up-Down");
QRadioButton *leftRightButton = new QRadioButton("Left-Right");
directionButtons->addButton(upButton, 0);
directionButtons->addButton(leftRightButton, 1);

QPushButton *okButton = new QPushButton("OK");
connect(okButton, &QPushButton::clicked, dialog, &QDialog::accept);

QVBoxLayout *layout = new QVBoxLayout(dialog);
layout->addWidget(speedInput);
layout->addWidget(upButton);
layout->addWidget(leftRightButton);
layout->addWidget(okButton);

if(dialog->exec() == QDialog::Accepted)
{
// Get the speed and direction input
speed = speedInput->text().toInt();
direction = directionButtons->checkedId();
}

// Clean up the dialog
delete speedInput;
delete directionButtons;
delete dialog;
}


void MainWindow::animate()
{
if(timer == nullptr)
{
// Create a timer to animate the object
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::moveObject);
}

// Start the timer with the specified speed and direction
if(direction == 0)
object->setPos(0, -50);
else
object->setPos(-50, 0);

timer->start(1000.0 / speed);
}




void MainWindow::moveObject()
{
qreal x = object->x();
qreal y = object->y();

// Move the object in the specified direction
if(direction == 0)
y += 1;
else
x += 1;

// Reverse direction when reaching the edge of the scene
if(x >= scene->width() - object->boundingRect().width() || x <= 0)
direction = 1 - direction;

if(y >= scene->height() - object->boundingRect().height() || y <= 0)
direction = 1 - direction;

// Set the new position of the object
object->setPos(x, y);
}



void MainWindow::stop()
{
if(timer != nullptr)
{
// Stop the timer
timer->stop();

// Reset the direction and speed
direction = 0;
speed = 0;
}
}

при клике на animate окно закрывается