//
// Created by HW on 2023/07/26.
//

// You may need to build the project (run Qt uic code generator) to get "ui_NavBar.h" resolved

#include "navbar.h"
#include "ui_NavBar.h"


NavBar::NavBar(QList<ButtonStruct *> &buttonNames,QWidget *parent) :
        QWidget(parent), ui(new Ui::NavBar) {
    ui->setupUi(this);
    layout=new QHBoxLayout();
    layout->setContentsMargins(0,0,0,0);
    this->setMinimumHeight(48);
    this->setMaximumHeight(200);
    for(auto buttonName : buttonNames){
        MyButton *button=new MyButton(buttonName->logo,buttonName->text,height(),height(),&buttons);
        QSizePolicy sizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::QSizePolicy::Fixed);
        button->setSizePolicy(sizePolicy);
        layout->addWidget(button);
        buttons<<button;
    }

    layout->setAlignment(Qt::AlignHCenter);
    layout->setSpacing(0);
    this->setLayout(layout);

}

NavBar::~NavBar() {
    for(auto button : buttons){
        delete button;
    }
    delete layout;
    delete ui;
}

void NavBar::paintEvent(QPaintEvent *event) {
    QWidget::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    QRectF rect(0, 0, this->width(), this->height());
    painter.fillRect(rect, QColor("#333332"));

}
void NavBar::resizeEvent(QResizeEvent *event) {
    QWidget::resizeEvent(event);
    for(auto button:buttons){
        button->setSize(event->size().height(),event->size().height(),event->size().height(),event->size().height());
        button->resize(event->size().height(),event->size().height());
        button->update();
    }
}
