/*
    delegate.cpp

    A delegate that allows the user to change integer values from the model
    using a spin box widget.
*/

#include "x_checkboxdelegate.h"

#include <QCheckBox>
#include <QMouseEvent>
#include <QApplication>
X_CheckBoxDelegate::X_CheckBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{

}

QWidget *X_CheckBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem & option ,
    const QModelIndex & index ) const
{
//    if(index.column()>=3&&index.column()<=9)
//    {
//        QCheckBox *editor = new QCheckBox(parent);
//       // editor->setFrame(false);

//        ///editor->setAlignment(Qt::AlignHCenter);
//       // editor->setDisplayFormat("hh:mm");

//        return editor;

//    }

    if(index.column()>=3&&index.column()<=9)
    {
         QCheckBox *editor=new QCheckBox(parent);
         editor->installEventFilter(const_cast<X_CheckBoxDelegate*>(this));
         return editor;
     }
     else
         return QStyledItemDelegate::createEditor(parent,option,index);

    return nullptr;
}

//void X_CheckBoxDelegate::setEditorData(QWidget *editor,
//                                    const QModelIndex &index) const
//{
//    if(index.column()>=3&&index.column()<=9)
//    {
//            bool data = index.model()->data(index, Qt::UserRole).toBool();
//            QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
//            checkBox->setChecked(data);

//    }
//    else
//         QStyledItemDelegate::setEditorData(editor,index);

//}

//void X_CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
//                                   const QModelIndex &index) const
//{
//    QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
//    bool value = checkBox->isChecked();
//    model->setData(index, value, Qt::UserRole);
//}
// 绘制复选框
void X_CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem viewOption(option);
    initStyleOption(&viewOption, index);
    if (option.state.testFlag(QStyle::State_HasFocus))
        viewOption.state = viewOption.state ^ QStyle::State_HasFocus;

    QStyledItemDelegate::paint(painter, viewOption, index);

    if (index.column()>=3&&index.column()<=9)
    {
        bool data = index.model()->data(index, Qt::UserRole).toBool();

        QStyleOptionButton checkBoxStyle;
        checkBoxStyle.state = data ? QStyle::State_On : QStyle::State_Off;
        checkBoxStyle.state |= QStyle::State_Enabled;
        checkBoxStyle.iconSize = QSize(20, 20);
        checkBoxStyle.rect = option.rect;

        QCheckBox checkBox;
        QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkBoxStyle, painter, &checkBox);
    }
}

// 响应鼠标事件，更新数据
bool X_CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    QRect decorationRect = option.rect;

    QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
    {
        if (index.column()>=3&&index.column()<=9)
        {
            bool data = model->data(index, Qt::UserRole).toBool();
            model->setData(index, !data, Qt::UserRole);
        }
    }

    return QStyledItemDelegate::editorEvent(event, model, option, index);
}
void X_CheckBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}

