static void
findKnobsOnSameLine(const KnobsVec& knobs,
                    const KnobPtr& ref,
                    KnobsVec& knobsOnSameLine)
{
    int idx = -1;

    for (U32 k = 0; k < knobs.size(); ++k) {
        if (knobs[k] == ref) {
            idx = k;
            break;
        }
    }
    assert(idx != -1);
    if (idx < 0) {
        return;
    }
    ///find all knobs backward that are on the same line.
    int k = idx - 1;
    KnobPtr parent = ref->getParentKnob();

    while ( k >= 0 && !knobs[k]->isNewLineActivated() ) {
        if (parent) {
            assert(knobs[k]->getParentKnob() == parent);
            knobsOnSameLine.push_back(knobs[k]);
        } else {
            if ( !knobs[k]->getParentKnob() &&
                 !dynamic_cast<KnobPage*>( knobs[k].get() ) &&
                 !dynamic_cast<KnobGroup*>( knobs[k].get() ) ) {
                knobsOnSameLine.push_back(knobs[k]);
            }
        }
        --k;
    }

    ///find all knobs forward that are on the same line.
    k = idx;
    while ( k < (int)(knobs.size() - 1) && !knobs[k]->isNewLineActivated() ) {
        if (parent) {
            assert(knobs[k + 1]->getParentKnob() == parent);
            knobsOnSameLine.push_back(knobs[k + 1]);
        } else {
            if ( !knobs[k + 1]->getParentKnob() &&
                 !dynamic_cast<KnobPage*>( knobs[k + 1].get() ) &&
                 !dynamic_cast<KnobGroup*>( knobs[k + 1].get() ) ) {
                knobsOnSameLine.push_back(knobs[k + 1]);
            }
        }
        ++k;
    }
}
void
DockablePanelPrivate::initializeKnobVector(const KnobsVec& knobs,
                                           QWidget* lastRowWidget)
{
    std::list<boost::shared_ptr<KnobPage> > pages;
    KnobsVec regularKnobs;

    //Extract pages first
    for (U32 i = 0; i < knobs.size(); ++i) {
        KnobPage *isPage = dynamic_cast<KnobPage*>( knobs[i].get() );
        if (isPage) {
            pages.push_back( boost::dynamic_pointer_cast<KnobPage>(knobs[i]) );
            continue;
        } else {
            regularKnobs.push_back(knobs[i]);
        }
    }
    for (std::list<boost::shared_ptr<KnobPage> >::iterator it = pages.begin(); it != pages.end(); ++it) {
        //create page
        (void)findKnobGuiOrCreate( *it, true, 0, KnobsVec() );

        KnobsVec children = (*it)->getChildren();
        KnobsVec::iterator prev = children.end();
        for (KnobsVec::iterator it2 = children.begin(); it2 != children.end(); ++it2) {
            bool makeNewLine = true;
            KnobGroup *isGroup = dynamic_cast<KnobGroup*>( it2->get() );

            ////The knob  will have a vector of all other knobs on the same line.
            KnobsVec knobsOnSameLine;


            //If the knob is dynamic (i:e created after the initial creation of knobs)
            //it can be added as part of a group defined earlier hence we have to insert it at the proper index.
            KnobPtr parentKnob = (*it2)->getParentKnob();
            KnobGroup* isParentGroup = dynamic_cast<KnobGroup*>( parentKnob.get() );


            if (!isGroup) {
                if ( ( prev != children.end() ) && !(*prev)->isNewLineActivated() ) {
                    makeNewLine = false;
                }
                if (isParentGroup) {
                    KnobsVec groupsiblings = isParentGroup->getChildren();
                    findKnobsOnSameLine(groupsiblings, *it2, knobsOnSameLine);
                } else {
                    findKnobsOnSameLine(children, *it2, knobsOnSameLine);
                }
            }

            KnobGuiPtr newGui = findKnobGuiOrCreate(*it2, makeNewLine, lastRowWidget, knobsOnSameLine);

            ///childrens cannot be on the same row than their parent
            if (!isGroup && newGui) {
                lastRowWidget = newGui->getFieldContainer();
            }


            std::vector<KnobPtr>::iterator foundRegular = std::find(regularKnobs.begin(), regularKnobs.end(), *it2);
            if ( foundRegular != regularKnobs.end() ) {
                regularKnobs.erase(foundRegular);
            }


            if ( prev == children.end() ) {
                prev = children.begin();
            } else {
                ++prev;
            }
        }
    }

    //For knobs left,  create them
    KnobsVec::iterator prev = regularKnobs.end();
    for (KnobsVec::iterator it = regularKnobs.begin(); it != regularKnobs.end(); ++it) {
        bool makeNewLine = true;
        KnobGroup *isGroup = dynamic_cast<KnobGroup*>( it->get() );

        ////The knob  will have a vector of all other knobs on the same line.
        KnobsVec knobsOnSameLine;


        //If the knob is dynamic (i:e created after the initial creation of knobs)
        //it can be added as part of a group defined earlier hence we have to insert it at the proper index.
        KnobPtr parentKnob = (*it)->getParentKnob();
        KnobGroup* isParentGroup = dynamic_cast<KnobGroup*>( parentKnob.get() );


        if (!isGroup) {
            if ( ( prev != regularKnobs.end() ) && !(*prev)->isNewLineActivated() ) {
                makeNewLine = false;
            }

            KnobPage* isParentPage = dynamic_cast<KnobPage*>( parentKnob.get() );
            if (isParentPage) {
                KnobsVec children = isParentPage->getChildren();
                findKnobsOnSameLine(children, (*it), knobsOnSameLine);
            } else if (isParentGroup) {
                KnobsVec children = isParentGroup->getChildren();
                findKnobsOnSameLine(children, (*it), knobsOnSameLine);
            } else {
                findKnobsOnSameLine(regularKnobs, (*it), knobsOnSameLine);
            }
        }

        KnobGuiPtr newGui = findKnobGuiOrCreate(*it, makeNewLine, lastRowWidget, knobsOnSameLine);

        ///childrens cannot be on the same row than their parent
        if (!isGroup && newGui) {
            lastRowWidget = newGui->getFieldContainer();
        }

        if ( prev == regularKnobs.end() ) {
            prev = regularKnobs.begin();
        } else {
            ++prev;
        }
    }

    _publicInterface->refreshTabWidgetMaxHeight();
} // DockablePanelPrivate::initializeKnobVector