Dataview sample: adding items question Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Dataview sample: adding items question

Post by papillon68 »

I'm studying and customizing the dataview sample but I can't figure out how to append items to a specific child or level. The relevant code section looks like this:

Code: Select all

    m_root = new MyTreeCrownModelNode(NULL,  "Root node" ); // 1st item
    m_root->Append(new MyTreeCrownModelNode (m_root,"Layer 1" )); // 2nd item
    m_root->Append(new MyTreeCrownModelNode (m_root, "Layer 2")); // 3rd item
Now, without creating any accessory function, say I want to hardcode and add a child to the second item directly. I thought I could do something like this:

Code: Select all

	MyTreeCrownModelNode *m_pop = new MyTreeCrownModelNode(m_root->GetNthChild(1), "Layer 3" );
	m_root->Append(m_pop);
The item is added to the list but it's just a root child as the others, while I wanted it to be a child of the 2nd item.

Just to clarify, here is the result I'd like to obtain:
  • Root node
    --- Layer 1
    ------ Layer 3
    --- Layer 2
  • And here is what I get now:
    Root node
    --- Layer 1
    --- Layer 2
    --- Layer 3
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Dataview sample: adding items question

Post by doublemax »

Untested:

Code: Select all

m_root->GetNthChild(1)->Append(m_pop);
Use the source, Luke!
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: Dataview sample: adding items question

Post by Manolo »

The wxDVC has no notion of parent/children items. It asks the wxDataViewModel about it by wxDataViewModel::GetParent(item) and wxDataViewModel::GetChildren(item, children), just for rendering and user interaction matters. You must override these functions in your model.

The root of the whole tree is an "invisible" item. You can refer to it as wxDataViewItem(0)

To add a new item to an existing one, create it and inform the model about this change, by calling wxDataViewModel::ItemAdded(parent, newIntem).

Look for these features in the wxDVC sample. Also, using a debugger and setting some breakpoints will show you the flow in the wxDVC.
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Re: Dataview sample: adding items question

Post by papillon68 »

Following the suggestions I modified the code as follows:

This is the function called when I click the Add item button, basically passing the selected item and the data to add

Code: Select all

void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
{
	wxDataViewItem selectedItem = m_ctrl->GetSelection();
	m_music_model->Add("add this", "add that", selectedItem);
}
And here below the DVM function that should perform the operation:

Code: Select all

void MyTreeCrownModel::Add(const wxString &crownLayerType, const wxString &crownLayerName, const wxDataViewItem &item)
{			
		MyTreeCrownModelNode *node = (MyTreeCrownModelNode*)item.GetID();
		wxLogMessage("selected item ID: %s", node->m_crownLayerID); // check the selected item ID, which is OK
		MyTreeCrownModelNode *m_addThis = new MyTreeCrownModelNode(m_root, crownLayerType, crownLayerName);
		m_root->Append(m_addThis); // add the new item to the root
		wxDataViewItem child((void*)m_addThis);
		wxDataViewItem parent((void*)node);
		ItemAdded(parent, child); // parent it to the selected item
}
Still not working, I honestly can't see what's the issue. From the documentation and sample I can't figure out either if the Append has to reference the item, or the root (as in the example above) but it doesn't really matter as the item doesn't get added.
ItemAdded returns a false.
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: Dataview sample: adding items question

Post by Manolo »

Where do you want to append the new node, to the hidden m_root or to the selected item?
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Re: Dataview sample: adding items question

Post by papillon68 »

Manolo wrote:Where do you want to append the new node, to the hidden m_root or to the selected item?
To the selected item.
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: Dataview sample: adding items question

Post by Manolo »

To append to the selected item then:

Code: Select all

      MyTreeCrownModelNode *node = (MyTreeCrownModelNode*)item.GetID();
      wxLogMessage("selected item ID: %s", node->m_crownLayerID); // check the selected item ID, which is OK
  // CHANGES:
      MyTreeCrownModelNode *m_addThis = new MyTreeCrownModelNode(node, crownLayerType, crownLayerName);
      node->Append(m_addThis); // add the new item to the select item 
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Re: Dataview sample: adding items question

Post by papillon68 »

Manolo wrote:To append to the selected item then:

Code: Select all

      MyTreeCrownModelNode *node = (MyTreeCrownModelNode*)item.GetID();
      wxLogMessage("selected item ID: %s", node->m_crownLayerID); // check the selected item ID, which is OK
  // CHANGES:
      MyTreeCrownModelNode *m_addThis = new MyTreeCrownModelNode(node, crownLayerType, crownLayerName);
      node->Append(m_addThis); // add the new item to the select item 
Thanks Manolo, but not working yet, item isn't added. In case some folks would like to try what's going on, I'm attaching the VS solution with sources.
The relevant function where I try to add the item is:
void MyTreeCrownModel::Add(const wxString &crownLayerType, const wxString &crownLayerName, wxDataViewItem &item) @ mymodels.cpp
dataview.zip
VC++ 2015 solution
(42.08 KiB) Downloaded 61 times
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Re: Dataview sample: adding items question

Post by papillon68 »

Just an update that I managed to fix things the way I wanted. From what I could understand, a wxDataViewCtrl doesn't allow to create a tree of items, it just allows only 1 level.
So what I did was to append containers, which can be nested one to another. By overriding the HasContainerColumns function, containers can display data from all columns, so basically for my purposes, that's all I need.
If somebody stumbles upon this and needs information, let me know. Cheers.
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
Post Reply