Move setValueWithFieldChanged to a separate file

Make datavaluefield suitable for refactoring
This commit is contained in:
Magne Sjaastad 2024-04-01 16:47:01 +02:00
parent ae054c3578
commit a985ec005c
5 changed files with 76 additions and 35 deletions

View File

@ -38,6 +38,7 @@
#include "cafCmdFeatureManager.h"
#include "cafPdmField.h"
#include "cafPdmSetFieldValue.h"
#include "cafPdmUiFieldHandle.h"
#include "cafPdmUiItem.h"
#include "cafPdmUiObjectHandle.h"
@ -142,9 +143,9 @@ void ToggleItemsFeatureImpl::setObjectToggleStateForSelection( SelectionToggleTy
{
caf::PdmField<bool>* field = dynamic_cast<caf::PdmField<bool>*>( uiObjectHandleChild->objectToggleField() );
if ( state == TOGGLE_ON ) field->setValueWithFieldChanged( true );
if ( state == TOGGLE_OFF ) field->setValueWithFieldChanged( false );
if ( state == TOGGLE_SUBITEMS ) field->setValueWithFieldChanged( !( field->v() ) );
if ( state == TOGGLE_ON ) caf::setValueWithFieldChanged( field, true );
if ( state == TOGGLE_OFF ) caf::setValueWithFieldChanged( field, false );
if ( state == TOGGLE_SUBITEMS ) caf::setValueWithFieldChanged( field, !( field->v() ) );
}
}
}
@ -158,11 +159,11 @@ void ToggleItemsFeatureImpl::setObjectToggleStateForSelection( SelectionToggleTy
{
caf::PdmField<bool>* field = dynamic_cast<caf::PdmField<bool>*>( uiObjectHandle->objectToggleField() );
if ( state == TOGGLE_ON ) field->setValueWithFieldChanged( true );
if ( state == TOGGLE_OFF ) field->setValueWithFieldChanged( false );
if ( state == TOGGLE_ON ) caf::setValueWithFieldChanged( field, true );
if ( state == TOGGLE_OFF ) caf::setValueWithFieldChanged( field, false );
if ( state == TOGGLE_SUBITEMS || state == TOGGLE )
{
field->setValueWithFieldChanged( !( field->v() ) );
caf::setValueWithFieldChanged( field, !( field->v() ) );
}
}
}

View File

@ -42,6 +42,7 @@
#include "cafPdmObject.h"
#include "cafPdmObjectHandle.h"
#include "cafPdmSetFieldValue.h"
#include "cafPdmUiItem.h"
#include <QAction>
@ -82,7 +83,7 @@ void ToggleItemsOnOthersOffFeature::onActionTriggered( bool isChecked )
if ( field )
{
field->setValueWithFieldChanged( false );
caf::setValueWithFieldChanged( field, false );
}
}
@ -91,7 +92,7 @@ void ToggleItemsOnOthersOffFeature::onActionTriggered( bool isChecked )
{
caf::PdmField<bool>* field = dynamic_cast<caf::PdmField<bool>*>( selectedObject->objectToggleField() );
field->setValueWithFieldChanged( true );
caf::setValueWithFieldChanged( field, true );
}
}

View File

@ -38,6 +38,7 @@ set(PROJECT_FILES
cafPdmFieldCapability.h
cafPdmFieldHandle.cpp
cafPdmFieldHandle.h
cafPdmSetFieldValue.h
cafPdmObjectCapability.h
cafPdmFieldReorderCapability.cpp
cafPdmFieldReorderCapability.h

View File

@ -41,7 +41,6 @@
#include "cafAssert.h"
#include "cafInternalPdmValueFieldSpecializations.h"
#include "cafPdmUiFieldHandleInterface.h"
#include <QVariant>
@ -95,7 +94,6 @@ public:
CAF_ASSERT( isInitializedByInitFieldMacro() );
m_fieldValue = fieldValue;
}
void setValueWithFieldChanged( const DataType& fieldValue );
// Implementation of PdmValueField interface
@ -137,29 +135,4 @@ protected:
DataType m_defaultFieldValue;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename DataType>
void caf::PdmDataValueField<DataType>::setValueWithFieldChanged( const DataType& fieldValue )
{
CAF_ASSERT( isInitializedByInitFieldMacro() );
PdmUiFieldHandleInterface* uiFieldHandleInterface = capability<PdmUiFieldHandleInterface>();
if ( uiFieldHandleInterface )
{
QVariant oldValue = uiFieldHandleInterface->toUiBasedQVariant();
m_fieldValue = fieldValue;
QVariant newUiBasedQVariant = uiFieldHandleInterface->toUiBasedQVariant();
uiFieldHandleInterface->notifyFieldChanged( oldValue, newUiBasedQVariant );
}
else
{
m_fieldValue = fieldValue;
}
}
} // End of namespace caf

View File

@ -0,0 +1,65 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2023 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.
//
//##################################################################################################
#pragma once
#include "cafPdmFieldHandle.h"
#include "cafPdmUiFieldHandleInterface.h"
namespace caf
{
// This is a template function, and auto is used to allow the compiler to deduce the type of the arguments
void setValueWithFieldChanged( auto fieldHandle, auto fieldValue )
{
PdmUiFieldHandleInterface* uiFieldHandleInterface = fieldHandle->capability<PdmUiFieldHandleInterface>();
if ( uiFieldHandleInterface )
{
QVariant oldValue = uiFieldHandleInterface->toUiBasedQVariant();
fieldHandle->setValue( fieldValue );
QVariant newUiBasedQVariant = uiFieldHandleInterface->toUiBasedQVariant();
uiFieldHandleInterface->notifyFieldChanged( oldValue, newUiBasedQVariant );
}
else
{
fieldHandle->setValue( fieldValue );
}
}
} // end namespace caf