VirtualBox

Changeset 49582 in vbox


Ignore:
Timestamp:
Nov 20, 2013 12:33:17 PM (11 years ago)
Author:
vboxsync
Message:

FE/Qt: Medium Manager: Using manual tree search mechanism instead of Qt iterators which glitches with randomly populated tree.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/medium
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumManager.cpp

    r49581 r49582  
    146146
    147147
    148 /* Medium-item iterator: */
    149 class UIMediumItemIterator : public QTreeWidgetItemIterator
     148/** Functor allowing to check if passed UIMediumItem is suitable by ID. */
     149class CheckIfSuitableByID : public CheckIfSuitableBy
    150150{
    151151public:
    152 
    153     UIMediumItemIterator(QTreeWidget *pTree)
    154         : QTreeWidgetItemIterator(pTree) {}
    155 
    156     UIMediumItem* operator*()
    157     {
    158         QTreeWidgetItem *pItem = QTreeWidgetItemIterator::operator*();
    159         return pItem && pItem->type() == UIMediumItem::Type ?
    160             static_cast<UIMediumItem*>(pItem) : 0;
    161     }
    162 
    163     UIMediumItemIterator& operator++()
    164     {
    165         return static_cast<UIMediumItemIterator&>(QTreeWidgetItemIterator::operator++());
    166     }
     152    /** Constructor. */
     153    CheckIfSuitableByID(const QString &strID) : m_strID(strID) {}
     154
     155private:
     156    /** Determines whether passed UIMediumItem is suitable by ID. */
     157    bool isItSuitable(UIMediumItem *pItem) const { return pItem->id() == m_strID; }
     158    /** Holds the ID to compare to. */
     159    QString m_strID;
     160};
     161
     162/** Functor allowing to check if passed UIMediumItem is suitable by @a state. */
     163class CheckIfSuitableByState : public CheckIfSuitableBy
     164{
     165public:
     166    /** Constructor. */
     167    CheckIfSuitableByState(KMediumState state) : m_state(state) {}
     168
     169private:
     170    /** Determines whether passed UIMediumItem is suitable by @a state. */
     171    bool isItSuitable(UIMediumItem *pItem) const { return pItem->state() == m_state; }
     172    /** Holds the @a state to compare to. */
     173    KMediumState m_state;
    167174};
    168175
     
    345352    {
    346353        pTree = treeWidget(type);
    347         pMediumItem = searchItem(pTree, strMediumID);
     354        pMediumItem = searchItem(pTree, CheckIfSuitableByID(strMediumID));
    348355        if (pMediumItem)
    349356            break;
     
    437444    switch (medium.type())
    438445    {
    439         case UIMediumType_HardDisk: pMediumItem = searchItem(mTwHD, medium.id()); break;
    440         case UIMediumType_DVD:      pMediumItem = searchItem(mTwCD, medium.id()); break;
    441         case UIMediumType_Floppy:   pMediumItem = searchItem(mTwFD, medium.id()); break;
     446        case UIMediumType_HardDisk: pMediumItem = searchItem(mTwHD, CheckIfSuitableByID(medium.id())); break;
     447        case UIMediumType_DVD:      pMediumItem = searchItem(mTwCD, CheckIfSuitableByID(medium.id())); break;
     448        case UIMediumType_Floppy:   pMediumItem = searchItem(mTwFD, CheckIfSuitableByID(medium.id())); break;
    442449        default: AssertFailed();
    443450    }
     
    14681475}
    14691476
    1470 UIMediumItem* UIMediumManager::searchItem(QTreeWidget *pTree, const QString &strId) const
    1471 {
    1472     /* Make sure passed ID is valid: */
    1473     if (strId.isNull())
     1477UIMediumItem* UIMediumManager::searchItem(QTreeWidget *pTree, const CheckIfSuitableBy &functor) const
     1478{
     1479    /* Make sure argument is valid: */
     1480    if (!pTree)
    14741481        return 0;
    14751482
    1476     /* Iterate other all the mediums of passed tree: */
    1477     UIMediumItemIterator iterator(pTree);
    1478     while (*iterator)
    1479     {
    1480         if ((*iterator)->id() == strId)
    1481             return *iterator;
    1482         ++iterator;
    1483     }
     1483    /* Return wrapper: */
     1484    return searchItem(pTree->invisibleRootItem(), functor);
     1485}
     1486
     1487UIMediumItem* UIMediumManager::searchItem(QTreeWidgetItem *pParentItem, const CheckIfSuitableBy &functor) const
     1488{
     1489    /* Make sure argument is valid: */
     1490    if (!pParentItem)
     1491        return 0;
     1492
     1493    /* Verify passed item if it is of 'medium' type too: */
     1494    if (UIMediumItem *pMediumParentItem = toMediumItem(pParentItem))
     1495        if (functor.isItSuitable(pMediumParentItem))
     1496            return pMediumParentItem;
     1497
     1498    /* Iterate other all the children: */
     1499    for (int iChildIndex = 0; iChildIndex < pParentItem->childCount(); ++iChildIndex)
     1500        if (UIMediumItem *pMediumChildItem = toMediumItem(pParentItem->child(iChildIndex)))
     1501            if (UIMediumItem *pRequiredMediumChildItem = searchItem(pMediumChildItem, functor))
     1502                return pRequiredMediumChildItem;
    14841503
    14851504    /* Null by default: */
     
    14971516    /* First try to create item under corresponding parent: */
    14981517    if (medium.parentID() != UIMedium::nullID())
    1499         if (UIMediumItem *pRoot = searchItem(pTree, medium.parentID()))
     1518        if (UIMediumItem *pRoot = searchItem(pTree, CheckIfSuitableByID(medium.parentID())))
    15001519            pMediumItem = new UIMediumItem(medium, pRoot);
    15011520    /* Else just create item as top-level one: */
     
    15751594            if (fCheckRest)
    15761595            {
    1577                 *pfInaccessible = false;
    1578 
    1579                 QTreeWidget *pTree = pMediumItem->treeWidget();
    1580 
    1581                 /* Find the first pfInaccessible item to be in charge: */
    1582                 UIMediumItemIterator it(pTree);
    1583                 for (; *it; ++ it)
    1584                 {
    1585                     if (*it != pMediumItem && (*it)->state() == KMediumState_Inaccessible)
    1586                     {
    1587                         *pfInaccessible = true;
    1588                         break;
    1589                     }
    1590                 }
     1596                /* Find the first KMediumState_Inaccessible item to be in charge: */
     1597                UIMediumItem *pInaccessibleMediumItem =
     1598                    searchItem(pMediumItem->treeWidget(), CheckIfSuitableByState(KMediumState_Inaccessible));
     1599                *pfInaccessible = !!pInaccessibleMediumItem;
    15911600            }
    15921601
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumManager.h

    r49581 r49582  
    3636class UIEnumerationProgressBar;
    3737class UIMedium;
     38
     39/** Functor interface allowing to check if passed UIMediumItem is suitable. */
     40class CheckIfSuitableBy
     41{
     42public:
     43    /** Determines whether passed UIMediumItem is suitable. */
     44    virtual bool isItSuitable(UIMediumItem *pItem) const = 0;
     45};
    3846
    3947/* Medium Manager Dialog: */
     
    134142    void setCurrentItem(QTreeWidget *pTree, QTreeWidgetItem *pItem);
    135143    UIMediumItem* toMediumItem(QTreeWidgetItem *pItem) const;
    136     UIMediumItem* searchItem(QTreeWidget *pTree, const QString &strId) const;
     144    UIMediumItem* searchItem(QTreeWidget *pTree, const CheckIfSuitableBy &functor) const;
     145    UIMediumItem* searchItem(QTreeWidgetItem *pParentItem, const CheckIfSuitableBy &functor) const;
    137146    UIMediumItem* createHardDiskItem(QTreeWidget *pTree, const UIMedium &medium) const;
    138147
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette