2013-09-02 08:14:29 +02:00
|
|
|
//##################################################################################################
|
|
|
|
|
//
|
|
|
|
|
// Custom Visualization Core library
|
2013-09-20 15:22:29 +02:00
|
|
|
// Copyright (C) 2011-2013 Ceetron 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.
|
2013-09-02 08:14:29 +02:00
|
|
|
//
|
|
|
|
|
//##################################################################################################
|
|
|
|
|
|
|
|
|
|
#include "cafPdmUiTreeViewEditor.h"
|
|
|
|
|
|
2015-07-29 14:19:43 +02:00
|
|
|
#include "cafPdmChildArrayField.h"
|
2013-09-02 08:14:29 +02:00
|
|
|
#include "cafPdmField.h"
|
2015-07-29 14:19:43 +02:00
|
|
|
#include "cafPdmObject.h"
|
|
|
|
|
#include "cafPdmUiCommandSystemProxy.h"
|
2015-10-23 11:03:23 +02:00
|
|
|
#include "cafPdmUiDragDropInterface.h"
|
|
|
|
|
#include "cafPdmUiEditorHandle.h"
|
2015-07-29 14:19:43 +02:00
|
|
|
#include "cafPdmUiTreeOrdering.h"
|
2018-06-11 11:16:35 +02:00
|
|
|
#include "cafPdmUiTreeViewQModel.h"
|
2015-07-29 14:19:43 +02:00
|
|
|
#include "cafSelectionManager.h"
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2015-10-23 11:03:23 +02:00
|
|
|
#include <QDragMoveEvent>
|
|
|
|
|
#include <QEvent>
|
2013-09-02 08:14:29 +02:00
|
|
|
#include <QGridLayout>
|
2015-07-29 14:19:43 +02:00
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QModelIndexList>
|
2019-11-04 15:26:03 +01:00
|
|
|
#include <QPainter>
|
2013-09-26 10:57:35 +02:00
|
|
|
#include <QSortFilterProxyModel>
|
2019-11-04 15:26:03 +01:00
|
|
|
#include <QStyleOptionViewItem>
|
2015-07-29 14:19:43 +02:00
|
|
|
#include <QTreeView>
|
|
|
|
|
#include <QWidget>
|
2013-09-02 08:14:29 +02:00
|
|
|
|
|
|
|
|
namespace caf
|
|
|
|
|
{
|
2015-10-23 11:03:23 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-10-23 11:03:23 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
class PdmUiTreeViewWidget : public QTreeView
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-06-19 07:53:59 +02:00
|
|
|
explicit PdmUiTreeViewWidget( QWidget* parent = nullptr )
|
|
|
|
|
: QTreeView( parent ){};
|
|
|
|
|
~PdmUiTreeViewWidget() override{};
|
2015-10-23 11:03:23 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
bool isTreeItemEditWidgetActive() const { return state() == QAbstractItemView::EditingState; }
|
2016-07-25 18:25:14 +02:00
|
|
|
|
2015-10-23 11:03:23 +02:00
|
|
|
protected:
|
2020-06-19 07:53:59 +02:00
|
|
|
void dragMoveEvent( QDragMoveEvent* event ) override
|
2015-10-23 11:03:23 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
caf::PdmUiTreeViewQModel* treeViewModel = dynamic_cast<caf::PdmUiTreeViewQModel*>( model() );
|
|
|
|
|
if ( treeViewModel && treeViewModel->dragDropInterface() )
|
2015-10-23 11:03:23 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
treeViewModel->dragDropInterface()->onProposedDropActionUpdated( event->proposedAction() );
|
2015-10-23 11:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
QTreeView::dragMoveEvent( event );
|
2015-10-23 11:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
void dragLeaveEvent( QDragLeaveEvent* event ) override
|
2015-10-23 11:03:23 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
caf::PdmUiTreeViewQModel* treeViewModel = dynamic_cast<caf::PdmUiTreeViewQModel*>( model() );
|
|
|
|
|
if ( treeViewModel && treeViewModel->dragDropInterface() )
|
2015-10-23 11:03:23 +02:00
|
|
|
{
|
|
|
|
|
treeViewModel->dragDropInterface()->onDragCanceled();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
QTreeView::dragLeaveEvent( event );
|
2015-10-23 11:03:23 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-02 08:14:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2013-09-02 08:14:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
PdmUiTreeViewEditor::PdmUiTreeViewEditor()
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_useDefaultContextMenu = false;
|
|
|
|
|
m_updateSelectionManager = false;
|
2015-09-18 19:24:02 +02:00
|
|
|
m_appendClassNameToUiItemText = false;
|
2020-06-19 07:53:59 +02:00
|
|
|
m_layout = nullptr;
|
|
|
|
|
m_treeView = nullptr;
|
|
|
|
|
m_treeViewModel = nullptr;
|
|
|
|
|
m_delegate = nullptr;
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2013-09-02 08:14:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
PdmUiTreeViewEditor::~PdmUiTreeViewEditor()
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeViewModel->setPdmItemRoot( nullptr );
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2013-09-02 08:14:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
QWidget* PdmUiTreeViewEditor::createWidget( QWidget* parent )
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_mainWidget = new QWidget( parent );
|
2013-09-05 07:14:39 +02:00
|
|
|
m_layout = new QVBoxLayout();
|
2020-06-19 07:53:59 +02:00
|
|
|
m_layout->setContentsMargins( 0, 0, 0, 0 );
|
|
|
|
|
m_mainWidget->setLayout( m_layout );
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeViewModel = new caf::PdmUiTreeViewQModel( this );
|
|
|
|
|
m_treeView = new PdmUiTreeViewWidget( m_mainWidget );
|
|
|
|
|
m_treeView->setModel( m_treeViewModel );
|
|
|
|
|
m_treeView->installEventFilter( this );
|
2013-09-26 10:57:35 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
m_delegate = new PdmUiTreeViewItemDelegate( m_treeView, m_treeViewModel );
|
2019-11-04 15:26:03 +01:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeView->setItemDelegate( m_delegate );
|
2019-11-04 15:26:03 +01:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
connect( treeView()->selectionModel(),
|
|
|
|
|
SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
|
|
|
|
|
SLOT( slotOnSelectionChanged( const QItemSelection&, const QItemSelection& ) ) );
|
2015-08-13 23:45:34 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
m_layout->addWidget( m_treeView );
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2015-07-29 14:19:43 +02:00
|
|
|
updateContextMenuSignals();
|
|
|
|
|
|
2013-09-02 08:14:29 +02:00
|
|
|
return m_mainWidget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2015-07-29 14:19:43 +02:00
|
|
|
///
|
2013-09-02 08:14:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::configureAndUpdateUi( const QString& uiConfigName )
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2017-09-27 08:08:53 +02:00
|
|
|
PdmUiTreeViewEditorAttribute editorAttributes;
|
2013-09-08 08:56:38 +02:00
|
|
|
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiObjectHandle* uiObjectHandle = dynamic_cast<PdmUiObjectHandle*>( this->pdmItemRoot() );
|
|
|
|
|
if ( uiObjectHandle )
|
2017-09-27 08:08:53 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
uiObjectHandle->objectEditorAttribute( uiConfigName, &editorAttributes );
|
2017-09-27 08:08:53 +02:00
|
|
|
}
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeViewModel->setColumnHeaders( editorAttributes.columnHeaders );
|
|
|
|
|
m_treeViewModel->setUiConfigName( uiConfigName );
|
|
|
|
|
m_treeViewModel->setPdmItemRoot( this->pdmItemRoot() );
|
2015-07-29 14:19:43 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( editorAttributes.currentObject )
|
2017-09-27 08:08:53 +02:00
|
|
|
{
|
|
|
|
|
PdmUiObjectHandle* uiObjectHandle = editorAttributes.currentObject->uiCapability();
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( uiObjectHandle )
|
2017-09-27 08:08:53 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
selectAsCurrentItem( uiObjectHandle );
|
2017-09-27 08:08:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-04 15:26:03 +01:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( m_delegate )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
|
|
|
|
m_delegate->clearAttributes();
|
|
|
|
|
updateItemDelegateForSubTree();
|
|
|
|
|
}
|
2015-07-29 14:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-07-29 14:19:43 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
QTreeView* PdmUiTreeViewEditor::treeView()
|
|
|
|
|
{
|
|
|
|
|
return m_treeView;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 18:25:14 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2016-07-25 18:25:14 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
bool PdmUiTreeViewEditor::isTreeItemEditWidgetActive() const
|
|
|
|
|
{
|
|
|
|
|
return m_treeView->isTreeItemEditWidgetActive();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
void PdmUiTreeViewEditor::selectedUiItems( std::vector<PdmUiItem*>& objects )
|
2015-07-29 14:19:43 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( !this->treeView() ) return;
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2015-08-13 23:45:34 +02:00
|
|
|
QModelIndexList idxList = this->treeView()->selectionModel()->selectedIndexes();
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
for ( int i = 0; i < idxList.size(); i++ )
|
2015-08-13 23:45:34 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
caf::PdmUiItem* item = this->m_treeViewModel->uiItemFromModelIndex( idxList[i] );
|
|
|
|
|
if ( item )
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
objects.push_back( item );
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-07-29 14:19:43 +02:00
|
|
|
}
|
2013-09-02 08:14:29 +02:00
|
|
|
|
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
|
|
|
void PdmUiTreeViewEditor::updateMySubTree( PdmUiItem* uiItem )
|
2015-07-29 14:19:43 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( m_treeViewModel )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeViewModel->updateSubTree( uiItem );
|
|
|
|
|
QModelIndex index = m_treeViewModel->findModelIndex( uiItem );
|
|
|
|
|
updateItemDelegateForSubTree( index );
|
2019-11-04 15:26:03 +01:00
|
|
|
}
|
2015-07-29 14:19:43 +02:00
|
|
|
}
|
2013-09-08 08:56:38 +02:00
|
|
|
|
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
|
|
|
void PdmUiTreeViewEditor::enableDefaultContextMenu( bool enable )
|
2015-07-29 14:19:43 +02:00
|
|
|
{
|
|
|
|
|
m_useDefaultContextMenu = enable;
|
|
|
|
|
|
|
|
|
|
updateContextMenuSignals();
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2013-09-02 08:14:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::enableSelectionManagerUpdating( bool enable )
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2015-08-13 23:45:34 +02:00
|
|
|
m_updateSelectionManager = enable;
|
2015-07-29 14:19:43 +02:00
|
|
|
}
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2015-07-29 14:19:43 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-07-29 14:19:43 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
void PdmUiTreeViewEditor::updateContextMenuSignals()
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( !m_treeView ) return;
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( m_useDefaultContextMenu )
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeView->setContextMenuPolicy( Qt::CustomContextMenu );
|
|
|
|
|
connect( m_treeView, SIGNAL( customContextMenuRequested( QPoint ) ), SLOT( customMenuRequested( QPoint ) ) );
|
2015-07-29 14:19:43 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeView->setContextMenuPolicy( Qt::DefaultContextMenu );
|
|
|
|
|
disconnect( m_treeView, nullptr, this, nullptr );
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2013-09-02 08:14:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::customMenuRequested( QPoint pos )
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2015-08-13 23:45:34 +02:00
|
|
|
// This seems a bit strange. Why ?
|
2020-06-19 07:53:59 +02:00
|
|
|
SelectionManager::instance()->setActiveChildArrayFieldHandle( this->currentChildArrayFieldHandle() );
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2015-07-29 14:19:43 +02:00
|
|
|
QMenu menu;
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiCommandSystemProxy::instance()->setCurrentContextMenuTargetWidget( m_mainWidget->parentWidget() );
|
2019-08-29 11:48:43 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
caf::PdmUiCommandSystemProxy::instance()->populateMenuWithDefaultCommands( "PdmUiTreeViewEditor", &menu );
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( menu.actions().size() > 0 )
|
2013-09-02 08:14:29 +02:00
|
|
|
{
|
2015-07-29 14:19:43 +02:00
|
|
|
// Qt doc: QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport().
|
2020-06-19 07:53:59 +02:00
|
|
|
QPoint globalPos = m_treeView->viewport()->mapToGlobal( pos );
|
2015-07-29 14:19:43 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
menu.exec( globalPos );
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
2019-08-29 11:48:43 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiCommandSystemProxy::instance()->setCurrentContextMenuTargetWidget( nullptr );
|
2013-09-02 08:14:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 14:19:43 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-07-29 14:19:43 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
PdmChildArrayFieldHandle* PdmUiTreeViewEditor::currentChildArrayFieldHandle()
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiItem* currentSelectedItem = SelectionManager::instance()->selectedItem( SelectionManager::FIRST_LEVEL );
|
2015-07-29 14:19:43 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiFieldHandle* uiFieldHandle = dynamic_cast<PdmUiFieldHandle*>( currentSelectedItem );
|
|
|
|
|
if ( uiFieldHandle )
|
2015-07-29 14:19:43 +02:00
|
|
|
{
|
|
|
|
|
PdmFieldHandle* fieldHandle = uiFieldHandle->fieldHandle();
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( dynamic_cast<PdmChildArrayFieldHandle*>( fieldHandle ) )
|
2015-07-29 14:19:43 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
return dynamic_cast<PdmChildArrayFieldHandle*>( fieldHandle );
|
2015-07-29 14:19:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmObjectHandle* pdmObject = dynamic_cast<caf::PdmObjectHandle*>( currentSelectedItem );
|
|
|
|
|
if ( pdmObject )
|
2015-07-29 14:19:43 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmChildArrayFieldHandle* parentChildArray = dynamic_cast<PdmChildArrayFieldHandle*>( pdmObject->parentField() );
|
2015-07-29 14:19:43 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( parentChildArray )
|
2015-07-29 14:19:43 +02:00
|
|
|
{
|
|
|
|
|
return parentChildArray;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 18:05:08 +01:00
|
|
|
return nullptr;
|
2015-07-29 14:19:43 +02:00
|
|
|
}
|
2013-09-02 08:14:29 +02:00
|
|
|
|
2015-08-13 23:45:34 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-13 23:45:34 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::selectAsCurrentItem( const PdmUiItem* uiItem )
|
2015-08-13 23:45:34 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
QModelIndex index = m_treeViewModel->findModelIndex( uiItem );
|
2019-10-11 15:54:19 +02:00
|
|
|
QModelIndex currentIndex = m_treeView->currentIndex();
|
|
|
|
|
|
|
|
|
|
m_treeView->clearSelection();
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeView->setCurrentIndex( index );
|
2015-08-13 23:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-11 15:54:19 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::selectItems( std::vector<const PdmUiItem*> uiItems )
|
2019-10-11 15:54:19 +02:00
|
|
|
{
|
|
|
|
|
m_treeView->clearSelection();
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( uiItems.empty() )
|
2019-10-11 15:54:19 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
QModelIndex index = findModelIndex( uiItems.back() );
|
|
|
|
|
m_treeView->setCurrentIndex( index );
|
2019-10-11 15:54:19 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
for ( const PdmUiItem* uiItem : uiItems )
|
2019-10-11 15:54:19 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
QModelIndex itemIndex = findModelIndex( uiItem );
|
|
|
|
|
m_treeView->selectionModel()->select( itemIndex, QItemSelectionModel::Select );
|
2019-10-11 15:54:19 +02:00
|
|
|
}
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeView->setFocus( Qt::MouseFocusReason );
|
2019-10-11 15:54:19 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:45:34 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-13 23:45:34 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::slotOnSelectionChanged( const QItemSelection& selected, const QItemSelection& deselected )
|
2015-08-13 23:45:34 +02:00
|
|
|
{
|
2015-08-20 14:38:51 +02:00
|
|
|
this->updateSelectionManager();
|
2015-08-13 23:45:34 +02:00
|
|
|
|
|
|
|
|
emit selectionChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-14 18:03:51 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-14 18:03:51 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::setExpanded( const PdmUiItem* uiItem, bool doExpand ) const
|
2015-08-14 18:03:51 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
QModelIndex index = m_treeViewModel->findModelIndex( uiItem );
|
|
|
|
|
m_treeView->setExpanded( index, doExpand );
|
2019-08-20 10:35:30 +02:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( doExpand )
|
2019-08-20 10:35:30 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeView->scrollTo( index );
|
2019-08-20 10:35:30 +02:00
|
|
|
}
|
2015-08-14 18:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 09:57:57 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-16 09:57:57 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiItem* PdmUiTreeViewEditor::uiItemFromModelIndex( const QModelIndex& index ) const
|
2015-08-16 09:57:57 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
return m_treeViewModel->uiItemFromModelIndex( index );
|
2015-08-16 09:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-16 09:57:57 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
QModelIndex PdmUiTreeViewEditor::findModelIndex( const PdmUiItem* object ) const
|
2015-08-16 09:57:57 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
return m_treeViewModel->findModelIndex( object );
|
2015-08-16 09:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-18 11:23:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-18 11:23:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::setDragDropInterface( PdmUiDragDropInterface* dragDropInterface )
|
2015-08-18 11:23:53 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_treeViewModel->setDragDropInterface( dragDropInterface );
|
2015-08-18 11:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-20 14:38:51 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-20 14:38:51 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
bool PdmUiTreeViewEditor::eventFilter( QObject* obj, QEvent* event )
|
2015-08-20 14:38:51 +02:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( event->type() == QEvent::FocusIn )
|
2015-08-20 14:38:51 +02:00
|
|
|
{
|
|
|
|
|
this->updateSelectionManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// standard event processing
|
2020-06-19 07:53:59 +02:00
|
|
|
return QObject::eventFilter( obj, event );
|
2015-08-20 14:38:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-08-20 14:38:51 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
void PdmUiTreeViewEditor::updateSelectionManager()
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( m_updateSelectionManager )
|
2015-08-20 14:38:51 +02:00
|
|
|
{
|
|
|
|
|
std::vector<PdmUiItem*> items;
|
2020-06-19 07:53:59 +02:00
|
|
|
this->selectedUiItems( items );
|
|
|
|
|
SelectionManager::instance()->setSelectedItems( items );
|
2015-08-20 14:38:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 15:26:03 +01:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::updateItemDelegateForSubTree( const QModelIndex& modelIndex /*= QModelIndex()*/ )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
|
|
|
|
auto allIndices = m_treeViewModel->allIndicesRecursive();
|
2020-06-19 07:53:59 +02:00
|
|
|
for ( QModelIndex index : allIndices )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiItem* uiItem = m_treeViewModel->uiItemFromModelIndex( index );
|
|
|
|
|
PdmUiObjectHandle* uiObjectHandle = dynamic_cast<PdmUiObjectHandle*>( uiItem );
|
|
|
|
|
if ( uiObjectHandle )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
|
|
|
|
PdmUiTreeViewItemAttribute attribute;
|
2020-06-19 07:53:59 +02:00
|
|
|
uiObjectHandle->objectEditorAttribute( "", &attribute );
|
|
|
|
|
if ( !attribute.tag.isEmpty() )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
m_delegate->addAttribute( index, attribute );
|
2019-11-04 15:26:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-18 19:24:02 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-09-18 19:24:02 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewEditor::enableAppendOfClassNameToUiItemText( bool enable )
|
2015-09-18 19:24:02 +02:00
|
|
|
{
|
|
|
|
|
m_appendClassNameToUiItemText = enable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
///
|
2015-09-18 19:24:02 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
bool PdmUiTreeViewEditor::isAppendOfClassNameToUiItemTextEnabled()
|
|
|
|
|
{
|
|
|
|
|
return m_appendClassNameToUiItemText;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 15:26:03 +01:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
PdmUiTreeViewItemDelegate::PdmUiTreeViewItemDelegate( QObject* parent, PdmUiTreeViewQModel* model )
|
|
|
|
|
: QStyledItemDelegate( parent )
|
|
|
|
|
, m_model( model )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
void PdmUiTreeViewItemDelegate::clearAttributes()
|
|
|
|
|
{
|
|
|
|
|
m_attributes.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewItemDelegate::addAttribute( QModelIndex index, const PdmUiTreeViewItemAttribute& attribute )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
|
|
|
|
m_attributes[index] = attribute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 07:53:59 +02:00
|
|
|
void PdmUiTreeViewItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
QStyledItemDelegate::paint( painter, option, index );
|
2019-11-04 15:26:03 +01:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
auto it = m_attributes.find( index );
|
|
|
|
|
if ( it == m_attributes.end() ) return;
|
2019-11-04 15:26:03 +01:00
|
|
|
|
2019-11-06 10:36:05 +01:00
|
|
|
// Save painter so we can restore it
|
|
|
|
|
painter->save();
|
|
|
|
|
|
2019-11-04 15:26:03 +01:00
|
|
|
const int insideTopBottomMargins = 1;
|
|
|
|
|
const int insideleftRightMargins = 6;
|
|
|
|
|
const int outsideLeftRightMargins = 4;
|
|
|
|
|
|
|
|
|
|
QFont font = QApplication::font();
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( font.pixelSize() > 0 )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
font.setPixelSize( std::max( 1, font.pixelSize() - 1 ) );
|
2019-11-04 15:26:03 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
font.setPointSize( std::max( 1, font.pointSize() - 1 ) );
|
2019-11-04 15:26:03 +01:00
|
|
|
}
|
2020-06-19 07:53:59 +02:00
|
|
|
painter->setFont( font );
|
|
|
|
|
|
|
|
|
|
QString text = it->second.tag;
|
|
|
|
|
QColor bgColor = it->second.bgColor;
|
|
|
|
|
QColor fgColor = it->second.fgColor;
|
|
|
|
|
|
|
|
|
|
QSize textSize( QFontMetrics( font ).size( Qt::TextSingleLine, text ) );
|
|
|
|
|
QRect rect = option.rect;
|
2019-11-04 15:26:03 +01:00
|
|
|
QSize fullSize = rect.size();
|
2020-06-19 07:53:59 +02:00
|
|
|
int textDiff = ( fullSize.height() - textSize.height() );
|
2019-11-04 15:26:03 +01:00
|
|
|
|
|
|
|
|
QRect textRect;
|
2020-06-19 07:53:59 +02:00
|
|
|
if ( it->second.position == PdmUiTreeViewItemAttribute::AT_END )
|
2019-11-04 15:26:03 +01:00
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
QPoint bottomRight = rect.bottomRight() - QPoint( outsideLeftRightMargins, 0 );
|
|
|
|
|
QPoint textBottomRight = bottomRight - QPoint( insideleftRightMargins, textDiff / 2 );
|
|
|
|
|
QPoint textTopLeft = textBottomRight - QPoint( textSize.width(), textSize.height() );
|
|
|
|
|
textRect = QRect( textTopLeft, textBottomRight );
|
2019-11-04 15:26:03 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-19 07:53:59 +02:00
|
|
|
QPoint textTopLeft = QPoint( 0, rect.topLeft().y() ) +
|
|
|
|
|
QPoint( outsideLeftRightMargins + insideleftRightMargins, +textDiff / 2 );
|
|
|
|
|
QPoint textBottomRight = textTopLeft + QPoint( textSize.width(), textSize.height() );
|
|
|
|
|
textRect = QRect( textTopLeft, textBottomRight );
|
2019-11-04 15:26:03 +01:00
|
|
|
}
|
2020-06-19 07:53:59 +02:00
|
|
|
QRect tagRect = textRect.marginsAdded(
|
|
|
|
|
QMargins( insideleftRightMargins, insideTopBottomMargins, insideleftRightMargins, insideTopBottomMargins ) );
|
2019-11-04 15:26:03 +01:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
QBrush brush( bgColor );
|
2019-11-04 15:26:03 +01:00
|
|
|
|
2020-06-19 07:53:59 +02:00
|
|
|
painter->setBrush( brush );
|
|
|
|
|
painter->setPen( bgColor );
|
|
|
|
|
painter->setRenderHint( QPainter::Antialiasing );
|
2019-11-04 15:26:03 +01:00
|
|
|
const double xRoundingRadiusPercent = 50.0;
|
|
|
|
|
const double yRoundingRadiusPercent = 25.0;
|
2020-06-19 07:53:59 +02:00
|
|
|
painter->drawRoundedRect( tagRect, xRoundingRadiusPercent, yRoundingRadiusPercent, Qt::RelativeSize );
|
|
|
|
|
|
|
|
|
|
painter->setPen( fgColor );
|
|
|
|
|
painter->drawText( textRect, Qt::AlignCenter, text );
|
2019-11-04 15:26:03 +01:00
|
|
|
|
|
|
|
|
// Restore painter
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-02 08:14:29 +02:00
|
|
|
} // end namespace caf
|