VirtualBox

Changeset 72086 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
May 3, 2018 12:17:54 PM (7 years ago)
Author:
vboxsync
Message:

FE/Qt: ​bugref:6699 Add a QSortFilterProxyModel between file table vie and model to enable filtering and sorting

Location:
trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestControlFileModel.cpp

    r71832 r72086  
    3232#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    3333
     34const char* UIGuestControlFileModel::strUpDirectoryString = "..";
     35
     36UIGuestControlFileProxyModel::UIGuestControlFileProxyModel(QObject *parent /* = 0 */)
     37    :QSortFilterProxyModel(parent)
     38{
     39}
     40
     41bool UIGuestControlFileProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
     42{
     43    QVariant leftData = sourceModel()->data(left);
     44    QVariant rightData = sourceModel()->data(right);
     45
     46    if (leftData.canConvert(QMetaType::QString) && rightData.canConvert(QMetaType::QString))
     47    {
     48
     49        if (leftData == UIGuestControlFileModel::strUpDirectoryString)
     50            return true && (sortOrder() == Qt::AscendingOrder);
     51        else if (rightData == UIGuestControlFileModel::strUpDirectoryString)
     52            return false && (sortOrder() == Qt::AscendingOrder);
     53    }
     54
     55    return QSortFilterProxyModel::lessThan(left, right);
     56}
    3457
    3558UIGuestControlFileModel::UIGuestControlFileModel(QObject *parent)
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestControlFileModel.h

    r71639 r72086  
    2121/* Qt includes: */
    2222#include <QAbstractItemModel>
     23#include <QSortFilterProxyModel>
    2324
    2425/* GUI includes: */
     
    2930class UIGuestControlFileTable;
    3031
     32/** A QSortFilterProxyModel extension used in file tables. Modifies some
     33  * of the base class behavior like lessThan(..) */
     34class UIGuestControlFileProxyModel : public QSortFilterProxyModel
     35{
     36
     37    Q_OBJECT;
     38
     39public:
     40
     41    UIGuestControlFileProxyModel(QObject *parent = 0);
     42
     43protected:
     44
     45    bool lessThan(const QModelIndex &left, const QModelIndex &right) const /* override */;
     46};
     47
    3148/** UIGuestControlFileModel serves as the model for a file structure.
    32     it supports a tree level hierarchy which can be displayed with
    33     QTableView and/or QTreeView. Note the file structure data is not
    34     kept by the model but rather by the containing widget which also servers
    35     as the interface to functionality this model provides.*/
     49  * it supports a tree level hierarchy which can be displayed with
     50  * QTableView and/or QTreeView. Note the file structure data is not
     51  * kept by the model but rather by the containing widget which also servers
     52  * as the interface to functionality this model provides.*/
    3653class UIGuestControlFileModel : public QAbstractItemModel
    3754{
     
    6178    void endReset();
    6279    bool insertRows(int position, int rows, const QModelIndex &parent);
     80    static const char* strUpDirectoryString;
    6381
    6482private:
     
    7290
    7391#endif /* !___UIGuestControlFileModel_h___ */
    74 
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestControlFileTable.cpp

    r71861 r72086  
    2929# include <QGridLayout>
    3030# include <QMenu>
     31# include <QSortFilterProxyModel>
    3132# include <QTextEdit>
    3233# include <QPushButton>
     
    691692    if (!isDirectory())
    692693        return false;
    693     if (data(0) == QString(".."))
     694    if (data(0) == UIGuestControlFileModel::strUpDirectoryString)
    694695        return true;
    695696    return false;
     
    739740    :QIWithRetranslateUI<QWidget>(pParent)
    740741    , m_pRootItem(0)
    741     , m_pView(0)
    742     , m_pModel(0)
    743742    , m_pLocationLabel(0)
    744743    , m_pCopy(0)
     
    746745    , m_pPaste(0)
    747746    , m_pPropertiesDialog(0)
     747    , m_pModel(0)
     748    , m_pView(0)
     749    , m_pProxyModel(0)
    748750    , m_pMainLayout(0)
    749751    , m_pLocationComboBox(0)
     
    827829        return;
    828830
     831    m_pProxyModel = new UIGuestControlFileProxyModel(this);
     832    if (!m_pProxyModel)
     833        return;
     834    m_pProxyModel->setSourceModel(m_pModel);
    829835
    830836    m_pView = new UIGuestControlFileView;
     
    832838    {
    833839        m_pMainLayout->addWidget(m_pView, 2, 0, 5, 5);
    834         m_pView->setModel(m_pModel);
     840        m_pView->setModel(m_pProxyModel);
     841        //m_pView->setModel(m_pModel);
    835842        m_pView->setItemDelegate(new UIFileDelegate);
     843        m_pView->setSortingEnabled(true);
     844        m_pView->sortByColumn(0, Qt::AscendingOrder);
    836845
    837846        connect(m_pView, &UIGuestControlFileView::doubleClicked,
    838847                this, &UIGuestControlFileTable::sltItemDoubleClicked);
    839 
    840848        connect(m_pView, &UIGuestControlFileView::sigGoUp,
    841849                this, &UIGuestControlFileTable::sltGoUp);
     
    9931001    if (!index.isValid() || !m_pView)
    9941002        return;
    995     m_pView->setRootIndex(index);
     1003    m_pView->setRootIndex(m_pProxyModel->mapFromSource(index));
    9961004    m_pView->clearSelection();
    9971005
     
    10011009        updateCurrentLocationEdit(item->path());
    10021010    }
    1003     m_pModel->signalUpdate();
     1011    /** @todo check if we really need this and if not remove it */
     1012    //m_pModel->signalUpdate();
    10041013}
    10051014
     
    10221031    startItem->setIsOpened(false);
    10231032    populateStartDirectory(startItem);
    1024     m_pView->setRootIndex(m_pModel->rootIndex());
     1033
    10251034    m_pModel->signalUpdate();
    10261035    updateCurrentLocationEdit(startPath);
     1036    m_pView->setRootIndex(m_pProxyModel->mapFromSource(m_pModel->rootIndex()));
    10271037}
    10281038
     
    10501060        }
    10511061    }
    1052 
    10531062}
    10541063
     
    10581067    if (parent)
    10591068
    1060     /* Make sure we have a ".." item within directories, and make sure it is not there for the start dir: */
     1069    /* Make sure we have an item representing up directory, and make sure it is not there for the start dir: */
    10611070    if (isDirectoryMap)
    10621071    {
    1063         if (!map.contains("..")  && !isStartDir)
     1072        if (!map.contains(UIGuestControlFileModel::strUpDirectoryString)  && !isStartDir)
    10641073        {
    10651074            QList<QVariant> data;
    1066             data << ".." << 4096 << "";
     1075            data << UIGuestControlFileModel::strUpDirectoryString << 4096 << "";
    10671076            UIFileTableItem *item = new UIFileTableItem(data, parent, FileObjectType_Directory);
    10681077            item->setIsOpened(false);
    1069             map.insert("..", item);
     1078            map.insert(UIGuestControlFileModel::strUpDirectoryString, item);
    10701079        }
    1071         else if (map.contains("..")  && isStartDir)
     1080        else if (map.contains(UIGuestControlFileModel::strUpDirectoryString)  && isStartDir)
    10721081        {
    1073             map.remove("..");
     1082            map.remove(UIGuestControlFileModel::strUpDirectoryString);
    10741083        }
    10751084    }
     
    10871096    if (!index.isValid() ||  !m_pModel || !m_pView)
    10881097        return;
    1089     goIntoDirectory(index);
     1098    QModelIndex nIndex = m_pProxyModel ? m_pProxyModel->mapToSource(index) : index;
     1099    goIntoDirectory(nIndex);
    10901100}
    10911101
     
    10941104    if (!m_pView || !m_pModel)
    10951105        return;
    1096     QModelIndex currentRoot = m_pView->rootIndex();
     1106    QModelIndex currentRoot = currentRootIndex();
     1107
    10971108    if (!currentRoot.isValid())
    10981109        return;
     
    11901201    if (!m_pView || !m_pModel)
    11911202        return;
    1192     QModelIndex currentIndex = m_pView->rootIndex();
     1203    QModelIndex currentIndex = currentRootIndex();
    11931204
    11941205    UIFileTableItem *treeItem = indexData(currentIndex);
     
    12041215        readDirectory(treeItem->path(), treeItem, isRootDir);
    12051216    m_pModel->endReset();
    1206     m_pView->setRootIndex(currentIndex);
     1217    m_pView->setRootIndex(m_pProxyModel->mapFromSource(currentIndex));
    12071218}
    12081219
     
    12181229    for(int i = 0; i < selectedItemIndices.size(); ++i)
    12191230    {
    1220         deleteByIndex(selectedItemIndices.at(i));
     1231        QModelIndex index =
     1232            m_pProxyModel ? m_pProxyModel->mapToSource(selectedItemIndices.at(i)) : selectedItemIndices.at(i);
     1233        deleteByIndex(index);
    12211234    }
    12221235    /** @todo dont refresh here, just delete the rows and update the table view: */
     
    12351248    if (selectedItemIndices.size() == 0)
    12361249        return;
    1237     UIFileTableItem *item = indexData(selectedItemIndices.at(0));
     1250    QModelIndex modelIndex =
     1251        m_pProxyModel ? m_pProxyModel->mapToSource(selectedItemIndices.at(0)) : selectedItemIndices.at(0);
     1252    UIFileTableItem *item = indexData(modelIndex);
    12381253    if (!item || item->isUpDirectory())
    12391254        return;
     
    12451260    if (!m_pModel || !m_pView)
    12461261        return;
    1247     QModelIndex currentIndex = m_pView->rootIndex();
     1262    QModelIndex currentIndex = currentRootIndex();
    12481263    if (!currentIndex.isValid())
    12491264        return;
     
    13341349    if (!pSelectionModel)
    13351350        return;
    1336     QModelIndex currentRoot = m_pView->rootIndex();
     1351    QModelIndex currentRoot = currentRootIndex();
    13371352    if (!currentRoot.isValid())
    13381353        return;
     
    13481363        if (item && item->isUpDirectory())
    13491364        {
    1350             pSelectionModel->select(index, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
     1365            QModelIndex indexToDeselect = m_pProxyModel ? m_pProxyModel->mapFromSource(index) : index;
     1366            pSelectionModel->select(indexToDeselect, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
    13511367        }
    13521368    }
     
    13601376    if (!pSelectionModel)
    13611377        return;
    1362     QModelIndex currentRoot = m_pView->rootIndex();
     1378    QModelIndex currentRoot = currentRootIndex();
    13631379    if (!currentRoot.isValid())
    13641380        return;
     
    13691385        if (!index.isValid())
    13701386            continue;
    1371         pSelectionModel->select(index, flags);
     1387        QModelIndex indexToSelect = m_pProxyModel ? m_pProxyModel->mapFromSource(index) : index;
     1388        pSelectionModel->select(indexToSelect, flags);
    13721389    }
    13731390}
     
    15481565    for(int i = 0; i < selectedItemIndices.size(); ++i)
    15491566    {
    1550         UIFileTableItem *item = static_cast<UIFileTableItem*>(selectedItemIndices.at(i).internalPointer());
     1567        QModelIndex index =
     1568            m_pProxyModel ? m_pProxyModel->mapToSource(selectedItemIndices.at(i)) : selectedItemIndices.at(i);
     1569        UIFileTableItem *item = static_cast<UIFileTableItem*>(index.internalPointer());
    15511570        if (!item)
    15521571            continue;
     
    15851604}
    15861605
    1587 /* static */ QString UIGuestControlFileTable::fileTypeString(FileObjectType type)
     1606QString UIGuestControlFileTable::fileTypeString(FileObjectType type)
    15881607{
    15891608    QString strType("Unknown");
     
    16351654}
    16361655
     1656QModelIndex UIGuestControlFileTable::currentRootIndex() const
     1657{
     1658    if (!m_pView)
     1659        return QModelIndex();
     1660    if (!m_pProxyModel)
     1661        return m_pView->rootIndex();
     1662    return m_pProxyModel->mapToSource(m_pView->rootIndex());
     1663}
     1664
    16371665#include "UIGuestControlFileTable.moc"
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestControlFileTable.h

    r71852 r72086  
    4242class QILineEdit;
    4343class QGridLayout;
     44class QSortFilterProxyModel;
    4445class QTextEdit;
    4546class QVBoxLayout;
    4647class UIFileTableItem;
    4748class UIGuestControlFileModel;
     49class UIGuestControlFileProxyModel;
    4850class UIGuestControlFileView;
    4951class UIToolBar;
     
    265267    void retranslateUi();
    266268    void updateCurrentLocationEdit(const QString& strLocation);
     269    /* @p index is for model not for 'proxy' model */
    267270    void changeLocation(const QModelIndex &index);
    268271    void initializeFileTree();
     
    279282     *  drive letters */
    280283    virtual void     determineDriveLetters() = 0;
    281     static QString   fileTypeString(FileObjectType type);
     284    QString          fileTypeString(FileObjectType type);
     285    /* @p item index is item location in model not in 'proxy' model */
    282286    void             goIntoDirectory(const QModelIndex &itemIndex);
    283287    /** Follows the path trail, opens directories as it descends */
     
    290294
    291295    UIFileTableItem         *m_pRootItem;
    292     UIGuestControlFileView  *m_pView;
    293     UIGuestControlFileModel *m_pModel;
    294296    QILabel                 *m_pLocationLabel;
    295297    QAction                 *m_pCopy;
     
    306308
    307309private slots:
    308 
     310    /* index is passed by the item view and represents the double clicked object's 'proxy' model index */
    309311    void sltItemDoubleClicked(const QModelIndex &index);
    310312    void sltGoUp();
     
    327329    void             prepareObjects();
    328330    void             prepareActions();
     331    /* @itemIndex is assumed to be 'model' index not 'proxy model' index */
    329332    void             deleteByIndex(const QModelIndex &itemIndex);
    330333    /** Returns the UIFileTableItem for path / which is a direct (and single) child of m_pRootItem */
     
    339342     *  drive letter are direct children of the start directory. On other systems start directory is '/' */
    340343    void            populateStartDirectory(UIFileTableItem *startItem);
     344    QModelIndex     currentRootIndex() const;
     345    UIGuestControlFileModel      *m_pModel;
     346    UIGuestControlFileView       *m_pView;
     347    UIGuestControlFileProxyModel *m_pProxyModel;
     348
    341349    QGridLayout     *m_pMainLayout;
    342350    QComboBox       *m_pLocationComboBox;
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestFileTable.cpp

    r71861 r72086  
    402402}
    403403
    404 /* static */ FileObjectType UIGuestFileTable::fileType(const CFsObjInfo &fsInfo)
     404FileObjectType UIGuestFileTable::fileType(const CFsObjInfo &fsInfo)
    405405{
    406406    if (fsInfo.isNull() || !fsInfo.isOk())
     
    416416}
    417417
    418 /* static */ FileObjectType UIGuestFileTable::fileType(const CGuestFsObjInfo &fsInfo)
     418FileObjectType UIGuestFileTable::fileType(const CGuestFsObjInfo &fsInfo)
    419419{
    420420    if (fsInfo.isNull() || !fsInfo.isOk())
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestFileTable.h

    r71861 r72086  
    4141protected:
    4242
    43     void retranslateUi() /* override */;
    44     virtual void readDirectory(const QString& strPath, UIFileTableItem *parent, bool isStartDir = false) /* override */;
    45     virtual void deleteByItem(UIFileTableItem *item) /* override */;
    46     virtual void goToHomeDirectory() /* override */;
    47     virtual bool renameItem(UIFileTableItem *item, QString newBaseName);
    48     virtual bool createDirectory(const QString &path, const QString &directoryName);
     43    void            retranslateUi() /* override */;
     44    virtual void    readDirectory(const QString& strPath, UIFileTableItem *parent, bool isStartDir = false) /* override */;
     45    virtual void    deleteByItem(UIFileTableItem *item) /* override */;
     46    virtual void    goToHomeDirectory() /* override */;
     47    virtual bool    renameItem(UIFileTableItem *item, QString newBaseName);
     48    virtual bool    createDirectory(const QString &path, const QString &directoryName);
    4949    virtual QString fsObjectPropertyString() /* override */;
    50     virtual void  showProperties() /* override */;
    51     virtual void determineDriveLetters() /* override */;
     50    virtual void    showProperties() /* override */;
     51    virtual void    determineDriveLetters() /* override */;
    5252
    5353private:
    5454
    55     static FileObjectType fileType(const CFsObjInfo &fsInfo);
    56     static FileObjectType fileType(const CGuestFsObjInfo &fsInfo);
     55    FileObjectType fileType(const CFsObjInfo &fsInfo);
     56    FileObjectType fileType(const CGuestFsObjInfo &fsInfo);
    5757
    5858    bool copyGuestToHost(const QString &guestSourcePath, const QString& hostDestinationPath);
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIHostFileTable.cpp

    r71861 r72086  
    2727/* GUI includes: */
    2828# include "QILabel.h"
     29# include "UIGuestControlFileModel.h"
    2930# include "UIHostFileTable.h"
    3031# include "UIVMInformationDialog.h"
     
    100101    {
    101102        const QFileInfo &entryInfo = entryList.at(i);
    102         if (entryInfo.baseName().isEmpty() || entryInfo.baseName() == "." || entryInfo.baseName() == "..")
     103        if (entryInfo.baseName().isEmpty() || entryInfo.baseName() == "." ||
     104            entryInfo.baseName() == UIGuestControlFileModel::strUpDirectoryString)
    103105            continue;
    104106        statistics.m_totalSize += entryInfo.size();
     
    164166    for (int i = 0; i < entries.size(); ++i)
    165167    {
     168
    166169        const QFileInfo &fileInfo = entries.at(i);
    167170        QList<QVariant> data;
     
    256259}
    257260
    258 /* static */ FileObjectType UIHostFileTable::fileType(const QFileInfo &fsInfo)
     261FileObjectType UIHostFileTable::fileType(const QFileInfo &fsInfo)
    259262{
    260263    if (!fsInfo.exists())
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIHostFileTable.h

    r71861 r72086  
    3434protected:
    3535
    36     static FileObjectType fileType(const QFileInfo &fsInfo);
    37     void retranslateUi() /* override */;
    38     virtual void readDirectory(const QString& strPath, UIFileTableItem *parent, bool isStartDir = false) /* override */;
    39     virtual void deleteByItem(UIFileTableItem *item) /* override */;
    40     virtual void goToHomeDirectory() /* override */;
    41     virtual bool renameItem(UIFileTableItem *item, QString newBaseName);
    42     virtual bool createDirectory(const QString &path, const QString &directoryName);
     36    FileObjectType fileType(const QFileInfo &fsInfo);
     37    void            retranslateUi() /* override */;
     38    virtual void    readDirectory(const QString& strPath, UIFileTableItem *parent, bool isStartDir = false) /* override */;
     39    virtual void    deleteByItem(UIFileTableItem *item) /* override */;
     40    virtual void    goToHomeDirectory() /* override */;
     41    virtual bool    renameItem(UIFileTableItem *item, QString newBaseName);
     42    virtual bool    createDirectory(const QString &path, const QString &directoryName);
    4343    virtual QString fsObjectPropertyString() /* override */;
    44     virtual void  showProperties() /* override */;
    45     virtual void determineDriveLetters() /* override */;
     44    virtual void    showProperties() /* override */;
     45    virtual void    determineDriveLetters() /* override */;
    4646
    4747private:
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