NXOpen-树列表例子

原文链接:https://blog.csdn.net/qq_30722721/article/details/102561571

//基本概念:一个Node节点代表一行
//***部分命令请注意顺序关系***
 
//插入列,注意插入列仅initialize_cb()/dialogShown_cb()设置一次,update()时会报错
for (int i = 0; i < columnCount; ++i)
{
    tree->InsertColumn(i, "", columnWidth);
    tree->SetColumnResizePolicy(i, BlockStyler::Tree::ColumnResizePolicyConstantWidth);
    tree->SetColumnSortable(i, false);
}
 
//创建    
Node *node = tree->CreateNode("label");
tree->InsertNode(node, NULL, NULL, Tree::NodeInsertOptionLast);
tree->InsertNode(node, parentNode, NULL, Tree::NodeInsertOptionLast);//父节点
 
//遍历子节点
void GetAllNode(Node* inputNode, vector<Node*> &nodes)
{
    if (inputNode == NULL)
    {
        return;
    }
 
    Node* node = inputNode->FirstChildNode();
    while (node != NULL)
    {
        nodes.push_back(node);
        NXCommon::GetAllNode(node, nodes);
        node = node->NextSiblingNode();
    }
}
 
//得到树控件所有节点
vector<Node*> GetAllNodes(Tree* tree)
{
    vector<Node*> res;
    Node *nd = tree->RootNode();
    while (nd != NULL)
    {
        res.push_back(nd);
        nd = nd->NextSiblingNode();
    }
    return res;
}
 
/得到树控件所有节点 含子节点
std::vector<Node*> GetAllNodesContainSon(Tree* tree)
{
    Node *nd = tree->RootNode();
 
    vector<Node*> res;
    while (nd != NULL)
    {
        res.push_back(nd);
        GetAllNode(nd, res);
        nd = nd->NextSiblingNode();
    }
 
    return res;
}
 
//清空树节点
void ClearTree(Tree* tree)
{
    vector<BlockStyler::Node*> nodes = GetAllNodes(tree);
    for (int i = 0; i < nodes.size(); i++)
    {
        tree->DeleteNode(nodes[i]);
    }
}
 
//节点选择状态
tree->SelectNode(node, false, false); //不选中
node->IsSelected();  //节点是否选中
 
//显示文本
node->SetColumnDisplayText(columnID, "Text");
 
//节点添加属性
node->GetNodeData()->AddInteger("XX",int);
node->GetNodeData()->SetInteger("XX",int); //先调用AddInteger才有效,否则报错
 
//展开节点
node->Expand(NXOpen::BlockStyler::Node::ExpandOption::ExpandOptionExpand);
 
//注意:OnEditOptionSelectedCallback()不支持节点增删改
NXOpen::BlockStyler::Tree::EditControlOption OnEditOptionSelectedCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID, int selectedOptionID, NXString selectedOptionText, NXOpen::BlockStyler::Tree::ControlType type)
{
    if (tree == NULL || node == NULL)
        return Tree::EditControlOptionReject;
 
    //如果要检查输入值是否为double,必须强制检查
    std::string originText = selectedOptionText.GetLocaleText(); //重要 得到输入的值
    std::string text = NXCommon::DoubleToStr(NXCommon::StrToDouble(originText));
    if (originText != text)
    {
        return Tree::EditControlOptionReject;
    }
 
    return Tree::EditControlOptionAccept;
}
 
//编辑Node某列
NXOpen::BlockStyler::Tree::ControlType AskEditControlCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID)
{
    if (tree == NULL || node == NULL)
    {
        return Tree::ControlTypeNone;
    }
    
    //只有枚举样式,设置一个值时,效果等同字符串控件    
    std::vector<NXString> tmpContents;
    tmpContents.push_back("A");
    tmpContents.push_back("B");
    tree->SetEditOptions(tmpContents, 0);
    return Tree::ControlTypeComboBox;
}
 
//Node 右键菜单
void OnMenuCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID )
{
    TreeListMenu *menu = tree->CreateMenu();
    menu->AddMenuItem(0, "创建");
    menu->AddMenuItem(1, "删除");
    tree->SetMenu(menu);
    delete menu;
    menu = NULL;
}
 
//Node 拖动
//更新树控件时,Tree最好只更新一次,
//否则代码中未执行完的代码可能报错,如:node[i]->GetColumnDisplayText(0).GetLocaleText();
目前NX树控件不支持直接拖动,可以通过
(1) 重绘树控件
(2) 将拖动节点备份,重新插入目标节点(个人觉得比较麻烦)
(3) 其他....
 
NXOpen::BlockStyler::Node::DragType IsDragAllowedCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID)
{
    if (tree == NULL || node == NULL)
        return BlockStyler::Node::DragTypeNone;
 
    return BlockStyler::Node::DragTypeAll;//允许节点拖动,但没有拖动效果
}
 
//Node 某列排序  注意:代码放置在数据填充之后生效
tree->SetSortRootNodes(true);
tree->SetColumnSortable(columnID, true);
tree->SetColumnSortOption(columnID, Tree::ColumnSortOption::ColumnSortOptionDescending);
//排序回调函数处理 
int ColumnSortCallback(NXOpen::BlockStyler::Tree *tree, int columnID, NXOpen::BlockStyler::Node *node1, NXOpen::BlockStyler::Node *node2)
{
    //返回值为0、正负值;分别表示两个节点相同,第一个节点大于第二个,第一个节点小于第二个
    int res = 0;
 
    
    //注意:node1是树控件上后一个节点,node2是前一个节点 !!!
    std::string content1 = node1->GetColumnDisplayText(columnID).GetLocaleText();
    std::string content2 = node2->GetColumnDisplayText(columnID).GetLocaleText();
    if (IsDoubleString(content1))
    {
        if (IsGT(StrToDouble(content1), StrToDouble(content2)))
        {
            res = 1;
        }
        else
        {
            res = -1;
        }
    }
    else
    {
        if (content1 > content2)
        {
            res = 1;
        }
        else
        {
            res = -1;
        }
    }
 
    return res;
}
 
 
//Node 某列显示/隐藏
tree->SetColumnVisible(columnID,false);
 
//Node 第一列前:打勾+图片
node->SetState(1); //1 = 不打勾,2 = 打勾
node->SetDisplayIcon(bmpPath);
node->SetSelectedIcon(bmpPath);
 
//Node 设置显示文本颜色
node->SetForegroundColor(186);

NXOpen-树列表例子

上一篇:poj 1102(水题)


下一篇:msf安装