2015-07-29 07:19:43 -05:00
|
|
|
//##################################################################################################
|
|
|
|
//
|
|
|
|
// Custom Visualization Core library
|
|
|
|
// Copyright (C) Ceetron Solutions AS
|
|
|
|
//
|
|
|
|
// This library may be used under the terms of either the GNU General Public License or
|
|
|
|
// the GNU Lesser General Public License as follows:
|
|
|
|
//
|
|
|
|
// GNU General Public License Usage
|
|
|
|
// This library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
|
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
// GNU Lesser General Public License Usage
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
|
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
//##################################################################################################
|
|
|
|
|
|
|
|
#include "cafPdmUiCheckBoxDelegate.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
namespace caf
|
|
|
|
{
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
PdmUiCheckBoxDelegate::PdmUiCheckBoxDelegate( QObject* pParent )
|
|
|
|
: QStyledItemDelegate( pParent )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
PdmUiCheckBoxDelegate::~PdmUiCheckBoxDelegate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
QRect adjustedPaintRect( const QStyleOptionViewItem& option )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
const int margin = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameHMargin ) + 1;
|
2018-10-03 01:40:28 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
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() ) );
|
2018-10-03 01:40:28 -05:00
|
|
|
|
|
|
|
return newRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2018-10-03 01:40:28 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
void PdmUiCheckBoxDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
|
2018-10-03 01:40:28 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
QStyleOptionViewItem viewItemOption( option );
|
2018-10-03 01:40:28 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
viewItemOption.rect = adjustedPaintRect( option );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
QStyledItemDelegate::paint( painter, viewItemOption, index );
|
|
|
|
}
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
/// Returns true to avoid other factories to produce editors for a check box
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
bool PdmUiCheckBoxDelegate::editorEvent( QEvent* event,
|
|
|
|
QAbstractItemModel* model,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
Q_ASSERT( event );
|
|
|
|
Q_ASSERT( model );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
// make sure that the item is checkable
|
2020-06-19 00:53:59 -05:00
|
|
|
Qt::ItemFlags flags = model->flags( index );
|
|
|
|
if ( !( flags & Qt::ItemIsUserCheckable ) || !( flags & Qt::ItemIsEnabled ) ) return false;
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
// make sure that we have a check state
|
2020-06-19 00:53:59 -05:00
|
|
|
QVariant value = index.data( Qt::CheckStateRole );
|
|
|
|
if ( !value.isValid() ) return false;
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
// make sure that we have the right event type
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( event->type() == QEvent::MouseButtonRelease )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
QRect paintRect = adjustedPaintRect( option );
|
|
|
|
QRect checkRect = QStyle::alignedRect( option.direction, Qt::AlignCenter, option.decorationSize, paintRect );
|
2018-10-03 01:40:28 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( !checkRect.contains( static_cast<QMouseEvent*>( event )->pos() ) ) return true;
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
2020-06-19 00:53:59 -05:00
|
|
|
else if ( event->type() == QEvent::KeyPress )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( static_cast<QKeyEvent*>( event )->key() != Qt::Key_Space &&
|
|
|
|
static_cast<QKeyEvent*>( event )->key() != Qt::Key_Select )
|
2015-07-29 07:19:43 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-27 06:28:13 -05:00
|
|
|
return false;
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
Qt::CheckState state = ( static_cast<Qt::CheckState>( value.toInt() ) == Qt::Checked ? Qt::Unchecked : Qt::Checked );
|
|
|
|
return model->setData( index, state, Qt::CheckStateRole );
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 01:40:28 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2018-10-03 01:40:28 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
QSize PdmUiCheckBoxDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
|
2018-10-03 01:40:28 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
return QSize( option.decorationSize.width(), option.decorationSize.height() );
|
2018-10-03 01:40:28 -05:00
|
|
|
}
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
} // end namespace caf
|