#7636 Native CheckBox : Simplify use of native checkbox

This commit is contained in:
Magne Sjaastad 2021-04-29 08:27:48 +02:00
parent e36420163a
commit fa0ca687cd
2 changed files with 83 additions and 4 deletions

View File

@ -58,8 +58,8 @@ void PdmUiCheckBoxEditor::configureAndUpdateUi( const QString& uiConfigName )
CAF_ASSERT( !m_checkBox.isNull() );
CAF_ASSERT( !m_label.isNull() );
PdmUiCheckBoxEditorAttribute attributes;
caf::PdmUiObjectHandle* uiObject = uiObj( uiField()->fieldHandle()->ownerObject() );
PdmUiCheckBoxEditorAttribute attributes = defaultAttributes();
caf::PdmUiObjectHandle* uiObject = uiObj( uiField()->fieldHandle()->ownerObject() );
if ( uiObject )
{
uiObject->editorAttribute( uiField()->fieldHandle(), uiConfigName, &attributes );
@ -112,4 +112,50 @@ void PdmUiCheckBoxEditor::slotClicked( bool checked )
this->setValueToField( v );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiCheckBoxEditorAttribute PdmUiCheckBoxEditor::defaultAttributes() const
{
return {};
}
//==================================================================================================
//
//
// PdmUiNativeCheckBoxEditor
//
//
//==================================================================================================
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiNativeCheckBoxEditor );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiNativeCheckBoxEditor::configureFieldForEditor( caf::PdmFieldHandle* fieldHandle )
{
if ( !fieldHandle ) return;
if ( auto uiCap = fieldHandle->uiCapability() )
{
uiCap->setUiEditorTypeName( caf::PdmUiNativeCheckBoxEditor::uiEditorTypeName() );
// Hide the editor label, as the label is managed by the native checkbox
uiCap->setUiLabelPosition( caf::PdmUiItemInfo::HIDDEN );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiCheckBoxEditorAttribute PdmUiNativeCheckBoxEditor::defaultAttributes() const
{
PdmUiCheckBoxEditorAttribute attributes;
attributes.m_useNativeCheckBoxLabel = true;
return attributes;
}
} // end namespace caf

View File

@ -35,6 +35,7 @@
//##################################################################################################
#pragma once
#include "cafPdmUiFieldEditorHandle.h"
#include <QCheckBox>
@ -45,9 +46,8 @@
namespace caf
{
//==================================================================================================
/// The default editor for several PdmFields.
///
//==================================================================================================
class PdmUiCheckBoxEditorAttribute : public PdmUiEditorAttribute
{
public:
@ -57,6 +57,14 @@ public:
bool m_useNativeCheckBoxLabel;
};
//==================================================================================================
//
// Checkbox editor used to display default Qt checkbox
// On Windows, the default behavior is like this
//
// "some text as label" [x]
//
//==================================================================================================
class PdmUiCheckBoxEditor : public PdmUiFieldEditorHandle
{
Q_OBJECT
@ -74,9 +82,34 @@ protected:
protected slots:
void slotClicked( bool checked );
virtual PdmUiCheckBoxEditorAttribute defaultAttributes() const;
private:
QPointer<QCheckBox> m_checkBox;
QPointer<QShortenedLabel> m_label;
};
//==================================================================================================
//
// Check box editor used to display native checkbox
// On Windows, the default behavior to show the checkbox to the left of the label text
//
// [x] "some text as label"
//
//==================================================================================================
class PdmUiNativeCheckBoxEditor : public PdmUiCheckBoxEditor
{
Q_OBJECT
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
public:
PdmUiNativeCheckBoxEditor() = default;
~PdmUiNativeCheckBoxEditor() override = default;
static void configureFieldForEditor( caf::PdmFieldHandle* fieldHandle );
protected:
PdmUiCheckBoxEditorAttribute defaultAttributes() const override;
};
} // end namespace caf