#3449 Improve checkbox column size hint, alignment and "hit" rectanngle

This commit is contained in:
Jacob Støren 2018-10-03 08:40:28 +02:00
parent 979882da03
commit bd79bd89c1
2 changed files with 34 additions and 16 deletions

View File

@ -59,6 +59,23 @@ PdmUiCheckBoxDelegate::~PdmUiCheckBoxDelegate()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QRect adjustedPaintRect(const QStyleOptionViewItem &option)
{
const int margin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
QRect newRect = QStyle::alignedRect(option.direction,
Qt::AlignCenter,
QSize(option.decorationSize.width() + margin, option.rect.height()),
QRect(option.rect.x(),
option.rect.y(),
option.rect.width(),
option.rect.height()));
return newRect;
}
//--------------------------------------------------------------------------------------------------
///
@ -67,15 +84,8 @@ void PdmUiCheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem
{
QStyleOptionViewItemV4 viewItemOption(option);
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
QRect newRect = QStyle::alignedRect(option.direction,
Qt::AlignCenter,
QSize(option.decorationSize.width() + 5, option.rect.height()),
QRect(option.rect.x(),
option.rect.y(),
option.rect.width() - (2 * textMargin),
option.rect.height()));
viewItemOption.rect = newRect;
viewItemOption.rect = adjustedPaintRect(option);
QStyledItemDelegate::paint(painter, viewItemOption, index);
}
@ -103,14 +113,12 @@ bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model
// make sure that we have the right event type
if (event->type() == QEvent::MouseButtonRelease)
{
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
QRect paintRect = adjustedPaintRect(option);
QRect checkRect = QStyle::alignedRect(option.direction,
Qt::AlignCenter,
option.decorationSize,
QRect(option.rect.x() + (2 * textMargin),
option.rect.y(),
option.rect.width() - (2 * textMargin),
option.rect.height()));
paintRect);
if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
return true;
@ -129,6 +137,13 @@ bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model
return model->setData(index, state, Qt::CheckStateRole);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QSize PdmUiCheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(option.decorationSize.width(), option.decorationSize.height());
}
} // end namespace caf

View File

@ -57,6 +57,9 @@ public:
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};