Example #1
0
void MainWindow::drawGraph()
{
        /*
            _scene->loadLayout("digraph test{node [style=filled,fillcolor=white];N1 -> N2;N2 -> N3;N3 -> N4;N4 -> N1;}");
            connect(_scene, SIGNAL(nodeContextMenu(QGVNode*)), SLOT(nodeContextMenu(QGVNode*)));
            connect(_scene, SIGNAL(nodeDoubleClick(QGVNode*)), SLOT(nodeDoubleClick(QGVNode*)));
            ui->graphicsView->setScene(_scene);
            return;
            */

        //Configure scene attributes
        _scene->setGraphAttribute("label", "DEMO");
        _scene->setGraphAttribute("dpi", "96,0");

        //_scene->setGraphAttribute("splines", "ortho");
        //_scene->setGraphAttribute("rankdir", "LR");
        //_scene->setGraphAttribute("fontname", "Cabin");
        //_scene->setGraphAttribute("concentrate", "true"); //Error !
        _scene->setGraphAttribute("nodesep", "0.5");

        _scene->setNodeAttribute("shape", "record");
        //_scene->setNodeAttribute("style", "filled");
        //_scene->setNodeAttribute("fillcolor", "white");
        //_scene->setNodeAttribute("height", "1.2");
        _scene->setEdgeAttribute("minlen", "2");
        //_scene->setEdgeAttribute("weight", "0.5");
        //_scene->setEdgeAttribute("dir", "both");
        //Add some nodes
        QGVNode *node1 = _scene->addNode("<f0> left|<f1> middle|<f2> right");
//        node1->setIcon(QImage(":/icons/Gnome-System-Run-64.png"));
        QGVNode *node2 = _scene->addNode("hello\nworld |{ <here> b |{c|d|e}| <there>f}| g | h");
//        node2->setIcon(QImage(":/icons/Gnome-Network-Transmit-64.png"));
        QGVNode *node3 = _scene->addNode("<f0> one|<f1> two");
////        node3->setIcon(QImage(":/icons/Gnome-Network-Transmit-64.png"));

        QGVNode *node4 = _scene->addNode("hello\nworld2 |{ b |{c|<here> d|e}| f}| g | h");
//        node4->setIcon(QImage(":/icons/Gnome-Stock-Person-64.png"));
        QGVNode *node5 = _scene->addNode("SWITCH");
//        node5->setIcon(QImage(":/icons/Gnome-Network-Server-64.png"));
//
//        //Add some edges
        _scene->addEdge(node1, node2, "TTL")->setAttribute("headport", "here:n");
        _scene->addEdge(node1, node2, "SERIAL");
        _scene->addEdge(node1, node3, "RAZ")->setAttribute("color", "blue");
        _scene->addEdge(node2, node3, "SECU")->setAttribute("tailport", "there:s");
////
        _scene->addEdge(node2, node4, "STATUS")->setAttribute("color", "red");

        _scene->addEdge(node4, node3, "ACK")->setAttribute("color", "red");
////
        _scene->addEdge(node4, node2, "TBIT");
        _scene->addEdge(node4, node2, "ETH");
        _scene->addEdge(node4, node2, "RS232");

        _scene->addEdge(node4, node5, "ETH1");
        _scene->addEdge(node2, node5, "ETH2");

        QGVSubGraph *sgraph = _scene->addSubGraph("SUB1");
        sgraph->setAttribute("label", "OFFICE");

        QGVNode *snode1 = sgraph->addNode("PC0152");
        QGVNode *snode2 = sgraph->addNode("PC0153");

        _scene->addEdge(snode1, snode2, "RT7");

        _scene->addEdge(node3, snode1, "GB8");
        _scene->addEdge(node3, snode2, "TS9");


        QGVSubGraph *ssgraph = sgraph->addSubGraph("SUB2");
        ssgraph->setAttribute("label", "DESK");
        _scene->addEdge(snode1, ssgraph->addNode("PC0155"), "S10");

        //Layout scene
        _scene->applyLayout();

        //Fit in view
        ui->graphicsView->fitInView(_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
Example #2
0
void MainWindow::drawGraph(Graph &graph)
{
    initScene();
    if(graph.nodesCount() == 0)
        return;

    layoutSubgraph = ui->actionSubgraph->isChecked();


    //Configure scene attributes
    //scene->setGraphAttribute("label", "yarp-viz");
    scene->setGraphAttribute("splines", layoutStyle.c_str()); //curved, polyline, line. ortho
    scene->setGraphAttribute("rankdir", "LR");
    scene->setGraphAttribute("bgcolor", "#2e3e56");
    //scene->setGraphAttribute("concentrate", "true"); //Error !
    scene->setGraphAttribute("nodesep", "0.4");
    scene->setGraphAttribute("ranksep", "0.5");
    //scene->setNodeAttribute("shape", "box");
    scene->setNodeAttribute("style", "filled");
    scene->setNodeAttribute("fillcolor", "gray");
    scene->setNodeAttribute("height", "1.0");
    scene->setEdgeAttribute("minlen", "2.0");
    //scene->setEdgeAttribute("dir", "both");

    // drawing nodes
    // create a map between graph nodes and their visualization
    //std::map<const Vertex*, QGVNode*> nodeSet;

    //adding all machine subgraphs
    pvertex_const_iterator itr;
    const pvertex_set& vertices = graph.vertices();
    int countChild =0;
    for(itr = vertices.begin(); itr!=vertices.end(); itr++) {
        dynamic_cast<GraphicVertex*>(*itr)->setGraphicItem(nullptr);
        const Property& prop = (*itr)->property;
        QGVSubGraph *sgraph;
        if(dynamic_cast<MachineVertex*>(*itr))
        {
            string hostname =  prop.find("hostname").asString();
            if(layoutSubgraph) {
                std::stringstream key;
                key<<hostname;
                if(sceneSubGraphMap[key.str()] == nullptr)
                {
                    sgraph = scene->addSubGraph(prop.toString().c_str());
                    sceneSubGraphMap[key.str()] = sgraph;
                    dynamic_cast<GraphicVertex*>(*itr)->setGraphicItem(sgraph);
                    sgraph->setVertex(*itr);
                    //sgraph->setAttribute("label", prop.find("name").asString().c_str());
                    sgraph->setAttribute("color", "#FFFFFF");
                    sgraph->setAttribute("label", prop.find("hostname").toString().c_str());
                    string host = prop.find("os").asString();
                    if(host == "Linux")
                        sgraph->setIcon(QImage(":/icons/resources/Linux-icon.png"));
                    else if(host == "Windows")
                        sgraph->setIcon(QImage(":/icons/resources/Windows-icon.png"));
                    else if(host == "Mac")
                        sgraph->setIcon(QImage(":/icons/resources/Mac-icon.png"));
                    else
                        sgraph->setIcon(QImage(":/icons/resources/Gnome-System-Run-64.png"));
                    std::string endNodeName = key.str() + ".end";
                    QGVNode * node = sgraph->addNode(endNodeName.c_str());
                    node->setAttribute("shape", "circle");
                    node->setAttribute("height", "0.0000000000001"); //a subgraph cannot be empty, adding fake hidden node
                    node->setAttribute("fixedsize", "true");
                    node->setAttribute("label", "");
                    node->setAttribute("fillcolor", "#2e3e56");
                    node->setAttribute("color", "#2e3e56");
                    node->setAttribute("node_type", "end");
                    node->setAttribute("rawname", endNodeName.c_str());
                    sceneNodeMap[endNodeName] = node;
                }
            }
        }
    }


    // adding all process subgraphs

    for(itr = vertices.begin(); itr!=vertices.end(); itr++) {
        dynamic_cast<GraphicVertex*>(*itr)->setGraphicItem(nullptr);
        const Property& prop = (*itr)->property;
        QGVSubGraph *sgraph;
        if(dynamic_cast<ProcessVertex*>(*itr) && !prop.find("hidden").asBool())
        {
            int randNum = rand()%(16777214 - 0 + 1) + 0;
            stringstream hexStream;
            hexStream<<std::hex<< randNum;
            string hexRandNum ="#" + hexStream.str();
            string name =  std::string(prop.find("name").asString().c_str()) + std::to_string(countChild);
            if(layoutSubgraph)
            {
                std::stringstream key;
                key<<prop.find("hostname").asString();
                QGVSubGraph *sgraphParent = sceneSubGraphMap[key.str()];
                if(sgraphParent == nullptr || (!ui->actionDebugMode->isChecked() && name.find("yarplogger") != string::npos))
                {
                    continue;
                }
                sgraph = sgraphParent->addSubGraph(name.c_str());
                countChild++;
            }
            else
            {
                continue;
            }

            std::stringstream label;
            label << "   " << prop.find("name").asString().c_str()
                  << " (" << prop.find("pid").asInt32() << ")   ";
            sgraph->setAttribute("shape", "box");
            sgraph->setAttribute("label", label.str().c_str());
            if(prop.check("color")) {
                sgraph->setAttribute("fillcolor", prop.find("color").asString().c_str());
                sgraph->setAttribute("color", prop.find("color").asString().c_str());
            }else {
                sgraph->setAttribute("fillcolor", "#a5cf80");
                sgraph->setAttribute("color", "#a5cf80");
            }
            sgraph->setAttribute("colorOfTheProcess", hexRandNum.c_str());
            //nodeSet[*itr] = node;
            dynamic_cast<GraphicVertex*>(*itr)->setGraphicItem(sgraph);
            sgraph->setVertex(*itr);
            std::stringstream keyProcess;
            keyProcess<<prop.find("hostname").asString()<<prop.find("pid").asInt32();
            std::string endNodeName = keyProcess.str() + ".end";
            QGVNode * node = sgraph->addNode(endNodeName.c_str());
            node->setAttribute("shape", "circle");
            node->setAttribute("height", "0.0000000000001"); //a subgraph cannot be empty, adding fake hidden node
            node->setAttribute("fixedsize", "true");
            node->setAttribute("label", "");
            node->setAttribute("fillcolor", "#2e3e56");
            node->setAttribute("color", "#2e3e56");
            node->setAttribute("node_type", "end");
            node->setAttribute("rawname", endNodeName.c_str());
            sceneNodeMap[endNodeName] = node;
            sceneSubGraphMap[keyProcess.str()]= sgraph;
        }
    }

    // adding port nodes
    //pvertex_const_iterator itr;
    //const pvertex_set& vertices = graph.vertices();
    int portCounts = 0;

    for(itr = vertices.begin(); itr!=vertices.end(); itr++) {
        const Property& prop = (*itr)->property;
        string portName = prop.find("name").asString();
        if(dynamic_cast<PortVertex*>(*itr)) {
            PortVertex* pv = dynamic_cast<PortVertex*>(*itr);
            ProcessVertex* v = (ProcessVertex*) pv->getOwner();
            if(ui->actionHideDisconnectedPorts->isChecked() && pv->property.find("orphan").asBool())
                continue;
            if(!ui->actionDebugMode->isChecked() && (portName.find("/log") != string::npos || portName.find("/yarplogger") != string::npos ))
                continue;
            std::stringstream key;
            if(v->property.find("hidden").asBool())
            {
                pv->property.put("hidden",true);
                updateNodeWidgetItems();
                continue;
            }
            else if(prop.find("hidden").asBool())
                continue;
            QGVNode *node;
            QString colorProcess;
            if(layoutSubgraph) {
                key<<v->property.find("hostname").asString()<<v->property.find("pid").asInt32();
                QGVSubGraph *sgraph = sceneSubGraphMap[key.str()];
                if(sgraph)
                {
                    node =  sgraph->addNode(portName.c_str());
                    if(ui->actionColorMode->isChecked())
                    {
                        QColor color(sgraph->getAttribute("colorOfTheProcess"));
                        if(color.lightness()<100)
                            node->setAttribute("labelfontcolor","#ffffff");
                        colorProcess = sgraph->getAttribute("colorOfTheProcess");
                    }
                }
                else
                    node =  scene->addNode(portName.c_str());
            }
            else
                node =  scene->addNode(portName.c_str());
            node->setAttribute("shape", "ellipse");
            if(prop.check("color")) {
                node->setAttribute("fillcolor", prop.find("color").asString().c_str());
                node->setAttribute("color", prop.find("color").asString().c_str());
            }
            else if(!colorProcess.isEmpty())
            {
                node->setAttribute("fillcolor", colorProcess);
                node->setAttribute("color", colorProcess);
            }
            else {
                node->setAttribute("fillcolor", "#edad56");
                node->setAttribute("color", "#edad56");
            }
            //nodeSet[*itr] = node;
            dynamic_cast<GraphicVertex*>(*itr)->setGraphicItem(node);
            node->setVertex(*itr);
            portCounts++;
        }
    }

    // arrange the nodes deifferently if they are not port nodes
    if(portCounts == 0) {
        scene->setGraphAttribute("nodesep", "0.5");
        scene->setGraphAttribute("ranksep", "1.5");
    }

    for(itr = vertices.begin(); itr!=vertices.end(); itr++) {
        const Vertex &v1 = (**itr);
        for(size_t i=0; i<v1.outEdges().size(); i++) {
            const Edge& edge = v1.outEdges()[i];
            const Vertex &v2 = edge.second();
            string targetName = v2.property.find("name").asString();
            if(!ui->actionDebugMode->isChecked() && targetName.find("/yarplogger") != string::npos)
                continue;
            //yInfo()<<"Drawing:"<<v1.property.find("name").asString()<<" -> "<<v2.property.find("name").asString();
            // add ownership edges
            if(!v1.property.find("hidden").asBool() && !v2.property.find("hidden").asBool()) {
                if(edge.property.find("type").asString() == "ownership" &&
                        edge.property.find("dir").asString() != "unknown") {
                    continue;
                }

                if(edge.property.find("type").asString() == "connection") {                    
                    //QGVEdge* gve = scene->addEdge(nodeSet[&v1], nodeSet[&v2],
                    //                               edge.property.find("carrier").asString().c_str());
                    string lable="";
                    if(!ui->actionHideConnectionsLable->isChecked())
                        lable = edge.property.find("carrier").asString();
                    QGVEdge* gve = scene->addEdge((QGVNode*)((GraphicVertex*)&v1)->getGraphicItem(),
                                                  (QGVNode*)((GraphicVertex*)&v2)->getGraphicItem(),
                                                   lable.c_str());
                    QosStyle::PacketPriorityLevel level=
                            (QosStyle::PacketPriorityLevel)edge.property.find("FromPacketPriority").asInt32();
                    switch (level) {
                    case QosStyle::PacketPriorityNormal:
                        gve->setAttribute("color", "white");
                        break;
                    case QosStyle::PacketPriorityHigh:
                        gve->setAttribute("color", "orange");
                        break;
                    case QosStyle::PacketPriorityCritical:
                        gve->setAttribute("color", "red");
                        break;
                    case QosStyle::PacketPriorityLow:
                        gve->setAttribute("color", "yellow");
                        break;
                    default:
                        gve->setAttribute("color", "white");
                        break;
                    }
                    //gve->setToolTip("hello!");
                    gve->setEdge(&edge);
                }
            }
        }
    }

    //Layout scene
    scene->applyLayout();

    //Fit in view
    ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);

    //QGVSubGraph *ssgraph = sgraph->addSubGraph("SUB2");
    //ssgraph->setAttribute("label", "DESK");
    //scene->addEdge(snode1, ssgraph->addNode("PC0155"), "S10");
}