void ProfileScene::addVertex(QPoint mousePos)
{
    //if clicked on an edge, add point on this edge
    QGraphicsLineItem* currentEdge = 0;
    bool foundEdge(false);
    Profile* profile = currentProfile;
    Vertex* currentVertex = profile->getProfileVertex();

    while(currentVertex->getNeighbor2() != 0) {
        currentEdge = currentVertex->getEdge2();
        if (currentEdge->isUnderMouse()) {
            foundEdge = true;
            break;
        }
        currentVertex = currentVertex->getNeighbor2();
    }

    float w = mousePos.x();
    float z = mousePos.y();


    if (foundEdge) {
        QGraphicsEllipseItem* ellipse = new QGraphicsEllipseItem(w - vertexRadius, z - vertexRadius, vertexRadius * 2.0f, vertexRadius * 2.0f);
        QRectF thisSize = this->sceneRect();
        Utils::adjustCoordinatesSceneTo3D(w, z, thisSize.width(), thisSize.height());

        //build the new vertex and add the edges between the new neighbour
        Vertex* newVertex = new Vertex(w, z);
        newVertex->setEllipse(ellipse);

        Vertex* nextVertex = currentVertex->getNeighbor2();
        Vertex* previousVertex = currentVertex;
        QGraphicsLineItem* edge1 = previousVertex->replaceNeighbour(nextVertex, newVertex);
        QGraphicsLineItem* edge2 = nextVertex->replaceNeighbour(previousVertex, newVertex);


        //set all neighbour/edges of the new vertex
        newVertex->setNeighbor1(previousVertex);//addNeighbor
        newVertex->setEdge1(edge1);
        newVertex->setNeighbor2(nextVertex);//addNeighbor
        newVertex->setEdge2(edge2);


        //finally show the ellipse and delete the old edge
        this->addItem(ellipse);
        this->addItem(edge1);
        this->addItem(edge2);
        this->removeItem(currentEdge);
        delete currentEdge;

        // tell the mesh to generate new point/triangle
        mesh->setUpdateOnMesh();
    } else {
        // we don't have clicked on an edge, we will put the point at the current position

        Vertex * newVertex = new Vertex(0,0);

        newVertex->setEllipse(new QGraphicsEllipseItem(w - vertexRadius, z - vertexRadius, vertexRadius * 2.0f, vertexRadius*2.0f));
        QRectF thisSize = this->sceneRect();
        Utils::adjustCoordinatesSceneTo3D(w, z, thisSize.width(), thisSize.height());
        newVertex->setX(w);
        newVertex->setY(z);

        this->currentProfile->addVertexEnd(newVertex);
        this->addItem(newVertex->getEllipse());
        this->addItem(newVertex->getEdge1());

        // tell the mesh to generate new point/triangle
        mesh->setUpdateOnMesh();
    }
}