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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

QPainter p;

void table(int x, int y, int legWidth, int length, int legLength, int rotation)
{
    int edge1[] = {x + legWidth,
                   y + legWidth + rotation};
    int edge2[] = {x + legWidth + rotation,
                   y + legWidth};
    int edge3[] = {x + legWidth + length,
                   y + legWidth};
    int edge4[] = {x + legWidth + length - rotation,
                   y + legWidth + rotation};

    p.setPen(QPen(QColor(51, 25, 0), legWidth, Qt::SolidLine));
    p.drawLine(edge1[0] + legWidth, edge1[1], edge1[0] + legWidth, edge1[1] + legLength);
    p.drawLine(edge4[0], edge4[1], edge4[0], edge4[1] + legLength);
    p.drawLine(edge2[0] + legWidth, edge2[1], edge2[0] + legWidth, edge2[1] + legLength);
    p.drawLine(edge3[0], edge3[1], edge3[0], edge3[1] + legLength);

    p.setPen(QPen(QColor(112, 61, 0), legWidth, Qt::SolidLine));
    p.drawLine(edge1[0], edge1[1], edge1[0], edge1[1] + legLength);
    p.drawLine(edge4[0] - legWidth, edge4[1], edge4[0] - legWidth, edge4[1] + legLength);
    p.drawLine(edge2[0], edge2[1], edge2[0], edge2[1] + legLength);
    p.drawLine(edge3[0] - legWidth, edge3[1], edge3[0] - legWidth, edge3[1] + legLength);

    p.setBrush(QColor(92, 41, 0));

    QPolygon polygon;
    polygon << QPoint(edge1[0], edge1[1]) << QPoint(edge2[0], edge2[1])
            << QPoint(edge3[0], edge3[1]) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(polygon);
}

void chair(int x, int y, int legWidth, int height, int length, int legLength, int rotation)
{
    int edge1[] = {x + legWidth,
                   y + height + legWidth + rotation};
    int edge2[] = {x + legWidth + rotation,
                   y + height + legWidth};
    int edge3[] = {x + legWidth + length,
                   y + height + legWidth};
    int edge4[] = {x + legWidth + length - rotation,
                   y + height + legWidth + rotation};

    p.setPen(QPen(QColor(51, 25, 0), legWidth, Qt::SolidLine));
    p.drawLine(edge1[0] + legWidth, edge1[1], edge1[0] + legWidth, edge1[1] + legLength);
    p.drawLine(edge4[0], edge4[1], edge4[0], edge4[1] + legLength);
    p.drawLine(edge2[0] + legWidth, edge2[1], edge2[0] + legWidth, edge2[1] + legLength);
    p.drawLine(edge3[0], edge3[1], edge3[0], edge3[1] + legLength);

    p.setPen(QPen(QColor(145, 61, 0), legWidth, Qt::SolidLine));
    p.drawLine(edge1[0], edge1[1], edge1[0], edge1[1] + legLength);
    p.drawLine(edge4[0] - legWidth, edge4[1], edge4[0] - legWidth, edge4[1] + legLength);
    p.drawLine(edge2[0], edge2[1], edge2[0], edge2[1] + legLength);
    p.drawLine(edge3[0] - legWidth, edge3[1], edge3[0] - legWidth, edge3[1] + legLength);

    p.setBrush(QColor(125, 41, 0));

    QPolygon seat;
    seat << QPoint(edge1[0], edge1[1]) << QPoint(edge2[0], edge2[1])
         << QPoint(edge3[0], edge3[1]) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(seat);

    QPolygon back;
    back << QPoint(edge1[0], edge1[1]) << QPoint(edge1[0], edge1[1] - height)
         << QPoint(edge4[0], edge4[1] - height) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(back);
}

