Esempio n. 1
0
int DrawViewCollection::countChildren()
{
    //Count the children recursively if needed
    int numChildren = 0;

    const std::vector<App::DocumentObject *> &views = Views.getValues();
    for(std::vector<App::DocumentObject *>::const_iterator it = views.begin(); it != views.end(); ++it) {

        if((*it)->getTypeId().isDerivedFrom(TechDraw::DrawViewCollection::getClassTypeId())) {
            TechDraw::DrawViewCollection *viewCollection = static_cast<TechDraw::DrawViewCollection *>(*it);
            numChildren += viewCollection->countChildren() + 1;
        } else {
            numChildren += 1;
        }
    }
    return numChildren;
}
void MDIViewPage::updateDrawing(bool forceUpdate)
{
    // We cannot guarantee if the number of graphical representations (QGIVxxxx) have changed so check the number
    // Why?
    const std::vector<QGIView *> &graphicsList = m_view->getViews();
    const std::vector<App::DocumentObject*> &pageChildren  = pageGui->getPageObject()->Views.getValues();

    // Count total # DocumentObjects in Page
    unsigned int docObjCount = 0;
    for(std::vector<App::DocumentObject*>::const_iterator it = pageChildren.begin(); it != pageChildren.end(); ++it) {
        App::DocumentObject *docObj = *it;
        if(docObj->getTypeId().isDerivedFrom(TechDraw::DrawViewCollection::getClassTypeId())) {
            TechDraw::DrawViewCollection *collection = dynamic_cast<TechDraw::DrawViewCollection *>(docObj);
            docObjCount += collection->countChildren(); // Include self
        }
        docObjCount += 1;
    }

    if(graphicsList.size() < docObjCount) {
        // there are more DocumentObjects than graphical representations (QGIVxxxx's)
        // Find which DocumentObjects have no graphical representation (QGIVxxxx)
        // Iterate over DocumentObjects without graphical representations and create the QGIVxxxx
        // TODO think of a better algorithm to deal with any changes to views list
        std::vector<App::DocumentObject*> notFnd;
        findMissingViews(pageChildren, notFnd);
        for(std::vector<App::DocumentObject*>::const_iterator it = notFnd.begin(); it != notFnd.end(); ++it) {
            attachView(*it);
        }
    } else if(graphicsList.size() > docObjCount) {
        // There are more graphical representations (QGIVxxxx) than DocumentObjects
        // Remove the orphans
        std::vector<QGIView *>::const_iterator itGraphics = graphicsList.begin();
        std::vector<QGIView *> newGraphicsList;
        bool fnd = false;
        while(itGraphics != graphicsList.end()) {
            fnd = orphanExists((*itGraphics)->getViewName(), pageChildren);
            if(fnd) {
                newGraphicsList.push_back(*itGraphics);
            } else {
                if (m_view->scene() == (*itGraphics)->scene()) {
                    (*itGraphics)->hide();
                    m_view->scene()->removeItem(*itGraphics);
                } else {   // this "shouldn't" happen, but it does
                    Base::Console().Log("ERROR - MDIViewPage::updateDrawing - %s already removed from QGraphicsScene\n",
                                        (*itGraphics)->getViewName());
                }
                deleteItems.append(*itGraphics); // delete in the destructor when completly safe. TEMP SOLUTION
            }
            itGraphics++;
        }

        // Update the QGVPage (QGraphicsView) list of QGIVxxxx
        m_view->setViews(newGraphicsList);
    }

    // Update all the QGIVxxxx
    const std::vector<QGIView *> &upviews = m_view->getViews();
    for(std::vector<QGIView *>::const_iterator it = upviews.begin(); it != upviews.end(); ++it) {
        if((*it)->getViewObject()->isTouched() ||
           forceUpdate) {
            (*it)->updateView(forceUpdate);
        }
    }
}