2015-07-29 07:19:43 -05:00
|
|
|
//##################################################################################################
|
|
|
|
//
|
|
|
|
// Custom Visualization Core library
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//##################################################################################################
|
|
|
|
|
|
|
|
#include "cafCmdAddItemExec.h"
|
|
|
|
|
|
|
|
#include "cafCmdAddItemExecData.h"
|
|
|
|
|
|
|
|
#include "cafNotificationCenter.h"
|
|
|
|
#include "cafPdmReferenceHelper.h"
|
|
|
|
#include "cafSelectionManager.h"
|
|
|
|
|
|
|
|
#include "cafPdmChildArrayField.h"
|
|
|
|
|
|
|
|
namespace caf
|
|
|
|
{
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString CmdAddItemExec::name()
|
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
PdmFieldHandle* field =
|
|
|
|
PdmReferenceHelper::fieldFromReference( m_commandData->m_rootObject, m_commandData->m_pathToField );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
QString containedObjectType = "object";
|
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
PdmChildArrayFieldHandle* listField = dynamic_cast<PdmChildArrayFieldHandle*>( field );
|
|
|
|
if ( listField )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2015-08-20 08:24:49 -05:00
|
|
|
PdmXmlFieldHandle* xfh = listField->xmlCapability();
|
2020-06-19 00:53:59 -05:00
|
|
|
containedObjectType = xfh->dataTypeName();
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
return QString( "Create new '%1'" ).arg( containedObjectType );
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void CmdAddItemExec::redo()
|
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
PdmFieldHandle* field =
|
|
|
|
PdmReferenceHelper::fieldFromReference( m_commandData->m_rootObject, m_commandData->m_pathToField );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
PdmChildArrayFieldHandle* listField = dynamic_cast<PdmChildArrayFieldHandle*>( field );
|
|
|
|
if ( listField && field->xmlCapability() )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-03-04 01:20:29 -06:00
|
|
|
QString classKeyword = field->xmlCapability()->dataTypeName();
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( classKeyword.isEmpty() ) return;
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
caf::PdmObjectHandle* obj = PdmDefaultObjectFactory::instance()->create( classKeyword );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( !obj ) return;
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
listField->insertAt( m_commandData->m_indexAfter, obj );
|
|
|
|
|
|
|
|
if ( m_commandData->m_indexAfter == -1 )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
m_commandData->m_createdItemIndex = static_cast<int>( listField->size() - 1 );
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_commandData->m_createdItemIndex = m_commandData->m_indexAfter;
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( m_notificationCenter ) m_notificationCenter->notifyObserversOfDataChange( obj );
|
2015-10-06 08:26:22 -05:00
|
|
|
|
2015-08-20 03:26:16 -05:00
|
|
|
listField->uiCapability()->updateConnectedEditors();
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( listField->ownerObject() )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
caf::PdmUiObjectHandle* ownerUiObject = uiObj( listField->ownerObject() );
|
|
|
|
if ( ownerUiObject )
|
2015-10-06 08:26:22 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
ownerUiObject->fieldChangedByUi( listField, QVariant(), QVariant() );
|
2015-10-06 08:26:22 -05:00
|
|
|
}
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void CmdAddItemExec::undo()
|
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
PdmFieldHandle* field =
|
|
|
|
PdmReferenceHelper::fieldFromReference( m_commandData->m_rootObject, m_commandData->m_pathToField );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
PdmChildArrayFieldHandle* listField = dynamic_cast<PdmChildArrayFieldHandle*>( field );
|
|
|
|
if ( listField && m_commandData->m_createdItemIndex >= 0 )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
|
|
|
std::vector<caf::PdmObjectHandle*> children;
|
2020-06-19 00:53:59 -05:00
|
|
|
listField->childObjects( &children );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
caf::PdmObjectHandle* obj = children[m_commandData->m_createdItemIndex];
|
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
caf::SelectionManager::instance()->removeObjectFromAllSelections( obj );
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
listField->erase( m_commandData->m_createdItemIndex );
|
2015-08-26 07:32:49 -05:00
|
|
|
listField->uiCapability()->updateConnectedEditors();
|
2015-07-29 07:19:43 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( m_notificationCenter ) m_notificationCenter->notifyObservers();
|
2015-10-06 08:26:22 -05:00
|
|
|
|
2020-06-19 00:53:59 -05:00
|
|
|
if ( listField->ownerObject() )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
caf::PdmUiObjectHandle* ownerUiObject = uiObj( listField->ownerObject() );
|
|
|
|
if ( ownerUiObject )
|
2015-10-06 08:26:22 -05:00
|
|
|
{
|
2020-06-19 00:53:59 -05:00
|
|
|
ownerUiObject->fieldChangedByUi( listField, QVariant(), QVariant() );
|
2015-10-06 08:26:22 -05:00
|
|
|
}
|
2015-07-29 07:19:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
delete obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
CmdAddItemExec::CmdAddItemExec( NotificationCenter* notificationCenter )
|
|
|
|
: CmdExecuteCommand( notificationCenter )
|
2015-07-29 07:19:43 -05:00
|
|
|
{
|
|
|
|
m_commandData = new CmdAddItemExecData;
|
|
|
|
}
|
|
|
|
|
2015-08-13 04:48:35 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-08-13 04:48:35 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
CmdAddItemExec::~CmdAddItemExec()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-06-19 00:53:59 -05:00
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
CmdAddItemExecData* CmdAddItemExec::commandData()
|
|
|
|
{
|
|
|
|
return m_commandData;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace caf
|