void ToolBarAreaImpl::setNewToolBar(ToolBar* toolBar, vector<int>& numStrechablesOfRow)
{
    if(DEBUG_MODE){
        cout << "ToolBarAreaImpl::setNewToolBar()" << endl;
    }
    
    if(toolBar){

        ToolBarRowPtr toolBarRow;
        int rowIndex = -1;
        int width = toolBar->minimumSizeHint().width();

        for(size_t i=0; i < toolBarRows.size(); ++i){
            ToolBarRowPtr existingRow = toolBarRows[i];
            ToolBar* lastToolBar = existingRow->toolBars.back();
            if(lastToolBar){
                QRect r = lastToolBar->geometry();
                int lastX = r.x() + r.width();
                int lastSpace = self->width() - lastX;
                if(width <= lastSpace){
                    toolBar->desiredX = lastX + 1;
                    if(toolBar->isStretchable()){
                        width = std::min(toolBar->sizeHint().width(), lastSpace);
                    }
                    toolBarRow = existingRow;
                    rowIndex = i;
                    break;
                }
            }
        }
        if(!toolBarRow){
            toolBar->desiredX = 0;
            rowIndex = toolBarRows.size();
            toolBarRow.reset(new ToolBarRow(self));
            toolBarRows.push_back(toolBarRow);
            numStrechablesOfRow.push_back(0);
        }
        
        toolBarRow->toolBars.push_back(toolBar);
        if(toolBar->isStretchable()){
            numStrechablesOfRow[rowIndex]++;
        }
        toolBar->setParent(self);
        toolBar->setGeometry(toolBar->desiredX, 0, width, toolBar->sizeHint().height());
        toolBar->show();
    }
}
void ToolBarAreaImpl::setNewToolBars()
{
    vector<int> numStrechablesOfRow;

    for(size_t i=0; i < newToolBars.size(); ++i){
        setNewToolBar(newToolBars[i], numStrechablesOfRow);
    }
    newToolBars.clear();

    for(size_t i=0; i < numStrechablesOfRow.size(); ++i){
        int numStrechables = numStrechablesOfRow[i];
        if(numStrechables > 0){
            ToolBarRowPtr& row = toolBarRows[i];
            ToolBar* lastToolBar = row->toolBars.back();
            QRect r = lastToolBar->geometry();
            int space = self->width() - (r.x() + r.width());
            if(space > 0){
                expandStrechableBars(row, (space / numStrechables), 0, 0, numStrechables, space);
            }
        }
    }
}