Use standard containers in dataview sample
Update the sample to use std::vector<> and std::unordered_map<> for storing the model data, this should be much more useful for the people trying to write their own model. Also use std::unique_ptr<> to avoid manual memory management.
This commit is contained in:
parent
b42b82bfb2
commit
f5920de681
2 changed files with 27 additions and 41 deletions
|
|
@ -135,12 +135,15 @@ void MyMusicTreeModel::Delete( const wxDataViewItem &item )
|
|||
m_ninth = nullptr;
|
||||
|
||||
// first remove the node from the parent's array of children;
|
||||
// NOTE: MyMusicTreeModelNodePtrArray is only an array of _pointers_
|
||||
// thus removing the node from it doesn't result in freeing it
|
||||
node->GetParent()->GetChildren().Remove( node );
|
||||
|
||||
// free the node
|
||||
delete node;
|
||||
auto& siblings = node->GetParent()->GetChildren();
|
||||
for ( auto it = siblings.begin(); it != siblings.end(); ++it )
|
||||
{
|
||||
if ( it->get() == node )
|
||||
{
|
||||
siblings.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// notify control
|
||||
ItemDeleted( parent, item );
|
||||
|
|
@ -151,12 +154,7 @@ void MyMusicTreeModel::Clear()
|
|||
m_classical = nullptr;
|
||||
m_ninth = nullptr;
|
||||
|
||||
while (!m_root->GetChildren().IsEmpty())
|
||||
{
|
||||
MyMusicTreeModelNode* node = m_root->GetNthChild(0);
|
||||
m_root->GetChildren().Remove(node);
|
||||
delete node;
|
||||
}
|
||||
m_root->GetChildren().clear();
|
||||
|
||||
Cleared();
|
||||
}
|
||||
|
|
@ -320,14 +318,12 @@ unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem &parent,
|
|||
return 0;
|
||||
}
|
||||
|
||||
unsigned int count = node->GetChildren().GetCount();
|
||||
for (unsigned int pos = 0; pos < count; pos++)
|
||||
for ( const auto& child : node->GetChildren() )
|
||||
{
|
||||
MyMusicTreeModelNode *child = node->GetChildren().Item( pos );
|
||||
array.Add( wxDataViewItem( (void*) child ) );
|
||||
array.Add( wxDataViewItem( child.get() ) );
|
||||
}
|
||||
|
||||
return count;
|
||||
return array.size();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -514,7 +510,7 @@ void MyListModel::GetValueByRow( wxVariant &variant,
|
|||
|
||||
case Col_Custom:
|
||||
{
|
||||
IntToStringMap::const_iterator it = m_customColValues.find(row);
|
||||
const auto it = m_customColValues.find(row);
|
||||
if ( it != m_customColValues.end() )
|
||||
variant = it->second;
|
||||
else
|
||||
|
|
|
|||
|
|
@ -8,18 +8,17 @@
|
|||
// Licence: wxWindows licence
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "wx/hashmap.h"
|
||||
#include "wx/vector.h"
|
||||
|
||||
WX_DECLARE_HASH_MAP(unsigned, wxString, wxIntegerHash, wxIntegerEqual,
|
||||
IntToStringMap);
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// MyMusicTreeModelNode: a node inside MyMusicTreeModel
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class MyMusicTreeModelNode;
|
||||
WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodePtrArray );
|
||||
using MyMusicTreeModelNodePtr = std::unique_ptr<MyMusicTreeModelNode>;
|
||||
using MyMusicTreeModelNodePtrArray = std::vector<MyMusicTreeModelNodePtr>;
|
||||
|
||||
class MyMusicTreeModelNode
|
||||
{
|
||||
|
|
@ -49,16 +48,7 @@ public:
|
|||
m_container = true;
|
||||
}
|
||||
|
||||
~MyMusicTreeModelNode()
|
||||
{
|
||||
// free all our children nodes
|
||||
size_t count = m_children.GetCount();
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
MyMusicTreeModelNode *child = m_children[i];
|
||||
delete child;
|
||||
}
|
||||
}
|
||||
~MyMusicTreeModelNode() = default;
|
||||
|
||||
bool IsContainer() const
|
||||
{ return m_container; }
|
||||
|
|
@ -68,13 +58,13 @@ public:
|
|||
MyMusicTreeModelNodePtrArray& GetChildren()
|
||||
{ return m_children; }
|
||||
MyMusicTreeModelNode* GetNthChild( unsigned int n )
|
||||
{ return m_children.Item( n ); }
|
||||
{ return m_children.at( n ).get(); }
|
||||
void Insert( MyMusicTreeModelNode* child, unsigned int n)
|
||||
{ m_children.Insert( child, n); }
|
||||
{ m_children.insert( m_children.begin() + n, MyMusicTreeModelNodePtr(child) ); }
|
||||
void Append( MyMusicTreeModelNode* child )
|
||||
{ m_children.Add( child ); }
|
||||
{ m_children.push_back( MyMusicTreeModelNodePtr(child) ); }
|
||||
unsigned int GetChildCount() const
|
||||
{ return m_children.GetCount(); }
|
||||
{ return m_children.size(); }
|
||||
|
||||
public: // public to avoid getters/setters
|
||||
wxString m_title;
|
||||
|
|
@ -87,7 +77,7 @@ public: // public to avoid getters/setters
|
|||
// needs to know in advance if a node is or _will be_ a container.
|
||||
// Thus implementing:
|
||||
// bool IsContainer() const
|
||||
// { return m_children.GetCount()>0; }
|
||||
// { return !m_children.empty(); }
|
||||
// doesn't work with wxGTK when MyMusicTreeModel::AddToClassical is called
|
||||
// AND the classical node was removed (a new node temporary without children
|
||||
// would be added to the control)
|
||||
|
|
@ -223,10 +213,10 @@ public:
|
|||
unsigned int row, unsigned int col ) override;
|
||||
|
||||
private:
|
||||
wxVector<bool> m_toggleColValues;
|
||||
std::vector<bool> m_toggleColValues;
|
||||
wxArrayString m_textColValues;
|
||||
wxArrayString m_iconColValues;
|
||||
IntToStringMap m_customColValues;
|
||||
std::unordered_map<unsigned, wxString> m_customColValues;
|
||||
wxBitmapBundle m_icon[2];
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue