bool ColorDelegate::editorEvent(QEvent* event,
                                QAbstractItemModel* model,
                                const QStyleOptionViewItem& option,
                                const QModelIndex& index)
{

    if ( event->type() == QEvent::MouseButtonRelease && index.data().canConvert<QColor>())
    {
        QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);

        if ( mouse_event->button() == Qt::LeftButton &&
            ( index.flags() & Qt::ItemIsEditable) )
        {
            ColorDialog *editor = new ColorDialog(const_cast<QWidget*>(option.widget));
            connect(this, &QObject::destroyed, editor, &QObject::deleteLater);
            editor->setMinimumSize(editor->sizeHint());
            auto original_color = index.data().value<QColor>();
            editor->setColor(original_color);
            auto set_color = [model, index](const QColor& color){
                model->setData(index, QVariant(color));
            };
            connect(editor, &ColorDialog::colorSelected, this, set_color);
            editor->show();
        }

        return true;
    }

    return QAbstractItemDelegate::editorEvent(event, model, option, index);
}
QWidget *ColorDelegate::createEditor(QWidget *parent,
                                      const QStyleOptionViewItem &option,
                                      const QModelIndex &index) const
{
    if (index.data().canConvert<QColor>())
    {
        ColorDialog *editor = new ColorDialog(parent);
        editor->setMinimumSize(editor->sizeHint());
        connect(editor, SIGNAL(colorChanged(QColor)), this, SLOT(color_changed()));
        connect(editor, SIGNAL(accepted()), this, SLOT(close_editor()));
        return editor;
    }
    else
        return QStyledItemDelegate::createEditor(parent, option, index);
}