Implement new wxPGProperty::HasAnyChild() method

There are many checks in the code whether wxPGProperty has any child.
Doing so by checking if GetChildCount() is greater than zero doesn't
seem to express intent simply and clearly. With dedicated method this
could be done simpler and hopefully in a more optimal way.
This commit is contained in:
Artur Wieczorek 2022-12-03 17:10:55 +02:00
parent d0d43165c9
commit 1d85e22543
12 changed files with 69 additions and 56 deletions

View file

@ -1559,7 +1559,7 @@ public:
// Returns true if property has visible children.
bool IsExpanded() const
{ return (!(m_flags & wxPG_PROP_COLLAPSED) && GetChildCount() > 0); }
{ return (!(m_flags & wxPG_PROP_COLLAPSED) && HasAnyChild()); }
// Returns true if all parents expanded.
bool IsVisible() const;
@ -1838,6 +1838,12 @@ public:
return (unsigned int) m_children.size();
}
// Checks if contains any child property.
bool HasAnyChild() const
{
return !m_children.empty();
}
// Returns sub-property at index i.
wxPGProperty* Item( unsigned int i ) const
{ return m_children[i]; }

View file

@ -245,7 +245,7 @@ public:
{
wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
if ( p->GetChildCount() == 0 || p->HasFlag(wxPG_PROP_AGGREGATE) )
if ( !p->HasAnyChild() || p->HasFlag(wxPG_PROP_AGGREGATE) )
return wxNullProperty;
return p->Item(0);

View file

@ -155,7 +155,7 @@ wxPG_ITERATE_DEFAULT = wxPG_ITERATE_NORMAL
#define wxPG_ITERATOR_PARENTEXMASK_TEST(PWC, PARENTMASK) \
( \
!PWC->HasFlag(PARENTMASK) && \
PWC->GetChildCount() > 0 \
PWC->HasAnyChild() \
)

View file

@ -1478,6 +1478,13 @@ public:
*/
unsigned int GetChildCount() const;
/**
Checks if there is any child property.
@since 3.3.0
*/
bool HasAnyChild() const;
/**
Returns height of children, recursively, and
by taking expanded/collapsed status into account.

View file

@ -221,7 +221,7 @@ wxVectorProperty::wxVectorProperty( const wxString& label,
void wxVectorProperty::RefreshChildren()
{
if ( !GetChildCount() ) return;
if ( !HasAnyChild() ) return;
const wxVector3f& vector = wxVector3fRefFromVariant(m_value);
Item(0)->SetValue( vector.x );
Item(1)->SetValue( vector.y );
@ -270,7 +270,7 @@ wxTriangleProperty::wxTriangleProperty( const wxString& label,
void wxTriangleProperty::RefreshChildren()
{
if ( !GetChildCount() ) return;
if ( !HasAnyChild() ) return;
const wxTriangle& triangle = wxTriangleRefFromVariant(m_value);
Item(0)->SetValue( WXVARIANT(triangle.a) );
Item(1)->SetValue( WXVARIANT(triangle.b) );
@ -2192,7 +2192,7 @@ void FormMain::OnInsertPropClick( wxCommandEvent& WXUNUSED(event) )
{
wxString propLabel;
if ( !m_pPropGridManager->GetGrid()->GetRoot()->GetChildCount() )
if ( !m_pPropGridManager->GetGrid()->GetRoot()->HasAnyChild() )
{
wxMessageBox("No items to relate - first add some with Append.");
return;
@ -2258,7 +2258,7 @@ void FormMain::OnInsertCatClick( wxCommandEvent& WXUNUSED(event) )
{
wxString propLabel;
if ( !m_pPropGridManager->GetGrid()->GetRoot()->GetChildCount() )
if ( !m_pPropGridManager->GetGrid()->GetRoot()->HasAnyChild() )
{
wxMessageBox("No items to relate - first add some with Append.");
return;
@ -2303,7 +2303,7 @@ void FormMain::OnDelPropRClick( wxCommandEvent& WXUNUSED(event) )
for (;;)
{
if ( p->GetChildCount() == 0 )
if ( !p->HasAnyChild() )
break;
unsigned int n = static_cast<unsigned int>(rand()) % p->GetChildCount();

View file

@ -203,7 +203,7 @@ wxSizeProperty::wxSizeProperty( const wxString& label, const wxString& name,
void wxSizeProperty::RefreshChildren()
{
if ( !GetChildCount() ) return;
if ( !HasAnyChild() ) return;
const wxSize& size = wxSizeRefFromVariant(m_value);
Item(0)->SetValue( (long)size.x );
Item(1)->SetValue( (long)size.y );
@ -241,7 +241,7 @@ wxPointProperty::wxPointProperty( const wxString& label, const wxString& name,
void wxPointProperty::RefreshChildren()
{
if ( !GetChildCount() ) return;
if ( !HasAnyChild() ) return;
const wxPoint& point = wxPointRefFromVariant(m_value);
Item(0)->SetValue( (long)point.x );
Item(1)->SetValue( (long)point.y );

View file

@ -1357,7 +1357,7 @@ bool FormMain::RunTests( bool fullTest, bool interactive )
if ( pgman->GetPageCount() )
RT_FAILURE();
if ( pgman->GetGrid()->GetRoot()->GetChildCount() )
if ( pgman->GetGrid()->GetRoot()->HasAnyChild() )
RT_FAILURE();
// Recreate the original grid
@ -1368,7 +1368,7 @@ bool FormMain::RunTests( bool fullTest, bool interactive )
pgman->SelectProperty("Label");
pgman->GetGrid()->Clear();
if ( pgman->GetGrid()->GetRoot()->GetChildCount() )
if ( pgman->GetGrid()->GetRoot()->HasAnyChild() )
RT_FAILURE();
// Recreate the original grid

View file

@ -293,7 +293,7 @@ wxPGWindowList wxPGTextCtrlEditor::CreateControls( wxPropertyGrid* propGrid,
//
// If has children, and limited editing is specified, then don't create.
if ( property->HasFlag(wxPG_PROP_NOEDITOR) &&
property->GetChildCount() > 0 )
property->HasAnyChild() )
return nullptr;
int argFlags = 0;

View file

@ -593,7 +593,7 @@ void wxPGProperty::InitAfterAdded( wxPropertyGridPageState* pageState,
//
// Has initial children
if ( GetChildCount() > 0 )
if ( HasAnyChild() )
{
// Check parental flags
wxASSERT_MSG( ((m_flags & wxPG_PROP_PARENTAL_FLAGS) ==
@ -945,14 +945,14 @@ void wxPGProperty::DoGenerateComposedValue( wxString& text,
}
}
if ( childResults && curChild->GetChildCount() > 0 )
if ( childResults && curChild->HasAnyChild() )
(*childResults)[curChild->GetName()] = s;
bool skip = false;
if ( (argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT) && s.empty() )
skip = true;
if ( curChild->GetChildCount() == 0 || skip )
if ( !curChild->HasAnyChild() || skip )
text += s;
else
text += wxS("[") + s + wxS("]");
@ -966,7 +966,7 @@ void wxPGProperty::DoGenerateComposedValue( wxString& text,
if ( !skip )
{
if ( curChild->GetChildCount() == 0 )
if ( !curChild->HasAnyChild() )
text += wxS("; ");
else
text += wxS(" ");
@ -988,7 +988,7 @@ void wxPGProperty::DoGenerateComposedValue( wxString& text,
wxString wxPGProperty::ValueToString( wxVariant& WXUNUSED(value),
int argFlags ) const
{
wxCHECK_MSG( GetChildCount() > 0,
wxCHECK_MSG( HasAnyChild(),
wxString(),
wxS("If user property does not have any children, it must ")
wxS("override GetValueAsString") );
@ -1045,7 +1045,7 @@ bool wxPGProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argF
// Convert semicolon delimited tokens into child values.
bool wxPGProperty::StringToValue( wxVariant& v, const wxString& text, int argFlags ) const
{
if ( GetChildCount() == 0 )
if ( !HasAnyChild() )
return false;
unsigned int curChild = 0;
@ -1383,7 +1383,7 @@ void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
if ( pList && !pList->IsNull() )
{
wxASSERT( pList->IsType(wxPG_VARIANT_TYPE_LIST) );
wxASSERT( GetChildCount() > 0 );
wxASSERT( HasAnyChild() );
wxASSERT( !IsCategory() );
wxVariantList& list = pList->GetList();
@ -1708,7 +1708,7 @@ void wxPGProperty::SetBackgroundColour( const wxColour& colour,
{
while ( firstProp->IsCategory() )
{
if ( firstProp->GetChildCount() == 0 )
if ( !firstProp->HasAnyChild() )
return;
firstProp = firstProp->Item(0);
}
@ -1744,7 +1744,7 @@ void wxPGProperty::SetTextColour( const wxColour& colour,
{
while ( firstProp->IsCategory() )
{
if ( firstProp->GetChildCount() == 0 )
if ( !firstProp->HasAnyChild() )
return;
firstProp = firstProp->Item(0);
}
@ -1778,7 +1778,7 @@ void wxPGProperty::SetDefaultColours(int flags)
{
while ( firstProp->IsCategory() )
{
if ( firstProp->GetChildCount() == 0 )
if ( !firstProp->HasAnyChild() )
return;
firstProp = firstProp->Item(0);
}
@ -2166,7 +2166,7 @@ const wxPGProperty* wxPGProperty::GetLastVisibleSubItem() const
{
//
// Returns last visible sub-item, recursively.
if ( !IsExpanded() || GetChildCount() == 0 )
if ( !IsExpanded() || !HasAnyChild() )
return this;
return Last()->GetLastVisibleSubItem();
@ -2318,7 +2318,7 @@ void wxPGProperty::SortChildren(int (*fCmp)(wxPGProperty**, wxPGProperty**))
void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
{
wxASSERT( GetChildCount() > 0 );
wxASSERT( HasAnyChild() );
wxASSERT( !IsCategory() );
*value = GetValue();
@ -2395,7 +2395,7 @@ wxPGProperty* wxPGProperty::GetPropertyByName( const wxString& name ) const
wxPGProperty* p = GetPropertyByName(name. substr(0,pos));
if ( !p || p->GetChildCount() == 0 )
if ( !p || !p->HasAnyChild() )
return nullptr;
return p->GetPropertyByName(name.substr(pos+1,name.length()-pos-1));
@ -2448,7 +2448,7 @@ int wxPGProperty::GetChildrenHeight( int lh, int iMax ) const
if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
{
if ( !pwc->IsExpanded() ||
pwc->GetChildCount() == 0 )
!pwc->HasAnyChild() )
h += lh;
else
h += pwc->GetChildrenHeight(lh) + lh;
@ -2487,7 +2487,7 @@ wxPGProperty* wxPGProperty::GetItemAtY( unsigned int y,
iy += lh;
if ( pwc->IsExpanded() &&
pwc->GetChildCount() > 0 )
pwc->HasAnyChild() )
{
result = pwc->GetItemAtY( y, lh, &iy );
if ( result )
@ -2545,7 +2545,7 @@ void wxPGProperty::DeleteChildren()
{
wxPropertyGridPageState* state = m_parentState;
if ( GetChildCount() == 0 )
if ( !HasAnyChild() )
return;
// Because deletion is sometimes deferred, we have to use
@ -2623,7 +2623,7 @@ bool wxPGProperty::AreAllChildrenSpecified( const wxVariant* pendingList ) const
return false;
// Check recursively
if ( child->GetChildCount() > 0 )
if ( child->HasAnyChild() )
{
const wxVariant* childList = nullptr;
@ -2658,7 +2658,7 @@ bool wxPGProperty::IsTextEditable() const
return false;
if ( HasFlag(wxPG_PROP_NOEDITOR) &&
(GetChildCount() > 0 ||
(HasAnyChild() ||
wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
)
return false;

View file

@ -125,7 +125,7 @@ wxPGProperty* wxPropertyGridInterface::RemoveProperty( wxPGPropArg id )
{
wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
wxCHECK( p->GetChildCount() == 0 || p->HasFlag(wxPG_PROP_AGGREGATE),
wxCHECK( !p->HasAnyChild() || p->HasFlag(wxPG_PROP_AGGREGATE),
wxNullProperty);
wxPropertyGridPageState* state = p->GetParentState();
@ -286,7 +286,7 @@ bool wxPropertyGridInterface::ExpandAll( bool doExpand )
{
wxPropertyGridPageState* state = m_pState;
if ( state->DoGetRoot()->GetChildCount() == 0 )
if ( !state->DoGetRoot()->HasAnyChild() )
return true;
wxPropertyGrid* pg = state->GetGrid();
@ -302,7 +302,7 @@ bool wxPropertyGridInterface::ExpandAll( bool doExpand )
for ( it = GetVIterator( wxPG_ITERATE_ALL ); !it.AtEnd(); it.Next() )
{
wxPGProperty* p = it.GetProperty();
if ( p->GetChildCount() > 0 )
if ( p->HasAnyChild() )
{
if ( doExpand )
{
@ -504,7 +504,7 @@ wxPGProperty* wxPropertyGridInterface::GetPropertyByName( const wxString& name,
const wxString& subname ) const
{
wxPGProperty* p = DoGetPropertyByName(name);
if ( !p || p->GetChildCount() == 0 )
if ( !p || !p->HasAnyChild() )
return wxNullProperty;
return p->GetPropertyByName(subname);

View file

@ -43,7 +43,7 @@ void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState* state, int flags
m_state = state;
m_baseParent = state->DoGetRoot();
if ( !property && m_baseParent->GetChildCount() > 0 )
if ( !property && m_baseParent->HasAnyChild() )
property = m_baseParent->Item(0);
m_property = property;
@ -110,7 +110,7 @@ void wxPropertyGridIteratorBase::Prev()
property = parent->Item(index);
// Go to last children?
if ( property->GetChildCount() > 0 &&
if ( property->HasAnyChild() &&
wxPG_ITERATOR_PARENTEXMASK_TEST(property, m_parentExMask) )
{
// First child
@ -144,7 +144,7 @@ void wxPropertyGridIteratorBase::Next( bool iterateChildren )
if ( !property )
return;
if ( property->GetChildCount() > 0 &&
if ( property->HasAnyChild() &&
wxPG_ITERATOR_PARENTEXMASK_TEST(property, m_parentExMask) &&
iterateChildren )
{
@ -234,7 +234,7 @@ void wxPropertyGridPageState::InitNonCatMode()
// to run as expected.
m_properties = &m_regularArray;
if ( m_properties->GetChildCount() > 0 )
if ( m_properties->HasAnyChild() )
{
//
// Prepare m_abcArray
@ -372,7 +372,7 @@ void wxPropertyGridPageState::OnClientWidthChange( int newWidth, int widthChange
// If too long, don't set splitter
if ( timeSinceCreation < 250 )
{
if ( m_properties->GetChildCount() > 0 )
if ( m_properties->HasAnyChild() )
{
SetSplitterLeft( false );
}
@ -397,14 +397,14 @@ void wxPropertyGridPageState::OnClientWidthChange( int newWidth, int widthChange
wxPGProperty* wxPropertyGridPageState::GetLastItem( int flags )
{
if ( m_properties->GetChildCount() == 0 )
if ( !m_properties->HasAnyChild() )
return nullptr;
wxPG_ITERATOR_CREATE_MASKS(flags, wxPGProperty::FlagType itemExMask, wxPGProperty::FlagType parentExMask)
// First, get last child of last parent
wxPGProperty* pwc = m_properties->Last();
while ( pwc->GetChildCount() > 0 &&
while ( pwc->HasAnyChild() &&
wxPG_ITERATOR_PARENTEXMASK_TEST(pwc, parentExMask) )
pwc = pwc->Last();
@ -461,7 +461,7 @@ wxPGProperty* wxPropertyGridPageState::BaseGetPropertyByLabel
if ( p->GetLabel() == label )
return p;
// Check children recursively.
if ( p->GetChildCount() > 0 )
if ( p->HasAnyChild() )
{
p = BaseGetPropertyByLabel(label, p);
if ( p )
@ -560,7 +560,7 @@ bool wxPropertyGridPageState::EnableCategories( bool enable )
p->m_depth = parent->GetDepth() + 1;
}
if ( p->GetChildCount() > 0 )
if ( p->HasAnyChild() )
{
i = 0;
parent = p;
@ -629,7 +629,7 @@ void wxPropertyGridPageState::DoSortChildren( wxPGProperty* p,
p = m_properties;
// Can only sort items with children
if ( p->GetChildCount() == 0 )
if ( !p->HasAnyChild() )
return;
// Never sort children of aggregate properties
@ -744,7 +744,7 @@ int wxPropertyGridPageState::GetColumnFitWidth(const wxDC& dc,
maxW = w;
}
if ( p->GetChildCount() > 0 &&
if ( p->HasAnyChild() &&
( subProps || p->IsCategory() ) )
{
w = GetColumnFitWidth(p, col, subProps );
@ -786,7 +786,7 @@ int wxPropertyGridPageState::GetColumnFitWidth(const wxPGProperty* p, unsigned i
maxW = w;
}
if ( pc->GetChildCount() > 0 && (subProps || pc->IsCategory()) )
if ( pc->HasAnyChild() && (subProps || pc->IsCategory()) )
{
w = GetColumnFitWidth(pc, col, subProps);
@ -1346,7 +1346,7 @@ bool wxPropertyGridPageState::DoCollapse( wxPGProperty* p )
{
wxCHECK_MSG( p, false, wxS("invalid property id") );
if ( p->GetChildCount() == 0 ) return false;
if ( !p->HasAnyChild() ) return false;
if ( !p->IsExpanded() ) return false;
@ -1363,7 +1363,7 @@ bool wxPropertyGridPageState::DoExpand( wxPGProperty* p )
{
wxCHECK_MSG( p, false, wxS("invalid property id") );
if ( p->GetChildCount() == 0 ) return false;
if ( !p->HasAnyChild() ) return false;
if ( p->IsExpanded() ) return false;
@ -1414,7 +1414,7 @@ wxVariant wxPropertyGridPageState::DoGetPropertyValues( const wxString& listname
wxVariantList tempList;
wxVariant v( tempList, listname );
if ( pwc->GetChildCount() > 0 )
if ( pwc->HasAnyChild() )
{
if ( flags & wxPG_KEEP_STRUCTURE )
{
@ -1423,7 +1423,7 @@ wxVariant wxPropertyGridPageState::DoGetPropertyValues( const wxString& listname
for ( unsigned int i = 0; i < pwc->GetChildCount(); i++ )
{
wxPGProperty* p = pwc->Item(i);
if ( p->GetChildCount() == 0 || p->HasFlag(wxPG_PROP_AGGREGATE) )
if ( !p->HasAnyChild() || p->HasFlag(wxPG_PROP_AGGREGATE) )
{
wxVariant variant = p->GetValue();
variant.SetName( p->GetBaseName() );
@ -1447,7 +1447,7 @@ wxVariant wxPropertyGridPageState::DoGetPropertyValues( const wxString& listname
const wxPGProperty* p = it.GetProperty();
// Use a trick to ignore wxParentProperty itself, but not its sub-properties.
if ( p->GetChildCount() == 0 || p->HasFlag(wxPG_PROP_AGGREGATE) )
if ( !p->HasAnyChild() || p->HasFlag(wxPG_PROP_AGGREGATE) )
{
wxVariant variant = p->GetValue();
variant.SetName( p->GetName() );
@ -2010,7 +2010,7 @@ void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete )
unsigned int indinparent = item->GetIndexInParent();
// Delete children
if ( item->GetChildCount() > 0 && !item->HasFlag(wxPG_PROP_AGGREGATE) )
if ( item->HasAnyChild() && !item->HasFlag(wxPG_PROP_AGGREGATE) )
{
// deleting a category
item->DeleteChildren();

View file

@ -69,7 +69,7 @@ wxString wxStringProperty::ValueToString( wxVariant& value,
{
wxString s = value.GetString();
if ( GetChildCount() > 0 && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
if ( HasAnyChild() && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
{
// Value stored in m_value is non-editable, non-full value
if ( (argFlags & wxPG_FULL_VALUE) ||
@ -97,7 +97,7 @@ wxString wxStringProperty::ValueToString( wxVariant& value,
bool wxStringProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
{
if ( GetChildCount() > 0 && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
if ( HasAnyChild() && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
return wxPGProperty::StringToValue(variant, text, argFlags);
if ( variant != text )
@ -1771,7 +1771,7 @@ long wxFlagsProperty::IdToBit( const wxString& id ) const
void wxFlagsProperty::RefreshChildren()
{
if ( !m_choices.IsOk() || GetChildCount() == 0 ) return;
if ( !m_choices.IsOk() || !HasAnyChild() ) return;
int flags = m_value.GetLong();