ResInsight/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxDelegate.cpp

138 lines
5.7 KiB
C++
Raw Permalink Normal View History

//##################################################################################################
//
// 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
///
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
PdmUiCheckBoxDelegate::PdmUiCheckBoxDelegate( QObject* pParent )
: QStyledItemDelegate( pParent )
{
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
//--------------------------------------------------------------------------------------------------
PdmUiCheckBoxDelegate::~PdmUiCheckBoxDelegate()
{
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
QRect adjustedPaintRect( const QStyleOptionViewItem& option )
{
2020-06-19 00:53:59 -05:00
const int margin = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameHMargin ) + 1;
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() ) );
return newRect;
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
void PdmUiCheckBoxDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
2020-06-19 00:53:59 -05:00
QStyleOptionViewItem viewItemOption( option );
2020-06-19 00:53:59 -05:00
viewItemOption.rect = adjustedPaintRect( option );
2020-06-19 00:53:59 -05:00
QStyledItemDelegate::paint( painter, viewItemOption, index );
}
//--------------------------------------------------------------------------------------------------
/// 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 )
{
2020-06-19 00:53:59 -05:00
Q_ASSERT( event );
Q_ASSERT( model );
// 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;
// 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;
// make sure that we have the right event type
2020-06-19 00:53:59 -05:00
if ( event->type() == QEvent::MouseButtonRelease )
{
2020-06-19 00:53:59 -05:00
QRect paintRect = adjustedPaintRect( option );
QRect checkRect = QStyle::alignedRect( option.direction, Qt::AlignCenter, option.decorationSize, paintRect );
2020-06-19 00:53:59 -05:00
if ( !checkRect.contains( static_cast<QMouseEvent*>( event )->pos() ) ) return true;
}
2020-06-19 00:53:59 -05:00
else if ( event->type() == QEvent::KeyPress )
{
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 )
return true;
}
else
{
return false;
}
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 );
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
QSize PdmUiCheckBoxDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
2020-06-19 00:53:59 -05:00
return QSize( option.decorationSize.width(), option.decorationSize.height() );
}
} // end namespace caf