You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.7 KiB
C++
74 lines
2.7 KiB
C++
#include "checkboxdelegate.h"
|
|
|
|
#include <QApplication>
|
|
#include <QEvent>
|
|
#include <QKeyEvent>
|
|
#include <QMouseEvent>
|
|
#include <QPainter>
|
|
|
|
CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
|
|
: QStyledItemDelegate(parent)
|
|
{
|
|
}
|
|
|
|
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
QStyleOptionViewItem viewItemOption(option);
|
|
|
|
if (option.state & QStyle::State_Selected) {
|
|
painter->setBrush(option.palette.highlight());
|
|
QPen nPen;
|
|
nPen.setColor(option.palette.highlight().color());
|
|
painter->setPen(nPen);
|
|
painter->drawRect(option.rect);
|
|
}
|
|
|
|
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
|
QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
|
|
QSize(option.decorationSize.width() - 1.5 * textMargin, option.decorationSize.height()),
|
|
QRect(option.rect.x(), option.rect.y(),
|
|
option.rect.width(), option.rect.height()));
|
|
viewItemOption.rect = newRect;
|
|
|
|
QStyledItemDelegate::paint(painter, viewItemOption, index);
|
|
}
|
|
|
|
bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
|
|
const QModelIndex &index)
|
|
{
|
|
Q_ASSERT(event);
|
|
Q_ASSERT(model);
|
|
|
|
// make sure that the item is checkable
|
|
Qt::ItemFlags flags = model->flags(index);
|
|
|
|
if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
|
|
return false;
|
|
|
|
// make sure that we have a check state
|
|
QVariant value = index.data(Qt::CheckStateRole);
|
|
|
|
if (!value.isValid())
|
|
return false;
|
|
|
|
// make sure that we have the right event type
|
|
if (event->type() == QEvent::MouseButtonRelease) {
|
|
QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
|
|
option.decorationSize,
|
|
QRect(option.rect.x(), option.rect.y(),
|
|
option.rect.width(), option.rect.height()));
|
|
|
|
if (!checkRect.contains(static_cast<QMouseEvent *>(event)->pos()))
|
|
return false;
|
|
} else if (event->type() == QEvent::KeyPress) {
|
|
if (static_cast<QKeyEvent *>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent *>(event)->key() != Qt::Key_Select)
|
|
return false;
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
|
|
? Qt::Unchecked : Qt::Checked);
|
|
return model->setData(index, state, Qt::CheckStateRole);
|
|
}
|