cutter/src/widgets/BoolToggleDelegate.cpp
2021-01-24 18:34:16 +02:00

29 lines
1.1 KiB
C++

#include "BoolToggleDelegate.h"
#include <QEvent>
BoolTogggleDelegate::BoolTogggleDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
QWidget *BoolTogggleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.data(Qt::EditRole).type() == QVariant::Bool) {
return nullptr;
}
return QStyledItemDelegate::createEditor(parent, option, index);
}
bool BoolTogggleDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
if (model->flags(index).testFlag(Qt::ItemFlag::ItemIsEditable)) {
if (event->type() == QEvent::MouseButtonDblClick) {
auto data = index.data(Qt::EditRole);
if (data.type() == QVariant::Bool) {
model->setData(index, !data.toBool());
return true;
}
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}