void box(int x, int y, int height, int length, int rotation)
{
    int halfX = (length - rotation) / 2;
    int halfY = height / 4;
    x += halfX;
    y += halfY;

    int edge1[] = {x,
                   y + height + rotation};
    int edge2[] = {x + rotation,
                   y + height};
    int edge3[] = {x + length,
                   y + height};
    int edge4[] = {x + length - rotation,
                   y + height + rotation};

    p.setPen(QPen(QColor(51, 51, 155), 5, Qt::SolidLine));

    p.setBrush(QColor(104, 105, 255));
    QPolygon bottom;
    bottom << QPoint(edge1[0], edge1[1]) << QPoint(edge2[0], edge2[1])
           << QPoint(edge3[0], edge3[1]) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(bottom);

    p.setBrush(QColor(174, 175, 255));
    QPolygon back;
    back << QPoint(edge2[0], edge2[1]) << QPoint(edge2[0], edge2[1] - height)
         << QPoint(edge3[0], edge3[1] - height) << QPoint(edge3[0], edge3[1]);
    p.drawPolygon(back);

    p.setBrush(QColor(164, 165, 255));
    QPolygon leftSide;
    leftSide << QPoint(edge1[0], edge1[1]) << QPoint(edge1[0], edge1[1] - height)
             << QPoint(edge2[0], edge2[1] - height) << QPoint(edge2[0], edge2[1]);
    p.drawPolygon(leftSide);

    p.setBrush(QColor(194, 195, 255));
    QPolygon rightSide;
    rightSide << QPoint(edge3[0], edge3[1]) << QPoint(edge3[0], edge3[1] - height)
              << QPoint(edge4[0], edge4[1] - height) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(rightSide);

    p.setBrush(QColor(204, 205, 255));
    QPolygon front;
    front << QPoint(edge1[0], edge1[1]) << QPoint(edge1[0], edge1[1] - height)
          << QPoint(edge4[0], edge4[1] - height) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(front);

    p.setBrush(QColor(194, 195, 255));
    QPolygon leftHalf;
    leftHalf << QPoint(edge1[0], edge1[1] - height) << QPoint(edge1[0] - halfX, edge1[1] - height - halfY)
             << QPoint(edge2[0] - halfX, edge2[1] - height - halfY) << QPoint(edge2[0], edge2[1] - height);
    p.drawPolygon(leftHalf);

    QPolygon rightHalf;
    rightHalf << QPoint(edge3[0], edge3[1] - height) << QPoint(edge3[0] + halfX, edge3[1] - height - halfY)
              << QPoint(edge4[0] + halfX, edge4[1] - height - halfY) << QPoint(edge4[0], edge4[1] - height);
    p.drawPolygon(rightHalf);

    p.setPen(QPen(QColor(0, 0, 0), 5, Qt::SolidLine));
    p.setFont(QFont("times", 22));
    p.drawText(edge1[0], edge1[1] - height, edge4[0] - edge1[0], height, Qt::AlignCenter, "Скотч");
}

void cat(int x, int y, int size)
{
    int x0 = (x + size);
    int y0 = (y + size);
    int headSize[] = {size / 5, size / 4};
    int bodySize[] = {size / 3, size / 2};

    p.setPen(QPen(QColor(102, 61, 0), 5, Qt::SolidLine));
    p.setBrush(QColor(255, 173, 51));
    // туловище
    p.drawEllipse(x0, y0,
                  bodySize[0], bodySize[1]);
    // голова
    p.drawEllipse(x0 + headSize[0] / 1.5, y0 + headSize[1] / 6,
                  -headSize[0], -headSize[1]);
    // глаз
    p.setPen(QPen(QColor(102, 138, 0), 15, Qt::SolidLine));
    p.setBrush(QColor(96, 128, 0));
    p.drawEllipse(x0, y0 - headSize[1] / 2,
                  headSize[0] / 10, headSize[0] / 10);
    p.setPen(QPen(QColor(102, 61, 0), 5, Qt::SolidLine));
    p.setBrush(QColor(255, 173, 51));
    // нижняя права лапа
    p.drawEllipse(x0 + headSize[0] / 6, y0 + bodySize[1],
                  -headSize[0] / 3, -headSize[1] / 2);
    // нижняя левая лапа
    p.drawEllipse(x0 + headSize[0] / 3.3, y0 + bodySize[1] * 1.1,
                  -headSize[0] / 2.5, -headSize[1] / 1.8);
    // левая верхняя лапа
    p.drawEllipse(x0 + headSize[0] / 5, y0 + bodySize[1] * 0.45,
                  -headSize[0] / 2.5, -headSize[1] / 1.8);
    // правая верхняя лапа
    p.drawEllipse(x0 + headSize[0] / 3.3, y0 + bodySize[1] * 0.5,
                  -headSize[0] / 2.5, -headSize[1] / 1.8);
    // усы
    p.drawLine(x0, y0 - headSize[1] / 6,
            x0 - headSize[0] / 2.5, y0 + headSize[1] / 6);
    p.drawLine(x0, y0 - headSize[1] / 8,
            x0 - headSize[0] / 2.5, y0 + headSize[1] / 3);

    p.drawLine(x0 - headSize[0] / 2, y0 - headSize[1] / 1.5,
            x0 - headSize[0] / 3, y0 - headSize[1] / 5);
    p.drawLine(x0 - headSize[0] / 1.5, y0 - headSize[1] / 1.5,
            x0 - headSize[0] / 3, y0 - headSize[1] / 5);
}

