Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

TreeView remove item by pointer/name


Recommended Posts

Hello,

It is rather hard to remove an item from a TreeView if its not on the lowest subnode. 
 

auto it = std::find(itemFolder->begin(), itemFolder->end(), itemClass);
if (it != itemFolder->end())
	itemFolder->erase(it);
subnode->ClearNodes();
// rebuild nodes
for each (itemClass k in *itemFolder)
{
	subnode->AddNode(k.info);
}

This for example works okay if you rebuild the tree in the following but you won't be able to remove if it is in a top treenode.

a.PNG

Link to post
Share on other sites
7 minutes ago, Optimus Josh said:

Maybe the Widget  class should have a FindChild method?

A findchild method could also be useful for many situations, but in this scenario you would know which exact widget you need to remove. Is there a way to hide a widget by widget text ?

  • Like 1
Link to post
Share on other sites
41 minutes ago, Slastraf said:

A findchild method could also be useful for many situations, but in this scenario you would know which exact widget you need to remove. Is there a way to hide a widget by widget text ?

Didn't we just say the exact same thing two different ways? :huh:

Link to post
Share on other sites
1 minute ago, Optimus Josh said:

Didn't we just say the exact same thing two different ways? :huh:

FindChild implies you would need to know there is a parent. But if you dont know the parent you would need to call it from the root, then it collapses on itself and it becomes a Find() function. Now if you had a find function there needs to be a unique identifier for each widget which the user could set and dont need to worry about all of this.

  • Confused 1
Link to post
Share on other sites
  • 2 weeks later...

How to use it ?
Yesterday it seemed to work by setting subnode->SetParent(nullptr); then it would hide the whole tree but today I get an error.
I made code to reproduce it, can replace lines 74-76 in the default project main.
 


	auto tree2 = CreateTreeView(border, border, 280, subpanel3->ClientSize().y - border * 2, subpanel3, TREEVIEW_DRAGANDDROP | TREEVIEW_DRAGINSERT);
	tree2->SetLayout(1, 0, 1, 1);
	
	auto subnode2 = tree2->root->AddNode("Node 1");
	for (int n = 1; n < 10; ++n)
	{
		subnode2->AddNode("Subnode " + String(n));
	}
//error here
	subnode2->SetParent(nullptr);

BTW, also happens with SetParent(NULL);

Link to post
Share on other sites

Step one is to produce the bug. Now I have done that and we have something we can both run:

#include "pch.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = ListDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0]);

    //Create User Interface
    auto ui = CreateInterface(window);

    //Create widget
    auto sz = ui->root->ClientSize();
    auto treeview = CreateTreeView(10, 10, sz.x - 20, sz.y - 20, ui->root);

    auto node = treeview->root->AddNode("Node 1");
    node->AddNode("Subnode 1");
    node->AddNode("Subnode 2");
    node->AddNode("Subnode 3");

    node = treeview->root->AddNode("Node 2");
    node->AddNode("Subnode 1");
    node->AddNode("Subnode 2");
    node->AddNode("Subnode 3");

    node = treeview->root->AddNode("Node 3");
    node->AddNode("Subnode 1");
    node->AddNode("Subnode 2");
    node->AddNode("Subnode 3");

    while (true)
    {
        const auto& event = WaitEvent();
        switch (event.id)
        {
        case EVENT_WINDOWCLOSE:
            return 0;
            break;
        case EVENT_WIDGETACTION:
            {
                node = event.extra->As<TreeViewNode>();
                node->SetParent(NULL);
            }
            break;
        }
    }
    return 0;
}

 

Link to post
Share on other sites
11 minutes ago, Optimus Josh said:

Step one is to produce the bug. Now I have done that and we have something we can both run:



#include "pch.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = ListDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0]);

    //Create User Interface
    auto ui = CreateInterface(window);

    //Create widget
    auto sz = ui->root->ClientSize();
    auto treeview = CreateTreeView(10, 10, sz.x - 20, sz.y - 20, ui->root);

    auto node = treeview->root->AddNode("Node 1");
    node->AddNode("Subnode 1");
    node->AddNode("Subnode 2");
    node->AddNode("Subnode 3");

    node = treeview->root->AddNode("Node 2");
    node->AddNode("Subnode 1");
    node->AddNode("Subnode 2");
    node->AddNode("Subnode 3");

    node = treeview->root->AddNode("Node 3");
    node->AddNode("Subnode 1");
    node->AddNode("Subnode 2");
    node->AddNode("Subnode 3");

    while (true)
    {
        const auto& event = WaitEvent();
        switch (event.id)
        {
        case EVENT_WINDOWCLOSE:
            return 0;
            break;
        case EVENT_WIDGETACTION:
            {
                node = event.extra->As<TreeViewNode>();
                node->SetParent(NULL);
            }
            break;
        }
    }
    return 0;
}

 

That is good and I have implemented a similar thing, but what if you want to remove all nodes from the tree ?
You could select an item and delete it, but in an other case I want to reset the whole tree. For example you have 10 different users and for each user there is a list of items and you want the tree to display all items of the selected user. This requires for the entire tree to refresh and rebuild. This functionality does not work anymore because a few days ago you could take the node which you iterated the tree with (like my example above) and set its parent to null and the tree would hide. This does not work anymore. Can you provide an example for reseting the tree ?

Link to post
Share on other sites
3 minutes ago, Slastraf said:

That is good and I have implemented a similar thing, but what if you want to remove all nodes from the tree ?
You could select an item and delete it, but in an other case I want to reset the whole tree. For example you have 10 different users and for each user there is a list of items and you want the tree to display all items of the selected user. This requires for the entire tree to refresh and rebuild. This functionality does not work anymore because a few days ago you could take the node which you iterated the tree with (like my example above) and set its parent to null and the tree would hide. This does not work anymore. Can you provide an example for reseting the tree ?

I created an example that shows the error you described, and I am working on fixing it, and now you are describing another situation you want me to guess and try to create before I have fixed the thing you were reporting? You are dancing around too much. I need a piece of code I can copy and paste.

Link to post
Share on other sites
3 minutes ago, Optimus Josh said:

I created an example that shows the error you described, and I am working on fixing it, and now you are describing another situation you want me to guess and try to create before I have fixed the thing you were reporting? You are dancing around too much. I need a piece of code I can copy and paste.

Sorry to bother so much, yes the original post was about selection Nodes, but my post from 19 hours ago was to reset the whole tree (and it worked prior but not anymore). Regardless, both things need to work. There shou.ld be ->SetParent(NULL) to remove 1 item and also tree->root->SetParent(NULL) or an equivalent tree->root->ClearNodes() (exists but throws error currently) which resets the whole tree.

Link to post
Share on other sites

I have the problem fixed and I am making a new build now. To clear a tree, you would just remove all children from the root. You can use Lock and Unlock to prevent drawing as they are being removed:

treeview->Lock();
while (!treeview->kids.empty())
{
	treeview->kids[0]->SetParent(NULL);
}
treeview->Unlock();

signals GIF

  • Haha 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...