#include "passworddialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
#include <QEvent>

PasswordDialog::PasswordDialog(QWidget *parent) :
    QDialog(parent),
    passwordLineEdit(new QLineEdit(this)),
    confirmButton(new QPushButton("确认", this))
{
    QVBoxLayout *layout = new QVBoxLayout(this);

    QLabel *label = new QLabel("请输入密码:", this);
    label->setStyleSheet("QLabel { font-size: 14px; }");
    layout->addWidget(label);

    passwordLineEdit->setEchoMode(QLineEdit::Password);
    layout->addWidget(passwordLineEdit);

    confirmButton->setStyleSheet("QPushButton { font-size: 12px; }");
    layout->addWidget(confirmButton);

    connect(confirmButton, &QPushButton::clicked, this, &PasswordDialog::onConfirmButtonClicked);

    setLayout(layout);

    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setAttribute(Qt::WA_DeleteOnClose);

    // 安装事件过滤器
//    passwordLineEdit->installEventFilter(this);
    passwordLineEdit->setFont(QFont("Arial"));
}

QString PasswordDialog::getPassword() const
{
    return passwordLineEdit->text();
}

void PasswordDialog::onConfirmButtonClicked()
{
//    qDebug() << "Entered password:" << getPassword();
    accept(); // 关闭对话框并返回 QDialog::Accepted
}


//bool PasswordDialog::eventFilter(QObject *obj, QEvent *event)
//{
//    if (obj == passwordLineEdit) {
//        if (event->type() == QEvent::FocusIn) {
//            // 处理焦点进入事件，确保虚拟键盘能够正常工作
//            qDebug() << "QLineEdit got focus";
//        }
//    }
//    return QDialog::eventFilter(obj, event);
//}