void computer(int x, int y, int width, int height, int rotation)
{
    int penWidth = 7;
    int edge1[] = {x + penWidth,
                   y + penWidth + height + rotation};
    int edge2[] = {x + penWidth + rotation,
                   y + penWidth + height};
    int edge3[] = {x + penWidth + width,
                   y + penWidth + height};
    int edge4[] = {x + penWidth + width - rotation,
                   y + penWidth + height + rotation};

    p.setPen(QPen(QColor(255, 0, 255), 7, Qt::SolidLine));

    int legWidth = (edge4[0] + edge1[0]) / 7;

    p.setPen(QPen(QColor(235, 0, 235), 7, Qt::SolidLine));
    p.drawArc((edge4[0] + edge1[0]) / 2 - legWidth / 2 + rotation / 7, edge1[1] + penWidth - rotation / 7,
            legWidth, height / 8, 0 * 16, 180 * 16);

    p.setPen(QPen(QColor(255, 0, 255), 7, Qt::SolidLine));
    p.drawArc((edge4[0] + edge1[0]) / 2 - legWidth / 2, edge1[1] + penWidth,
            legWidth, height / 8, 0 * 16, 180 * 16);

    p.setBrush(QColor(235, 123, 235));
    QPolygon bottom;
    bottom << QPoint(edge1[0], edge1[1]) << QPoint(edge2[0], edge2[1])
           << QPoint(edge3[0], edge3[1]) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(bottom);

    p.setBrush(QColor(245, 143, 245));
    QPolygon back;
    back << QPoint(edge2[0], edge2[1]) << QPoint(edge2[0], edge2[1] - height)
         << QPoint(edge3[0], edge3[1] - height) << QPoint(edge3[0], edge3[1]);
    p.drawPolygon(back);

    p.setBrush(QColor(240, 138, 240));
    QPolygon leftSide;
    leftSide << QPoint(edge1[0], edge1[1]) << QPoint(edge1[0], edge1[1] - height)
             << QPoint(edge2[0], edge2[1] - height) << QPoint(edge2[0], edge2[1]);
    p.drawPolygon(leftSide);

    p.setBrush(QColor(245, 143, 245));
    QPolygon rightSide;
    rightSide << QPoint(edge3[0], edge3[1]) << QPoint(edge3[0], edge3[1] - height)
              << QPoint(edge4[0], edge4[1] - height) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(rightSide);

    p.setBrush(QColor(255, 153, 255));
    QPolygon front;
    front << QPoint(edge1[0], edge1[1]) << QPoint(edge1[0], edge1[1] - height)
          << QPoint(edge4[0], edge4[1] - height) << QPoint(edge4[0], edge4[1]);
    p.drawPolygon(front);

    p.setBrush(QColor(255, 163, 255));
    QPolygon top;
    top << QPoint(edge1[0], edge1[1] - height) << QPoint(edge2[0], edge2[1] - height)
        << QPoint(edge3[0], edge3[1] - height) << QPoint(edge4[0], edge4[1] - height);
    p.drawPolygon(top);


    p.drawImage(QRect(edge1[0], edge1[1] - height, edge4[0] - edge1[0], height), QImage("Z:\paint/catos.jpg"));
}

void MainWindow::paintEvent(QPaintEvent *)
{
    p.begin(this);

    table(300, 700, 10, 500, 300, 100);
    chair(150, 700, 10, 300, 300, 150, 75);
    computer(300, 380, 512, 287, 30);
    cat(650, 650, 300);
    box(1000, 150, 300, 500, 100);

    p.end();
}
