#include "CustomQDateTimeEdit.h"

#include <QInputDialog>
#include <QApplication>

CustomQDateTimeEdit::CustomQDateTimeEdit(QWidget *parent)
    : QDateTimeEdit(parent)
{
    setReadOnly(true);// 防止直接输入，强制用对话框输入

    m_input_dialog.reset(new DialogEditValue());
    m_input_dialog->setParent(parent);
    m_input_dialog->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    m_input_dialog->setWindowModality(Qt::WindowModal);

    setTimeSpec(Qt::UTC);

    QLineEdit* edit = this->findChild<QLineEdit*>();
    if (edit) {
        edit->setReadOnly(true);
        edit->installEventFilter(this);
    }
}

CustomQDateTimeEdit::~CustomQDateTimeEdit()
{
}

bool CustomQDateTimeEdit::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress) {
        // 弹出你的输入对话框
        m_input_dialog->setValue(text(), General::Type::DATE_TIME);
        m_input_dialog->setEditType(EditType::DATETIME);
        auto ret = m_input_dialog->exec();
        if (ret == QDialog::Accepted) {
            setDateTime(QDateTime::fromString(m_input_dialog->getValue(General::Type::DATE_TIME), Qt::ISODate));
        }
        return true; // 拦截
    }
    return QDateTimeEdit::eventFilter(obj, event);
}
