Files

153 lines
6.2 KiB
C++
Raw Permalink Normal View History

2012-06-26 16:10:41 +02:00
//##################################################################################################
//
// Custom Visualization Core library
2015-07-29 14:19:43 +02:00
// 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.
2012-06-26 16:10:41 +02:00
//
//##################################################################################################
2015-07-29 14:19:43 +02:00
#include "cafPdmUiTableViewDelegate.h"
2012-06-26 16:10:41 +02:00
#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmUiTableViewQModel.h"
2015-07-29 14:19:43 +02:00
#include <QComboBox>
2012-06-26 16:10:41 +02:00
namespace caf
{
2015-07-29 14:19:43 +02:00
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
2015-07-29 14:19:43 +02:00
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
PdmUiTableViewDelegate::PdmUiTableViewDelegate( QObject* parent, PdmUiTableViewQModel* model )
: QStyledItemDelegate( parent )
, m_model( model )
2015-07-29 14:19:43 +02:00
{
m_activeEditorCount = 0;
m_openComboBoxPopupOnEdit = false;
2015-07-29 14:19:43 +02:00
}
2012-06-26 16:10:41 +02:00
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
2012-06-26 16:10:41 +02:00
//--------------------------------------------------------------------------------------------------
2015-07-29 14:19:43 +02:00
PdmUiTableViewDelegate::~PdmUiTableViewDelegate()
2012-06-26 16:10:41 +02:00
{
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
2012-06-26 16:10:41 +02:00
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
QWidget* PdmUiTableViewDelegate::createEditor( QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index ) const
2012-06-26 16:10:41 +02:00
{
2020-06-19 07:53:59 +02:00
CAF_ASSERT( m_model );
QWidget* editorWidget = m_model->getEditorWidgetAndTransferOwnership( parent, index );
2015-07-29 14:19:43 +02:00
2020-06-19 07:53:59 +02:00
connect( editorWidget, SIGNAL( destroyed( QObject* ) ), SLOT( slotEditorDestroyed( QObject* ) ) );
2015-07-29 14:19:43 +02:00
m_activeEditorCount++;
return editorWidget;
2012-06-26 16:10:41 +02:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
2012-06-26 16:10:41 +02:00
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
void PdmUiTableViewDelegate::setEditorData( QWidget* editor, const QModelIndex& index ) const
2012-06-26 16:10:41 +02:00
{
2020-06-19 07:53:59 +02:00
PdmUiFieldEditorHandle* fieldHandle = m_model->getEditor( index );
if ( fieldHandle )
2015-07-29 14:19:43 +02:00
{
fieldHandle->updateUi();
}
if ( m_openComboBoxPopupOnEdit )
{
if ( auto* comboBox = qobject_cast<QComboBox*>( editor ) )
{
// Defer until the editor is shown and its geometry is set, otherwise the popup may be misplaced.
QMetaObject::invokeMethod( comboBox, "showPopup", Qt::QueuedConnection );
}
}
2012-06-26 16:10:41 +02:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
2012-06-26 16:10:41 +02:00
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
void PdmUiTableViewDelegate::updateEditorGeometry( QWidget* editor,
const QStyleOptionViewItem& option,
const QModelIndex& index ) const
2012-06-26 16:10:41 +02:00
{
2020-06-19 07:53:59 +02:00
editor->setGeometry( option.rect );
2012-06-26 16:10:41 +02:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
2012-06-26 16:10:41 +02:00
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
void PdmUiTableViewDelegate::slotEditorDestroyed( QObject* obj )
2012-06-26 16:10:41 +02:00
{
2015-07-29 14:19:43 +02:00
m_activeEditorCount--;
2012-06-26 16:10:41 +02:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
2012-06-26 16:10:41 +02:00
//--------------------------------------------------------------------------------------------------
2015-07-29 14:19:43 +02:00
bool PdmUiTableViewDelegate::isEditorOpen() const
2012-06-26 16:10:41 +02:00
{
2015-07-29 14:19:43 +02:00
return m_activeEditorCount > 0;
2012-06-26 16:10:41 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableViewDelegate::setOpenComboBoxPopupOnEdit( bool enable )
{
m_openComboBoxPopupOnEdit = enable;
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
void PdmUiTableViewDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
2020-06-19 07:53:59 +02:00
QStyleOptionViewItem viewItemOption( option );
2020-06-19 07:53:59 +02:00
QVariant fgText = index.data( Qt::ForegroundRole );
2020-06-19 07:53:59 +02:00
if ( fgText.canConvert<QColor>() )
{
viewItemOption.palette.setColor( QPalette::Active, QPalette::HighlightedText, qvariant_cast<QColor>( fgText ) );
viewItemOption.palette.setColor( QPalette::Inactive, QPalette::HighlightedText, qvariant_cast<QColor>( fgText ) );
}
2020-06-19 07:53:59 +02:00
this->QStyledItemDelegate::paint( painter, viewItemOption, index );
}
2012-06-26 16:10:41 +02:00
2015-07-29 14:19:43 +02:00
} // end namespace caf