VirtualBox

Changeset 101692 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Oct 31, 2023 4:08:48 PM (13 months ago)
Author:
vboxsync
Message:

FE/Qt: bugref:10513: Reworking tree-view UISettingsSelector to use QSortFilterProxyModel for automatic sorting routine.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/UISettingsSelector.cpp

    r101691 r101692  
    3535#include <QLayout>
    3636#include <QPainter>
     37#include <QSortFilterProxyModel>
    3738#include <QTabWidget>
    3839#include <QToolButton>
     
    245246    /** Returns item with certain @a strLink. */
    246247    QModelIndex findItem(const QString &strLink);
    247 
    248     /** Sorts the contents of model by @a iColumn and @a enmOrder. */
    249     virtual void sort(int iColumn = 0, Qt::SortOrder enmOrder = Qt::AscendingOrder) RT_OVERRIDE;
    250248
    251249private:
     
    438436}
    439437
    440 void UISelectorDelegate::paint(QPainter *pPainter, const QStyleOptionViewItem &option, const QModelIndex &index) const
     438void UISelectorDelegate::paint(QPainter *pPainter, const QStyleOptionViewItem &option, const QModelIndex &proxyIndex) const
    441439{
    442440    /* Sanity checks: */
    443441    AssertPtrReturnVoid(pPainter);
     442    AssertReturnVoid(proxyIndex.isValid());
     443
     444    /* Acquire model: */
     445    const QSortFilterProxyModel *pProxyModel = qobject_cast<const QSortFilterProxyModel*>(proxyIndex.model());
     446    AssertPtrReturnVoid(pProxyModel);
     447    const UISelectorModel *pModel = qobject_cast<const UISelectorModel*>(pProxyModel->sourceModel());
     448    AssertPtrReturnVoid(pModel);
     449
     450    /* Acquire model index: */
     451    const QModelIndex index = pProxyModel->mapToSource(proxyIndex);
    444452    AssertReturnVoid(index.isValid());
    445 
    446     /* Acquire model: */
    447     const UISelectorModel *pModel = qobject_cast<const UISelectorModel*>(index.model());
    448453
    449454    /* Acquire item properties: */
     
    725730}
    726731
    727 void UISelectorModel::sort(int /* iColumn */, Qt::SortOrder enmOrder)
    728 {
    729     /* Prepare empty list for sorted items: */
    730     QList<UISelectorTreeViewItem*> sortedItems;
    731 
    732     /* For each of items: */
    733     const int iItemCount = m_pRootItem->childCount();
    734     for (int iItemPos = 0; iItemPos < iItemCount; ++iItemPos)
    735     {
    736         /* Get iterated item/id: */
    737         UISelectorTreeViewItem *pItem = m_pRootItem->childItem(iItemPos);
    738         const int iID = pItem->id();
    739 
    740         /* Gather suitable position in the list of sorted items: */
    741         int iInsertPosition = 0;
    742         for (; iInsertPosition < sortedItems.size(); ++iInsertPosition)
    743         {
    744             /* Get sorted item/id: */
    745             UISelectorTreeViewItem *pSortedItem = sortedItems.at(iInsertPosition);
    746             const int iSortedID = pSortedItem->id();
    747 
    748             /* Apply sorting rule: */
    749             if (   ((enmOrder == Qt::AscendingOrder) && (iID < iSortedID))
    750                 || ((enmOrder == Qt::DescendingOrder) && (iID > iSortedID)))
    751                 break;
    752         }
    753 
    754         /* Insert iterated item into sorted position: */
    755         sortedItems.insert(iInsertPosition, pItem);
    756     }
    757 
    758     /* Update corresponding model-indexes: */
    759     m_pRootItem->setChildren(sortedItems);
    760     beginMoveRows(root(), 0, iItemCount - 1, root(), 0);
    761     endMoveRows();
    762 }
    763 
    764732Qt::ItemFlags UISelectorModel::flags(const QModelIndex &specifiedIndex) const
    765733{
     
    790758{
    791759    /* Sanity check: */
    792     UISelectorModel *pModel = qobject_cast<UISelectorModel*>(model());
     760    QSortFilterProxyModel *pProxyModel = qobject_cast<QSortFilterProxyModel*>(model());
     761    AssertPtrReturn(pProxyModel, QITreeView::minimumSizeHint());
     762    UISelectorModel *pModel = qobject_cast<UISelectorModel*>(pProxyModel->sourceModel());
    793763    AssertPtrReturn(pModel, QITreeView::minimumSizeHint());
    794764
     
    820790{
    821791    /* Configure tree-view: */
     792    setSortingEnabled(true);
     793    sortByColumn(0, Qt::AscendingOrder);
    822794    setFrameShape(QFrame::NoFrame);
    823795    viewport()->setAutoFillBackground(false);
     
    925897    , m_pTreeView(0)
    926898    , m_pModel(0)
     899    , m_pModelProxy(0)
    927900{
    928901    prepare();
     
    990963{
    991964    int iID = -1;
    992     const QModelIndex index = m_pTreeView->currentIndex();
     965    const QModelIndex proxyIndex = m_pTreeView->currentIndex();
     966    const QModelIndex index = m_pModelProxy->mapToSource(proxyIndex);
    993967    if (index.isValid())
    994968        iID = m_pModel->data(index, UISelectorModel::R_ItemId).toString().toInt();
     
    1008982{
    1009983    const QModelIndex index = m_pModel->findItem(iID);
    1010     if (index.isValid())
    1011         m_pTreeView->setCurrentIndex(index);
    1012 }
    1013 
    1014 void UISettingsSelectorTreeView::sltHandleCurrentChanged(const QModelIndex &index,
     984    const QModelIndex proxyIndex = m_pModelProxy->mapFromSource(index);
     985    if (proxyIndex.isValid())
     986        m_pTreeView->setCurrentIndex(proxyIndex);
     987}
     988
     989void UISettingsSelectorTreeView::sltHandleCurrentChanged(const QModelIndex &proxyIndex,
    1015990                                                         const QModelIndex & /* previousIndex */)
    1016991{
     992    const QModelIndex index = m_pModelProxy->mapToSource(proxyIndex);
    1017993    if (index.isValid())
    1018994    {
     
    10331009        if (m_pModel)
    10341010        {
    1035             m_pTreeView->setModel(m_pModel);
    1036             m_pTreeView->setRootIndex(m_pModel->root());
     1011            /* Create proxy-model: */
     1012            m_pModelProxy = new QSortFilterProxyModel(m_pTreeView);
     1013            if (m_pModelProxy)
     1014            {
     1015                /* Configure proxy-model: */
     1016                m_pModelProxy->setSortRole(UISelectorModel::R_ItemId);
     1017                m_pModelProxy->setSourceModel(m_pModel);
     1018                m_pTreeView->setModel(m_pModelProxy);
     1019            }
     1020            const QModelIndex proxyIndex = m_pModelProxy->mapFromSource(m_pModel->root());
     1021            m_pTreeView->setRootIndex(proxyIndex);
    10371022        }
    10381023
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/UISettingsSelector.h

    r101691 r101692  
    4141class QAction;
    4242class QActionGroup;
     43class QSortFilterProxyModel;
    4344class QTabWidget;
    4445class QTreeWidget;
     
    184185
    185186    /** Holds the tree-view instance. */
    186     UISelectorTreeView *m_pTreeView;
     187    UISelectorTreeView    *m_pTreeView;
    187188    /** Holds the model instance. */
    188     UISelectorModel    *m_pModel;
     189    UISelectorModel       *m_pModel;
     190    /** Holds the proxy-model instance. */
     191    QSortFilterProxyModel *m_pModelProxy;
    189192};
    190193
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