void GraphGraphicsView::reposition()
{
    if (nodes.length() == 0)
        return;
    qsrand(QTime::currentTime().msec());
    for (int i = 0; i < lines.length(); ++i) {
        scene.removeItem(lines[i]);
    }
    lines.clear();
    for (int i = 0; i < arrows.length(); ++i) {
        scene.removeItem(arrows[i]);
    }
    arrows.clear();
    for (int i = 0; i < weights.size(); ++i) {
        scene.removeItem(weights[i]);
    }
    weights.clear();

    for (int i = 0; i < nodes.length(); ++i)
    {
        NodeGraphicsItem* item = nodes[i];
        qreal powX;
        qreal powY = qPow(-1,i);
        if (qrand() % 2)
            powX = -1;
        else
            powX = 1;
        item->setPos(powX * (qrand() % 450), powY * (qrand() % 220));

        bool tooClose = false;
        for (int j = 0; j < nodes.length(); ++j)
        {
            if (i != j)
            {
                tooClose = tooClose || itemLevelTooClose(nodes[j], item);
                //qDebug() << "tooClose " << tooClose;
            }
        }
        while(tooClose)
        {
            tooClose = false;
            //qDebug() << "too close";
            item->setPos(powX * (qrand() % 450), powY * (qrand() % 220));
            for (int j = 0; j < nodes.length(); ++j) {
                if (i != j)
                {
                    tooClose = tooClose || itemLevelTooClose(nodes[j], item);
                }
            }
        }
    }

    drawAllEdges();
    drawWeights();
}
int GUIDisplayVisitor::draw(Component* component, int xIndex, int& yIndex)
{
    int x = xIndex * (Component::MAX_WIDTH_PIXEL + Component::PADDING);
    int y = yIndex * (Component::MAX_HEIGHT_PIXEL + Component::PADDING);
    int middleY = y + component->getHeight() / 2;
    list<Component*> children = component->getNodeList();
    list<int> childrenMiddleY;
    // traversal
    if (!component->isCollapse()) {
        for (list<Component*>::iterator iterator = children.begin(); iterator != children.end(); iterator++) {
            Component* child = *iterator;
            int childMiddleY = draw(child, xIndex + 1, yIndex);
            childrenMiddleY.push_back(childMiddleY);
        }
    }
    // create self GraphicsItem
    NodeGraphicsItem* nodeGraphicsItem = new NodeGraphicsItem(_mindMapPresentationModel, component);
    if (children.size() == 0) {
        yIndex++;
    } else {
        // calc average line
        middleY = 0;
        for (list<int>::iterator iterator = childrenMiddleY.begin(); iterator != childrenMiddleY.end(); iterator++) {
            middleY += ((float) * iterator) / childrenMiddleY.size();
        }
        // create line between self and child
        for (list<int>::iterator iterator = childrenMiddleY.begin(); iterator != childrenMiddleY.end(); iterator++) {
            int rightX = x + nodeGraphicsItem->boundingRect().right();
            int leftX = x + nodeGraphicsItem->boundingRect().left();
            QGraphicsLineItem* line = new QGraphicsLineItem(rightX, middleY, leftX + Component::MAX_WIDTH_PIXEL + Component::PADDING, *iterator);
            _graphicsScene->addItem(line);
        }
    }
    nodeGraphicsItem->setPos(x, middleY - component->getHeight() / 2);
    component->setX(0);
    component->setY(0);
    _nodeGraphicsItems.push_back(nodeGraphicsItem);
    _graphicsScene->addItem(nodeGraphicsItem);
    return middleY;
}