Hi,

I encounter this problem and I think it might be related to the Wt framework. I 
want to see if anyone can help me with it.

Synopsis: When a WTreeNode is added to the WTree and then is removed using 
removeChildNode() but without completely destroying the node and re-adds it 
back to the WTree using addChildNode(), the select() / emit() signal 
underneaths the node apparently is now duplicated. And each time the node is 
removed and re-added the signal grows in a polynomial rate.

Below is a prove of concept code snippet
-------------------------------ExamplePage.h-------------------------------------
#ifndef EXAMPLEPAGE_H_
#define EXAMPLEPAGE_H_
    
#include <Wt/WContainerWidget>
#include <Wt/WTree>
#include <Wt/WTreeNode>
#include <vector>
#include <string>
#include <iostream>
        
#include "WtApp.h"
    
class ValueTreeNode : public Wt::WTreeNode
{   
    std::wstring value_;
 public:
    
    ValueTreeNode(const std::wstring& value,
                  const std::wstring &labelText, 
                  Wt::WIconPair *labelIcon=0,
                  Wt::WTreeNode *parentNode=0) :
        Wt::WTreeNode(labelText, labelIcon, parentNode),
        value_(value)
    {
    }
    
    virtual ~ValueTreeNode() 
    {
    
    }
    
    const std::wstring value() const 
    {
        return value_;
    }   
};  

class ExamplePage : public Wt::WContainerWidget
{
 public:
    ExamplePage(Wt::WContainerWidget* parent=0);
    virtual ~ExamplePage();
 private:
    Wt::WTree *tree;
    std::vector<ValueTreeNode*> children_;
    void addButtonAction();
    void removeButtonAction();
    void updates(bool b);
};

#endif



-------------------------------ExamplePage.cpp-------------------------------------
#include "ExamplePage.h"
#include <Wt/WIconPair>    
#include <Wt/WSignalMapper>
#include <Wt/WSignal>
#include <Wt/WText>
#include <Wt/WPushButton>

ExamplePage::ExamplePage(Wt::WContainerWidget* parent) : 
Wt::WContainerWidget(parent)
{   
    Wt::WIconPair * labelIcon = new 
Wt::WIconPair("resources/icons/universes_closed.png","resources/icon 
/universes_open.png",true);
    
    ValueTreeNode * root = new ValueTreeNode(L"Root",L"Root",labelIcon,NULL);
    root->selected().connect(SLOT(this,ExamplePage::updates));
    root->setLoadPolicy(Wt::WTreeNode::PreLoading);
    root->expand();
    
    tree = new Wt::WTree(this);

    tree->setSelectionMode(Wt::SingleSelection);
    tree->setTreeRoot(root);
    
    addWidget(tree);

    std::vector<std::wstring> values;
    values.push_back(L"Node 1");
    
    for (std::vector<std::wstring>::iterator i = values.begin(); i != 
values.end(); i++)
    {
        Wt::WIconPair * labelIcon = new 
Wt::WIconPair("resources/icons/studies_closed.png","resources/icons/studies_open.png",true);
        ValueTreeNode* childNode = new 
ValueTreeNode(*i,*i,labelIcon,tree->treeRoot());
        childNode->selected().connect(SLOT(this,ExamplePage::updates));
        children_.push_back(childNode);
    }
    
    new Wt::WBreak(this);
    
    Wt::WPushButton * addButton = new Wt::WPushButton("Add", this);
    addButton->clicked().connect(SLOT(this,ExamplePage::addButtonAction));
    
    Wt::WPushButton * removeButton = new Wt::WPushButton("Remove", this);
    removeButton->clicked().connect(SLOT(this,ExamplePage::removeButtonAction));
}
    
ExamplePage::~ExamplePage()
{   
    delete tree;
}
    
void ExamplePage::addButtonAction()
{   
    new Wt::WBreak(this);
    new Wt::WBreak(this);
    Wt::WText* buttonClicked = new Wt::WText("Add Button Clicked", this);
    Wt::WTreeNode *treeRoot = tree->treeRoot();
    for(int i=0; i<children_.size(); i++)
    {
        treeRoot->addChildNode(children_[i]);
    }
}

void ExamplePage::removeButtonAction()
{
    new Wt::WBreak(this);
    new Wt::WBreak(this);
    Wt::WText* buttonClicked = new Wt::WText("Remove Button Clicked", this);
    Wt::WTreeNode *treeRoot = tree->treeRoot();
    for(int i=0; i<children_.size(); i++)
    {
        treeRoot->removeChildNode(children_[i]);
    }
}
void ExamplePage::updates(bool b)
{
    new Wt::WBreak(this);
    Wt::WText* valueSelected = new Wt::WText("Tree Node is Clicked!", this);
}

----------------------------------------------------------------------------------------------------

Here is a link to the code in action:
http://slice4.aheh.com:8050/


Sincerely,

Jimmy
------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to