#include "wnd_create_mat.h"

#include <qevent.h>
#include <QLabel>
#include <QLineEdit>
#include <qmessagebox.h>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>



WndCreateMat::WndCreateMat(QWidget *parent, const std::vector<std::string> &items,
                           std::string &outShader, std::string &outMatName) :
QDialog(parent), out_shader(&outShader), out_matName(&outMatName), m_tex(new QLineEdit) {
    this->setWindowFlag(Qt::WindowType::WindowCloseButtonHint, true);
    this->setLayout(new QVBoxLayout);
    this->layout()->setAlignment(Qt::AlignTop);
    this->layout()->addWidget(new QLabel{"Material Name"});
    this->layout()->addWidget(m_tex);
    this->layout()->addWidget(new QLabel{"Select Shader"});
    QScrollArea *scroll = new QScrollArea;
    scroll->setLayout(new QVBoxLayout);
    QWidget *w = new QWidget;
    scroll->layout()->addWidget(w);
    scroll->setWidget(w);
    scroll->setWidgetResizable(true);
    QVBoxLayout *lay = new QVBoxLayout{};
    w->setLayout(lay);
    lay->setAlignment(Qt::AlignTop);
    this->layout()->addWidget(scroll);

    for (auto &i: items) {
        QPushButton *btn = new QPushButton{i.c_str()};
        lay->addWidget(btn);
        connect(btn, &QPushButton::clicked, this, &WndCreateMat::OnBtnClicked);
    }
    this->resize(230, 430);
}
void WndCreateMat::keyPressEvent(QKeyEvent *event) {
    event->accept();
    // QMessageBox::keyPressEvent(event);
}
void WndCreateMat::OnBtnClicked() {
    auto *btn = qobject_cast<QPushButton *>(this->sender());
    if (!btn) { return; }

    if (m_tex->text().isEmpty()) {
        QMessageBox::about(this, "错误", "请输入Material Name");
    } else {
        *out_matName = m_tex->text().toUtf8().toStdString();
        *out_shader = btn->text().toUtf8().toStdString();
        this->accept();
    }
}














//
