Preparations for quick access features

* Add tool button editor with callback support
* Guard null pointer usage
* Allow proxy field with no getter
* Remove duplicated code
This commit is contained in:
Magne Sjaastad 2024-10-21 14:08:15 +02:00 committed by GitHub
parent 5ffd7f3f61
commit b0d967ff40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 205 additions and 29 deletions

View File

@ -74,7 +74,8 @@ public:
}
DataType value() const
{
CAF_ASSERT( m_valueGetter );
if ( !m_valueGetter ) return DataType();
return m_valueGetter->getValue();
}

View File

@ -96,26 +96,6 @@ PdmObjectHandle* PdmReferenceHelper::objectFromReference( PdmObjectHandle* root,
return objectFromReferenceStringList( root, decodedReference );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmFieldHandle* PdmReferenceHelper::findField( PdmObjectHandle* object, const QString& fieldKeyword )
{
if ( object == nullptr ) return nullptr;
std::vector<PdmFieldHandle*> fields = object->fields();
for ( size_t i = 0; i < fields.size(); i++ )
{
if ( fields[i]->keyword() == fieldKeyword )
{
return fields[i];
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -194,7 +174,9 @@ PdmFieldHandle* PdmReferenceHelper::fieldFromReference( PdmObjectHandle* root, c
decodedReference.pop_front();
PdmObjectHandle* parentObject = objectFromReferenceStringList( root, decodedReference );
return findField( parentObject, fieldKeyword );
if ( !parentObject ) return nullptr;
return parentObject->findField( fieldKeyword );
}
//--------------------------------------------------------------------------------------------------
@ -209,9 +191,10 @@ PdmObjectHandle* PdmReferenceHelper::objectFromReferenceStringList( PdmObjectHan
int i = 0;
while ( i < reference.size() )
{
QString fieldKeyword = reference.at( i++ );
if ( !currentObject ) return nullptr;
PdmFieldHandle* fieldHandle = findField( currentObject, fieldKeyword );
QString fieldKeyword = reference.at( i++ );
auto fieldHandle = currentObject->findField( fieldKeyword );
if ( !fieldHandle )
{
return nullptr;

View File

@ -60,8 +60,6 @@ public:
private:
static QStringList referenceFromRootToObjectAsStringList( PdmObjectHandle* root, PdmObjectHandle* obj );
static PdmObjectHandle* objectFromReferenceStringList( PdmObjectHandle* root, const QStringList& reference );
static PdmFieldHandle* findField( PdmObjectHandle* object, const QString& fieldKeyword );
};
} // end namespace caf

View File

@ -37,6 +37,7 @@ set(MOC_HEADER_FILES
cafPdmUiTableViewQModel.h
cafPdmUiTextEditor.h
cafPdmUiToolButtonEditor.h
cafPdmUiToolButtonCallbackEditor.h
cafPdmUiTreeView.h
cafPdmUiTreeViewQModel.h
cafPdmUiTreeViewEditor.h
@ -100,6 +101,8 @@ set(PROJECT_FILES
cafPdmUiToolBarEditor.h
cafPdmUiToolButtonEditor.cpp
cafPdmUiToolButtonEditor.h
cafPdmUiToolButtonCallbackEditor.cpp
cafPdmUiToolButtonCallbackEditor.h
cafPdmUiTreeSelectionEditor.h
cafPdmUiTreeSelectionEditor.cpp
cafPdmUiTreeSelectionQModel.h

View File

@ -57,10 +57,12 @@ CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiPushButtonEditor );
//--------------------------------------------------------------------------------------------------
void PdmUiPushButtonEditor::configureAndUpdateUi( const QString& uiConfigName )
{
CAF_ASSERT( !m_pushButton.isNull() );
CAF_ASSERT( !m_label.isNull() );
if ( !m_pushButton ) return;
if ( m_label )
{
PdmUiFieldEditorHandle::updateLabelFromField( m_label, uiConfigName );
}
m_pushButton->setCheckable( true );
m_pushButton->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) );

View File

@ -0,0 +1,102 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2014 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 "cafPdmUiToolButtonCallbackEditor.h"
#include "cafPdmFieldHandle.h"
#include "cafPdmObjectHandle.h"
#include "cafPdmUiFieldHandle.h"
#include "cafPdmUiObjectHandle.h"
namespace caf
{
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiToolButtonCallbackEditor );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiToolButtonCallbackEditor::configureAndUpdateUi( const QString& uiConfigName )
{
CAF_ASSERT( !m_toolButton.isNull() );
if ( auto ic = uiField()->uiIcon( uiConfigName ) )
{
m_toolButton->setIcon( *ic );
}
QString buttonText = uiField()->uiName( uiConfigName );
m_toolButton->setText( buttonText );
m_toolButton->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) );
m_toolButton->setToolTip( uiField()->uiToolTip( uiConfigName ) );
if ( auto pdmUiOjectHandle = uiObj( uiField()->fieldHandle()->ownerObject() ) )
{
pdmUiOjectHandle->editorAttribute( uiField()->fieldHandle(), uiConfigName, &m_attributes );
}
m_toolButton->setCheckable( false );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiToolButtonCallbackEditor::createEditorWidget( QWidget* parent )
{
m_toolButton = new QToolButton( parent );
connect( m_toolButton, SIGNAL( clicked( bool ) ), this, SLOT( slotClicked( bool ) ) );
return m_toolButton;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiToolButtonCallbackEditor::createLabelWidget( QWidget* parent )
{
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiToolButtonCallbackEditor::slotClicked( bool checked )
{
if ( m_attributes.m_onClickedCallback )
{
m_attributes.m_onClickedCallback();
}
}
} // end namespace caf

View File

@ -0,0 +1,87 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2014 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 "cafPdmUiFieldEditorHandle.h"
#include <QPointer>
#include <QToolButton>
#include <QWidget>
#include <functional>
namespace caf
{
class PdmUiToolButtonCallbackEditorAttribute : public PdmUiEditorAttribute
{
public:
PdmUiToolButtonCallbackEditorAttribute() {}
// Usage:
// In the class having a proxyfield connected to a bool
// In defineEditorAttributes:
// attr->setCallback( std::bind( &MyClass::myFunctionToBeCalled, this ) );
//
void setCallback( std::function<void()> callback ) { m_onClickedCallback = callback; }
public:
std::function<void()> m_onClickedCallback;
};
class PdmUiToolButtonCallbackEditor : public PdmUiFieldEditorHandle
{
Q_OBJECT
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
public:
PdmUiToolButtonCallbackEditor() {}
~PdmUiToolButtonCallbackEditor() override {}
protected:
QWidget* createEditorWidget( QWidget* parent ) override;
QWidget* createLabelWidget( QWidget* parent ) override;
void configureAndUpdateUi( const QString& uiConfigName ) override;
protected slots:
void slotClicked( bool checked );
private:
QPointer<QToolButton> m_toolButton;
PdmUiToolButtonCallbackEditorAttribute m_attributes;
};
} // end namespace caf